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