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 
<?php
/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class containing common functions used all over the plugin.
 *
 * @author   Tyche Softwares
 * @package  BKAP/Global-Function
 * @category Classes
 */

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

/**
 * Contains common functions used all over the plugin.
 * 
 * @class bkap_common
 */

class bkap_common{
    
    /**
     * Return min date based on the Advance Booking Period.
     * 
     * @param integer $product_id - Product ID for which calculations need to be done.
     * @param string $current_time - UNIX TimeStamp
     * @return $min_date date - 'j-n-Y' format
     * @since 4.1.0
     */

    public static function bkap_min_date_based_on_AdvanceBookingPeriod( $product_id, $current_time ) {
    
        $bkap_abp = get_post_meta( $product_id, '_bkap_abp', true );        
        $bkap_abp = ( isset( $bkap_abp ) && $bkap_abp != "" ) ? $bkap_abp : 0;
        
        // Convert the advance period to seconds and add it to the current time
        $advance_seconds      =   $bkap_abp *60 *60;
        $cut_off_timestamp    =   $current_time + $advance_seconds;
        $cut_off_date         =   date( "d-m-Y", $cut_off_timestamp );
        $min_date             =   date( "j-n-Y", strtotime( $cut_off_date ) );
    
        return $min_date;
    }
    
    /**
     * Return true/false based on the timeslot available for selected date.
     * 
     * @param integer $product_id - Product ID
     * @param string $start_date - Date for which availability is being checked
     * @return boolean
     * @since 4.3.0
     */

    public static function bkap_check_timeslot_for_weekday( $product_id, $start_date ) {
    
        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
    
        $start_weekday           =  date( 'w', strtotime( $start_date ) );
        $start_booking_weekday   =  'booking_weekday_' . $start_weekday;

        if( !isset( $booking_settings['booking_time_settings'] ) ){
            return false;
        }elseif ( is_array( $booking_settings['booking_time_settings'] ) && array_key_exists( $start_booking_weekday, $booking_settings['booking_time_settings'] ) ) {
            return true;
        }else if ( is_array( $booking_settings['booking_time_settings'] ) && 
            array_key_exists( date( 'j-n-Y', strtotime( $start_date ) ), $booking_settings['booking_time_settings'] ) ) {
            return true;
        }
    
        return false;    
    }
    
    /**
     * Return function name to be executed when multiple time slots are enabled.
     * 
     * This function returns the function name to display the timeslots on the 
     * frontend if type of timeslot is Multiple for multiple time slots addon.
     * 
     * @param integer $product_id
     * @return string 
     * @since 2.0
     */

    public static function bkap_ajax_on_select_date( $product_id ) {
        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
        
        if( isset( $booking_settings['booking_enable_multiple_time'] ) && $booking_settings['booking_enable_multiple_time'] == "multiple" && function_exists('is_bkap_multi_time_active') && is_bkap_multi_time_active() ) {
            return 'multiple_time';
        }
    }

    /**
     * Return an array of dates that fall in a date range
     * 
     * This function returns an array of dates that falls
     * in a date range in the d-n-Y format.
     * 
     * @param string $StartDate d-n-Y format
     * @param string $EndDate d-n-Y format
     * @return array $Days - array of dates within the range
     * @since 2.0
     */

    public static function bkap_get_betweendays( $StartDate, $EndDate, $format = 'd-n-Y'  ) {
        $Days[]                   =   $StartDate;
        $CurrentDate              =   $StartDate;
            
        $CurrentDate_timestamp    =   strtotime($CurrentDate);
        $EndDate_timestamp        =   strtotime($EndDate);
        
        if( $CurrentDate_timestamp != $EndDate_timestamp )
        {
            while( $CurrentDate_timestamp < $EndDate_timestamp )
            {
                $CurrentDate            =   date( $format, strtotime( "+1 day", strtotime( $CurrentDate ) ) );
                $CurrentDate_timestamp  =   $CurrentDate_timestamp + 86400;
                $Days[]                 =   $CurrentDate;
            }
            array_pop( $Days );
        }
        return $Days;
    }
    
    /**
     * Return an array of dates that fall in a date range
     *
     * This function returns an array of dates that falls in a date range 
     * in the d-n-Y format including the end date if the flat charge per day is enable.
     *
     * @param string $StartDate d-n-Y format
     * @param string $EndDate d-n-Y format
     * @return array $Days - array of dates within the range
     * 
     * @since 4.7.0
     */

    public static function bkap_get_betweendays_when_flat( $StartDate, $EndDate, $pro_id, $format = 'd-n-Y' ) {
        $Days[]                   =   $StartDate;
        $CurrentDate              =   $StartDate;
        $booking_settings         =   get_post_meta( $pro_id, 'woocommerce_booking_settings', true );
         
         
        $CurrentDate_timestamp    =   strtotime($CurrentDate);
        $EndDate_timestamp        =   strtotime($EndDate);
    
        if( $CurrentDate_timestamp != $EndDate_timestamp )
        {
             
            while( $CurrentDate_timestamp < $EndDate_timestamp )
            {
                $CurrentDate            =   date( $format, strtotime( "+1 day", strtotime( $CurrentDate ) ) );
                $CurrentDate_timestamp  =   $CurrentDate_timestamp + 86400;
                $Days[]                 =   $CurrentDate;
            }
        }
        return $Days;
    }
    
    /**
     * Send the Base language product ID
     * 
     * This function has been written as a part of making the Booking plugin
     * compatible with WPML. It returns the base language Product ID when WPML
     * is enabled. 
     * 
     * @param integer $product_id
     * @return integer $base_product_id
     * @since 2.0 
     */

    public static function bkap_get_product_id( $product_id ) {
        $base_product_id = $product_id;
        // If WPML is enabled, the make sure that the base language product ID is used to calculate the availability
        if ( function_exists( 'icl_object_id' ) ) {
            global $sitepress;
            global $polylang;
             
            if( isset( $polylang ) ){
                $default_lang = pll_current_language();
            }else{
                $default_lang = $sitepress->get_default_language();
            }
            
            $base_product_id = icl_object_id( $product_id, 'product', false, $default_lang );
            // The base product ID is blanks when the product is being created.
            if (! isset( $base_product_id ) || ( isset( $base_product_id ) && $base_product_id == '' ) ) {
                $base_product_id = $product_id;
            }
        } 
        return $base_product_id;
    }

    /**
     * Send the Base language Variation ID
     * 
     * This function has been written as a part of making the Booking plugin
     * compatible with WPML. It returns the base language Variation ID when WPML
     * is enabled. 
     * 
     * @param int $variation_id - Variation ID
     * @return int Variation ID
     * @since 4.5.1
     */

    public static function bkap_get_variation_id( $variation_id ) {
        $base_variation_id = $variation_id;
        // If WPML is enabled, the make sure that the base language product ID is used to calculate the availability
        if ( function_exists( 'icl_object_id' ) ) {
            global $sitepress;
            global $polylang;
             
            if( isset( $polylang ) ){
                $default_lang = pll_current_language();
            }else{
                $default_lang = $sitepress->get_default_language();
            }
            
            $base_variation_id = icl_object_id( $variation_id, 'product-variation', false, $default_lang );
            // The base variation_id is blanks when the variation is being created.
            if (! isset( $base_variation_id ) || ( isset( $base_variation_id ) && $base_variation_id == '' ) ) {
                $base_variation_id = $variation_id;
            }
        } 
        return $base_variation_id;
    }
    
    /**
     * Returns the selected setting of Multicurrency at product level from WPML plugin when it is active
     *
     * This function has been written as a part of making the Booking plugin
     * compatible with WPML. It returns the selected setting of Multicurrency at product level from WPML plugin when it is active
     *
     * @param integer $product_id - Product ID 
     * @param integer $variation_id - Variation ID
     * @param string $product_type - Product Type
     * @return integer $custom_post
     * @since 4.3.0
     */

    public static function bkap_get_custom_post( $product_id, $variation_id, $product_type ) {
        if( $product_type == 'variable' ) {
            $custom_post = get_post_meta( $variation_id, '_wcml_custom_prices_status', true );
        } else if( $product_type == 'simple' || $product_type == 'grouped' ) {
            $custom_post = get_post_meta( $product_id, '_wcml_custom_prices_status', true );
        }
        if ( $custom_post == '' ) { // possible when the setting has been left to it's default value
            $custom_post = 0;
        }
        return $custom_post;
    }
    
    /**
     * Return Woocommerce price
     * 
     * This function returns the Woocommerce price applicable for a product.
     * Different Product Types such as simple, variable, bundles etc. have been taken into account here. 
     * 
     * @param integer $product_id 
     * @param integer $variation_id 
     * @param string $product_type - simple, bundled, composite, variation etc.
     * @param string $check_in - booking start date
     * @param string $check_out - booking end date
     * @return integer $price
     * 
     * @since 4.3.0
     */

    public static function bkap_get_price( $product_id, $variation_id, $product_type, $check_in ='', $check_out = '' ) {
      
        global $wpdb;
        
        $price = 0;
        $wpml_multicurreny_enabled = 'no';
        if ( function_exists( 'icl_object_id' ) ) {
            global $woocommerce_wpml, $woocommerce;
            if ( isset( $woocommerce_wpml->settings[ 'enable_multi_currency' ] ) && $woocommerce_wpml->settings[ 'enable_multi_currency' ] == '2' ) {
                if ( $product_type == 'variable' ){
                    $custom_post = bkap_common::bkap_get_custom_post( $product_id, $variation_id, $product_type );
                    if( $custom_post == 1 ) {
                        $client_currency = $woocommerce->session->get( 'client_currency' );
                        if( $client_currency != '' && $client_currency != get_option( 'woocommerce_currency' ) ) {
                            $price = get_post_meta( $variation_id, '_price_' . $client_currency, true );
                            $wpml_multicurreny_enabled = 'yes';
                        } 
                    }
                } else if( $product_type == 'simple' || 'bundle' == $product_type ) {
                    $custom_post = bkap_common::bkap_get_custom_post( $product_id, $variation_id, $product_type );
                    if( $custom_post == 1 ) {
                        $client_currency = $woocommerce->session->get( 'client_currency' );
                        if( $client_currency != '' && $client_currency != get_option( 'woocommerce_currency' ) ) {
                            $price = get_post_meta( $product_id, '_price_' . $client_currency, true );
                            $wpml_multicurreny_enabled = 'yes';
                        }
                    }
                }
            }
        } 
        
        if( $wpml_multicurreny_enabled == 'no' ) {
            
            if ( $product_type == 'variable' ){
                
                $sale_price = get_post_meta( $variation_id, '_sale_price', true );
                
                $sale_price_dates_from = '';
                $sale_price_dates_to   = '';
                               
                if( !isset( $sale_price ) || $sale_price == '' || $sale_price == 0 ) {
                    $regular_price  =   get_post_meta( $variation_id, '_regular_price', true );
                    $price          =   $regular_price;
                } else {
                   
                    $sale_price_dates_from = get_post_meta( $variation_id, '_sale_price_dates_from', true );
                    $sale_price_dates_from_strtotime = ( isset( $sale_price_dates_from ) && "" != $sale_price_dates_from ) ? $sale_price_dates_from : '';
                    $sale_price_dates_to = get_post_meta( $variation_id, '_sale_price_dates_to', true ) ;
                    $sale_price_dates_to_strtotime = ( isset( $sale_price_dates_to ) && "" != $sale_price_dates_to ) ? $sale_price_dates_to : '';
               
               
                   if ( isset ( $sale_price_dates_from_strtotime ) && '' != $sale_price_dates_from_strtotime && isset ( $sale_price_dates_to_strtotime ) && '' != $sale_price_dates_to_strtotime ) {                      

                        if ( 
                            ( strtotime($check_in) >= $sale_price_dates_from_strtotime )
                            && ( strtotime($check_in) <= $sale_price_dates_to_strtotime ) ) 
                        {
                           
                            $price          =   $sale_price;

                        } else {
                            
                            $regular_price  =   get_post_meta( $variation_id, '_regular_price', true );
                            $price          =   $regular_price;
                         } 
                    } else {

                                  $price          =   $sale_price;
                    } 
                } 
            }
            elseif( $product_type == 'simple' || 'bundle' == $product_type || 'composite' == $product_type ) {
                $product_obj = self::bkap_get_product( $product_id );

                $sale_price = get_post_meta( $product_id, '_sale_price', true );

                if ( $sale_price != "" ) {
                    
                    $sale_price_dates_from = '';
                    $sale_price_dates_to   = '';

                    $sale_price_dates_from = get_post_meta( $product_id, '_sale_price_dates_from', true );
                    $sale_price_dates_from_strtotime = ( isset( $sale_price_dates_from ) && "" != $sale_price_dates_from ) ? $sale_price_dates_from : '';
                           
                    $sale_price_dates_to = get_post_meta( $product_id, '_sale_price_dates_to', true ) ;
                    $sale_price_dates_to_strtotime = ( isset( $sale_price_dates_to ) && "" != $sale_price_dates_to ) ? $sale_price_dates_to : '';
                   
                    if ( isset ( $sale_price_dates_from_strtotime ) && '' != $sale_price_dates_from_strtotime && isset ( $sale_price_dates_to_strtotime ) && '' != $sale_price_dates_to_strtotime ) {
                      
                        if ( ( strtotime( $check_in ) >= $sale_price_dates_from_strtotime )
                            && ( strtotime( $check_in ) <= $sale_price_dates_to_strtotime ) ) {
                                                       
                            $price =   $product_obj->get_sale_price();                            

                        } else {
                            
                            $regular_price  =   get_post_meta( $product_id, '_regular_price', true );
                            $price =   $product_obj->get_regular_price();
                            
                        }
                      
                    } else {
                        $regular_price  =   get_post_meta( $product_id, '_sale_price', true );
                        $price =   $product_obj->get_sale_price();    
                    }
                }else{
                    $price = $product_obj->get_price();
                }

            } else {
                if ( isset( $variation_id ) && $variation_id !== '0' ) {
                    $product_obj = self::bkap_get_product( $variation_id );
                }else {
                    $product_obj = self::bkap_get_product( $product_id );
                }

                $price = $product_obj->get_price();
            }

            // check if any of the products are individually priced
            //if yes then we need to add those to the bundle price
            /*if ( $price > 0 && 'bundle' == $product_type ) {
                $bundle_price = bkap_common::get_bundle_price( $price, $product_id, $variation_id );

                $price = $bundle_price;
            }*/
        }
        return $price;
    }
    
    /**
     * Calculates the Total Bundle Price 
     * 
     * The bundle price + the Individual child 
     * price based on the bundle settings 
     * 
     * @param $price - Bundle Price
     * @param int $product_id - Product ID
     * @param int $variation_id - Variation ID
     * @return $price - Final Bundle Price
     * 
     * @since 4.3.0
     */

    static function get_bundle_price( $price, $product_id, $variation_id ) {
         
        global $wpdb;
         
        // get all the IDs for the items in the bundle
        $bundle_items_query = "SELECT bundled_item_id, product_id FROM `" . $wpdb->prefix . "woocommerce_bundled_items`
                                 WHERE bundle_id = %d";
        $get_bundle_items = $wpdb->get_results( $wpdb->prepare( $bundle_items_query, $product_id ) );
    
        if ( isset( $get_bundle_items ) && count( $get_bundle_items ) > 0 ) {

            // fetch the status of optional child products, whether they have been selected on the front end or no
            $explode_optional = array();
            if ( isset( $_POST[ 'bundle_optional' ] ) && '' != $_POST[ 'bundle_optional' ] ) {
                $explode_optional = $_POST[ 'bundle_optional' ];
                //$explode_optional = explode( ',', $bundle_optional );
            }
             
            $child_count = 0;
            foreach( $get_bundle_items as $b_key => $b_value ) {
                $bundled_product_obj = wc_pb_get_bundled_item($b_value->bundled_item_id);

                $quantity_min = $bundled_product_obj->get_quantity();
                $quantity_max = $bundled_product_obj->get_quantity( 'max', true );

                $child_selected = 'off';
                $bundle_item_id = $b_value->bundled_item_id;
                // get the pricing settings for each item
                $price_query = "SELECT meta_key, meta_value FROM `" . $wpdb->prefix  . "woocommerce_bundled_itemmeta`
                                         WHERE bundled_item_id = %d
                                         AND meta_key IN ( 'priced_individually', 'discount', 'optional' )";
                $price_results = $wpdb->get_results( $wpdb->prepare( $price_query, $bundle_item_id ) );
                //
                if( isset( $price_results ) && count( $price_results ) > 0 ) {

                    foreach( $price_results as $key => $value ) {
                        switch( $value->meta_key ) {
                            case 'priced_individually':
                                $price_type = $value->meta_value;
                                break;
                            case 'discount':
                                $price_discount = $value->meta_value;
                                break;
                            case 'optional':
                                $optional = $value->meta_value;
                                break;
                            default:
                                break;
                        }
                    }

                    // if product is optional, see if it has been selected or no
                    // on - selected, off - not selected
                    if ( 'yes' == $optional ) {
                        if ( isset( $explode_optional[ $bundle_item_id ] ) && '' != $explode_optional[ $bundle_item_id ] ) {
                            $child_selected = $explode_optional[ $bundle_item_id ];
                            $child_count++;
                        }
                    }

                    $variation_array = array();
                    // product is individually priced
                    if ( 'yes' == $price_type ) {
                        $bundle_child_id = $b_value->product_id;
                        $product_obj = self::bkap_get_product( $bundle_child_id );
                        $bundle_item_prd_type = $product_obj->get_type();
                        $bundle_item_var = 0;
                        if ( 'variable' == $bundle_item_prd_type ) {
                            $variation_list = $product_obj->get_available_variations();
                            foreach ($variation_list as $var_key => $var_value) {
                                array_push( $variation_array, $var_value['variation_id'] );
                            }
                            $variations_selected = explode( ',', $variation_id );

                            // find the variation selected
                            foreach( $variations_selected as $v_key => $v_val ) {
                                if ( in_array( $v_val, $variation_array ) ) {
                                    $bundle_item_var = $v_val;
                                    break;
                                }
                            }
                        }

                        $child_price = bkap_common::bkap_get_price( $bundle_child_id, $bundle_item_var, $bundle_item_prd_type );

                        if ( '' != $price_discount && $price_discount > 0 ) {
                            // calculate the discounted price
                            $discount = ( $price_discount * $child_price ) / 100;
                            $child_price -= $discount;
                        }

                        if ( $quantity_min === $quantity_max ) {
                            $child_price = $child_price * $quantity_min;
                        }else if ( isset( $_POST['bundle_qty'][ $bundle_item_id ] ) ) {
                            $child_price = $child_price * $_POST['bundle_qty'][ $bundle_item_id ];
                        }

                        if ( isset( $_POST['quantity'] ) && $_POST['quantity'] > 0 ) {
                            $child_price = $child_price * $_POST['quantity'];
                        }

                        if ( isset( $_POST[ 'diff_days' ] ) && $_POST[ 'diff_days' ] > 0 ) {
                            $child_price = $child_price * $_POST[ 'diff_days' ];
                        }

                        // if the product is optional, the child price should be added only if it's selected
                        if ( 'yes' == $optional ) { // if the child product is optional
                            if ( 'on' == $child_selected ) {
                                $price += $child_price;
                            }
                        } else { // else product is not optional, so always add the child price
                            $price += $child_price;
                        }
                    }
                }
            }
        }

        return $price;
    }

    /**
     * Calculates the Total Composite Price 
     * 
     * The composite price + the Individual child 
     * price based on the product settings 
     * 
     * @param int $price
     * @param int $product_id
     * @param int $variation_id
     * @return int $price
     * @since 4.7.0
     */

    public static function get_composite_price( $price, $product_id, $variation_id ) {
        
        $product_obj = wc_get_product( $product_id );

        $component_ids = $product_obj->get_component_ids();
        $composite_data = array();
        $child_price = '';

        $variation_array = explode( ',', $variation_id );

        if ( isset( $_POST['composite_data'] ) ) {
            $composite_data = $_POST['composite_data'];
        }

        if ( !empty( $composite_data ) ) {
            foreach ($composite_data as $c_key => $c_value) {
                if ( isset( $c_value['p_id'] ) && '' !== $c_value['p_id'] ) {

                    $component_data = $product_obj->get_component_data( $c_key );
                    if ( isset( $component_data['priced_individually'] ) && 'yes' === $component_data['priced_individually'] ) {

                        $child_product = wc_get_product( $c_value['p_id'] );
                        $child_type = $child_product->get_type();

                        $selected_variation = '';
                        if ( $child_type == 'variable' && isset( $c_value['v_id'] ) ) {
                            $selected_variation = $c_value['v_id'];
                        }

                        if ( $child_type == 'bundle' ) {
                            $child_price = bkap_common::get_bundle_price( 0, $c_value['p_id'], $variation_id );
                        }else {
                            $child_price = bkap_common::bkap_get_price( $c_value['p_id'], $selected_variation, $child_type );
                        }

                        $child_discount = $product_obj->get_component_discount( $c_key );
                        if ( isset( $child_discount) && '' !== $child_discount ) {
                            $child_price = $child_price - ( ( $child_price * $child_discount ) / 100 );
                        }

                        if ( isset( $_POST['quantity'] ) && $_POST['quantity'] > 0 ) {
                            $child_price = $child_price * $_POST['quantity'];
                        }

                        if ( isset( $_POST[ 'diff_days' ] ) && $_POST[ 'diff_days' ] > 0 ) {
                            $child_price = $child_price * $_POST[ 'diff_days' ];
                        }

                        if ( isset( $c_value['qty'] ) && '' !== $c_value['qty'] ) {
                            $child_price = $child_price * $c_value['qty'];
                        }

                        $price += $child_price;
                    }
                }
            }
        }

        return $price;
    }
    
    /**
     * Return product type
     * 
     * Returns the Product type based on the ID received
     * 
     * @params integer $product_id
     * @return string $product_type
     * @since 4.2.0
     */

    public static function bkap_get_product_type($product_id) {
        $product      =   self::bkap_get_product( $product_id );
        $product_type =   $product->get_type();
        
        return $product_type;
    }
    
    /**
     * Returns the WooCommerce Product Addons Options total
     * 
     * This function returns the WooCommerce Product Addons
     * options total selected by a user for a given product.
     * 
     * @param int $diff_days Number of days between start and end. 1 in case of single days
     * @param array $cart_item_meta Cart Item Meta array
     * @param int $product_quantity Product Quantity
     * @return int Total Price after calculations
     * 
     * @since 4.5.0 added $product_quantity variable
     */

    public static function woo_product_addons_compatibility_cart( $diff_days, $cart_item_meta, $product_quantity ) {
        $addons_price = 0;
        if( class_exists('WC_Product_Addons') ) {
            $single_addon_price = 0;
            
            if( isset( $cart_item_meta['addons'] ) ) {
                $product_addons = $cart_item_meta['addons'];
                
                foreach( $product_addons as $key => $val ) {
                    $single_addon_price += $val['price'];
                }
                
                if( isset( $diff_days ) && $diff_days > 1 && $single_addon_price > 0 ) {
                    //$diff_days         -=  1;
                    $single_addon_price =  $single_addon_price * $diff_days * $product_quantity;
                    $addons_price      +=  $single_addon_price;
                }else{
                    $addons_price      +=  $single_addon_price * $product_quantity;
                }
                    
            }
        }
        return $addons_price;
    }
    
    /**
     * Checks if the product requires booking confirmation from admin
     *
     * If the Product is a bookable product and requires confirmation,
     * returns true else returns false
     *
     * @param int $product_id
     * @return boolean
     * @since 2.5
     */

    public static function bkap_product_requires_confirmation( $product_id ) {
        $product = self::bkap_get_product( $product_id );
         
        // Booking Settings
        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
         
        if (
            is_object( $product )
            && isset( $booking_settings[ 'booking_enable_date' ] ) && 'on' == $booking_settings[ 'booking_enable_date' ]
            && isset( $booking_settings[ 'booking_confirmation' ] ) && 'on' == $booking_settings[ 'booking_confirmation' ]
        ) {
            return true;
        }
         
        return false;
    }
    
    /**
     * Checks if Cart contains bookable products that require confirmation
     *
     * Returns true if Cart contains any bookable products that require
     * confirmation, else returns false.
     *
     * @return boolean
     * @since 2.5
     */

    public static function bkap_cart_requires_confirmation() {
         
        $requires = false;

        if ( isset( WC()->cart ) ) {
            foreach ( WC()->cart->cart_contents as $item ) {
                 
                $duplicate_of     =   bkap_common::bkap_get_product_id( $item['product_id'] );
                
                $requires_confirmation = bkap_common::bkap_product_requires_confirmation( $duplicate_of );
                 
                if ( $requires_confirmation ) {
                    $requires = true;
                    break;
                }
            }
        } 
        return $requires;
    
    }
    
    /**
     * Checks if an order contains products that require
     * admin confirmation.
     * 
     * @param WC_Order $order
     * @return boolean
     * @since 2.5
     */

    public static function bkap_order_requires_confirmation( $order ) {
        $requires = false;
    
        if ( $order ) {
            foreach ( $order->get_items() as $item ) {
                if ( bkap_common::bkap_product_requires_confirmation( $item['product_id'] ) ) {
                    $requires = true;
                    break;
                }
            }
        }
    
        return $requires;
    }
    
    /**
     * Returns a booking object containing booking details 
     * for a bookable item in an order.
     * Used in Confirmation/Cancellation emails
     * 
     * @param integer $item_id
     * @return stdClass
     * @since 2.5
     */

    public static function get_bkap_booking( $item_id ) {
         
        global $wpdb;
         
        $booking_object = new stdClass();
         
        $start_date_label = ( '' == get_option( 'book_item-meta-date' ) ) ? __( 'Start Date', 'woocommerce-booking' ) : get_option( 'book_item-meta-date' ) ;
        $end_date_label = ( '' == get_option( 'checkout_item-meta-date' ) ) ? __( 'End Date', 'woocommerce-booking' ) : get_option( 'checkout_item-meta-date' );
        $time_label = ( '' == get_option( 'book_item-meta-time' ) ) ? __( 'Booking Time', 'woocommerce-booking' ) : get_option( 'book_item-meta-time' );
    
        // order ID
        $query_order_id = "SELECT order_id FROM `". $wpdb->prefix."woocommerce_order_items`
                            WHERE order_item_id = %d";
        $get_order_id = $wpdb->get_results( $wpdb->prepare( $query_order_id, $item_id ) );
         
        $order_id = 0;
        if ( isset( $get_order_id ) && is_array( $get_order_id ) && count( $get_order_id ) > 0 ) {
            $order_id = $get_order_id[0]->order_id;
        }
        $booking_object->order_id = $order_id;
         
        $order = new WC_order( $order_id );
         
        // order date
        $post_data = get_post( $order_id );
        $booking_object->order_date = $post_data->post_date;
         
        // product ID
        $booking_object->product_id = wc_get_order_item_meta( $item_id, '_product_id' );
         
        // product name
        $_product = self::bkap_get_product( $booking_object->product_id );
        $booking_object->product_title = $_product->get_title();
         
        // get the booking status
        $booking_object->item_booking_status = wc_get_order_item_meta( $item_id, '_wapbk_booking_status' );
         
        // get the hidden booking date and time
        $booking_object->item_hidden_date = wc_get_order_item_meta( $item_id, '_wapbk_booking_date' );
        $booking_object->item_hidden_checkout_date = wc_get_order_item_meta( $item_id, '_wapbk_checkout_date' );
        $booking_object->item_hidden_time = wc_get_order_item_meta( $item_id, '_wapbk_time_slot' );
    
        // get the booking date and time to be displayed
        $booking_object->item_booking_date = wc_get_order_item_meta( $item_id, $start_date_label );
        $booking_object->item_checkout_date = wc_get_order_item_meta( $item_id, $end_date_label );
        $booking_object->item_booking_time = wc_get_order_item_meta( $item_id, $time_label );
         
        // email adress
        $booking_object->billing_email = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_email : $order->get_billing_email();
         
        // customer ID
        $booking_object->customer_id = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->user_id : $order->get_user_id();
    
        return $booking_object;
    
    }
    
    /**
     * Returns the number of time slots present for a date.
     * The date needs to be passed in the j-n-Y format
     * 
     * @param integer $product_id
     * @param string $date_check_in
     * @return integer $number_of_slots
     * @since 2.6
     */

    public static function bkap_get_number_of_slots( $product_id, $date_check_in ) {
         
        // Booking settings
        $booking_settings =   get_post_meta( $product_id , 'woocommerce_booking_settings' , true );
            
        $number_of_slots = 0;
        // find the number of slots present for this date/day
        if ( is_array( $booking_settings[ 'booking_time_settings' ] ) && count( $booking_settings[ 'booking_time_settings' ] ) > 0 ) {
            if ( array_key_exists( $date_check_in, $booking_settings[ 'booking_time_settings' ] ) ) {
                $number_of_slots = count( $booking_settings[ 'booking_time_settings' ][ $date_check_in ] );
            } else { // it's a recurring weekday
                $weekday            =   date( 'w', strtotime( $date_check_in ) );
                $booking_weekday    =   'booking_weekday_' . $weekday;
                if( array_key_exists( $booking_weekday, $booking_settings[ 'booking_time_settings' ] ) ) {
                    $number_of_slots = count( $booking_settings[ 'booking_time_settings' ][ $booking_weekday ] );
                }
            }
        }
        return $number_of_slots;    
    }
    
    /**
     * Checks whether a product is bookable or no
     * 
     * @param int $product_id
     * @return bool $bookable
     * @since 2.6
     */

    public static function bkap_get_bookable_status( $product_id ) {
         
        $bookable = false;
         
        // Booking settings
        $booking_settings =   get_post_meta( $product_id , 'woocommerce_booking_settings' , true );
         
        if( isset( $booking_settings ) && isset( $booking_settings[ 'booking_enable_date' ] ) && 'on' == $booking_settings[ 'booking_enable_date' ] ) {
            $bookable = true;
        }
         
        return $bookable;
    }
    
    /**
     * Get all products and variations and sort alphbetically, return in array (title, id)
     * 
     * @param boolean $variations - True if variations for a product also need to be returned, else false
     * @return array $full_product_list
     * @since 2.6
     */

    public static function get_woocommerce_product_list( $variations = true ) {
        
        $full_product_list = array();
         
        $args = array( 
            'post_type'         => array( 'product' ), 
            'posts_per_page'    => -1,
            'post_status'       => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
            'meta_query'        => array(
                                        array(
                                          'key'     => '_bkap_enable_booking',
                                          'value'   => 'on',
                                          'compare' => '=',
                                        ),
                                    )
        );

        $product        = get_posts( $args );    
        $parent_array   = array();
        
        foreach ( $product as $k => $value ) {
            $theid          = $value->ID;            
            $duplicate_of   = bkap_common::bkap_get_product_id( $theid );
            $_product       = self::bkap_get_product( $duplicate_of );

            if( "variable" == $_product->get_type() && $variations ) {

                $variations = $_product->get_available_variations();

                if( empty( $variations ) ) {
                    continue;
                }

                $args = array(
                    'post_type'     => 'product_variation',
                    'post_status'   => array( 'private', 'publish' ),
                    'numberposts'   => -1,
                    'orderby'       => 'menu_order',
                    'order'         => 'asc',
                    'post_parent'   => $duplicate_of // get parent post-ID
                );
                $variations = get_posts( $args );

                foreach ( $variations as $variation ) {
                    $thetitle           = "";
                    $variation_ID       = $variation->ID;                    
                    $product_variation  = new WC_Product_Variation( $variation_ID ); // get variations meta
                    $thetitle           = $product_variation->get_formatted_name();
                    $full_product_list[] = array( $thetitle, $variation_ID );
                }                   

            } else {
                $thetitle               = $_product->get_formatted_name();
                $product_type           = $_product->get_type();   
                $full_product_list[]    = array( $thetitle, $theid );
            }
        }

        // sort into alphabetical order, by title
        sort( $full_product_list );
        
        return $full_product_list;    
    }
    
    /**
     * Get all products and sort alphbetically.
     * Return in array (title, id, fixed block option, price range option)
     * 
     * @return array $full_product_list
     * @since 4.1.0
     */  

    public static function get_woocommerce_product_list_f_p(){
        $full_product_list = array();
        
        $args       = array( 
            'post_type' => 'product', 
            'posts_per_page' => -1,
            'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')
        );
        $product    = query_posts( $args );
        
        foreach ( $product as $k => $value ) {
            $theid = $value->ID;
            $duplicate_of = bkap_common::bkap_get_product_id( $theid );
            
            $bkap_fixed_price = false;
            $bkap_range_price = false;
            
            // Booking settings
            $booking_settings =   get_post_meta( $duplicate_of , 'woocommerce_booking_settings' , true );
            
            $bkap_fixed = ( isset( $booking_settings[ 'booking_fixed_block_enable' ] ) && "" != $booking_settings[ 'booking_fixed_block_enable' ] ) ? $booking_settings[ 'booking_fixed_block_enable' ] : "";
            $bkap_range = ( isset( $booking_settings[ 'booking_block_price_enable' ] ) && "" != $booking_settings[ 'booking_block_price_enable' ] ) ? $booking_settings[ 'booking_block_price_enable' ] : "";
            
            if( isset( $booking_settings ) && isset( $bkap_fixed ) && 'yes' == $bkap_fixed ) {
                $bkap_fixed_price = true;
            }
            
            if( isset( $booking_settings ) && isset( $bkap_range ) && 'yes' == $bkap_range ) {
                $bkap_range_price = true;
            }
            
            if( $bkap_fixed_price || $bkap_range_price ){
                
                $_product = self::bkap_get_product( $theid );
                $thetitle = $_product->get_formatted_name();
                
                $full_product_list[] = array( $thetitle, $theid, $bkap_fixed_price, $bkap_range_price);
            }
            
        }
        wp_reset_query();
        // sort into alphabetical order, by title
        sort($full_product_list);
         
        return $full_product_list;
            
    }
    
    /**
     * Adds item meta for bookable products when an order is placed
     * 
     * @param integer $item_id 
     * @param integer $product_id
     * @param array $booking_data
     * @param bool $gcal_import
     * @since 4.1.0
     */

    public static function bkap_update_order_item_meta( $item_id, $product_id, $booking_data, $gcal_import = false ) {
    
        $booking_settings  =   get_post_meta( $product_id, 'woocommerce_booking_settings', true );
    
        if ( $gcal_import ) {
            wc_add_order_item_meta( $item_id, '_wapbk_booking_status', 'paid' );
        } else {
            if( isset( WC()->session ) && WC()->session !== null && 
                WC()->session->get( 'chosen_payment_method' ) === 'bkap-booking-gateway' ) {
                wc_add_order_item_meta( $item_id, '_wapbk_booking_status', 'pending-confirmation' );
            } else {
                wc_add_order_item_meta( $item_id, '_wapbk_booking_status', 'confirmed' );
            }
        }
         
        if ( $booking_data['date'] != "" ) {
            $name         =   ( '' == get_option( 'book_item-meta-date' ) ) ? __( 'Start Date', 'woocommerce-booking' ) : get_option( 'book_item-meta-date' ) ;
            $name = apply_filters( 'bkap_change_checkout_start_date_label', $name, $booking_settings);
            
            $date_select  =   $booking_data['date'];
            
            wc_add_order_item_meta( $item_id, $name, sanitize_text_field( $date_select, true ) );
        }
         
        if ( array_key_exists( 'hidden_date', $booking_data ) && $booking_data['hidden_date'] != "" ) {
            // save the date in Y-m-d format
            $date_booking = date( 'Y-m-d', strtotime( $booking_data['hidden_date'] ) );
            wc_add_order_item_meta( $item_id, '_wapbk_booking_date', sanitize_text_field( $date_booking, true ) );
        }
         
        if ( array_key_exists( 'date_checkout', $booking_data ) && $booking_data['date_checkout'] != "" ) {
    
            if ( $booking_settings['booking_enable_multiple_day'] == 'on' ) {
                $name_checkout           =   ( '' == get_option( 'checkout_item-meta-date' ) ) ? __( 'End Date', 'woocommerce-booking' ) : get_option( 'checkout_item-meta-date' );
                $date_select_checkout    =   $booking_data['date_checkout'];
                
                wc_add_order_item_meta( $item_id, $name_checkout, sanitize_text_field( $date_select_checkout, true ) );
            }
        }
         
        if ( array_key_exists( 'hidden_date_checkout', $booking_data ) && $booking_data['hidden_date_checkout'] != "" ) {
             
            if ( $booking_settings['booking_enable_multiple_day'] == 'on' ) {
                // save the date in Y-m-d format
                $date_booking = date( 'Y-m-d', strtotime( $booking_data['hidden_date_checkout'] ) );
                wc_add_order_item_meta( $item_id, '_wapbk_checkout_date', sanitize_text_field( $date_booking, true ) );
            }
        }
         
        if ( array_key_exists( 'time_slot', $booking_data ) && $booking_data['time_slot'] != "" ) {
            $time_slot_to_display     =   '';
            $time_select              =   $booking_data['time_slot'];
            $time_exploded            =   explode( "-", $time_select );
             
            $saved_settings = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
    
            if ( isset( $saved_settings ) ) {
                $time_format = $saved_settings->booking_time_format;
            }else{
                $time_format = "12";
            }
    
            $time_slot_to_display = '';
            $from_time = trim($time_exploded[0]);
    
            if( isset( $time_exploded[1] ) ){
                $to_time = trim( $time_exploded[1] );
            }else{
                $to_time = '';
            }
    
            if ( $time_format == '12' ) {
                $from_time = date( 'h:i A', strtotime( $time_exploded[0] ) );
                if( isset( $time_exploded[1] ) )$to_time = date( 'h:i A', strtotime( $time_exploded[1] ) );
            }
    
            $query_from_time  =   date( 'G:i', strtotime( $time_exploded[0] ) );
            $meta_data_format =   $query_from_time;
    
            if( isset( $time_exploded[1] ) ){
                $query_to_time       = date( 'G:i', strtotime( $time_exploded[1] ) );
                $meta_data_format   .= ' - ' . $query_to_time;
            }else{
                $query_to_time       = '';
            }
    
            if( $to_time != '' ) {
                $time_slot_to_display = $from_time.' - '.$to_time;
            }else {
                $time_slot_to_display = $from_time;
            }

            $name_time_slot = ( '' == get_option( 'book_item-meta-time' ) ) ? __( 'Booking Time', 'woocommerce-booking' ): get_option( 'book_item-meta-time' ) ;
    
            wc_add_order_item_meta( $item_id, $name_time_slot , $time_slot_to_display, true );
            wc_add_order_item_meta( $item_id,  '_wapbk_time_slot', $meta_data_format, true );
             
        }
        
        if ( array_key_exists( 'resource_id', $booking_data ) && $booking_data['resource_id'] != 0 ) {
             
            wc_add_order_item_meta( $item_id, '_resource_id', $booking_data['resource_id'] );
        }
        
        if ( array_key_exists( 'resource_id', $booking_data ) && $booking_data['resource_id'] != 0 ) {
        
            $resource_label = Class_Bkap_Product_Resource::bkap_get_resource_label( $product_id );
        
            if ( $resource_label == "" )
                $resource_label = __( 'Resource Type', 'wocommerce-booking' );
        
            $resource_title = get_the_title( $booking_data['resource_id'] );
        
            wc_add_order_item_meta( $item_id, $resource_label, $resource_title );
        }
        do_action( 'bkap_update_item_meta', $item_id, $product_id, $booking_data ); 
    }
    
    /**
     * Creates a list of orders that are not yet exported to GCal
     * 
     * @param integer $user_id - To check if it's being run for a tour operator
     * @return array $total_orders_to_export
     * @since 2.6
     */

    public static function bkap_get_total_bookings_to_export( $user_id ) {
    
        global $wpdb;
        $total_orders_to_export = array();
        
        $user_id = get_current_user_id();
        
        // get the user role
        $user = new WP_User( $user_id );
        if( 'tour_operator' == $user->roles[ 0 ] ) {
            $event_items = get_the_author_meta( 'tours_event_item_ids', $user_id );
        } else {
            $event_items = get_option( 'bkap_event_item_ids' );
        }
        
        if( $event_items == '' || $event_items == '{}' || $event_items == '[]' || $event_items == 'null' ) {
            $event_items = array();
        }
    
        $current_time = current_time( 'timestamp' );
    
        $bkap_query = "SELECT ID, post_status FROM `" . $wpdb->prefix . "posts` WHERE post_type = 'shop_order'";
        $results = $wpdb->get_results( $bkap_query );
    
        $total_orders_to_export = array();
    
        foreach ( $results as $key => $value ) {
            $order_id = $value->ID;
    
            $order_status = $value->post_status;
            
            if( isset( $order_status ) && ( $order_status != 'wc-cancelled' ) && ( $order_status != 'wc-refunded' ) && ( $order_status != 'trash' ) && ( $order_status != '' ) && ( 'auto-draft' != $order_status ) ) {
        
                $get_items_sql  = $wpdb->get_results( $wpdb->prepare( "SELECT order_item_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $order_id, 'line_item' ) );

                $item_values = array();
                foreach ( $get_items_sql as $i_key => $i_value ) {
                    $get_items = $wpdb->get_results( $wpdb->prepare( "SELECT order_item_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key IN ( '_product_id', '_wapbk_booking_status', '_wapbk_booking_date' ) AND order_item_id = %d" , $i_value->order_item_id ) );
                    
                    if ( is_array( $get_items ) && count( $get_items ) > 0 ) {
                        foreach ( $get_items as $get_key => $get_value ) {
                            if ( isset( $get_value->meta_key ) ) {
                                switch ( $get_value->meta_key ) {
                                    case '_product_id':
                                        $item_values[ $get_value->order_item_id ][ 'product_id' ] = $get_value->meta_value;
                                        break;
                                    case '_wapbk_booking_date':
                                        $item_values[ $get_value->order_item_id ][ 'wapbk_booking_date' ] = $get_value->meta_value;
                                        break;
                                    case '_wapbk_booking_status':
                                        $item_values[ $get_value->order_item_id ][ 'wapbk_booking_status' ] = $get_value->meta_value;
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }
                }
                $i = 0;
                
                if ( is_array( $item_values ) && count( $item_values ) > 0 ) {
                    foreach ( $item_values as $i_key => $i_values ) {
                            
                        $booking_status = '';
                        $booking_date = '';
        
                        if( !in_array( $i_key, $event_items ) ) {
        
                            $is_bookable = bkap_common::bkap_get_bookable_status( $i_values[ 'product_id' ] );
                            $valid_date = false;
                            if ( isset( $i_values[ 'wapbk_booking_date' ] ) ) { 
                               $valid_date = bkap_common::bkap_check_date_set( $i_values[ 'wapbk_booking_date' ] );
                            }
                            if ( $is_bookable && $valid_date ) {
                                // check if the item belongs to a tour operator
                                $booking_settings = get_post_meta( $i_values[ 'product_id' ], 'woocommerce_booking_settings', true );
                                 
                                // check if the tour operators plugin is active
                                if ( function_exists( 'is_bkap_tours_active' ) && is_bkap_tours_active() ) {
                                     
                                    if ( isset( $booking_settings[ 'booking_tour_operator' ] ) && $booking_settings[ 'booking_tour_operator' ] != '' ) {
                                        // if yes, then if gcal setup is allowed for the tour operator the item should be ignored for the admin
                                        if ( 'yes' == get_option( 'bkap_allow_tour_operator_gcal_api' ) ) {
                                            if ( $booking_settings[ 'booking_tour_operator' ] == $user_id ) {
                                                $add_item = 'YES';
                                            } else {
                                                $add_item = 'NO';
                                            }
                                        } else if ( 'tour_operator' != $user->roles[ 0 ] ) { // if no, then the admin should be allowed to export the item
                                            $add_item = 'YES';
                                        }
                                    } else if ( 'tour_operator' != $user->roles[ 0 ] ) { // as admin should be allowed to export such items
                                        $add_item = 'YES';
                                    }
                                } else {
                                    $add_item = 'YES';
                                }
                            }
                             
                            if ( isset( $add_item ) && 'YES' == $add_item ) {
                                
                                if ( isset( $i_values[ 'wapbk_booking_status' ] ) ) {
                                    $booking_status = $i_values[ 'wapbk_booking_status' ];
                                }
                                if ( isset( $i_values[ 'wapbk_booking_date' ] ) ) {
                                    $booking_date = strtotime( $i_values[ 'wapbk_booking_date' ] );
                                }
                                if ( isset( $booking_status ) && $booking_status != 'pending-confirmation' && isset( $booking_date ) && $booking_date != '' && $booking_date >= $current_time ) {
                                    $total_orders_to_export[ $value->ID ][ $i ] = $i_key;
                                }
                                $i++;
                            }
                        }
                    }
                }
            }
        }
        return $total_orders_to_export;
    }
    
    /**
     * Returns an array with currency data
     *
     * @return array $wc_price_args - array of currency data such as currency, separators etc.
     * @since 2.6.3
     */

    public static function get_currency_args() {
        if ( function_exists( 'icl_object_id' ) ) {
            global $woocommerce_wpml;
            
            if ( isset( $woocommerce_wpml->settings[ 'enable_multi_currency' ] ) && $woocommerce_wpml->settings[ 'enable_multi_currency' ] == '2' ) {
                if ( WCML_VERSION >= '3.8' ) {
                    $currency = $woocommerce_wpml->multi_currency->get_client_currency();
                } else {
                   $currency = $woocommerce_wpml->multi_currency_support->get_client_currency();
                }
            }else{
                $currency = get_woocommerce_currency();
            }
            $wc_price_args = array(
                'currency'           => $currency,
                'decimal_separator'  => wc_get_price_decimal_separator(),
                'thousand_separator' => wc_get_price_thousand_separator(),
                'decimals'           => wc_get_price_decimals(),
                'price_format'       => get_woocommerce_price_format()
            );
        } else {
            $wc_price_args = array(
                'currency'           => get_woocommerce_currency(),
                'decimal_separator'  => wc_get_price_decimal_separator(),
                'thousand_separator' => wc_get_price_thousand_separator(),
                'decimals'           => wc_get_price_decimals(),
                'price_format'       => get_woocommerce_price_format()
            );
        }
        return $wc_price_args;
    }
    
    /**
     * The below function adds notices to be displayed.
     * It displays the notices as well using print notices function.
     * This helps in displaying notices without having to reload the page.
     * 
     * @since 2.9
     */

    public static function bkap_add_notice() {
        $product_id = $_POST[ 'post_id' ];
    
        $message = '';
        if ( isset( $_POST[ 'message' ] ) ) {
            $message = $_POST[ 'message' ];
        }
    
        $notice_type = 'error';
        if ( isset( $_POST[ 'notice_type' ] ) ) {
            $notice_type = $_POST[ 'notice_type' ];
        }
    
        if ( ( isset( $message ) && '' != $message ) ) {
            wc_add_notice( __( $message, 'woocommerce-booking' ), $notice_type );
            wc_print_notices();
        }
        die;
    }
    
    /**
     * This function clears any notices set in the session
     * 
     * @since 2.9
     */

    public static function bkap_clear_notice() {
        wc_clear_notices();
        die;
    }
    
    /**
     * This function will return the differance days between two dates.
     * An object similar to the one returned by the new DateTime() is returned.
     * 
     * @param object $date1 - Date 1
     * @param object $date2 - Date 2
     * @return object $result - contains difference details between 2 dates
     * @since 3.1
     */

    public static function dateTimeDiff( $date1, $date2 ) {
        
        $one = $date1->format('U');
        $two = $date2->format('U');
        
        $invert = false;
        if ( $one > $two ) {
            list( $one, $two ) = array( $two, $one );
            $invert = true;
        }
        
        $key           = array("y", "m", "d", "h", "i", "s");
        $a             = array_combine( $key, array_map( "intval", explode( " ", date( "Y m d H i s", $one ) ) ) );
        $b             = array_combine( $key, array_map( "intval", explode( " ", date( "Y m d H i s", $two ) ) ) );
        $result        = new stdClass();
        $current_time  = current_time( 'timestamp' );
        $date          = ( date ("d", $current_time) ) - 1 ;
         
         
        $result->y         = $b["y"] - $a["y"];
        $result->m         = $b["m"] - $a["m"];
        $result->d         = $date;
        $result->h         = $b["h"] - $a["h"];
        $result->i         = $b["i"] - $a["i"];
        $result->s         = $b["s"] - $a["s"];
        $result->invert    = $invert ? 1 : 0;
        $result->days      = intval(abs(($one - $two)/86400));
        
         
        if ( $invert ) {
            bkap_common::_date_normalize( $a, $result );
        } else {
            bkap_common::_date_normalize( $b, $result );
        }
        
        return $result;
    }

    /**
     * Calculates the difference between the dates. 
     * Called  from dateTimeDiff()
     * 
     * @param integer $start
     * @param integer $end
     * @param integer adj
     * @param string $a
     * @param string $b
     * @param array $result
     * @return array $result
     * @since 3.1
     */

    public static function _date_range_limit( $start, $end, $adj, $a, $b, $result ) {
        $result = (array)$result;
        if ( $result[ $a ] < $start ) {
            $result[ $b ] -= intval( ( $start - $result[ $a ] - 1 ) / $adj ) + 1;
            $result[ $a ] += $adj * intval( ( $start - $result[ $a ] - 1) / $adj + 1 );
        }
    
        if ( $result[ $a ] >= $end ) {
            $result[ $b ] += intval( $result[ $a ] / $adj );
            $result[ $a ] -= $adj * intval( $result[ $a ] / $adj );
        }
    
        return $result;
    }

    /**
     * Calculates the Ramge Limit.
     * Called from dateTimeDiff()
     * 
     * @param array $base
     * @param object $result
     * @return object $result
     * @since 3.1
     */

    public static function _date_range_limit_days( $base, $result ) {
        $days_in_month_leap    = array( 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
        $days_in_month         = array( 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
    
        bkap_common::_date_range_limit(1, 13, 12, "m", "y", $base);
    
        $year  = $base["y"];
        $month = $base["m"];
    
        if ( !$result["invert"] ) {
            while ( $result["d"] < 0 ) {
                $month--;
                if ( $month < 1 ) {
                    $month += 12;
                    $year--;
                }
    
                $leapyear  = $year % 400 == 0 || ( $year % 100 != 0 && $year % 4 == 0 );
                $days      = $leapyear ? $days_in_month_leap[ $month ] : $days_in_month[ $month ];
    
                $result["d"] += $days;
                $result["m"]--;
            }
        } else {
            while ( $result["d"] < 0 ) {
                $leapyear  = $year % 400 == 0 || ( $year % 100 != 0 && $year % 4 == 0 );
                $days      = $leapyear ? $days_in_month_leap[ $month ] : $days_in_month[ $month ];
    
                $result["d"] += $days;
                $result["m"]--;
    
                $month++;
                if ( $month > 12 ) {
                    $month -= 12;
                    $year++;
                }
            }
        }
    
        return $result;
    }
    
    /**
     * Normalize the Date. Called from dateTimeDiff()
     *  
     * @param array $base
     * @param object $result
     * @return array $result
     * @since 3.1
     */

    public static  function _date_normalize( $base, $result ) {
        $result = bkap_common::_date_range_limit(0, 60, 60, "s", "i", $result);
        $result = bkap_common::_date_range_limit(0, 60, 60, "i", "h", $result);
        $result = bkap_common::_date_range_limit(0, 24, 24, "h", "d", $result);
        $result = bkap_common::_date_range_limit(0, 12, 12, "m", "y", $result);
    
        $result = bkap_common::_date_range_limit_days( $base, $result );
    
        $result = bkap_common::_date_range_limit( 0, 12, 12, "m", "y", $result );
    
        return $result;
    }

    /**
     * Ensures whether the date is a future date.
     * Used when exporting bookings to Google Calendar
     *
     * @param string $date - Booking Date
     * @return boolean $future_date_set
     * @since 2.6
     */

    public static function bkap_check_date_set( $date ) {
        $future_date_set = false;
    
        if ( isset( $date ) && '' != $date ) {
    
            if ( strtotime( $date ) > current_time( 'timestamp' ) ) {
                $future_date_set = true;
            }
        }
        return $future_date_set;
    }

    /**
     * Returns TRUE if a cart contains bookable products
     *
     * @return boolean $contains_bookable
     * @since 2.5
     */

    public static function bkap_cart_contains_bookable() {
    
        $contains_bookable = false;
    
        $cart_items_count = WC()->cart->cart_contents_count;
    
        if ( $cart_items_count > 0  ) {
            foreach ( WC()->cart->cart_contents as $item ) {
    
                $is_bookable = bkap_common::bkap_get_bookable_status( $item['product_id'] );
    
                if ( $is_bookable ) {
                    $contains_bookable = true;
                    break;
                }
            }
        }
        return $contains_bookable;
    
    }

    /**
     * Create and return an array of valid Booking statuses
     *
     * @return array $allowed_status - booking statuses
     * @since 4.0.0
     */

     static function get_bkap_booking_statuses() {

        return $allowed_status = array( 'pending-confirmation' => 'Pending Confirmation',
            'confirmed' => 'Confirmed',
            'paid' => 'Paid',
            'cancelled' => 'Cancelled'
        );
        
    }
    
    /**
     * Create and return an array of valid event statuses
     * - For Imported Events from Google Calendar
     *
     * @return array $allowed_status - event statuses
     * @since 4.0.0
     */

    static function get_bkap_event_statuses() {
    
        return $allowed_status = array( 'bkap-unmapped' => 'Un-mapped',
            'bkap-mapped' => 'Mapped',
            'bkap-deleted' => 'Deleted'
        );
         
    }

    /**
     * Fetches the Booking Post ID using the Item ID
     *
     * @param int $item_id - Item ID
     * @return int booking ID from wp_post
     * @since 4.0.0
     */

    static function get_booking_id( $item_id ) {
        global $wpdb;

        $query_posts = "SELECT post_id FROM `" . $wpdb->prefix . "postmeta`
                   where meta_key = %s
                   AND meta_value = %d";

        $get_posts = $wpdb->get_results( $wpdb->prepare( $query_posts, '_bkap_order_item_id', $item_id ) );

        if ( count( $get_posts ) > 0 ) {
            return $get_posts[ 0 ]->post_id;
        } else {
            return false;
        }
    }
  
    /**
     * This function Checks if the Specific Date contains time slot or not.
     * 
     * @param integer $product_id
     * @return boolean $timeslots_present
     * @since 4.2
     */

    public static function bkap_check_specific_date_has_timeslot ( $product_id ) {

        $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
        $booking_specific_dates = ( isset( $booking_settings[ 'booking_specific_date' ] ) ) ? $booking_settings[ 'booking_specific_date' ] : array();

          if( "" != $booking_specific_dates && count( $booking_specific_dates ) > 0 ){

          if( isset( $booking_settings['booking_time_settings'] ) && count($booking_settings['booking_time_settings'] ) > 0 ) {
            $booking_time_settings_key = array_keys(  $booking_settings['booking_time_settings'] );

          }

          foreach ( $booking_specific_dates as $booking_specific_dates_key => $booking_specific_dates_value ) {

            if ( is_array ( $booking_time_settings_key ) && count( $booking_time_settings_key ) > 0 ){

              if( in_array( $booking_specific_dates_key, $booking_time_settings_key ) ) {

                  return true;
              }
            }

          } 
        }
        
          return false;
    }
  
    /**
     * The function checks if the passed product ID is listed 
     * as a child in _children postmeta.
     * 
     * If yes, then it returns the parent product ID else it returns 0
     * 
     * @param int $child_id
     * @return int $parent_id
     * 
     * @since 3.5.2
     */

    public static function bkap_get_parent_id( $child_id ) {
        $parent_id = '';
         
        global $wpdb;
         
        $query_children = "SELECT post_id, meta_value FROM `" . $wpdb->prefix . "postmeta`
                           WHERE meta_key = %s";
         
        $results_children = $wpdb->get_results( $wpdb->prepare( $query_children, '_children' ) );
         
        if ( is_array( $results_children ) && count( $results_children ) > 0 ) {
             
            foreach( $results_children as $r_value ) {
                // check if the meta value is non blanks
                if ( $r_value->meta_value != '' ) {
                    // unserialize the data, create an array
                    $child_array = maybe_unserialize( $r_value->meta_value );
                    // if child ID is present in the array, we've found the parent
                    if ( ( in_array( $child_id, $child_array ) ) ) {
                        $parent_id = $r_value->post_id;
                        break;
                    }
                }
            }
        }
        return $parent_id;
    }

    /**
     * Get WooCommerce Product object
     * 
     * @param int|string $product_id Product ID
     * @return WC_Product Product Object
     * @since 4.1.1
     */

    public static function bkap_get_product( $product_id ) {
        return wc_get_product( $product_id );
    }

    /**
     * Get Gravity Forms Addon Data for pricing purpose from cart item
     * 
     * @param array $cart_item Cart Item Array
     * @return array Addon Pricing array
     * @since 4.2
     */

    public static function bkap_get_cart_item_addon_data( $cart_item ) {

        $addon_pricing_data = array();

        // For compatibility with Gravity Forms
        if ( isset( $cart_item['_gform_total'] ) && $cart_item['_gform_total'] > 0 ) {
            $addon_pricing_data['gf_options'] = $cart_item['_gform_total'];
        }

        // For compatibility with WooCommerce Product Addons
        if ( isset( $cart_item['addons'] ) && count( $cart_item['addons'] ) > 0 ) {
            $addon_pricing_data['wpa_options'] = self::bkap_get_wpa_cart_totals( $cart_item );
        }

        return $addon_pricing_data;
    }

    /**
     * Get WooCommerce Product addons prices
     * 
     * @param array $cart_item Cart Item
     * @return float Addon Total
     * @since 4.2
     */

    public static function bkap_get_wpa_cart_totals( $cart_item ) {

        $wpa_addons_total = 0;
        foreach ( $cart_item['addons'] as $addon_key => $addon_value ) {
            $wpa_addons_total = $wpa_addons_total + $addon_value['price'];
        }

        return $wpa_addons_total;
    }

    /**
     * Get Addon data for pricing purpose from Order Item data
     * Includes Gravity Form Addons as well as WooCommerce Product Addons
     * 
     * @param WC_Order_Item $order_item Order Item object
     * @return array Addon pricing array
     * @since 4.2
     */

    public static function bkap_get_order_item_addon_data( $order_item ) {

        $addon_pricing_data = array();

        // For compatibility with Gravity Forms
        $currency_symbol = get_woocommerce_currency_symbol();
        if ( isset( $order_item['Total'] ) && $order_item['Total'] !== '' ) {
            $addon_pricing_data['gf_options'] = str_replace( html_entity_decode($currency_symbol), '', $order_item['Total'] );
        }

        // For compatibility with WooCommerce Product Addons
        if ( isset( $order_item['_wapbk_wpa_prices'] ) && $order_item['_wapbk_wpa_prices'] !== '' ) {
            $addon_pricing_data['wpa_options'] = $order_item['_wapbk_wpa_prices'];
        }

        return $addon_pricing_data;
    }

    /**
     * Check if cart item passed is composite in some parent product
     * 
     * @param array $cart_item Cart Item Array
     * @return bool
     * @since 4.7.0
     */

    public static function bkap_is_cartitem_composite( $cart_item ) {
        
        if ( isset( $cart_item['composite_parent'] ) ) {
            return true;
        }
        return false;
    }

    /**
     * Check if order item is composite in some parent product
     * 
     * @param WC_Order_Item $item Order Item object
     * @return bool
     * @since 4.7.0
     */

    public static function bkap_is_orderitem_composite( $item ) {
        
        if ( isset( $item['_composite_parent'] ) ) {
            return true;
        }
        return false;
    }

    /**
     * Check if cart item passed is bundled in some parent product
     * 
     * @param array $cart_item Cart Item Array
     * @return bool
     * @since 4.2
     */

    public static function bkap_is_cartitem_bundled( $cart_item ) {
        
        if ( isset( $cart_item['bundled_by'] ) ) {
            return true;
        }
        return false;
    }

    /**
     * Check if order item is bundled in some parent product
     * 
     * @param WC_Order_Item $item Order Item object
     * @return bool
     * @since 4.2
     */

    public static function bkap_is_orderitem_bundled( $item ) {
        
        if ( isset( $item['_bundled_by'] ) ) {
            return true;
        }
        return false;
    }

    /**
     * Get cart configuration for Bundled Products
     * 
     * @param WC_Product $product Product Object
     * @return array Cart Config
     * @since 4.2
     */

    public static function bkap_bundle_add_to_cart_config( $product ) {

        $posted_config = array();

        if ( is_object( $product ) && 'bundle' === $product->get_type() ) {

            $product_id    = WC_PB_Core_Compatibility::get_id( $product );
            $bundled_items = $product->get_bundled_items();

            if ( ! empty( $bundled_items ) ) {

                $posted_data = $_POST;

                if ( empty( $_POST[ 'add-to-cart' ] ) && ! empty( $_GET[ 'add-to-cart' ] ) ) {
                    $posted_data = $_GET;
                }

                foreach ( $bundled_items as $bundled_item_id => $bundled_item ) {

                    $posted_config[ $bundled_item_id ] = array();

                    $bundled_product_id   = $bundled_item->product_id;
                    $bundled_product_type = $bundled_item->product->get_type();
                    $is_optional          = $bundled_item->is_optional();

                    $bundled_item_quantity_request_key = apply_filters( 'woocommerce_product_bundle_field_prefix', '', $product_id ) . 'bundle_quantity_' . $bundled_item_id;
                    $bundled_product_qty               = isset( $posted_data[ $bundled_item_quantity_request_key ] ) ? absint( $posted_data[ $bundled_item_quantity_request_key ] ) : $bundled_item->get_quantity();

                    $posted_config[ $bundled_item_id ][ 'product_id' ] = $bundled_product_id;

                    if ( $bundled_item->has_title_override() ) {
                        $posted_config[ $bundled_item_id ][ 'title' ] = $bundled_item->get_raw_title();
                    }

                    if ( $is_optional ) {

                        /** Documented in method 'get_posted_bundle_configuration'. */
                        $bundled_item_selected_request_key = apply_filters( 'woocommerce_product_bundle_field_prefix', '', $product_id ) . 'bundle_selected_optional_' . $bundled_item_id;

                        $posted_config[ $bundled_item_id ][ 'optional_selected' ] = isset( $posted_data[ $bundled_item_selected_request_key ] ) ? 'yes' : 'no';

                        if ( 'no' === $posted_config[ $bundled_item_id ][ 'optional_selected' ] ) {
                            $bundled_product_qty = 0;
                        }
                    }

                    $posted_config[ $bundled_item_id ][ 'quantity' ] = $bundled_product_qty;

                    // Store variable product options in stamp to avoid generating the same bundle cart id.
                    if ( 'variable' === $bundled_product_type || 'variable-subscription' === $bundled_product_type ) {

                        $attr_stamp = array();
                        $attributes = $bundled_item->product->get_attributes();

                        foreach ( $attributes as $attribute ) {

                            if ( ! $attribute[ 'is_variation' ] ) {
                                continue;
                            }

                            $taxonomy = WC_PB_Core_Compatibility::wc_variation_attribute_name( $attribute[ 'name' ] );

                            /** Documented in method 'get_posted_bundle_configuration'. */
                            $bundled_item_taxonomy_request_key = apply_filters( 'woocommerce_product_bundle_field_prefix', '', $product_id ) . 'bundle_' . $taxonomy . '_' . $bundled_item_id;

                            if ( isset( $posted_data[ $bundled_item_taxonomy_request_key ] ) ) {

                                // Get value from post data.
                                if ( $attribute[ 'is_taxonomy' ] ) {
                                    $value = sanitize_title( stripslashes( $posted_data[ $bundled_item_taxonomy_request_key ] ) );
                                } else {
                                    $value = wc_clean( stripslashes( $posted_data[ $bundled_item_taxonomy_request_key ] ) );
                                }

                                $attr_stamp[ $taxonomy ] = $value;
                            }
                        }

                        $posted_config[ $bundled_item_id ][ 'attributes' ]   = $attr_stamp;
                        $bundled_item_variation_id_request_key               = apply_filters( 'woocommerce_product_bundle_field_prefix', '', $product_id ) . 'bundle_variation_id_' . $bundled_item_id;
                        $posted_config[ $bundled_item_id ][ 'variation_id' ] = isset( $posted_data[ $bundled_item_variation_id_request_key ] ) ? $posted_data[ $bundled_item_variation_id_request_key ] : '';
                    }
                }
            }
        }

        return $posted_config;
    }

    /**
     * Returns an array of Booking IDs for the order ID sent.
     * 
     * @param integer $order_id 
     * @since 4.2.0
     */

    public static function get_booking_ids_from_order_id( $order_id ) {
         
        $booking_ids = array();
        if ( absint( $order_id ) > 0 ) {
    
            global $wpdb;
            if ( false !== get_post_status( $order_id ) ) {
                 
                $order_query = "SELECT ID from `" . $wpdb->prefix . "posts`
                               WHERE post_parent = %d";
    
                $results = $wpdb->get_results( $wpdb->prepare( $order_query, $order_id ) );
                 
                if ( isset( $results ) && count( $results ) > 0 ) {
                    foreach( $results as $r_value ) {
                        $booking_ids[] = $r_value->ID;
                    }
                }
            }
        }
         
        return $booking_ids;
    }

    /**
     * Returns an array of all bookings for passed status.
     *
     * @param string|array $post_status - Valid Booking Statuses
     * @param array $additional_args - that can be added to the query.
     * @return array $bookings_array - Bookings present in wp_posts
     * @since 4.2.0
     */

    public static function bkap_get_bookings( $post_status, $additional_args = '' ) {
        
        $bookings_array = array();
        $search = ( isset( $_GET['s'] ) && '' !== $_GET['s'] ) ? $_GET['s'] : '';

        $args = array(
            'post_status'    => $post_status,
            's'              => $search
        );

        $wp_args = wp_parse_args( $args, array(
            'post_status'    => array( 'draft', 'cancelled', 'confirmed', 'paid', 'pending-confirmation' ),
            'post_type'      => 'bkap_booking',
            'parent'         => null,
            'posts_per_page' => -1,
            'meta_query'     => array(),
            'orderby'        => 'date',
            'order'          => 'DESC',
            'return'         => 'objects',
        ) );

        if ( isset( $additional_args ) && $additional_args !== '' ) {
            $wp_args = wp_parse_args( $wp_args, $additional_args );
        }

        $booking = new WP_Query( $wp_args );

        foreach ( $booking->posts as $posts ) {
            $bookings_array[] = new BKAP_Booking($posts);
        }

        return $bookings_array;
    }

    /**
     * Returns the Status Label
     *
     * @param string $status - Booking status
     * @return string
     * @since 4.2.0
     */

    public static function get_mapped_status( $status ) {
        
        switch ( $status ) {
            case 'paid':
                return __( 'Paid and Confirmed', 'woocommerce-booking' );
                break;

            case 'confirmed':
                return __( 'Confirmed', 'woocommerce-booking' );
                break;

            case 'pending-confirmation':
                return __( 'Pending Confirmation', 'woocommerce-booking' );
                break;

            case 'cancelled':
                return __( 'Cancelled', 'woocommerce-booking' );
                break;

            case 'draft':
                return __( 'Draft', 'woocommerce-booking' );
                break;
            
            default:
                return __( 'Paid and Confirmed', 'woocommerce-booking' );
                break;
        }
    }   
}
?>
API documentation generated by ApiGen