| 1 |
<?php |
| 2 |
|
| 3 |
// set some globals |
| 4 |
reset_the_repeater_field(); |
| 5 |
|
| 6 |
|
| 7 |
/*-------------------------------------------------------------------------------------- |
| 8 |
* |
| 9 |
* get_fields |
| 10 |
* |
| 11 |
* @author Elliot Condon |
| 12 |
* @since 1.0.3 |
| 13 |
* |
| 14 |
*-------------------------------------------------------------------------------------*/ |
| 15 |
|
| 16 |
function get_fields($post_id = false) |
| 17 |
{ |
| 18 |
// vars |
| 19 |
global $post; |
| 20 |
|
| 21 |
if(!$post_id) |
| 22 |
{ |
| 23 |
$post_id = $post->ID; |
| 24 |
} |
| 25 |
elseif($post_id == "options") |
| 26 |
{ |
| 27 |
$post_id = 999999999; |
| 28 |
} |
| 29 |
|
| 30 |
// default |
| 31 |
$value = array(); |
| 32 |
|
| 33 |
$keys = get_post_custom_keys($post_id); |
| 34 |
|
| 35 |
if($keys) |
| 36 |
{ |
| 37 |
foreach($keys as $key) |
| 38 |
{ |
| 39 |
if(substr($key, 0, 1) != "_") |
| 40 |
{ |
| 41 |
$value[$key] = get_field($key, $post_id); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// no value |
| 47 |
if(empty($value)) |
| 48 |
{ |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
return $value; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
/*-------------------------------------------------------------------------------------- |
| 59 |
* |
| 60 |
* get_field |
| 61 |
* |
| 62 |
* @author Elliot Condon |
| 63 |
* @since 1.0.3 |
| 64 |
* |
| 65 |
*-------------------------------------------------------------------------------------*/ |
| 66 |
|
| 67 |
function get_field($field_name, $post_id = false) |
| 68 |
{ |
| 69 |
global $post, $acf; |
| 70 |
|
| 71 |
if(!$post_id) |
| 72 |
{ |
| 73 |
$post_id = $post->ID; |
| 74 |
} |
| 75 |
elseif($post_id == "options") |
| 76 |
{ |
| 77 |
$post_id = 999999999; |
| 78 |
} |
| 79 |
|
| 80 |
// default |
| 81 |
$value = ""; |
| 82 |
|
| 83 |
// get value |
| 84 |
$field_key = get_post_meta($post_id, '_' . $field_name, true); |
| 85 |
|
| 86 |
if($field_key != "") |
| 87 |
{ |
| 88 |
// we can load the field properly! |
| 89 |
$field = $acf->get_acf_field($field_key); |
| 90 |
$value = $acf->get_value_for_api($post_id, $field); |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
// just load the text version |
| 95 |
$value = get_post_meta($post_id, $field_name, true); |
| 96 |
} |
| 97 |
|
| 98 |
// no value? |
| 99 |
if($value == "") $value = false; |
| 100 |
|
| 101 |
return $value; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/*-------------------------------------------------------------------------------------- |
| 107 |
* |
| 108 |
* the_field |
| 109 |
* |
| 110 |
* @author Elliot Condon |
| 111 |
* @since 1.0.3 |
| 112 |
* |
| 113 |
*-------------------------------------------------------------------------------------*/ |
| 114 |
|
| 115 |
function the_field($field_name, $post_id = false) |
| 116 |
{ |
| 117 |
$value = get_field($field_name, $post_id); |
| 118 |
|
| 119 |
if(is_array($value)) |
| 120 |
{ |
| 121 |
$value = @implode(', ',$value); |
| 122 |
} |
| 123 |
|
| 124 |
echo $value; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/*-------------------------------------------------------------------------------------- |
| 129 |
* |
| 130 |
* the_repeater_field |
| 131 |
* |
| 132 |
* @author Elliot Condon |
| 133 |
* @since 1.0.3 |
| 134 |
* |
| 135 |
*-------------------------------------------------------------------------------------*/ |
| 136 |
|
| 137 |
function the_repeater_field($field_name, $post_id = false) |
| 138 |
{ |
| 139 |
|
| 140 |
// if no field, create field + reset count |
| 141 |
if(!$GLOBALS['acf_field']) |
| 142 |
{ |
| 143 |
reset_the_repeater_field(); |
| 144 |
$GLOBALS['acf_field'] = get_field($field_name, $post_id); |
| 145 |
} |
| 146 |
|
| 147 |
// increase order_no |
| 148 |
$GLOBALS['acf_count']++; |
| 149 |
|
| 150 |
// vars |
| 151 |
$field = $GLOBALS['acf_field']; |
| 152 |
$i = $GLOBALS['acf_count']; |
| 153 |
|
| 154 |
if(isset($field[$i])) |
| 155 |
{ |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
// no row, reset the global values |
| 160 |
reset_the_repeater_field(); |
| 161 |
return false; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
function the_flexible_field($field_name, $post_id = false) |
| 166 |
{ |
| 167 |
return the_repeater_field($field_name, $post_id); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/*-------------------------------------------------------------------------------------- |
| 172 |
* |
| 173 |
* reset_the_repeater_field |
| 174 |
* |
| 175 |
* @author Elliot Condon |
| 176 |
* @since 1.0.3 |
| 177 |
* |
| 178 |
*-------------------------------------------------------------------------------------*/ |
| 179 |
|
| 180 |
function reset_the_repeater_field() |
| 181 |
{ |
| 182 |
$GLOBALS['acf_field'] = false; |
| 183 |
$GLOBALS['acf_count'] = -1; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/*-------------------------------------------------------------------------------------- |
| 188 |
* |
| 189 |
* get_sub_field |
| 190 |
* |
| 191 |
* @author Elliot Condon |
| 192 |
* @since 1.0.3 |
| 193 |
* |
| 194 |
*-------------------------------------------------------------------------------------*/ |
| 195 |
|
| 196 |
function get_sub_field($field_name) |
| 197 |
{ |
| 198 |
|
| 199 |
// vars |
| 200 |
$field = $GLOBALS['acf_field']; |
| 201 |
$i = $GLOBALS['acf_count']; |
| 202 |
|
| 203 |
// no value |
| 204 |
if(!$field) return false; |
| 205 |
|
| 206 |
if(!isset($field[$i][$field_name])) return false; |
| 207 |
|
| 208 |
return $field[$i][$field_name]; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/*-------------------------------------------------------------------------------------- |
| 213 |
* |
| 214 |
* the_sub_field |
| 215 |
* |
| 216 |
* @author Elliot Condon |
| 217 |
* @since 1.0.3 |
| 218 |
* |
| 219 |
*-------------------------------------------------------------------------------------*/ |
| 220 |
|
| 221 |
function the_sub_field($field_name, $field = false) |
| 222 |
{ |
| 223 |
$value = get_sub_field($field_name, $field); |
| 224 |
|
| 225 |
if(is_array($value)) |
| 226 |
{ |
| 227 |
$value = implode(', ',$value); |
| 228 |
} |
| 229 |
|
| 230 |
echo $value; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/*-------------------------------------------------------------------------------------- |
| 235 |
* |
| 236 |
* register_field |
| 237 |
* |
| 238 |
* @author Elliot Condon |
| 239 |
* @since 3.0.0 |
| 240 |
* |
| 241 |
*-------------------------------------------------------------------------------------*/ |
| 242 |
|
| 243 |
$GLOBALS['acf_register_field'] = array(); |
| 244 |
|
| 245 |
function register_field($class = "", $url = "") |
| 246 |
{ |
| 247 |
$GLOBALS['acf_register_field'][] = array( |
| 248 |
'url' => $url, |
| 249 |
'class' => $class, |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
function acf_register_field($array) |
| 254 |
{ |
| 255 |
$array = array_merge($array, $GLOBALS['acf_register_field']); |
| 256 |
|
| 257 |
return $array; |
| 258 |
} |
| 259 |
add_filter('acf_register_field', 'acf_register_field'); |
| 260 |
|
| 261 |
|
| 262 |
/*-------------------------------------------------------------------------------------- |
| 263 |
* |
| 264 |
* register_field_group |
| 265 |
* |
| 266 |
* @author Elliot Condon |
| 267 |
* @since 3.0.6 |
| 268 |
* |
| 269 |
*-------------------------------------------------------------------------------------*/ |
| 270 |
|
| 271 |
$GLOBALS['acf_register_field_group'] = array(); |
| 272 |
|
| 273 |
function register_field_group($array) |
| 274 |
{ |
| 275 |
// add id |
| 276 |
$array['id'] = uniqid(); |
| 277 |
$GLOBALS['acf_register_field_group'][] = $array; |
| 278 |
} |
| 279 |
|
| 280 |
function acf_register_field_group($array) |
| 281 |
{ |
| 282 |
$array = array_merge($array, $GLOBALS['acf_register_field_group']); |
| 283 |
|
| 284 |
// order field groups based on menu_order |
| 285 |
// Obtain a list of columns |
| 286 |
foreach ($array as $key => $row) { |
| 287 |
$menu_order[$key] = $row['menu_order']; |
| 288 |
} |
| 289 |
|
| 290 |
// Sort the array with menu_order ascending |
| 291 |
// Add $array as the last parameter, to sort by the common key |
| 292 |
if(isset($menu_order)) |
| 293 |
{ |
| 294 |
array_multisort($menu_order, SORT_ASC, $array); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
return $array; |
| 299 |
} |
| 300 |
add_filter('acf_register_field_group', 'acf_register_field_group'); |
| 301 |
|
| 302 |
|
| 303 |
/*-------------------------------------------------------------------------------------- |
| 304 |
* |
| 305 |
* register_options_page |
| 306 |
* |
| 307 |
* @author Elliot Condon |
| 308 |
* @since 3.0.0 |
| 309 |
* |
| 310 |
*-------------------------------------------------------------------------------------*/ |
| 311 |
|
| 312 |
$GLOBALS['acf_register_options_page'] = array(); |
| 313 |
|
| 314 |
function register_options_page($title = "") |
| 315 |
{ |
| 316 |
$GLOBALS['acf_register_options_page'][] = array( |
| 317 |
'title' => $title, |
| 318 |
'slug' => 'options-' . sanitize_title_with_dashes( $title ), |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
function acf_register_options_page($array) |
| 323 |
{ |
| 324 |
$array = array_merge($array, $GLOBALS['acf_register_options_page']); |
| 325 |
|
| 326 |
return $array; |
| 327 |
} |
| 328 |
add_filter('acf_register_options_page', 'acf_register_options_page'); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
/*-------------------------------------------------------------------------------------- |
| 333 |
* |
| 334 |
* get_sub_field |
| 335 |
* |
| 336 |
* @author Elliot Condon |
| 337 |
* @since 1.0.3 |
| 338 |
* |
| 339 |
*-------------------------------------------------------------------------------------*/ |
| 340 |
|
| 341 |
function get_row_layout() |
| 342 |
{ |
| 343 |
|
| 344 |
// vars |
| 345 |
$field = $GLOBALS['acf_field']; |
| 346 |
$i = $GLOBALS['acf_count']; |
| 347 |
|
| 348 |
// no value |
| 349 |
if(!$field) return false; |
| 350 |
|
| 351 |
if(!isset($field[$i]['acf_fc_layout'])) return false; |
| 352 |
|
| 353 |
return $field[$i]['acf_fc_layout']; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/*-------------------------------------------------------------------------------------- |
| 358 |
* |
| 359 |
* shorcode support |
| 360 |
* |
| 361 |
* @author Elliot Condon |
| 362 |
* @since 1.1.1 |
| 363 |
* |
| 364 |
*-------------------------------------------------------------------------------------*/ |
| 365 |
|
| 366 |
function acf_shortcode( $atts ) |
| 367 |
{ |
| 368 |
// extract attributs |
| 369 |
extract( shortcode_atts( array( |
| 370 |
'field' => "" |
| 371 |
), $atts ) ); |
| 372 |
|
| 373 |
// $field is requird |
| 374 |
if(!$field || $field == "") |
| 375 |
{ |
| 376 |
return ""; |
| 377 |
} |
| 378 |
|
| 379 |
// get value and return it |
| 380 |
$value = get_field($field); |
| 381 |
|
| 382 |
if(is_array($value)) |
| 383 |
{ |
| 384 |
$value = @implode(', ',$value); |
| 385 |
} |
| 386 |
|
| 387 |
return $value; |
| 388 |
} |
| 389 |
add_shortcode( 'acf', 'acf_shortcode' ); |
| 390 |
|
| 391 |
|
| 392 |
/*-------------------------------------------------------------------------------------- |
| 393 |
* |
| 394 |
* Front end form Head |
| 395 |
* |
| 396 |
* @author Elliot Condon |
| 397 |
* @since 1.1.4 |
| 398 |
* |
| 399 |
*-------------------------------------------------------------------------------------*/ |
| 400 |
|
| 401 |
function acf_form_head() |
| 402 |
{ |
| 403 |
// global vars |
| 404 |
global $acf; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
// run database save first |
| 409 |
if(isset($_POST) && isset($_POST['acf_save'])) |
| 410 |
{ |
| 411 |
$post_id = $_POST['post_id']; |
| 412 |
|
| 413 |
// save |
| 414 |
// strip slashes |
| 415 |
$_POST = array_map('stripslashes_deep', $_POST); |
| 416 |
|
| 417 |
// save fields |
| 418 |
$fields = $_POST['fields']; |
| 419 |
|
| 420 |
if($fields) |
| 421 |
{ |
| 422 |
foreach($fields as $key => $value) |
| 423 |
{ |
| 424 |
// get field |
| 425 |
$field = $acf->get_acf_field($key); |
| 426 |
|
| 427 |
$acf->update_value($post_id, $field, $value); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
// redirect |
| 433 |
if(isset($_POST['return'])) |
| 434 |
{ |
| 435 |
wp_redirect($_POST['return']); |
| 436 |
exit; |
| 437 |
} |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
// register css / javascript |
| 443 |
foreach($acf->fields as $field) |
| 444 |
{ |
| 445 |
$acf->fields[$field->name]->admin_print_scripts(); |
| 446 |
$acf->fields[$field->name]->admin_print_styles(); |
| 447 |
} |
| 448 |
wp_enqueue_style(array( |
| 449 |
'colors-fresh' |
| 450 |
)); |
| 451 |
|
| 452 |
|
| 453 |
// form was not posted, load js head stuff |
| 454 |
add_action('wp_head', 'acf_form_wp_head'); |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
function acf_form_wp_head() |
| 459 |
{ |
| 460 |
// global vars |
| 461 |
global $post, $acf; |
| 462 |
|
| 463 |
|
| 464 |
// fields admin_head |
| 465 |
foreach($acf->fields as $field) |
| 466 |
{ |
| 467 |
$acf->fields[$field->name]->admin_head(); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
// Style |
| 472 |
echo '<link rel="stylesheet" type="text/css" href="'.$acf->dir.'/css/global.css" />'; |
| 473 |
echo '<link rel="stylesheet" type="text/css" href="'.$acf->dir.'/css/input.css" />'; |
| 474 |
|
| 475 |
|
| 476 |
// Javascript |
| 477 |
echo '<script type="text/javascript" src="'.$acf->dir.'/js/input-actions.js" ></script>'; |
| 478 |
echo '<script type="text/javascript"> |
| 479 |
acf.validation_message = "' . __("Validation Failed. One or more fields below are required.",'acf') . '"; |
| 480 |
acf.post_id = ' . $post->ID . '; |
| 481 |
acf.editor_mode = "wysiwyg"; |
| 482 |
acf.admin_url = "' . admin_url() . '"; |
| 483 |
</script>'; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
/*-------------------------------------------------------------------------------------- |
| 488 |
* |
| 489 |
* Front end form |
| 490 |
* |
| 491 |
* @author Elliot Condon |
| 492 |
* @since 1.1.4 |
| 493 |
* |
| 494 |
*-------------------------------------------------------------------------------------*/ |
| 495 |
|
| 496 |
function acf_form($options = null) |
| 497 |
{ |
| 498 |
global $post, $acf; |
| 499 |
|
| 500 |
|
| 501 |
// defaults |
| 502 |
$defaults = array( |
| 503 |
'post_id' => $post->ID, // post id to get field groups from and save data to |
| 504 |
'field_groups' => array(), // this will find the field groups for this post |
| 505 |
'form_attributes' => array( // attributes will be added to the form element |
| 506 |
'class' => '' |
| 507 |
), |
| 508 |
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url |
| 509 |
'html_field_open' => '<div class="field">', // field wrapper open |
| 510 |
'html_field_close' => '</div>', // field wrapper close |
| 511 |
'html_before_fields' => '', // html inside form before fields |
| 512 |
'html_after_fields' => '', // html inside form after fields |
| 513 |
'submit_value' => 'Update', // vale for submit field |
| 514 |
'updated_message' => 'Post updated.', // default updated message. Can be false |
| 515 |
); |
| 516 |
|
| 517 |
|
| 518 |
// merge defaults with options |
| 519 |
if($options && is_array($options)) |
| 520 |
{ |
| 521 |
$options = array_merge($defaults, $options); |
| 522 |
} |
| 523 |
else |
| 524 |
{ |
| 525 |
$options = $defaults; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
// post_id for options page |
| 530 |
if($options['post_id'] == "options") |
| 531 |
{ |
| 532 |
$options['post_id'] = 999999999; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
// register post box |
| 537 |
if(!$options['field_groups']) |
| 538 |
{ |
| 539 |
$options['field_groups'] = $acf->get_input_metabox_ids(array('post_id' => $options['post_id']), false); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
// updated message |
| 544 |
if(isset($_GET['updated']) && $_GET['updated'] == 'true' && $options['updated_message']) |
| 545 |
{ |
| 546 |
echo '<div id="message" class="updated"><p>' . $options['updated_message'] . '</p></div>'; |
| 547 |
} |
| 548 |
|
| 549 |
// display form |
| 550 |
?> |
| 551 |
<form action="" id="post" method="post" <?php if($options['form_attributes']){foreach($options['form_attributes'] as $k => $v){echo $k . '="' . $v .'" '; }} ?>> |
| 552 |
<div style="display:none"> |
| 553 |
<input type="hidden" name="acf_save" value="true" /> |
| 554 |
<input type="hidden" name="post_id" value="<?php echo $options['post_id']; ?>" /> |
| 555 |
<input type="hidden" name="return" value="<?php echo $options['return']; ?>" /> |
| 556 |
<?php wp_editor('', 'acf-temp-editor'); ?> |
| 557 |
</div> |
| 558 |
|
| 559 |
<div id="poststuff"> |
| 560 |
<div class="acf_postbox"> |
| 561 |
<?php |
| 562 |
|
| 563 |
// html before fields |
| 564 |
echo $defaults['html_before_fields']; |
| 565 |
|
| 566 |
$field_groups = $acf->get_field_groups(); |
| 567 |
if($field_groups): |
| 568 |
foreach($field_groups as $field_group): |
| 569 |
|
| 570 |
if(!in_array($field_group['id'], $options['field_groups'])) continue; |
| 571 |
|
| 572 |
|
| 573 |
// defaults |
| 574 |
if(!$field_group['options']) |
| 575 |
{ |
| 576 |
$field_group['options'] = array( |
| 577 |
'layout' => 'default' |
| 578 |
); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
if($field_group['fields']) |
| 583 |
{ |
| 584 |
|
| 585 |
echo '<div class="options" data-layout="' . $field_group['options']['layout'] . '"></div>'; |
| 586 |
foreach($field_group['fields'] as $field) |
| 587 |
{ |
| 588 |
// if they didn't select a type, skip this field |
| 589 |
if($field['type'] == 'null') continue; |
| 590 |
|
| 591 |
// set value |
| 592 |
$field['value'] = $acf->get_value($options['post_id'], $field); |
| 593 |
|
| 594 |
// required |
| 595 |
if(!isset($field['required'])) |
| 596 |
{ |
| 597 |
$field['required'] == "0"; |
| 598 |
} |
| 599 |
|
| 600 |
$required_class = ""; |
| 601 |
$required_label = ""; |
| 602 |
|
| 603 |
if($field['required'] == "1") |
| 604 |
{ |
| 605 |
$required_class = ' required'; |
| 606 |
$required_label = ' <span class="required">*</span>'; |
| 607 |
} |
| 608 |
|
| 609 |
echo '<div class="field field-' . $field['type'] . $required_class . '">'; |
| 610 |
|
| 611 |
echo '<label class="field_label" for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>'; |
| 612 |
if($field['instructions']) echo '<p class="instructions">' . $field['instructions'] . '</p>'; |
| 613 |
|
| 614 |
$field['name'] = 'fields[' . $field['key'] . ']'; |
| 615 |
$acf->create_field($field); |
| 616 |
|
| 617 |
echo '</div>'; |
| 618 |
|
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
endforeach; |
| 623 |
endif; |
| 624 |
|
| 625 |
// html after fields |
| 626 |
echo $defaults['html_after_fields']; |
| 627 |
|
| 628 |
?> |
| 629 |
<div class="field"> |
| 630 |
<input type="submit" value="<?php echo $options['submit_value']; ?>" /> |
| 631 |
</div> |
| 632 |
</div> |
| 633 |
</div> |
| 634 |
</form> |
| 635 |
|
| 636 |
|
| 637 |
<?php |
| 638 |
|
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
?> |