acf.php
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Advanced Custom Fields |
| 4 | Plugin URI: http://plugins.elliotcondon.com/advanced-custom-fields/ |
| 5 | Description: Completely Customise your edit pages with an assortment of field types: Wysiwyg, text, image, select, checkbox and more! Hide unwanted metaboxes and assign to any edit page! |
| 6 | Version: 1.0.3 |
| 7 | Author: Elliot Condon |
| 8 | Author URI: http://www.elliotcondon.com/ |
| 9 | License: GPL |
| 10 | Copyright: Elliot Condon |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | $acf = new Acf(); |
| 15 | include('core/api.php'); |
| 16 | |
| 17 | class Acf |
| 18 | { |
| 19 | var $name; |
| 20 | var $dir; |
| 21 | var $path; |
| 22 | var $siteurl; |
| 23 | var $wpadminurl; |
| 24 | var $version; |
| 25 | var $fields; |
| 26 | |
| 27 | function Acf() |
| 28 | { |
| 29 | |
| 30 | // set class variables |
| 31 | $this->name = 'Advanced Custom Fields'; |
| 32 | $this->path = dirname(__FILE__).''; |
| 33 | $this->dir = plugins_url('',__FILE__); |
| 34 | $this->siteurl = get_bloginfo('url'); |
| 35 | $this->wpadminurl = admin_url(); |
| 36 | $this->version = '1.0.3'; |
| 37 | |
| 38 | // set text domain |
| 39 | load_plugin_textdomain('acf', false, $this->path.'/lang' ); |
| 40 | |
| 41 | // populate post types |
| 42 | $this->fields = $this->_get_field_types(); |
| 43 | |
| 44 | |
| 45 | // add actions |
| 46 | add_action('init', array($this, '_init')); |
| 47 | add_action('admin_head', array($this,'_admin_head')); |
| 48 | add_action('admin_menu', array($this,'_admin_menu')); |
| 49 | add_action('save_post', array($this, '_save_post')); |
| 50 | add_action('admin_footer-edit.php', array($this, '_admin_footer')); |
| 51 | |
| 52 | //register_activation_hook(__FILE__, array($this,'activate')); |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /*--------------------------------------------------------------------------------------------- |
| 59 | * Init |
| 60 | * |
| 61 | * @author Elliot Condon |
| 62 | * @since 1.0.0 |
| 63 | * |
| 64 | ---------------------------------------------------------------------------------------------*/ |
| 65 | function _init() |
| 66 | { |
| 67 | // create acf post type |
| 68 | $this->_acf_post_type(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /*--------------------------------------------------------------------------------------------- |
| 73 | * Save Post |
| 74 | * |
| 75 | * @author Elliot Condon |
| 76 | * @since 1.0.0 |
| 77 | * |
| 78 | ---------------------------------------------------------------------------------------------*/ |
| 79 | function _save_post($post_id) |
| 80 | { |
| 81 | // do not save if this is an auto save routine |
| 82 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; |
| 83 | |
| 84 | // verify this with nonce because save_post can be triggered at other times |
| 85 | if (!wp_verify_nonce($_POST['ei_noncename'], 'ei-n')) return $post_id; |
| 86 | |
| 87 | // set post ID if is a revision |
| 88 | if(wp_is_post_revision($post_id)) |
| 89 | { |
| 90 | $post_id = wp_is_post_revision($post_id); |
| 91 | } |
| 92 | |
| 93 | // delete _acf custom fields if needed |
| 94 | if($_POST['fields_meta_box'] == 'true' || $_POST['location_meta_box'] == 'true' || $_POST['input_meta_box'] == 'true') |
| 95 | { |
| 96 | $this->delete_acf_custom_fields($post_id); |
| 97 | } |
| 98 | |
| 99 | // include meta box save files |
| 100 | include('core/fields_save.php'); |
| 101 | include('core/location_save.php'); |
| 102 | include('core/options_save.php'); |
| 103 | include('core/input_save.php'); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /*--------------------------------------------------------------------------------------------- |
| 108 | * Create ACF Post Type |
| 109 | * |
| 110 | * @author Elliot Condon |
| 111 | * @since 1.0.0 |
| 112 | * |
| 113 | ---------------------------------------------------------------------------------------------*/ |
| 114 | function _acf_post_type() |
| 115 | { |
| 116 | include('core/acf_post_type.php'); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /*--------------------------------------------------------------------------------------------- |
| 121 | * Admin Menu |
| 122 | * |
| 123 | * @author Elliot Condon |
| 124 | * @since 1.0.0 |
| 125 | * |
| 126 | ---------------------------------------------------------------------------------------------*/ |
| 127 | function _admin_menu() { |
| 128 | |
| 129 | // add sub menu |
| 130 | add_submenu_page('options-general.php', 'CFA', __('Adv Custom Fields','acf'), 'manage_options','edit.php?post_type=acf'); |
| 131 | |
| 132 | // remove acf menu item |
| 133 | global $menu; |
| 134 | $restricted = array('Advanced Custom Fields'); |
| 135 | end ($menu); |
| 136 | while (prev($menu)){ |
| 137 | $value = explode(' ',$menu[key($menu)][0]); |
| 138 | if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);} |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | /*--------------------------------------------------------------------------------------------- |
| 144 | * Admin Head |
| 145 | * |
| 146 | * @author Elliot Condon |
| 147 | * @since 1.0.0 |
| 148 | * |
| 149 | ---------------------------------------------------------------------------------------------*/ |
| 150 | function _admin_head() |
| 151 | { |
| 152 | include('core/admin_head.php'); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /*--------------------------------------------------------------------------------------------- |
| 157 | * activate |
| 158 | * |
| 159 | * @author Elliot Condon |
| 160 | * @since 1.0.0 |
| 161 | * |
| 162 | ---------------------------------------------------------------------------------------------*/ |
| 163 | function activate() |
| 164 | { |
| 165 | //include('core/update.php'); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /*--------------------------------------------------------------------------------------------- |
| 170 | * _get_field_types |
| 171 | * |
| 172 | * @author Elliot Condon |
| 173 | * @since 1.0.0 |
| 174 | * |
| 175 | ---------------------------------------------------------------------------------------------*/ |
| 176 | function _get_field_types() |
| 177 | { |
| 178 | $array = array(); |
| 179 | |
| 180 | include('core/fields/text.php'); |
| 181 | include('core/fields/textarea.php'); |
| 182 | include('core/fields/wysiwyg.php'); |
| 183 | include('core/fields/image.php'); |
| 184 | include('core/fields/file.php'); |
| 185 | include('core/fields/select.php'); |
| 186 | include('core/fields/checkbox.php'); |
| 187 | include('core/fields/page_link.php'); |
| 188 | include('core/fields/date_picker/date_picker.php'); |
| 189 | |
| 190 | $array['text'] = new Text(); |
| 191 | $array['textarea'] = new Textarea(); |
| 192 | $array['wysiwyg'] = new Wysiwyg(); |
| 193 | $array['image'] = new Image($this->dir); |
| 194 | $array['file'] = new File($this->dir); |
| 195 | $array['select'] = new Select(); |
| 196 | $array['checkbox'] = new Checkbox(); |
| 197 | $array['page_link'] = new Page_link($this); |
| 198 | $array['date_picker'] = new Date_picker($this->dir); |
| 199 | |
| 200 | return $array; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /*--------------------------------------------------------------------------------------------- |
| 205 | * create_field |
| 206 | * |
| 207 | * @author Elliot Condon |
| 208 | * @since 1.0.0 |
| 209 | * |
| 210 | ---------------------------------------------------------------------------------------------*/ |
| 211 | function create_field($options) |
| 212 | { |
| 213 | if(!$this->fields[$options['type']]) |
| 214 | { |
| 215 | echo 'error: Field Type does not exist!'; |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | $this->fields[$options['type']]->html($options); |
| 220 | } |
| 221 | |
| 222 | /*--------------------------------------------------------------------------------------------- |
| 223 | * save_field |
| 224 | * |
| 225 | * @author Elliot Condon |
| 226 | * @since 1.0.0 |
| 227 | * |
| 228 | ---------------------------------------------------------------------------------------------*/ |
| 229 | function save_field($options) |
| 230 | { |
| 231 | if(!$this->fields[$options['field_type']]) |
| 232 | { |
| 233 | echo 'error: Field Type does not exist!'; |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | $this->fields[$options['field_type']]->save_field($options['post_id'], $options['field_name'], $options['field_value']); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /*--------------------------------------------------------------------------------------------- |
| 242 | * Add Meta Box to the ACF post type edit page |
| 243 | * |
| 244 | * @author Elliot Condon |
| 245 | * @since 1.0.0 |
| 246 | * |
| 247 | ---------------------------------------------------------------------------------------------*/ |
| 248 | function _fields_meta_box() |
| 249 | { |
| 250 | include('core/fields_meta_box.php'); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /*--------------------------------------------------------------------------------------------- |
| 255 | * Add Meta Box to the ACF post type edit page |
| 256 | * |
| 257 | * @author Elliot Condon |
| 258 | * @since 1.0.0 |
| 259 | * |
| 260 | ---------------------------------------------------------------------------------------------*/ |
| 261 | function _location_meta_box() |
| 262 | { |
| 263 | include('core/location_meta_box.php'); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /*--------------------------------------------------------------------------------------------- |
| 268 | * Add Meta Box to the selected post type edit page |
| 269 | * |
| 270 | * @author Elliot Condon |
| 271 | * @since 1.0.0 |
| 272 | * |
| 273 | ---------------------------------------------------------------------------------------------*/ |
| 274 | function _input_meta_box($post, $args) |
| 275 | { |
| 276 | include('core/input_meta_box.php'); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /*--------------------------------------------------------------------------------------------- |
| 281 | * Add Meta Box to the ACF post type edit page |
| 282 | * |
| 283 | * @author Elliot Condon |
| 284 | * @since 1.0.0 |
| 285 | * |
| 286 | ---------------------------------------------------------------------------------------------*/ |
| 287 | function _options_meta_box() |
| 288 | { |
| 289 | include('core/options_meta_box.php'); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /*--------------------------------------------------------------------------------------------- |
| 294 | * delete_acf_custom_fields |
| 295 | * |
| 296 | * @author Elliot Condon |
| 297 | * @since 1.0.0 |
| 298 | * |
| 299 | ---------------------------------------------------------------------------------------------*/ |
| 300 | function delete_acf_custom_fields($post_id) |
| 301 | { |
| 302 | |
| 303 | foreach(get_post_custom($post_id) as $key => $values) |
| 304 | { |
| 305 | if(strpos($key, '_acf') !== false) |
| 306 | { |
| 307 | // this custom field needs to be deleted! |
| 308 | delete_post_meta($post_id, $key); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /*--------------------------------------------------------------------------------------------- |
| 314 | * get_fields |
| 315 | * |
| 316 | * @author Elliot Condon |
| 317 | * @since 1.0.0 |
| 318 | * |
| 319 | ---------------------------------------------------------------------------------------------*/ |
| 320 | function get_fields($acf_id) |
| 321 | { |
| 322 | $keys = get_post_custom_keys($acf_id); |
| 323 | |
| 324 | if(empty($keys)) |
| 325 | { |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | $fields = array(); |
| 330 | for($i = 0; $i < 99; $i++) |
| 331 | { |
| 332 | if(in_array('_acf_field_'.$i.'_label',$keys)) |
| 333 | { |
| 334 | $field = array( |
| 335 | 'label' => get_post_meta($acf_id, '_acf_field_'.$i.'_label', true), |
| 336 | 'name' => get_post_meta($acf_id, '_acf_field_'.$i.'_name', true), |
| 337 | 'type' => get_post_meta($acf_id, '_acf_field_'.$i.'_type', true), |
| 338 | 'options' => $this->string_to_clean_array( |
| 339 | get_post_meta($acf_id, '_acf_field_'.$i.'_options', true) |
| 340 | ), |
| 341 | ); |
| 342 | |
| 343 | // if matrix, it will have sub fields |
| 344 | for($j = 0; $j < 99; $j++) |
| 345 | { |
| 346 | if(in_array('_acf_field_'.$i.'_field_'.$j.'_label',$keys)) |
| 347 | { |
| 348 | $field['options']['repeaters'][] = array( |
| 349 | 'label' => get_post_meta($acf_id, '_acf_field_'.$i.'_field_'.$j.'_label', true), |
| 350 | 'name' => get_post_meta($acf_id, '_acf_field_'.$i.'_field_'.$j.'_name', true), |
| 351 | 'type' => get_post_meta($acf_id, '_acf_field_'.$i.'_field_'.$j.'_type', true), |
| 352 | ); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | // data doesnt exist, break loop |
| 357 | //echo 'not in array, field = '.$field['label'].'<br>'; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | $fields[] = $field; |
| 363 | |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | // data doesnt exist, break loop |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return $fields; |
| 373 | } |
| 374 | |
| 375 | /*--------------------------------------------------------------------------------------------- |
| 376 | * get_field_options |
| 377 | * |
| 378 | * @author Elliot Condon |
| 379 | * @since 1.0.0 |
| 380 | * |
| 381 | ---------------------------------------------------------------------------------------------*/ |
| 382 | function get_field_options($type, $options) |
| 383 | { |
| 384 | $field_options = $this->fields[$type]->options(); |
| 385 | |
| 386 | ?> |
| 387 | <table class="field_options"> |
| 388 | <?php foreach($field_options as $field_option): ?> |
| 389 | <tr> |
| 390 | <td class="label"> |
| 391 | <label for="post_type"><?php echo $field_options[0]['label'] ?></label> |
| 392 | </td> |
| 393 | <td> |
| 394 | <?php $acf->create_field('text',$options); ?> |
| 395 | </td> |
| 396 | </tr> |
| 397 | <?php endforeach; ?> |
| 398 | </table> |
| 399 | <?php |
| 400 | } |
| 401 | |
| 402 | /*--------------------------------------------------------------------------------------------- |
| 403 | * get_acf_location |
| 404 | * |
| 405 | * @author Elliot Condon |
| 406 | * @since 1.0.0 |
| 407 | * |
| 408 | ---------------------------------------------------------------------------------------------*/ |
| 409 | function get_acf_location($acf_id) |
| 410 | { |
| 411 | $location = array( |
| 412 | 'post_type' => get_post_meta($acf_id, '_acf_location_post_type', true), |
| 413 | 'page_slug' => get_post_meta($acf_id, '_acf_location_page_slug', true), |
| 414 | 'post_id' => get_post_meta($acf_id, '_acf_location_post_id', true), |
| 415 | 'page_template' => get_post_meta($acf_id, '_acf_location_page_template', true), |
| 416 | 'parent_id' => get_post_meta($acf_id, '_acf_location_parent_id', true), |
| 417 | ); |
| 418 | |
| 419 | // post type needs to be in array format |
| 420 | //$location['post_type'] = str_replace(', ',',',$location['post_type']); |
| 421 | //$location['post_type'] = explode(',',$location['post_type']); |
| 422 | |
| 423 | return $location; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | /*--------------------------------------------------------------------------------------------- |
| 428 | * get_acf_options |
| 429 | * |
| 430 | * @author Elliot Condon |
| 431 | * @since 1.0.0 |
| 432 | * |
| 433 | ---------------------------------------------------------------------------------------------*/ |
| 434 | function get_acf_options($acf_id) |
| 435 | { |
| 436 | $options = array(); |
| 437 | |
| 438 | $keys = get_post_custom_keys($acf_id); |
| 439 | |
| 440 | if(empty($keys)) |
| 441 | { |
| 442 | $options['show_on_page'] = 'the_content, discussion, custom_fields, comments, slug, author'; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | $options['show_on_page'] = get_post_meta($acf_id, '_acf_option_show_on_page', true); |
| 447 | } |
| 448 | |
| 449 | return $options; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /*--------------------------------------------------------------------------------------------- |
| 454 | * add_to_Edit_screen |
| 455 | * |
| 456 | * @author Elliot Condon |
| 457 | * @since 1.0.0 |
| 458 | * |
| 459 | ---------------------------------------------------------------------------------------------*/ |
| 460 | function add_to_edit_screen() |
| 461 | { |
| 462 | |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /*--------------------------------------------------------------------------------------------- |
| 467 | * string_to_clean_array |
| 468 | * |
| 469 | * @author Elliot Condon |
| 470 | * @since 1.0.0 |
| 471 | * |
| 472 | ---------------------------------------------------------------------------------------------*/ |
| 473 | function string_to_clean_array($string) |
| 474 | { |
| 475 | if(!is_array(unserialize($string))) |
| 476 | { |
| 477 | return array(); |
| 478 | } |
| 479 | |
| 480 | $array = unserialize($string); |
| 481 | /*print_r($string); |
| 482 | |
| 483 | foreach($array as $key => $value) |
| 484 | { |
| 485 | if(is_array($value)) // options is an array, so unserialize it and strip slashes |
| 486 | { |
| 487 | $child_array = array(); |
| 488 | foreach($value as $child_key => $child_value) |
| 489 | { |
| 490 | $child_array[$child_key] = stripslashes($child_value); |
| 491 | } |
| 492 | $value[$key] = $child_array; |
| 493 | } |
| 494 | else // everythis else is a simple string. |
| 495 | { |
| 496 | $array[$key] = stripslashes($value); |
| 497 | } |
| 498 | }*/ |
| 499 | return $array; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /*--------------------------------------------------------------------------------------------- |
| 504 | * get_adv_options |
| 505 | * |
| 506 | * @author Elliot Condon |
| 507 | * @since 1.0.0 |
| 508 | * |
| 509 | ---------------------------------------------------------------------------------------------*/ |
| 510 | function get_adv_options($acf_id) |
| 511 | { |
| 512 | $adv = array(); |
| 513 | |
| 514 | $adv['show_on_page'] = get_post_meta($acf_id, '_acf_option_show_on_page', true); |
| 515 | |
| 516 | if(empty($adv['show_on_page'])) |
| 517 | { |
| 518 | $adv['show_on_page'] = array(); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | $adv['show_on_page'] = str_replace(', ',',',$adv['show_on_page']); |
| 523 | $adv['show_on_page'] = explode(',',$adv['show_on_page']); |
| 524 | } |
| 525 | |
| 526 | return $adv; |
| 527 | } |
| 528 | |
| 529 | /*--------------------------------------------------------------------------------------------- |
| 530 | * admin_footer |
| 531 | * |
| 532 | * @author Elliot Condon |
| 533 | * @since 1.0.0 |
| 534 | * |
| 535 | ---------------------------------------------------------------------------------------------*/ |
| 536 | function _admin_footer() |
| 537 | { |
| 538 | if($_GET['post_type'] != 'acf'){return false;} |
| 539 | |
| 540 | echo "<style type='text/css'>.row-actions span.inline, .row-actions span.view { display: none; }</style>"; |
| 541 | echo '<link rel="stylesheet" href="'.$this->dir.'/css/style.info.css" type="text/css" media="all" />'; |
| 542 | echo '<script type="text/javascript" src="'.$this->dir.'/js/functions.info.js"></script>'; |
| 543 | include('core/info_meta_box.php'); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | |
| 548 | } |