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