Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • BKAP
    • Addons
    • Admin-Bookings
    • Attribute-Lockout
    • Block-Pricing
    • Booking-Box
    • Booking-Confirmation
    • Booking-Process
    • Cancel-Order
    • Cart
    • Checkout-Process
    • Core
    • Date-Time
    • Emails
    • Functions
    • Global-Function
    • Global-Settings
    • Google-Calendar-Sync
    • ICS
    • Import-Booking
    • Labels
    • License
    • Menus
    • Meta-Boxes
    • Reschedule
    • Resources
    • Search-Widget
    • Special-Price
    • Uninstall
    • Validation
    • Variation-Lockout
    • Vendors
      • Dokan
      • WC-Vendors
    • View-Bookings
  • None

Classes

  • bkap_addon_compatibility_class
  • bkap_admin_bookings
  • bkap_approve_booking
  • bkap_attributes
  • bkap_block_booking
  • BKAP_Booking
  • bkap_booking_box_class
  • bkap_booking_confirmation
  • bkap_booking_process
  • BKAP_Bookings_View
  • bkap_calendar_sync
  • bkap_cancel_order
  • bkap_cart
  • bkap_checkout
  • bkap_common
  • BKAP_Customer_Meta_Box
  • BKAP_Details_Meta_Box
  • bkap_dokan_calendar_class
  • bkap_dokan_class
  • bkap_dokan_orders_class
  • bkap_dokan_products_class
  • bkap_edit_bookings_class
  • BKAP_Email_Booking_Cancelled
  • BKAP_Email_Booking_Confirmed
  • BKAP_Email_Booking_Rescheduled_Admin
  • BKAP_Email_Imported_Event
  • BKAP_Email_Manager
  • BKAP_Email_New_Booking
  • BKAP_Gcal
  • BKAP_Gcal_Event
  • bkap_gcal_sync_settings
  • bkap_global_settings
  • BKAP_Google_Events_View
  • bkap_ics
  • bkap_license
  • bkap_load_scripts_class
  • BKAP_OPC_Addon
  • BKAP_Payment_Gateway
  • BKAP_Product_Resource
  • bkap_rescheduled_order_class
  • BKAP_Resource_Details_Meta_Box
  • BKAP_Save_Meta_Box
  • bkap_special_booking_price
  • bkap_timeslot_price
  • bkap_validation
  • bkap_variations
  • BKAP_Vendors
  • bkap_wc_vendors
  • bkap_wcv_product
  • Booking_Information_In_Voucher_Template
  • Class_Bkap_Product_Resource
  • Custom_WooCommerce_Widget_Product_Search
  • EDD_BOOK_Plugin_Updater
  • global_menu
  • import_bookings
  • product_gcal_settings
  • view_bookings
  • WAPBK_Import_Bookings_Table
  • WAPBK_View_Bookings_Table
  • woocommerce_booking

Functions

  • addDayswithdate
  • bkap_400_create_meta
  • bkap_400_recurring_data
  • bkap_400_update_db_tab
  • bkap_400_update_enable_specific
  • bkap_400_update_enable_week_blocking
  • bkap_400_update_holidays
  • bkap_400_update_ranges
  • bkap_400_update_recurring_lockout
  • bkap_400_update_settings
  • bkap_400_update_special
  • bkap_400_update_specific
  • bkap_410_create_meta
  • bkap_410_update_block_pricing_option_values_in_new_way
  • bkap_410_update_fixed_blocks
  • bkap_410_update_price_ranges
  • bkap_410_update_settings
  • bkap_420_add_gcal_posts
  • bkap_420_add_gcal_tour_posts
  • bkap_420_gcal_batch_size
  • bkap_420_gcal_meta
  • bkap_420_update_settings
  • bkap_add_days_to_date
  • bkap_array_of_given_date_range
  • bkap_booked_resources
  • bkap_booking_posts_for_resource
  • bkap_calculate_bookings_for_resource
  • bkap_check_booking_available
  • bkap_check_day_booking_available
  • bkap_check_for_max_date
  • bkap_check_for_min_date
  • bkap_check_holiday
  • bkap_check_in_custom_holiday_range
  • bkap_check_resource_available
  • bkap_check_resource_booked_in_date_range
  • bkap_db_420_1
  • bkap_db_420_2
  • bkap_delete_event_from_gcal
  • bkap_delete_folder_and_files
  • bkap_delete_options
  • bkap_delete_tables
  • bkap_find_matching_product_variation
  • bkap_generate_bookable_data
  • bkap_get_book_arrays
  • bkap_get_book_t
  • bkap_get_booked
  • bkap_get_booked_checkout
  • bkap_get_custom_posts
  • bkap_get_dateToCal
  • bkap_get_day_between_Week
  • bkap_get_db_count
  • bkap_get_escapeString
  • bkap_get_lockout
  • bkap_get_month_range
  • bkap_get_post_meta
  • bkap_get_posted_availability
  • bkap_get_total_timeslot_maximum_booking
  • bkap_get_week_range
  • bkap_import_events_cron
  • bkap_insert_event_to_gcal
  • bkap_manual_db_update_v420
  • bkap_orderby_date_key
  • bkap_save_resources
  • bkap_sort_by_from_time
  • bkap_update_po_file
  • bkap_vendors_get_bookings
  • bkap_widgets_init
  • bkapSortByPriority
  • calback_bkap_max_date
  • check_in_fixed_block_booking
  • check_in_range
  • check_in_range_abp
  • check_in_range_holidays
  • check_in_range_weekdays
  • create_posts
  • get_availability_for_date
  • get_booked_multiple
  • get_bookings_for_date
  • get_bookings_for_range
  • get_date_lockout
  • get_slot_availability
  • get_slot_lockout
  • get_standard_decimal_thousand_separator_price
  • is_booking_active
  • woocommerce_bkap_add_cron_schedule
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 
<?php 
/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class for Booking meta box in Add/Edit Product page.
 *
 * @author   Tyche Softwares
 * @package  BKAP/Booking-Box
 * @category Classes
 */

include_once( 'bkap-common.php' );
include_once( 'includes/product-calendar-sync-settings.php' );

/**
 * Class for Booking meta box
 * @class bkap_booking_box_class
 */

class bkap_booking_box_class {
    
    /**
     * This function updates the booking settings for each product in the wp_postmeta table in the database. 
     * It will be called when update / publish button clicked on admin side.
     * 
     * @param integer $post_id - Post ID
     * @param array $post - Contains the data 
     * 
     * @hook woocommerce_process_product_meta
     * @since 1.0
     * 
     **/

    public static function bkap_process_bookings_box( $post_id, $post ) {
        // Save Bookings
        $product_bookings   =   array();
        $duplicate_of       =   bkap_common::bkap_get_product_id( $post_id );
        $booking_settings   =   get_post_meta(  $duplicate_of, 'woocommerce_booking_settings', true );

        $booking_settings   = (array) apply_filters( 'bkap_save_product_settings', $booking_settings, $duplicate_of );
        update_post_meta( $duplicate_of, 'woocommerce_booking_settings', $booking_settings );
    }
    
    /**
     * This function adds a meta box for booking settings on product page.
     * 
     * @hook add_meta_boxes
     * @since 1.0
     **/

    public static function bkap_booking_box() {
        
        add_meta_box( 'woocommerce-booking', 
                      __( 'Booking', 'woocommerce-booking' ),
                      array( 'bkap_booking_box_class', 'bkap_meta_box' ),
                      'product',
                      'normal',
                      'core'
                    );
    }

    /**
     * Adds JS code for Vertical tabs in the Booking meta box.
     *
     * @hook admin_footer
     * @since 2.2
     */

    public static function bkap_print_js() {
        
        if ( get_post_type() == 'product' ) {
            
            ?>
            <script type="text/javascript">
            jQuery(document).ready(function () {
                 jQuery("#bkap-tabbed-nav").zozoTabs({
                       
                       orientation: "vertical",
                       position: "top-left",
                       size: "medium",
                       animation: {
                            easing: "easeInOutExpo",
                            duration: 400,
                            effects: "none"
                       },
                 });
            });
            </script>
            <?php
        }
    }
                   
    /**
     * Displays the settings for the product in the Booking meta box.
     * @global object $post WP_Post 
     * @global object $wpdb Global wpdb object
     *
     * @since 1.0
     **/

    public static function bkap_meta_box() {
        global $post, $wpdb;

        $duplicate_of       = bkap_common::bkap_get_product_id( $post->ID );        
        $booking_settings   = get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );        

        do_action( 'bkap_add_resource_section', $duplicate_of );
        
        $post_type = get_post_type( $duplicate_of );        
        
        ?>
        <div id='bkap-tabbed-nav'>
            <ul>
                <li class = "bkap_general" >
                    <a id="addnew" class="bkap_tab"><i class="fa fa-cog" aria-hidden="true"></i><?php _e( 'General', 'woocommerce-booking' );?> </a>
                </li>
                <li class = "bkap_availability" >
                    <a id="settings" class="bkap_tab"><i class="fa fa-calendar" aria-hidden="true"></i><?php _e( 'Availability', 'woocommerce-booking' ); ?></a>
                </li>
                <?php
                    do_action( 'bkap_add_tabs', $duplicate_of ); 
                ?>
            </ul>
            <div>
                <!-- General tab starts here -->  
                
                <div id="booking_options">
                    
                    <!-- Enable Booking div starts here -->           
                    
                    <div id="enable_booking_options_section" class="booking_options-flex-main">
                        
                        <?php do_action( 'bkap_before_enable_booking', $duplicate_of );?>    
                        
                        <div class="booking_options-flex-child">
                            <label for="booking_enable_date"> <?php _e( 'Enable Booking', 'woocommerce-booking' );?> </label>
                        </div>
                        
                        <?php 
                            $booking_settings       =   get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );
                        
                            $enable_date            = '';
                            $only_day               = ''; // the only days radio button
                            $single_days            = '';      // single day radio button
                            $date_time              = ''; // date & time radio button
                            $multiple_days          = ''; // multiple days radio button
                            $display_only_day       = 'display:none;'; // display only days div
                            $multiple_days_setup    = 'style="display:none;"'; // fields in the settings tab for multiple days
                            $purchase_without_date  = '';
                            
                            if ( isset( $booking_settings[ 'booking_enable_date' ] ) && $booking_settings[ 'booking_enable_date' ] == 'on' ) {
                                   
                                   $enable_date         = 'checked';
                                   $only_day            = 'checked'; 
                                   $single_days         = 'checked'; 
                                   $date_time           = ''; 
                                   $multiple_days       = ''; 
                                   $display_only_day    = '';
                                   $specific_date_table = 'display:none;';
                                   
                                   if( isset( $booking_settings[ 'booking_specific_booking' ]) && $booking_settings[ 'booking_specific_booking' ] == 'on' ){
                                       $specific_date_table = '';
                                   }
                                   
                            }
                            if ( isset( $booking_settings[ 'booking_enable_multiple_day' ] ) && 'on' == $booking_settings[ 'booking_enable_multiple_day' ] ) {
                                
                                $only_day               = 'checked'; // the only days radio button
                                $multiple_days          = 'checked'; // multiple days radio button
                                $single_days            = ''; // single day radio button
                                $date_time              = ''; // date & time radio button
                                $purchase_without_date  = 'display:none';
                                $multiple_days_setup    = 'display="block"';
                            
                            } else if ( isset( $booking_settings[ 'booking_enable_time' ] ) && 'on' == $booking_settings[ 'booking_enable_time' ] ) {
                                
                                $only_day           = '';
                                $date_time          = 'checked';
                                $display_only_day   = 'display:none;';
                            }
                        ?>
                        
                        <div class="booking_options-flex-child">
                            <label class="bkap_switch">
                              <input type="checkbox" id="booking_enable_date" name="booking_enable_date" <?php echo $enable_date;?> >
                              <div class="bkap_slider round"></div>
                            </label>
                        </div>
                        
                        <div class="booking_options-flex-child bkap_help_class">
                            <img class="help_tip" width="16" height="16"  data-tip="<?php _e( 'Enable Booking Date on Products Page', 'woocommerce-booking' );?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                        </div>
                        
                    </div>
                    <hr/>                    
                    <!-- Booking Type div starts here -->
                    <?php do_action( 'bkap_before_booking_method_select', $duplicate_of );?>    
                    <div id="enable_booking_types_section" class="booking_types-flex-main">
                        
                        <div class="booking_types-flex-child">
                            <label for="booking_enable_type"> <?php _e( 'Booking Type', 'woocommerce-booking' );?> </label>
                        </div>
                        
                        <div class="booking_types-flex-child"> 
                            <div class="booking_types-flex-child-day">
                                <input type="radio" id="enable_booking_day_type" name="booking_enable_type" class="enable_booking_type" value="booking_enable_only_day" <?php echo $only_day;?>></input>
                                <label for="enable_booking_day_type"> <?php _e( 'Only Day', 'woocommerce-booking' );?> </label>
                            </div>
                            
                            <div class="booking_types-flex-child-day">
                               <input type="radio" id="enable_booking_day_and_time_type" name="booking_enable_type" class="enable_booking_type" value="booking_enable_date_and_time" <?php echo $date_time;?>></input>
                               <label for="enable_booking_day_and_time_type"> <?php _e( 'Date & Time', 'woocommerce-booking' );?> </label>
                            </div>
                        </div>
                        
                        <div class="booking_types-flex-child bkap_help_class">
                            <img class="help_tip" width="16" height="16"  data-tip="<?php _e( 'Choose booking type for your business', 'woocommerce-booking' );?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                        </div>
                         
                    </div>
                    
                    <!-- Div for Single Day and Multiple Days starts here -->
                    <div id="enable_only_day_booking_section" style="margin-top:20px;<?php echo $display_only_day; ?>" class="only_day_booking_section_flex_main" >
                        <div class="only_day_booking_section_flex_child1"></div>
                        <div class="only_day_booking_section_flex_child2">
                            <div class="only_day_booking_section_flex_child21">
                                <input type="radio" id="enable_booking_single" name="booking_enable_only_day" class="enable_only_day" value="booking_enable_single_day" <?php echo $single_days;?>></input>
                                <label for="enable_booking_single"> <?php _e( 'Single Day', 'woocommerce-booking' );?> </label>
                            </div>
                            <div class="only_day_booking_section_flex_child22">
                                <input type="radio" id="enable_booking_multiple_days" name="booking_enable_only_day" class="enable_only_day" value="booking_enable_multiple_days" <?php echo $multiple_days;?>></input>
                                <label for="enable_booking_multiple_days"> <?php _e( 'Multiple Nights', 'woocommerce-booking' );?> </label>
                            </div>
                        </div>
                        <div class="only_day_booking_section_flex_child3 bkap_help_class"></div>
                        
                    </div>
                    
                    <!-- Descrpition of the selected booking method will be display -->
                    <p class="show-booking-day-description"></p>
                    
                    <hr/>
                    <div id="enable_inline_calendar_section" class="booking_options-flex-main" style="margin-top:15px;margin-bottom:15px;">
                                
                        <div class="booking_options-flex-child">
                            <label for="enable_inline_calendar"> <?php _e( 'Enable Inline Calendar', 'woocommerce-booking' );?> </label>
                        </div>
                                
                        <?php 
                        $enable_inline_calendar = '';
                        if( isset( $booking_settings[ 'enable_inline_calendar' ] ) && $booking_settings[ 'enable_inline_calendar' ] == 'on' ) {
                            $enable_inline_calendar = 'checked';
                        }
                        ?>

                        <div class="booking_options-flex-child">
                            <label class="bkap_switch">
                              <input type="checkbox" id="enable_inline_calendar" name="enable_inline_calendar" <?php echo $enable_inline_calendar;?> >
                              <div class="bkap_slider round"></div>
                            </label>
                        </div>
                        
                        <div class="booking_options-flex-child bkap_help_class">
                            <img class="help_tip" width="16" height="16"  data-tip="<?php _e( 'Enable Inline Calendar on Products Page', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                        </div>
                        
                    </div>
                    
                    <?php do_action( 'bkap_before_purchase_without_date', $duplicate_of ); ?>

                    <div id="purchase_wo_date_section" class="booking_options-flex-main" style="<?php echo $purchase_without_date;?>">
                        
                        <div class="booking_options-flex-child">
                            <label for="booking_purchase_without_date"> <?php _e( 'Purchase without choosing a date', 'woocommerce-booking' ); ?> </label>
                        </div>
                                        
                        <?php 
                            $date_show = '';
                            if( isset( $booking_settings['booking_purchase_without_date'] ) && $booking_settings['booking_purchase_without_date'] == 'on' ) {
                                   $without_date = 'checked';
                            } else {
                                   $without_date = '';
                            }
                        ?>

                        <div class="booking_options-flex-child">
                            <label class="bkap_switch">
                              <input type="checkbox" id="booking_purchase_without_date" name="booking_purchase_without_date" <?php echo $without_date;?> >
                              <div class="bkap_slider round"></div>
                            </label>
                        </div>
                        
                        <div class="booking_options-flex-child bkap_help_class">
                            <img class="help_tip" width="16" height="16"  data-tip="<?php _e( 'Enables your customers to purchase without choosing a date. Select this option if you want the ADD TO CART button always visible on the product page. Cannot be applied to products that require confirmation.', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                        </div>
                        
                    </div>

                    <?php 
                        do_action( 'bkap_after_purchase_wo_date', $duplicate_of );
                        do_action( 'bkap_before_product_holidays', $duplicate_of );
                        do_action( 'bkap_after_product_holidays', $duplicate_of );
                    ?>

                    <hr style="margin-top:20px;" />
                    
                    <?php 
                        if( isset( $post_type ) && $post_type === 'product' ) {
                            self::bkap_save_button( 'bkap_save_booking_options' );
                        }
                    ?>

                    <div id='general_update_notification' style='display:none;'></div>          
                </div>
                <!-- Booking Options tab ends here -->
                 
                <div id="booking_settings" style="display:none;">
                              
                    <table class="form-table">
                
                        <?php do_action( 'bkap_before_minimum_days', $duplicate_of ); ?>
                        
                        <tr>
                            <th style="width:50%;">
                                <label for="booking_minimum_number_days"><?php _e( 'Advance Booking Period (in hours)', 'woocommerce-booking' ); ?></label>
                            </th>
                            <td>
                                <?php 
                                $min_days = 0;
                                if ( isset( $booking_settings['booking_minimum_number_days'] ) && $booking_settings['booking_minimum_number_days'] != "" ) {
                                    $min_days = $booking_settings['booking_minimum_number_days'];
                                }
                                ?>
                                <input type="number" style="width:90px;" name="booking_minimum_number_days" id="booking_minimum_number_days" min="0" max="9999" value="<?php echo sanitize_text_field( $min_days, true );?>" >
                            </td>
                            <td>
                                <img class="help_tip" width="16" height="16" data-tip="<?php _e( 'Enable Booking after X number of hours from the current time. The customer can select a booking date/time slot that is available only after the minimum hours that are entered here. For example, if you need 12 hours advance notice for a booking, enter 12 here.', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                            </td>
                        </tr>

                        <?php do_action( 'bkap_before_number_of_dates', $duplicate_of ); ?>
                        
                        <tr>
                            <th style="width:50%;">
                                <label for="booking_maximum_number_days"><?php _e( 'Number of dates to choose', 'woocommerce-booking' ); ?></label>
                            </th>
                            <td>
                                <?php 
                                $max_date = "";
                                $readonly_no_of_dates_to_choose = "";
                                
                                // if custom range is added then readonly number of dates to choose field. 
                                if( isset( $booking_settings[ 'booking_date_range' ] ) && $booking_settings[ 'booking_date_range' ] != "" && count( $booking_settings[ 'booking_date_range' ]) > 0 ){
                                    $readonly_no_of_dates_to_choose = "readonly";
                                }
                                 
                                if ( isset( $booking_settings[ 'booking_maximum_number_days' ] ) && $booking_settings[ 'booking_maximum_number_days' ] != "" ) {
                                    $max_date = $booking_settings[ 'booking_maximum_number_days' ];
                                } else {
                                    $max_date = "30";
                                }
                                ?>
                                <input type="number" style="width:90px;" name="booking_maximum_number_days" id="booking_maximum_number_days" min="0" max="9999" value="<?php echo sanitize_text_field( $max_date, true );?>" <?php echo $readonly_no_of_dates_to_choose; ?> >
                            </td>
                            <td>
                               <img class="help_tip" width="16" height="16" data-tip="<?php _e( 'The maximum number of booking dates you want to be available for your customers to choose from. For example, if you take only 2 months booking in advance, enter 60 here.', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                            </td>
                        </tr>
                        <?php 
                        do_action( 'bkap_after_number_of_dates', $duplicate_of );
                        ?>
                        <tr class="multiple_days_setup" <?php echo $multiple_days_setup; ?>>
                            <th style="width:50%;">
                                <label for="booking_lockout_date"><?php _e( 'Maximum Bookings On Any Date', 'woocommerce-booking' ); ?></label>
                            </th>
                            <td>
                                <?php 
                                $lockout_date = "";
                                if ( isset( $booking_settings['booking_date_lockout'] ) ) {
                                       $lockout_date = $booking_settings['booking_date_lockout'];
                                       //sanitize_text_field( $lockout_date, true )
                                } else {
                                       $lockout_date = "60";
                                }
                                ?>
                                <input type="number" style="width:90px;" name="booking_lockout_date" id="booking_lockout_date" min="0" max="9999" value="<?php echo sanitize_text_field( $lockout_date, true );?>" >
                            </td>
                            <td>
                                <img class="help_tip" width="16" height="16" data-tip="<?php _e( 'Set this field if you want to place a limit on maximum bookings on any given date. If you can manage up to 15 bookings in a day, set this value to 15. Once 15 orders have been booked, then that date will not be available for further bookings.', 'woocommerce-booking' );?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                            </td>
                        </tr>

                        <?php do_action( 'bkap_after_lockout_date', $duplicate_of ); ?>
                        
                        <tr class="multiple_days_setup" <?php echo $multiple_days_setup; ?>>
                            <th style="width:50%;">
                                <label for="booking_minimum_number_days_multiple"><?php _e( 'Minimum number of nights to book', 'woocommerce-booking' ); ?></label>
                            </th>
                            <td>
                                <?php 
                                $minimum_day_multiple = "";
                                if ( isset( $booking_settings[ 'booking_minimum_number_days_multiple' ] ) && $booking_settings[ 'booking_minimum_number_days_multiple' ] != "" ) {
                                    $minimum_day_multiple = $booking_settings[ 'booking_minimum_number_days_multiple' ];
                                } else {
                                    $minimum_day_multiple = "0";
                                }   
                                ?>
                                <input type="number" style="width:90px;" name="booking_minimum_number_days_multiple" id="booking_minimum_number_days_multiple" min="0" max="9999" value="<?php echo $minimum_day_multiple;?>" >
                            </td>
                            <td>
                                <img class="help_tip" width="16" height="16" data-tip="<?php _e( 'The minimum number of booking days you want to book for multiple days booking. For example, if you take minimum 2 days of booking, add 2 in the field here.', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                            </td>
                        </tr>

                        <?php do_action( 'bkap_after_minimum_days_multiple', $duplicate_of ); ?>
                        
                        <tr class="multiple_days_setup" <?php echo $multiple_days_setup; ?>>
                            <th style="width:50%;">
                                <label for="booking_maximum_number_days_multiple"><?php _e( 'Maximum number of nights to book', 'woocommerce-booking' ); ?></label>
                            </th>
                            <td>
                                <?php 
                                    $maximum_day_multiple = "";
                                    if ( isset( $booking_settings[ 'booking_maximum_number_days_multiple' ] ) && $booking_settings[ 'booking_maximum_number_days_multiple' ] != "" ) {
                                        $maximum_day_multiple = $booking_settings[ 'booking_maximum_number_days_multiple' ];
                                    } else {
                                        $maximum_day_multiple = "365";
                                    }   
                                ?>
                                <input type="number" style="width:90px;" name="booking_maximum_number_days_multiple" id="booking_maximum_number_days_multiple" min="0" max="9999" value="<?php echo $maximum_day_multiple;?>" >
                            </td>
                            <td>
                                <img class="help_tip" width="16" height="16" data-tip="<?php _e( 'The maximum number of booking days you want to book for multiple days booking. For example, if you take maximum 60 days of booking, add 60 in the field here.', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                            </td>
                        </tr>

                        <?php do_action( 'bkap_after_maximum_days_multiple', $duplicate_of ); ?>

                    </table>
                    
                    <hr/>
                    
                    <?php
                        //call function to display the weekdays and availablility setup
                        //self::bkap_get_weekdays_html( $duplicate_of, true, true, $booking_settings );
                        if ( 'checked' == $multiple_days ) {
                            self::bkap_get_weekdays_html( $duplicate_of, false, true, $booking_settings );
                        } else {
                            self::bkap_get_weekdays_html( $duplicate_of, true, true, $booking_settings );
                        }
                    ?>
                    
                    <!-- Descrpition of the per night price for multiple days -->
                    <p class="show-multiple-day-per-night-price-description"></p>

                    <hr style="clear:both;" />
                    
                    <?php
                    // add specific setup
                    self::bkap_get_specific_html( $duplicate_of, $booking_settings ); 
                    ?>
                    
                    <div style="clear: both;"></div>
                    
                    <?php
                    // add date and time setup
                    self::bkap_get_date_time_html( $duplicate_of, $booking_settings ); 
                    ?>
                    
                    <?php 
                    // These hooks have been moved here to ensure no existing functionality for any client is broken
                    // in case if they hv added custom fields
                    do_action( 'bkap_before_enable_multiple_days', $duplicate_of );
                    do_action( 'bkap_after_lockout_time', $duplicate_of );
                    do_action( 'bkap_before_range_selection_radio', $duplicate_of );
                    do_action( 'bkap_before_booking_start_date_range', $duplicate_of );
                    do_action( 'bkap_before_booking_end_date_range', $duplicate_of );
                    do_action( 'bkap_before_recurring_date_range', $duplicate_of );
                    do_action( 'bkap_after_recurring_date_range', $duplicate_of );
                    do_action( 'bkap_after_recurring_years_range', $duplicate_of );
                    ?>
                    
                    <hr style="margin-top:20px"/>
                    
                    <?php 
                        if( isset( $post_type ) && $post_type === 'product' ) {
                            self::bkap_save_button( 'bkap_save_settings' );
                        }
                    ?>                    
                    
                    <div id='availability_update_notification' style='display:none;'></div>                    
                </div>
                        
                <?php do_action( 'bkap_after_listing_enabled', $duplicate_of ); ?>

            </div>
        </div>
        <?php 
    }

    /**
     * This function will print a save button in each of the tabs.
     * It needs the callback JS function as the parameter
     * 
     * @param str $save_fn - Name of the callback JS function.
     * @since 4.6.0
     */

    static function bkap_save_button( $save_fn ) {
        
        $save_fn .= '()';
        ?>

        <div style="width:100%;margin-left: 40%;">
            <button type="button" class="button-primary bkap-primary" onclick="<?php echo $save_fn; ?>" ><i class="fa fa-floppy-o fa-lg"></i>&nbsp;&nbsp;&nbsp;<?php _e( 'Save Changes', 'woocommerce-booking' ); ?></button>
        </div>
        
        <?php                   
    }
    
    /**
     * The function adds the html for the Weekdays UI
     * which allows the admin to enable/disable weekdays,
     * set lockout and price for the same.
     * 
     * @param int $product_id
     * @param boolean $lockout
     * @param boolean $price
     * @param array booking settings
     * @global array $bkap_weekdays Array of weekdays
     * @since 4.0.0
     */

    static function bkap_get_weekdays_html( $product_id, $lockout = false, $price = true, $booking_settings = array() ) {
        global $bkap_weekdays;
        
        if ( isset( $booking_settings[ 'booking_enable_date' ] ) && 'on' == $booking_settings[ 'booking_enable_date' ] ) { // bookable product
            $display                = '';
            $recurring_weekdays     = ( isset( $booking_settings[ 'booking_recurring' ] ) ) ? $booking_settings[ 'booking_recurring' ] : array();
            $recurring_lockout      = ( isset( $booking_settings[ 'booking_recurring_lockout' ] ) ? $booking_settings[ 'booking_recurring_lockout' ] : array());
            $booking_special_prices = get_post_meta( $product_id, '_bkap_special_price', true );
            $special_prices         = array();

            /** Create a list of the special prices as day and price **/
            if ( is_array( $booking_special_prices ) && count( $booking_special_prices ) > 0 ) {
                
                foreach ( $booking_special_prices as $special_key => $special_value ) {
                    $weekday_set = $special_value[ 'booking_special_weekday' ];
                    
                    if ( $weekday_set != "" ) {
                        $special_prices[ $weekday_set ] = $special_value[ 'booking_special_price' ];
                    } 
                }
                
            }

        } else { // non-bookable product
            $display            = 'display:none;';
            $recurring_weekdays = array();
            $special_prices     = array();
            $recurring_lockout  = array();
        }

        ?>
        
        <div id="set_weekdays" class="weekdays_flex_main" style="margin-bottom:20px;width:100%;float:left; <?php echo $display; ?>" >
            <div class="weekdays_flex_child" >
                <div class="weekdays_flex_child_1 bkap_weekdays_heading" style="max-width:27%;"><b><?php _e( 'Weekday', 'woocommerce-booking' );?></b></div>
                <div class="weekdays_flex_child_2 bkap_weekdays_heading" style="max-width:20%;"><b><?php _e( 'Bookable', 'woocommerce-booking' ); ?></b></div>
                
                <?php 
                    $mutiple_display = '';
                    
                    if ( !$lockout ) {
                        $mutiple_display = 'display:none;';
                    }
                ?>
                <div class="weekdays_flex_child_3 bkap_weekdays_heading" style="max-width:26%;<?php echo $mutiple_display;?>"><b><?php _e( 'Maximum bookings', 'woocommerce-booking' );?></b></div>
                
                
                <?php if ( $price ) { 
                    $currency_symbol = get_woocommerce_currency_symbol();
                ?>
                <div class="weekdays_flex_child_4 bkap_weekdays_heading" ><b><?php _e( "Price ($currency_symbol)", 'woocommerce-booking' );?> </b></div>
                <?php }?>
            </div>
                    
            <?php 
            $i = 0;
            foreach( $bkap_weekdays as $w_key => $w_value ) {
            ?>
                <div class="weekdays_flex_child">
                    <div class="weekdays_flex_child_1" style="padding-top:5px; max-width:27%; float:left;"><?php echo $w_value; ?></div>
                    
                    <?php 
                    $weekday_status = 'checked';
                    $fields_status = '';
                    if ( isset( $recurring_weekdays[ $w_key ] ) && '' == $recurring_weekdays[ $w_key ] ) {
                        $weekday_status = '';
                        $fields_status = 'disabled';
                    }   ?>
                    <div class="weekdays_flex_child_2" style="padding-top:5px; max-width:20%; float:left;">
                        <label class="bkap_switch">
                            <input id="<?php echo $w_key;?>" type="checkbox" name="<?php echo $w_value; ?>" <?php echo $weekday_status; ?> >
                            <div class="bkap_slider round"></div> 
                        </label>  
                    
                    </div>
                    
                    <?php
                        $weekday_lockout = isset( $recurring_lockout[ $w_key ] ) ? $recurring_lockout[ $w_key ] : '';
                    ?>
                    <div class="weekdays_flex_child_3" style="padding-top:5px; min-width:26%;<?php echo $mutiple_display; ?>"> <input style="float:left;" type="number" id="weekday_lockout_<?php echo $i;?>" name="day_lockout" min="0" max="9999" placeholder="Max bookings" value="<?php echo $weekday_lockout; ?>" <?php echo $fields_status; ?>/></div>
                    
                    
                    <?php 
                    if ( $price ) {
                        $special_price = '';
                        if ( is_array( $special_prices ) && count( $special_prices ) > 0 && array_key_exists( $w_key, $special_prices ) ) {
                            $special_price = $special_prices[ $w_key ];
                        }        
                    ?>
                    <div class="weekdays_flex_child_4" style="padding-top:5px;"> <input style="width:95px;" type="text" id="weekday_price_<?php echo $i;?>" name="day_price" min="0" placeholder="Special Price" value="<?php echo $special_price;?>"/> </div>
                    <?php }?>
                </div>    
                    
                <?php 
                $i++;
            }
            ?>
        </div>
               
        <?php 
        }
        
        /**
         * Adds the specific dates availability checkbox and 
         * the table for the same.
         * 
         * @param integer $product_id - Product ID
         * @param array $booking_settings 
         * 
         * @since 4.0.0
         */

        static function bkap_get_specific_html( $product_id, $booking_settings ) {
        
            $specific_date_checkbox = '';
            $specific_date_table    = '';
            
            $display                = 'display:block;';
            $specific_date_checkbox = '';
            $specific_date_table    = 'display:none;';
             
            if( isset( $booking_settings[ 'booking_specific_booking' ]) && $booking_settings[ 'booking_specific_booking' ] == 'on' ){
                $specific_date_table    = 'display:block;';
                $specific_date_checkbox = 'checked';
            }
            
            ?>
            <div style="clear: both;" ></div>
            
            <div class="specific_date_title" style="display:flex;width:100%;margin-top:20px;">
                <div>
                  <b><?php _e( 'Set Availability by Dates/Months', 'woocommerce-booking' ); ?></b>
                </div>
                <div style="margin-left: 10px">
                    <label class="bkap_switch">
                    <input title="Select one of booking type for enable this." type="checkbox" name="specific_date_checkbox" id="specific_date_checkbox"  <?php echo $specific_date_checkbox;?>>
                    <div class="bkap_slider round"></div>
                    </label>
                </div>
            </div>
            
            <div style="clear: both;" ></div>
            <!-- Below is the div to display table for adding specific date range and other ranges -->
            
            <div class="specific_date" style="<?php echo $specific_date_table; ?>">
                <table class="specific">
                    <?php self::bkap_get_specific_heading_html( $product_id ); ?>
                    <?php self::bkap_get_specific_default_row_html( $product_id, $booking_settings ); ?>
                    <?php self::bkap_get_specific_row_to_display_html( $product_id, $booking_settings ); ?>
                    <tr style="padding:5px; border-top:2px solid #eee">
                       <td colspan="4" style="border-right: 0px;"><i><small><?php _e( 'Create custom ranges, holidays and more here.', 'woocommerce-booking' ); ?></small><i></td>
                       <td colspan="2" align="right" style="border-left: none;"><button type="button" class="button-primary bkap_add_new_range"><?php _e( 'Add New Range' , 'woocommerce-booking' );?></button></td>
                    </tr>
                </table>
           </div>
           <?php 
        }
                
        /**
         * Prints the table headers for the 'Set Availability by Dates/Months'
         * which allows the admin to enable/disable weekdays, set lockout and 
         * price for the same.
         * 
         * @param $product_id - Product ID
         * @since 4.0.0
         */

         static function bkap_get_specific_heading_html($product_id){
            
            ?>            
            <tr>
            <th style="width:20%"><?php _e( 'Range Type'                , 'woocommerce-booking' );?></th>
            <th style="width:20%"><?php _e( 'From'                      , 'woocommerce-booking' );?></th>
            <th style="width:20%"><?php _e( 'To'                        , 'woocommerce-booking' );?></th>
            <th style="width:10%"><?php _e( 'Bookable'                  , 'woocommerce-booking' );?></th>
            <th style="border-right:0px;width:25%"><?php _e( 'Max bookings' , 'woocommerce-booking' );?></th>
            <th style="border-left:0px;"></th>
            </tr>            
            <?php 
        }
        

        /**
         * Prints the default row for the 'Set Availability by Dates/Months'
         * which allows the admin to enable/disable weekdays, set lockout and 
         * price for the same.
         * This row is used to add new rows to the table.
         * 
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Existing Booking Settings
         *
         * @global array $bkap_month array of months 
         * @global array $bkap_dates_months_availability array of months 
         * @since 4.0.0
         */

         static function bkap_get_specific_default_row_html( $product_id, $booking_settings ){
            global $bkap_months;
            global $bkap_dates_months_availability;
            
            ?>
            
            <!-- We are fetching below tr when add new range is clicked -->
            <tr class="added_specific_date_range_row" style="display: none;">
               <td>
                    <select style="width:100%;" id="range_dropdown" >
                        <?php 
                        foreach( $bkap_dates_months_availability as $d_value => $d_name ) {
                            printf( "<option value='%s'>%s</option>\n", $d_value, $d_name );
                        }?>
                    </select>
               </td>
               
               
                   <!-- From Custom-->
                   <td class="date_selection_textbox1" style="width:20%;">
                        <div class="fake-input">
                            <input type="text" id="datepicker_textbox1" class="datepicker_start_date date_selection_textbox" style="width:100%;" />
                            <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="custom_checkin_cal" width="15" height="15" />
                        </div>
                   </td>
                   <!-- To Custom-->
                   <td class="date_selection_textbox2">
                        <div class="fake-input" >
                            <input type="text" id="datepicker_textbox2" class="datepicker_end_date date_selection_textbox" style="width:100%;" />
                            <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="custom_checkout_cal" width="15" height="15" />
                        </div>
                   </td>
                   
                   <!-- Specific Date Textarea -->
                   <td class="date_selection_textbox3" colspan="2" style="display:none;" >
                         <div class="fake-textarea" >
                             <textarea id="textareamultidate_cal1" class="textareamultidate_cal" rows="1" col="30" style="width:100%;height:auto;"></textarea>
                             <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="specific_date_multidate_cal" class="bkap_multiple_datepicker_cal_image" width="15" height="15" />
                         </div>
                   </td>
                   
                   <!-- From Month-->
                   <td class="date_selection_textbox4" style="display:none;">
                        <select id="bkap_availability_from_month" style="width:100%;">
                            <?php
                            foreach( $bkap_months as $m_number => $m_name ) {
                                printf( "<option value='%d'>%s</option>\n", $m_number, $m_name );
                            } 
                            ?>
                        </select>
                   </td>
                   <!-- To Month-->
                   <td class="date_selection_textbox5" style="display:none;">
                        <select id="bkap_availability_to_month" style="width:100%;">
                            <?php
                            foreach( $bkap_months as $m_number => $m_name ) {
                                printf( "<option value='%d'>%s</option>\n", $m_number, $m_name );
                            } 
                            ?>
                        </select>
                   </td>
                   
                   <!-- Holiday Textarea -->
                   <td class="date_selection_textbox6" colspan="2" style="display:none;" >
                         <div class="fake-textarea" >
                             <textarea id="textareamultidate_cal2" class="textareamultidate_cal" rows="1" col="30" style="width:100%;height:auto;"></textarea>
                             <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="holiday_multidate_cal" class="bkap_multiple_datepicker_cal_image" width="15" height="15" />
                         </div>
                   </td>
               
                   <td style="padding-left:2%;">
                        <div class="bkap_popup">
                        <span class="bkap_popuptext" id="bkap_myPopup"></span>
                        <label class="bkap_switch">
                        
                          <input id="bkap_bookable_nonbookable" type="checkbox" name="bkap_bookable_nonbookable">
                          <div class="bkap_slider round"></div>
                        </label>
                        
                        <div>
                   </td>
                   
                   <td class="bkap_lockout_column_data_1" >
                    <input id="bkap_number_of_year_to_recur_custom_range" title="Please enter number of years you want to recur this custom range" type="number" min="0" style="width:65%;font-size:11px;margin-left: 15%;" placeholder="No. of Years">
                    &nbsp;
                    <i id="bkap_recurring" class="fa fa-refresh" aria-hidden="true" title="Recurring yearly"></i>
                   </td>
                   <td class="bkap_lockout_column_data_2"  style="display:none;">
                        <input id="bkap_number_of_year_to_recur_holiday" title="Please enter number of years you want to recur selected holidays" type="number" min="0" style="width:65%;font-size:11px;margin-left: 15%;" placeholder="No. of Years">
                        &nbsp;
                        <i id="bkap_recurring" class="fa fa-refresh" aria-hidden="true" title="Recurring yearly"></i>
                   </td>
                   <td class="bkap_lockout_column_data_3"  style="display:none;">
                        <input id="bkap_number_of_year_to_recur_month" title="Please enter number of years you want to recur selected month" type="number" min="0" style="width:65%;font-size:11px;margin-left: 15%;" placeholder="No. of Years">
                        &nbsp;
                    <i id="bkap_recurring" class="fa fa-refresh" aria-hidden="true" title="Recurring yearly"></i>
                   </td>    
                   <td class="bkap_lockout_column_data_4" style="display:none;">
                        <input id="bkap_specific_date_lockout" title="This is lockout for selected specific dates." type="number" min="0" style="width:47%;font-size:11px;" placeholder="Max bookings">
                        <input id="bkap_specific_date_price" title="This is price for selected specific dates." type="number" min="0" style="width:45%;float:right;font-size:11px;" placeholder="Price">
                   </td>
                   
                   <td id="bkap_close" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
           </tr>
           <!-- We are fetching above tr when add new range is clicked -->
            
            <?php 
        }
        
        /**
         * Prints the existing data for the 'Set Availability by Dates/Months'
         * which allows the admin to enable/disable weekdays, set lockout and 
         * price for the same.
         * 
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings
         * @global array $bkap_month array of months 
         * @global array $bkap_dates_months_availability array of months 
         * 
         * @since 4.0.0
         */

         static function bkap_get_specific_row_to_display_html( $product_id, $booking_settings ){
            global $bkap_months;
            global $bkap_dates_months_availability;
            
            $booking_type = $booking_custom_ranges = $booking_holiday_ranges = $booking_month_ranges = $booking_specific_dates = $booking_special_prices = $booking_product_holiday = array(); 
            
            // Fetching data from post meta.
            $booking_type           = get_post_meta( $product_id, '_bkap_booking_type', true );
            $booking_custom_ranges  = get_post_meta( $product_id, '_bkap_custom_ranges', true );
            $booking_holiday_ranges = get_post_meta( $product_id, '_bkap_holiday_ranges', true );
            $booking_month_ranges   = get_post_meta( $product_id, '_bkap_month_ranges', true );
            $booking_specific_dates = get_post_meta( $product_id, '_bkap_specific_dates', true );
            $booking_special_prices = get_post_meta( $product_id, '_bkap_special_price', true );
            $booking_product_holiday = isset( $booking_settings['booking_product_holiday'] ) ? $booking_settings['booking_product_holiday'] : "" ;

            // sorting holidays in chronological order.
            if ( is_array( $booking_product_holiday ) && count( $booking_product_holiday ) > 0 ) {
                uksort( $booking_product_holiday, 'bkap_orderby_date_key' );
            }
            
            // Calculating counts for ranges.
            $count_custom_ranges    = $booking_custom_ranges  != "" ? count( $booking_custom_ranges ) : 0;
            $count_holiday_ranges   = $booking_holiday_ranges != "" ? count( $booking_holiday_ranges ) : 0;
            $count_month_ranges     = $booking_month_ranges   != "" ? count( $booking_month_ranges ) : 0;
            $count_specific_dates   = $booking_specific_dates != "" ? count( $booking_specific_dates ) : 0;
            
            $count_special_prices   = $booking_special_prices != "" ? count( $booking_special_prices ) : 0;
            $count_product_holiday  = $booking_product_holiday != "" ? count( $booking_product_holiday ) : 0;
            
            $array_of_all_added_ranges  = array();
            $bkap_range_count           = 0;            
            $special_prices             = array();

            // Modify the special prices array
            if ( isset( $booking_special_prices ) && $count_special_prices > 0 ) {
                
                foreach( $booking_special_prices as $s_key => $s_value ) {
                    
                    if ( isset( $s_value[ 'booking_special_date' ] ) && $s_value[ 'booking_special_date' ] != '' ) {
                        $s_date = date( 'j-n-Y', strtotime( $s_value[ 'booking_special_date' ] ) );
                        $special_prices[ $s_date ] = $s_value[ 'booking_special_price' ];
                    }
                }
            }
             
            if ( isset( $booking_custom_ranges ) && $count_custom_ranges > 0 ) {
                
                for ( $bkap_range = 0; $bkap_range < $count_custom_ranges; $bkap_range++ ) {
                    $array_of_all_added_ranges[$bkap_range]['bkap_type']            = "custom_range";
                    $array_of_all_added_ranges[$bkap_range]['bkap_start']           = $booking_custom_ranges[$bkap_range]['start'];
                    $array_of_all_added_ranges[$bkap_range]['bkap_end']             = $booking_custom_ranges[$bkap_range]['end'];
                    $array_of_all_added_ranges[$bkap_range]['bkap_years_to_recur']  = $booking_custom_ranges[$bkap_range]['years_to_recur'];
                    $bkap_range_count++;
                }
            }
            
            if ( isset( $booking_product_holiday ) && $count_product_holiday > 0 ) {

                foreach ( $booking_product_holiday as  $booking_product_holiday_keys => $booking_product_holiday_values ) {
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_type']            = "holidays";
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_holiday_date']    = $booking_product_holiday_keys;
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_years_to_recur']  = $booking_product_holiday_values;
                    $bkap_range_count++;
                }
            }
            
            if ( isset( $booking_month_ranges ) && $count_month_ranges > 0 ) {
                
                for ( $bkap_range = 0; $bkap_range < $count_month_ranges; $bkap_range++ ) {
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_type']            = "range_of_months";
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_start']           = $booking_month_ranges[$bkap_range]['start'];
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_end']             = $booking_month_ranges[$bkap_range]['end'];
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_years_to_recur']  = $booking_month_ranges[$bkap_range]['years_to_recur'];
                    $bkap_range_count++;
                }
            }
            
            if ( isset( $booking_specific_dates ) && $count_specific_dates > 0 ) {
                
                foreach ( $booking_specific_dates as  $booking_specific_dates_keys => $booking_specific_dates_values ) {
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_type']            = "specific_dates";
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_specific_date']   = $booking_specific_dates_keys;
                    $array_of_all_added_ranges[$bkap_range_count]['bkap_specific_lockout']= $booking_specific_dates_values;
                    // check if that date has a special price set
                    $array_of_all_added_ranges[ $bkap_range_count ][ 'bkap_specific_price' ] = ( isset( $special_prices[ $booking_specific_dates_keys ] ) ) ? $special_prices[ $booking_specific_dates_keys ] : '';
                    $bkap_range_count++;
                }
            }

            // if the booking type is multiple day, then no data is present in specific dates, so loop through the special prices
            if ( 'multiple_days' == $booking_type ) {
                
                if ( is_array( $special_prices ) && count( $special_prices ) > 0 ) {
                    
                    foreach ( $special_prices as $sp_date => $sp_price ) {
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_type']            = "specific_dates";
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_specific_date']   = $sp_date;
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_specific_lockout']= '';
                        $array_of_all_added_ranges[ $bkap_range_count ][ 'bkap_specific_price' ] = $sp_price;
                        $bkap_range_count++;
                    }
                }
            }
            
            if ( isset( $booking_holiday_ranges ) && $count_holiday_ranges > 0 ) {
                for ( $bkap_range = 0; $bkap_range < $count_holiday_ranges; $bkap_range++ ) {
            
                    $bkap_holiday_from_month        = date('F',strtotime( $booking_holiday_ranges[$bkap_range]['start'] ) );
                    $bkap_holiday_to_month          = date('F',strtotime( $booking_holiday_ranges[$bkap_range]['end'] ) );
                    $holiday_start_date_of_month    = date('1-n-Y',strtotime( $booking_holiday_ranges[$bkap_range]['start'] ) );
                    $holiday_end_date_of_month      = date('t-n-Y',strtotime( $booking_holiday_ranges[$bkap_range]['end'] ) );
                    
                    // Check if the start date is the start of the month and end date is the end date of the month then range type should be month range.
                    if ( $booking_holiday_ranges[ $bkap_range ]['start'] == $holiday_start_date_of_month && $holiday_end_date_of_month == $booking_holiday_ranges[$bkap_range]['end'] ) {
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_type']           = ( isset( $booking_holiday_ranges[$bkap_range]['range_type'] ) ) ? $booking_holiday_ranges[$bkap_range]['range_type'] : "range_of_months";
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_start']          = $booking_holiday_ranges[$bkap_range]['start'];
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_end']            = $booking_holiday_ranges[$bkap_range]['end'];
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_years_to_recur'] = $booking_holiday_ranges[$bkap_range]['years_to_recur'];
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_bookable'] = "off";
                    } else {
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_type']           = ( isset( $booking_holiday_ranges[$bkap_range]['range_type'] ) ) ? $booking_holiday_ranges[$bkap_range]['range_type'] : "custom_range";
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_start']          = $booking_holiday_ranges[$bkap_range]['start'];
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_end']            = $booking_holiday_ranges[$bkap_range]['end'];
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_years_to_recur'] = $booking_holiday_ranges[$bkap_range]['years_to_recur'];
                        $array_of_all_added_ranges[$bkap_range_count]['bkap_bookable'] = "off";
                    }

                    $bkap_range_count++;
                }
            }
            
            $i = 0;
              
            while ( $i < count( $array_of_all_added_ranges ) ) {
                  
                  $range_type               = $array_of_all_added_ranges[$i]['bkap_type'];
                  $custom_range_disaply     = $holidays_disaply = $range_of_months_disaply = $specific_dates_disaply = "";
                  
                  $bkap_start               = ( isset( $array_of_all_added_ranges[$i]['bkap_start'] )            && !is_null( $array_of_all_added_ranges[$i]['bkap_start'] ) )            ? $array_of_all_added_ranges[$i]['bkap_start']            : "";
                  $bkap_end                 = ( isset( $array_of_all_added_ranges[$i]['bkap_end'] )              && !is_null( $array_of_all_added_ranges[$i]['bkap_end'] ) )              ? $array_of_all_added_ranges[$i]['bkap_end']              : "";
                  $bkap_years_to_recur      = ( isset( $array_of_all_added_ranges[$i]['bkap_years_to_recur'] )   && !is_null( $array_of_all_added_ranges[$i]['bkap_years_to_recur'] ) )   ? $array_of_all_added_ranges[$i]['bkap_years_to_recur']   : "";
                  $bkap_bookable            = 'checked="checked"';
                  $custom_bkap_start        = $custom_bkap_end = $month_bkap_start = $month_bkap_end = $bkap_holiday_date = $custom_bkap_years_to_recur = $holiday_bkap_years_to_recur = $month_bkap_years_to_recur = $bkap_specific_price = $bkap_specific_lockout = $bkap_specific_date = "";
                  
                  switch ( $range_type ) {
                      case "custom_range":
                          
                          $holidays_disaply             = $range_of_months_disaply = $specific_dates_disaply = "display:none;";
                          $custom_bkap_start            = $bkap_start;
                          $custom_bkap_end              = $bkap_end;
                          $custom_bkap_years_to_recur   = $bkap_years_to_recur;
                          if( isset( $array_of_all_added_ranges[$i]['bkap_bookable'] ) && $array_of_all_added_ranges[$i]['bkap_bookable'] == "off" ){
                              $bkap_bookable = "";
                          }
                          
                          break;
                          
                      case "holidays":
                          
                          $custom_range_disaply = $range_of_months_disaply = $specific_dates_disaply = "display:none;";
                          $bkap_holiday_date            = ( isset( $array_of_all_added_ranges[$i]['bkap_holiday_date'] )              && !is_null( $array_of_all_added_ranges[$i]['bkap_holiday_date'] ) )              ? $array_of_all_added_ranges[$i]['bkap_holiday_date']              : "";
                          $holiday_bkap_years_to_recur  = $bkap_years_to_recur;
                          $bkap_bookable                = "";
                          break;
                          
                      case "range_of_months":
                          
                          $custom_range_disaply         = $holidays_disaply = $specific_dates_disaply = "display:none;";
                          $month_bkap_start             = date( "F", strtotime( $bkap_start ) );
                          $month_bkap_end               = date( "F", strtotime( $bkap_end ) );
                          $month_bkap_years_to_recur    = $bkap_years_to_recur;                          
                          if( isset( $array_of_all_added_ranges[$i]['bkap_bookable'] ) && $array_of_all_added_ranges[$i]['bkap_bookable'] == "off" ){
                              $bkap_bookable = "";
                          }   
                          break;
                          
                      case "specific_dates":
                          
                          $custom_range_disaply     = $holidays_disaply = $range_of_months_disaply = "display:none;";
                          $bkap_specific_date       = ( isset( $array_of_all_added_ranges[$i]['bkap_specific_date'] )    && !is_null( $array_of_all_added_ranges[$i]['bkap_specific_date'] ) )    ? $array_of_all_added_ranges[$i]['bkap_specific_date']    : "";
                          $bkap_specific_lockout    = ( isset( $array_of_all_added_ranges[$i]['bkap_specific_lockout'] ) && !is_null( $array_of_all_added_ranges[$i]['bkap_specific_lockout'] ) ) ? $array_of_all_added_ranges[$i]['bkap_specific_lockout'] : "";
                          $bkap_specific_price      = ( isset ( $array_of_all_added_ranges[ $i ][ 'bkap_specific_price' ] ) ) ? $array_of_all_added_ranges[ $i ][ 'bkap_specific_price' ] : '';
                          break;
                          
                      default:
                          break;
                  }
                  
                  $bkap_row_toggle = '';
                  $bkap_row_toggle_display = '';
                  if( $i > 4 ){
                      $bkap_row_toggle = "bkap_row_toggle";
                      $bkap_row_toggle_display = 'style="display:none;"'; 
                  }
                  ?>
                  
                  <tr class="added_specific_date_range_row_<?php echo $i;?> <?php echo $bkap_row_toggle;?>" <?php echo $bkap_row_toggle_display;?>>
                  
                      <td style="width:20%;">
                        <select style="width:100%;" id="range_dropdown_<?php echo $i;?>">
                        
                        <?php 
                            foreach( $bkap_dates_months_availability as $d_value => $d_name ) {
                                $bkap_range_selected = '';
                                if( $d_value == $range_type ){
                                    $bkap_range_selected = "selected";
                                }
                                printf( "<option value='%s' %s>%s</option>\n", $d_value,$bkap_range_selected, $d_name );
                            }
                        ?>

                        </select>
                      </td>
                      
                      <td class="date_selection_textbox1" style="width:20%;<?php echo $custom_range_disaply;?>">
                           <div class="fake-input">
                               <input type="text" id="datepicker_textbox_<?php echo $i;?>" class="datepicker_start_date date_selection_textbox" style="width:100%;" value="<?php echo $custom_bkap_start;?>"/>
                               <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="custom_checkin_cal_<?php echo $i;?>" width="15" height="15" />
                           </div>
                      </td>
                           
                      <td class="date_selection_textbox2" style="width:20%;<?php echo $custom_range_disaply;?>">
                           <div class="fake-input" >
                               <input type="text" id="datepicker_textbox__<?php echo $i;?>" class="datepicker_end_date date_selection_textbox" style="width:100%;" value="<?php echo $custom_bkap_end;?>" />
                               <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="custom_checkout_cal_<?php echo $i;?>" width="15" height="15" />
                           </div>
                      </td>
                      
                      <td class="date_selection_textbox3" colspan="2" style="<?php echo $specific_dates_disaply;?>width:40%;" >
                           <div class="fake-textarea" >
                               <textarea id="specific_dates_multidatepicker_<?php echo $i;?>" class="textareamultidate_cal" rows="1" col="30" style="width:100%;height:auto;"><?php echo $bkap_specific_date;?></textarea>
                               <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="specific_date_multidate_cal_<?php echo $i;?>" class="bkap_multiple_datepicker_cal_image" width="15" height="15" />
                           </div>
                      </td>
                  
                      <!-- From Month-->
                      <td class="date_selection_textbox4" style="<?php echo $range_of_months_disaply;?>width:20%;">
                           <select id="bkap_availability_from_month_<?php echo $i;?>" style="width:100%;">
                            
                            <?php
                            foreach ( $bkap_months as $m_number => $m_name ) {
                                if ( $m_name == $month_bkap_start ) {
                                    $month_bkap_start_selected = "selected";
                                    printf( "<option value='%d' %s>%s</option>\n", $m_number, $month_bkap_start_selected, $m_name );
                                } else {
                                    printf( "<option value='%d'>%s</option>\n", $m_number, $m_name );
                                }
                            } 
                            ?>

                            </select>
                      </td>
                       <!-- To Month-->
                       <td class="date_selection_textbox5" style="<?php echo $range_of_months_disaply;?>width:20%;">
                            <select id="bkap_availability_to_month_<?php echo $i;?>" style="width:100%;">
                                
                                <?php
                                    foreach ( $bkap_months as $m_number => $m_name ) {
                                        
                                        if ( $m_name == $month_bkap_end ) {
                                            $month_bkap_end_selected = "selected";
                                            printf( "<option value='%d' %s>%s</option>\n", $m_number, $month_bkap_end_selected, $m_name );
                                        } else {
                                            printf( "<option value='%d'>%s</option>\n", $m_number, $m_name );
                                        }
                                    } 
                                ?>

                            </select>
                       </td>
                       
                       <!-- Holiday Textarea -->
                       <td class="date_selection_textbox6" colspan="2" style="<?php echo $holidays_disaply;?>width:40%" >
                            <div class="fake-textarea" >
                                <textarea id="holidays_multidatepicker_<?php echo $i;?>" class="textareamultidate_cal" rows="1" col="30" style="width:100%;height:auto;" style="overflow:hidden" onkeyup="auto_grow(this)"><?php echo $bkap_holiday_date;?></textarea>
                                <img src="<?php echo plugins_url();?>/woocommerce-booking/images/cal.gif" id="holiday_multidate_cal_<?php echo $i;?>" class="bkap_multiple_datepicker_cal_image" width="15" height="15" />
                            </div>
                       </td>
                       
                       <td style="padding-left:2%;width:10%;">
                            <div class="bkap_popup">
                            <span class="bkap_popuptext" id="bkap_myPopup_<?php echo $i;?>"></span>
                            <label class="bkap_switch">
                                 <input id="bkap_bookable_nonbookable_<?php echo $i;?>" type="checkbox" name="bkap_bookable_nonbookable" <?php echo $bkap_bookable;?>>
                                 <div class="bkap_slider round"></div>
                            </label>
                            </div>
                            
                       </td>
                       
                       <td class="bkap_lockout_column_data_1" style="<?php echo $custom_range_disaply;?>">
                            <input id="bkap_number_of_year_to_recur_custom_range_<?php echo $i;?>" value="<?php echo $custom_bkap_years_to_recur;?>" title="Please enter number of years you want to recur this custom range" type="number" min="0" style="width:65%;font-size:11px;margin-left: 15%;" placeholder="No. of Years">

                            &nbsp;
                            <i id="bkap_recurring" class="fa fa-refresh" aria-hidden="true" title="Recurring yearly"></i>
                       </td>
                       
                       <td class="bkap_lockout_column_data_2"  style="<?php echo $holidays_disaply;?>">
                            <input id="bkap_number_of_year_to_recur_holiday_<?php echo $i;?>" value="<?php echo $holiday_bkap_years_to_recur;?>"  title="Please enter number of years you want to recur selected holidays" type="number" min="0" style="width:65%;font-size:11px;margin-left: 15%;" placeholder="No. of Years">
                            &nbsp;
                            <i id="bkap_recurring" class="fa fa-refresh" aria-hidden="true" title="Recurring yearly"></i>
                       </td>
                       
                       <td class="bkap_lockout_column_data_3"  style="<?php echo $range_of_months_disaply;?>">
                            <input id="bkap_number_of_year_to_recur_month_<?php echo $i;?>" value="<?php echo $month_bkap_years_to_recur;?>" title="Please enter number of years you want to recur selected month" type="number" min="0" style="width:65%;font-size:11px;margin-left:15%;" placeholder="No. of Years">
                            &nbsp;
                            <i id="bkap_recurring" class="fa fa-refresh" aria-hidden="true" title="Recurring yearly"></i>
                       </td>    
                       
                       <td class="bkap_lockout_column_data_4" style="<?php echo $specific_dates_disaply;?>">
                            <input id="bkap_specific_date_lockout_<?php echo $i;?>" value="<?php echo $bkap_specific_lockout;?>" title="This is number of maximum bookings for selected specific dates." type="number" min="0"style="width:47%;font-size:11px;" placeholder="Max bookings">
                            <input id="bkap_specific_date_price_<?php echo $i;?>" value="<?php echo $bkap_specific_price; ?>" title="This is price for selected specific dates." type="number" min="0" style="width:45%;float:right;font-size:11px;" placeholder="Price">
                       </td>
                       
                       <td style="width:4%;" id="bkap_close_<?php echo $i;?>" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
                       
                  </tr>
                  <?php 
                  $i++;
              }
              
              if ( count( $array_of_all_added_ranges ) > 5 ) {
              ?>
                <tr style="">
                   <td colspan="6">
                   <span class="bkap_expand-close">
                       <a href="#" class="bkap_expand_all"><?php echo __( 'Expand', 'woocommerce-booking' ); ?></a> / <a href="#" class="bkap_close_all"><?php echo __( 'Close', 'woocommerce-booking' ); ?></a>
                   </span>
                   </td>
                </tr>
              <?php
              }
        }

        /**
         * Displays Time Slots present for a product
         *
         * @since 4.5.0
         */

        public static function bkap_load_time_slots () {
            ob_start();

            if ( empty( $_POST['bkap_product_id'] ) ) {
                wp_die( -1 );
            }

            $bkap_loop        = 0;
            $bkap_product_id  = absint( $_POST['bkap_product_id'] );
            
            $bkap_per_page    = ! empty( $_POST['bkap_per_page'] ) ? absint( $_POST['bkap_per_page'] ) : 15;
            $bkap_page        = ! empty( $_POST['bkap_page'] ) ? absint( $_POST['bkap_page'] ) : 1;
            
            $booking_settings =   get_post_meta( $bkap_product_id, 'woocommerce_booking_settings', true );

            /**
             * Set the pagination limits for the  records.
             */
            $bkap_end_record_on     = $bkap_page * $bkap_per_page ;
            $bkap_start_record_from = ( $bkap_page > 1 ) ? ( ( $bkap_page - 1 ) * $bkap_per_page ) + 1  : 1 ;

            if( isset( $booking_settings[ 'booking_time_settings' ] ) && is_array( $booking_settings['booking_time_settings'] ) ) {

                include( 'templates/meta-boxes/html-bkap-time-slots-meta-box.php' );
            }

            wp_die();
        }

        /**
         * Create pagination links for the Time Slots table.
         * 
         * @param integer $bkap_per_page_time_slots - Number of Time Slots to be displayed per page.
         * @param integer $bkap_total_time_slots_number - Number of slots present for the product.
         * @param integer $bkap_total_pages - Total Number of pages that need to be displayed.
         * @param string $bkap_encode_booking_times - JSON Encoded time slots data
         * 
         * @since 4.5.0
         */

        public static function bkap_get_pagination_for_time_slots ( $bkap_per_page_time_slots, $bkap_total_time_slots_number, $bkap_total_pages, $bkap_encode_booking_times ) {
            ?>
            <div class="bkap_toolbar"  data-bkap-total="<?php echo $bkap_total_time_slots_number; ?>" data-total_pages="<?php echo $bkap_total_pages; ?>" data-page="1" data-edited="false" data-time-slots = "<?php echo $bkap_encode_booking_times ; ?>" >
                <div class="bkap_time_slots_pagenav">
                    <span class="bkap_displaying_num">
                        <span class="bkap_display_count_num">
                            <?php _e( $bkap_total_time_slots_number, 'woocommerce-booking' ); ?>
                        </span>
                        <?php print( _n( 'time slot', 'time slots', $bkap_total_time_slots_number, 'woocommerce-booking' ) ); ?>
                        
                    </span>
                    <span class="bkap_pagination_links">
                        <a class="bkap_first_page disabled" title="<?php esc_attr_e( 'Go to the first page', 'woocommerce-booking' ); ?>" href="#">&laquo;</a>
                        <a class="bkap_prev_page disabled" title="<?php esc_attr_e( 'Go to the previous page', 'woocommerce-booking' ); ?>" href="#">&lsaquo;</a>
                        <span class="bkap_paging_select">
                            <label for="bkap_current_page_selector_1" class="bkap_screen_reader_text"><?php _e( 'Select Page', 'woocommerce-booking' ); ?></label>
                            <select class="bkap_page_selector" id="bkap_current_page_selector_1" title="<?php esc_attr_e( 'Current page', 'woocommerce-booking' ); ?>">
                                <?php for ( $i = 1; $i <= $bkap_total_pages; $i++ ) : ?>
                                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                <?php endfor; ?>
                            </select>
                             <?php _ex( 'of', 'number of pages', 'woocommerce-booking' ); ?> <span class="bkap_total_pages"><?php echo $bkap_total_pages; ?></span>
                        </span>
                        <a class="bkap_next_page" title="<?php esc_attr_e( 'Go to the next page', 'woocommerce-booking' ); ?>" href="#">&rsaquo;</a>
                        <a class="bkap_last_page" title="<?php esc_attr_e( 'Go to the last page', 'woocommerce-booking' ); ?>" href="#">&raquo;</a>
                    </span>
                </div>
                <div class="clear"></div>
            </div>
            <?php
        }

        /**
         * Displays the Time Slots data in the Availability tab.
         * 
         * @param integer $product_id - Product ID
         * @param array $booking_settings
         * @since 4.0.0
         */

         static function bkap_get_date_time_html( $product_id, $booking_settings = array() ) {
            
            $date_time_table = isset( $booking_settings ['booking_enable_time'] ) && $booking_settings ['booking_enable_time'] == 'on' ? '' : 'display:none;';
            
            $booking_times                      = array();
            $bkap_encode_booking_times          = array();
            $bkap_display_time_slots_pagination = 'display:none;';
            $bkap_total_time_slots_number       = 1;
            $bkap_total_pages                   = 0;
            $bkap_per_page_time_slots           = absint( apply_filters( 'bkap_time_slots_per_page', 15 ) );

            if ( isset( $booking_settings[ 'booking_time_settings' ] ) && is_array( $booking_settings['booking_time_settings'] ) ) {
                
                foreach ( $booking_settings['booking_time_settings'] as $bkap_weekday_key => $bkap_weekday_value ) {

                    foreach ( $bkap_weekday_value as $day_key => $time_data  ) {
                        
                        $bkap_from_hr      = ( isset( $time_data['from_slot_hrs'] ) && !is_null( $time_data['from_slot_hrs'] ) ) ? $time_data['from_slot_hrs'] : "";
                        $bkap_from_min     = ( isset( $time_data['from_slot_min'] ) && !is_null( $time_data['from_slot_min'] ) ) ? $time_data['from_slot_min'] : "";
                        
                        $bkap_from_time    = $bkap_from_hr.":".$bkap_from_min;
                         
                        $bkap_to_hr        = ( isset( $time_data['to_slot_hrs'] ) && !is_null( $time_data['to_slot_hrs'] ) ) ? $time_data['to_slot_hrs'] : "";
                        $bkap_to_min       = ( isset( $time_data['to_slot_min'] ) && !is_null( $time_data['to_slot_min'] ) ) ? $time_data['to_slot_min'] : "";
                        
                        $bkap_to_time      = ( $bkap_to_hr === '0' && $bkap_to_min === '00' ) ? '' : "$bkap_to_hr:$bkap_to_min";
                         
                        $bkap_lockout      = ( isset( $time_data['lockout_slot'] ) && !is_null( $time_data['lockout_slot'] ) ) ? $time_data['lockout_slot'] : "";
                        $bkap_price        = ( isset( $time_data['slot_price'] ) && !is_null( $time_data['slot_price'] ) ) ? $time_data['slot_price'] : "";
                         
                        $bkap_global       = ( isset( $time_data['global_time_check'] ) && !is_null( $time_data['global_time_check'] ) ) ? $time_data['global_time_check'] : "";
                        $bkap_note         = ( isset( $time_data['booking_notes'] ) && !is_null( $time_data['booking_notes'] ) ) ? $time_data['booking_notes'] : "";

                        $booking_times[ $bkap_total_time_slots_number ] = array();
                        $booking_times[ $bkap_total_time_slots_number ] [ 'day' ]               = $bkap_weekday_key;
                        $booking_times[ $bkap_total_time_slots_number ] [ 'from_time' ]         = $bkap_from_time;
                        $booking_times[ $bkap_total_time_slots_number ] [ 'to_time' ]           = $bkap_to_time;
                        $booking_times[ $bkap_total_time_slots_number ] [ 'lockout_slot' ]      = $bkap_lockout;
                        $booking_times[ $bkap_total_time_slots_number ] [ 'slot_price' ]        = $bkap_price;
                        $booking_times[ $bkap_total_time_slots_number ] [ 'global_time_check' ] = $bkap_global;
                        $booking_times[ $bkap_total_time_slots_number ] [ 'booking_notes' ]     = $bkap_note;

                        $bkap_total_time_slots_number ++;
                    }
                }
                
                if ( $bkap_total_time_slots_number > 1 ) {
                    $bkap_display_time_slots_pagination = '';
                    $bkap_total_time_slots_number--;
                }

                $bkap_total_pages           = ceil( $bkap_total_time_slots_number / $bkap_per_page_time_slots );                
                $bkap_encode_booking_times  = htmlspecialchars( json_encode ( $booking_times, JSON_FORCE_OBJECT ) );
            } else {

                /**
                 * When we add a new product we need to pass this array as a string so we are creating a json object string.
                 */

                $bkap_encode_booking_times = htmlspecialchars( json_encode ( $booking_times, JSON_FORCE_OBJECT ) );
            }
           
           ?>
           <!-- Table for adding Date/Day and time table -->
            <div class="bkap_date_timeslot_div" style="<?php echo $date_time_table;?>">
           
            <?php do_action( 'bkap_before_time_enabled', $product_id );?>
            
            <div>
                <h4><?php _e( 'Set Weekdays/Dates And It\'s Timeslots :', 'woocommerce-booking' ); ?></h4>
            </div>
            
            <?php do_action( 'bkap_after_time_enabled', $product_id );?>
            
            <table id="bkap_date_timeslot_table">
                <?php
                 // add date and time setup.
                self::bkap_get_daydate_and_time_heading( $product_id, $booking_settings, $bkap_display_time_slots_pagination, $bkap_per_page_time_slots, $bkap_total_time_slots_number, $bkap_total_pages, $bkap_encode_booking_times ); 

                ?>
                <?php
                self::bkap_get_daydate_and_time_table_base_data( $product_id, $booking_settings );

                if ( $bkap_total_time_slots_number > 0 ) {

                    /**
                     * This tr is a identifier, when we recive the response from ajax we will remove this tr and replace 
                     * our genrated data.
                     */
                    ?>
                        <tr class="bkap_replace_response_data">
                            
                        </tr>

                    <?php
                }
                ?>

                <tr style="padding:5px; border-top:2px solid #eee; <?php echo $bkap_display_time_slots_pagination; ?> " > 
                    <td colspan="8" align="right" style="border-right: 0px;">
                        <?php
                            /**
                             * Add the  pagination
                             */
                            self::bkap_get_pagination_for_time_slots ( $bkap_per_page_time_slots, $bkap_total_time_slots_number, $bkap_total_pages, $bkap_encode_booking_times );

                        ?>
                    </td> 
                </tr>
                <tr style="padding:5px; border-top:2px solid #eee">
                   <td colspan="5" style="border-right: 0px;">
                   <i>
                       <small><?php _e( 'Create timeslots for the days/dates selected above.', 'woocommerce-booking' ); ?>
                       <br><?php _e( 'Enter time in 24 hours format e.g. 14:00.', 'woocommerce-booking' );?>
                       <br><?php _e( 'Leave "To time" unchanged if you do not wish to create a fixed time duration slot.', 'woocommerce-booking' );?>
                       </small>
                   <i></td>
                   <td colspan="3" align="right" style="border-left: none;"><button type="button" class="button-primary bkap_add_new_date_time_range"><?php _e( 'Add New Timeslot' , 'woocommerce-booking' );?></button></td>
                </tr>
            </table>
            
           </div>
           <?php 
        }
        
        /**
         * Displays the table headers for the Time Slots table
         * in Booking meta box->Availability tab.
         * 
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings
         * @param string $bkap_display_time_slots_pagination - Display pagination or no. Blanks - indicates Yes
         * @param integer $bkap_per_page_time_slots - Time Slots to be displayed per page
         * @param integer $bkap_total_time_slots_number - Total Number of Slots
         * @param integer $bkap_total_pages - Number of pages
         * @param string $bkap_encode_booking_times - JSON encoded time slots data
         * 
         * @since 4.5.0
         */

        static function bkap_get_daydate_and_time_heading( $product_id, $booking_settings, $bkap_display_time_slots_pagination, $bkap_per_page_time_slots, $bkap_total_time_slots_number, $bkap_total_pages, $bkap_encode_booking_times  ){
        
            ?>
            <tr>
                <th width="20%"><?php _e( 'Weekday', 'woocommerce-booking' );?></th>
                <th width="10%"><?php _e( 'From', 'woocommerce-booking' );?></th>
                <th width="10%"><?php _e( 'To', 'woocommerce-booking' );?></th>
                <th width="10%"><?php _e( 'Maximum Bookings', 'woocommerce-booking' );?></th>
                <th width="10%"><?php _e( 'Price', 'woocommerce-booking' );?></th>
                <th width="10%"><?php _e( 'Global', 'woocommerce-booking' );?></th>
                <th width="23%"><?php _e( 'Note', 'woocommerce-booking' );?></th>
                <th width="4%"></th>
            </tr>
            <tr style="padding:5px; border-top:2px solid #eee; <?php echo $bkap_display_time_slots_pagination; ?> " > 
                    <td colspan="8" align="right" style="border-right: 0px;">
                        <?php
                            /**
                             * Add the  pagination
                             */
                            self::bkap_get_pagination_for_time_slots ( $bkap_per_page_time_slots, $bkap_total_time_slots_number, $bkap_total_pages, $bkap_encode_booking_times );

                        ?>
                    </td> 
                </tr>
            <?php 
         }
         
        /**
          * Add the default row for the time slots table. This row is used to add 
          * other data when the New icon is clicked.
          * 
          * @param integer $product_id - Product ID
          * @param array $booking_settings - Booking Settings
          * 
          * @since 4.0.0
          */

        static function bkap_get_daydate_and_time_table_base_data( $product_id, $booking_settings ){
             // count integer hase to disaply ni value change block kari nakhishu.
            global $bkap_weekdays;
            
            $recurring_weekdays = array();
            $specific_dates     = array();
            
            $bookable           = bkap_common::bkap_get_bookable_status( $product_id );

            if ( $bookable && isset( $booking_settings[ 'booking_recurring' ] ) && count( $booking_settings[ 'booking_recurring' ] ) > 0 ) { // bookable product
                $recurring_weekdays = $booking_settings[ 'booking_recurring' ];
            } else if ( ! $bookable ) { // it's a new product
                
                foreach ( $bkap_weekdays as $day_name => $day_value ) {
                    $recurring_weekdays[ $day_name ] = 'on'; // all weekdays are on by default
                }
            }
            
            if ( $bookable && isset( $booking_settings[ 'booking_specific_date' ] ) && count( $booking_settings[ 'booking_specific_date' ] ) > 0 ) {
                $specific_dates = $booking_settings[ 'booking_specific_date' ];
            }
            
            ?>
            <tr id="bkap_default_date_time_row" style="display: none;">
                <td width="20%" id="select_td">
                    <select id="bkap_dateday_selector" multiple="multiple">
                       <?php 
                        foreach( $bkap_weekdays as $w_value => $w_name ) {
                            if ( isset( $recurring_weekdays[ $w_value ] ) && 'on' == $recurring_weekdays[ $w_value ] ) {
                                printf( "<option value='%s'>%s</option>\n", $w_value, $w_name );
                            }
                        }
                        foreach( $specific_dates as $dates => $lockout ) {
                            printf( "<option value='%s'>%s</option>\n", $dates, $dates );
                        }    
                        ?>
                       <option name="all" value="all"><?php _e( 'All', 'woocommerce-booking' );?></option>
                    </select>
                </td>
                <td width="10%"><input id="bkap_from_time" type="text" name="quantity" style="width:100%;" title="Please enter time in 24 hour format e.g 14:00 or 03:00" placeholder="HH:MM" maxlength="5" onkeypress="return bkap_isNumberKey(event)"></td>
                <td width="10%"><input id="bkap_to_time" type="text" name="quantity" style="width:100%;" title="Please enter time in 24 hour format e.g 14:00 or 03:00" placeholder="HH:MM" maxlength="5" onkeypress="return bkap_isNumberKey(event)"></td>
                <td width="10%"><input id="bkap_lockout_time" type="number" name="quantity" style="width:100%;" min="0" placeholder="Max bookings"></td>
                <td width="10%"><input id="bkap_price_time"type="text" name="quantity" style="width:100%;" placeholder="Price"></td>
                <td width="10%" style="text-align:center;">
                    <label class="bkap_switch">
                       <input id="bkap_global_time" type="checkbox" name="bkap_global_timeslot" style="margin-left: 35%;">
                       <div class="bkap_slider round"></div>
                    </label>
                </td>
                <td width="23%"><textarea id="bkap_note_time" rows="1" cols="2" style="width:100%;"></textarea></td>
                <td width="4%" id="bkap_close" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
            </tr>
            <?php 
        }
        
    /**
     * This function saves the data from the tabs.
     * Different save buttons are present in each tab.
     * They all will call this function, which will check
     * the data present and save the same.
     * 
     * @since 4.0.0
     */

    public static function bkap_save_settings() {
        
        // Product ID
        $post_id = $_POST[ 'product_id' ];
        $product_id = bkap_common::bkap_get_product_id( $post_id );
        
        // Booking Options Tab settings
        $clean_booking_options = '';
        if ( isset( $_POST[ 'booking_options' ] ) ) {
            $post_booking_options = $_POST[ 'booking_options' ];
            $tempData = str_replace( "\\", "", $post_booking_options );
            $clean_booking_options = json_decode($tempData);

        } 
            
        // Settings Tab settings
        $clean_settings_data = '';
        if ( isset( $_POST[ 'settings_data' ] ) ) {
            $post_settings_data = $_POST[ 'settings_data' ];
            $tempData = str_replace( "\\", "", $post_settings_data );
            $clean_settings_data = json_decode($tempData);
            
        }
        
        $ranges_array = array();
        // Fixed Blocks Tab
        if ( isset( $_POST[ 'blocks_enabled' ] ) ) {
            $ranges_array[ 'blocks_enabled' ] = $_POST[ 'blocks_enabled' ];
        }
        
        // Fixed Block Booking table data.
        $clean_fixed_block_data = '';
        if ( isset( $_POST[ 'fixed_block_data' ] ) ) {
            $post_fixed_block_data = $_POST[ 'fixed_block_data' ];
            $tempData = str_replace( "\\", "", $post_fixed_block_data );
            $clean_fixed_block_data = json_decode( $tempData );
        }
        
        // Fixed Block Booking table data.
        $clean_price_range_data = '';
        if ( isset( $_POST[ 'price_range_data' ] ) ) {
            $post_price_range_data = $_POST[ 'price_range_data' ];
            $clean_price_range_data = (object) array( 
                'bkap_price_range_data' => stripslashes( $post_price_range_data ) );
        }
        
        
        // Price Ranges Tab
        if ( isset( $_POST[ 'ranges_enabled' ] ) ) {
            $ranges_array[ 'ranges_enabled' ] = $_POST[ 'ranges_enabled' ];
        }
        
        // GCal Tab
        $clean_gcal_data = '';
        if ( isset( $_POST[ 'gcal_data' ] ) ) {
            $post_gcal_data = $_POST[ 'gcal_data' ];
            $tempData = str_replace( "\\", "", $post_gcal_data );
            $clean_gcal_data = json_decode($tempData);
        }
        
        $booking_box_class = new bkap_booking_box_class();
        $booking_box_class->setup_data( $product_id, $clean_booking_options, $clean_settings_data, $ranges_array, $clean_gcal_data, $clean_fixed_block_data, $clean_price_range_data );
        die();
    }

    /**
     * Creates the data to be saved in the DB when either the Save button is 
     * clicked from one of the tabs or the product is Published/Updated.
     * 
     * @param int $product_id
     * @param stdClass $clean_booking_options
     * @param stdClass $clean_settings_data
     * 
     * @since 4.0.0
     */

    function setup_data( $product_id, $clean_booking_options, $clean_settings_data, $ranges_array, $clean_gcal_data, $clean_fixed_block_data, $clean_price_range_data ) {

        $final_booking_options  = array();
        $settings_data          = array();
        $block_ranges           = array();
        $gcal_data              = array();
        $fixed_block_data       = array();
        $price_range_data       = array();
        
        if ( $clean_booking_options != '' && count( $clean_booking_options ) > 0 ) {
            
            $final_booking_options[ '_bkap_enable_booking' ]        = $clean_booking_options->booking_enable_date;
            $final_booking_options[ '_bkap_booking_type' ]          = $clean_booking_options->booking_type;
            $final_booking_options[ '_bkap_enable_inline' ]         = $clean_booking_options->enable_inline;
            $final_booking_options[ '_bkap_purchase_wo_date' ]      = $clean_booking_options->purchase_wo_date;
            $final_booking_options[ '_bkap_requires_confirmation' ] = $clean_booking_options->requires_confirmation;
            
            if ( isset( $clean_booking_options->wkpbk_block_single_week ) && isset($clean_booking_options->special_booking_start_weekday ) && isset( $clean_booking_options->special_booking_end_weekday ) ) {

                $final_booking_options[ '_bkap_week_blocking']  = $clean_booking_options->wkpbk_block_single_week;
                $final_booking_options[ '_bkap_start_weekday' ] = $clean_booking_options->special_booking_start_weekday;
                $final_booking_options[ '_bkap_end_weekday' ]   = $clean_booking_options->special_booking_end_weekday;
            }
        
        }

        if ( $clean_settings_data != '' && count( $clean_settings_data ) > 0 ) {

            // Booking enabled
            if ( isset( $clean_booking_options->booking_enable_date ) && '' != $clean_booking_options->booking_enable_date ) {
                $booking_enabled = $clean_booking_options->booking_enable_date;
            } else {
                $booking_enabled = get_post_meta( $product_id, '_bkap_enable_booking', true );
            }
            
            // Booking Type
            if ( isset( $clean_booking_options->booking_type ) && '' != $clean_booking_options->booking_type ) {
                $booking_type = $clean_booking_options->booking_type;
            } else {
                $booking_type = get_post_meta( $product_id, '_bkap_booking_type', true );
            }
            
            $settings_data[ '_bkap_abp' ]               = $clean_settings_data->abp;
            $settings_data[ '_bkap_max_bookable_days' ] = $clean_settings_data->max_bookable;
            
            if ( isset( $clean_settings_data->date_lockout ) ) {
                $settings_data[ '_bkap_date_lockout' ]  = $clean_settings_data->date_lockout;
            }

            if ( isset( $clean_settings_data->min_days_multiple ) ) {
                $settings_data[ '_bkap_multiple_day_min' ] = $clean_settings_data->min_days_multiple;
            }

            if ( isset( $clean_settings_data->max_days_multiple ) ) {
                $settings_data[ '_bkap_multiple_day_max' ] = $clean_settings_data->max_days_multiple;
            }
            
            $booking_recurring  = array();
            $recurring_lockout  = array();
            $recurring_prices   = array();
            
            for ( $i = 0; $i <= 6; $i++ ) {
                $weekday_name   = "booking_weekday_$i";
                $lockout_name   = "weekday_lockout_$i";
                $price_name     = "weekday_price_$i";
            
                $booking_recurring[ $weekday_name ] = $clean_settings_data->$weekday_name;
                $recurring_lockout[ $weekday_name ] = isset( $clean_settings_data->$lockout_name ) ? $clean_settings_data->$lockout_name : 0;
            
                if ( is_numeric( $clean_settings_data->$price_name ) ) {
                    $recurring_prices[ $weekday_name ] = $clean_settings_data->$price_name;
                }
            }
            
            $enable_recurring = '';
            if ( in_array( 'on', $booking_recurring ) ) {
                $enable_recurring = 'on';
            }
            
            $settings_data[ '_bkap_enable_recurring' ]      = $enable_recurring;
            $settings_data[ '_bkap_recurring_weekdays' ]    = $booking_recurring;
            $settings_data[ '_bkap_recurring_lockout' ]     = $recurring_lockout;            
            $settings_data[ '_bkap_enable_specific' ]       = $clean_settings_data->enable_specific;            
            $settings_data[ '_bkap_product_holidays' ]      = $this->create_date_list( $clean_settings_data->holidays_list );

            //$settings_data[ '_bkap_specific_dates' ] = $this->create_date_list( $clean_settings_data->specific_list );
            if ( isset( $booking_type ) && 'multiple_days' != $booking_type ) {
                $settings_data[ '_bkap_specific_dates' ] = $this->create_date_list( $clean_settings_data->specific_list );
            } else {
                $settings_data[ '_bkap_specific_dates' ] = array();
            }
            
            $specific_prices = $this->create_specific_price_list( $clean_settings_data->specific_list );
            
            // update the special prices
            $special_price_class = new bkap_special_booking_price();
            $special_price_class->bkap_save_special_booking_price( $product_id, $recurring_prices, $specific_prices );
            
            $settings_data[ '_bkap_custom_ranges']      = $this->create_range_data( $clean_settings_data->custom_range );
            $settings_data[ '_bkap_holiday_ranges' ]    = $this->create_range_data( $clean_settings_data->holiday_range );
            $settings_data[ '_bkap_month_ranges' ]      = $this->create_range_data( $clean_settings_data->month_range );

            // date & time settings
            $booking_time_settings  = array();
            $existing_time_settings = get_post_meta( $product_id, '_bkap_time_settings', true );

            if ( isset( $clean_settings_data->booking_times ) && count( $clean_settings_data->booking_times ) > 0 ) {
                
                foreach ( $clean_settings_data->booking_times as $booking_times ) {
                    
                    $record_present = false; // assume no record is present for this date/day and time slot
                    $days = array();
                    
                    if ( is_array( $booking_times->day ) && count( $booking_times->day ) > 0 ) {
                        
                        foreach ( $booking_times->day as $day ) {
                            $days[] = $day;
                        }
                    } else {
                        $days[] = $booking_times->day;
                    }

                    // check if any of the values is set to 'ALL' if so, then unset it and insert records for all the values in the dropdown
                    foreach ( $days as $d_key => $d_value ) {
                    
                        if ( 'all' == $d_value ) {
                            
                            unset( $days[ $d_key ] );
                            
                            // add records for all the days/dates
                            foreach ( $booking_recurring as $b_key => $b_value ) {
                                if ( 'on' == $b_value ) {
                                    $days[] = $b_key;
                                }
                            }
                            
                            // specific dates
                            foreach ( $settings_data[ '_bkap_specific_dates' ] as $dates => $lockout ) {
                                $days[] = $dates;
                            }
                        }
                    }
                    
                    // for all the days
                    foreach ( $days as $day_check ) {
            
                        $from_slot_array    = explode( ':', $booking_times->from_time );
            
                        $from_slot_hrs      = trim( $from_slot_array[ 0 ] );
                        $from_slot_min      = trim( $from_slot_array[ 1 ] );

                        $from_slot_hrs      = ( $from_slot_hrs != "" ) ? $from_slot_hrs : '00';
                        $from_slot_min      = ( $from_slot_min != "" ) ? $from_slot_min : '00';
            
                        $to_slot_hrs        = '0';
                        $to_slot_min        = '00';
            
                        if ( isset( $booking_times->to_time ) && '' != $booking_times->to_time ) {
                            $to_slot_array = explode( ':', $booking_times->to_time );
            
                            $to_slot_hrs = trim( $to_slot_array[ 0 ] );
                            $to_slot_min = trim( $to_slot_array[ 1 ] );
                        }
            
                        // check if a record exists already
                        if ( is_array( $existing_time_settings ) && count( $existing_time_settings ) > 0 ) {
                            
                            // check if there's a record present for that day/date
                            if ( array_key_exists( $day_check, $existing_time_settings ) ) {
            
                                foreach( $existing_time_settings[ $day_check ] as $key => $existing_record ) {
            
                                    if ( $from_slot_hrs == $existing_record['from_slot_hrs']
                                        && $from_slot_min == $existing_record['from_slot_min']
                                        && $to_slot_hrs == $existing_record['to_slot_hrs']
                                        && $to_slot_min == $existing_record['to_slot_min'] ) {
                                            
                                        $new_key        =  $key;
                                        $record_present = true;
                                        break;
                                    }
                                }
                            }
                        }

                        if ( ! $record_present ) {
                            // check if there's a record present for that day/date
                            if ( array_key_exists( $day_check, $booking_time_settings ) ) {
                                $new_key = count( $booking_time_settings[ $day_check ] );
                            } else {
                                $new_key = 0;
                            }
                        }
            
                        $booking_time_settings[ $day_check ][ $new_key ][ 'from_slot_hrs' ]     = $from_slot_hrs;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'from_slot_min' ]     =  $from_slot_min;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'to_slot_hrs' ]       = $to_slot_hrs;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'to_slot_min' ]       =  $to_slot_min;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'booking_notes' ]     = $booking_times->booking_notes;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'slot_price' ]        = $booking_times->slot_price;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'lockout_slot' ]      = $booking_times->lockout_slot;
                        $booking_time_settings[ $day_check ][ $new_key ][ 'global_time_check' ] = $booking_times->global_time_check;
                    }
                }
            
                if ( is_array( $booking_time_settings ) ) {
                    $settings_data[ '_bkap_time_settings' ] = $booking_time_settings;
                }
            }
        }
        
        if ( isset( $ranges_array[ 'blocks_enabled' ] ) ) {
            $block_ranges[ '_bkap_fixed_blocks' ] = $ranges_array[ 'blocks_enabled' ];
        }
        
        
        // Fixed Block bookings data.
        if( $clean_fixed_block_data != '' && count( $clean_fixed_block_data ) > 0 ) {
            $block_ranges['_bkap_fixed_blocks_data'] = bkap_block_booking::bkap_updating_fixed_block_data_in_db( $product_id, $clean_fixed_block_data );
            
        }
        
        if ( isset( $ranges_array[ 'ranges_enabled' ] ) ) {
            $block_ranges[ '_bkap_price_ranges' ] = $ranges_array[ 'ranges_enabled' ];
        }

        // Price by range of day data.
        if( $clean_price_range_data != '' && count( $clean_price_range_data ) > 0 ) {        
            $block_ranges['_bkap_price_range_data'] = bkap_block_booking::bkap_updating_price_range_data_in_db( $product_id, $clean_price_range_data );       
        }
          
        if ( $clean_gcal_data != '' && count( $clean_gcal_data ) > 0 ) {
        
            $gcal_data[ '_bkap_gcal_integration_mode' ] = $clean_gcal_data->gcal_sync_mode;
            $gcal_data[ '_bkap_gcal_key_file_name' ]    = $clean_gcal_data->key_file_name;
            $gcal_data[ '_bkap_gcal_service_acc' ]      = $clean_gcal_data->service_acc_email;
            $gcal_data[ '_bkap_gcal_calendar_id' ]      = $clean_gcal_data->calendar_id;
        
            if ( isset( $clean_gcal_data->gcal_auto_mapping ) ) {
                $gcal_data[ '_bkap_enable_automated_mapping' ] = $clean_gcal_data->gcal_auto_mapping;
            }
        
            if ( isset( $clean_gcal_data->default_variation ) ) {
                $gcal_data[ '_bkap_default_variation' ] = $clean_gcal_data->default_variation;
            }
        
            $import_feed_url = array();

            for ( $i = 0; ; $i++ ) {
                $field_name = "ics_feed_url_$i";
        
                if ( isset( $clean_gcal_data->$field_name ) ) {
                    $import_feed_url[ $i ] = $clean_gcal_data->$field_name;
                } else {
                    break;
                }
            }
            $gcal_data[ '_bkap_import_url' ] = $import_feed_url;
        
        }
        
        // update individual settings
        $this->update_single_post_meta( $product_id, $final_booking_options, $settings_data, $block_ranges, $gcal_data );
        
        // update old post meta record
        $this->update_serialized_post_meta( $product_id, $final_booking_options, $settings_data, $block_ranges, $gcal_data );
        
        // update booking history
        if ( ( isset( $booking_enabled ) && 'on' == $booking_enabled ) && isset( $booking_type ) && 'only_day' == $booking_type ) {
            $this->update_bkap_history_only_days( $product_id, $settings_data );
        } else if ( ( isset( $booking_enabled ) && 'on' == $booking_enabled ) && isset( $booking_type ) && 'date_time' == $booking_type ) {
            $this->update_bkap_history_date_time( $product_id, $settings_data );
        }
        
    }
            
    /**
     * Receives a string which contains a list of dates and
     * the number of recurring years. It splits it into an array
     * where the date is the key and the number of years is the value
     *
     * @param str $dates_string
     * String format is as below:
     * date1,date2+years;date3+years;....
     * @return array $dates_array
     * 
     * @since 4.0.0
     */

    function create_date_list( $dates_string ) {
    
        $dates_array = array();
    
        $dates_split = explode( ';', $dates_string );
    
        // if dates have been set up
        if ( is_array( $dates_split ) && count( $dates_split ) > 0 ) {
            foreach( $dates_split as $d_value ) {
                if ( $d_value != '' ) {
    
                    $dates_list = $d_value;
                    $recur_years = 0;
                    // recurring years and prices are added using +
                    $recurring_setup = strpos( $d_value, '+' );
                    // check if recurring years have been setup
                    if ( $recurring_setup !== false ) {
                        $dates_list = substr( $d_value, 0, $recurring_setup );
                        $explode_dates = explode( '+', $d_value );
                        if ( $explode_dates[ 1 ] > 0 ) {
                            $recur_years = $explode_dates[ 1 ];
                        }
                    }
                    // get the dates list, there maybe more than 1 dates comma separated
                    $explode_dates = explode( ',', $dates_list );
                    foreach ( $explode_dates as $single_date ) {
                        if ( '' != $single_date ) {
                            $dates_array[ $single_date ] = $recur_years;
                        }
                    }
    
                }
            }
        }
    
        return $dates_array;
    }
    
    /**
     * Creates and returns an array of specific dates with their prices
     * 
     * @param string $dates_string - Specific Dates with the prices
     * @since 4.0.0
     * @return array $dates_array - [date] = 'Price'
     */

    function create_specific_price_list( $dates_string ) {
    
        $dates_array = array();
    
        $dates_split = explode( ';', $dates_string );
    
        // if dates have been set up
        if ( is_array( $dates_split ) && count( $dates_split ) > 0 ) {
            foreach( $dates_split as $d_value ) {
                if ( $d_value != '' ) {
    
                    $dates_list = $d_value;
                    $recur_years = 0;
                    $date_price = '';
    
                    $recurring_setup = strpos( $d_value, '+' );
                    // check if recurring years & price have been setup
                    if ( $recurring_setup !== false ) {
                        $dates_list = substr( $d_value, 0, $recurring_setup );
                        $explode_dates = explode( '+', $d_value );
                        // check if price is set
                        if ( isset( $explode_dates[ 2 ] ) && is_numeric( $explode_dates[ 2 ] ) && $explode_dates[ 2 ] > 0 ) {
                            $date_price = $explode_dates[ 2 ];
                        }
                    }
                    // get the dates list
                    $explode_dates = explode( ',', $dates_list );
                    foreach ( $explode_dates as $single_date ) {
                        if ( '' != $single_date && is_numeric( $date_price ) ) {
                            $dates_array[ $single_date ] = $date_price;
                        }
                    }
    
                }
            }
        }
    
        return $dates_array;
    }
    
    /**
     * Returns an array of the custom ranges data.
     * This includes the Ranges setup using 'Custom Range' as
     * well as 'Range of Months'.
     * 
     * @param string $range_string - Contains the data passed from the table in string format
     * @return array $range_array - Array with keys 'start', 'end', years_to_recur' and 'range_type'
     * @global array $bkap_month array of months 
     *    
     * @since 4.0.0
     */

    function create_range_data( $range_string ) {
        global $bkap_months;
        $range_array = array();
    
        $range_split = explode( ';', $range_string );
    
        $current_year = date( 'Y', current_time( 'timestamp' ) );
        $next_year = date( 'Y', strtotime( '+1 year' ) );
        
        // if ranges have been set up
        if ( is_array( $range_split ) && count( $range_split ) > 0 ) {
            foreach( $range_split as $r_value ) {
                if ( $r_value != '' ) {
                    $range_start = '';
                    $range_end   = '';
                    $range_type  = '';
                    $range_recur = 0;
    
                    $explode_range = explode( '+', $r_value );
    
                    if ( isset( $explode_range[ 0 ] ) ) {
                        $range_start = $explode_range[ 0 ];
                        if ( is_numeric( $range_start ) ) { // it's a month number
                            $month_name = $bkap_months[ $range_start ];
                            $month_to_use = "$month_name $current_year";
                            $range_start = date ( 'j-n-Y', strtotime( $month_to_use ) );
                        } else { // it is a date
                            if ( $range_start == '' ) {
                                continue; // pick the next range
                            } else {
                                $range_start = date ( 'j-n-Y', strtotime( $range_start ) );
                            }
                        }
                    }
                    if ( isset( $explode_range[ 1 ] ) ) {
                        $range_end = $explode_range[ 1 ];
                        if ( is_numeric( $range_end ) ) { // it's a month number
                            $month_name = $bkap_months[ $range_end ];
                            
                            if ( $explode_range[ 0 ] <= $explode_range[ 1 ] ) {
                                $month_to_use = "$month_name $current_year";
                            } else {
                                $month_to_use = "$month_name $next_year";
                            }
                            $month_start = date ( 'j-n-Y', strtotime( $month_to_use ) );
                            
                            $days = date( 't', strtotime( $month_start ) );
                            $days -= 1;
                            $range_end = date ( 'j-n-Y', strtotime( "+$days days", strtotime( $month_start ) ) );
                            
                        } else { // it is a date
                            if ( $range_end == '' ) {
                                continue; // pick the next range
                            } else {
                                $range_end = date( 'j-n-Y', strtotime( $range_end ) );
                            }
                        }
                    }
                    if ( isset( $explode_range[ 2 ] ) ) {
                        $range_recur = $explode_range[ 2 ];
                    }

                    if ( isset( $explode_range[ 3 ] ) ) {
                        $range_type = $explode_range[ 3 ];
                    }
    
                    $range_array[] = array( 'start' => $range_start,
                                            'end' => $range_end,
                                            'years_to_recur' => $range_recur,
                                            'range_type' => $range_type
                                        );
    
                }
            }
        }
        return $range_array;
    }

    /**
     * Updates the individual booking settings data in the 
     * post meta table.
     * 
     * @param int $product_id - Product ID
     * @param array $booking_options - Data for Booking Options tab
     * @param array $settings_data - Data for Availability tab
     * @param array $block_ranges - Data for Block Pricing tab
     * @param $gcal_data - Data for Google Sync Settings tab
     * 
     * @since 4.0.0
     */

     function update_single_post_meta( $product_id, $booking_options, $settings_data, $block_ranges, $gcal_data ) {
        
        if ( is_array( $booking_options ) && count( $booking_options ) > 0 ) {
            foreach( $booking_options as $booking_key => $booking_value ) {
                update_post_meta( $product_id, $booking_key, $booking_value );
            }
        }
        
        if ( is_array( $settings_data ) && count( $settings_data ) > 0 ) {
            foreach( $settings_data as $settings_key => $settings_value ) {
                update_post_meta( $product_id, $settings_key, $settings_value );
            }
        }
        
        if ( is_array( $block_ranges ) && count( $block_ranges ) > 0 ) {
            foreach( $block_ranges as $br_keys => $br_values ) {
                update_post_meta( $product_id, $br_keys, $br_values );
            }
        }
        
        if ( is_array( $gcal_data ) && count( $gcal_data ) > 0 ) {
            foreach( $gcal_data as $gcal_key => $gcal_value ) {
                update_post_meta( $product_id, $gcal_key, $gcal_value );
            }
        }
    }
    
    /**
     * Updates the 'woocommerce_booking_settings' record in 
     * postmeta table for the product.
     * 
     * @param int $product_id - Product ID
     * @param array $booking_options - Data for Booking Options tab
     * @param array $settings_data - Data for Availability tab
     * @param array $block_ranges - Data for Block Pricing tab
     * @param $gcal_data - Data for Google Sync Settings tab
     * 
     * @since 4.0.0
     */

    function update_serialized_post_meta( $product_id, $booking_options, $settings_data, $block_ranges, $gcal_data ) {
    
        // Save Bookings
        $updated_settings = array();
    
        if ( isset( $booking_options ) && is_array( $booking_options ) && count( $booking_options ) > 0 ) {
    
            if ( isset( $booking_options[ '_bkap_enable_booking' ] ) ) {
                $updated_settings[ 'booking_enable_date' ] = $booking_options[ '_bkap_enable_booking' ];
            } else {
                $updated_settings[ 'booking_enable_date' ] = '';
            }
    
            if ( isset( $booking_options[ '_bkap_booking_type' ] ) && '' != $booking_options[ '_bkap_booking_type' ] ) {
    
                if ( 'date_time' == $booking_options[ '_bkap_booking_type' ] ) {
                    $updated_settings[ 'booking_enable_multiple_day' ] = '';
                    $updated_settings[ 'booking_enable_time' ] = 'on';
                } else if ( 'multiple_days' == $booking_options[ '_bkap_booking_type' ] ) {
                    $updated_settings[ 'booking_enable_multiple_day' ] = 'on';
                    $updated_settings[ 'booking_enable_time' ] = '';
                } else if ( 'only_day' == $booking_options[ '_bkap_booking_type' ] ) {
                    $updated_settings[ 'booking_enable_multiple_day' ] = '';
                    $updated_settings[ 'booking_enable_time' ] = '';
                }
            }
    
            if ( isset( $booking_options[ '_bkap_enable_inline' ] ) ) {
                $updated_settings[ 'enable_inline_calendar' ] = $booking_options[ '_bkap_enable_inline' ];
            } else {
                $updated_settings[ 'enable_inline_calendar' ] = '';
            } 
            
            if ( isset( $booking_options[ '_bkap_purchase_wo_date' ] ) ) {
                $updated_settings[ 'booking_purchase_without_date' ] = $booking_options[ '_bkap_purchase_wo_date' ];
            } else {
                $updated_settings[ 'booking_purchase_without_date' ] = '';
            } 
             
            if ( isset( $booking_options[ '_bkap_requires_confirmation' ] ) ) {
                $updated_settings[ 'booking_confirmation' ] = $booking_options[ '_bkap_requires_confirmation' ];
            } else {
                $updated_settings[ 'booking_confirmation' ] = '';
            } 

            if( isset( $booking_options[ '_bkap_week_blocking' ] ) ) {
                $updated_settings[ 'wkpbk_block_single_week' ] =  $booking_options[ '_bkap_week_blocking' ];
            }else {
                $updated_settings[ 'wkpbk_block_single_week' ] = '';
            }

            if( isset( $booking_options[ '_bkap_start_weekday' ] ) ) {
                $updated_settings[ 'special_booking_start_weekday' ] =  $booking_options[ '_bkap_start_weekday' ];
            }else {
                $updated_settings[ 'special_booking_start_weekday' ] = '';
            }

            if( isset( $booking_options[ '_bkap_end_weekday' ] ) ) {
                $updated_settings[ 'special_booking_end_weekday' ] =  $booking_options[ '_bkap_end_weekday' ];
            }else {
                $updated_settings[ 'special_booking_end_weekday' ] = '';
            }
        }
    
        if ( isset( $settings_data ) && is_array( $settings_data ) && count( $settings_data ) > 0 ) {
        
            //product level - minimum booking for multiple days
            $multiple_min_days = 0;
            if ( isset( $settings_data[ '_bkap_multiple_day_min' ] ) && $settings_data[ '_bkap_multiple_day_min' ] > 0 ) {
                $updated_settings[ 'booking_minimum_number_days_multiple' ] = $settings_data[ '_bkap_multiple_day_min' ];
                $updated_settings[ 'enable_minimum_day_booking_multiple' ] = 'on';
            } else {
                $updated_settings[ 'enable_minimum_day_booking_multiple' ] = '';
                $updated_settings[ 'booking_minimum_number_days_multiple' ] = 0;
            }
    
            $multiple_max_days = 365;
            if ( isset( $settings_data[ '_bkap_multiple_day_max' ] ) && $settings_data[ '_bkap_multiple_day_max' ] > 0 ) {
                $multiple_max_days = $settings_data[ '_bkap_multiple_day_max' ];
            }
            $updated_settings[ 'booking_maximum_number_days_multiple' ] = $multiple_max_days;
            
            if ( isset( $settings_data[ '_bkap_custom_ranges' ] ) ) {
                $updated_settings[ 'booking_date_range' ] = $settings_data[ '_bkap_custom_ranges' ];
            } else {
                $updated_settings[ 'booking_date_range' ] = array();
            } 
            
            if ( isset( $settings_data[ '_bkap_abp' ] ) ) {
                $updated_settings[ 'booking_minimum_number_days' ] = $settings_data[ '_bkap_abp' ];
            } else {
                $updated_settings[ 'booking_minimum_number_days' ] = 0;
            } 
    
            if ( isset( $settings_data[ '_bkap_max_bookable_days' ] ) ) {
                $updated_settings[ 'booking_maximum_number_days' ] = $settings_data[ '_bkap_max_bookable_days' ];
            } else {
                $updated_settings[ 'booking_maximum_number_days' ] = '';
            } 
    
            if ( isset( $settings_data[ '_bkap_date_lockout' ] ) ) {
                $updated_settings[ 'booking_date_lockout' ] = $settings_data[ '_bkap_date_lockout' ];
            } else {
                $updated_settings[ 'booking_date_lockout' ] = '';
            } 
            
            if ( isset( $settings_data[ '_bkap_product_holidays' ] ) ) {
                $updated_settings[ 'booking_product_holiday' ] = $settings_data[ '_bkap_product_holidays' ];
            } else {
                $updated_settings[ 'booking_product_holiday' ] = array();
            } 
            
            if ( isset( $settings_data[ '_bkap_specific_dates' ] ) ) {
                $updated_settings[ 'booking_specific_date' ] = $settings_data[ '_bkap_specific_dates' ];
            } else {
                $updated_settings[ 'booking_specific_date' ] = array();
            } 
            
            if ( isset( $settings_data[ '_bkap_enable_recurring' ] ) ) {
                $updated_settings[ 'booking_recurring_booking' ] = $settings_data[ '_bkap_enable_recurring' ];
            } else {
                $updated_settings[ 'booking_recurring_booking' ] = '';
            } 
            
            if ( isset( $settings_data[ '_bkap_recurring_weekdays' ] ) ) {
                $updated_settings[ 'booking_recurring' ] = $settings_data[ '_bkap_recurring_weekdays' ];
            } else {
                $updated_settings[ 'booking_recurring' ] = array();
            } 
            
            if ( isset( $settings_data[ '_bkap_recurring_lockout' ] ) ) {
                $updated_settings[ 'booking_recurring_lockout' ] = $settings_data[ '_bkap_recurring_lockout' ];
            } else {
                $updated_settings[ 'booking_recurring_lockout' ] = array();
            } 

            if ( isset( $settings_data[ '_bkap_enable_specific' ] ) ) {
                $updated_settings[ 'booking_specific_booking' ] = $settings_data[ '_bkap_enable_specific' ];
            } else {
                $updated_settings[ 'booking_specific_booking' ] = '';
            } 
            
            if ( isset( $settings_data[ '_bkap_time_settings' ] ) ) {
                $updated_settings[ 'booking_time_settings' ] = $settings_data[ '_bkap_time_settings' ];
            } else {
                $updated_settings[ 'booking_time_settings' ] = array();
            } 
        }
    
        if ( isset( $block_ranges ) && is_array( $block_ranges ) && count( $block_ranges ) > 0 ) {
            if( isset( $block_ranges[ '_bkap_fixed_blocks' ] ) ) {
                $updated_settings[ 'booking_fixed_block_enable' ] = $block_ranges[ '_bkap_fixed_blocks' ];
            } 
        
            if ( isset( $block_ranges[ '_bkap_price_ranges' ] ) ) {
                $updated_settings[ 'booking_block_price_enable' ] = $block_ranges[ '_bkap_price_ranges' ];
            }

            if( isset( $block_ranges[ '_bkap_price_range_data' ] ) ){
                $updated_settings[ 'bkap_price_range_data' ] = $block_ranges[ '_bkap_price_range_data' ];
            }
            
            if( isset( $block_ranges[ '_bkap_fixed_blocks_data' ] ) ){
                $updated_settings[ 'bkap_fixed_blocks_data' ] = $block_ranges[ '_bkap_fixed_blocks_data' ];
            }
        }
        
        if ( isset( $gcal_data ) && is_array( $gcal_data ) && count( $gcal_data ) > 0 ) {
        
            if ( isset( $gcal_data[ '_bkap_gcal_integration_mode' ] ) ) {
                $updated_settings[ 'product_sync_integration_mode' ] = $gcal_data[ '_bkap_gcal_integration_mode' ];
            } else {
                $updated_settings[ 'product_sync_integration_mode' ] = '';
            } 
        
            if ( isset( $gcal_data[ '_bkap_gcal_key_file_name' ] ) ) {
                $updated_settings[ 'product_sync_key_file_name' ] = $gcal_data[ '_bkap_gcal_key_file_name' ];
            } else {
                $updated_settings[ 'product_sync_key_file_name' ] = '';
            } 
        
            if ( isset( $gcal_data[ '_bkap_gcal_service_acc' ] ) ) {
                $updated_settings[ 'product_sync_service_acc_email_addr' ] = $gcal_data[ '_bkap_gcal_service_acc' ];
            } else {
                $updated_settings[ 'product_sync_service_acc_email_addr' ] = '';
            } 
        
            if ( isset( $gcal_data[ '_bkap_gcal_calendar_id' ] ) ) {
                $updated_settings[ 'product_sync_calendar_id' ] = $gcal_data[ '_bkap_gcal_calendar_id' ];
            } else {
                $updated_settings[ 'product_sync_calendar_id' ] = '';
            } 
        
            if ( isset( $gcal_data[ '_bkap_enable_automated_mapping' ] ) ) {
                $updated_settings[ 'enable_automated_mapping' ] = $gcal_data[ '_bkap_enable_automated_mapping' ];
            } else {
                $updated_settings[ 'enable_automated_mapping' ] = '';
            } 
        
            if ( isset( $gcal_data[ '_bkap_default_variation' ] ) ) {
                $updated_settings[ 'gcal_default_variation' ] = $gcal_data[ '_bkap_default_variation' ];
            } else {
                $updated_settings[ 'gcal_default_variation' ] = '';
            }
        
            if ( isset( $gcal_data[ '_bkap_import_url' ] ) ) {
                $updated_settings[ 'ics_feed_url' ] = $gcal_data[ '_bkap_import_url' ];
            } else {
                $updated_settings[ 'ics_feed_url' ] = array();
            } 
        
        }
        
        // Fetch the existing settings
        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
        // Merge the existing settings with the updated ones
        $final_settings = ( is_array( $booking_settings ) && count( $booking_settings ) > 0 ) ? array_merge( $booking_settings, $updated_settings ) : $updated_settings;
        // update post meta
        update_post_meta( $product_id, 'woocommerce_booking_settings', $final_settings );
    
    }
    
    /**
     * Updates the Booking History table for Only Days
     * Booking Type
     * 
     * @param int $product_id - Product ID
     * @param array $settings_data - Data for Availability tab     
     * @global object $wpdb Global wpdb Object
     * 
     * @since 4.0.0
     */

    function update_bkap_history_only_days( $product_id, $settings_data ) {
    
        if ( count( $settings_data ) > 0 ) {
    
            global $wpdb;
    
            $recurring_array = $settings_data[ '_bkap_recurring_weekdays' ];
            $recurring_lockout = $settings_data[ '_bkap_recurring_lockout' ];
            $specific_array = $settings_data[ '_bkap_specific_dates' ];
            
            // recurring days and lockout update
            if ( count ( $recurring_array ) > 0 && count( $recurring_lockout ) > 0 ) {
    
                foreach( $recurring_array as $weekday => $w_status ) {
    
                    if ( 'on' == $w_status ) { // weekday is enabled
    
                        $insert = true;
                        $available_booking = $recurring_lockout[ $weekday ];
                        $updated_lockout = $recurring_lockout[ $weekday ];
    
                        // check if the weekday is already present
                        $check_weekday_query = "SELECT total_booking, available_booking FROM `" . $wpdb->prefix . "booking_history`
                                                WHERE post_id = %d
                                                AND weekday = %s
                                                AND start_date = '0000-00-00'
                                                AND status = ''";
    
                        $check_weekday = $wpdb->get_results( $wpdb->prepare( $check_weekday_query, $product_id, $weekday ) );
    
                        // if yes, then update the lockout
                        if ( isset( $check_weekday ) && count( $check_weekday ) > 0 ) { // there will be only 1 active record at any given time
                            $insert            =   false;
                            if ( is_numeric($recurring_lockout[ $weekday ] ) && $recurring_lockout[ $weekday ] > 0 ) {
                                $change_in_lockout =   $recurring_lockout[ $weekday ] - $check_weekday[ 0 ]->total_booking;
                            } else if ( $recurring_lockout[ $weekday ] === '' || $recurring_lockout[ $weekday ] == 0 ) { // unlimited bookings
                                $change_in_lockout = 0;
                            }
                            
                        } else {
                            // if not found, check if there's a date record present
                            $existing_lockout = "SELECT total_booking FROM `" . $wpdb->prefix . "booking_history`
                                                                                WHERE post_id = %d
                                                                                AND start_date != '0000-00-00'
                                                                                AND weekday = %s
                                                                                ORDER BY id DESC LIMIT 1";
                            $lockout_results = $wpdb->get_results( $wpdb->prepare( $existing_lockout, $product_id, $weekday ) );
    
                            if ( isset( $lockout_results ) && count( $lockout_results ) > 0 ) {
                                if ( is_numeric( $recurring_lockout[ $weekday] ) && $recurring_lockout[ $weekday] > 0 ) {
                                    $change_in_lockout = $recurring_lockout[ $weekday] - $lockout_results[ 0 ]->total_booking;
                                    $available_booking = $lockout_results[ 0 ]->total_booking + $change_in_lockout;
                                } else if ( $recurring_lockout[ $weekday] === '' || $recurring_lockout[ $weekday] == 0 ) {
                                    $change_in_lockout = 0;
                                    $available_booking = 0;
                                }
                            }
    
                        }
    
                        if ( $insert ) {
                            $query_insert    =  "INSERT INTO `" . $wpdb->prefix . "booking_history`
                                                (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                                VALUES (
                                                '" . $product_id . "',
                                                '" . $weekday . "',
                                                '0000-00-00',
                                                '0000-00-00',
                                                '',
                                                '',
                                                '" . $updated_lockout . "',
                                                '" . $available_booking . "' )";
                            $wpdb->query( $query_insert );
                        } else if ( isset( $change_in_lockout ) && is_numeric( $change_in_lockout ) ) {
                            
                            // Update the existing record so that lockout is managed and orders do not go missing frm the View bookings page
                            if ( $change_in_lockout == 0 && ( $recurring_lockout[ $weekday ] === '' || $recurring_lockout[ $weekday ] == 0 ) ) { // unlimited bookings
                                
                                $query_update    =  "UPDATE `" . $wpdb->prefix . "booking_history`
                                                    SET total_booking = '" . $updated_lockout . "',
                                                    available_booking = '" . $change_in_lockout . "'
                                                    WHERE post_id = '" . $product_id . "'
                                                    AND weekday = '" . $weekday . "'
                                                    AND start_date = '0000-00-00'
                                                    AND status = ''";
                            } else {
                                $query_update    =  "UPDATE `" . $wpdb->prefix . "booking_history`
                                                    SET total_booking = '" . $updated_lockout . "',
                                                    available_booking = available_booking + '" . $change_in_lockout . "'
                                                    WHERE post_id = '" . $product_id . "'
                                                    AND weekday = '" . $weekday . "'
                                                    AND start_date = '0000-00-00'
                                                    AND status = ''";
                            }
                            $wpdb->query( $query_update );
                        }
    
                        if ( isset( $change_in_lockout ) && is_numeric( $change_in_lockout ) ) {
                            
                            //Update the existing records for the dates
                            if ( $change_in_lockout == 0 && ( $recurring_lockout[ $weekday ] === '' || $recurring_lockout[ $weekday ] == 0 ) ) { // unlimited bookings
                                
                                $query_update        =   "UPDATE `" . $wpdb->prefix . "booking_history`
                                                        SET total_booking = '" . $updated_lockout . "',
                                                        available_booking = '" . $change_in_lockout . "',
                                                        status = ''
                                                        WHERE post_id = '" . $product_id . "'
                                                        AND weekday = '" . $weekday . "'
                                                        AND start_date <> '0000-00-00'";
                                
                            } else {
                                $query_update        =   "UPDATE `" . $wpdb->prefix . "booking_history`
                                                        SET total_booking = '" . $updated_lockout . "',
                                                        available_booking = available_booking + '" . $change_in_lockout . "',
                                                        status = ''
                                                        WHERE post_id = '" . $product_id . "'
                                                        AND weekday = '" . $weekday . "'
                                                        AND start_date <> '0000-00-00'";
                            }
                        
                            $wpdb->query( $query_update );
                        }
                    } else { // weekday is disabled
    
                        // if a record exists in the table, it needs to be deactivated
                        $update_query = "UPDATE `" . $wpdb->prefix . "booking_history`
                                        SET status = 'inactive'
                                        WHERE post_id = %d
                                        AND weekday = %s";
                        $wpdb->query( $wpdb->prepare( $update_query, $product_id, $weekday ) );
    
                        // Delete the base records for the recurring weekdays
                        $delete_base_query  =   "DELETE FROM `" . $wpdb->prefix."booking_history`
                                                    WHERE post_id = '" . $product_id ."'
                                                    AND weekday = '" .  $weekday . "'
                                                    AND start_date = '0000-00-00'";
    
                        $wpdb->query( $delete_base_query );
                    }
    
                }
            }
            
            if ( is_array( $specific_array ) && count( $specific_array ) > 0 ) {
            
                foreach( $specific_array as $specific_date => $specific_lockout ) {

                    $specific_date = date( 'Y-m-d', strtotime( $specific_date ) );
                    
                    $insert = true;
                    $available_booking = $specific_lockout;
                    $updated_lockout = $specific_lockout;

                    $check_date_query1 = "SELECT total_booking, available_booking FROM `" . $wpdb->prefix . "booking_history`
                                                WHERE post_id = %d
                                                AND weekday != ''
                                                AND start_date = %s
                                                AND status = ''";
            
                    $check_date1 = $wpdb->get_results( $wpdb->prepare( $check_date_query1, $product_id, $specific_date ) );

                    if( count( $check_date1 ) > 0 ) {

                        $query_update1  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET weekday = '',                                                
                                                status = ''
                                                WHERE post_id = '" . $product_id . "'
                                                AND start_date = '" . $specific_date . "'";

                        $wpdb->query( $query_update1 );
                    }
            
                    // check if the date is already present
                    $check_date_query = "SELECT total_booking, available_booking FROM `" . $wpdb->prefix . "booking_history`
                                                WHERE post_id = %d
                                                AND weekday = ''
                                                AND start_date = %s
                                                AND status = ''";
            
                    $check_date = $wpdb->get_results( $wpdb->prepare( $check_date_query, $product_id, $specific_date ) );
            
                    // if yes, then update the lockout
                    if ( isset( $check_date ) && count( $check_date ) > 0 ) { // there will be only 1 active record at any given time
                        $insert            =   false;
                        if ( is_numeric( $specific_lockout ) && $specific_lockout > 0 ) {
                            $change_in_lockout =   $specific_lockout - $check_date[ 0 ]->total_booking;
                        } else if ( $specific_lockout === '' || $specific_lockout == 0 ) { // unlimited bookings
                            $change_in_lockout = 0;
                        }
                    } else {
                        // if not found, check if there's an inactive date record present
                        $existing_lockout = "SELECT total_booking FROM `" . $wpdb->prefix . "booking_history`
                                                                                WHERE post_id = %d
                                                                                AND start_date = %s
                                                                                AND weekday = ''
                                                                                AND status <> ''";
                        $lockout_results = $wpdb->get_results( $wpdb->prepare( $existing_lockout, $product_id, $specific_date ) );
            
                        if ( isset( $lockout_results ) && count( $lockout_results ) > 0 ) {
                            $insert = false;
                            if ( is_numeric( $specific_lockout ) && $specific_lockout > 0 ) {
                                $change_in_lockout = $specific_lockout - $lockout_results[ 0 ]->total_booking;
                            } else if ( $specific_lockout === '' || $specific_lockout == 0 ) { // unlimited bookings
                                $change_in_lockout = 0;
                            }
                        }
            
                    }
            
                    if ( $insert ) {
                        $query_insert  =   "INSERT INTO `" . $wpdb->prefix . "booking_history`
                                            (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                            VALUES (
                                            '" . $product_id . "',
                                            '',
                                            '" . $specific_date . "',
                                            '0000-00-00',
                                            '',
                                            '',
                                            '" . $specific_lockout . "',
                                            '" . $available_booking . "' )";
                        $wpdb->query( $query_insert );
                    } else if ( isset( $change_in_lockout ) && is_numeric( $change_in_lockout ) ) {
                        
                        // Update the existing record so that lockout is managed and orders do not go missing frm the View bookings page
                        if ( $change_in_lockout == 0 && ( $specific_lockout === '' || $specific_lockout == 0 ) ) { // unlimited bookings
                            
                            $query_update  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET total_booking = '" . $specific_lockout . "',
                                                available_booking = '" . $change_in_lockout . "',
                                                status = ''
                                                WHERE post_id = '" . $product_id . "'
                                                AND start_date = '" . $specific_date . "'";
                            
                        } else {
                            $query_update  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET total_booking = '" . $specific_lockout . "',
                                                available_booking = available_booking + '" . $change_in_lockout . "',
                                                status = ''
                                                WHERE post_id = '" . $product_id . "'
                                                AND start_date = '" . $specific_date . "'";
                        }
                        $wpdb->query( $query_update );
                    }
            
            
                }
            }
    
        }
    }
    
    /**
     * Updates the Booking History table for Date & Time
     * Booking Type
     * 
     * @param int $product_id - Product ID
     * @param array $settings_data - Data for Availability tab
     * @global object $wpdb Global wpdb Object
     * 
     * @since 4.0.0
     */

    function update_bkap_history_date_time( $product_id, $settings_data ) {
    
        if ( count( $settings_data ) > 0 ) {
    
            global $wpdb;
    
            $booking_time_settings = $settings_data[ '_bkap_time_settings' ];
    
            // recurring days and lockout update
            if ( is_array ( $booking_time_settings ) && count( $booking_time_settings ) > 0 ) {
    
                foreach( $booking_time_settings as $day => $s_data ) {
    
                    if ( 'booking' == substr( $day, 0, 7 ) ) { // recurring weekdays
                         
                        foreach ( $s_data as $time_data ) {
    
                            $insert = true;
                            $available_booking = $time_data[ 'lockout_slot' ];
                            $updated_lockout = $time_data[ 'lockout_slot' ];
    
                            $from_time = $time_data[ 'from_slot_hrs' ] . ':' . $time_data[ 'from_slot_min' ];
                            $to_time = $time_data[ 'to_slot_hrs' ] . ':' . $time_data[ 'to_slot_min' ];
    
                            if ( $to_time == '0:00' ) {
                                $to_time = '';
                            }
                            
                            $from_db = date( 'H:i', strtotime( $from_time ) );
                            $to_db = date( 'H:i', strtotime( $to_time ) );
                            
                            $from_gi = date( 'G:i', strtotime( $from_time ) );
                            $to_gi = date( 'G:i', strtotime( $to_time ) );
                            
                            // check if the weekday is already present
                            // Duplicate records were being inserted when openended timeslot becasue DATE_TIME of blank returns no records.
                // Hence in below if, we are not comparing with DATE_TIME function.
                 
                            if ( $to_time == '' ) {
                                $check_weekday_query = "SELECT total_booking, available_booking FROM `" . $wpdb->prefix . "booking_history`
                                                        WHERE post_id = %d
                                                        AND weekday = %s
                                                        AND start_date = '0000-00-00'
                                                        AND TIME_FORMAT( from_time, '%H:%i' ) = %s
                                                        AND to_time = %s
                                                        AND status = ''";
        
                                $check_weekday = $wpdb->get_results( $wpdb->prepare( $check_weekday_query, $product_id, $day, $from_db, $to_time ) );
                            } else {
                                $check_weekday_query = "SELECT total_booking, available_booking FROM `" . $wpdb->prefix . "booking_history`
                                                        WHERE post_id = %d
                                                        AND weekday = %s
                                                        AND start_date = '0000-00-00'
                                                        AND TIME_FORMAT( from_time, '%H:%i' ) = %s
                                                        AND TIME_FORMAT( to_time, '%H:%i' ) = %s
                                                        AND status = ''";
        
                                $check_weekday = $wpdb->get_results( $wpdb->prepare( $check_weekday_query, $product_id, $day, $from_db, $to_db ) );                                
                            }
                            
    
                            // if yes, then update the lockout
                            if ( isset( $check_weekday ) && count( $check_weekday ) > 0 ) { // there will be only 1 active record at any given time
                                $insert            =   false;
                                if ( is_numeric( $time_data[ 'lockout_slot' ] ) && $time_data[ 'lockout_slot' ] > 0 ) {
                                    $change_in_lockout =   $time_data[ 'lockout_slot' ] - $check_weekday[ 0 ]->total_booking;
                                } else if ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) { // unlimited bookings
                                    $change_in_lockout = 0;
                                }
                                
                            } else {
                                // if not found, check if there's a date record present
                                $existing_lockout = "SELECT total_booking FROM `" . $wpdb->prefix . "booking_history`
                                                                                WHERE post_id = %d
                                                                                AND start_date != '0000-00-00'
                                                                                AND weekday = %s
                                                                                AND TIME_FORMAT( from_time, '%H:%i' ) = %s
                                                                                AND TIME_FORMAT( to_time, '%H:%i' ) = %s
                                                                                ORDER BY id DESC LIMIT 1";
                                $lockout_results = $wpdb->get_results( $wpdb->prepare( $existing_lockout, $product_id, $day, $from_db, $to_db ) );
    
                                if ( isset( $lockout_results ) && count( $lockout_results ) > 0 ) {

                                    if ( is_numeric( $time_data[ 'lockout_slot' ] ) && $time_data[ 'lockout_slot' ] > 0 ) {
                                        $change_in_lockout =   $time_data[ 'lockout_slot' ] - $lockout_results[ 0 ]->total_booking;
                                        $available_booking = $lockout_results[ 0 ]->total_booking + $change_in_lockout;
                                    } else if ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) { // unlimited bookings
                                        $change_in_lockout = 0;
                                        $available_booking = 0;
                                    }
                                }
    
                            }
    
                            if ( $insert ) {
                                
                                $current_date = date( 'Y-m-d', current_time( 'timestamp' ) );
                                
                                $query_insert    =  "INSERT INTO `" . $wpdb->prefix . "booking_history`
                                                (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                                VALUES (
                                                '" . $product_id . "',
                                                '" . $day . "',
                                                '0000-00-00',
                                                '0000-00-00',
                                                '" . $from_time . "',
                                                '" . $to_time . "',
                                                '" . $updated_lockout . "',
                                                '" . $available_booking . "' )";
                                $wpdb->query( $query_insert );
                                
                                // if there are other time slots present for the weekday, add this slot for the date
                                $fetch_dates = "SELECT DISTINCT( start_date ) FROM `" . $wpdb->prefix . "booking_history`
                                                WHERE start_date >= %s
                                                AND post_id = %d
                                                AND weekday = %s";
                                
                                $dates_set = $wpdb->get_col( $wpdb->prepare( $fetch_dates, $current_date, $product_id, $day ) );
                                
                                if( is_array( $dates_set ) && count( $dates_set ) > 0 ) {
                                    
                                    // build an array of dates that already have this slot present
                                    $fetch_dates_present = "SELECT DISTINCT( start_date ) FROM `" . $wpdb->prefix . "booking_history`
                                                WHERE start_date >= %s
                                                AND post_id = %d
                                                AND weekday = %s
                                                AND TIME_FORMAT( from_time, '%H:%i' ) = %s
                                                AND TIME_FORMAT( to_time, '%H:%i' ) = %s";
                                    
                                    $dates_present = $wpdb->get_col( $wpdb->prepare( $fetch_dates_present, $current_date, $product_id, $day, $from_db, $to_db ) );
                                    
                                    foreach( $dates_set as $date ) {
                                        // In a scenario where a future date is locked out, as all the time slot bookings are full, 
                                        // we need to run this insert to ensure the date is unblocked and bookings can be taken for the new slot
                                        if( ! in_array( $date, $dates_present ) ) {

                                            if( $to_time == '' ){
                                                $to_gi = '';
                                            }

                                            $query_insert    =  "INSERT INTO `" . $wpdb->prefix . "booking_history`
                                                    (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                                    VALUES (
                                                    '" . $product_id . "',
                                                    '" . $day . "',
                                                    '" . $date . "',
                                                    '0000-00-00',
                                                    '" . $from_gi . "',
                                                    '" . $to_gi . "',
                                                    '" . $updated_lockout . "',
                                                    '" . $available_booking . "' )";
                                    
                                            $wpdb->query( $query_insert );
                                        }
                                    }
                                }
                            } else if ( isset( $change_in_lockout ) && is_numeric( $change_in_lockout ) ) {
                                
                                // Update the existing record so that lockout is managed and orders do not go missing frm the View bookings page
                                if ( $change_in_lockout == 0 && ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) ) { // unlimited bookings
                                    
                                    $query_update    =  "UPDATE `" . $wpdb->prefix . "booking_history`
                                                SET total_booking = '" . $updated_lockout . "',
                                                available_booking = '" . $change_in_lockout . "'
                                                WHERE post_id = '" . $product_id . "'
                                                AND weekday = '" . $day . "'
                                                AND start_date = '0000-00-00'
                                                AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                                AND TIME_FORMAT( to_time, '%H:%i' ) = '" . $to_db . "'
                                                AND status = ''";
                                    
                                } else {
                                       if($to_time == '' ){
                                           
                                            $query_update    =  "UPDATE `" . $wpdb->prefix . "booking_history`
                                            SET total_booking = '" . $updated_lockout . "',
                                            available_booking = available_booking + '" . $change_in_lockout . "'
                                            WHERE post_id = '" . $product_id . "'
                                            AND weekday = '" . $day . "'
                                            AND start_date = '0000-00-00'
                                            AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                            AND to_time = ''
                                            AND status = ''";
                                           
                                           
                                       }else {
                                            $query_update    =  "UPDATE `" . $wpdb->prefix . "booking_history`
                                            SET total_booking = '" . $updated_lockout . "',
                                            available_booking = available_booking + '" . $change_in_lockout . "'
                                            WHERE post_id = '" . $product_id . "'
                                            AND weekday = '" . $day . "'
                                            AND start_date = '0000-00-00'
                                            AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                            AND TIME_FORMAT( to_time, '%H:%i' ) = '" . $to_db . "'
                                            AND status = ''";
                                     
                                       }
                                 }
                                $wpdb->query( $query_update );
                                
                            }
    
                            if ( isset( $change_in_lockout ) && is_numeric( $change_in_lockout ) ) {
                                
                                //Update the existing records for the dates
                                if ( $change_in_lockout == 0 && ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) ) { // unlimited bookings
                                
                                    $query_update        =   "UPDATE `" . $wpdb->prefix . "booking_history`
                                                    SET total_booking = '" . $updated_lockout . "',
                                                    available_booking = '" . $change_in_lockout . "',
                                                    status = ''
                                                    WHERE post_id = '" . $product_id . "'
                                                    AND weekday = '" . $day . "'
                                                    AND start_date <> '0000-00-00'
                                                    AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                                    AND TIME_FORMAT( to_time, '%H:%i' ) = '" . $to_db . "'";
                                } else {
                                    
                                    if($to_time == ''){
                                        $query_update        =   "UPDATE `" . $wpdb->prefix . "booking_history`
                                        SET total_booking = '" . $updated_lockout . "',
                                        available_booking = available_booking + '" . $change_in_lockout . "',
                                        status = ''
                                        WHERE post_id = '" . $product_id . "'
                                        AND weekday = '" . $day . "'
                                        AND start_date <> '0000-00-00'
                                        AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                        AND to_time = ''";
                                        
                                    } else {
                                        $query_update        =   "UPDATE `" . $wpdb->prefix . "booking_history`
                                        SET total_booking = '" . $updated_lockout . "',
                                        available_booking = available_booking + '" . $change_in_lockout . "',
                                        status = ''
                                        WHERE post_id = '" . $product_id . "'
                                        AND weekday = '" . $day . "'
                                        AND start_date <> '0000-00-00'
                                        AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                        AND TIME_FORMAT( to_time, '%H:%i' ) = '" . $to_db . "'";
                                        
                                    }
                                    
                                }
                                $wpdb->query( $query_update );
                            }
    
                        }
                    } else { // specific dates

                        $date = date( 'Y-m-d', strtotime( $day ) );
                        foreach ( $s_data as $time_data ) {
    
                            $insert = true;
                            $available_booking = $time_data[ 'lockout_slot' ];
                            $updated_lockout = $time_data[ 'lockout_slot' ];
    
                            $from_time = $time_data[ 'from_slot_hrs' ] . ':' . $time_data[ 'from_slot_min' ] ;
                            $to_time = $time_data[ 'to_slot_hrs' ] . ':' . $time_data[ 'to_slot_min' ];
    
                            if ( $to_time == '0:00' ) {
                                $to_time = '';
                            }
    
                            // check if the date is already present
                            $check_date_query = "SELECT total_booking, available_booking FROM `" . $wpdb->prefix . "booking_history`
                                                WHERE post_id = %d
                                                AND weekday = ''
                                                AND start_date = %s
                                                AND from_time = %s
                                                AND to_time = %s
                                                AND status = ''";
    
                            $check_date = $wpdb->get_results( $wpdb->prepare( $check_date_query, $product_id, $date, $from_time, $to_time ) );
    
                            // if yes, then update the lockout
                            if ( isset( $check_date ) && count( $check_date ) > 0 ) { // there will be only 1 active record at any given time
                                $insert            =   false;
                                if ( is_numeric( $time_data[ 'lockout_slot' ] ) && $time_data[ 'lockout_slot' ] > 0 ) {
                                    $change_in_lockout =   $time_data[ 'lockout_slot' ] - $check_date[ 0 ]->total_booking;
                                } else if ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) { // unlimited bookings
                                    $change_in_lockout = 0;
                                }
                                
                            } else {
                                // if not found, check if there's an inactive date record present
                                $existing_lockout = "SELECT total_booking FROM `" . $wpdb->prefix . "booking_history`
                                                                                WHERE post_id = %d
                                                                                AND start_date = %s
                                                                                AND weekday = ''
                                                                                AND from_time = %s
                                                                                AND to_time = %s
                                                                                AND status <> ''";
                                $lockout_results = $wpdb->get_results( $wpdb->prepare( $existing_lockout, $product_id, $date, $from_time, $to_time ) );
    
                                if ( isset( $lockout_results ) && count( $lockout_results ) > 0 ) {
                                    $insert = false;
                                    if ( is_numeric( $time_data[ 'lockout_slot' ] ) && $time_data[ 'lockout_slot' ] > 0 ) {
                                        $change_in_lockout = $time_data[ 'lockout_slot' ] - $lockout_results[ 0 ]->total_booking;
                                    } else if ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) { // unlimited bookings
                                        $change_in_lockout = 0;
                                    }
                                }
    
                            }
    
                            if ( $insert ) {
                                $query_insert  =   "INSERT INTO `" . $wpdb->prefix . "booking_history`
                                            (post_id,weekday,start_date,end_date,from_time,to_time,total_booking,available_booking)
                                            VALUES (
                                            '" . $product_id . "',
                                            '',
                                            '" . $date . "',
                                            '0000-00-00',
                                            '" . $from_time . "',
                                            '" . $to_time . "',
                                            '" . $time_data[ 'lockout_slot' ] . "',
                                            '" . $available_booking . "' )";
                                $wpdb->query( $query_insert );
                            } else {
                                
                                // Update the existing record so that lockout is managed and orders do not go missing frm the View bookings page
                                if ( $change_in_lockout == 0 && ( $time_data[ 'lockout_slot' ] === '' || $time_data[ 'lockout_slot' ] == 0 ) ) { // unlimited bookings
                                    $query_update  =   "UPDATE `".$wpdb->prefix."booking_history`
                                                SET total_booking = '" . $time_data[ 'lockout_slot' ] . "',
                                                available_booking = '" . $change_in_lockout . "',
                                                status = ''
                                                WHERE post_id = '" . $product_id . "'
                                                AND start_date = '" . $date . "'
                                                AND from_time = '" . $from_time . "'
                                                AND to_time = '" . $to_time . "'";
                                } else {
                                    $query_update  =   "UPDATE `".$wpdb->prefix."booking_history`
                                            SET total_booking = '" . $time_data[ 'lockout_slot' ] . "',
                                            available_booking = available_booking + '" . $change_in_lockout . "',
                                            status = ''
                                            WHERE post_id = '" . $product_id . "'
                                            AND start_date = '" . $date . "'
                                            AND from_time = '" . $from_time . "'
                                            AND to_time = '" . $to_time . "'";
                                }
                                
                                $wpdb->query( $query_update );
                            }
                        }
    
                    }
                }
            }
        }
    }

    /**
     * Called when a record from the Set Availability by Dates/Months
     * Table in Booking meta box->Availability tab needs to be deleted.
     * 
     * Called via AJAX
     * 
     * @since 4.0.0
     */

    function bkap_delete_specific_range() {
    
        $product_id = $_POST[ 'product_id' ];
        $record_type = $_POST[ 'record_type' ];
        $start = $_POST[ 'start' ];
        $end = $_POST[ 'end' ];
    
        $booking_box_class = new bkap_booking_box_class();
        $booking_box_class->delete_ranges( $product_id, $record_type, $start, $end );
    
        die();
    }
    
    /**
     * Deletes a record from the Set Availability by Dates/Months
     * Table in Booking meta box->Availability tab.
     * 
     * @param int $product_id - Product ID
     * @param string $record_type - Such as 'range_of_months', 'custom_range' and so on.
     * @param string $start - Start Date
     * @param string $end - End Date
     * 
     * @since 4.0.0
     */

    function delete_ranges( $product_id, $record_type, $start, $end ) {
    
        if ( '' != $record_type ) {
            switch( $record_type ) {
    
                case 'custom_range':
                    $custom_ranges = get_post_meta( $product_id, '_bkap_custom_ranges', true );
    
                    // get the key for the range
                    $delete_key = $this->get_range_key( $custom_ranges, $start, $end );
                    if ( is_numeric( $delete_key ) ) {
                        $this->delete_serialized_range( $product_id, 'booking_date_range', $delete_key );
                        $this->delete_single_range( $product_id, '_bkap_custom_ranges', $delete_key );
                    }
                    break;
                case 'range_of_months':
                    global $bkap_months;
    
                    $current_year = date( 'Y', current_time( 'timestamp' ) );
                    $next_year = date( 'Y', strtotime( '+1 year' ) );
                    
                    $month_range = get_post_meta( $product_id, '_bkap_month_ranges', true );
    
                    if ( is_numeric( $start ) ) { // it's a month number
                        $month_name = $bkap_months[ $start ];
                        $month_to_use = "$month_name $current_year";
                        $range_start = date ( 'j-n-Y', strtotime( $month_to_use ) );
                    }
    
                    if ( is_numeric( $end ) ) { // it's a month number
                        $month_name = $bkap_months[ $end ];
                        if ( $start < $end ) {
                            $month_to_use = "$month_name $current_year";
                        } else {
                            $month_to_use = "$month_name $next_year";
                        }
                        $month_start = date ( 'j-n-Y', strtotime( $month_to_use ) );
    
                        $days = date( 't', strtotime( $month_start ) );
                        $days -= 1;
                        $range_end = date ( 'j-n-Y', strtotime( "+$days days", strtotime( $month_start ) ) );
                    }
    
                    // get the key for the range
                    $delete_key = $this->get_range_key( $month_range, $range_start, $range_end );
    
                    if ( is_numeric ) {
                        $this->delete_single_range( $product_id, '_bkap_month_ranges', $delete_key );
                    }
                    break;
                case 'specific_dates':
                    // remove the record from serial data
                    $this->delete_serialized_range( $product_id, 'booking_specific_date', $start );
                    // remove from individual data
                    $this->delete_single_range( $product_id, '_bkap_specific_dates', $start );
                    // update booking history
                    $this->delete_specific_date( $product_id, $start );
                    // update the special prices data
                    $this->delete_special_price( $product_id, $start );
                    break;
                case 'holidays':
                    // remove the record from serial data
                    $this->delete_serialized_range( $product_id, 'booking_product_holiday', $start );
                    // remove from individual data
                    $this->delete_single_range( $product_id, '_bkap_product_holidays', $start );
                    break;
                case 'holiday_range':
                    $holiday_range = get_post_meta( $product_id, '_bkap_holiday_ranges', true );
    
                    // get the key for the range
                    $delete_key = $this->get_range_key( $holiday_range, $start, $end );
    
                    if ( is_numeric ) {
                        $this->delete_single_range( $product_id, '_bkap_holiday_ranges', $delete_key );
                    }
                default:
                    break;
            }
        }
    
    }
    
    /**
     * Returns the array key from a given range if a match
     * is found.
     * 
     * @param array $range - array to search
     * @param string $start - start date (j-n-Y)
     * @param string $end - end date (j-n-Y)
     * @return int $key - array key
     * 
     * @since 4.0.0
     */

    function get_range_key( $range, $start, $end ) {
    
        $delete_key = '';
        if ( is_array( $range ) && count( $range ) > 0 ) {
            foreach( $range as $range_key => $range_value ) {
                $r_start = $range_value[ 'start' ];
                $r_end = $range_value[ 'end' ];
    
                if ( $r_start == $start && $r_end == $end ) {
                    $delete_key = $range_key;
                    break;
                }
            }
    
        }
    
        return $delete_key;
    
    }
    
    /**
     * Deletes a given array record from the 
     * individual booking settings in postmeta.
     * 
     * @param integer $product_id - Product ID
     * @param string $range_name - Meta Key from which the data needs to be removed.
     * @param integer $key - Array key to be removed.
     * 
     * @since 4.0.0
     */

    function delete_single_range( $product_id, $range_name, $key ) {
    
        $range_data = get_post_meta( $product_id, $range_name, true );
    
        if ( array_key_exists( $key, $range_data ) ) {
            unset( $range_data[ $key ] );
        }
    
        update_post_meta( $product_id, $range_name, $range_data );
    }
    
    /**
     * Deletes a record from a given range in 
     * the serialized booking settings i.e. woocommerce_booking_settings
     * in postmeta table. 
     *
     * @param int $product_id - Product ID
     * @param string $name - contains the array key name
     * @param int $key - Array key to be unset
     * 
     * @since 4.0.0
     */

    function delete_serialized_range( $product_id, $name, $key ) {
    
        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
    
        $record_data = $booking_settings[ $name ];
    
        if ( array_key_exists( $key, $record_data ) ) {
            unset( $record_data[ $key ] );
        }
    
        $booking_settings[ $name ] = $record_data;
    
        update_post_meta( $product_id, 'woocommerce_booking_settings', $booking_settings );
    }
    
    /**
     * Updates a specific date record to inactive
     * status in booking history table for a given
     * date. 
     * 
     * @param int $product_id - Product ID
     * @param string $date - j-n-Y Format
     * @global object $wpdb Global wpdb Object
     * 
     * @since 4.0.0
     */

    function delete_specific_date( $product_id, $date ) {
    
        global $wpdb;
    
        $specific_date = date ( 'Y-m-d', strtotime( $date ) );
    
        $update_specific = "UPDATE `" . $wpdb->prefix . "booking_history`
                            SET status = 'inactive'
                            WHERE post_id = '" . $product_id . "'
                            AND start_date = '" . $specific_date . "'
                            AND weekday = ''
                            AND from_time = ''
                            AND to_time = ''";
    
        $wpdb->query( $update_specific );
    }
    
    /**
     * Deletes the special price record
     * from post meta for a given specific
     * date.
     * 
     * @param int $product_id - Product ID
     * @param string $date - Date for which the data needs to be removed.
     * @since 4.0.0
     */

    function delete_special_price( $product_id, $date ) {

        $date = date( 'Y-m-d', strtotime( $date ) );
        
        $special_prices = get_post_meta( $product_id, '_bkap_special_price', true );
    
        if ( is_array( $special_prices ) && count( $special_prices ) > 0 ) {
    
            $updated_special_prices = array();
            foreach( $special_prices as $s_key => $s_price ) {
    
                if ( $s_price[ 'booking_special_date' ] != $date ) {
                    $updated_special_prices[ $s_key ] = $s_price;
                }
            }
    
            update_post_meta( $product_id, '_bkap_special_price', $updated_special_prices );
        }
    }
    
    /**
     * Deletes the Date/Day and Time Slot from the
     * Date & Time table in the Availability settings
     * 
     * Called via ajax
     * 
     * @since 4.0.0
     */

    static function bkap_delete_date_time() {
    
        $product_id     = $_POST[ 'product_id' ];
        $day            = $_POST[ 'day' ]; // this will be an array
        $from_time      = $_POST[ 'from_time' ];
        $to_time        = $_POST[ 'to_time' ];
    
        $booking_box_class = new bkap_booking_box_class();
        if ( is_array( $day ) && count( $day ) > 0 ) {
            foreach( $day as $day_value ) {
                // update post meta serialized
                $booking_box_class->delete_serialized_time_settings( $product_id, $day_value, $from_time, $to_time );
                // update post meta individual
                $booking_box_class->delete_individual_time_settings( $product_id, $day_value, $from_time, $to_time );
                // update booking history
                $booking_box_class->delete_booking_history( $product_id, $day_value, $from_time, $to_time );
            }
        }
        die();
    }
    
    /**
     * Deletes the time slot from the serialized
     * post meta record i.e. woocommerce_booking_settings
     * in postmeta table.
     * 
     * @param int $product_id - Product ID
     * @param string $day_value - Weekday/Date
     * @param string $from_time - H:i
     * @param string $to_time - H:i
     * 
     * @since 4.0.0
     */
    
    function delete_serialized_time_settings( $product_id, $day_value, $from_time, $to_time ) {
    
        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
    
        $existing_settings = $booking_settings[ 'booking_time_settings' ];
    
        $updated_time_settings = $this->unset_time_array( $existing_settings, $day_value, $from_time, $to_time );
    
        $booking_settings[ 'booking_time_settings' ] = $updated_time_settings;
    
        update_post_meta( $product_id, 'woocommerce_booking_settings', $booking_settings );
    }
    
    /**
     * Deletes the time slot from the _bkap_time_settings
     * post meta record in postmeta table.
     * 
     * @param int $product_id - Product ID
     * @param string $day_value - Weekday/Date
     * @param string $from_time - H:i
     * @param string $to_time - H:i
     * 
     * @since 4.0.0
     */

    function delete_individual_time_settings( $product_id, $day_value, $from_time, $to_time ) {
    
        $existing_settings = get_post_meta( $product_id, '_bkap_time_settings', true );
    
        $updated_time_settings = $this->unset_time_array( $existing_settings, $day_value, $from_time, $to_time );
    
        update_post_meta( $product_id, '_bkap_time_settings', $updated_time_settings );
    
    }
    
    /**
     * Unsets the time slots record which needs to be removed from the list
     * of time slots present for the product.
     * 
     * @param array $existing_settings - Existing Time Slots for the Product
     * @param string $day_value - Weekday/Date
     * @param string $from_time - H:i
     * @param string $to_time - H:i
     * @return array $existing_settings - updated array with the desired slot removed.
     * 
     * @since 4.0.0
     */

     function unset_time_array( $existing_settings, $day_value, $from_time, $to_time ) {
    
        //split the time into hrs and mins
        $from_time_array = explode( ':', $from_time );
        $from_hrs = $from_time_array[ 0 ];
        $from_mins = $from_time_array[ 1 ];
    
        $to_hrs = '0';
        $to_mins = '00';
        if ( isset( $to_time ) && '' != $to_time ) {
            $to_time_array = explode( ':', $to_time );
            $to_hrs = $to_time_array[ 0 ];
            $to_mins = $to_time_array[ 1 ];
        }
    
        if ( is_array( $existing_settings ) && count( $existing_settings ) > 0 ) {
    
            foreach( $existing_settings as $day => $day_settings ) {
    
                if ( $day == $day_value ) { // matching day/date
    
                    foreach( $day_settings as $time_key => $time_settings ) {
    
                        // Match the time
                        if ( trim( $from_hrs ) == $time_settings[ 'from_slot_hrs' ] && trim( $from_mins ) == $time_settings[ 'from_slot_min' ] && trim( $to_hrs ) == $time_settings[ 'to_slot_hrs' ] && trim( $to_mins ) == $time_settings[ 'to_slot_min' ] ) {
                            $unset_key = $time_key;
                            break;
                        }
    
                    }
                    // unset the array
                    if ( isset( $unset_key ) && is_numeric( $unset_key ) ) {
                        unset( $existing_settings[ $day ][ $unset_key ] );
                        break;
                    }
                }
            }
        }
    
        return $existing_settings;
    }
    
    /**
     * Updates the Booking History table. Removes/Inactivates the 
     * desired records for the deleted time slot.
     * 
     * @param int $product_id - Product ID
     * @param string $day_value - Weekday/Date
     * @param string $from_time - H:i
     * @param string $to_time - H:i
     * @global object $wpdb Global wpdb Object
     * 
     * @since 4.0.0
     */

    function delete_booking_history( $product_id, $day_value, $from_time = '', $to_time = '' ) {
    
        global $wpdb;
        
        $to_hrs = '';
        $to_mins = '';
        
        if ( isset( $to_time ) && '' != $to_time ) {
            $to_time_array = explode( ':', $to_time );
            $to_hrs = $to_time_array[ 0 ];
            $to_mins = $to_time_array[ 1 ];
        }
        
        if ( $to_hrs == 0 && $to_mins == 0 ){
            $to_time = '';
        }
    
        if ( isset( $day_value ) && substr( $day_value, 0, 7 ) == 'booking' ) { // recurring weekday
    
            // delete the base record
            $delete_base = "DELETE FROM `" . $wpdb->prefix . "booking_history`
                            WHERE post_id = '" . $product_id . "'
                            AND weekday = '" . $day_value . "'
                            AND start_date = '0000-00-00'
                            AND from_time = '" . $from_time . "'
                            AND to_time = '" . $to_time . "'";
            $wpdb->query( $delete_base );
    
            // set all date records to inactive
            $from_db = date( 'H:i', strtotime( $from_time ) );
            $to_db = date( 'H:i', strtotime( $to_time ) );
            
            if ( $to_time == '' ) {
                $update_date_status = "UPDATE `" . $wpdb->prefix . "booking_history`
                                    SET status = 'inactive'
                                    WHERE post_id = '" . $product_id . "'
                                    AND weekday = '" . $day_value . "'
                                    AND start_date <> '0000-00-00'
                                    AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                    AND to_time = '" . $to_time . "'";

            } else{
                $update_date_status = "UPDATE `" . $wpdb->prefix . "booking_history`
                                    SET status = 'inactive'
                                    WHERE post_id = '" . $product_id . "'
                                    AND weekday = '" . $day_value . "'
                                    AND start_date <> '0000-00-00'
                                    AND TIME_FORMAT( from_time, '%H:%i' ) = '" . $from_db . "'
                                    AND TIME_FORMAT( to_time, '%H:%i' ) = '" . $to_db . "'";
            }
            $wpdb->query( $update_date_status );
    
        } else if ( isset( $day_value ) && '' != $day_value ) { // specific date
    
            $date = date( 'Y-m-d', strtotime( $day_value ) );
            // set the date record to inactive
            $update_date_query = "UPDATE `" . $wpdb->prefix . "booking_history`
                                SET status = 'inactive'
                                WHERE post_id = '" . $product_id . "'
                                AND start_date = '" . $date . "'
                                AND from_time = '" . $from_time . "'
                                AND to_time = '" . $to_time . "'";
    
            $wpdb->query( $update_date_query );
        }
    }
}// end of class    
?>
API documentation generated by ApiGen