api-helpers.php 89.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538
<?php

/**
 *  This function will return true for a non empty array
 *
 *  @since   5.4.0
 *
 *  @param   mixed $array The variable to test.
 *  @return  boolean
 */
function acf_is_array( $array ) {
	return ( is_array( $array ) && ! empty( $array ) );
}

/**
 *  Alias of acf()->has_setting()
 *
 *  @since   5.6.5
 *
 *  @param   string $name Name of the setting to check for.
 *  @return  boolean
 */
function acf_has_setting( $name = '' ) {
	return acf()->has_setting( $name );
}


/**
 *  acf_raw_setting
 *
 *  alias of acf()->get_setting()
 *
 *  @date    2/2/18
 *  @since   5.6.5
 *
 *  @param   n/a
 *  @return  n/a
 */
function acf_raw_setting( $name = '' ) {
	return acf()->get_setting( $name );
}


/*
*  acf_update_setting
*
*  alias of acf()->update_setting()
*
*  @type    function
*  @date    28/09/13
*  @since   5.0.0
*
*  @param   $name (string)
*  @param   $value (mixed)
*  @return  n/a
*/
function acf_update_setting( $name, $value ) {
	// validate name.
	$name = acf_validate_setting( $name );

	// update.
	return acf()->update_setting( $name, $value );
}


/**
 *  acf_validate_setting
 *
 *  Returns the changed setting name if available.
 *
 *  @date    2/2/18
 *  @since   5.6.5
 *
 *  @param   n/a
 *  @return  n/a
 */
function acf_validate_setting( $name = '' ) {
	return apply_filters( 'acf/validate_setting', $name );
}


/**
 * Alias of acf()->get_setting()
 *
 * @since   5.0.0
 *
 * @param   string $name The name of the setting to test.
 * @param string $value An optional default value for the setting if it doesn't exist.
 * @return  n/a
 */
function acf_get_setting( $name, $value = null ) {
	$name = acf_validate_setting( $name );

	// replace default setting value if it exists.
	if ( acf_has_setting( $name ) ) {
		$value = acf_raw_setting( $name );
	}

	// filter.
	$value = apply_filters( "acf/settings/{$name}", $value );

	return $value;
}

/**
 * Return an array of ACF's internal post type names
 *
 * @since 6.1
 * @return array An array of ACF's internal post type names
 */
function acf_get_internal_post_types() {
	return array( 'acf-field-group', 'acf-post-type', 'acf-taxonomy', 'acf-ui-options-page' );
}

/*
*  acf_append_setting
*
*  This function will add a value into the settings array found in the acf object
*
*  @type    function
*  @date    28/09/13
*  @since   5.0.0
*
*  @param   $name (string)
*  @param   $value (mixed)
*  @return  n/a
*/

function acf_append_setting( $name, $value ) {

	// vars
	$setting = acf_raw_setting( $name );

	// bail early if not array
	if ( ! is_array( $setting ) ) {
		$setting = array();
	}

	// append
	$setting[] = $value;

	// update
	return acf_update_setting( $name, $setting );
}


/**
 *  acf_get_data
 *
 *  Returns data.
 *
 *  @date    28/09/13
 *  @since   5.0.0
 *
 *  @param   string $name
 *  @return  mixed
 */

function acf_get_data( $name ) {
	return acf()->get_data( $name );
}


/**
 *  acf_set_data
 *
 *  Sets data.
 *
 *  @date    28/09/13
 *  @since   5.0.0
 *
 *  @param   string $name
 *  @param   mixed  $value
 *  @return  n/a
 */

function acf_set_data( $name, $value ) {
	return acf()->set_data( $name, $value );
}

/**
 * Appends data to an existing key.
 *
 * @since   5.9.0
 *
 * @param string $name The data name.
 * @param mixed  $data The data to append to name.
 * @return void
 */
function acf_append_data( $name, $data ) {
	$prev_data = acf()->get_data( $name );
	if ( is_array( $prev_data ) ) {
		$data = array_merge( $prev_data, $data );
	}
	acf()->set_data( $name, $data );
}

/**
 *  Alias of acf()->init() - the core ACF init function.
 *
 *  @since   5.0.0
 */
function acf_init() {
	acf()->init();
}


/*
*  acf_has_done
*
*  This function will return true if this action has already been done
*
*  @type    function
*  @date    16/12/2015
*  @since   5.3.2
*
*  @param   $name (string)
*  @return  (boolean)
*/

function acf_has_done( $name ) {

	// return true if already done
	if ( acf_raw_setting( "has_done_{$name}" ) ) {
		return true;
	}

	// update setting and return
	acf_update_setting( "has_done_{$name}", true );
	return false;
}




/**
 * This function will return the path to a file within an external folder
 *
 *  @since   5.5.8
 *
 *  @param   string $file Directory path.
 *  @param   string $path Optional file path.
 *  @return  string File path.
 */
function acf_get_external_path( $file, $path = '' ) {
	return plugin_dir_path( $file ) . $path;
}


/**
 * This function will return the url to a file within an internal ACF folder
 *
 *  @since   5.5.8
 *
 *  @param   string $file Directory path.
 *  @param   string $path Optional file path.
 *  @return  string File path.
 */
function acf_get_external_dir( $file, $path = '' ) {
	return acf_plugin_dir_url( $file ) . $path;
}


/**
 *  This function will calculate the url to a plugin folder.
 *  Different to the WP plugin_dir_url(), this function can calculate for urls outside of the plugins folder (theme include).
 *
 *  @date    13/12/17
 *  @since   5.6.8
 *
 *  @param   string $file A file path inside the ACF plugin to get the plugin directory path from.
 *  @return  string The plugin directory path.
 */
function acf_plugin_dir_url( $file ) {
	$path = plugin_dir_path( $file );
	$path = wp_normalize_path( $path );

	// check plugins.
	$check_path = wp_normalize_path( realpath( WP_PLUGIN_DIR ) );
	if ( strpos( $path, $check_path ) === 0 ) {
		return str_replace( $check_path, plugins_url(), $path );
	}

	// check wp-content.
	$check_path = wp_normalize_path( realpath( WP_CONTENT_DIR ) );
	if ( strpos( $path, $check_path ) === 0 ) {
		return str_replace( $check_path, content_url(), $path );
	}

	// check root.
	$check_path = wp_normalize_path( realpath( ABSPATH ) );
	if ( strpos( $path, $check_path ) === 0 ) {
		return str_replace( $check_path, site_url( '/' ), $path );
	}

	// return.
	return plugin_dir_url( $file );
}


/**
 *  This function will merge together 2 arrays and also convert any numeric values to ints
 *
 *  @since   5.0.0
 *
 *  @param   array $args The configured arguments array.
 *  @param   array $defaults The default properties for the passed args to inherit.
 *  @return  array $args Parsed arguments with defaults applied.
 */
function acf_parse_args( $args, $defaults = array() ) {
	$args = wp_parse_args( $args, $defaults );

	// parse types
	$args = acf_parse_types( $args );

	return $args;
}


/*
*  acf_parse_types
*
*  This function will convert any numeric values to int and trim strings
*
*  @type    function
*  @date    18/10/13
*  @since   5.0.0
*
*  @param   $var (mixed)
*  @return  $var (mixed)
*/

function acf_parse_types( $array ) {
	return array_map( 'acf_parse_type', $array );
}


/*
*  acf_parse_type
*
*  description
*
*  @type    function
*  @date    11/11/2014
*  @since   5.0.9
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_parse_type( $v ) {

	// Check if is string.
	if ( is_string( $v ) ) {

		// Trim ("Word " = "Word").
		$v = trim( $v );

		// Convert int strings to int ("123" = 123).
		if ( is_numeric( $v ) && strval( intval( $v ) ) === $v ) {
			$v = intval( $v );
		}
	}

	// return.
	return $v;
}


/**
 *  This function will load in a file from the 'admin/views' folder and allow variables to be passed through
 *
 *  @date    28/09/13
 *  @since   5.0.0
 *
 *  @param string $view_path
 *  @param array  $view_args
 *
 *  @return void
 */
function acf_get_view( $view_path = '', $view_args = array() ) {
	// allow view file name shortcut
	if ( substr( $view_path, -4 ) !== '.php' ) {
		$view_path = acf_get_path( "includes/admin/views/{$view_path}.php" );
	}

	// include
	if ( file_exists( $view_path ) ) {
		// Use `EXTR_SKIP` here to prevent `$view_path` from being accidentally/maliciously overridden.
		extract( $view_args, EXTR_SKIP );
		include $view_path;
	}
}


/*
*  acf_merge_atts
*
*  description
*
*  @type    function
*  @date    2/11/2014
*  @since   5.0.9
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_merge_atts( $atts, $extra = array() ) {

	// bail early if no $extra
	if ( empty( $extra ) ) {
		return $atts;
	}

	// trim
	$extra = array_map( 'trim', $extra );
	$extra = array_filter( $extra );

	// merge in new atts
	foreach ( $extra as $k => $v ) {

		// append
		if ( $k == 'class' || $k == 'style' ) {

			$atts[ $k ] .= ' ' . $v;

			// merge
		} else {

			$atts[ $k ] = $v;

		}
	}

	return $atts;
}


/**
 * This function will create and echo a basic nonce input
 *
 *  @since   5.6.0
 *
 *  @param string $nonce The nonce parameter string.
 *  @return void
 */
function acf_nonce_input( $nonce = '' ) {
	echo '<input type="hidden" name="_acf_nonce" value="' . wp_create_nonce( $nonce ) . '" />';
}


/**
 * This function will remove the var from the array, and return the var
 *
 * @since   5.0.0
 *
 * @param array  $extract_array an array passed as reference to be extracted.
 * @param string $key The key to extract from the array.
 * @param mixed  $default_value The default value if it doesn't exist in the extract array.
 * @return mixed Extracted var or default.
 */
function acf_extract_var( &$extract_array, $key, $default_value = null ) {
	// check if exists - uses array_key_exists to extract NULL values (isset will fail).
	if ( is_array( $extract_array ) && array_key_exists( $key, $extract_array ) ) {

		// store and unset value.
		$v = $extract_array[ $key ];
		unset( $extract_array[ $key ] );

		return $v;
	}

	return $default_value;
}


/**
 * This function will remove the vars from the array, and return the vars
 *
 * @since   5.0.0
 *
 * @param array $extract_array an array passed as reference to be extracted.
 * @param array $keys An array of keys to extract from the original array.
 * @return array An array of extracted values.
 */
function acf_extract_vars( &$extract_array, $keys ) {
	$r = array();

	foreach ( $keys as $key ) {
		$r[ $key ] = acf_extract_var( $extract_array, $key );
	}

	return $r;
}


/*
*  acf_get_sub_array
*
*  This function will return a sub array of data
*
*  @type    function
*  @date    15/03/2016
*  @since   5.3.2
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_get_sub_array( $array, $keys ) {

	$r = array();

	foreach ( $keys as $key ) {

		$r[ $key ] = $array[ $key ];

	}

	return $r;
}


/**
 *  Returns an array of post type names.
 *
 *  @date    7/10/13
 *  @since   5.0.0
 *
 *  @param array $args Optional. An array of key => value arguments to match against the post type objects. Default empty array.
 *  @return array A list of post type names.
 */
function acf_get_post_types( $args = array() ) {
	$post_types = array();

	// extract special arg
	$exclude   = acf_extract_var( $args, 'exclude', array() );
	$exclude[] = 'acf-field';
	$exclude[] = 'acf-field-group';
	$exclude[] = 'acf-post-type';
	$exclude[] = 'acf-taxonomy';
	$exclude[] = 'acf-ui-options-page';

	// Get post type objects.
	$objects = get_post_types( $args, 'objects' );

	foreach ( $objects as $i => $object ) {
		// Bail early if is exclude.
		if ( in_array( $i, $exclude ) ) {
			continue;
		}

		// Bail early if is builtin (WP) private post type
		// i.e. nav_menu_item, revision, customize_changeset, etc.
		if ( $object->_builtin && ! $object->public ) {
			continue;
		}

		$post_types[] = $i;
	}

	return apply_filters( 'acf/get_post_types', $post_types, $args );
}

function acf_get_pretty_post_types( $post_types = array() ) {

	// get post types
	if ( empty( $post_types ) ) {

		// get all custom post types
		$post_types = acf_get_post_types();

	}

	// get labels
	$ref = array();
	$r   = array();

	foreach ( $post_types as $post_type ) {

		// vars
		$label = acf_get_post_type_label( $post_type );

		// append to r
		$r[ $post_type ] = $label;

		// increase counter
		if ( ! isset( $ref[ $label ] ) ) {

			$ref[ $label ] = 0;

		}

		++$ref[ $label ];
	}

	// get slugs
	foreach ( array_keys( $r ) as $i ) {

		// vars
		$post_type = $r[ $i ];

		if ( $ref[ $post_type ] > 1 ) {

			$r[ $i ] .= ' (' . $i . ')';

		}
	}

	// return
	return $r;
}

/**
 *  Function acf_get_post_stati()
 *
 *  Returns an array of post status names.
 *
 *  @date    01/24/23
 *  @since   6.1.0
 *
 *  @param   array $args Optional. An array of key => value arguments to match against the post status objects. Default empty array.
 *  @return  array A list of post status names.
 */
function acf_get_post_stati( $args = array() ) {

	$args['internal'] = false;

	$post_statuses = get_post_stati( $args );

	unset( $post_statuses['acf-disabled'] );

	$post_statuses = (array) apply_filters( 'acf/get_post_stati', $post_statuses, $args );

	return $post_statuses;
}
/**
 *  Function acf_get_pretty_post_statuses()
 *
 *  Returns a clean array of post status names.
 *
 *  @date    02/16/23
 *  @since   6.1.0
 *
 *  @param   array $post_statuses Optional. An array of post status objects. Default empty array.
 *  @return  array An array of post status names.
 */
function acf_get_pretty_post_statuses( $post_statuses = array() ) {

	// Get all post statuses.
	$post_statuses = array_merge( $post_statuses, acf_get_post_stati() );

	$ref    = array();
	$result = array();

	foreach ( $post_statuses as $post_status ) {
		$label = acf_get_post_status_label( $post_status );

		$result[ $post_status ] = $label;

		if ( ! isset( $ref[ $label ] ) ) {
			$ref[ $label ] = 0;
		}

		++$ref[ $label ];
	}

	foreach ( array_keys( $result ) as $i ) {
		$post_status = $result[ $i ];

		if ( $ref[ $post_status ] > 1 ) {
			$result[ $i ] .= ' (' . $i . ')';
		}
	}

	return $result;
}

/*
*  acf_get_post_type_label
*
*  This function will return a pretty label for a specific post_type
*
*  @type    function
*  @date    5/07/2016
*  @since   5.4.0
*
*  @param   $post_type (string)
*  @return  (string)
*/

function acf_get_post_type_label( $post_type ) {

	// vars
	$label = $post_type;

	// check that object exists
	// - case exists when importing field group from another install and post type does not exist
	if ( post_type_exists( $post_type ) ) {

		$obj   = get_post_type_object( $post_type );
		$label = $obj->labels->singular_name;

	}

	// return
	return $label;
}


/**
 *  Function acf_get_post_status_label()
 *
 *  This function will return a pretty label for a specific post_status
 *
 *  @type    function
 *  @date    01/24/2023
 *  @since   6.1.0
 *
 *  @param   string $post_status The post status.
 *  @return  string The post status label.
 */
function acf_get_post_status_label( $post_status ) {
	$label = $post_status;
	$obj   = get_post_status_object( $post_status );
	$label = is_object( $obj ) ? $obj->label : '';

	return $label;
}


/*
*  acf_verify_nonce
*
*  This function will look at the $_POST['_acf_nonce'] value and return true or false
*
*  @type    function
*  @date    15/10/13
*  @since   5.0.0
*
*  @param   $nonce (string)
*  @return  (boolean)
*/

function acf_verify_nonce( $value ) {

	// vars
	$nonce = acf_maybe_get_POST( '_acf_nonce' );

	// bail early nonce does not match (post|user|comment|term)
	if ( ! $nonce || ! wp_verify_nonce( $nonce, $value ) ) {
		return false;
	}

	// reset nonce (only allow 1 save)
	$_POST['_acf_nonce'] = false;

	// return
	return true;
}


/*
*  acf_verify_ajax
*
*  This function will return true if the current AJAX request is valid
*  It's action will also allow WPML to set the lang and avoid AJAX get_posts issues
*
*  @type    function
*  @date    7/08/2015
*  @since   5.2.3
*
*  @param   n/a
*  @return  (boolean)
*/

function acf_verify_ajax() {

	// bail early if not acf nonce
	if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['nonce'] ), 'acf_nonce' ) ) {
		return false;
	}

	// action for 3rd party customization
	do_action( 'acf/verify_ajax' );

	// return
	return true;
}


/*
*  acf_get_image_sizes
*
*  This function will return an array of available image sizes
*
*  @type    function
*  @date    23/10/13
*  @since   5.0.0
*
*  @param   n/a
*  @return  (array)
*/

function acf_get_image_sizes() {

	// vars
	$sizes = array(
		'thumbnail' => __( 'Thumbnail', 'acf' ),
		'medium'    => __( 'Medium', 'acf' ),
		'large'     => __( 'Large', 'acf' ),
	);

	// find all sizes
	$all_sizes = get_intermediate_image_sizes();

	// add extra registered sizes
	if ( ! empty( $all_sizes ) ) {

		foreach ( $all_sizes as $size ) {

			// bail early if already in array
			if ( isset( $sizes[ $size ] ) ) {

				continue;

			}

			// append to array
			$label          = str_replace( '-', ' ', $size );
			$label          = ucwords( $label );
			$sizes[ $size ] = $label;

		}
	}

	// add sizes
	foreach ( array_keys( $sizes ) as $s ) {

		// vars
		$data = acf_get_image_size( $s );

		// append
		if ( $data['width'] && $data['height'] ) {

			$sizes[ $s ] .= ' (' . $data['width'] . ' x ' . $data['height'] . ')';

		}
	}

	// add full end
	$sizes['full'] = __( 'Full Size', 'acf' );

	// filter for 3rd party customization
	$sizes = apply_filters( 'acf/get_image_sizes', $sizes );

	// return
	return $sizes;
}

function acf_get_image_size( $s = '' ) {

	// global
	global $_wp_additional_image_sizes;

	// rename for nicer code
	$_sizes = $_wp_additional_image_sizes;

	// vars
	$data = array(
		'width'  => isset( $_sizes[ $s ]['width'] ) ? $_sizes[ $s ]['width'] : get_option( "{$s}_size_w" ),
		'height' => isset( $_sizes[ $s ]['height'] ) ? $_sizes[ $s ]['height'] : get_option( "{$s}_size_h" ),
	);

	// return
	return $data;
}

/**
 * acf_version_compare
 *
 * Similar to the version_compare() function but with extra functionality.
 *
 * @date    21/11/16
 * @since   5.5.0
 *
 * @param   string $left The left version number.
 * @param   string $compare The compare operator.
 * @param   string $right The right version number.
 * @return  bool
 */
function acf_version_compare( $left = '', $compare = '>', $right = '' ) {

	// Detect 'wp' placeholder.
	if ( $left === 'wp' ) {
		global $wp_version;
		$left = $wp_version;
	}

	// Return result.
	return version_compare( $left, $right, $compare );
}


/*
*  acf_get_full_version
*
*  This function will remove any '-beta1' or '-RC1' strings from a version
*
*  @type    function
*  @date    24/11/16
*  @since   5.5.0
*
*  @param   $version (string)
*  @return  (string)
*/

function acf_get_full_version( $version = '1' ) {

	// remove '-beta1' or '-RC1'
	if ( $pos = strpos( $version, '-' ) ) {

		$version = substr( $version, 0, $pos );

	}

	// return
	return $version;
}


/*
*  acf_get_terms
*
*  This function is a wrapper for the get_terms() function
*
*  @type    function
*  @date    28/09/2016
*  @since   5.4.0
*
*  @param   $args (array)
*  @return  (array)
*/

function acf_get_terms( $args ) {

	// defaults
	$args = wp_parse_args(
		$args,
		array(
			'taxonomy'               => null,
			'hide_empty'             => false,
			'update_term_meta_cache' => false,
		)
	);

	// return
	return get_terms( $args );
}


/*
*  acf_get_taxonomy_terms
*
*  This function will return an array of available taxonomy terms
*
*  @type    function
*  @date    7/10/13
*  @since   5.0.0
*
*  @param   $taxonomies (array)
*  @return  (array)
*/

function acf_get_taxonomy_terms( $taxonomies = array() ) {

	// force array
	$taxonomies = acf_get_array( $taxonomies );

	// get pretty taxonomy names
	$taxonomies = acf_get_pretty_taxonomies( $taxonomies );

	// vars
	$r = array();

	// populate $r
	foreach ( array_keys( $taxonomies ) as $taxonomy ) {

		// vars
		$label           = $taxonomies[ $taxonomy ];
		$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
		$terms           = acf_get_terms(
			array(
				'taxonomy'   => $taxonomy,
				'hide_empty' => false,
			)
		);

		// bail early i no terms
		if ( empty( $terms ) ) {
			continue;
		}

		// sort into hierachial order!
		if ( $is_hierarchical ) {

			$terms = _get_term_children( 0, $terms, $taxonomy );

		}

		// add placeholder
		$r[ $label ] = array();

		// add choices
		foreach ( $terms as $term ) {

			$k                 = "{$taxonomy}:{$term->slug}";
			$r[ $label ][ $k ] = acf_get_term_title( $term );

		}
	}

	// return
	return $r;
}


/*
*  acf_decode_taxonomy_terms
*
*  This function decodes the $taxonomy:$term strings into a nested array
*
*  @type    function
*  @date    27/02/2014
*  @since   5.0.0
*
*  @param   $terms (array)
*  @return  (array)
*/

function acf_decode_taxonomy_terms( $strings = false ) {

	// bail early if no terms
	if ( empty( $strings ) ) {
		return false;
	}

	// vars
	$terms = array();

	// loop
	foreach ( $strings as $string ) {

		// vars
		$data     = acf_decode_taxonomy_term( $string );
		$taxonomy = $data['taxonomy'];
		$term     = $data['term'];

		// create empty array
		if ( ! isset( $terms[ $taxonomy ] ) ) {

			$terms[ $taxonomy ] = array();

		}

		// append
		$terms[ $taxonomy ][] = $term;

	}

	// return
	return $terms;
}


/*
*  acf_decode_taxonomy_term
*
*  This function will return the taxonomy and term slug for a given value
*
*  @type    function
*  @date    31/03/2014
*  @since   5.0.0
*
*  @param   $string (string)
*  @return  (array)
*/

function acf_decode_taxonomy_term( $value ) {

	// vars
	$data = array(
		'taxonomy' => '',
		'term'     => '',
	);

	// int
	if ( is_numeric( $value ) ) {

		$data['term'] = $value;

		// string
	} elseif ( is_string( $value ) ) {

		$value            = explode( ':', $value );
		$data['taxonomy'] = isset( $value[0] ) ? $value[0] : '';
		$data['term']     = isset( $value[1] ) ? $value[1] : '';

		// error
	} else {

		return false;

	}

	// allow for term_id (Used by ACF v4)
	if ( is_numeric( $data['term'] ) ) {

		// global
		global $wpdb;

		// find taxonomy
		if ( ! $data['taxonomy'] ) {

			$data['taxonomy'] = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $data['term'] ) );

		}

		// find term (may have numeric slug '123')
		$term = get_term_by( 'slug', $data['term'], $data['taxonomy'] );

		// attempt get term via ID (ACF4 uses ID)
		if ( ! $term ) {
			$term = get_term( $data['term'], $data['taxonomy'] );
		}

		// bail early if no term
		if ( ! $term ) {
			return false;
		}

		// update
		$data['taxonomy'] = $term->taxonomy;
		$data['term']     = $term->slug;

	}

	// return
	return $data;
}

/**
 * acf_array
 *
 * Casts the value into an array.
 *
 * @date    9/1/19
 * @since   5.7.10
 *
 * @param   mixed $val The value to cast.
 * @return  array
 */
function acf_array( $val = array() ) {
	return (array) $val;
}

/**
 * Returns a non-array value.
 *
 * @date    11/05/2020
 * @since   5.8.10
 *
 * @param   mixed $val The value to review.
 * @return  mixed
 */
function acf_unarray( $val ) {
	if ( is_array( $val ) ) {
		return reset( $val );
	}
	return $val;
}

/*
*  acf_get_array
*
*  This function will force a variable to become an array
*
*  @type    function
*  @date    4/02/2014
*  @since   5.0.0
*
*  @param   $var (mixed)
*  @return  (array)
*/

function acf_get_array( $var = false, $delimiter = '' ) {

	// array
	if ( is_array( $var ) ) {
		return $var;
	}

	// bail early if empty
	if ( acf_is_empty( $var ) ) {
		return array();
	}

	// string
	if ( is_string( $var ) && $delimiter ) {
		return explode( $delimiter, $var );
	}

	// place in array
	return (array) $var;
}


/*
*  acf_get_numeric
*
*  This function will return numeric values
*
*  @type    function
*  @date    15/07/2016
*  @since   5.4.0
*
*  @param   $value (mixed)
*  @return  (mixed)
*/

function acf_get_numeric( $value = '' ) {

	// vars
	$numbers  = array();
	$is_array = is_array( $value );

	// loop
	foreach ( (array) $value as $v ) {

		if ( is_numeric( $v ) ) {
			$numbers[] = (int) $v;
		}
	}

	// bail early if is empty
	if ( empty( $numbers ) ) {
		return false;
	}

	// convert array
	if ( ! $is_array ) {
		$numbers = $numbers[0];
	}

	// return
	return $numbers;
}


/**
 * acf_get_posts
 *
 * Similar to the get_posts() function but with extra functionality.
 *
 * @date    3/03/15
 * @since   5.1.5
 *
 * @param   array $args The query args.
 * @return  array
 */
function acf_get_posts( $args = array() ) {

	// Vars.
	$posts = array();

	// Apply default args.
	$args = wp_parse_args(
		$args,
		array(
			'posts_per_page'         => -1,
			'post_type'              => '',
			'post_status'            => 'any',
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		)
	);

	// Avoid default 'post' post_type by providing all public types.
	if ( ! $args['post_type'] ) {
		$args['post_type'] = acf_get_post_types();
	}

	if ( ! $args['post_status'] ) {
		$args['post_status'] = acf_get_post_stati();
	}

	// Check if specifc post ID's have been provided.
	if ( $args['post__in'] ) {

		// Clean value into an array of IDs.
		$args['post__in'] = array_map( 'intval', acf_array( $args['post__in'] ) );
	}

	/**
	 * Filters the args used in `acf_get_posts()` that are passed to `get_posts()`.
	 *
	 * @since 6.1.7
	 *
	 * @param array $args The args passed to `get_posts()`.
	 */
	$args = apply_filters( 'acf/acf_get_posts/args', $args );

	// Query posts.
	$posts = get_posts( $args );

	// Remove any potential empty results.
	$posts = array_filter( $posts );

	// Manually order results.
	if ( $posts && $args['post__in'] ) {
		$order = array();
		foreach ( $posts as $i => $post ) {
			$order[ $i ] = array_search( $post->ID, $args['post__in'] );
		}
		array_multisort( $order, $posts );
	}

	/**
	 * Filters the results found in the `acf_get_posts()` function.
	 *
	 * @since 6.1.7
	 *
	 * @param array $posts The results from the `get_posts()` call.
	 */
	return apply_filters( 'acf/acf_get_posts/results', $posts );
}


/*
*  _acf_query_remove_post_type
*
*  This function will remove the 'wp_posts.post_type' WHERE clause completely
*  When using 'post__in', this clause is unneccessary and slow.
*
*  @type    function
*  @date    4/03/2015
*  @since   5.1.5
*
*  @param   $sql (string)
*  @return  $sql
*/

function _acf_query_remove_post_type( $sql ) {

	// global
	global $wpdb;

	// bail early if no 'wp_posts.ID IN'
	if ( strpos( $sql, "$wpdb->posts.ID IN" ) === false ) {

		return $sql;

	}

	// get bits
	$glue = 'AND';
	$bits = explode( $glue, $sql );

	// loop through $where and remove any post_type queries
	foreach ( $bits as $i => $bit ) {

		if ( strpos( $bit, "$wpdb->posts.post_type" ) !== false ) {

			unset( $bits[ $i ] );

		}
	}

	// join $where back together
	$sql = implode( $glue, $bits );

	// return
	return $sql;
}


/*
*  acf_get_grouped_posts
*
*  This function will return all posts grouped by post_type
*  This is handy for select settings
*
*  @type    function
*  @date    27/02/2014
*  @since   5.0.0
*
*  @param   $args (array)
*  @return  (array)
*/

function acf_get_grouped_posts( $args ) {

	// vars
	$data = array();

	// defaults
	$args = wp_parse_args(
		$args,
		array(
			'posts_per_page'         => -1,
			'paged'                  => 0,
			'post_type'              => 'post',
			'orderby'                => 'menu_order title',
			'order'                  => 'ASC',
			'post_status'            => 'any',
			'suppress_filters'       => false,
			'update_post_meta_cache' => false,
		)
	);

	// find array of post_type
	$post_types          = acf_get_array( $args['post_type'] );
	$post_types_labels   = acf_get_pretty_post_types( $post_types );
	$is_single_post_type = ( count( $post_types ) == 1 );

	// attachment doesn't work if it is the only item in an array
	if ( $is_single_post_type ) {
		$args['post_type'] = reset( $post_types );
	}

	// add filter to orderby post type
	if ( ! $is_single_post_type ) {
		add_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
	}

	// get posts
	$posts = get_posts( $args );

	// remove this filter (only once)
	if ( ! $is_single_post_type ) {
		remove_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
	}

	// loop
	foreach ( $post_types as $post_type ) {

		// vars
		$this_posts = array();
		$this_group = array();

		// populate $this_posts
		foreach ( $posts as $post ) {
			if ( $post->post_type == $post_type ) {
				$this_posts[] = $post;
			}
		}

		// bail early if no posts for this post type
		if ( empty( $this_posts ) ) {
			continue;
		}

		// sort into hierachial order!
		// this will fail if a search has taken place because parents wont exist
		if ( is_post_type_hierarchical( $post_type ) && empty( $args['s'] ) ) {

			// vars
			$post_id   = $this_posts[0]->ID;
			$parent_id = acf_maybe_get( $args, 'post_parent', 0 );
			$offset    = 0;
			$length    = count( $this_posts );

			// get all posts from this post type
			$all_posts = get_posts(
				array_merge(
					$args,
					array(
						'posts_per_page' => -1,
						'paged'          => 0,
						'post_type'      => $post_type,
					)
				)
			);

			// find starting point (offset)
			foreach ( $all_posts as $i => $post ) {
				if ( $post->ID == $post_id ) {
					$offset = $i;
					break;
				}
			}

			// order posts
			$ordered_posts = get_page_children( $parent_id, $all_posts );

			// compare aray lengths
			// if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
			// this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
			if ( count( $ordered_posts ) == count( $all_posts ) ) {
				$this_posts = array_slice( $ordered_posts, $offset, $length );
			}
		}

		// populate $this_posts
		foreach ( $this_posts as $post ) {
			$this_group[ $post->ID ] = $post;
		}

		// group by post type
		$label          = $post_types_labels[ $post_type ];
		$data[ $label ] = $this_group;

	}

	// return
	return $data;
}


function _acf_orderby_post_type( $ordeby, $wp_query ) {

	// global
	global $wpdb;

	// get post types
	$post_types = $wp_query->get( 'post_type' );

	// prepend SQL
	if ( is_array( $post_types ) ) {

		$post_types = implode( "','", $post_types );
		$ordeby     = "FIELD({$wpdb->posts}.post_type,'$post_types')," . $ordeby;

	}

	// return
	return $ordeby;
}


function acf_get_post_title( $post = 0, $is_search = false ) {

	// vars
	$post    = get_post( $post );
	$title   = '';
	$prepend = '';
	$append  = '';

	// bail early if no post
	if ( ! $post ) {
		return '';
	}

	// title
	$title = get_the_title( $post->ID );

	// empty
	if ( $title === '' ) {

		$title = __( '(no title)', 'acf' );

	}

	// status
	if ( get_post_status( $post->ID ) != 'publish' ) {

		$append .= ' (' . get_post_status( $post->ID ) . ')';

	}

	// ancestors
	if ( $post->post_type !== 'attachment' ) {

		// get ancestors
		$ancestors = get_ancestors( $post->ID, $post->post_type );
		$prepend  .= str_repeat( '- ', count( $ancestors ) );

		// add parent
		/*
		removed in 5.6.5 as not used by the UI
		if( $is_search && !empty($ancestors) ) {

			// reverse
			$ancestors = array_reverse($ancestors);


			// convert id's into titles
			foreach( $ancestors as $i => $id ) {

				$ancestors[ $i ] = get_the_title( $id );

			}


			// append
			$append .= ' | ' . __('Parent', 'acf') . ': ' . implode(' / ', $ancestors);

		}
		*/

	}

	// merge
	$title = $prepend . $title . $append;

	// return
	return $title;
}


function acf_order_by_search( $array, $search ) {

	// vars
	$weights = array();
	$needle  = strtolower( $search );

	// add key prefix
	foreach ( array_keys( $array ) as $k ) {

		$array[ '_' . $k ] = acf_extract_var( $array, $k );

	}

	// add search weight
	foreach ( $array as $k => $v ) {

		// vars
		$weight   = 0;
		$haystack = strtolower( $v );
		$strpos   = strpos( $haystack, $needle );

		// detect search match
		if ( $strpos !== false ) {

			// set eright to length of match
			$weight = strlen( $search );

			// increase weight if match starts at begining of string
			if ( $strpos == 0 ) {

				++$weight;

			}
		}

		// append to wights
		$weights[ $k ] = $weight;

	}

	// sort the array with menu_order ascending
	array_multisort( $weights, SORT_DESC, $array );

	// remove key prefix
	foreach ( array_keys( $array ) as $k ) {

		$array[ substr( $k, 1 ) ] = acf_extract_var( $array, $k );

	}

	// return
	return $array;
}


/*
*  acf_get_pretty_user_roles
*
*  description
*
*  @type    function
*  @date    23/02/2016
*  @since   5.3.2
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_get_pretty_user_roles( $allowed = false ) {

	// vars
	$editable_roles = get_editable_roles();
	$allowed        = acf_get_array( $allowed );
	$roles          = array();

	// loop
	foreach ( $editable_roles as $role_name => $role_details ) {

		// bail early if not allowed
		if ( ! empty( $allowed ) && ! in_array( $role_name, $allowed ) ) {
			continue;
		}

		// append
		$roles[ $role_name ] = translate_user_role( $role_details['name'] );

	}

	// return
	return $roles;
}


/*
*  acf_get_grouped_users
*
*  This function will return all users grouped by role
*  This is handy for select settings
*
*  @type    function
*  @date    27/02/2014
*  @since   5.0.0
*
*  @param   $args (array)
*  @return  (array)
*/

function acf_get_grouped_users( $args = array() ) {

	// vars
	$r = array();

	// defaults
	$args = wp_parse_args(
		$args,
		array(
			'users_per_page' => -1,
			'paged'          => 0,
			'role'           => '',
			'orderby'        => 'login',
			'order'          => 'ASC',
		)
	);

	// offset
	$i              = 0;
	$min            = 0;
	$max            = 0;
	$users_per_page = acf_extract_var( $args, 'users_per_page' );
	$paged          = acf_extract_var( $args, 'paged' );

	if ( $users_per_page > 0 ) {

		// prevent paged from being -1
		$paged = max( 0, $paged );

		// set min / max
		$min = ( ( $paged - 1 ) * $users_per_page ) + 1; // 1,  11
		$max = ( $paged * $users_per_page ); // 10, 20

	}

	// find array of post_type
	$user_roles = acf_get_pretty_user_roles( $args['role'] );

	// fix role
	if ( is_array( $args['role'] ) ) {

		// global
		global $wp_version, $wpdb;

		// vars
		$roles = acf_extract_var( $args, 'role' );

		// new WP has role__in
		if ( version_compare( $wp_version, '4.4', '>=' ) ) {

			$args['role__in'] = $roles;

			// old WP doesn't have role__in
		} else {

			// vars
			$blog_id    = get_current_blog_id();
			$meta_query = array( 'relation' => 'OR' );

			// loop
			foreach ( $roles as $role ) {

				$meta_query[] = array(
					'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
					'value'   => '"' . $role . '"',
					'compare' => 'LIKE',
				);

			}

			// append
			$args['meta_query'] = $meta_query;

		}
	}

	// get posts
	$users = get_users( $args );

	// loop
	foreach ( $user_roles as $user_role_name => $user_role_label ) {

		// vars
		$this_users = array();
		$this_group = array();

		// populate $this_posts
		foreach ( array_keys( $users ) as $key ) {

			// bail early if not correct role
			if ( ! in_array( $user_role_name, $users[ $key ]->roles ) ) {
				continue;
			}

			// extract user
			$user = acf_extract_var( $users, $key );

			// increase
			++$i;

			// bail early if too low
			if ( $min && $i < $min ) {
				continue;
			}

			// bail early if too high (don't bother looking at any more users)
			if ( $max && $i > $max ) {
				break;
			}

			// group by post type
			$this_users[ $user->ID ] = $user;

		}

		// bail early if no posts for this post type
		if ( empty( $this_users ) ) {
			continue;
		}

		// append
		$r[ $user_role_label ] = $this_users;

	}

	// return
	return $r;
}

/**
 * acf_json_encode
 *
 * Returns json_encode() ready for file / database use.
 *
 * @date    29/4/19
 * @since   5.0.0
 *
 * @param   array $json The array of data to encode.
 * @return  string
 */
function acf_json_encode( $json ) {
	return json_encode( $json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
}


/*
*  acf_str_exists
*
*  This function will return true if a sub string is found
*
*  @type    function
*  @date    1/05/2014
*  @since   5.0.0
*
*  @param   $needle (string)
*  @param   $haystack (string)
*  @return  (boolean)
*/

function acf_str_exists( $needle, $haystack ) {

	// return true if $haystack contains the $needle
	if ( is_string( $haystack ) && strpos( $haystack, $needle ) !== false ) {

		return true;

	}

	// return
	return false;
}


/*
*  acf_debug
*
*  description
*
*  @type    function
*  @date    2/05/2014
*  @since   5.0.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_debug() {

	// vars
	$args = func_get_args();
	$s    = array_shift( $args );
	$o    = '';
	$nl   = "\r\n";

	// start script
	$o .= '<script type="text/javascript">' . $nl;

	$o .= 'console.log("' . $s . '"';

	if ( ! empty( $args ) ) {

		foreach ( $args as $arg ) {

			if ( is_object( $arg ) || is_array( $arg ) ) {

				$arg = json_encode( $arg );

			} elseif ( is_bool( $arg ) ) {

				$arg = $arg ? 'true' : 'false';

			} elseif ( is_string( $arg ) ) {

				$arg = '"' . $arg . '"';

			}

			$o .= ', ' . $arg;

		}
	}

	$o .= ');' . $nl;

	// end script
	$o .= '</script>' . $nl;

	// echo
	echo $o;
}

function acf_debug_start() {

	acf_update_setting( 'debug_start', memory_get_usage() );
}

function acf_debug_end() {

	$start = acf_get_setting( 'debug_start' );
	$end   = memory_get_usage();

	return $end - $start;
}


/*
*  acf_encode_choices
*
*  description
*
*  @type    function
*  @date    4/06/2014
*  @since   5.0.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_encode_choices( $array = array(), $show_keys = true ) {

	// bail early if not array (maybe a single string)
	if ( ! is_array( $array ) ) {
		return $array;
	}

	// bail early if empty array
	if ( empty( $array ) ) {
		return '';
	}

	// vars
	$string = '';

	// if allowed to show keys (good for choices, not for default values)
	if ( $show_keys ) {

		// loop
		foreach ( $array as $k => $v ) {

			// ignore if key and value are the same
			if ( strval( $k ) == strval( $v ) ) {
				continue;
			}

			// show key in the value
			$array[ $k ] = $k . ' : ' . $v;

		}
	}

	// implode
	$string = implode( "\n", $array );

	// return
	return $string;
}

function acf_decode_choices( $string = '', $array_keys = false ) {

	// bail early if already array
	if ( is_array( $string ) ) {

		return $string;

		// allow numeric values (same as string)
	} elseif ( is_numeric( $string ) ) {

		// do nothing

		// bail early if not a string
	} elseif ( ! is_string( $string ) ) {

		return array();

		// bail early if is empty string
	} elseif ( $string === '' ) {

		return array();

	}

	// vars
	$array = array();

	// explode
	$lines = explode( "\n", $string );

	// key => value
	foreach ( $lines as $line ) {

		// vars
		$k = trim( $line );
		$v = trim( $line );

		// look for ' : '
		if ( acf_str_exists( ' : ', $line ) ) {

			$line = explode( ' : ', $line );

			$k = trim( $line[0] );
			$v = trim( $line[1] );

		}

		// append
		$array[ $k ] = $v;

	}

	// return only array keys? (good for checkbox default_value)
	if ( $array_keys ) {

		return array_keys( $array );

	}

	// return
	return $array;
}


/*
*  acf_str_replace
*
*  This function will replace an array of strings much like str_replace
*  The difference is the extra logic to avoid replacing a string that has alread been replaced
*  This is very useful for replacing date characters as they overlap with eachother
*
*  @type    function
*  @date    21/06/2016
*  @since   5.3.8
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_str_replace( $string = '', $search_replace = array() ) {

	// vars
	$ignore = array();

	// remove potential empty search to avoid PHP error
	unset( $search_replace[''] );

	// loop over conversions
	foreach ( $search_replace as $search => $replace ) {

		// ignore this search, it was a previous replace
		if ( in_array( $search, $ignore ) ) {
			continue;
		}

		// bail early if subsctring not found
		if ( strpos( $string, $search ) === false ) {
			continue;
		}

		// replace
		$string = str_replace( $search, $replace, $string );

		// append to ignore
		$ignore[] = $replace;

	}

	// return
	return $string;
}


/*
*  date & time formats
*
*  These settings contain an association of format strings from PHP => JS
*
*  @type    function
*  @date    21/06/2016
*  @since   5.3.8
*
*  @param   n/a
*  @return  n/a
*/

acf_update_setting(
	'php_to_js_date_formats',
	array(

		// Year
		'Y' => 'yy',    // Numeric, 4 digits                                1999, 2003
		'y' => 'y',     // Numeric, 2 digits                                99, 03


	// Month
		'm' => 'mm',    // Numeric, with leading zeros                      01–12
		'n' => 'm',     // Numeric, without leading zeros                   1–12
		'F' => 'MM',    // Textual full                                     January – December
		'M' => 'M',     // Textual three letters                            Jan - Dec


	// Weekday
		'l' => 'DD',    // Full name  (lowercase 'L')                       Sunday – Saturday
		'D' => 'D',     // Three letter name                                Mon – Sun


	// Day of Month
		'd' => 'dd',    // Numeric, with leading zeros                      01–31
		'j' => 'd',     // Numeric, without leading zeros                   1–31
		'S' => '',      // The English suffix for the day of the month      st, nd or th in the 1st, 2nd or 15th.

	)
);

acf_update_setting(
	'php_to_js_time_formats',
	array(

		'a' => 'tt',    // Lowercase Ante meridiem and Post meridiem        am or pm
		'A' => 'TT',    // Uppercase Ante meridiem and Post meridiem        AM or PM
		'h' => 'hh',    // 12-hour format of an hour with leading zeros     01 through 12
		'g' => 'h',     // 12-hour format of an hour without leading zeros  1 through 12
		'H' => 'HH',    // 24-hour format of an hour with leading zeros     00 through 23
		'G' => 'H',     // 24-hour format of an hour without leading zeros  0 through 23
		'i' => 'mm',    // Minutes with leading zeros                       00 to 59
		's' => 'ss',    // Seconds, with leading zeros                      00 through 59

	)
);


/*
*  acf_split_date_time
*
*  This function will split a format string into seperate date and time
*
*  @type    function
*  @date    26/05/2016
*  @since   5.3.8
*
*  @param   $date_time (string)
*  @return  $formats (array)
*/

function acf_split_date_time( $date_time = '' ) {

	// vars
	$php_date = acf_get_setting( 'php_to_js_date_formats' );
	$php_time = acf_get_setting( 'php_to_js_time_formats' );
	$chars    = str_split( $date_time );
	$type     = 'date';

	// default
	$data = array(
		'date' => '',
		'time' => '',
	);

	// loop
	foreach ( $chars as $i => $c ) {

		// find type
		// - allow misc characters to append to previous type
		if ( isset( $php_date[ $c ] ) ) {

			$type = 'date';

		} elseif ( isset( $php_time[ $c ] ) ) {

			$type = 'time';

		}

		// append char
		$data[ $type ] .= $c;

	}

	// trim
	$data['date'] = trim( $data['date'] );
	$data['time'] = trim( $data['time'] );

	// return
	return $data;
}


/*
*  acf_convert_date_to_php
*
*  This fucntion converts a date format string from JS to PHP
*
*  @type    function
*  @date    20/06/2014
*  @since   5.0.0
*
*  @param   $date (string)
*  @return  (string)
*/

function acf_convert_date_to_php( $date = '' ) {

	// vars
	$php_to_js = acf_get_setting( 'php_to_js_date_formats' );
	$js_to_php = array_flip( $php_to_js );

	// return
	return acf_str_replace( $date, $js_to_php );
}

/*
*  acf_convert_date_to_js
*
*  This fucntion converts a date format string from PHP to JS
*
*  @type    function
*  @date    20/06/2014
*  @since   5.0.0
*
*  @param   $date (string)
*  @return  (string)
*/

function acf_convert_date_to_js( $date = '' ) {

	// vars
	$php_to_js = acf_get_setting( 'php_to_js_date_formats' );

	// return
	return acf_str_replace( $date, $php_to_js );
}


/*
*  acf_convert_time_to_php
*
*  This fucntion converts a time format string from JS to PHP
*
*  @type    function
*  @date    20/06/2014
*  @since   5.0.0
*
*  @param   $time (string)
*  @return  (string)
*/

function acf_convert_time_to_php( $time = '' ) {

	// vars
	$php_to_js = acf_get_setting( 'php_to_js_time_formats' );
	$js_to_php = array_flip( $php_to_js );

	// return
	return acf_str_replace( $time, $js_to_php );
}


/*
*  acf_convert_time_to_js
*
*  This fucntion converts a date format string from PHP to JS
*
*  @type    function
*  @date    20/06/2014
*  @since   5.0.0
*
*  @param   $time (string)
*  @return  (string)
*/

function acf_convert_time_to_js( $time = '' ) {

	// vars
	$php_to_js = acf_get_setting( 'php_to_js_time_formats' );

	// return
	return acf_str_replace( $time, $php_to_js );
}


/*
*  acf_update_user_setting
*
*  description
*
*  @type    function
*  @date    15/07/2014
*  @since   5.0.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_update_user_setting( $name, $value ) {

	// get current user id
	$user_id = get_current_user_id();

	// get user settings
	$settings = get_user_meta( $user_id, 'acf_user_settings', true );

	// ensure array
	$settings = acf_get_array( $settings );

	// delete setting (allow 0 to save)
	if ( acf_is_empty( $value ) ) {

		unset( $settings[ $name ] );

		// append setting
	} else {

		$settings[ $name ] = $value;

	}

	// update user data
	return update_metadata( 'user', $user_id, 'acf_user_settings', $settings );
}


/*
*  acf_get_user_setting
*
*  description
*
*  @type    function
*  @date    15/07/2014
*  @since   5.0.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_get_user_setting( $name = '', $default = false ) {

	// get current user id
	$user_id = get_current_user_id();

	// get user settings
	$settings = get_user_meta( $user_id, 'acf_user_settings', true );

	// ensure array
	$settings = acf_get_array( $settings );

	// bail arly if no settings
	if ( ! isset( $settings[ $name ] ) ) {
		return $default;
	}

	// return
	return $settings[ $name ];
}


/*
*  acf_in_array
*
*  description
*
*  @type    function
*  @date    22/07/2014
*  @since   5.0.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_in_array( $value = '', $array = false ) {

	// bail early if not array
	if ( ! is_array( $array ) ) {
		return false;
	}

	// find value in array
	return in_array( $value, $array );
}


/*
*  acf_get_valid_post_id
*
*  This function will return a valid post_id based on the current screen / parameter
*
*  @type    function
*  @date    8/12/2013
*  @since   5.0.0
*
*  @param   $post_id (mixed)
*  @return  $post_id (mixed)
*/

function acf_get_valid_post_id( $post_id = 0 ) {

	// allow filter to short-circuit load_value logic
	$preload = apply_filters( 'acf/pre_load_post_id', null, $post_id );
	if ( $preload !== null ) {
		return $preload;
	}

	// vars
	$_post_id = $post_id;

	// if not $post_id, load queried object
	if ( ! $post_id ) {

		// try for global post (needed for setup_postdata)
		$post_id = (int) get_the_ID();

		// try for current screen
		if ( ! $post_id ) {

			$post_id = get_queried_object();

		}
	}

	// $post_id may be an object.
	// todo: Compare class types instead.
	if ( is_object( $post_id ) ) {

		// post
		if ( isset( $post_id->post_type, $post_id->ID ) ) {

			$post_id = $post_id->ID;

			// user
		} elseif ( isset( $post_id->roles, $post_id->ID ) ) {

			$post_id = 'user_' . $post_id->ID;

			// term
		} elseif ( isset( $post_id->taxonomy, $post_id->term_id ) ) {

			$post_id = 'term_' . $post_id->term_id;

			// comment
		} elseif ( isset( $post_id->comment_ID ) ) {

			$post_id = 'comment_' . $post_id->comment_ID;

			// default
		} else {

			$post_id = 0;

		}
	}

	// allow for option == options
	if ( $post_id === 'option' ) {

		$post_id = 'options';

	}

	// append language code
	if ( $post_id == 'options' ) {

		$dl = acf_get_setting( 'default_language' );
		$cl = acf_get_setting( 'current_language' );

		if ( $cl && $cl !== $dl ) {

			$post_id .= '_' . $cl;

		}
	}

	// filter for 3rd party
	$post_id = apply_filters( 'acf/validate_post_id', $post_id, $_post_id );

	// return
	return $post_id;
}



/*
*  acf_get_post_id_info
*
*  This function will return the type and id for a given $post_id string
*
*  @type    function
*  @date    2/07/2016
*  @since   5.4.0
*
*  @param   $post_id (mixed)
*  @return  $info (array)
*/

function acf_get_post_id_info( $post_id = 0 ) {

	// vars
	$info = array(
		'type' => 'post',
		'id'   => 0,
	);

	// bail early if no $post_id
	if ( ! $post_id ) {
		return $info;
	}

	// check cache
	// - this function will most likely be called multiple times (saving loading fields from post)
	// $cache_key = "get_post_id_info/post_id={$post_id}";

	// if( acf_isset_cache($cache_key) ) return acf_get_cache($cache_key);

	// numeric
	if ( is_numeric( $post_id ) ) {

		$info['id'] = (int) $post_id;

		// string
	} elseif ( is_string( $post_id ) ) {

		// vars
		$glue = '_';
		$type = explode( $glue, $post_id );
		$id   = array_pop( $type );
		$type = implode( $glue, $type );
		$meta = array( 'post', 'user', 'comment', 'term' );

		// check if is taxonomy (ACF < 5.5)
		// - avoid scenario where taxonomy exists with name of meta type
		if ( ! in_array( $type, $meta ) && acf_isset_termmeta( $type ) ) {
			$type = 'term';
		}

		// meta
		if ( is_numeric( $id ) && in_array( $type, $meta ) ) {

			$info['type'] = $type;
			$info['id']   = (int) $id;

			// option
		} else {

			$info['type'] = 'option';
			$info['id']   = $post_id;

		}
	}

	// update cache
	// acf_set_cache($cache_key, $info);

	// filter
	$info = apply_filters( 'acf/get_post_id_info', $info, $post_id );

	// return
	return $info;
}


/*
acf_log( acf_get_post_id_info(4) );

acf_log( acf_get_post_id_info('post_4') );

acf_log( acf_get_post_id_info('user_123') );

acf_log( acf_get_post_id_info('term_567') );

acf_log( acf_get_post_id_info('category_204') );

acf_log( acf_get_post_id_info('comment_6') );

acf_log( acf_get_post_id_info('options_lol!') );

acf_log( acf_get_post_id_info('option') );

acf_log( acf_get_post_id_info('options') );

*/


/*
*  acf_isset_termmeta
*
*  This function will return true if the termmeta table exists
*  https://developer.wordpress.org/reference/functions/get_term_meta/
*
*  @type    function
*  @date    3/09/2016
*  @since   5.4.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_isset_termmeta( $taxonomy = '' ) {

	// bail early if no table
	if ( get_option( 'db_version' ) < 34370 ) {
		return false;
	}

	// check taxonomy
	if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
		return false;
	}

	// return
	return true;
}

/**
 * This function will walk through the $_FILES data and upload each found.
 *
 * @date    25/10/2014
 * @since   5.0.9
 *
 * @param array $ancestors An internal parameter, not required.
 * @return void
 */
function acf_upload_files( $ancestors = array() ) {

	$file = acf_sanitize_files_array( $_FILES['acf'] );

	// walk through ancestors
	if ( ! empty( $ancestors ) ) {

		foreach ( $ancestors as $a ) {

			foreach ( array_keys( $file ) as $k ) {

				$file[ $k ] = $file[ $k ][ $a ];

			}
		}
	}

	// is array?
	if ( is_array( $file['name'] ) ) {

		foreach ( array_keys( $file['name'] ) as $k ) {

			$_ancestors = array_merge( $ancestors, array( $k ) );

			acf_upload_files( $_ancestors );

		}

		return;

	}

	// Bail early if file has error (no file uploaded).
	if ( $file['error'] ) {
		return;
	}

	$field_key  = end( $ancestors );
	$nonce_name = $field_key . '_file_nonce';

	if ( empty( $_REQUEST['acf'][ $nonce_name ] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['acf'][ $nonce_name ] ), 'acf/file_uploader_nonce/' . $field_key ) ) {
		return;
	}

	// Assign global _acfuploader for media validation.
	$_POST['_acfuploader'] = $field_key;

	// file found!
	$attachment_id = acf_upload_file( $file );

	// update $_POST
	array_unshift( $ancestors, 'acf' );
	acf_update_nested_array( $_POST, $ancestors, $attachment_id );
}

/*
*  acf_upload_file
*
*  This function will uploade a $_FILE
*
*  @type    function
*  @date    27/10/2014
*  @since   5.0.9
*
*  @param   $uploaded_file (array) array found from $_FILE data
*  @return  $id (int) new attachment ID
*/

function acf_upload_file( $uploaded_file ) {

	// required
	// require_once( ABSPATH . "/wp-load.php" ); // WP should already be loaded
	require_once ABSPATH . '/wp-admin/includes/media.php'; // video functions
	require_once ABSPATH . '/wp-admin/includes/file.php';
	require_once ABSPATH . '/wp-admin/includes/image.php';

	// required for wp_handle_upload() to upload the file
	$upload_overrides = array( 'test_form' => false );

	// upload
	$file = wp_handle_upload( $uploaded_file, $upload_overrides );

	// bail early if upload failed
	if ( isset( $file['error'] ) ) {

		return $file['error'];

	}

	// vars
	$url      = $file['url'];
	$type     = $file['type'];
	$file     = $file['file'];
	$filename = basename( $file );

	// Construct the object array
	$object = array(
		'post_title'     => $filename,
		'post_mime_type' => $type,
		'guid'           => $url,
	);

	// Save the data
	$id = wp_insert_attachment( $object, $file );

	// Add the meta-data
	wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

	/** This action is documented in wp-admin/custom-header.php */
	do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication

	// return new ID
	return $id;
}


/*
*  acf_update_nested_array
*
*  This function will update a nested array value. Useful for modifying the $_POST array
*
*  @type    function
*  @date    27/10/2014
*  @since   5.0.9
*
*  @param   $array (array) target array to be updated
*  @param   $ancestors (array) array of keys to navigate through to find the child
*  @param   $value (mixed) The new value
*  @return  (boolean)
*/

function acf_update_nested_array( &$array, $ancestors, $value ) {

	// if no more ancestors, update the current var
	if ( empty( $ancestors ) ) {

		$array = $value;

		// return
		return true;

	}

	// shift the next ancestor from the array
	$k = array_shift( $ancestors );

	// if exists
	if ( isset( $array[ $k ] ) ) {

		return acf_update_nested_array( $array[ $k ], $ancestors, $value );

	}

	// return
	return false;
}


/*
*  acf_is_screen
*
*  This function will return true if all args are matched for the current screen
*
*  @type    function
*  @date    9/12/2014
*  @since   5.1.5
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_is_screen( $id = '' ) {

	// bail early if not defined
	if ( ! function_exists( 'get_current_screen' ) ) {
		return false;
	}

	// vars
	$current_screen = get_current_screen();

	// no screen
	if ( ! $current_screen ) {
		return false;

		// array
	} elseif ( is_array( $id ) ) {
		return in_array( $current_screen->id, $id );

		// string
	} else {
		return ( $id === $current_screen->id );
	}
}

/**
 * Check if we're in an ACF admin screen
 *
 * @since  6.2.2
 *
 * @return boolean Returns true if the current screen is an ACF admin screen.
 */
function acf_is_acf_admin_screen() {
	if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
		return false;
	}
	$screen = get_current_screen();
	if ( $screen && ! empty( $screen->post_type ) && substr( $screen->post_type, 0, 4 ) === 'acf-' ) {
		return true;
	}

	return false;
}


/*
*  acf_maybe_get
*
*  This function will return a var if it exists in an array
*
*  @type    function
*  @date    9/12/2014
*  @since   5.1.5
*
*  @param   $array (array) the array to look within
*  @param   $key (key) the array key to look for. Nested values may be found using '/'
*  @param   $default (mixed) the value returned if not found
*  @return  $post_id (int)
*/

function acf_maybe_get( $array = array(), $key = 0, $default = null ) {

	return isset( $array[ $key ] ) ? $array[ $key ] : $default;
}

function acf_maybe_get_POST( $key = '', $default = null ) {

	return isset( $_POST[ $key ] ) ? acf_sanitize_request_args( $_POST[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- Checked elsewhere.
}

function acf_maybe_get_GET( $key = '', $default = null ) {

	return isset( $_GET[ $key ] ) ? acf_sanitize_request_args( $_GET[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked elsewhere.
}

/**
 * Returns an array of attachment data.
 *
 * @date    05/01/2015
 * @since   5.1.5
 *
 * @param   int|WP_Post The attachment ID or object.
 * @return  array|false
 */
function acf_get_attachment( $attachment ) {

	// Allow filter to short-circuit load attachment logic.
	// Alternatively, this filter may be used to switch blogs for multisite media functionality.
	$response = apply_filters( 'acf/pre_load_attachment', null, $attachment );
	if ( $response !== null ) {
		return $response;
	}

	// Get the attachment post object.
	$attachment = get_post( $attachment );
	if ( ! $attachment ) {
		return false;
	}
	if ( $attachment->post_type !== 'attachment' ) {
		return false;
	}

	// Load various attachment details.
	$meta          = wp_get_attachment_metadata( $attachment->ID );
	$attached_file = get_attached_file( $attachment->ID );
	if ( strpos( $attachment->post_mime_type, '/' ) !== false ) {
		list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
	} else {
		list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
	}

	// Generate response.
	$response = array(
		'ID'          => $attachment->ID,
		'id'          => $attachment->ID,
		'title'       => $attachment->post_title,
		'filename'    => wp_basename( $attached_file ),
		'filesize'    => 0,
		'url'         => wp_get_attachment_url( $attachment->ID ),
		'link'        => get_attachment_link( $attachment->ID ),
		'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
		'author'      => $attachment->post_author,
		'description' => $attachment->post_content,
		'caption'     => $attachment->post_excerpt,
		'name'        => $attachment->post_name,
		'status'      => $attachment->post_status,
		'uploaded_to' => $attachment->post_parent,
		'date'        => $attachment->post_date_gmt,
		'modified'    => $attachment->post_modified_gmt,
		'menu_order'  => $attachment->menu_order,
		'mime_type'   => $attachment->post_mime_type,
		'type'        => $type,
		'subtype'     => $subtype,
		'icon'        => wp_mime_type_icon( $attachment->ID ),
	);

	// Append filesize data.
	if ( isset( $meta['filesize'] ) ) {
		$response['filesize'] = $meta['filesize'];
	} else {
		/**
		 * Allows shortcutting our ACF's `filesize` call to prevent us making filesystem calls.
		 * Mostly useful for third party plugins which may offload media to other services, and filesize calls will induce a remote download.
		 *
		 * @since 6.2.2
		 *
		 * @param int|null The default filesize.
		 * @param WP_Post $attachment The attachment post object we're looking for the filesize for.
		 */
		$shortcut_filesize = apply_filters( 'acf/filesize', null, $attachment );
		if ( $shortcut_filesize ) {
			$response['filesize'] = intval( $shortcut_filesize );
		} elseif ( file_exists( $attached_file ) ) {
			$response['filesize'] = filesize( $attached_file );
		}
	}

	// Restrict the loading of image "sizes".
	$sizes_id = 0;

	// Type specific logic.
	switch ( $type ) {
		case 'image':
			$sizes_id = $attachment->ID;
			$src      = wp_get_attachment_image_src( $attachment->ID, 'full' );
			if ( $src ) {
				$response['url']    = $src[0];
				$response['width']  = $src[1];
				$response['height'] = $src[2];
			}
			break;
		case 'video':
			$response['width']  = acf_maybe_get( $meta, 'width', 0 );
			$response['height'] = acf_maybe_get( $meta, 'height', 0 );
			if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
				$sizes_id = $featured_id;
			}
			break;
		case 'audio':
			if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
				$sizes_id = $featured_id;
			}
			break;
	}

	// Load array of image sizes.
	if ( $sizes_id ) {
		$sizes      = get_intermediate_image_sizes();
		$sizes_data = array();
		foreach ( $sizes as $size ) {
			$src = wp_get_attachment_image_src( $sizes_id, $size );
			if ( $src ) {
				$sizes_data[ $size ]             = $src[0];
				$sizes_data[ $size . '-width' ]  = $src[1];
				$sizes_data[ $size . '-height' ] = $src[2];
			}
		}
		$response['sizes'] = $sizes_data;
	}

	/**
	 * Filters the attachment $response after it has been loaded.
	 *
	 * @date    16/06/2020
	 * @since   5.9.0
	 *
	 * @param   array $response Array of loaded attachment data.
	 * @param   WP_Post $attachment Attachment object.
	 * @param   array|false $meta Array of attachment meta data, or false if there is none.
	 */
	return apply_filters( 'acf/load_attachment', $response, $attachment, $meta );
}


/**
 *  This function will truncate and return a string
 *
 *  @date    8/08/2014
 *  @since   5.0.0
 *
 *  @param string $text   The text to truncate.
 *  @param int    $length The number of characters to allow in the string.
 *
 *  @return  string
 */
function acf_get_truncated( $text, $length = 64 ) {
	$text       = trim( $text );
	$the_length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );

	$cut_length = $length - 3;
	$return     = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $cut_length ) : substr( $text, 0, $cut_length );

	if ( $the_length > $cut_length ) {
		$return .= '...';
	}

	return $return;
}

/*
*  acf_current_user_can_admin
*
*  This function will return true if the current user can administrate the ACF field groups
*
*  @type    function
*  @date    9/02/2015
*  @since   5.1.5
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_current_user_can_admin() {

	if ( acf_get_setting( 'show_admin' ) && current_user_can( acf_get_setting( 'capability' ) ) ) {

		return true;

	}

	// return
	return false;
}


/*
*  acf_get_filesize
*
*  This function will return a numeric value of bytes for a given filesize string
*
*  @type    function
*  @date    18/02/2015
*  @since   5.1.5
*
*  @param   $size (mixed)
*  @return  (int)
*/

function acf_get_filesize( $size = 1 ) {

	// vars
	$unit  = 'MB';
	$units = array(
		'TB' => 4,
		'GB' => 3,
		'MB' => 2,
		'KB' => 1,
	);

	// look for $unit within the $size parameter (123 KB)
	if ( is_string( $size ) ) {

		// vars
		$custom = strtoupper( substr( $size, -2 ) );

		foreach ( $units as $k => $v ) {

			if ( $custom === $k ) {

				$unit = $k;
				$size = substr( $size, 0, -2 );

			}
		}
	}

	// calc bytes
	$bytes = floatval( $size ) * pow( 1024, $units[ $unit ] );

	// return
	return $bytes;
}


/*
*  acf_format_filesize
*
*  This function will return a formatted string containing the filesize and unit
*
*  @type    function
*  @date    18/02/2015
*  @since   5.1.5
*
*  @param   $size (mixed)
*  @return  (int)
*/

function acf_format_filesize( $size = 1 ) {

	// convert
	$bytes = acf_get_filesize( $size );

	// vars
	$units = array(
		'TB' => 4,
		'GB' => 3,
		'MB' => 2,
		'KB' => 1,
	);

	// loop through units
	foreach ( $units as $k => $v ) {

		$result = $bytes / pow( 1024, $v );

		if ( $result >= 1 ) {

			return $result . ' ' . $k;

		}
	}

	// return
	return $bytes . ' B';
}


/*
*  acf_get_valid_terms
*
*  This function will replace old terms with new split term ids
*
*  @type    function
*  @date    27/02/2015
*  @since   5.1.5
*
*  @param   $terms (int|array)
*  @param   $taxonomy (string)
*  @return  $terms
*/

function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) {

	// force into array
	$terms = acf_get_array( $terms );

	// force ints
	$terms = array_map( 'intval', $terms );

	// bail early if function does not yet exist or
	if ( ! function_exists( 'wp_get_split_term' ) || empty( $terms ) ) {

		return $terms;

	}

	// attempt to find new terms
	foreach ( $terms as $i => $term_id ) {

		$new_term_id = wp_get_split_term( $term_id, $taxonomy );

		if ( $new_term_id ) {

			$terms[ $i ] = $new_term_id;

		}
	}

	// return
	return $terms;
}


/*
*  acf_validate_attachment
*
*  This function will validate an attachment based on a field's restrictions and return an array of errors
*
*  @type    function
*  @date    3/07/2015
*  @since   5.2.3
*
*  @param   $attachment (array) attachment data. Changes based on context
*  @param   $field (array) field settings containing restrictions
*  @param   $context (string) $file is different when uploading / preparing
*  @return  $errors (array)
*/

function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) {

	// vars
	$errors = array();
	$file   = array(
		'type'   => '',
		'width'  => 0,
		'height' => 0,
		'size'   => 0,
	);

	// upload
	if ( $context == 'upload' ) {

		// vars
		$file['type'] = pathinfo( $attachment['name'], PATHINFO_EXTENSION );
		$file['size'] = filesize( $attachment['tmp_name'] );

		if ( strpos( $attachment['type'], 'image' ) !== false ) {

			$size           = getimagesize( $attachment['tmp_name'] );
			$file['width']  = acf_maybe_get( $size, 0 );
			$file['height'] = acf_maybe_get( $size, 1 );

		}

		// prepare
	} elseif ( $context == 'prepare' ) {

		$use_path       = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
		$file['type']   = pathinfo( $use_path, PATHINFO_EXTENSION );
		$file['size']   = acf_maybe_get( $attachment, 'filesizeInBytes', 0 );
		$file['width']  = acf_maybe_get( $attachment, 'width', 0 );
		$file['height'] = acf_maybe_get( $attachment, 'height', 0 );

		// custom
	} else {

		$file         = array_merge( $file, $attachment );
		$use_path     = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
		$file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );

	}

	// image
	if ( $file['width'] || $file['height'] ) {

		// width
		$min_width = (int) acf_maybe_get( $field, 'min_width', 0 );
		$max_width = (int) acf_maybe_get( $field, 'max_width', 0 );

		if ( $file['width'] ) {

			if ( $min_width && $file['width'] < $min_width ) {

				// min width
				$errors['min_width'] = sprintf( __( 'Image width must be at least %dpx.', 'acf' ), $min_width );

			} elseif ( $max_width && $file['width'] > $max_width ) {

				// min width
				$errors['max_width'] = sprintf( __( 'Image width must not exceed %dpx.', 'acf' ), $max_width );

			}
		}

		// height
		$min_height = (int) acf_maybe_get( $field, 'min_height', 0 );
		$max_height = (int) acf_maybe_get( $field, 'max_height', 0 );

		if ( $file['height'] ) {

			if ( $min_height && $file['height'] < $min_height ) {

				// min height
				$errors['min_height'] = sprintf( __( 'Image height must be at least %dpx.', 'acf' ), $min_height );

			} elseif ( $max_height && $file['height'] > $max_height ) {

				// min height
				$errors['max_height'] = sprintf( __( 'Image height must not exceed %dpx.', 'acf' ), $max_height );

			}
		}
	}

	// file size
	if ( $file['size'] ) {

		$min_size = acf_maybe_get( $field, 'min_size', 0 );
		$max_size = acf_maybe_get( $field, 'max_size', 0 );

		if ( $min_size && $file['size'] < acf_get_filesize( $min_size ) ) {

			// min width
			$errors['min_size'] = sprintf( __( 'File size must be at least %s.', 'acf' ), acf_format_filesize( $min_size ) );

		} elseif ( $max_size && $file['size'] > acf_get_filesize( $max_size ) ) {

			// min width
			$errors['max_size'] = sprintf( __( 'File size must not exceed %s.', 'acf' ), acf_format_filesize( $max_size ) );

		}
	}

	// file type
	if ( $file['type'] ) {

		$mime_types = acf_maybe_get( $field, 'mime_types', '' );

		// lower case
		$file['type'] = strtolower( $file['type'] );
		$mime_types   = strtolower( $mime_types );

		// explode
		$mime_types = str_replace( array( ' ', '.' ), '', $mime_types );
		$mime_types = explode( ',', $mime_types ); // split pieces
		$mime_types = array_filter( $mime_types ); // remove empty pieces

		if ( ! empty( $mime_types ) && ! in_array( $file['type'], $mime_types ) ) {

			// glue together last 2 types
			if ( count( $mime_types ) > 1 ) {

				$last1 = array_pop( $mime_types );
				$last2 = array_pop( $mime_types );

				$mime_types[] = $last2 . ' ' . __( 'or', 'acf' ) . ' ' . $last1;

			}

			$errors['mime_types'] = sprintf( __( 'File type must be %s.', 'acf' ), implode( ', ', $mime_types ) );

		}
	}

	/**
	*  Filters the errors for a file before it is uploaded or displayed in the media modal.
	*
	*  @date    3/07/2015
	*  @since   5.2.3
	*
	*  @param   array $errors An array of errors.
	*  @param   array $file An array of data for a single file.
	*  @param   array $attachment An array of attachment data which differs based on the context.
	*  @param   array $field The field array.
	*  @param   string $context The curent context (uploading, preparing)
	*/
	$errors = apply_filters( "acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field, $context );
	$errors = apply_filters( "acf/validate_attachment/name={$field['_name']}", $errors, $file, $attachment, $field, $context );
	$errors = apply_filters( "acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field, $context );
	$errors = apply_filters( 'acf/validate_attachment', $errors, $file, $attachment, $field, $context );

	// return
	return $errors;
}


/*
*  _acf_settings_uploader
*
*  Dynamic logic for uploader setting
*
*  @type    function
*  @date    7/05/2015
*  @since   5.2.3
*
*  @param   $uploader (string)
*  @return  $uploader
*/

add_filter( 'acf/settings/uploader', '_acf_settings_uploader' );

function _acf_settings_uploader( $uploader ) {

	// if can't upload files
	if ( ! current_user_can( 'upload_files' ) ) {

		$uploader = 'basic';

	}

	// return
	return $uploader;
}


/*
*  acf_translate_keys
*
*  description
*
*  @type    function
*  @date    7/12/2015
*  @since   5.3.2
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

/*
function acf_translate_keys( $array, $keys ) {

	// bail early if no keys
	if( empty($keys) ) return $array;


	// translate
	foreach( $keys as $k ) {

		// bail early if not exists
		if( !isset($array[ $k ]) ) continue;


		// translate
		$array[ $k ] = acf_translate( $array[ $k ] );

	}


	// return
	return $array;

}
*/


/*
*  acf_translate
*
*  This function will translate a string using the new 'l10n_textdomain' setting
*  Also works for arrays which is great for fields - select -> choices
*
*  @type    function
*  @date    4/12/2015
*  @since   5.3.2
*
*  @param   $string (mixed) string or array containins strings to be translated
*  @return  $string
*/

function acf_translate( $string ) {

	// vars
	$l10n       = acf_get_setting( 'l10n' );
	$textdomain = acf_get_setting( 'l10n_textdomain' );

	// bail early if not enabled
	if ( ! $l10n ) {
		return $string;
	}

	// bail early if no textdomain
	if ( ! $textdomain ) {
		return $string;
	}

	// is array
	if ( is_array( $string ) ) {

		return array_map( 'acf_translate', $string );

	}

	// bail early if not string
	if ( ! is_string( $string ) ) {
		return $string;
	}

	// bail early if empty
	if ( $string === '' ) {
		return $string;
	}

	// allow for var_export export
	if ( acf_get_setting( 'l10n_var_export' ) ) {

		// bail early if already translated
		if ( substr( $string, 0, 7 ) === '!!__(!!' ) {
			return $string;
		}

		// return
		return "!!__(!!'" . $string . "!!', !!'" . $textdomain . "!!')!!";

	}

	// vars
	return __( $string, $textdomain );
}


/*
*  acf_maybe_add_action
*
*  This function will determine if the action has already run before adding / calling the function
*
*  @type    function
*  @date    13/01/2016
*  @since   5.3.2
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_maybe_add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {

	// if action has already run, execute it
	// - if currently doing action, allow $tag to be added as per usual to allow $priority ordering needed for 3rd party asset compatibility
	if ( did_action( $tag ) && ! doing_action( $tag ) ) {

		call_user_func( $function_to_add );

		// if action has not yet run, add it
	} else {

		add_action( $tag, $function_to_add, $priority, $accepted_args );

	}
}


/*
*  acf_is_row_collapsed
*
*  This function will return true if the field's row is collapsed
*
*  @type    function
*  @date    2/03/2016
*  @since   5.3.2
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_is_row_collapsed( $field_key = '', $row_index = 0 ) {

	// collapsed
	$collapsed = acf_get_user_setting( 'collapsed_' . $field_key, '' );

	// cookie fallback ( version < 5.3.2 )
	if ( $collapsed === '' ) {

		$collapsed = acf_extract_var( $_COOKIE, "acf_collapsed_{$field_key}", '' );
		$collapsed = str_replace( '|', ',', $collapsed );

		// update
		acf_update_user_setting( 'collapsed_' . $field_key, $collapsed );

	}

	// explode
	$collapsed = explode( ',', $collapsed );
	$collapsed = array_filter( $collapsed, 'is_numeric' );

	// collapsed class
	return in_array( $row_index, $collapsed );
}


/*
*  acf_get_attachment_image
*
*  description
*
*  @type    function
*  @date    24/10/16
*  @since   5.5.0
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_get_attachment_image( $attachment_id = 0, $size = 'thumbnail' ) {

	// vars
	$url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
	$alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );

	// bail early if no url
	if ( ! $url ) {
		return '';
	}

	// return
	$value = '<img src="' . $url . '" alt="' . $alt . '" />';
}


/*
*  acf_get_post_thumbnail
*
*  This function will return a thumbail image url for a given post
*
*  @type    function
*  @date    3/05/2016
*  @since   5.3.8
*
*  @param   $post (obj)
*  @param   $size (mixed)
*  @return  (string)
*/

function acf_get_post_thumbnail( $post = null, $size = 'thumbnail' ) {

	// vars
	$data = array(
		'url'  => '',
		'type' => '',
		'html' => '',
	);

	// post
	$post = get_post( $post );

	// bail early if no post
	if ( ! $post ) {
		return $data;
	}

	// vars
	$thumb_id  = $post->ID;
	$mime_type = acf_maybe_get( explode( '/', $post->post_mime_type ), 0 );

	// attachment
	if ( $post->post_type === 'attachment' ) {

		// change $thumb_id
		if ( $mime_type === 'audio' || $mime_type === 'video' ) {

			$thumb_id = get_post_thumbnail_id( $post->ID );

		}

		// post
	} else {

		$thumb_id = get_post_thumbnail_id( $post->ID );

	}

	// try url
	$data['url'] = wp_get_attachment_image_src( $thumb_id, $size );
	$data['url'] = acf_maybe_get( $data['url'], 0 );

	// default icon
	if ( ! $data['url'] && $post->post_type === 'attachment' ) {

		$data['url']  = wp_mime_type_icon( $post->ID );
		$data['type'] = 'icon';

	}

	// html
	$data['html'] = '<img src="' . $data['url'] . '" alt="" />';

	// return
	return $data;
}

/**
 * acf_get_browser
 *
 * Returns the name of the current browser.
 *
 * @date    17/01/2014
 * @since   5.0.0
 *
 * @param   void
 * @return  string
 */
function acf_get_browser() {

	// Check server var.
	if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
		$agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );

		// Loop over search terms.
		$browsers = array(
			'Firefox' => 'firefox',
			'Trident' => 'msie',
			'MSIE'    => 'msie',
			'Edge'    => 'edge',
			'Chrome'  => 'chrome',
			'Safari'  => 'safari',
		);
		foreach ( $browsers as $k => $v ) {
			if ( strpos( $agent, $k ) !== false ) {
				return $v;
			}
		}
	}

	// Return default.
	return '';
}


/*
*  acf_is_ajax
*
*  This function will reutrn true if performing a wp ajax call
*
*  @type    function
*  @date    7/06/2016
*  @since   5.3.8
*
*  @param   n/a
*  @return  (boolean)
*/

function acf_is_ajax( $action = '' ) {

	// vars
	$is_ajax = false;

	// check if is doing ajax
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

		$is_ajax = true;

	}

	// phpcs:disable WordPress.Security.NonceVerification.Missing
	// check $action
	if ( $action && acf_maybe_get( $_POST, 'action' ) !== $action ) {
	// phpcs:enable WordPress.Security.NonceVerification.Missing
		$is_ajax = false;

	}

	// return
	return $is_ajax;
}

/**
 * Returns a date value in a formatted string.
 *
 * @since 5.3.8
 *
 * @param string $value  The date value to format.
 * @param string $format The format to use.
 * @return string
 */
function acf_format_date( $value, $format ) {
	// Bail early if no value or value is not what we expect.
	if ( ! $value || ( ! is_string( $value ) && ! is_int( $value ) ) ) {
		return $value;
	}

	// Numeric (either unix or YYYYMMDD).
	if ( is_numeric( $value ) && strlen( $value ) !== 8 ) {
		$unixtimestamp = $value;
	} else {
		$unixtimestamp = strtotime( $value );
	}

	return date_i18n( $format, $unixtimestamp );
}

/**
 * acf_clear_log
 *
 * Deletes the debug.log file.
 *
 * @date    21/1/19
 * @since   5.7.10
 *
 * @param   type $var Description. Default.
 * @return  type Description.
 */
function acf_clear_log() {
	unlink( WP_CONTENT_DIR . '/debug.log' );
}

/*
*  acf_log
*
*  description
*
*  @type    function
*  @date    24/06/2016
*  @since   5.3.8
*
*  @param   $post_id (int)
*  @return  $post_id (int)
*/

function acf_log() {

	// vars
	$args = func_get_args();

	// loop
	foreach ( $args as $i => $arg ) {

		// array | object
		if ( is_array( $arg ) || is_object( $arg ) ) {
			$arg = print_r( $arg, true );

			// bool
		} elseif ( is_bool( $arg ) ) {
			$arg = 'bool(' . ( $arg ? 'true' : 'false' ) . ')';
		}

		// update
		$args[ $i ] = $arg;
	}

	// log
	error_log( implode( ' ', $args ) );
}

/**
 *  acf_dev_log
 *
 *  Used to log variables only if ACF_DEV is defined
 *
 *  @date    25/8/18
 *  @since   5.7.4
 *
 *  @param   mixed
 *  @return  void
 */
function acf_dev_log() {
	if ( defined( 'ACF_DEV' ) && ACF_DEV ) {
		call_user_func_array( 'acf_log', func_get_args() );
	}
}

/*
*  acf_doing
*
*  This function will tell ACF what task it is doing
*
*  @type    function
*  @date    28/06/2016
*  @since   5.3.8
*
*  @param   $event (string)
*  @param   context (string)
*  @return  n/a
*/

function acf_doing( $event = '', $context = '' ) {

	acf_update_setting( 'doing', $event );
	acf_update_setting( 'doing_context', $context );
}


/*
*  acf_is_doing
*
*  This function can be used to state what ACF is doing, or to check
*
*  @type    function
*  @date    28/06/2016
*  @since   5.3.8
*
*  @param   $event (string)
*  @param   context (string)
*  @return  (boolean)
*/

function acf_is_doing( $event = '', $context = '' ) {

	// vars
	$doing = false;

	// task
	if ( acf_get_setting( 'doing' ) === $event ) {

		$doing = true;

	}

	// context
	if ( $context && acf_get_setting( 'doing_context' ) !== $context ) {

		$doing = false;

	}

	// return
	return $doing;
}


/*
*  acf_is_plugin_active
*
*  This function will return true if the ACF plugin is active
*  - May be included within a theme or other plugin
*
*  @type    function
*  @date    13/07/2016
*  @since   5.4.0
*
*  @param   $basename (int)
*  @return  $post_id (int)
*/


function acf_is_plugin_active() {

	// vars
	$basename = acf_get_setting( 'basename' );

	// ensure is_plugin_active() exists (not on frontend)
	if ( ! function_exists( 'is_plugin_active' ) ) {

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

	}

	// return
	return is_plugin_active( $basename );
}

/*
*  acf_send_ajax_results
*
*  This function will print JSON data for a Select2 AJAX query
*
*  @type    function
*  @date    19/07/2016
*  @since   5.4.0
*
*  @param   $response (array)
*  @return  n/a
*/

function acf_send_ajax_results( $response ) {

	// validate
	$response = wp_parse_args(
		$response,
		array(
			'results' => array(),
			'more'    => false,
			'limit'   => 0,
		)
	);

	// limit
	if ( $response['limit'] && $response['results'] ) {

		// vars
		$total = 0;

		foreach ( $response['results'] as $result ) {

			// parent
			++$total;

			// children
			if ( ! empty( $result['children'] ) ) {

				$total += count( $result['children'] );

			}
		}

		// calc
		if ( $total >= $response['limit'] ) {

			$response['more'] = true;

		}
	}

	// return
	wp_send_json( $response );
}


/*
*  acf_is_sequential_array
*
*  This function will return true if the array contains only numeric keys
*
*  @source  http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
*  @type    function
*  @date    9/09/2016
*  @since   5.4.0
*
*  @param   $array (array)
*  @return  (boolean)
*/

function acf_is_sequential_array( $array ) {

	// bail early if not array
	if ( ! is_array( $array ) ) {
		return false;
	}

	// loop
	foreach ( $array as $key => $value ) {

		// bail early if is string
		if ( is_string( $key ) ) {
			return false;
		}
	}

	// return
	return true;
}


/*
*  acf_is_associative_array
*
*  This function will return true if the array contains one or more string keys
*
*  @source  http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
*  @type    function
*  @date    9/09/2016
*  @since   5.4.0
*
*  @param   $array (array)
*  @return  (boolean)
*/

function acf_is_associative_array( $array ) {

	// bail early if not array
	if ( ! is_array( $array ) ) {
		return false;
	}

	// loop
	foreach ( $array as $key => $value ) {

		// bail early if is string
		if ( is_string( $key ) ) {
			return true;
		}
	}

	// return
	return false;
}


/*
*  acf_add_array_key_prefix
*
*  This function will add a prefix to all array keys
*  Useful to preserve numeric keys when performing array_multisort
*
*  @type    function
*  @date    15/09/2016
*  @since   5.4.0
*
*  @param   $array (array)
*  @param   $prefix (string)
*  @return  (array)
*/

function acf_add_array_key_prefix( $array, $prefix ) {

	// vars
	$array2 = array();

	// loop
	foreach ( $array as $k => $v ) {

		$k2            = $prefix . $k;
		$array2[ $k2 ] = $v;

	}

	// return
	return $array2;
}


/*
*  acf_remove_array_key_prefix
*
*  This function will remove a prefix to all array keys
*  Useful to preserve numeric keys when performing array_multisort
*
*  @type    function
*  @date    15/09/2016
*  @since   5.4.0
*
*  @param   $array (array)
*  @param   $prefix (string)
*  @return  (array)
*/

function acf_remove_array_key_prefix( $array, $prefix ) {

	// vars
	$array2 = array();
	$l      = strlen( $prefix );

	// loop
	foreach ( $array as $k => $v ) {

		$k2            = ( substr( $k, 0, $l ) === $prefix ) ? substr( $k, $l ) : $k;
		$array2[ $k2 ] = $v;

	}

	// return
	return $array2;
}


/*
*  acf_strip_protocol
*
*  This function will remove the proticol from a url
*  Used to allow licenses to remain active if a site is switched to https
*
*  @type    function
*  @date    10/01/2017
*  @since   5.5.4
*  @author  Aaron
*
*  @param   $url (string)
*  @return  (string)
*/

function acf_strip_protocol( $url ) {

	// strip the protical
	return str_replace( array( 'http://', 'https://' ), '', $url );
}


/*
*  acf_connect_attachment_to_post
*
*  This function will connect an attacment (image etc) to the post
*  Used to connect attachements uploaded directly to media that have not been attaced to a post
*
*  @type    function
*  @date    11/01/2017
*  @since   5.8.0 Added filter to prevent connection.
*  @since   5.5.4
*
*  @param   int $attachment_id The attachment ID.
*  @param   int $post_id The post ID.
*  @return  bool True if attachment was connected.
*/
function acf_connect_attachment_to_post( $attachment_id = 0, $post_id = 0 ) {

	// bail early if $attachment_id is not valid.
	if ( ! $attachment_id || ! is_numeric( $attachment_id ) ) {
		return false;
	}

	// bail early if $post_id is not valid.
	if ( ! $post_id || ! is_numeric( $post_id ) ) {
		return false;
	}

	/**
	*  Filters whether or not to connect the attachment.
	*
	*  @date    8/11/18
	*  @since   5.8.0
	*
	*  @param   bool $bool Returning false will prevent the connection. Default true.
	*  @param   int $attachment_id The attachment ID.
	*  @param   int $post_id The post ID.
	*/
	if ( ! apply_filters( 'acf/connect_attachment_to_post', true, $attachment_id, $post_id ) ) {
		return false;
	}

	// vars
	$post = get_post( $attachment_id );

	// Check if is valid post.
	if ( $post && $post->post_type == 'attachment' && $post->post_parent == 0 ) {

		// update
		wp_update_post(
			array(
				'ID'          => $post->ID,
				'post_parent' => $post_id,
			)
		);

		// return
		return true;
	}

	// return
	return true;
}


/*
*  acf_encrypt
*
*  This function will encrypt a string using PHP
*  https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
*
*  @type    function
*  @date    27/2/17
*  @since   5.5.8
*
*  @param   $data (string)
*  @return  (string)
*/


function acf_encrypt( $data = '' ) {

	// bail early if no encrypt function
	if ( ! function_exists( 'openssl_encrypt' ) ) {
		return base64_encode( $data );
	}

	// generate a key
	$key = wp_hash( 'acf_encrypt' );

	// Generate an initialization vector
	$iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );

	// Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
	$encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $key, 0, $iv );

	// The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
	return base64_encode( $encrypted_data . '::' . $iv );
}


/*
*  acf_decrypt
*
*  This function will decrypt an encrypted string using PHP
*  https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
*
*  @type    function
*  @date    27/2/17
*  @since   5.5.8
*
*  @param   $data (string)
*  @return  (string)
*/

function acf_decrypt( $data = '' ) {

	// bail early if no decrypt function
	if ( ! function_exists( 'openssl_decrypt' ) ) {
		return base64_decode( $data );
	}

	// generate a key
	$key = wp_hash( 'acf_encrypt' );

	// To decrypt, split the encrypted data from our IV - our unique separator used was "::"
	list($encrypted_data, $iv) = explode( '::', base64_decode( $data ), 2 );

	// decrypt
	return openssl_decrypt( $encrypted_data, 'aes-256-cbc', $key, 0, $iv );
}

/**
 *  acf_parse_markdown
 *
 *  A very basic regex-based Markdown parser function based off [slimdown](https://gist.github.com/jbroadway/2836900).
 *
 *  @date    6/8/18
 *  @since   5.7.2
 *
 *  @param   string $text The string to parse.
 *  @return  string
 */

function acf_parse_markdown( $text = '' ) {

	// trim
	$text = trim( $text );

	// rules
	$rules = array(
		'/=== (.+?) ===/'            => '<h2>$1</h2>',                   // headings
		'/== (.+?) ==/'              => '<h3>$1</h3>',                   // headings
		'/= (.+?) =/'                => '<h4>$1</h4>',                   // headings
		'/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href="$2">$1</a>',           // links
		'/(\*\*)(.*?)\1/'            => '<strong>$2</strong>',           // bold
		'/(\*)(.*?)\1/'              => '<em>$2</em>',                   // intalic
		'/`(.*?)`/'                  => '<code>$1</code>',               // inline code
		'/\n\*(.*)/'                 => "\n<ul>\n\t<li>$1</li>\n</ul>",  // ul lists
		'/\n[0-9]+\.(.*)/'           => "\n<ol>\n\t<li>$1</li>\n</ol>",  // ol lists
		'/<\/ul>\s?<ul>/'            => '',                              // fix extra ul
		'/<\/ol>\s?<ol>/'            => '',                              // fix extra ol
	);
	foreach ( $rules as $k => $v ) {
		$text = preg_replace( $k, $v, $text );
	}

	// autop
	$text = wpautop( $text );

	// return
	return $text;
}

/**
 *  acf_get_sites
 *
 *  Returns an array of sites for a network.
 *
 *  @date    29/08/2016
 *  @since   5.4.0
 *
 *  @param   void
 *  @return  array
 */
function acf_get_sites() {
	$results = array();
	$sites   = get_sites( array( 'number' => 0 ) );
	if ( $sites ) {
		foreach ( $sites as $site ) {
			$results[] = get_site( $site )->to_array();
		}
	}
	return $results;
}

/**
 *  acf_convert_rules_to_groups
 *
 *  Converts an array of rules from ACF4 to an array of groups for ACF5
 *
 *  @date    25/8/18
 *  @since   5.7.4
 *
 *  @param   array  $rules An array of rules.
 *  @param   string $anyorall The anyorall setting used in ACF4. Defaults to 'any'.
 *  @return  array
 */
function acf_convert_rules_to_groups( $rules, $anyorall = 'any' ) {

	// vars
	$groups = array();
	$index  = 0;

	// loop
	foreach ( $rules as $rule ) {

		// extract vars
		$group = acf_extract_var( $rule, 'group_no' );
		$order = acf_extract_var( $rule, 'order_no' );

		// calculate group if not defined
		if ( $group === null ) {
			$group = $index;

			// use $anyorall to determine if a new group is needed
			if ( $anyorall == 'any' ) {
				++$index;
			}
		}

		// calculate order if not defined
		if ( $order === null ) {
			$order = isset( $groups[ $group ] ) ? count( $groups[ $group ] ) : 0;
		}

		// append to group
		$groups[ $group ][ $order ] = $rule;

		// sort groups
		ksort( $groups[ $group ] );
	}

	// sort groups
	ksort( $groups );

	// return
	return $groups;
}

/**
 *  acf_register_ajax
 *
 *  Regsiters an ajax callback.
 *
 *  @date    5/10/18
 *  @since   5.7.7
 *
 *  @param   string $name The ajax action name.
 *  @param   array  $callback The callback function or array.
 *  @param   bool   $public Whether to allow access to non logged in users.
 *  @return  void
 */
function acf_register_ajax( $name = '', $callback = false, $public = false ) {

	// vars
	$action = "acf/ajax/$name";

	// add action for logged-in users
	add_action( "wp_ajax_$action", $callback );

	// add action for non logged-in users
	if ( $public ) {
		add_action( "wp_ajax_nopriv_$action", $callback );
	}
}

/**
 *  acf_str_camel_case
 *
 *  Converts a string into camelCase.
 *  Thanks to https://stackoverflow.com/questions/31274782/convert-array-keys-from-underscore-case-to-camelcase-recursively
 *
 *  @date    24/10/18
 *  @since   5.8.0
 *
 *  @param   string $string The string ot convert.
 *  @return  string
 */
function acf_str_camel_case( $string = '' ) {
	return lcfirst( str_replace( ' ', '', ucwords( str_replace( '_', ' ', $string ) ) ) );
}

/**
 *  acf_array_camel_case
 *
 *  Converts all aray keys to camelCase.
 *
 *  @date    24/10/18
 *  @since   5.8.0
 *
 *  @param   array $array The array to convert.
 *  @return  array
 */
function acf_array_camel_case( $array = array() ) {
	$array2 = array();
	foreach ( $array as $k => $v ) {
		$array2[ acf_str_camel_case( $k ) ] = $v;
	}
	return $array2;
}

/**
 * Returns true if the current screen is using the block editor.
 *
 * @date 13/12/18
 * @since 5.8.0
 *
 * @return bool
 */
function acf_is_block_editor() {
	if ( function_exists( 'get_current_screen' ) ) {
		$screen = get_current_screen();
		if ( $screen && method_exists( $screen, 'is_block_editor' ) ) {
			return $screen->is_block_editor();
		}
	}
	return false;
}

/**
 * Return an array of the WordPress reserved terms
 *
 * @since 6.1
 *
 * @return array The WordPress reserved terms list.
 */
function acf_get_wp_reserved_terms() {
	return array( 'action', 'attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'custom', 'customize_messenger_channel', 'customized', 'cpage', 'day', 'debug', 'embed', 'error', 'exact', 'feed', 'fields', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nonce', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'status', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'terms', 'theme', 'title', 'type', 'types', 'w', 'withcomments', 'withoutcomments', 'year' );
}