advanced-custom-fields
Last commit date
core
14 years ago
css
14 years ago
images
14 years ago
js
14 years ago
lang
14 years ago
acf.php
14 years ago
readme.txt
14 years ago
screenshot-1.png
14 years ago
screenshot-2.png
14 years ago
screenshot-3.png
14 years ago
screenshot-4.png
14 years ago
acf.php
1586 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Advanced Custom Fields |
| 4 | Plugin URI: http://plugins.elliotcondon.com/advanced-custom-fields/ |
| 5 | Description: Customise your edit pages with an assortment of field types: Wysiwyg, Repeater, text, textarea, image, file, select, checkbox post type, page link and more! Hide unwanted metaboxes and assign to any edit page! |
| 6 | Version: 3.0.3 |
| 7 | Author: Elliot Condon |
| 8 | Author URI: http://www.elliotcondon.com/ |
| 9 | License: GPL |
| 10 | Copyright: Elliot Condon |
| 11 | */ |
| 12 | |
| 13 | //ini_set('error_reporting', E_ALL); |
| 14 | |
| 15 | include('core/api.php'); |
| 16 | |
| 17 | $acf = new Acf(); |
| 18 | |
| 19 | class Acf |
| 20 | { |
| 21 | var $dir; |
| 22 | var $path; |
| 23 | var $siteurl; |
| 24 | var $wpadminurl; |
| 25 | var $version; |
| 26 | var $upgrade_version; |
| 27 | var $fields; |
| 28 | var $options_page; |
| 29 | |
| 30 | |
| 31 | /*-------------------------------------------------------------------------------------- |
| 32 | * |
| 33 | * Constructor |
| 34 | * |
| 35 | * @author Elliot Condon |
| 36 | * @since 1.0.0 |
| 37 | * |
| 38 | *-------------------------------------------------------------------------------------*/ |
| 39 | |
| 40 | function Acf() |
| 41 | { |
| 42 | |
| 43 | // set class variables |
| 44 | $this->path = dirname(__FILE__).''; |
| 45 | $this->dir = plugins_url('',__FILE__); |
| 46 | $this->siteurl = get_bloginfo('url'); |
| 47 | $this->wpadminurl = admin_url(); |
| 48 | $this->version = '3.0.3'; |
| 49 | $this->upgrade_version = '3.0.0'; // this is the latest version which requires an upgrade |
| 50 | |
| 51 | |
| 52 | // set text domain |
| 53 | //load_plugin_textdomain('acf', false, $this->path.'/lang' ); |
| 54 | load_plugin_textdomain('acf', false, basename(dirname(__FILE__)).'/lang' ); |
| 55 | |
| 56 | // load options page |
| 57 | $this->setup_options_page(); |
| 58 | |
| 59 | // actions |
| 60 | add_action('init', array($this, 'init')); |
| 61 | add_action('admin_menu', array($this,'admin_menu')); |
| 62 | add_action('admin_head', array($this,'admin_head')); |
| 63 | add_action('save_post', array($this, 'save_post')); |
| 64 | add_action('wp_ajax_get_input_metabox_ids', array($this, 'get_input_metabox_ids')); |
| 65 | add_action('wp_ajax_get_input_style', array($this, 'the_input_style')); |
| 66 | add_action('admin_footer', array($this, 'admin_footer')); |
| 67 | add_action('admin_print_scripts', array($this, 'admin_print_scripts')); |
| 68 | add_action('admin_print_styles', array($this, 'admin_print_styles')); |
| 69 | add_action('wp_ajax_acf_upgrade', array($this, 'upgrade_ajax')); |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | /*-------------------------------------------------------------------------------------- |
| 79 | * |
| 80 | * setup_fields |
| 81 | * |
| 82 | * @author Elliot Condon |
| 83 | * @since 1.0.0 |
| 84 | * |
| 85 | *-------------------------------------------------------------------------------------*/ |
| 86 | |
| 87 | function setup_fields() |
| 88 | { |
| 89 | // vars |
| 90 | $return = array(); |
| 91 | |
| 92 | // include parent field |
| 93 | include_once('core/fields/acf_field.php'); |
| 94 | |
| 95 | // include child fields |
| 96 | include_once('core/fields/acf_field.php'); |
| 97 | include_once('core/fields/text.php'); |
| 98 | include_once('core/fields/textarea.php'); |
| 99 | include_once('core/fields/wysiwyg.php'); |
| 100 | include_once('core/fields/image.php'); |
| 101 | include_once('core/fields/file.php'); |
| 102 | include_once('core/fields/select.php'); |
| 103 | include_once('core/fields/checkbox.php'); |
| 104 | include_once('core/fields/radio.php'); |
| 105 | include_once('core/fields/true_false.php'); |
| 106 | include_once('core/fields/page_link.php'); |
| 107 | include_once('core/fields/post_object.php'); |
| 108 | include_once('core/fields/relationship.php'); |
| 109 | include_once('core/fields/date_picker/date_picker.php'); |
| 110 | include_once('core/fields/color_picker.php'); |
| 111 | |
| 112 | $return['text'] = new acf_Text($this); |
| 113 | $return['textarea'] = new acf_Textarea($this); |
| 114 | $return['wysiwyg'] = new acf_Wysiwyg($this); |
| 115 | $return['image'] = new acf_Image($this); |
| 116 | $return['file'] = new acf_File($this); |
| 117 | $return['select'] = new acf_Select($this); |
| 118 | $return['checkbox'] = new acf_Checkbox($this); |
| 119 | $return['radio'] = new acf_Radio($this); |
| 120 | $return['true_false'] = new acf_True_false($this); |
| 121 | $return['page_link'] = new acf_Page_link($this); |
| 122 | $return['post_object'] = new acf_Post_object($this); |
| 123 | $return['relationship'] = new acf_Relationship($this); |
| 124 | $return['date_picker'] = new acf_Date_picker($this); |
| 125 | $return['color_picker'] = new acf_Color_picker($this); |
| 126 | |
| 127 | if($this->is_field_unlocked('repeater')) |
| 128 | { |
| 129 | include_once('core/fields/repeater.php'); |
| 130 | $return['repeater'] = new acf_Repeater($this); |
| 131 | } |
| 132 | |
| 133 | if($this->is_field_unlocked('flexible_content')) |
| 134 | { |
| 135 | include_once('core/fields/flexible_content.php'); |
| 136 | $return['flexible_content'] = new acf_flexible_content($this); |
| 137 | } |
| 138 | |
| 139 | // hook to load in third party fields |
| 140 | $custom = apply_filters('acf_register_field',array()); |
| 141 | |
| 142 | if(!empty($custom)) |
| 143 | { |
| 144 | foreach($custom as $v) |
| 145 | { |
| 146 | //var_dump($v['url']); |
| 147 | include($v['url']); |
| 148 | $name = $v['class']; |
| 149 | $custom_field = new $name($this); |
| 150 | $return[$custom_field->name] = $custom_field; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $this->fields = $return; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /*-------------------------------------------------------------------------------------- |
| 159 | * |
| 160 | * setup_options_page |
| 161 | * |
| 162 | * @author Elliot Condon |
| 163 | * @since 1.0.0 |
| 164 | * |
| 165 | *-------------------------------------------------------------------------------------*/ |
| 166 | |
| 167 | function setup_options_page() |
| 168 | { |
| 169 | include_once('core/options_page.php'); |
| 170 | $this->options_page = new Options_page($this); |
| 171 | } |
| 172 | |
| 173 | /*-------------------------------------------------------------------------------------- |
| 174 | * |
| 175 | * admin_menu |
| 176 | * |
| 177 | * @author Elliot Condon |
| 178 | * @since 1.0.0 |
| 179 | * |
| 180 | *-------------------------------------------------------------------------------------*/ |
| 181 | |
| 182 | function admin_menu() { |
| 183 | |
| 184 | // add acf page to options menu |
| 185 | add_options_page(__("Adv Custom Fields",'acf'), __("Adv Custom Fields",'acf'), 'manage_options', 'edit.php?post_type=acf'); |
| 186 | add_options_page(__("ACF Upgrade",'acf'), __("Adv Upgrade",'acf'), 'manage_options', 'acf-upgrade', array($this, 'upgrade')); |
| 187 | |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /*-------------------------------------------------------------------------------------- |
| 192 | * |
| 193 | * Init |
| 194 | * |
| 195 | * @author Elliot Condon |
| 196 | * @since 1.0.0 |
| 197 | * |
| 198 | *-------------------------------------------------------------------------------------*/ |
| 199 | |
| 200 | function init() |
| 201 | { |
| 202 | include('core/actions/init.php'); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /*-------------------------------------------------------------------------------------- |
| 207 | * |
| 208 | * upgrade |
| 209 | * |
| 210 | * @author Elliot Condon |
| 211 | * @since 1.0.0 |
| 212 | * |
| 213 | *-------------------------------------------------------------------------------------*/ |
| 214 | |
| 215 | function upgrade() |
| 216 | { |
| 217 | include('core/admin/upgrade.php'); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /*-------------------------------------------------------------------------------------- |
| 222 | * |
| 223 | * ajax_upgrade |
| 224 | * |
| 225 | * @author Elliot Condon |
| 226 | * @since 1.0.0 |
| 227 | * |
| 228 | *-------------------------------------------------------------------------------------*/ |
| 229 | |
| 230 | function upgrade_ajax() |
| 231 | { |
| 232 | include('core/admin/upgrade_ajax.php'); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | |
| 237 | /*-------------------------------------------------------------------------------------- |
| 238 | * |
| 239 | * admin_print_scripts / admin_print_styles |
| 240 | * |
| 241 | * @author Elliot Condon |
| 242 | * @since 3.0.0 |
| 243 | * |
| 244 | *-------------------------------------------------------------------------------------*/ |
| 245 | |
| 246 | function admin_print_scripts() { |
| 247 | |
| 248 | if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php'))) |
| 249 | { |
| 250 | if($GLOBALS['post_type'] == 'acf') |
| 251 | { |
| 252 | // hmmm |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | // fields admin_head |
| 257 | foreach($this->fields as $field) |
| 258 | { |
| 259 | $this->fields[$field->name]->admin_print_scripts(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
| 266 | function admin_print_styles() { |
| 267 | |
| 268 | if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php'))) |
| 269 | { |
| 270 | if($GLOBALS['post_type'] == 'acf') |
| 271 | { |
| 272 | // hmmm |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | // fields admin_head |
| 277 | foreach($this->fields as $field) |
| 278 | { |
| 279 | $this->fields[$field->name]->admin_print_styles(); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /*-------------------------------------------------------------------------------------- |
| 288 | * |
| 289 | * admin_head |
| 290 | * |
| 291 | * @author Elliot Condon |
| 292 | * @since 1.0.0 |
| 293 | * |
| 294 | *-------------------------------------------------------------------------------------*/ |
| 295 | |
| 296 | function admin_head() |
| 297 | { |
| 298 | // vars |
| 299 | global $post; |
| 300 | |
| 301 | // hide upgrade page from nav |
| 302 | echo '<style type="text/css"> #menu-settings a[href="options-general.php?page=acf-upgrade"]{ display:none; }</style>'; |
| 303 | |
| 304 | |
| 305 | // only add to edit pages |
| 306 | if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php'))) |
| 307 | { |
| 308 | |
| 309 | if($GLOBALS['post_type'] == 'acf') |
| 310 | { |
| 311 | echo '<script type="text/javascript" src="'.$this->dir.'/js/fields.js" ></script>'; |
| 312 | echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/global.css" />'; |
| 313 | echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/fields.css" />'; |
| 314 | |
| 315 | add_meta_box('acf_fields', 'Fields', array($this, 'meta_box_fields'), 'acf', 'normal', 'high'); |
| 316 | add_meta_box('acf_location', 'Location </span><span class="description">- Add Fields to Edit Screens', array($this, 'meta_box_location'), 'acf', 'normal', 'high'); |
| 317 | add_meta_box('acf_options', 'Options</span><span class="description">- Customise the edit page', array($this, 'meta_box_options'), 'acf', 'normal', 'high'); |
| 318 | |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | |
| 323 | // find post type and add wysiwyg support |
| 324 | $post_type = get_post_type($post); |
| 325 | |
| 326 | // add css + javascript |
| 327 | echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/global.css" />'; |
| 328 | echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/input.css" />'; |
| 329 | echo '<script type="text/javascript" src="'.$this->dir.'/js/input.js" ></script>'; |
| 330 | echo '<style type="text/css">.acf_postbox, .postbox[id*="acf_"] { display: none; }</style>'; |
| 331 | |
| 332 | // get style for page |
| 333 | $metabox_ids = $this->get_input_metabox_ids(array('post_id' => $post->ID), false); |
| 334 | $style = isset($metabox_ids[0]) ? $this->get_input_style($metabox_ids[0]) : ''; |
| 335 | echo '<style type="text/css" id="acf_style" >' .$style . '</style>'; |
| 336 | |
| 337 | // fields admin_head |
| 338 | foreach($this->fields as $field) |
| 339 | { |
| 340 | $this->fields[$field->name]->admin_head(); |
| 341 | } |
| 342 | |
| 343 | // get acf's |
| 344 | $acfs = get_pages(array( |
| 345 | 'numberposts' => -1, |
| 346 | 'post_type' => 'acf', |
| 347 | 'sort_column' => 'menu_order', |
| 348 | 'order' => 'ASC', |
| 349 | )); |
| 350 | if($acfs) |
| 351 | { |
| 352 | foreach($acfs as $acf) |
| 353 | { |
| 354 | // hide / show |
| 355 | $show = in_array($acf->ID, $metabox_ids) ? "true" : "false"; |
| 356 | |
| 357 | // load |
| 358 | $options = $this->get_acf_options($acf->ID); |
| 359 | $fields = $this->get_acf_fields($acf->ID); |
| 360 | |
| 361 | // add meta box |
| 362 | add_meta_box( |
| 363 | 'acf_' . $acf->ID, |
| 364 | $acf->post_title, |
| 365 | array($this, 'meta_box_input'), |
| 366 | $post_type, |
| 367 | $options['position'], |
| 368 | 'default', |
| 369 | array( 'fields' => $fields, 'options' => $options, 'show' => $show ) |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /*-------------------------------------------------------------------------------------- |
| 383 | * |
| 384 | * admin_footer |
| 385 | * |
| 386 | * @author Elliot Condon |
| 387 | * @since 1.0.0 |
| 388 | * |
| 389 | *-------------------------------------------------------------------------------------*/ |
| 390 | |
| 391 | function admin_footer() |
| 392 | { |
| 393 | // acf edit list |
| 394 | if($GLOBALS['pagenow'] == 'edit.php' && $GLOBALS['post_type'] == 'acf') |
| 395 | { |
| 396 | include('core/admin/meta_box_acf.php'); |
| 397 | } |
| 398 | |
| 399 | // input meta boxes |
| 400 | if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php')) && $GLOBALS['post_type'] != 'acf') |
| 401 | { |
| 402 | wp_preload_dialogs( array( 'plugins' => 'safari,inlinepopups,spellchecker,paste,wordpress,media,fullscreen,wpeditimage,wpgallery,tabfocus' ) ); |
| 403 | ?> |
| 404 | <script type="text/javascript"> |
| 405 | (function($){ |
| 406 | |
| 407 | // add classes |
| 408 | $('#poststuff .postbox[id*="acf_"]').addClass('acf_postbox'); |
| 409 | $('#adv-settings label[for*="acf_"]').addClass('acf_hide_label'); |
| 410 | |
| 411 | // hide acf stuff |
| 412 | $('#poststuff .acf_postbox').hide(); |
| 413 | $('#adv-settings .acf_hide_label').hide(); |
| 414 | |
| 415 | // loop through acf metaboxes |
| 416 | $('#poststuff .postbox.acf_postbox').each(function(){ |
| 417 | |
| 418 | // vars |
| 419 | var options = $(this).find('.inside > .options'); |
| 420 | var show = options.attr('data-show'); |
| 421 | var layout = options.attr('data-layout'); |
| 422 | var id = $(this).attr('id').replace('acf_', ''); |
| 423 | |
| 424 | // layout |
| 425 | $(this).addClass('acf_postbox').addClass(layout); |
| 426 | |
| 427 | // show / hide |
| 428 | if(show == 'true') |
| 429 | { |
| 430 | $(this).show(); |
| 431 | $('#adv-settings .acf_hide_label[for="acf_' + id + '-hide"]').show(); |
| 432 | } |
| 433 | |
| 434 | }); |
| 435 | |
| 436 | })(jQuery); |
| 437 | </script> |
| 438 | <?php |
| 439 | } |
| 440 | |
| 441 | } |
| 442 | |
| 443 | |
| 444 | /*-------------------------------------------------------------------------------------- |
| 445 | * |
| 446 | * meta_box_fields |
| 447 | * |
| 448 | * @author Elliot Condon |
| 449 | * @since 1.0.0 |
| 450 | * |
| 451 | *-------------------------------------------------------------------------------------*/ |
| 452 | |
| 453 | function meta_box_fields() |
| 454 | { |
| 455 | include('core/admin/meta_box_fields.php'); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /*-------------------------------------------------------------------------------------- |
| 460 | * |
| 461 | * meta_box_location |
| 462 | * |
| 463 | * @author Elliot Condon |
| 464 | * @since 1.0.0 |
| 465 | * |
| 466 | *-------------------------------------------------------------------------------------*/ |
| 467 | |
| 468 | function meta_box_location() |
| 469 | { |
| 470 | include('core/admin/meta_box_location.php'); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /*-------------------------------------------------------------------------------------- |
| 475 | * |
| 476 | * meta_box_options |
| 477 | * |
| 478 | * @author Elliot Condon |
| 479 | * @since 1.0.0 |
| 480 | * |
| 481 | *-------------------------------------------------------------------------------------*/ |
| 482 | |
| 483 | function meta_box_options() |
| 484 | { |
| 485 | include('core/admin/meta_box_options.php'); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | /*-------------------------------------------------------------------------------------- |
| 490 | * |
| 491 | * meta_box_input |
| 492 | * |
| 493 | * @author Elliot Condon |
| 494 | * @since 1.0.0 |
| 495 | * |
| 496 | *-------------------------------------------------------------------------------------*/ |
| 497 | |
| 498 | function meta_box_input($post, $args) |
| 499 | { |
| 500 | include('core/admin/meta_box_input.php'); |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /*-------------------------------------------------------------------------------------- |
| 505 | * |
| 506 | * get_acf_fields |
| 507 | * - returns an array of fields for a acf object |
| 508 | * |
| 509 | * @author Elliot Condon |
| 510 | * @since 1.0.0 |
| 511 | * |
| 512 | *-------------------------------------------------------------------------------------*/ |
| 513 | |
| 514 | function get_acf_fields($post_id) |
| 515 | { |
| 516 | // vars |
| 517 | $return = array(); |
| 518 | $keys = get_post_custom_keys($post_id); |
| 519 | |
| 520 | if($keys) |
| 521 | { |
| 522 | foreach($keys as $key) |
| 523 | { |
| 524 | if(strpos($key, 'field_') !== false) |
| 525 | { |
| 526 | $field = $this->get_acf_field($key, $post_id); |
| 527 | |
| 528 | $return[$field['order_no']] = $field; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | ksort($return); |
| 533 | } |
| 534 | // return fields |
| 535 | return $return; |
| 536 | |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /*-------------------------------------------------------------------------------------- |
| 541 | * |
| 542 | * get_acf_field |
| 543 | * - returns a field |
| 544 | * |
| 545 | * @author Elliot Condon |
| 546 | * @since 1.0.0 |
| 547 | * |
| 548 | *-------------------------------------------------------------------------------------*/ |
| 549 | |
| 550 | function get_acf_field($field_name, $post_id = false) |
| 551 | { |
| 552 | $post_id = $post_id ? $post_id : $this->get_post_meta_post_id($field_name); |
| 553 | |
| 554 | $field = get_post_meta($post_id, $field_name, true); |
| 555 | |
| 556 | return $field; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /*-------------------------------------------------------------------------------------- |
| 562 | * |
| 563 | * get_post_meta_post_id |
| 564 | * - returns the post_id for a meta_key |
| 565 | * |
| 566 | * @author Elliot Condon |
| 567 | * @since 1.0.0 |
| 568 | * |
| 569 | *-------------------------------------------------------------------------------------*/ |
| 570 | |
| 571 | function get_post_meta_post_id($field_name) |
| 572 | { |
| 573 | global $wpdb; |
| 574 | $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $field_name) ); |
| 575 | |
| 576 | if($post_id) return (int)$post_id; |
| 577 | |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | /*-------------------------------------------------------------------------------------- |
| 583 | * |
| 584 | * create_field |
| 585 | * |
| 586 | * @author Elliot Condon |
| 587 | * @since 1.0.0 |
| 588 | * |
| 589 | *-------------------------------------------------------------------------------------*/ |
| 590 | |
| 591 | function create_field($field) |
| 592 | { |
| 593 | if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']])) |
| 594 | { |
| 595 | _e('Error: Field Type does not exist!','acf'); |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | // defaults |
| 600 | if(!isset($field['class'])) $field['class'] = $field['type']; |
| 601 | |
| 602 | $this->fields[$field['type']]->create_field($field); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /*-------------------------------------------------------------------------------------- |
| 607 | * |
| 608 | * get_acf_location |
| 609 | * |
| 610 | * @author Elliot Condon |
| 611 | * @since 1.0.0 |
| 612 | * |
| 613 | *-------------------------------------------------------------------------------------*/ |
| 614 | |
| 615 | function get_acf_location($post_id) |
| 616 | { |
| 617 | // vars |
| 618 | $return = array( |
| 619 | 'rules' => array(), |
| 620 | 'allorany' => get_post_meta($post_id, 'allorany', true) ? get_post_meta($post_id, 'allorany', true) : 'all', |
| 621 | ); |
| 622 | |
| 623 | // get all fields |
| 624 | $rules = get_post_meta($post_id, 'rule', false); |
| 625 | |
| 626 | if($rules) |
| 627 | { |
| 628 | foreach($rules as $rule) |
| 629 | { |
| 630 | $return['rules'][$rule['order_no']] = $rule; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | ksort($return['rules']); |
| 635 | |
| 636 | // return fields |
| 637 | return $return; |
| 638 | |
| 639 | } |
| 640 | |
| 641 | |
| 642 | /*-------------------------------------------------------------------------------------- |
| 643 | * |
| 644 | * get_acf_options |
| 645 | * |
| 646 | * @author Elliot Condon |
| 647 | * @since 1.0.0 |
| 648 | * |
| 649 | *-------------------------------------------------------------------------------------*/ |
| 650 | |
| 651 | function get_acf_options($post_id) |
| 652 | { |
| 653 | // defaults |
| 654 | $options = array( |
| 655 | 'position' => get_post_meta($post_id, 'position', true) ? get_post_meta($post_id, 'position', true) : 'normal', |
| 656 | 'layout' => get_post_meta($post_id, 'layout', true) ? get_post_meta($post_id, 'layout', true) : 'default', |
| 657 | 'show_on_page' => get_post_meta($post_id, 'show_on_page', true) ? get_post_meta($post_id, 'show_on_page', true) : array(), |
| 658 | ); |
| 659 | |
| 660 | // If this is a new acf, there will be no custom keys! |
| 661 | if(!get_post_custom_keys($post_id)) |
| 662 | { |
| 663 | $options['show_on_page'] = array('the_content', 'discussion', 'custom_fields', 'comments', 'slug', 'author'); |
| 664 | } |
| 665 | |
| 666 | // return |
| 667 | return $options; |
| 668 | } |
| 669 | |
| 670 | |
| 671 | /*-------------------------------------------------------------------------------------- |
| 672 | * |
| 673 | * save_post |
| 674 | * |
| 675 | * @author Elliot Condon |
| 676 | * @since 1.0.0 |
| 677 | * |
| 678 | *-------------------------------------------------------------------------------------*/ |
| 679 | |
| 680 | function save_post($post_id) |
| 681 | { |
| 682 | |
| 683 | // do not save if this is an auto save routine |
| 684 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; |
| 685 | |
| 686 | // only save once! WordPress save's twice for some strange reason. |
| 687 | global $flag; |
| 688 | if ($flag != 0) return $post_id; |
| 689 | $flag = 1; |
| 690 | |
| 691 | // set post ID if is a revision |
| 692 | if(wp_is_post_revision($post_id)) |
| 693 | { |
| 694 | $post_id = wp_is_post_revision($post_id); |
| 695 | } |
| 696 | |
| 697 | // include save files |
| 698 | if(isset($_POST['save_fields']) && $_POST['save_fields'] == 'true') include('core/actions/save_fields.php'); |
| 699 | if(isset($_POST['save_input']) && $_POST['save_input'] == 'true') include('core/actions/save_input.php'); |
| 700 | |
| 701 | } |
| 702 | |
| 703 | |
| 704 | /*-------------------------------------------------------------------------------------- |
| 705 | * |
| 706 | * get_value |
| 707 | * |
| 708 | * @author Elliot Condon |
| 709 | * @since 3.0.0 |
| 710 | * |
| 711 | *-------------------------------------------------------------------------------------*/ |
| 712 | |
| 713 | function get_value($post_id, $field) |
| 714 | { |
| 715 | if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']])) |
| 716 | { |
| 717 | return ''; |
| 718 | } |
| 719 | |
| 720 | return $this->fields[$field['type']]->get_value($post_id, $field); |
| 721 | } |
| 722 | |
| 723 | |
| 724 | /*-------------------------------------------------------------------------------------- |
| 725 | * |
| 726 | * get_value_for_api |
| 727 | * |
| 728 | * @author Elliot Condon |
| 729 | * @since 3.0.0 |
| 730 | * |
| 731 | *-------------------------------------------------------------------------------------*/ |
| 732 | |
| 733 | function get_value_for_api($post_id, $field) |
| 734 | { |
| 735 | if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']])) |
| 736 | { |
| 737 | return ''; |
| 738 | } |
| 739 | |
| 740 | return $this->fields[$field['type']]->get_value_for_api($post_id, $field); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /*-------------------------------------------------------------------------------------- |
| 745 | * |
| 746 | * update_value |
| 747 | * |
| 748 | * @author Elliot Condon |
| 749 | * @since 3.0.0 |
| 750 | * |
| 751 | *-------------------------------------------------------------------------------------*/ |
| 752 | |
| 753 | function update_value($post_id, $field, $value) |
| 754 | { |
| 755 | $this->fields[$field['type']]->update_value($post_id, $field, $value); |
| 756 | } |
| 757 | |
| 758 | |
| 759 | /*-------------------------------------------------------------------------------------- |
| 760 | * |
| 761 | * update_field |
| 762 | * |
| 763 | * @author Elliot Condon |
| 764 | * @since 3.0.0 |
| 765 | * |
| 766 | *-------------------------------------------------------------------------------------*/ |
| 767 | |
| 768 | function update_field($post_id, $field) |
| 769 | { |
| 770 | // format the field (select, repeater, etc) |
| 771 | $field = $this->pre_save_field($field); |
| 772 | |
| 773 | // save it! |
| 774 | update_post_meta($post_id, $field['key'], $field); |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /*-------------------------------------------------------------------------------------- |
| 779 | * |
| 780 | * pre_save_field |
| 781 | * |
| 782 | * @author Elliot Condon |
| 783 | * @since 3.0.0 |
| 784 | * |
| 785 | *-------------------------------------------------------------------------------------*/ |
| 786 | |
| 787 | function pre_save_field($field) |
| 788 | { |
| 789 | // format the field (select, repeater, etc) |
| 790 | return $this->fields[$field['type']]->pre_save_field($field); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | /*-------------------------------------------------------------------------------------- |
| 795 | * |
| 796 | * format_value_for_input |
| 797 | * |
| 798 | * @author Elliot Condon |
| 799 | * @since 3.0.0 |
| 800 | * |
| 801 | *-------------------------------------------------------------------------------------*/ |
| 802 | |
| 803 | //function format_value_for_input($value, $field) |
| 804 | //{ |
| 805 | // return $this->fields[$field['type']]->format_value_for_input($value, $field); |
| 806 | //} |
| 807 | |
| 808 | |
| 809 | /*-------------------------------------------------------------------------------------- |
| 810 | * |
| 811 | * format_value_for_api |
| 812 | * |
| 813 | * @author Elliot Condon |
| 814 | * @since 3.0.0 |
| 815 | * |
| 816 | *-------------------------------------------------------------------------------------*/ |
| 817 | |
| 818 | function format_value_for_api($value, $field) |
| 819 | { |
| 820 | if(!isset($this)) |
| 821 | { |
| 822 | // called form api! |
| 823 | |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | // called from object |
| 828 | } |
| 829 | return $this->fields[$field['type']]->format_value_for_api($value, $field); |
| 830 | } |
| 831 | |
| 832 | |
| 833 | /*-------------------------------------------------------------------------------------- |
| 834 | * |
| 835 | * create_format_data |
| 836 | * |
| 837 | * @author Elliot Condon |
| 838 | * @since 3.0.0 |
| 839 | * |
| 840 | *-------------------------------------------------------------------------------------*/ |
| 841 | |
| 842 | function create_format_data($field) |
| 843 | { |
| 844 | return $this->fields[$field['type']]->create_format_data($field); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /*-------------------------------------------------------------------------------------- |
| 849 | * |
| 850 | * get_input_metabox_ids |
| 851 | * - called by function.fields to hide / show metaboxes |
| 852 | * |
| 853 | * @author Elliot Condon |
| 854 | * @since 2.0.5 |
| 855 | * |
| 856 | *-------------------------------------------------------------------------------------*/ |
| 857 | |
| 858 | function get_input_metabox_ids($overrides = array(), $json = true) |
| 859 | { |
| 860 | // overrides |
| 861 | if(isset($_POST)) |
| 862 | { |
| 863 | if(isset($_POST['post_id']) && $_POST['post_id'] != 'false') $overrides['post_id'] = $_POST['post_id']; |
| 864 | if(isset($_POST['page_template']) && $_POST['page_template'] != 'false') $overrides['page_template'] = $_POST['page_template']; |
| 865 | if(isset($_POST['page_parent']) && $_POST['page_parent'] != 'false') $overrides['page_parent'] = $_POST['page_parent']; |
| 866 | if(isset($_POST['page_type']) && $_POST['page_type'] != 'false') $overrides['page_type'] = $_POST['page_type']; |
| 867 | if(isset($_POST['page']) && $_POST['page'] != 'false') $overrides['page'] = $_POST['page']; |
| 868 | if(isset($_POST['post']) && $_POST['post'] != 'false') $overrides['post'] = $_POST['post']; |
| 869 | if(isset($_POST['post_category']) && $_POST['post_category'] != 'false') $overrides['post_category'] = $_POST['post_category']; |
| 870 | if(isset($_POST['post_format']) && $_POST['post_format'] != 'false') $overrides['post_format'] = $_POST['post_format']; |
| 871 | if(isset($_POST['taxonomy']) && $_POST['taxonomy'] != 'false') $overrides['taxonomy'] = $_POST['taxonomy']; |
| 872 | } |
| 873 | |
| 874 | // create post object to match against |
| 875 | $post = isset($overrides['post_id']) ? get_post($_POST['post_id']) : false; |
| 876 | |
| 877 | // find all acf objects |
| 878 | $acfs = get_pages(array( |
| 879 | 'numberposts' => -1, |
| 880 | 'post_type' => 'acf', |
| 881 | 'sort_column' => 'menu_order', |
| 882 | )); |
| 883 | |
| 884 | // blank array to hold acfs |
| 885 | $return = array(); |
| 886 | |
| 887 | if($acfs) |
| 888 | { |
| 889 | |
| 890 | foreach($acfs as $acf) |
| 891 | { |
| 892 | $add_box = false; |
| 893 | $location = $this->get_acf_location($acf->ID); |
| 894 | |
| 895 | if($location['allorany'] == 'all') |
| 896 | { |
| 897 | // ALL |
| 898 | $add_box = true; |
| 899 | |
| 900 | if($location['rules']) |
| 901 | { |
| 902 | foreach($location['rules'] as $rule) |
| 903 | { |
| 904 | |
| 905 | // if any rules dont return true, dont add this acf |
| 906 | if(!$this->match_location_rule($post, $rule, $overrides)) |
| 907 | { |
| 908 | $add_box = false; |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | } |
| 914 | elseif($location['allorany'] == 'any') |
| 915 | { |
| 916 | // ANY |
| 917 | |
| 918 | $add_box = false; |
| 919 | |
| 920 | if($location['rules']) |
| 921 | { |
| 922 | foreach($location['rules'] as $rule) |
| 923 | { |
| 924 | // if any rules return true, add this acf |
| 925 | if($this->match_location_rule($post, $rule, $overrides)) |
| 926 | { |
| 927 | $add_box = true; |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | if($add_box == true) |
| 934 | { |
| 935 | $return[] = $acf->ID; |
| 936 | } |
| 937 | |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if($json) |
| 942 | { |
| 943 | echo json_encode($return); |
| 944 | die; |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | return $return; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /*-------------------------------------------------------------------------------------- |
| 956 | * |
| 957 | * get_input_style |
| 958 | * - called by function.fields to hide / show other metaboxes |
| 959 | * |
| 960 | * @author Elliot Condon |
| 961 | * @since 2.0.5 |
| 962 | * |
| 963 | *-------------------------------------------------------------------------------------*/ |
| 964 | |
| 965 | function get_input_style($acf_id = false) |
| 966 | { |
| 967 | // get field group options |
| 968 | $options = $this->get_acf_options($acf_id); |
| 969 | $html = ""; |
| 970 | |
| 971 | // html |
| 972 | if(!in_array('the_content',$options['show_on_page'])) |
| 973 | { |
| 974 | $html .= '#postdivrich {display: none;} '; |
| 975 | } |
| 976 | if(!in_array('custom_fields',$options['show_on_page'])) |
| 977 | { |
| 978 | $html .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } '; |
| 979 | } |
| 980 | if(!in_array('discussion',$options['show_on_page'])) |
| 981 | { |
| 982 | $html .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} '; |
| 983 | } |
| 984 | if(!in_array('comments',$options['show_on_page'])) |
| 985 | { |
| 986 | $html .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} '; |
| 987 | } |
| 988 | if(!in_array('slug',$options['show_on_page'])) |
| 989 | { |
| 990 | $html .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} '; |
| 991 | } |
| 992 | if(!in_array('author',$options['show_on_page'])) |
| 993 | { |
| 994 | $html .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} '; |
| 995 | } |
| 996 | |
| 997 | return $html; |
| 998 | |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | /*-------------------------------------------------------------------------------------- |
| 1003 | * |
| 1004 | * the_input_style |
| 1005 | * - called by function.fields to hide / show other metaboxes |
| 1006 | * |
| 1007 | * @author Elliot Condon |
| 1008 | * @since 2.0.5 |
| 1009 | * |
| 1010 | *-------------------------------------------------------------------------------------*/ |
| 1011 | |
| 1012 | function the_input_style() |
| 1013 | { |
| 1014 | // overrides |
| 1015 | if(isset($_POST['acf_id'])) |
| 1016 | { |
| 1017 | echo $this->get_input_style($_POST['acf_id']); |
| 1018 | } |
| 1019 | |
| 1020 | die; |
| 1021 | |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | /*-------------------------------------------------------------------------------------- |
| 1026 | * |
| 1027 | * match_location_rule |
| 1028 | * |
| 1029 | * @author Elliot Condon |
| 1030 | * @since 2.0.0 |
| 1031 | * |
| 1032 | *-------------------------------------------------------------------------------------*/ |
| 1033 | |
| 1034 | function match_location_rule($post, $rule, $overrides = array()) |
| 1035 | { |
| 1036 | |
| 1037 | if(!$post) |
| 1038 | { |
| 1039 | // post is false! that's okay if the rule is for user_type or options_page |
| 1040 | if($rule['param'] != 'user_type' && $rule['param'] != 'options_page') |
| 1041 | { |
| 1042 | return false; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | switch ($rule['param']) { |
| 1050 | |
| 1051 | // POST TYPE |
| 1052 | case "post_type": |
| 1053 | |
| 1054 | $post_type = isset($overrides['post_type']) ? $overrides['post_type'] : get_post_type($post); |
| 1055 | |
| 1056 | if($rule['operator'] == "==") |
| 1057 | { |
| 1058 | if($post_type == $rule['value']) |
| 1059 | { |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | return false; |
| 1064 | } |
| 1065 | elseif($rule['operator'] == "!=") |
| 1066 | { |
| 1067 | if($post_type != $rule['value']) |
| 1068 | { |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | break; |
| 1076 | |
| 1077 | // PAGE |
| 1078 | case "page": |
| 1079 | |
| 1080 | $page = isset($overrides['page']) ? $overrides['page'] : $post->ID; |
| 1081 | |
| 1082 | if($rule['operator'] == "==") |
| 1083 | { |
| 1084 | if($page == $rule['value']) |
| 1085 | { |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | return false; |
| 1090 | } |
| 1091 | elseif($rule['operator'] == "!=") |
| 1092 | { |
| 1093 | if($page != $rule['value']) |
| 1094 | { |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | return false; |
| 1099 | } |
| 1100 | |
| 1101 | break; |
| 1102 | |
| 1103 | // PAGE |
| 1104 | case "page_type": |
| 1105 | |
| 1106 | $page_type = isset($overrides['page_type']) ? $overrides['page_type'] : $post->post_parent; |
| 1107 | |
| 1108 | if($rule['operator'] == "==") |
| 1109 | { |
| 1110 | if($rule['value'] == "parent" && $page_type == "0") |
| 1111 | { |
| 1112 | return true; |
| 1113 | } |
| 1114 | |
| 1115 | if($rule['value'] == "child" && $page_type != "0") |
| 1116 | { |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | return false; |
| 1121 | } |
| 1122 | elseif($rule['operator'] == "!=") |
| 1123 | { |
| 1124 | if($rule['value'] == "parent" && $page_type != "0") |
| 1125 | { |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
| 1129 | if($rule['value'] == "child" && $page_type == "0") |
| 1130 | { |
| 1131 | return true; |
| 1132 | } |
| 1133 | |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | break; |
| 1138 | |
| 1139 | // PAGE PARENT |
| 1140 | case "page_parent": |
| 1141 | |
| 1142 | $page_parent = isset($overrides['page_parent']) ? $overrides['page_parent'] : $post->post_parent; |
| 1143 | |
| 1144 | if($rule['operator'] == "==") |
| 1145 | { |
| 1146 | if($page_parent == $rule['value']) |
| 1147 | { |
| 1148 | return true; |
| 1149 | } |
| 1150 | |
| 1151 | return false; |
| 1152 | |
| 1153 | } |
| 1154 | elseif($rule['operator'] == "!=") |
| 1155 | { |
| 1156 | if($page_parent != $rule['value']) |
| 1157 | { |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
| 1164 | break; |
| 1165 | |
| 1166 | // PAGE |
| 1167 | case "page_template": |
| 1168 | |
| 1169 | $page_template = isset($overrides['page_template']) ? $overrides['page_template'] : get_post_meta($post->ID,'_wp_page_template',true); |
| 1170 | |
| 1171 | if($rule['operator'] == "==") |
| 1172 | { |
| 1173 | if($page_template == $rule['value']) |
| 1174 | { |
| 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | if($rule['value'] == "default" && !$page_template) |
| 1179 | { |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | return false; |
| 1184 | } |
| 1185 | elseif($rule['operator'] == "!=") |
| 1186 | { |
| 1187 | if($page_template != $rule['value']) |
| 1188 | { |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
| 1195 | break; |
| 1196 | |
| 1197 | // POST |
| 1198 | case "post": |
| 1199 | |
| 1200 | $post_id = isset($overrides['post']) ? $overrides['post'] : $post->ID; |
| 1201 | |
| 1202 | if($rule['operator'] == "==") |
| 1203 | { |
| 1204 | if($post_id == $rule['value']) |
| 1205 | { |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | return false; |
| 1210 | } |
| 1211 | elseif($rule['operator'] == "!=") |
| 1212 | { |
| 1213 | if($post_id != $rule['value']) |
| 1214 | { |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | return false; |
| 1219 | } |
| 1220 | |
| 1221 | break; |
| 1222 | |
| 1223 | // POST CATEGORY |
| 1224 | case "post_category": |
| 1225 | |
| 1226 | $cats = array(); |
| 1227 | |
| 1228 | if(isset($overrides['post_category'])) |
| 1229 | { |
| 1230 | $cats = $overrides['post_category']; |
| 1231 | } |
| 1232 | else |
| 1233 | { |
| 1234 | $all_cats = get_the_category($post->ID); |
| 1235 | foreach($all_cats as $cat) |
| 1236 | { |
| 1237 | $cats[] = $cat->term_id; |
| 1238 | } |
| 1239 | } |
| 1240 | if($rule['operator'] == "==") |
| 1241 | { |
| 1242 | if($cats) |
| 1243 | { |
| 1244 | if(in_array($rule['value'], $cats)) |
| 1245 | { |
| 1246 | return true; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | return false; |
| 1251 | } |
| 1252 | elseif($rule['operator'] == "!=") |
| 1253 | { |
| 1254 | if($cats) |
| 1255 | { |
| 1256 | if(!in_array($rule['value'], $cats)) |
| 1257 | { |
| 1258 | return true; |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | return false; |
| 1263 | } |
| 1264 | |
| 1265 | break; |
| 1266 | |
| 1267 | // PAGE PARENT |
| 1268 | /* |
| 1269 | case "post_format": |
| 1270 | |
| 1271 | $post_format = isset($overrides['post_format']) ? $overrides['post_format'] : get_post_format(); |
| 1272 | |
| 1273 | if($rule['operator'] == "==") |
| 1274 | { |
| 1275 | if($post_format == $rule['value']) |
| 1276 | { |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| 1280 | return false; |
| 1281 | |
| 1282 | } |
| 1283 | elseif($post_format == "!=") |
| 1284 | { |
| 1285 | if($post->post_parent != $rule['value']) |
| 1286 | { |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
| 1293 | break; |
| 1294 | */ |
| 1295 | |
| 1296 | // USER TYPE |
| 1297 | case "user_type": |
| 1298 | |
| 1299 | if($rule['operator'] == "==") |
| 1300 | { |
| 1301 | if(current_user_can($rule['value'])) |
| 1302 | { |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
| 1306 | return false; |
| 1307 | } |
| 1308 | elseif($rule['operator'] == "!=") |
| 1309 | { |
| 1310 | if(!current_user_can($rule['value'])) |
| 1311 | { |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
| 1318 | break; |
| 1319 | |
| 1320 | // Options Page |
| 1321 | case "options_page": |
| 1322 | |
| 1323 | if($rule['operator'] == "==") |
| 1324 | { |
| 1325 | if(get_admin_page_title() == $rule['value']) |
| 1326 | { |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
| 1330 | return false; |
| 1331 | } |
| 1332 | elseif($rule['operator'] == "!=") |
| 1333 | { |
| 1334 | if(get_admin_page_title() != $rule['value']) |
| 1335 | { |
| 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
| 1342 | break; |
| 1343 | |
| 1344 | |
| 1345 | // Post Format |
| 1346 | case "post_format": |
| 1347 | |
| 1348 | |
| 1349 | $post_format = isset($overrides['post_format']) ? has_post_format($overrides['post_format'],$post->ID) : has_post_format($rule['value'],$post->ID); |
| 1350 | |
| 1351 | if($rule['operator'] == "==") |
| 1352 | { |
| 1353 | if($post_format) |
| 1354 | { |
| 1355 | return true; |
| 1356 | } |
| 1357 | |
| 1358 | return false; |
| 1359 | } |
| 1360 | elseif($rule['operator'] == "!=") |
| 1361 | { |
| 1362 | if(!$post_format) |
| 1363 | { |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
| 1367 | return false; |
| 1368 | } |
| 1369 | |
| 1370 | break; |
| 1371 | |
| 1372 | // Taxonomy |
| 1373 | case "taxonomy": |
| 1374 | |
| 1375 | $terms = array(); |
| 1376 | |
| 1377 | if(isset($overrides['taxonomy'])) |
| 1378 | { |
| 1379 | $terms = $overrides['taxonomy']; |
| 1380 | } |
| 1381 | else |
| 1382 | { |
| 1383 | $taxonomies = get_object_taxonomies($post->post_type); |
| 1384 | if($taxonomies) |
| 1385 | { |
| 1386 | foreach($taxonomies as $tax) |
| 1387 | { |
| 1388 | $all_terms = get_the_terms($post->ID, $tax); |
| 1389 | if($all_terms) |
| 1390 | { |
| 1391 | foreach($all_terms as $all_term) |
| 1392 | { |
| 1393 | $terms[] = $all_term->term_id; |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | if($rule['operator'] == "==") |
| 1401 | { |
| 1402 | if($terms) |
| 1403 | { |
| 1404 | if(in_array($rule['value'], $terms)) |
| 1405 | { |
| 1406 | return true; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | return false; |
| 1411 | } |
| 1412 | elseif($rule['operator'] == "!=") |
| 1413 | { |
| 1414 | if($terms) |
| 1415 | { |
| 1416 | if(!in_array($rule['value'], $terms)) |
| 1417 | { |
| 1418 | return true; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
| 1425 | |
| 1426 | break; |
| 1427 | |
| 1428 | } |
| 1429 | |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | /*-------------------------------------------------------------------------------------- |
| 1434 | * |
| 1435 | * is_field_unlocked |
| 1436 | * |
| 1437 | * @author Elliot Condon |
| 1438 | * @since 3.0.0 |
| 1439 | * |
| 1440 | *-------------------------------------------------------------------------------------*/ |
| 1441 | |
| 1442 | function is_field_unlocked($field_name) |
| 1443 | { |
| 1444 | switch ($field_name) { |
| 1445 | case 'repeater': |
| 1446 | if(md5($this->get_license_key($field_name)) == "bbefed143f1ec106ff3a11437bd73432"){ return true; }else{ return false; } |
| 1447 | break; |
| 1448 | case 'options_page': |
| 1449 | if(md5($this->get_license_key($field_name)) == "1fc8b993548891dc2b9a63ac057935d8"){ return true; }else{ return false; } |
| 1450 | break; |
| 1451 | case 'flexible_content': |
| 1452 | if(md5($this->get_license_key($field_name)) == "d067e06c2b4b32b1c1f5b6f00e0d61d6"){ return true; }else{ return false; } |
| 1453 | break; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /*-------------------------------------------------------------------------------------- |
| 1458 | * |
| 1459 | * is_field_unlocked |
| 1460 | * |
| 1461 | * @author Elliot Condon |
| 1462 | * @since 3.0.0 |
| 1463 | * |
| 1464 | *-------------------------------------------------------------------------------------*/ |
| 1465 | |
| 1466 | function get_license_key($field_name) |
| 1467 | { |
| 1468 | return get_option('acf_' . $field_name . '_ac'); |
| 1469 | } |
| 1470 | |
| 1471 | |
| 1472 | /*-------------------------------------------------------------------------------------- |
| 1473 | * |
| 1474 | * admin_message |
| 1475 | * |
| 1476 | * @author Elliot Condon |
| 1477 | * @since 2.0.5 |
| 1478 | * |
| 1479 | *-------------------------------------------------------------------------------------*/ |
| 1480 | |
| 1481 | function admin_message($message = "", $type = 'updated') |
| 1482 | { |
| 1483 | $GLOBALS['acf_mesage'] = $message; |
| 1484 | $GLOBALS['acf_mesage_type'] = $type; |
| 1485 | |
| 1486 | function my_admin_notice() |
| 1487 | { |
| 1488 | echo '<div class="' . $GLOBALS['acf_mesage_type'] . '" id="message">'.$GLOBALS['acf_mesage'].'</div>'; |
| 1489 | } |
| 1490 | add_action('admin_notices', 'my_admin_notice'); |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | /*-------------------------------------------------------------------------------------- |
| 1496 | * |
| 1497 | * get_taxonomies_for_select |
| 1498 | * |
| 1499 | *--------------------------------------------------------------------------------------- |
| 1500 | * |
| 1501 | * returns a multidimentional array of taxonomies grouped by the post type / taxonomy |
| 1502 | * |
| 1503 | * @author Elliot Condon |
| 1504 | * @since 3.0.2 |
| 1505 | * |
| 1506 | *-------------------------------------------------------------------------------------*/ |
| 1507 | |
| 1508 | function get_taxonomies_for_select() |
| 1509 | { |
| 1510 | $post_types = get_post_types(); |
| 1511 | $choices = array(); |
| 1512 | |
| 1513 | if($post_types) |
| 1514 | { |
| 1515 | foreach($post_types as $post_type) |
| 1516 | { |
| 1517 | $post_type_object = get_post_type_object($post_type); |
| 1518 | $taxonomies = get_object_taxonomies($post_type); |
| 1519 | if($taxonomies) |
| 1520 | { |
| 1521 | foreach($taxonomies as $taxonomy) |
| 1522 | { |
| 1523 | $terms = get_terms($taxonomy, array('hide_empty' => false)); |
| 1524 | if($terms) |
| 1525 | { |
| 1526 | foreach($terms as $term) |
| 1527 | { |
| 1528 | $choices[$post_type_object->label . ': ' . $taxonomy][$term->term_id] = $term->name; |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | return $choices; |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | function in_taxonomy($post, $ids) |
| 1541 | { |
| 1542 | $terms = array(); |
| 1543 | |
| 1544 | $taxonomies = get_object_taxonomies($post->post_type); |
| 1545 | if($taxonomies) |
| 1546 | { |
| 1547 | foreach($taxonomies as $tax) |
| 1548 | { |
| 1549 | $all_terms = get_the_terms($post->ID, $tax); |
| 1550 | if($all_terms) |
| 1551 | { |
| 1552 | foreach($all_terms as $all_term) |
| 1553 | { |
| 1554 | $terms[] = $all_term->term_id; |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | if($terms) |
| 1561 | { |
| 1562 | if(is_array($ids)) |
| 1563 | { |
| 1564 | foreach($ids as $id) |
| 1565 | { |
| 1566 | if(in_array($id, $terms)) |
| 1567 | { |
| 1568 | return true; |
| 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | else |
| 1573 | { |
| 1574 | if(in_array($ids, $terms)) |
| 1575 | { |
| 1576 | return true; |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | return false; |
| 1582 | |
| 1583 | } |
| 1584 | |
| 1585 | } |
| 1586 | ?> |