Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 2366 lines (2014 sloc) 51.593 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
<?php

/**
* @author Dimas Begunoff
* @copyright Copyright (c) 2009, Dimas Begunoff, http://farinspace.com
* @license http://en.wikipedia.org/wiki/MIT_License The MIT License
* @package WPAlchemy
* @version 1.5.1
* @link http://github.com/farinspace/wpalchemy
* @link http://farinspace.com
*/

// todo: perhaps move _global_head and _global_foot locally, when first run
// define a constant to prevent other instances from running again ...

add_action('admin_head', array('WPAlchemy_MetaBox', '_global_head'));

add_action('admin_footer', array('WPAlchemy_MetaBox', '_global_foot'));

define('WPALCHEMY_MODE_ARRAY', 'array');

define('WPALCHEMY_MODE_EXTRACT', 'extract');

define('WPALCHEMY_FIELD_HINT_TEXT', 'text');

define('WPALCHEMY_FIELD_HINT_TEXTAREA', 'textarea');

define('WPALCHEMY_FIELD_HINT_CHECKBOX', 'checkbox');

define('WPALCHEMY_FIELD_HINT_CHECKBOX_MULTI', 'checkbox_multi');

define('WPALCHEMY_FIELD_HINT_RADIO', 'radio');

define('WPALCHEMY_FIELD_HINT_SELECT', 'select');

define('WPALCHEMY_FIELD_HINT_SELECT_MULTI', 'select_multi');

// depreciated, use WPALCHEMY_FIELD_HINT_SELECT_MULTI instead
define('WPALCHEMY_FIELD_HINT_SELECT_MULTIPLE', 'select_multiple');

define('WPALCHEMY_LOCK_TOP', 'top');

define('WPALCHEMY_LOCK_BOTTOM', 'bottom');

define('WPALCHEMY_LOCK_BEFORE_POST_TITLE', 'before_post_title');

define('WPALCHEMY_LOCK_AFTER_POST_TITLE', 'after_post_title');

define('WPALCHEMY_VIEW_START_OPENED', 'opened');

define('WPALCHEMY_VIEW_START_CLOSED', 'closed');

define('WPALCHEMY_VIEW_ALWAYS_OPENED', 'always_opened');

class WPAlchemy_MetaBox
{
/**
* User defined identifier for the meta box, prefix with an underscore to
* prevent option(s) form showing up in the custom fields meta box, this
* option should be used when instantiating the class.
*
* @since 1.0
* @access public
* @var string required
*/
var $id;

/**
* Used to set the title of the meta box, this option should be used when
* instantiating the class.
*
* @since 1.0
* @access public
* @var string required
* @see $hide_title
*/
var $title = 'Custom Meta';

/**
* Used to set the meta box content, the contents of your meta box should be
* defined within this file, this option should be used when instantiating
* the class.
*
* @since 1.0
* @access public
* @var string required
*/
var $template;

/**
* Used to set the post types that the meta box can appear in, this option
* should be used when instantiating the class.
*
* @since 1.0
* @access public
* @var array
*/
var $types;

/**
* @since 1.0
* @access public
* @var bool
*/
var $context = 'normal';

/**
* @since 1.0
* @access public
* @var bool
*/
var $priority = 'high';

/**
* @since 1.0
* @access public
* @var bool
*/
var $autosave = TRUE;

/**
* Used to set how the class does its data storage, data will be stored as
* an associative array in a single meta entry in the wp_postmeta table or
* data can be set and individual entries in the wp_postmeta table, the
* following constants should be used when setting this option,
* WPALCHEMY_MODE_ARRAY (default) and WPALCHEMY_MODE_EXTRACT, this option
* should be used when instantiating the class.
*
* @since 1.2
* @access public
* @var string
*/
var $mode = WPALCHEMY_MODE_ARRAY;

/**
* When the mode option is set to WPALCHEMY_MODE_EXTRACT, you have to take
* care to avoid name collisions with other meta entries. Use this option to
* automatically add a prefix to your variables, this option should be used
* when instantiating the class.
*
* @since 1.2
* @access public
* @var array
*/
var $prefix;

/**
* @since 1.0
* @access public
* @var bool
*/
var $exclude_template;

/**
* @since 1.0
* @access public
* @var bool
*/
var $exclude_category_id;

/**
* @since 1.0
* @access public
* @var bool
*/
var $exclude_category;

/**
* @since 1.0
* @access public
* @var bool
*/
var $exclude_tag_id;

/**
* @since 1.0
* @access public
* @var bool
*/
var $exclude_tag;

/**
* @since 1.0
* @access public
* @var bool
*/
var $exclude_post_id;

/**
* @since 1.0
* @access public
* @var bool
*/
var $include_template;

/**
* @since 1.0
* @access public
* @var bool
*/
var $include_category_id;

/**
* @since 1.0
* @access public
* @var bool
*/
var $include_category;

/**
* @since 1.0
* @access public
* @var bool
*/
var $include_tag_id;

/**
* @since 1.0
* @access public
* @var bool
*/
var $include_tag;

/**
* @since 1.0
* @access public
* @var bool
*/
var $include_post_id;

/**
* Callback used on the WordPress "admin_init" action, the main benefit is
* that this callback is executed only when the meta box is present, this
* option should be used when instantiating the class.
*
* @since 1.3.4
* @access public
* @var string|array optional
*/
var $init_action;

/**
* Callback used to override when the meta box gets displayed, must return
* true or false to determine if the meta box should or should not be
* displayed, this option should be used when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @param array $post_id first variable passed to the callback function
* @see can_output()
*/
var $output_filter;

/**
* Callback used to override or insert meta data before saving, you can halt
* saving by passing back FALSE (return FALSE), this option should be used
* when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @param array $meta meta box data, first variable passed to the callback function
* @param string $post_id second variable passed to the callback function
* @see $save_action, add_filter()
*/
var $save_filter;

/**
* Callback used to execute custom code after saving, this option should be
* used when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @param array $meta meta box data, first variable passed to the callback function
* @param string $post_id second variable passed to the callback function
* @see $save_filter, add_filter()
*/
var $save_action;

/**
* Callback used to override or insert STYLE or SCRIPT tags into the head,
* this option should be used when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @param array $content current head content, first variable passed to the callback function
* @see $head_action, add_filter()
*/
var $head_filter;

/**
* Callback used to insert STYLE or SCRIPT tags into the head,
* this option should be used when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @see $head_filter, add_action()
*/
var $head_action;

/**
* Callback used to override or insert SCRIPT tags into the footer, this
* option should be used when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @param array $content current foot content, first variable passed to the callback function
* @see $foot_action, add_filter()
*/
var $foot_filter;

/**
* Callback used to insert SCRIPT tags into the footer, this option should
* be used when instantiating the class.
*
* @since 1.3
* @access public
* @var string|array optional
* @see $foot_filter, add_action()
*/
var $foot_action;

/**
* Used to hide the default content editor in a page or post, this option
* should be used when instantiating the class.
*
* @since 1.3
* @access public
* @var bool optional
*/
var $hide_editor = FALSE;

/**
* Used in conjunction with the "hide_editor" option, prevents the media
* buttons from also being hidden.
*
* @since 1.5
* @access public
* @var bool optional
*/
var $use_media_buttons = FALSE;

/**
* Used to hide the meta box title, this option should be used when
* instantiating the class.
*
* @since 1.3
* @access public
* @var bool optional
* @see $title
*/
var $hide_title = FALSE;

/**
* Used to lock a meta box in place, possible values are: top, bottom,
* before_post_title, after_post_title, this option should be used when
* instantiating the class.
*
* @since 1.3.3
* @access public
* @var string optional possible values are: top, bottom, before_post_title, after_post_title
*/
var $lock;

/**
* Used to lock a meta box at top (below the default content editor), this
* option should be used when instantiating the class.
*
* @deprecated deprecated since version 1.3.3
* @since 1.3
* @access public
* @var bool optional
* @see $lock
*/
var $lock_on_top = FALSE;

/**
* Used to lock a meta box at bottom, this option should be used when
* instantiating the class.
*
* @deprecated deprecated since version 1.3.3
* @since 1.3
* @access public
* @var bool optional
* @see $lock
*/
var $lock_on_bottom = FALSE;

/**
* Used to set the initial view state of the meta box, possible values are:
* opened, closed, always_opened, this option should be used when
* instantiating the class.
*
* @since 1.3.3
* @access public
* @var string optional possible values are: opened, closed, always_opened
*/
var $view;

/**
* Used to hide the show/hide checkbox option from the screen options area,
* this option should be used when instantiating the class.
*
* @since 1.3.4
* @access public
* @var bool optional
*/
var $hide_screen_option = FALSE;

// private

var $meta;
var $name;
var $subname;

/**
* Used to provide field type hinting
*
* @since 1.3
* @access private
* @var string
* @see the_field()
*/
var $hint;

var $length = 0;
var $current = -1;
var $in_loop = FALSE;
var $in_template = FALSE;
var $group_tag;
var $current_post_id;

/**
* Used to store current loop details, cleared after loop ends
*
* @since 1.4
* @access private
* @var stdClass
* @see have_fields_and_multi(), have_fields()
*/
var $_loop_data;

function WPAlchemy_MetaBox($arr)
{
$this->_loop_data = new stdClass;

$this->meta = array();

$this->types = array('post', 'page');

if (is_array($arr))
{
foreach ($arr as $n => $v)
{
$this->$n = $v;
}

if (empty($this->id)) die('Meta box ID required');

if (is_numeric($this->id)) die('Meta box ID must be a string');

if (empty($this->template)) die('Meta box template file required');

// check for nonarray values

$exc_inc = array
(
'exclude_template',
'exclude_category_id',
'exclude_category',
'exclude_tag_id',
'exclude_tag',
'exclude_post_id',

'include_template',
'include_category_id',
'include_category',
'include_tag_id',
'include_tag',
'include_post_id'
);

foreach ($exc_inc as $v)
{
// ideally the exclude and include values should be in array form, convert to array otherwise
if (!empty($this->$v) AND !is_array($this->$v))
{
$this->$v = array_map('trim',explode(',',$this->$v));
}
}

// convert depreciated variables
if ($this->lock_on_top) $this->lock = WPALCHEMY_LOCK_TOP;
elseif ($this->lock_on_bottom) $this->lock = WPALCHEMY_LOCK_BOTTOM;

add_action('admin_init', array($this,'_init'));

// uses the default wordpress-importer plugin hook
add_action('import_post_meta', array($this, '_import'), 10, 3);
}
else
{
die('Associative array parameters required');
}
}

/**
* Used to correct double serialized data during post/page export/import,
* additionally will try to fix corrupted serialized data by recalculating
* string length values
*
* @since 1.3.16
* @access private
*/
function _import($post_id, $key, $value)
{
if (WPALCHEMY_MODE_ARRAY == $this->mode AND $key == $this->id)
{
// using $wp_import to get access to the raw postmeta data prior to it getting passed
// through "maybe_unserialize()" in "plugins/wordpress-importer/wordpress-importer.php"
// the "import_post_meta" action is called after "maybe_unserialize()"

global $wp_import;

foreach ( $wp_import->posts as $post )
{
if ( $post_id == $post['post_id'] )
{
foreach( $post['postmeta'] as $meta )
{
if ( $key == $meta['key'] )
{
// try to fix corrupted serialized data, specifically "\r\n" being converted to "\n" during wordpress XML export (WXR)
// "maybe_unserialize()" fixes a wordpress bug which double serializes already serialized data during export/import
$value = maybe_unserialize( preg_replace( '!s:(\d+):"(.*?)";!es', "'s:'.strlen('$2').':\"$2\";'", stripslashes( $meta['value'] ) ) );

update_post_meta( $post_id, $key, $value );
}
}
}
}
}
}

/**
* Used to initialize the meta box, runs on WordPress admin_init action,
* properly calls internal WordPress methods
*
* @since 1.0
* @access private
*/
function _init()
{
// must be creating or editing a post or page
if ( ! WPAlchemy_MetaBox::_is_post() AND ! WPAlchemy_MetaBox::_is_page()) return;

if ( ! empty($this->output_filter))
{
$this->add_filter('output', $this->output_filter);
}

if ($this->can_output())
{
foreach ($this->types as $type)
{
add_meta_box($this->id . '_metabox', $this->title, array($this, '_setup'), $type, $this->context, $this->priority);
}

add_action('save_post', array($this,'_save'));

$filters = array('save', 'head', 'foot');

foreach ($filters as $filter)
{
$var = $filter . '_filter';

if (!empty($this->$var))
{
if ('save' == $filter)
{
$this->add_filter($filter, $this->$var, 10, 2);
}
else
{
$this->add_filter($filter, $this->$var);
}
}
}

$actions = array('save', 'head', 'foot', 'init');

foreach ($actions as $action)
{
$var = $action . '_action';

if (!empty($this->$var))
{
if ('save' == $action)
{
$this->add_action($action, $this->$var, 10, 2);
}
else
{
$this->add_action($action, $this->$var);
}
}
}

add_action('admin_head', array($this,'_head'), 11);

add_action('admin_footer', array($this,'_foot'), 11);

// action: init
if ($this->has_action('init'))
{
$this->do_action('init');
}
}
}

/**
* Used to insert STYLE or SCRIPT tags into the head, called on WordPress
* admin_head action.
*
* @since 1.3
* @access private
* @see _foot()
*/
function _head()
{
$content = NULL;

ob_start();

?>
<style type="text/css">
<?php if ($this->hide_editor) { ?> #wp-content-editor-container, #post-status-info, <?php if ($this->use_media_buttons) { ?> #content-html, #content-tmce<?php } else { ?> #wp-content-wrap<?php } ?> { display:none; } <?php } ?>
</style>
<?php

$content = ob_get_contents();

ob_end_clean();

// filter: head
if ($this->has_filter('head'))
{
$content = $this->apply_filters('head', $content);
}

echo $content;

// action: head
if ($this->has_action('head'))
{
$this->do_action('head');
}
}

/**
* Used to insert SCRIPT tags into the footer, called on WordPress
* admin_footer action.
*
* @since 1.3
* @access private
* @see _head()
*/
function _foot()
{
$content = NULL;

if
(
$this->lock OR
$this->hide_title OR
$this->view OR
$this->hide_screen_option
)
{
ob_start();

?>
<script type="text/javascript">
/* <![CDATA[ */
(function($){ /* not using jQuery ondomready, code runs right away in footer */

var mb_id = '<?php echo $this->id; ?>';
var mb = $('#' + mb_id + '_metabox');

<?php if (WPALCHEMY_LOCK_TOP == $this->lock): ?>
<?php if ('side' == $this->context): ?>
var id = 'wpalchemy-side-top';
if ( ! $('#'+id).length)
{
$('<div></div>').attr('id',id).prependTo('#side-info-column');
}
<?php else: ?>
var id = 'wpalchemy-content-top';
if ( ! $('#'+id).length)
{
$('<div></div>').attr('id',id).insertAfter('#postdiv, #postdivrich');
}
<?php endif; ?>
$('#'+id).append(mb);
<?php elseif (WPALCHEMY_LOCK_BOTTOM == $this->lock): ?>
<?php if ('side' == $this->context): ?>
var id = 'wpalchemy-side-bottom';
if ( ! $('#'+id).length)
{
$('<div></div>').attr('id',id).appendTo('#side-info-column');
}
<?php else: ?>
if ( ! $('#advanced-sortables').children().length)
{
$('#advanced-sortables').css('display','none');
}

var id = 'wpalchemy-content-bottom';
if ( ! $('#'+id).length)
{
$('<div></div>').attr('id',id).insertAfter('#advanced-sortables');
}
<?php endif; ?>
$('#'+id).append(mb);
<?php elseif (WPALCHEMY_LOCK_BEFORE_POST_TITLE == $this->lock): ?>
<?php if ('side' != $this->context): ?>
var id = 'wpalchemy-content-bpt';
if ( ! $('#'+id).length)
{
$('<div></div>').attr('id',id).prependTo('#post-body-content');
}
$('#'+id).append(mb);
<?php endif; ?>
<?php elseif (WPALCHEMY_LOCK_AFTER_POST_TITLE == $this->lock): ?>
<?php if ('side' != $this->context): ?>
var id = 'wpalchemy-content-apt';
if ( ! $('#'+id).length)
{
$('<div></div>').attr('id',id).insertAfter('#titlediv');
}
$('#'+id).append(mb);
<?php endif; ?>
<?php endif; ?>

<?php if ( ! empty($this->lock)): ?>
$('.hndle', mb).css('cursor','pointer');
$('.handlediv', mb).remove();
<?php endif; ?>

<?php if ($this->hide_title): ?>
$('.hndle', mb).remove();
$('.handlediv', mb).remove();
mb.removeClass('closed'); /* start opened */
<?php endif; ?>

<?php if (WPALCHEMY_VIEW_START_OPENED == $this->view): ?>
mb.removeClass('closed');
<?php elseif (WPALCHEMY_VIEW_START_CLOSED == $this->view): ?>
mb.addClass('closed');
<?php elseif (WPALCHEMY_VIEW_ALWAYS_OPENED == $this->view): ?>
/* todo: need to find a way to add this script block below, load-scripts.php?... */
var h3 = mb.children('h3');
setTimeout(function(){ h3.unbind('click'); }, 1000);
$('.handlediv', mb).remove();
mb.removeClass('closed'); /* start opened */
$('.hndle', mb).css('cursor','auto');
<?php endif; ?>

<?php if ($this->hide_screen_option): ?>
$('.metabox-prefs label[for='+ mb_id +'_metabox-hide]').remove();
<?php endif; ?>

mb = null;

})(jQuery);
/* ]]> */
</script>
<?php

$content = ob_get_contents();

ob_end_clean();
}

// filter: foot
if ($this->has_filter('foot'))
{
$content = $this->apply_filters('foot', $content);
}

echo $content;

// action: foot
if ($this->has_action('foot'))
{
$this->do_action('foot');
}
}

/**
* Used to setup the meta box content template
*
* @since 1.0
* @access private
* @see _init()
*/
function _setup()
{
$this->in_template = TRUE;

// also make current post data available
global $post;

// shortcuts
$mb =& $this;
$metabox =& $this;
$id = $this->id;
$meta = $this->_meta(NULL, TRUE);

// use include because users may want to use one templete for multiple meta boxes
include $this->template;

// create a nonce for verification
echo '<input type="hidden" name="'. $this->id .'_nonce" value="' . wp_create_nonce($this->id) . '" />';

$this->in_template = FALSE;
}

/**
* Used to properly prefix the filter tag, the tag is unique to the meta
* box instance
*
* @since 1.3
* @access private
* @param string $tag name of the filter
* @return string uniquely prefixed tag name
*/
function _get_filter_tag($tag)
{
$prefix = 'wpalchemy_filter_' . $this->id . '_';
$prefix = preg_replace('/_+/', '_', $prefix);

$tag = preg_replace('/^'. $prefix .'/i', '', $tag);
return $prefix . $tag;
}

/**
* Uses WordPress add_filter() function, see WordPress add_filter()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L65
*/
function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1)
{
$tag = $this->_get_filter_tag($tag);;
add_filter($tag, $function_to_add, $priority, $accepted_args);
}

/**
* Uses WordPress has_filter() function, see WordPress has_filter()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L86
*/
function has_filter($tag, $function_to_check = FALSE)
{
$tag = $this->_get_filter_tag($tag);
return has_filter($tag, $function_to_check);
}

/**
* Uses WordPress apply_filters() function, see WordPress apply_filters()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L134
*/
function apply_filters($tag, $value)
{
$args = func_get_args();
$args[0] = $this->_get_filter_tag($tag);
return call_user_func_array('apply_filters', $args);
}

/**
* Uses WordPress remove_filter() function, see WordPress remove_filter()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L250
*/
function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1)
{
$tag = $this->_get_filter_tag($tag);
return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
}

/**
* Used to properly prefix the action tag, the tag is unique to the meta
* box instance
*
* @since 1.3
* @access private
* @param string $tag name of the action
* @return string uniquely prefixed tag name
*/
function _get_action_tag($tag)
{
$prefix = 'wpalchemy_action_' . $this->id . '_';
$prefix = preg_replace('/_+/', '_', $prefix);

$tag = preg_replace('/^'. $prefix .'/i', '', $tag);
return $prefix . $tag;
}

/**
* Uses WordPress add_action() function, see WordPress add_action()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L324
*/
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1)
{
$tag = $this->_get_action_tag($tag);
add_action($tag, $function_to_add, $priority, $accepted_args);
}

/**
* Uses WordPress has_action() function, see WordPress has_action()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L492
*/
function has_action($tag, $function_to_check = FALSE)
{
$tag = $this->_get_action_tag($tag);
return has_action($tag, $function_to_check);
}

/**
* Uses WordPress remove_action() function, see WordPress remove_action()
*
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L513
*/
function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1)
{
$tag = $this->_get_action_tag($tag);
return remove_action($tag, $function_to_remove, $priority, $accepted_args);
}

/**
* Uses WordPress do_action() function, see WordPress do_action()
* @since 1.3
* @access public
* @link http://core.trac.wordpress.org/browser/trunk/wp-includes/plugin.php#L352
*/
function do_action($tag, $arg = '')
{
$args = func_get_args();
$args[0] = $this->_get_action_tag($tag);
return call_user_func_array('do_action', $args);
}

/**
* Used to check if creating a new post or editing one
*
* @static
* @since 1.3.7
* @access private
* @return bool
* @see _is_page()
*/
function _is_post()
{
if ('post' == WPAlchemy_MetaBox::_is_post_or_page())
{
return TRUE;
}

return FALSE;
}

/**
* Used to check if creating a new page or editing one
*
* @static
* @since 1.3.7
* @access private
* @return bool
* @see _is_post()
*/
function _is_page()
{
if ('page' == WPAlchemy_MetaBox::_is_post_or_page())
{
return TRUE;
}

return FALSE;
}

/**
* Used to check if creating or editing a post or page
*
* @static
* @since 1.3.8
* @access private
* @return string "post" or "page"
* @see _is_post(), _is_page()
*/
function _is_post_or_page()
{
$post_type = WPAlchemy_MetaBox::_get_current_post_type();

if (isset($post_type))
{
if ('page' == $post_type)
{
return 'page';
}
else
{
return 'post';
}
}

return NULL;
}

/**
* Used to check for the current post type, works when creating or editing a
* new post, page or custom post type.
*
* @static
* @since 1.4.6
* @return string [custom_post_type], page or post
*/
function _get_current_post_type()
{
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;

if ( isset( $uri ) )
{
$uri_parts = parse_url($uri);

$file = basename($uri_parts['path']);

if ($uri AND in_array($file, array('post.php', 'post-new.php')))
{
$post_id = WPAlchemy_MetaBox::_get_post_id();

$post_type = isset($_GET['post_type']) ? $_GET['post_type'] : NULL ;

$post_type = $post_id ? get_post_type($post_id) : $post_type ;

if (isset($post_type))
{
return $post_type;
}
else
{
// because of the 'post.php' and 'post-new.php' checks above, we can default to 'post'
return 'post';
}
}
}

return NULL;
}

/**
* Used to get the current post id.
*
* @static
* @since 1.4.8
* @return int post ID
*/
function _get_post_id()
{
global $post;

$p_post_id = isset($_POST['post_ID']) ? $_POST['post_ID'] : null ;

$g_post_id = isset($_GET['post']) ? $_GET['post'] : null ;

$post_id = $g_post_id ? $g_post_id : $p_post_id ;

$post_id = isset($post->ID) ? $post->ID : $post_id ;

if (isset($post_id))
{
return (integer) $post_id;
}

return null;
}

/**
* @since 1.0
*/
function can_output()
{
$post_id = WPAlchemy_MetaBox::_get_post_id();

if (!empty($this->exclude_template) OR !empty($this->include_template))
{
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
}

if
(
!empty($this->exclude_category) OR
!empty($this->exclude_category_id) OR
!empty($this->include_category) OR
!empty($this->include_category_id)
)
{
$categories = wp_get_post_categories($post_id,'fields=all');
}

if
(
!empty($this->exclude_tag) OR
!empty($this->exclude_tag_id) OR
!empty($this->include_tag) OR
!empty($this->include_tag_id)
)
{
$tags = wp_get_post_tags($post_id);
}

// processing order: "exclude" then "include"
// processing order: "template" then "category" then "post"

$can_output = TRUE; // include all

if
(
!empty($this->exclude_template) OR
!empty($this->exclude_category_id) OR
!empty($this->exclude_category) OR
!empty($this->exclude_tag_id) OR
!empty($this->exclude_tag) OR
!empty($this->exclude_post_id) OR
!empty($this->include_template) OR
!empty($this->include_category_id) OR
!empty($this->include_category) OR
!empty($this->include_tag_id) OR
!empty($this->include_tag) OR
!empty($this->include_post_id)
)
{
if (!empty($this->exclude_template))
{
if (in_array($template_file,$this->exclude_template))
{
$can_output = FALSE;
}
}

if (!empty($this->exclude_category_id))
{
foreach ($categories as $cat)
{
if (in_array($cat->term_id,$this->exclude_category_id))
{
$can_output = FALSE;
break;
}
}
}

if (!empty($this->exclude_category))
{
foreach ($categories as $cat)
{
if
(
in_array($cat->slug,$this->exclude_category) OR
in_array($cat->name,$this->exclude_category)
)
{
$can_output = FALSE;
break;
}
}
}

if (!empty($this->exclude_tag_id))
{
foreach ($tags as $tag)
{
if (in_array($tag->term_id,$this->exclude_tag_id))
{
$can_output = FALSE;
break;
}
}
}

if (!empty($this->exclude_tag))
{
foreach ($tags as $tag)
{
if
(
in_array($tag->slug,$this->exclude_tag) OR
in_array($tag->name,$this->exclude_tag)
)
{
$can_output = FALSE;
break;
}
}
}

if (!empty($this->exclude_post_id))
{
if (in_array($post_id,$this->exclude_post_id))
{
$can_output = FALSE;
}
}

// excludes are not set use "include only" mode

if
(
empty($this->exclude_template) AND
empty($this->exclude_category_id) AND
empty($this->exclude_category) AND
empty($this->exclude_tag_id) AND
empty($this->exclude_tag) AND
empty($this->exclude_post_id)
)
{
$can_output = FALSE;
}

if (!empty($this->include_template))
{
if (in_array($template_file,$this->include_template))
{
$can_output = TRUE;
}
}

if (!empty($this->include_category_id))
{
foreach ($categories as $cat)
{
if (in_array($cat->term_id,$this->include_category_id))
{
$can_output = TRUE;
break;
}
}
}

if (!empty($this->include_category))
{
foreach ($categories as $cat)
{
if
(
in_array($cat->slug,$this->include_category) OR
in_array($cat->name,$this->include_category)
)
{
$can_output = TRUE;
break;
}
}
}

if (!empty($this->include_tag_id))
{
foreach ($tags as $tag)
{
if (in_array($tag->term_id,$this->include_tag_id))
{
$can_output = TRUE;
break;
}
}
}

if (!empty($this->include_tag))
{
foreach ($tags as $tag)
{
if
(
in_array($tag->slug,$this->include_tag) OR
in_array($tag->name,$this->include_tag)
)
{
$can_output = TRUE;
break;
}
}
}

if (!empty($this->include_post_id))
{
if (in_array($post_id,$this->include_post_id))
{
$can_output = TRUE;
}
}
}

$post_type = WPAlchemy_MetaBox::_get_current_post_type();

if (isset($post_type) AND ! in_array($post_type, $this->types))
{
$can_output = FALSE;
}

// filter: output (can_output)
if ($this->has_filter('output'))
{
$can_output = $this->apply_filters('output', $post_id);
}

return $can_output;
}

/**
* Used to insert global STYLE or SCRIPT tags into the head, called on
* WordPress admin_footer action.
*
* @static
* @since 1.3
* @access private
* @see _global_foot()
*/
function _global_head()
{
// must be creating or editing a post or page
if ( ! WPAlchemy_MetaBox::_is_post() AND ! WPAlchemy_MetaBox::_is_page()) return;

// todo: you're assuming people will want to use this exact functionality
// consider giving a developer access to change this via hooks/callbacks

// include javascript for special functionality
?><style type="text/css"> .wpa_group.tocopy { display:none; } </style>
<script type="text/javascript">
/* <![CDATA[ */
jQuery(function($)
{
$(document).click(function(e)
{
var elem = $(e.target);

if (elem.attr('class') && elem.filter('[class*=dodelete]').length)
{
e.preventDefault();

var p = elem.parents('.postbox'); /*wp*/

var the_name = elem.attr('class').match(/dodelete-([a-zA-Z0-9_-]*)/i);

the_name = (the_name && the_name[1]) ? the_name[1] : null ;

/* todo: expose and allow editing of this message */
if (confirm('This action can not be undone, are you sure?'))
{
if (the_name)
{
$('.wpa_group-'+ the_name, p).not('.tocopy').remove();
}
else
{
elem.parents('.wpa_group').remove();
}

the_name = elem.parents('.wpa_group').attr('class').match(/wpa_group-([a-zA-Z0-9_-]*)/i)[1];

checkLoopLimit(the_name);

$.wpalchemy.trigger('wpa_delete');
}
}
});

$('[class*=docopy-]').click(function(e)
{
e.preventDefault();

var p = $(this).parents('.postbox'); /*wp*/

var the_name = $(this).attr('class').match(/docopy-([a-zA-Z0-9_-]*)/i)[1];

var the_group = $('.wpa_group-'+ the_name +':first.tocopy', p);
var the_clone = the_group.clone().removeClass('tocopy last');

var the_props = ['name', 'id', 'for', 'class'];

the_group.find('*').each(function(i, elem)
{
for (var j = 0; j < the_props.length; j++)
{
var the_prop = $(elem).attr(the_props[j]);

if (the_prop)
{
var the_match = the_prop.match(/\[(\d+)\]/i);

if (the_match)
{
the_prop = the_prop.replace(the_match[0],'['+ (+the_match[1]+1) +']');

$(elem).attr(the_props[j], the_prop);
}

the_match = null;

// todo: this may prove to be too broad of a search
the_match = the_prop.match(/n(\d+)/i);

if (the_match)
{
the_prop = the_prop.replace(the_match[0], 'n' + (+the_match[1]+1));

$(elem).attr(the_props[j], the_prop);
}
}
}
});

if ($(this).hasClass('ontop'))
{
$('.wpa_group-'+ the_name +':first', p).before(the_clone);
}
else
{
the_group.before(the_clone);
}

checkLoopLimit(the_name);

$.wpalchemy.trigger('wpa_copy', [the_clone]);
});

function checkLoopLimit(name)
{
var elem = $('.docopy-' + name);

var the_class = $('.wpa_loop-' + name).attr('class');

if (the_class)
{
var the_match = the_class.match(/wpa_loop_limit-([0-9]*)/i);

if (the_match)
{
var the_limit = the_match[1];

if ($('.wpa_group-' + name).not('.wpa_group.tocopy').length >= the_limit)
{
elem.hide();
}
else
{
elem.show();
}
}
}
}
/* do an initial limit check, show or hide buttons */
$('[class*=docopy-]').each(function()
{
var the_name = $(this).attr('class').match(/docopy-([a-zA-Z0-9_-]*)/i)[1];

checkLoopLimit(the_name);
});
});
/* ]]> */
</script>
<?php
}

/**
* Used to insert global SCRIPT tags into the footer, called on WordPress
* admin_footer action.
*
* @static
* @since 1.3
* @access private
* @see _global_head()
*/
function _global_foot()
{
// must be creating or editing a post or page
if ( ! WPAlchemy_MetaBox::_is_post() AND ! WPAlchemy_MetaBox::_is_page()) return;

?>
<script type="text/javascript">
/* <![CDATA[ */
(function($){ /* not using jQuery ondomready, code runs right away in footer */

/* use a global dom element to attach events to */
$.wpalchemy = $('<div></div>').attr('id','wpalchemy').appendTo('body');

})(jQuery);
/* ]]> */
</script>
<?php
}

/**
* Gets the meta data for a meta box
*
* @since 1.0
* @access public
* @param int $post_id optional post ID for which to retrieve the meta data
* @return array
* @see _meta
*/
function the_meta($post_id = NULL)
{
return $this->_meta($post_id);
}

/**
* Gets the meta data for a meta box
*
* Internal method calls will typically bypass the data retrieval and will
* immediately return the current meta data
*
* @since 1.3
* @access private
* @param int $post_id optional post ID for which to retrieve the meta data
* @param bool $internal optional boolean if internally calling
* @return array
* @see the_meta()
*/
function _meta($post_id = NULL, $internal = FALSE)
{
if ( ! is_numeric($post_id))
{
if ($internal AND $this->current_post_id)
{
$post_id = $this->current_post_id;
}
else
{
global $post;

$post_id = $post->ID;
}
}

// this allows multiple internal calls to _meta() without having to fetch data everytime
if ($internal AND !empty($this->meta) AND $this->current_post_id == $post_id) return $this->meta;

$this->current_post_id = $post_id;

// WPALCHEMY_MODE_ARRAY

$meta = get_post_meta($post_id, $this->id, TRUE);

// WPALCHEMY_MODE_EXTRACT

$fields = get_post_meta($post_id, $this->id . '_fields', TRUE);

if ( ! empty($fields) AND is_array($fields))
{
$meta = array();

foreach ($fields as $field)
{
$field_noprefix = preg_replace('/^' . $this->prefix . '/i', '', $field);
$meta[$field_noprefix] = get_post_meta($post_id, $field, TRUE);
}
}

$this->meta = $meta;

return $this->meta;
}

// user can also use the_ID(), php functions are case-insensitive
/**
* @since 1.0
* @access public
*/
function the_id()
{
echo $this->get_the_id();
}

/**
* @since 1.0
* @access public
*/
function get_the_id()
{
return $this->id;
}

/**
* @since 1.0
* @access public
*/
function the_field($n, $hint = NULL)
{
if ($this->in_loop) $this->subname = $n;
else $this->name = $n;

$this->hint = $hint;
}

/**
* @since 1.0
* @access public
*/
function have_value($n = NULL)
{
if ($this->get_the_value($n)) return TRUE;

return FALSE;
}

/**
* @since 1.0
* @access public
*/
function the_value($n = NULL)
{
echo $this->get_the_value($n);
}

/**
* @since 1.0
* @access public
*/
function get_the_value($n = NULL, $collection = FALSE)
{
$this->_meta(NULL, TRUE);

$value = null;

if ($this->in_loop)
{
if(isset($this->meta[$this->name]))
{
$n = is_null($n) ? $this->subname : $n ;

if(!is_null($n))
{
if ($collection)
{
if(isset($this->meta[$this->name][$this->current]))
{
$value = $this->meta[$this->name][$this->current];
}
}
else
{
if(isset($this->meta[$this->name][$this->current][$n]))
{
$value = $this->meta[$this->name][$this->current][$n];
}
}
}
else
{
if ($collection)
{
if(isset($this->meta[$this->name]))
{
$value = $this->meta[$this->name];
}
}
else
{
if(isset($this->meta[$this->name][$this->current]))
{
$value = $this->meta[$this->name][$this->current];
}
}
}
}
}
else
{
$n = is_null($n) ? $this->name : $n ;

if(isset($this->meta[$n]))
{
$value = $this->meta[$n];
}
}

if (is_string($value) || is_numeric($value))
{
if ($this->in_template)
{
return htmlentities($value, ENT_QUOTES, 'UTF-8');
}
else
{
// http://wordpress.org/support/topic/call-function-called-by-embed-shortcode-direct
// http://phpdoc.wordpress.org/trunk/WordPress/Embed/WP_Embed.html#run_shortcode

global $wp_embed;

return do_shortcode($wp_embed->run_shortcode($value));
}
}
else
{
// value can sometimes be an array
return $value;
}
}

/**
* @since 1.0
* @access public
*/
function the_name($n = NULL)
{
echo $this->get_the_name($n);
}

/**
* @since 1.0
* @access public
*/
function get_the_name($n = NULL)
{
if (!$this->in_template AND $this->mode == WPALCHEMY_MODE_EXTRACT)
{
return $this->prefix . str_replace($this->prefix, '', is_null($n) ? $this->name : $n);
}

if ($this->in_loop)
{
$n = is_null($n) ? $this->subname : $n ;

if (!is_null($n)) $the_field = $this->id . '[' . $this->name . '][' . $this->current . '][' . $n . ']' ;

else $the_field = $this->id . '[' . $this->name . '][' . $this->current . ']' ;
}
else
{
$n = is_null($n) ? $this->name : $n ;

$the_field = $this->id . '[' . $n . ']';
}

$hints = array
(
WPALCHEMY_FIELD_HINT_CHECKBOX_MULTI,
WPALCHEMY_FIELD_HINT_SELECT_MULTI,
WPALCHEMY_FIELD_HINT_SELECT_MULTIPLE,
);

if (in_array($this->hint, $hints))
{
$the_field .= '[]';
}

return $the_field;
}

/**
* @since 1.1
* @access public
*/
function the_index()
{
echo $this->get_the_index();
}

/**
* @since 1.1
* @access public
*/
function get_the_index()
{
return $this->in_loop ? $this->current : 0 ;
}

/**
* @since 1.0
* @access public
*/
function is_first()
{
if ($this->in_loop AND $this->current == 0) return TRUE;

return FALSE;
}

/**
* @since 1.0
* @access public
*/
function is_last()
{
if ($this->in_loop AND ($this->current+1) == $this->length) return TRUE;

return FALSE;
}

/**
* Used to check if a value is a match
*
* @since 1.1
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @return bool
* @see is_value()
*/
function is_value($n, $v = NULL)
{
if (is_null($v))
{
$the_value = $this->get_the_value();

$v = $n;
}
else
{
$the_value = $this->get_the_value($n);
}

if($v == $the_value) return TRUE;

return FALSE;
}

/**
* Used to check if a value is selected, useful when working with checkbox,
* radio and select values.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @return bool
* @see is_value()
*/
function is_selected($n, $v = NULL)
{
if (is_null($v))
{
$the_value = $this->get_the_value(NULL);

$v = $n;
}
else
{
$the_value = $this->get_the_value($n);
}

if (is_array($the_value))
{
if (in_array($v, $the_value)) return TRUE;
}
elseif($v == $the_value)
{
return TRUE;
}

return FALSE;
}

/**
* Prints the current state of a checkbox field and should be used inline
* within the INPUT tag.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @see get_the_checkbox_state()
*/
function the_checkbox_state($n, $v = NULL)
{
echo $this->get_the_checkbox_state($n, $v);
}

/**
* Returns the current state of a checkbox field, the returned string is
* suitable to be used inline within the INPUT tag.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @return string suitable to be used inline within the INPUT tag
* @see the_checkbox_state()
*/
function get_the_checkbox_state($n, $v = NULL)
{
if ($this->is_selected($n, $v)) return ' checked="checked"';
}

/**
* Prints the current state of a radio field and should be used inline
* within the INPUT tag.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @see get_the_radio_state()
*/
function the_radio_state($n, $v = NULL)
{
echo $this->get_the_checkbox_state($n, $v);
}

/**
* Returns the current state of a radio field, the returned string is
* suitable to be used inline within the INPUT tag.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @return string suitable to be used inline within the INPUT tag
* @see the_radio_state()
*/
function get_the_radio_state($n, $v = NULL)
{
return $this->get_the_checkbox_state($n, $v);
}

/**
* Prints the current state of a select field and should be used inline
* within the SELECT tag.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @see get_the_select_state()
*/
function the_select_state($n, $v = NULL)
{
echo $this->get_the_select_state($n, $v);
}

/**
* Returns the current state of a select field, the returned string is
* suitable to be used inline within the SELECT tag.
*
* @since 1.3
* @access public
* @param string $n the field name to check or the value to check for (if the_field() is used prior)
* @param string $v optional the value to check for
* @return string suitable to be used inline within the SELECT tag
* @see the_select_state()
*/
function get_the_select_state($n, $v = NULL)
{
if ($this->is_selected($n, $v)) return ' selected="selected"';
}

/**
* @since 1.1
* @access public
*/
function the_group_open($t = 'div')
{
echo $this->get_the_group_open($t);
}

/**
* @since 1.1
* @access public
*/
function get_the_group_open($t = 'div')
{
$this->group_tag = $t;

$loop_open = NULL;

$loop_open_classes = array('wpa_loop', 'wpa_loop-' . $this->name);

$css_class = array('wpa_group', 'wpa_group-'. $this->name);

if ($this->is_first())
{
array_push($css_class, 'first');

$loop_open = '<div class="wpa_loop">';

if (isset($this->_loop_data->limit))
{
array_push($loop_open_classes, 'wpa_loop_limit-' . $this->_loop_data->limit);
}

$loop_open = '<div id="wpa_loop-'. $this->name .'" class="' . implode(' ', $loop_open_classes) . '">';
}

if ($this->is_last())
{
array_push($css_class, 'last');

if ($this->in_loop == 'multi')
{
array_push($css_class, 'tocopy');
}
}

return $loop_open . '<' . $t . ' class="'. implode(' ', $css_class) . '">';
}

/**
* @since 1.1
* @access public
*/
function the_group_close()
{
echo $this->get_the_group_close();
}

/**
* @since 1.1
* @access public
*/
function get_the_group_close()
{
$loop_close = NULL;

if ($this->is_last())
{
$loop_close = '</div>';
}

return '</' . $this->group_tag . '>' . $loop_close;
}

/**
* @since 1.1
* @access public
*/
function have_fields_and_multi($n, $options = NULL)
{
if (is_array($options))
{
// use as stdClass object
$options = (object)$options;

$length = @$options->length;

$this->_loop_data->limit = @$options->limit;
}
else
{
// backward compatibility (bc)
$length = $options;
}

$this->_meta(NULL, TRUE);

$this->in_loop = 'multi';

return $this->_loop($n, $length, 2);
}

/**
* @deprecated
* @since 1.0
* @access public
*/
function have_fields_and_one($n)
{
$this->_meta(NULL, TRUE);
$this->in_loop = 'single';
return $this->_loop($n,NULL,1);
}

/**
* @since 1.0
* @access public
*/
function have_fields($n,$length=NULL)
{
$this->_meta(NULL, TRUE);
$this->in_loop = 'normal';
return $this->_loop($n,$length);
}

/**
* @since 1.0
* @access private
*/
function _loop($n,$length=NULL,$and_one=0)
{
if (!$this->in_loop)
{
$this->in_loop = TRUE;
}

$this->name = $n;

$cnt = count(!empty($this->meta[$n])?$this->meta[$n]:NULL);

$length = is_null($length) ? $cnt : $length ;

if ($this->in_loop == 'multi' AND $cnt > $length) $length = $cnt;

$this->length = $length;

if ($this->in_template AND $and_one)
{
if ($length == 0)
{
$this->length = $and_one;
}
else
{
$this->length = $length+1;
}
}

$this->current++;

if ($this->current < $this->length)
{
$this->subname = NULL;

$this->fieldtype = NULL;

return TRUE;
}
else if ($this->current == $this->length)
{
$this->name = NULL;

$this->current = -1;
}

$this->in_loop = FALSE;

$this->_loop_data = new stdClass;

return FALSE;
}

/**
* @since 1.0
* @access private
*/
function _save($post_id)
{
/**
* note: the "save_post" action fires for saving revisions and post/pages,
* when saving a post this function fires twice, once for a revision save,
* and again for the post/page save ... the $post_id is different for the
* revision save, this means that "get_post_meta()" will not work if trying
* to get values for a revision (as it has no post meta data)
* see http://alexking.org/blog/2008/09/06/wordpress-26x-duplicate-custom-field-issue
*
* why let the code run twice? wordpress does not currently save post meta
* data per revisions (I think it should, so users can do a complete revert),
* so in the case that this functionality changes, let it run twice
*/

$real_post_id = isset($_POST['post_ID']) ? $_POST['post_ID'] : NULL ;

// check autosave
if (defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE AND !$this->autosave) return $post_id;

// make sure data came from our meta box, verify nonce
$nonce = isset($_POST[$this->id.'_nonce']) ? $_POST[$this->id.'_nonce'] : NULL ;
if (!wp_verify_nonce($nonce, $this->id)) return $post_id;

// check user permissions
if ($_POST['post_type'] == 'page')
{
if (!current_user_can('edit_page', $post_id)) return $post_id;
}
else
{
if (!current_user_can('edit_post', $post_id)) return $post_id;
}

// authentication passed, save data

$new_data = isset( $_POST[$this->id] ) ? $_POST[$this->id] : NULL ;

WPAlchemy_MetaBox::clean($new_data);

if (empty($new_data))
{
$new_data = NULL;
}

// filter: save
if ($this->has_filter('save'))
{
$new_data = $this->apply_filters('save', $new_data, $real_post_id);

/**
* halt saving
* @since 1.3.4
*/
if (FALSE === $new_data) return $post_id;

WPAlchemy_MetaBox::clean($new_data);
}

// get current fields, use $real_post_id (checked for in both modes)
$current_fields = get_post_meta($real_post_id, $this->id . '_fields', TRUE);

if ($this->mode == WPALCHEMY_MODE_EXTRACT)
{
$new_fields = array();

if (is_array($new_data))
{
foreach ($new_data as $k => $v)
{
$field = $this->prefix . $k;

array_push($new_fields,$field);

$new_value = $new_data[$k];

if (is_null($new_value))
{
delete_post_meta($post_id, $field);
}
else
{
update_post_meta($post_id, $field, $new_value);
}
}
}

$diff_fields = array_diff((array)$current_fields,$new_fields);

if (is_array($diff_fields))
{
foreach ($diff_fields as $field)
{
delete_post_meta($post_id,$field);
}
}

delete_post_meta($post_id, $this->id . '_fields');

if ( ! empty($new_fields))
{
add_post_meta($post_id,$this->id . '_fields', $new_fields, TRUE);
}

// keep data tidy, delete values if previously using WPALCHEMY_MODE_ARRAY
delete_post_meta($post_id, $this->id);
}
else
{
if (is_null($new_data))
{
delete_post_meta($post_id, $this->id);
}
else
{
update_post_meta($post_id, $this->id, $new_data);
}

// keep data tidy, delete values if previously using WPALCHEMY_MODE_EXTRACT
if (is_array($current_fields))
{
foreach ($current_fields as $field)
{
delete_post_meta($post_id, $field);
}

delete_post_meta($post_id, $this->id . '_fields');
}
}

// action: save
if ($this->has_action('save'))
{
$this->do_action('save', $new_data, $real_post_id);
}

return $post_id;
}

/**
* Cleans an array, removing blank ('') values
*
* @static
* @since 1.0
* @access public
* @param array the array to clean (passed by reference)
*/
function clean(&$arr)
{
if (is_array($arr))
{
foreach ($arr as $i => $v)
{
if (is_array($arr[$i]))
{
WPAlchemy_MetaBox::clean($arr[$i]);

if (!count($arr[$i]))
{
unset($arr[$i]);
}
}
else
{
if ('' == trim($arr[$i]) OR is_null($arr[$i]))
{
unset($arr[$i]);
}
}
}

if (!count($arr))
{
$arr = array();
}
else
{
$keys = array_keys($arr);

$is_numeric = TRUE;

foreach ($keys as $key)
{
if (!is_numeric($key))
{
$is_numeric = FALSE;
break;
}
}

if ($is_numeric)
{
$arr = array_values($arr);
}
}
}
}
}

/* eof */
Something went wrong with that request. Please try again.