public
Last active

Wordpress Metabox, stand alone class for multiple metabox abilities

  • Download Gist
example.php
PHP
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
<?php
 
/*
Here's a couple of metaboxes that I've recently created using this system
*/
 
 
$subpostings = redrokk_metabox_class::getInstance('subpostings', array(
'title' => '(optional) Subscription for Postings',
'description' => "As an optional feature, you have a complete api at your disposal which will allow you the ability to offer and manage member posts. In addition to these settings, you may need to create the associated pages for accepting posts from your frontend.",
'_object_types' => 'page',
'priority' => 'high',
 
 
//this combination allows you to include the metabox on a single post,
//or exclude a single post
'include_exclude' => true,
'object_ids' => 306,
 
'_fields' => array(
array(
'name' => 'Maximum Posts',
'id' => 'max_posts',
'type' => 'text',
'class' => 'small-text',
'desc' => "The maximum number of posts that this subscriber is allowed to make.
<br/>-1 allows an infinite number of posts
<br/> 0 does not allow the subscriber to post",
'default' => '0',
),
array(
'name' => 'New Posting Defaults',
'type' => 'title',
),
array(
'name' => 'New Post Email',
'id' => 'new_post_email',
'type' => 'checkbox',
'desc' => "Would you like an email when a subscriber makes a new post?"
),
array(
'name' => 'Post Type',
'id' => 'postnew__post_type',
'type' => 'post_type',
),
array(
'name' => 'New Post Status',
'id' => 'postnew__post_status',
'type' => 'post_status',
'desc' => "When a subscriber makes a new post it will have this status",
),
array(
'name' => 'Default Category',
'id' => 'postnew__post_category',
'type' => 'category',
),
array(
'name' => 'Tagged with',
'id' => 'postnew__tags_input',
'type' => 'text',
'desc' => "Leave a comma deliminated list of tags to add to new member posts"
),
array(
'name' => 'Edit Posting Defaults',
'type' => 'title',
),
array(
'name' => 'Edit Post Email',
'id' => 'edit_post_email',
'type' => 'checkbox',
'desc' => "Would you like an email when a subscriber makes an edit?"
),
array(
'name' => 'Can User Edit?',
'id' => 'can_user_edit',
'type' => 'checkbox',
'desc' => "Leaving unchecked will not allow users to edit their posts"
),
array(
'name' => 'Edit Post Status',
'id' => 'postedit__post_status',
'type' => 'post_status',
'desc' => "When a subscriber edits their posts, this will be the new status",
),
array(
'name' => 'Tagged with',
'id' => 'postedit__tags_input',
'type' => 'text',
'desc' => "Leave a comma deliminated list of tags to add to new member posts"
),
)
));
$subroles = redrokk_metabox_class::getInstance('subroles', array(
'title' => '(optional) Role Management',
'description' => "As an optional feature, you can use these settings to manage the users roles during certain subscription triggers.",
'_object_types' => $sub,
'priority' => 'high',
'_fields' => array(
array(
'name' => 'Role',
'id' => 'role',
'type' => 'role',
'desc' => "New subscribers will be assigned to this role.",
),
array(
'name' => 'Expired to Role',
'id' => 'downgrade_role',
'type' => 'role',
'desc' => "Terminated subscriptions will be assigned to this role.",
),
)
));
$subdisplay = redrokk_metabox_class::getInstance('subdisplay', array(
'title' => 'Subscription Listing Preview',
'_object_types' => 'post',
'priority' => 'default',
'callback' => 'ms_subdisplay', //use a valid callback to display a custom template
));
 
 
 
//some example fields to use in your form
$metabox->set('_fields', array(
array(
'name' => 'Title',
'type' => 'title',
),
array(
'name' => 'Title',
'desc' => '',
'id' => 'title',
'type' => 'text',
'std' => ''
),
array(
'name' => 'image',
'desc' => '',
'id' => 'imagefile',
'type' => 'image',
'std' => ''
),
array(
'name' => 'Textarea',
'desc' => 'Enter big text here',
'id' => 'textarea_id',
'type' => 'textarea',
'std' => 'Default value 2'
),
array(
'name' => 'Select box',
'id' => 'select_id',
'type' => 'select',
'options'=> array(
'value1' => 'Value 1',
'value2' => 'Value 2',
'value3' => 'Value 3',
'value4' => 'Value 4',
)
),
array(
'name' => 'Radio',
'id' => 'radio_id',
'type' => 'radio',
'value' => 'test',
'desc' => 'Check this box if you want its value saved',
),
array(
'name' => '',
'id' => 'radio_id',
'type' => 'radio',
'value' => 'test2',
'desc' => 'Check this box if you want its value saved',
),
array(
'name' => 'Checkbox',
'id' => 'checkbox_id',
'type' => 'checkbox',
'desc' => 'Check this box if you want its value saved',
),
));
metabox.class.php
PHP
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
<?php
/**
* @Author Anonymous
* @link http://www.redrokk.com
* @Package Wordpress
* @SubPackage RedRokk Library
*
* @version 2.0
*/
 
//security
defined('ABSPATH') or die('You\'re not supposed to be here.');
 
/**
*
*
* @author Anonymous
* @example
 
$gallery = redrokk_metabox_class::getInstance('gallery');
 
*/
if (!class_exists('redrokk_metabox_class')):
class redrokk_metabox_class
{
/**
* HTML 'id' attribute of the edit screen section
*
* @var string
*/
var $_id;
/**
* Save the form fields here that will be displayed to the user
*
* @var array
*/
protected $_fields;
protected $fields;
/**
* Title of the edit screen section, visible to user
* Default: None
*
* @var string
*/
var $title;
/**
* Function that prints out the HTML for the edit screen section. Pass
* function name as a string. Within a class, you can instead pass an
* array to call one of the class's methods. See the second example under
* Example below.
* Default: None
*
* @var callback
*/
var $callback = null;
/**
* The part of the page where the edit screen section should be shown
* ('normal', 'advanced', or 'side'). (Note that 'side' doesn't exist before 2.7)
* Default: 'advanced'
*
* @var string
*/
var $context = 'normal';
/**
* The priority within the context where the boxes should show
* ('high', 'core', 'default' or 'low')
* Default: 'default'
*
* @var string
*/
var $priority = 'default';
/**
* Arguments to pass into your callback function. The callback will receive the
* $post object and whatever parameters are passed through this variable.
* Default: null
*
* @var array
*/
var $callback_args;
/**
* Prebuilt metaboxes can be activated by using this type
* Default: default
*
* (options:)
* default
* images
*
* @var string
*/
var $_type;
/**
*
* @var unknown_type
*/
var $_category_name;
/**
* The type of Write screen on which to show the edit screen section
* ('post', 'page', 'link', or 'custom_post_type' where custom_post_type
* is the custom post type slug)
* Default: None
*
* @var array
*/
var $_object_types = array();
/**
* Whether or not to make the fields available as wp-options
*
* @var bool
*/
var $_isAdminPage = false;
/**
* Constructor.
*
*/
function __construct( $options = array() )
{
//initializing
$this->setProperties($options);
$this->setOptionHooks();
if (!$this->callback) {
$this->callback = array(&$this, 'show');
}
if (!$this->title) {
$this->title = ucfirst($this->_id);
}
//registering this metabox
add_action( 'add_meta_boxes', array(&$this, '_register') );
// backwards compatible (before WP 3.0)
// add_action( 'admin_init', array($this, '_register'), 1 );
add_action( 'save_post', array(&$this, '_save') );
add_filter( 'wp_redirect', array(&$this, '_redirectIntervention'), 40, 1 );
}
/**
* Method properly inturprets the given parameter and sets it accordingly
*
* @param string|object $value
*/
function setObjectTypes( $value )
{
if (is_a($value, 'redrokk_post_class')) {
$value = $value->_post_type;
}
if (is_a($value, 'redrokk_admin_class')) {
$value = $value->id;
$this->_isAdminPage = $value;
}
if (is_array($value)) {
foreach($value as $v)
$this->_object_types[] = $v;
return $this;
}
$this->_object_types[] = $value;
return $this;
}
/**
* Method is designed to return the currently visible post type
*/
function getCurrentPostType()
{
$post_type = false;
if (isset($_REQUEST['post_type'])) {
$post_type = $_REQUEST['post_type'];
}
elseif (isset($_REQUEST['post'])) {
$post = get_post($_REQUEST['post']);
$post_type = $post->post_type;
}
elseif (isset($_REQUEST['page'])) {
$post_type = $_REQUEST['page'];
}
return $post_type;
}
/**
* Method properly prepares the metabox type by binding the necessary hooks
*
* @param mixed $value
*/
function setType( $value = 'default' )
{
$this->_type = $value;
switch ($this->_type)
{
default:
case 'default':
add_action('metabox-show-'.$this->_id, array(&$this, '_renderForm'), 20 );
add_action('metabox-save-'.$this->_id, array(&$this, 'saveAsPostMeta'), 10, 2);
break;
case 'image':
case 'images':
$this->_fields = array(
array(
'name' => 'New Image',
'type' => 'title',
),
array(
'name' => 'Image Title',
'id' => $this->_id.'_post_title',
'type' => 'text',
),
array(
'name' => 'Description',
'id' => $this->_id.'_post_content',
'type' => 'textarea',
),
array(
'name' => 'Image File',
'id' => $this->_id.'_image',
'type' => 'image',
),
array(
'name' => '_metaboxed',
'id' => $this->_id.'_metaboxed',
'type' => 'hidden',
'default' => $this->_id
),
array(
'name' => 'Save Image',
'type' => 'submit',
),
);
add_action('metabox-show-'.$this->_id, array(&$this, '_renderListImageAttachments'), 20 );
add_action('metabox-show-'.$this->_id, array(&$this, '_renderForm'), 20 );
add_action('metabox-save-'.$this->_id, array(&$this, 'saveAsAttachment'), 1, 2);
break;
case 'video':
case 'videos':
$this->_fields = array(
array(
'name' => 'New Video',
'type' => 'title',
),
array(
'name' => 'Video Title',
'id' => $this->_id.'_post_title',
'type' => 'text',
),
array(
'name' => 'Description',
'id' => $this->_id.'_post_content',
'type' => 'textarea',
),
array(
'name' => 'Video File',
'id' => $this->_id.'_image',
'type' => 'image',
),
array(
'name' => 'Video Link',
'id' => $this->_id.'_link',
'type' => 'text',
),
array(
'name' => '_videocat',
'id' => $this->_id.'_videocat',
'default'=>$this->getCategory(),
'type' => 'hidden',
),
array(
'name' => '_metaid',
'id' => $this->_id.'_metaid',
'type' => 'hidden',
),
array(
'name' => 'Save Video',
'type' => 'submit',
),
);
add_action('metabox-show-'.$this->_id, array(&$this, '_renderListAttachments'), 20 );
add_action('metabox-show-'.$this->_id, array(&$this, '_renderListVideoAttachments'), 20 );
add_action('metabox-show-'.$this->_id, array(&$this, '_renderForm'), 20 );
add_action('metabox-save-'.$this->_id, array(&$this, 'saveAsPostMeta'), 1, 2);
break;
}
}
/**
* Returns the category to use
*/
function getCategory()
{
return isset($this->_category_name)
? $this->_category_name
: '_videocat';
}
/**
* Method will save the posted content as an image attachment
*
*/
function saveAsAttachment( $source, $post_id )
{
if (empty($_FILES) || !isset($_REQUEST[$this->_id.'files'])) return $source;
// initializing
$property = $_REQUEST[$this->_id.'files'];
$post_data = array();
if (isset($source[$this->_id.'_post_title']) && $source[$this->_id.'_post_title']) {
$post_data['post_title'] = $source[$this->_id.'_post_title'];
}
if (isset($source[$this->_id.'_post_content']) && $source[$this->_id.'_post_content']) {
$post_data['post_content'] = $source[$this->_id.'_post_content'];
}
$id = media_handle_upload($property, $post_id, $post_data);
$source[$property] = $id;
$type = 'post';
if ($this->getCurrentPostType() && $this->getCurrentPostType() !='page') {
$type = $this->getCurrentPostType();
}
 
//saving the attachment ID to the taxonomy
if (!in_array($type, get_post_types(array('public' => false)))) {
$old = get_metadata($type, $post_id, $property, true);
if ($id && $id != $old) {
wp_delete_attachment( $old, true );
update_metadata($type, $post_id, $property, $id);
}
}
 
foreach ((array)$source as $property => $new)
{
//skip everything but the specially prefixed
if (strpos($property, $this->_id) !== 0) continue;
if (in_array($property, array(
$this->_id.'_post_title',
$this->_id.'_post_content',
))) continue;
$old = get_metadata($type, $id, $property, true);
if ($new && $new != $old) {
update_metadata($type, $id, $property, $new);
}
elseif (!$new) {
delete_metadata($type, $id, $property, $old);
}
}
return $source;
}
/**
* Method saves the data provided as post meta values
*
* @param array $source
* @param integer $post_id
*/
function saveAsPostMeta( $source, $post_id )
{
$type = 'post';
if (!$this->getCurrentPostType()) {
$type = $this->_table;
}
//save as a file
//if there's no FILES then we save as a meta
$source = $this->saveAsAttachment( $source, $post_id );
//get the ID of this meta set
$id = false;
if (isset($source[$this->_id.'_metaid']) && $source[$this->_id.'_metaid']) {
$id = $source[$this->_id.'_metaid'];
}
// if this is a built in metabox
if ($this->_type != 'default'
&& (!isset($source[$this->_id.'_image']) || !$source[$this->_id.'_image']))
return false;
// Saving only the specially prefixed items
foreach ((array)$source as $property => $new)
{
//skip everything but the specially prefixed
if (strpos($property, $this->_id) !== 0) continue;
//each meta set has it's own ID
$property = str_replace($this->_id, $this->_category_name.'_'.$id, $property);
$old = get_metadata($type, $post_id, $property, true);
if ($new && $new != $old) {
update_metadata($type, $post_id, $property, $new);
}
elseif (!$new) {
delete_metadata($type, $post_id, $property, $old);
}
}
// maybe there's a last id
if (!$id) {
if (!$id = get_metadata($type, $post_id, '_metaidlast', true)) {
$id = 0;
}
$id++;
update_metadata($type, $post_id, '_metaidlast', $id);
}
// saving all of the standard items
foreach ((array)$source as $property => $new)
{
//skip special properties that are prefixed with the id
if (strpos($property, $this->_id) === 0) continue;
$old = get_metadata($type, $post_id, $property, true);
update_metadata($type, $post_id, $property, $new);
// if ($new && $new != $old) {
// update_metadata($type, $post_id, $property, $new);
// }
// elseif (!$new) {
// delete_metadata($type, $post_id, $property, $old);
// }
}
return true;
}
/**
* Do something with the data entered
*
* @param integer $post_id
*/
function _save( $post_id )
{
//initializing
$post = get_post($post_id);
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset($_REQUEST[ get_class().$this->_id ]) )
return;
if ( !wp_verify_nonce( $_REQUEST[ get_class().$this->_id ], plugin_basename( __FILE__ ) ) )
return;
// this metabox is to be displayed for a certain object type only
if ( !in_array($post->post_type, $this->_object_types) )
return;
// Check permissions
if ( 'page' == $post->post_type )
{
if ( !current_user_can( 'edit_page', $post->ID ) )
return;
}
else
{
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
}
//saving the request data
if (!$this->_type) $this->setType();
do_action('metabox-save-'.$this->_id, $this->getRequestPostMetas(), $post->ID, $this );
return true;
}
/**
* Method returns the post meta
*
*/
function getRequestPostMetas()
{
$ignores = array('post_title', 'post_name', 'post_content', 'post_excerpt', 'post',
'post_status', 'post_type', 'post_author', 'ping_status', 'post_parent', 'message',
'post_category', 'comment_status', 'menu_order', 'to_ping', 'pinged', 'post_password',
'guid', 'post_content_filtered', 'import_id', 'post_date', 'post_date_gmt', 'tags_input',
'action');
$fields = array();
foreach ((array)$this->getFields() as $field) {
if (!array_key_exists('id', $field)) continue;
$fields[] = $field['id'];
}
$requests = $_REQUEST;
foreach ((array)$requests as $k => $request)
{
if ((!empty($fields) && !in_array($k, $fields))
|| (in_array($k, $ignores) || strpos($k, 'nounce') !== false))
{
unset($requests[$k]);
}
}
return apply_filters('metabox-requests-'.$this->_id, $requests);
}
/**
* Display the inner contents of the metabox
*
* @param object $post
*/
function show( $post )
{
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), get_class().$this->_id );
do_action('metabox-show-'.$this->_id, $this);
}
/**
*
*/
function getImages( $post_id = false )
{
if (!$post_id) {
global $post;
$post_id = $post->ID;
}
//
return get_children(array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type'=> 'image',
'meta_query' => array(
array(
'key' => $this->_id.'_metaboxed',
'value' => $this->_id,
'compare' => '='
)
)
));
}
/**
* Method displays a list of attached images
*
*/
function _renderListImageAttachments()
{
global $post, $current_screen;
$images = $this->getImages();
// no images to render
if (empty($images)) {
?><p>No images have been saved.</p><?php
// rendering the images
} else {
?>
<table class="wp-list-table form-table widefat" style="border:none;">
<?php foreach ((array)$images as $post_id => $image): ?>
<?php $image_attributes = wp_get_attachment_image_src( $image->ID, 'thumbnail' ); ?>
<tbody id="the-list">
<tr>
<th scope="row" style="width:20%!important">
<div style="padding:10px;background:whiteSmoke;">
<img src="<?php echo wp_get_attachment_thumb_url( $image->ID ); ?>" /></div>
</th>
<td>
<b><?php echo $image->post_title; ?></b>
<p><?php echo get_the_content($image->ID); ?></p>
<div class="row-actions">
<span class="inline">
<a href="<?php echo wp_nonce_url(
"media.php?attachment_id=$image->ID"
."&action=edit&_redirect="
.urlencode( $this->_currentPageURL() )
); ?>">
Edit</a> |
</span>
<span class="trash">
<a class="submitdelete"
onclick="return showNotice.warn();"
href="<?php echo wp_nonce_url(
"post.php?action=delete&_redirect="
.urlencode( $this->_currentPageURL() )
."&amp;post=$image->ID",
'delete-attachment_' . $image->ID ); ?>">
Delete Permanently</a> |
</span>
<span class="inline">
<a target="_blank"
href="<?php echo get_attachment_link($image->ID); ?>">
View</a>
</span>
</div>
</td>
</tr>
</tbody>
<?php endforeach; ?>
</table>
<?php
}
return;
}
/**
* Return a clean list of meta listings created by this system
*
* @param string $category
* $param object $post
*/
public static function getMetaListings( $category, $post = null )
{
// initializing
if ($post === NULL) {
global $post;
}
$custom = get_post_custom($post->ID);
$return = array();
//looping all values to build our return array
foreach((array)$custom as $property => $value)
{
$parts = explode('_',$property);
if (!isset($parts[0]) || !isset($parts[1]) || !isset($parts[2])) continue;
if ($parts[0] != $category) continue;
$pro = str_replace($parts[0].'_'.$parts[1].'_', '', $property);
$return[$parts[1]][$pro] = $value;
}
return $return;
}
/**
* Function removes a specific category meta
*
* @param string $category
* $param string $meta_id
* $param object $post
*/
public static function deleteMetaListing( $category, $meta_id, $post = null )
{
// initializing
if ($post === NULL) {
global $post;
}
$listings = redrokk_metabox_class::getMetaListings( $category, $post );
if (!isset($listings[$meta_id])) return false;
$type = 'post';
foreach((array)$listings[$meta_id] as $property => $value) {
$pro = $category.'_'.$meta_id.'_'.$property;
delete_metadata($type, $post->ID, $pro, $value[0]);
}
return true;
}
/**
* Method displays a list of meta attachments
*
*/
function _renderListAttachments()
{
global $post;
//delete action prior to pulling new listings
if (isset($_REQUEST['redrokkdelete']) && $_REQUEST['redrokkdelete']) {
redrokk_metabox_class::deleteMetaListing($this->_category_name, $_REQUEST['redrokkdelete'], $post);
}
//pull new listings
$metaListings = redrokk_metabox_class::getMetaListings($this->_category_name, $post);
if (!empty($metaListings)) {
?>
<table class="wp-list-table form-table widefat" style="border:none;">
<tbody id="the-list">
<?php foreach ((array)$metaListings as $meta_id => $video): ?>
<?php $video = apply_filters('redrokk_metabox_class::_renderListAttachments', $video, $meta_id);?>
<tr id="<?php echo $this->_category_name; ?>_<?php echo $meta_id; ?>">
<th scope="row" style="width:20%!important">
<div style="padding:10px;background:whiteSmoke;">
<?php if(isset($video['link'])) echo apply_filters('the_content', $video['link'][0]); ?>
</div>
</th>
<td>
<b><?php if(isset($video['post_title'])) echo $video['post_title'][0]; ?></b>
<p><?php if(isset($video['post_content'])) echo $video['post_content'][0]; ?></p>
<div class="row-actions">
<span class="inline">
<a href="#" id="edit_<?php echo $this->_category_name; ?>_<?php echo $meta_id; ?>">
Edit</a> |
</span>
<span class="trash">
<a class="submitdelete"
onclick="return showNotice.warn();"
href="<?php echo site_url( "wp-admin/post.php?post={$post->ID}"
."&action=edit"
."&redrokkdelete=$meta_id"
); ?>">
Delete Permanently</a>
</span>
</div>
<script type="application/javascript">
jQuery('#edit_<?php echo $this->_category_name; ?>_<?php echo $meta_id; ?>').click(function(){
var data = {
<?php
$data = array();
//making sure all fields will be cleared
foreach ((array)$this->getFields() as $field) {
if (!isset($field['id']) || !isset($field['type'])) continue;
if (!in_array($field['type'], array('text','file','image','textarea','hidden')))
continue;
$id = str_replace($this->_id.'_', '', $field['id']);
$data[$id] = "'$id':''";
}
//adding our values to the array
foreach((array)$video as $vp => $vv)
{
if (isset($vv[0])) $vv = $vv[0];
$data[$vp] = "'$vp':'$vv'";
}
//adding the meta ID to the array
$data[$id] = "'metaid':'$meta_id'";
echo implode(',',$data);
?>
};
jQuery.each(data, function(key, value){
jQuery('#<?php echo $this->_id; ?>_'+key).val( value );
});
return false;
});
</script>
</td>
<?php do_action('redrokk_metabox_class::_renderListAttachments::rows', $video, $meta_id, $this); ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
}
/**
* Method displays a list of attached videos
*
*/
function _renderListVideoAttachments()
{
global $post;
//pull new listings
$videos =& get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=video/mp4" );
// no images to render
if (!empty($videos)) {
?>
<table class="wp-list-table form-table widefat" style="border:none;">
<tbody id="the-list">
<?php foreach ((array)$videos as $post_id => $video): ?>
<?php $image_attributes = wp_get_attachment_link( $video->ID ); ?>
<tr>
<th scope="row" style="width: 140px">
<div style="padding:10px;background:whiteSmoke;">
<?php echo $image_attributes; ?>
</div>
</th>
<td>
<b><?php echo $video->post_title; ?></b>
<p><?php echo get_the_content($video->ID); ?></p>
<div class="row-actions">
<span class="inline">
<a href="<?php echo wp_nonce_url(
"media.php?attachment_id=$meta_id"
."&action=edit&_redirect="
.urlencode( $this->_currentPageURL() )
); ?>">
Edit</a> |
</span>
<span class="trash">
<a class="submitdelete"
onclick="return showNotice.warn();"
href="<?php echo wp_nonce_url(
"post.php?action=delete&_redirect="
.urlencode( $this->_currentPageURL() )
."&amp;post=$video->ID",
'delete-attachment_' . $video->ID ); ?>">
Delete Permanently</a>
</span>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
}
/**
*
*/
function getFields()
{
// initializing
if (!$this->fields)
{
$custom = get_post_custom($this->_id);
$defaults = array(
'name' => '',
'desc' => '',
'id' => '',
'type' => 'text',
'options' => array(),
'default' => '',
'value' => '',
'class' => '',
'multiple' => '',
'args' => array(
'hide_empty' => 0,
'name' => 'element_name',
'hierarchical' => true
),
'attributes' => '',
'value' => '',
);
foreach ((array)$this->_fields as $key => $field)
{
$field = wp_parse_args($field, $defaults);
$field['args'] = wp_parse_args($field['args'], $defaults['args']);
// GET PREDEFINED OPTIONS
if (empty($field['options']))
{
$method = 'get_options'.substr($field['type'],6);
if (method_exists($this, $method))
{
$field['options'] = call_user_method($method, $this) +$field['options'];
}
}
// SAVED VALUE
if (is_array($custom) && array_key_exists($field['id'], $custom))
{
if (isset($custom[$field['id']][0])) {
$field['value'] = esc_attr( $custom[$field['id']][0] );
} else {
$field['value'] = esc_attr( $custom[$field['id']] );
}
}
elseif ($field['default'] || is_bool($field['default']))
{
if (strtolower($field['default']) == 'true') {
$field['value'] = true;
}
elseif (strtolower($field['default']) == 'false') {
$field['value'] = false;
}
else {
$field['value'] = $field['default'];
}
}
else
{
// SELECT BOX
if (substr($field['type'],0,6) == 'select')
{
if (is_array($field['options']))
$field['value'] = key($field['options']);
}
// CHECKBOX TO BOOLEAN
elseif (in_array($field['type'], array('checkbox','radio')))
{
if (!$field['default']) {
$field['value'] = false;
}
}
}
// rebuild the fields
$this->fields[$key] = $field;
}
}
return $this->fields;
}
/**
* Method renders the form from any source
*
* @param array $fields
*/
function _renderForm()
{
//initializing
global $post;
$fields = $this->getFields();
// no fields to render
if (empty($fields)) {
?>
<p>No form fields have been defined. Use <pre>
$metabox->set('_fields', array(
array(
'name' => 'Title',
'type' => 'title',
),
array(
'name' => 'Title',
'desc' => '',
'id' => 'title',
'type' => 'text',
'std' => ''
),
array(
'name' => 'image',
'desc' => '',
'id' => 'imagefile',
'type' => 'image',
'std' => ''
),
array(
'name' => 'Textarea',
'desc' => 'Enter big text here',
'id' => 'textarea_id',
'type' => 'textarea',
'std' => 'Default value 2'
),
array(
'name' => 'Select box',
'id' => 'select_id',
'type' => 'select',
'options'=> array(
'value1' => 'Value 1',
'value2' => 'Value 2',
'value3' => 'Value 3',
'value4' => 'Value 4',
)
),
array(
'name' => 'Radio',
'id' => 'radio_id',
'type' => 'radio',
'value' => 'test',
'desc' => 'Check this box if you want its value saved',
),
array(
'name' => '',
'id' => 'radio_id',
'type' => 'radio',
'value' => 'test2',
'desc' => 'Check this box if you want its value saved',
),
array(
'name' => 'Checkbox',
'id' => 'checkbox_id',
'type' => 'checkbox',
'desc' => 'Check this box if you want its value saved',
),
));</pre>
</p>
<?php
// rendering the fields
} else {
?>
<table class="form-table">
<?php
do_action("{$this->_class}_before");
$custom = get_post_custom($this->_id);
foreach ((array)$fields as $field):
extract($field);
$field['args']['name'] = $element_name = $id;
// grabbing the meta value
if (array_key_exists($id, $custom)) {
if (isset($custom[$id][0]))
$meta = esc_attr( $custom[$id][0] );
else
$meta = esc_attr( $custom[$id] );
} else {
$meta = $default;
}
$id = sanitize_title($id);
if (array_key_exists('deleteattachment', $_GET)
&& $id == $_GET['fileproperty']
&& $meta == $_GET['deleteattachment'])
{
wp_delete_attachment( $_GET['deleteattachment'], $force_delete = true );
update_post_meta($post->ID, $id, '');
}
?>
<?php switch ($type){ default: ?>
<?php if (is_callable($type) && function_exists($type)) : ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<?php call_user_func($type, $args); ?>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; endif; ?>
<?php case 'text': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<input <?php echo $attributes ?>
id="<?php echo $id; ?>"
value="<?php echo $meta; ?>"
type="<?php echo $type; ?>"
name="<?php echo $id; ?>"
class="text large-text <?php echo $class; ?>" />
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'submit': ?>
<?php case 'button': ?>
<tr>
<td colspan="2">
<input <?php echo $attributes ?>
id="<?php echo $id; ?>"
value="<?php echo $name; ?>"
type="submit"
name="submit"
class="button-primary <?php echo $class; ?>" />
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'file': ?>
<?php case 'image': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<?php if ($meta && wp_get_attachment_url( $meta ) && !in_array($this->_type, array('image','images','video','videos'))): ?>
<?php echo wp_get_attachment_image( $meta ); ?>
<span><a href="<?php echo add_query_arg('fileproperty', $id, add_query_arg('deleteattachment', $meta, $this->_currentPageURL())); ?>">
Delete Image</a></span>
<?php else: ?>
<input type="hidden" name="<?php echo $this->_id; ?>files" value="<?php echo $id; ?>" />
<!-- first hidden input forces this item to be submitted when it is not checked -->
<input <?php echo $attributes ?>
id="<?php echo $id; ?>"
type="file"
name="<?php echo $id; ?>"
onChange="jQuery(this).closest('form').attr('enctype', 'multipart/form-data');"
class="<?php echo $class; ?>" />
<?php endif; ?>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'title': ?>
<tr>
<th colspan="2" scope="row">
<h3 <?php echo $attributes ?> style="border: 1px solid #ddd;
padding: 10px;
background: #eee;
border-radius: 2px;
color: #666;
margin: 0;"><?php echo $name; ?>
</h3>
</th>
</tr>
<?php break; ?>
<?php case 'checkbox': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<input type="hidden" name="<?php echo $id; ?>" value="" />
<!-- first hidden input forces this item to be submitted when it is not checked -->
<?php foreach ((array)$options as $_value => $_name): ?>
<input value="<?php echo $_value; ?>" type="checkbox" <?php echo $attributes ?>
name="<?php echo $element_name; ?>" id="<?php echo $id; ?>"
<?php echo $meta == $_value? 'checked="checked"' :''; ?>
class="<?php echo $class; ?>" />
<?php echo $_name; ?>
<?php endforeach; ?>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'radio': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<?php foreach ((array)$options as $_value => $_name): ?>
<input name="<?php echo $element_name; ?>" id="<?php echo $id; ?>"
value="<?php echo $_value; ?>" type="<?php echo $type; ?>"
<?php echo $meta == $_value?'checked="checked"' :''; ?>
<?php echo $attributes ?> class="<?php echo $class; ?>" />
<?php echo $_name; ?>
<?php endforeach; ?>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'textarea': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<textarea <?php echo $attributes ?>
id="<?php echo $id; ?>"
name="<?php echo $id; ?>"
class="large-text <?php echo $class; ?>"
><?php echo $meta; ?></textarea>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'wpeditor': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<?php wp_editor( $meta, $id, $settings = array() ); ?>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'select_capabilities': ?>
<?php case 'select_roles': ?>
<?php case 'select_menu': ?>
<?php case 'select_pages': ?>
<?php case 'select_users': ?>
<?php case 'select_categories': ?>
<?php case 'select': ?>
<tr>
<th scope="row" style="width:20%!important">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td>
<?php if ($type == 'select_categories'): ?>
<?php wp_dropdown_categories($args); ?>
<?php else: ?>
<select <?php echo $attributes ?>
id="<?php echo $id; ?>"
name="<?php echo $id; ?>"
class="<?php echo $class; ?>"
<?php echo $multiple ?"MULTIPLE SIZE='$multiple'" :''; ?>
><?php foreach ((array)$options as $_value => $_name): ?>
<option
value="<?php echo $_value; ?>"
<?php echo $meta == $_value ?' selected="selected"' :''; ?>
><?php echo $_name; ?></option>
<?php endforeach; ?></select>
<?php endif; ?>
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php break; ?>
<?php case 'hidden': ?>
<tr>
<td colspan="2">
<input <?php echo $attributes ?>
id="<?php echo $id; ?>"
value="<?php echo $meta ?$meta :$default; ?>"
type="<?php echo $type; ?>"
name="<?php echo $id; ?>"
style="visibility:hidden;" />
</td>
</tr>
<?php break; ?>
<?php case 'custom': ?>
<tr>
<td colspan="2">
<?php echo $desc.$default; ?>
</td>
</tr>
<?php } ?>
<?php endforeach; ?>
</table>
<?php
}
return $this;
}
 
/**
* Returns an options list of menus
*/
function get_options_pages()
{
// initializing
$options = array('0'=>' -- ');
$pages = get_pages(array('post_type' => 'page', 'post_status' => 'publish'));
 
foreach($pages as $page) {
$options[$page->ID] = $page->post_title;
}
 
return $options;
}
 
/**
* Returns an options list of menus
*/
function get_options_menu()
{
// initializing
$options = array('0'=>' -- ');
$menus = get_terms('nav_menu', array(
'hide_empty' => 0
));
 
foreach($menus as $menu) {
$options[$menu->slug] = $menu->name;
}
 
return $options;
}
 
/**
* Returns an options list of users
*/
function get_options_users()
{
// initializing
global $wpdb;
 
$options = array('0'=>' -- ');
$query = $wpdb->prepare("SELECT $wpdb->users.ID, $wpdb->users.display_name FROM $wpdb->users");
$results = $wpdb->get_results( $query );
 
foreach ((array)$results as $result)
{
$options[$result->ID] = $result->display_name;
}
 
return $options;
}
 
/**
* Returns an options list of capabilities
*/
function get_options_capabilities()
{
// initializing
global $wpdb;
 
$options = array();
$roles = get_option($wpdb->prefix . 'user_roles');
 
foreach ((array)$roles as $role)
{
if(!isset($role['capabilities'])) continue;
foreach ((array)$role['capabilities'] as $cap => $v)
{
$options[$role['name']."::$cap"] = $role['name']."::$cap";
}
}
 
return $options;
}
 
/**
* Returns an options list of roles
*/
function get_options_roles()
{
// initializing
global $wpdb;
 
$options = array(
'read' => 'Public'
);
$roles = get_option($wpdb->prefix . 'user_roles');
 
foreach ((array)$roles as $role)
{
$options[strtolower($role['name'])] = $role['name'];
}
 
return $options;
}
/**
* Adds a box to the main column on the Post and Page edit screens
*
*/
function _register()
{
// this metabox is to be displayed for a certain object type only
if (!empty($this->_object_types) && !in_array($this->getCurrentPostType(), $this->_object_types) )
return;
if (!$this->callback_args) {
$this->callback_args = $this;
}
// if the user has not already set the type of this metabox,
// then we need to do that now
if (!$this->_type) {
$this->setType();
}
add_meta_box(
$this->_id,
$this->title,
$this->callback,
$this->getCurrentPostType(),
$this->context,
$this->priority,
$this->callback_args
);
}
/**
* Method set's the hooks for the options creted by this metabox
*
*/
function setOptionHooks()
{
foreach ((array)$this->getFields() as $field)
{
if (!isset($field['id'])) continue;
//creating the callback for the admin page
$function = create_function('$default','
if (!$default) {
$default = "'. $field['value'] .'";
}
return redrokk_admin_class::getInstance("'.$this->_isAdminPage.'")
->getOption("'.$field['id'].'", $default, true);
');
add_filter("pre_option_{$field['id']}", $function, 20, 2);
}
}
/**
* Method redirects the user if we have added a request redirect
* in the url
*
* @param string $location
*/
function _redirectIntervention( $location )
{
if (isset($_GET['_redirect'])) {
$location = urldecode($_GET['_redirect']);
}
return $location;
}
/**
* Get the current page url
*/
function _currentPageURL()
{
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/**
* Method to bind an associative array or object to the JTable instance.This
* method only binds properties that are publicly accessible and optionally
* takes an array of properties to ignore when binding.
*
* @param mixed $src An associative array or object to bind to the JTable instance.
* @param mixed $ignore An optional array or space separated list of properties to ignore while binding.
*
* @return boolean True on success.
*
* @link http://docs.joomla.org/JTable/bind
* @since 11.1
*/
public function bind($src, $ignore = array())
{
// If the source value is not an array or object return false.
if (!is_object($src) && !is_array($src))
{
trigger_error('Bind failed as the provided source is not an array.');
return false;
}
 
// If the source value is an object, get its accessible properties.
if (is_object($src))
{
$src = get_object_vars($src);
}
 
// If the ignore value is a string, explode it over spaces.
if (!is_array($ignore))
{
$ignore = explode(' ', $ignore);
}
 
// Bind the source value, excluding the ignored fields.
foreach ($this->getProperties() as $k => $v)
{
// Only process fields not in the ignore array.
if (!in_array($k, $ignore))
{
if (isset($src[$k]))
{
$this->$k = $src[$k];
}
}
}
 
return true;
}
/**
* Set the object properties based on a named array/hash.
*
* @param mixed $properties Either an associative array or another object.
*
* @return boolean
*
* @since 11.1
*
* @see set()
*/
public function setProperties($properties)
{
if (is_array($properties) || is_object($properties))
{
foreach ((array) $properties as $k => $v)
{
// Use the set function which might be overridden.
$this->set($k, $v);
}
return true;
}
 
return false;
}
/**
* Modifies a property of the object, creating it if it does not already exist.
*
* @param string $property The name of the property.
* @param mixed $value The value of the property to set.
*
* @return mixed Previous value of the property.
*
* @since 11.1
*/
public function set($property, $value = null)
{
$_property = 'set'.str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
if (method_exists($this, $_property)) {
return $this->$_property($value);
}
$previous = isset($this->$property) ? $this->$property : null;
$this->$property = $value;
return $previous;
}
/**
* Returns an associative array of object properties.
*
* @param boolean $public If true, returns only the public properties.
*
* @return array
*
* @see get()
*/
public function getProperties($public = true)
{
$vars = get_object_vars($this);
if ($public)
{
foreach ($vars as $key => $value)
{
if ('_' == substr($key, 0, 1))
{
unset($vars[$key]);
}
}
}
 
return $vars;
}
/**
* Method returns the called class
*
*/
public static function get_called_class()
{
if (function_exists('get_called_class')) {
return get_called_class();
}
$called_class = false;
$objects = array();
$traces = debug_backtrace();
foreach ($traces as $trace)
{
if (isset($trace['object'])) {
if (is_object($trace['object'])) {
$objects[] = $trace['object'];
}
}
}
if (count($objects)) {
$called_class = get_class($objects[0]);
}
return $called_class;
}
/**
*
* contains the current instance of this class
* @var object
*/
static $_instances = null;
/**
* Method is called when we need to instantiate this class
*
* @param array $options
*/
public static function getInstance( $id, $options = array() )
{
if (!isset(self::$_instances[$id]))
{
$class = self::get_called_class();
$options['_class'] = $class;
$options['_id'] = $id;
self::$_instances[$id] =& new $class($options);
}
return self::$_instances[$id];
}
}
endif;

Hey man, can you give me some help? i'm trying to create a metabox only for a specific custom post, how can i do that?
Thanks

like for a single post? not an entire post type right?

Sincerely,

Jonathon Byrd | Partner | Senior Software Engineer
5 Twenty Studios, LLC

C: (360) 747-7401 | O: (503) 268-1177 | F: (954) 867-1177
jon@5twentystudios.com | www.5TwentyStudios.comhttp://www.5twentystudios.com/

I added a bunch of examples for you Igobatto. I will most likely program the option to assign metaboxes to single posts in a few days.

Thanks man, that's what i'm looking for. also congrats for the great class.

There you go bro, I added the functionality to include a metabox on a single post, or exclude it from a single post. This will work on any post_type.

Hey man, sorry for so many questions, but i'm having another issue on my end, i just updated the class with your latest version and the metabox isnt rendering the form anymore, here are my code:

<?php
$slider = redrokk_post_class::getInstance('slider');
$slider->set('_single', 'Slider');
$slider->set('_plural', 'Sliders');
$slider->set('menu_icon', get_template_directory_uri().'/images/icons/slider.png');
$slider->set('supports', array('title'));

$link_box = redrokk_metabox_class::getInstance('link_box');
$link_box->set('title', 'Slider Details');
$link_box->set('_object_types', 'slider');
$link_box->set('_fields', array(
array('name' => 'Link Url', 'type' => 'text', 'id' => 'link_uri', 'desc' => 'insert the url that the slide will point to.'),
array('name' => 'Slider Image', 'type' => 'image', 'id' => 'slider_image', 'desc' => 'select the image, best size: 940px X 370px.'),
));
?>

also here are the link for a print of my wp-admin: www.lgobatto.com.br/slider-ss.png

You're uploading images huh, try this:

$link_box->set('type', 'images');

As far as it not showing up... I'm a bit stumped why it wouldnt show up.
Add on line 635

var_dump($fields);die(); and let me know what shows when you reload the
page.

Hey man, how are you, so back to my bug, the code are stopping on line 187
add_action( 'plugins_loaded', array(&$this, 'setType') );
this action is not calling the setType function.

quick question, there is some dependence to use this class? I'm also using Master Widget, Post Class and Taxonomy(this one isn't working for me also, but isn't my priority).

Thanks again for your help man,

i just enable wordpress debug mode, here are some infos:

Notice: Undefined offset: 0 in D:\Jobs\Vera Roca Webgroup\DjInkognito\Site v_2\wp-includes\plugin.php on line 764

Notice: Undefined offset: 0 in D:\Jobs\Vera Roca Webgroup\DjInkognito\Site v_2\wp-includes\plugin.php on line 782

Notice: add_option was called with an argument that is deprecated since version 2.3 with no alternative available. in D:\Jobs\Vera Roca Webgroup\DjInkognito\Site v_2\wp-includes\functions.php on line 3553

Hey man, i found a solution to make it work again, comparing with the older versions i found that:
// if the user has not already set the type of this metabox,
// then we need to do that now
if (!$this->_type) {
$this->setType();
}
so what i did was:
remove this: add_action( 'plugins_loaded', array(&$this, 'setType') ); from line 189
add the older code into _register functions just after this (line 944 i think):
if (!is_array($this->callback_args)) {
$this->callback_args = array();
}
$this->callback_args[] = $this;
now everything is working.

thanks

Can you fork it and paste in you copy?
On Mar 21, 2012 8:28 PM, "lgobatto" <
reply@reply.github.com>
wrote:

Hey man, i found a solution to make it work again, comparing with the
older versions i found that:
// if the user has not already set the type of this metabox,
// then we need to do that now
if (!$this->_type) {
$this->setType();
}
so what i did was:
remove this: add_action( 'plugins_loaded', array(&$this, 'setType') );
from line 189
add the older code into _register functions just after this (line 944 i
think):
if (!is_array($this->callback_args)) {
$this->callback_args = array();
}
$this->callback_args[] = $this;


Reply to this email directly or view it on GitHub:
https://gist.github.com/1880770

@changlog 6/13/2012 : Made metabox class compatible with redrokk_admin_class for administrative pages.

Probably a lame question, but how could I import your class in order to use it on my WP installation?

Thanks

I recently hooked redrokk_metabox_class into my Testimonials Widget plugin at http://wordpress.org/extend/plugins/testimonials-widget/. The final result is really slick and wonderful time saver. To help you out implementation, take a look at http://plugins.svn.wordpress.org/testimonials-widget/tags/2.1.4/testimonials-widget.php.

Basically, in my plugin constructor, I have an admin_init add_action which in turns calls the method add_meta_box_testimonials_widget. Now add_meta_box_testimonials_widget() itself only includes the metabox.class.php file and a simple redrokk_metabox_class::getInstance for my 3 post meta fields.

That's it and the meta data integration without any further effort is working well. Do note that the id shouldn't contain hyphens. If you do, the meta data boxes show up, but no data is saved.

I got this error message

Notice: Trying to get property of non-object in D:\xampp\htdocs\dev\wp-includes\post-template.php on line 30 in my local development

WOW, this is prefect man,
thank you.

@satrya: I'm getting the same error. (with define (WP_DEBUG, true) in my wp-config.php).

I've tracked it down to line 834 (in getFields(), which is called by setOptionHooks() which is called by __constructor()):

$custom = get_post_custom($this->_id);

I just started using this class yesterday, so obviously, I don't know the code very well, but it seems there are two problems with this line:

  1. get_post_custom() expects a post ID as its arg, but this is passing the id of the metabox. There is one other place where get_post_custom() is called in this way (line 997).

So, one question is: what is supposed to be accomplished by passing $this->_id?

  1. If the arg passed to get_post_custom() doesn't eval to an int, then it internally uses global $post (in a call to get_the_ID()). If I create a custom metabox with this class in my plugin's admin_init hook (as suggested by @michael-cannon above) then global $post is not yet set, and hence the "Trying to get property of non-object" error.

Now, changing line 834 to:

        global $post ;
        $custom = isset ($post) ? get_post_custom ($post->ID) : null ;

makes the error go away, but I'm not sure it's the right thing to do (again, I've only been working with this class for a few hours). So, I'm NOT suggesting the above as a patch, only as a hint to the author about one possible fix.

another question I have has to do with metaboxes not rendering if I create them, not in my plugin's admin_init hook.

My plugin registers a custom post type. In my call to register_post_type() I use the register_meta_box_cb parameter. If I try to create a metabox with this class in that callback (which to me seems like the natural place to do it), then that metabox does not render (although, other metaboxes that I create using add_meta_box() do render).

Why don't they render in that case?

maybe I'm being dense, but I also can't seem to get array valued fields to work, e.g.

redrokk_metabox_class::getInstance ('tmp', array (
    'title'         => 'Tmp',
    '_object_types' => 'post',
    'context'       => 'normal',
    'priority'      => 'high',
    '_fields'       =>
        array (
            array (
                'name'  => 'Post Types',
                'id'    => 'post_types[]',
                'type'  => 'checkbox',
                'options'=> array (
                    'post' => 'Post',
                    'page' => 'Page',
                    'attachment' => 'Attachment',
                    ),
                'desc' => 'Select all post types that apply',
                ),
            )
    )) ;

The above renders properly, but when I save the post, the post_types meta-key is not saved.

[note: if I use 'id' => 'post_types' in the _fields array then only the last checked checkbox gets saved]

If I modify line 508 to be:

if ((!empty($fields) && !(in_array($k, $fields) || in_array($k . '[]', $fields)))

then the post_types meta-key does get saved. However, upon editing the post again, the previously saved value(s) for the field are not checked :-(

The culprit here seems to be the call to get_post_custom() on line 997 (mentioned in my 1st comment). But the problem isn't in passing $this->_id, it's what could be argued is a bug in the way get_post_custom() is implemented in core (actually, it's in get_metadata(), which is indirectly called by get_post_custom()).

That is, get_post_custom() doesn't unserialize the meta-values, whereas get_post_meta($post_id, $metakey, true) does :-( You can see this by inserting the following after line 997:

echo '<pre>' . var_export ($custom, true) . '</pre>' ;
die () ;

And because the meta-values for array valued fields aren't being unserialized, the check on line 1134 doesn't result in checked='checked' being output:

<?php echo $meta == $_value?'checked="checked"' :''; ?>

From my little time looking over the code in this class, modifying it to get around what I think is a bug in get_post_custom() seems like ALOT of work. One way would be to replace the call to get_post_custom() with individual calls to get_post_meta(), which seems like a real hassle. Another would be explicitly call $meta = maybe_unserialze($meta) in the appropriate places.

One UGLY implementation of the 2nd alternative is to replace the lines 1005-1012 with:

if (array_key_exists($id, $custom)) {
    if (isset($custom[$id][0]))
        $meta = esc_attr( $custom[$id][0] );
    else
        $meta = esc_attr( $custom[$id] );
} else {
    $tmpid = str_replace ('[]', '', $id) ;
    if (array_key_exists (str_replace ('[]', '', $id), $custom)) {
        if (isset($custom[$tmpid][0]))
            $meta = maybe_unserialize ($custom[$tmpid][0]);
        else
            $meta = maybe_unserialize ($custom[$tmpid]);
    }
else {
    $meta = $default;
    }
}

and line 1117 with:

<?php echo (is_array ($meta) && in_array ($_value, $meta)) || $meta == $_value? 'checked="checked"' :''; ?>

[I told you it was UGLY! But it does work for the cases I've tried. As in my 1st comment, I'm NOT proposing this as a patch, I'm sure there are cleaner ways to work around the problem.]

Does the above analysis seem correct to you?

[p.s. I plan on opening a ticket on http://core.trac.wordpress.org about what I think is the bug in get_post_custom() after I've explored that issue a little more. If/when I do open that ticket, I'd appreciate your commenting on it about how the bug impacts your code (assuming you agree with my analysis above).]

[p.p.s. all the above applies equally well with select's where multiple='multiple', with an equivalent change to line 1194 as I have above for 1117]

having explored the issue of get_post_custom() returning serialized values, I see that it is documented in the Codex (http://codex.wordpress.org/Function_Reference/get_post_custom#Retrieving_data_from_the_array) as having this behavior:

Note: not only does the function return a multi-dimensional array (ie: always be
prepared to deal with an array of   arrays, even if expecting array of single values),
but if also returns serialized values of any arrays stored as meta values. If you
expect that possibly an array may be stored as a metavalue, then be prepared to 
'maybe_unserialize'. 

And there has been on open ticket on it (http://core.trac.wordpress.org/ticket/20853) for 9 months.

So, while I still think it's a "bug", it appears that it is by design and that they are unlikely to "fix" it :-(

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.