admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
template-tags.php
1401 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_template_tags')): |
| 8 | |
| 9 | class acfe_template_tags{ |
| 10 | |
| 11 | // vars |
| 12 | public $tags = array(); |
| 13 | public $context = array(); |
| 14 | |
| 15 | /** |
| 16 | * construct |
| 17 | */ |
| 18 | function __construct(){ |
| 19 | |
| 20 | // {field:my_field} |
| 21 | // {field:my_field:false} |
| 22 | // {field:field_5d5f3b2b3e3e4} |
| 23 | $this->add_tag(array( |
| 24 | 'name' => 'field', |
| 25 | 'priority' => 5, |
| 26 | 'condition' => function($args){ |
| 27 | return !acf_is_empty($args); |
| 28 | }, |
| 29 | 'resolver' => function($args){ |
| 30 | |
| 31 | // extract args |
| 32 | $keys = explode(':', $args); |
| 33 | $name = array_shift($keys); |
| 34 | $format = array_shift($keys); |
| 35 | |
| 36 | // validate |
| 37 | $format = $format && $format === 'false' ? false : true; |
| 38 | |
| 39 | // vars |
| 40 | $setup_meta = !acfe_is_local_meta() && !empty($_POST['acf']); |
| 41 | |
| 42 | if($setup_meta){ |
| 43 | acfe_setup_meta(wp_unslash($_POST['acf']), 'acfe/tag/field', true); |
| 44 | } |
| 45 | |
| 46 | // vars |
| 47 | $post_id = acf_get_valid_post_id(); |
| 48 | $field = acf_maybe_get_field($name, $post_id, false); |
| 49 | |
| 50 | if(!$field){ |
| 51 | |
| 52 | $field = acf_validate_field(array( |
| 53 | 'name' => $name, |
| 54 | 'key' => '', |
| 55 | 'type' => '', |
| 56 | )); |
| 57 | |
| 58 | // prevent formatting |
| 59 | $format = false; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | // field_key |
| 64 | // get_field('my_group_my_sub_field') can retrieve values from group subfield |
| 65 | // but get_field('field_abcdef123456') cannot, thus this implementation |
| 66 | // apply acf/load_value filter so payment/date range fields generate dynamic subfields |
| 67 | if(acf_is_field_key($name)){ |
| 68 | $acf = acfe_get_fields(); // pass via acf_get_value() > acf/load_value |
| 69 | $value = acfe_get_value_from_acf_values_by_key($acf, $name); |
| 70 | |
| 71 | // field name |
| 72 | }else{ |
| 73 | $value = acf_get_value($post_id, $field); |
| 74 | } |
| 75 | |
| 76 | if($setup_meta){ |
| 77 | acfe_reset_meta(); |
| 78 | } |
| 79 | |
| 80 | // format via context |
| 81 | if($this->has_context('format')){ |
| 82 | $format = $this->get_context('format'); |
| 83 | } |
| 84 | |
| 85 | // context = save |
| 86 | // never format wysiwyg in save context |
| 87 | // to avoid shortcodes to be saved as interpreted |
| 88 | if($this->get_context('context') === 'save'){ |
| 89 | if($field['type'] === 'wysiwyg'){ |
| 90 | $format = false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // unformat = field_type |
| 95 | // never format specific fields in unformat context |
| 96 | if($this->get_context('unformat') && in_array($field['type'], acfe_as_array($this->get_context('unformat')))){ |
| 97 | $format = false; |
| 98 | } |
| 99 | |
| 100 | // format value |
| 101 | if($format){ |
| 102 | $value = acfe_form_format_value($value, $field); |
| 103 | } |
| 104 | |
| 105 | // context display |
| 106 | // value is already unslashed when using name, via acfe_setup_meta() |
| 107 | if($this->get_context('context') === 'display' && acf_is_field_key($name)){ |
| 108 | $value = wp_unslash($value); |
| 109 | } |
| 110 | |
| 111 | // return |
| 112 | return $value; |
| 113 | |
| 114 | } |
| 115 | )); |
| 116 | |
| 117 | // {get_field:my_field} |
| 118 | // {get_field:my_field:145} |
| 119 | // {get_field:my_field:145:false} |
| 120 | // {get_field:field_5d5f3b2b3e3e4} |
| 121 | $this->add_tag(array( |
| 122 | 'name' => 'get_field', |
| 123 | 'condition' => function($args){ |
| 124 | return !acf_is_empty($args); |
| 125 | }, |
| 126 | 'resolver' => function($args){ |
| 127 | |
| 128 | // extract args |
| 129 | $keys = explode(':', $args); |
| 130 | $name = array_shift($keys); |
| 131 | $post_id = array_shift($keys); |
| 132 | $format = array_shift($keys); |
| 133 | |
| 134 | // validate |
| 135 | $post_id = $post_id && ($post_id === 'false' || $post_id === 'current') ? false : $post_id; |
| 136 | $format = $format && $format === 'false' ? false : true; |
| 137 | |
| 138 | // return |
| 139 | return get_field($name, $post_id, $format); |
| 140 | |
| 141 | } |
| 142 | )); |
| 143 | |
| 144 | // {fields} |
| 145 | $this->add_tag(array( |
| 146 | 'name' => 'fields', |
| 147 | 'resolver' => function($args){ |
| 148 | |
| 149 | $values = acfe_get_fields(); |
| 150 | $tag = $this->get_tag('field'); |
| 151 | |
| 152 | if(!$values){ |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // html |
| 157 | $html = ''; |
| 158 | |
| 159 | // loop raw values |
| 160 | foreach($values as $field_key => $unformatted){ |
| 161 | |
| 162 | // get field array |
| 163 | $field = acf_get_field($field_key); |
| 164 | |
| 165 | if($field){ |
| 166 | |
| 167 | // bypass |
| 168 | if($field['key'] === '_validate_email' || $field['type'] === 'acfe_recaptcha'){ |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | // label |
| 173 | $label = !empty($field['label']) ? $field['label'] : $field['name']; |
| 174 | |
| 175 | $formatted = call_user_func_array($tag['resolver'], array($field_key)); |
| 176 | |
| 177 | // html |
| 178 | $html .= "$label: $formatted<br />\n"; |
| 179 | |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
| 184 | // get form context |
| 185 | $form = $this->get_context('form', array('name' => '')); // default for deprecated filter |
| 186 | |
| 187 | // depreacted filters |
| 188 | $html = apply_filters_deprecated("acfe/form/template_tag/fields", array($html, array(), $form), '0.9'); |
| 189 | $html = apply_filters_deprecated("acfe/form/template_tag/fields/form={$form['name']}", array($html, array(), $form), '0.9'); |
| 190 | |
| 191 | // return |
| 192 | return $html; |
| 193 | |
| 194 | } |
| 195 | )); |
| 196 | |
| 197 | // {render:group_5d5f3b2b3e3e4} |
| 198 | // {render:field_5d5f3b2b3e3e4} |
| 199 | // {render:my_field} |
| 200 | // {render:fields} |
| 201 | // {render:submit} |
| 202 | $this->add_tag(array( |
| 203 | 'name' => 'render', |
| 204 | 'condition' => function($args){ |
| 205 | return !acf_is_empty($args); |
| 206 | }, |
| 207 | 'resolver' => function($args){ |
| 208 | |
| 209 | // get context |
| 210 | $form = $this->get_context('form'); |
| 211 | if(!$form){ |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | // field key |
| 216 | if(acf_is_field_key($args)){ |
| 217 | |
| 218 | $field = acf_get_field($args); |
| 219 | if(!$field){ |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | $fields = array($field); |
| 224 | |
| 225 | // field group key |
| 226 | }elseif(acf_is_field_group_key($args)){ |
| 227 | |
| 228 | $field_group = acf_get_field_group($args); |
| 229 | if(!$field_group){ |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | $fields = acf_get_fields($field_group); |
| 234 | if(!$fields){ |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // fields |
| 239 | }elseif($args === 'fields'){ |
| 240 | |
| 241 | $fields = acf_get_instance('acfe_module_form_front_render')->get_allowed_fields($form); |
| 242 | |
| 243 | // submit |
| 244 | }elseif($args === 'submit'){ |
| 245 | |
| 246 | // form submit |
| 247 | if($form['attributes']['submit']){ |
| 248 | |
| 249 | ob_start(); |
| 250 | do_action("acfe/form/render_submit", $form); |
| 251 | return ob_get_clean(); |
| 252 | |
| 253 | } |
| 254 | |
| 255 | return false; |
| 256 | |
| 257 | // field name |
| 258 | }else{ |
| 259 | |
| 260 | // try to get field key using field name from mapped field groups |
| 261 | // otheriwse, fallback to field name |
| 262 | $args = $this->get_field_key_from_field_groups($args); |
| 263 | |
| 264 | // get field array |
| 265 | $field = acf_get_field($args); |
| 266 | |
| 267 | // field name |
| 268 | if($field){ |
| 269 | $fields = array($field); |
| 270 | |
| 271 | // field group ID |
| 272 | }else{ |
| 273 | |
| 274 | $field_group = acf_get_field_group($args); |
| 275 | if(!$field_group){ |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | $fields = acf_get_fields($field_group); |
| 280 | if(!$fields){ |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | |
| 288 | ob_start(); |
| 289 | acf_render_fields($fields, $form['uniqid'], $form['attributes']['fields']['element'], $form['attributes']['fields']['instruction']); |
| 290 | return ob_get_clean(); |
| 291 | |
| 292 | } |
| 293 | )); |
| 294 | |
| 295 | // {action:my-post} |
| 296 | // {action:my-post:ID} |
| 297 | $this->add_tag(array( |
| 298 | 'name' => 'action', |
| 299 | 'condition' => function($args){ |
| 300 | |
| 301 | $keys = explode(':', $args); |
| 302 | $name = array_shift($keys); |
| 303 | $action = acfe_get_form_action($name); |
| 304 | |
| 305 | return !acf_is_empty($name) && $action; |
| 306 | |
| 307 | }, |
| 308 | 'resolver' => function($args){ |
| 309 | |
| 310 | // extract args |
| 311 | $keys = explode(':', $args); |
| 312 | $name = array_shift($keys); |
| 313 | |
| 314 | // get action |
| 315 | $action = acfe_get_form_action($name); |
| 316 | if(!$action){ |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return $this->array_get($action, $keys); |
| 321 | |
| 322 | } |
| 323 | )); |
| 324 | |
| 325 | // {get_option:my_option} |
| 326 | // {get_option:my_option:child} |
| 327 | $this->add_tag(array( |
| 328 | 'name' => 'get_option', |
| 329 | 'condition' => function($args){ |
| 330 | return !acf_is_empty($args); |
| 331 | }, |
| 332 | 'resolver' => function($args){ |
| 333 | |
| 334 | // extract args |
| 335 | $keys = explode(':', $args); |
| 336 | $name = array_shift($keys); |
| 337 | |
| 338 | // get option |
| 339 | $option = get_option($name); |
| 340 | |
| 341 | // other arguments |
| 342 | if($keys){ |
| 343 | return $this->array_get($option, $keys); |
| 344 | } |
| 345 | |
| 346 | // return |
| 347 | return $option; |
| 348 | |
| 349 | } |
| 350 | )); |
| 351 | |
| 352 | // {request:url_param} |
| 353 | // {request:url_param:child} |
| 354 | $this->add_tag(array( |
| 355 | 'name' => 'request', |
| 356 | 'condition' => function($args){ |
| 357 | return !acf_is_empty($args); |
| 358 | }, |
| 359 | 'resolver' => function($args){ |
| 360 | return $this->array_get($_REQUEST, $args); |
| 361 | } |
| 362 | )); |
| 363 | |
| 364 | // {query_var:my_var} |
| 365 | // {query_var:my_var:child} |
| 366 | $this->add_tag(array( |
| 367 | 'name' => 'query_var', |
| 368 | 'condition' => function($args){ |
| 369 | return !acf_is_empty($args); |
| 370 | }, |
| 371 | 'resolver' => function($args){ |
| 372 | |
| 373 | // extract args |
| 374 | $keys = explode(':', $args); |
| 375 | $name = array_shift($keys); |
| 376 | |
| 377 | // get query var |
| 378 | $query_var = get_query_var($name); |
| 379 | |
| 380 | // other arguments |
| 381 | if($keys){ |
| 382 | return $this->array_get($query_var, $keys); |
| 383 | } |
| 384 | |
| 385 | return $query_var; |
| 386 | |
| 387 | } |
| 388 | )); |
| 389 | |
| 390 | // {post} |
| 391 | // {post:ID} |
| 392 | $this->add_tag(array( |
| 393 | 'name' => 'post', |
| 394 | 'resolver' => function($args){ |
| 395 | |
| 396 | // extract |
| 397 | $keys = explode(':', $args); |
| 398 | $keys = array_merge(array('0'), $keys); |
| 399 | |
| 400 | // merge back |
| 401 | $args = implode(':', $keys); |
| 402 | |
| 403 | // fallback to {get_post} |
| 404 | $tag = $this->get_tag('get_post'); |
| 405 | |
| 406 | return call_user_func_array($tag['resolver'], array($args)); |
| 407 | |
| 408 | } |
| 409 | )); |
| 410 | |
| 411 | // {get_post} |
| 412 | // {get_post:145} |
| 413 | // {get_post:145:ID} |
| 414 | $this->add_tag(array( |
| 415 | 'name' => 'get_post', |
| 416 | 'resolver' => function($args){ |
| 417 | |
| 418 | // extract args |
| 419 | $keys = explode(':', $args); |
| 420 | $post_id = (int) array_shift($keys); |
| 421 | |
| 422 | // current post |
| 423 | if(empty($post_id)){ |
| 424 | |
| 425 | // default |
| 426 | $post_id = null; |
| 427 | |
| 428 | // check form context exists |
| 429 | $form = $this->get_context('form'); |
| 430 | if($form){ |
| 431 | |
| 432 | // decode post_id |
| 433 | $decoded = acf_decode_post_id($form['post_id']); |
| 434 | |
| 435 | // check if post |
| 436 | if($decoded['type'] === 'post'){ |
| 437 | $post_id = $decoded['id']; |
| 438 | } |
| 439 | |
| 440 | } |
| 441 | |
| 442 | } |
| 443 | |
| 444 | // merge back |
| 445 | $args = implode(':', $keys); |
| 446 | |
| 447 | // default to ID |
| 448 | if(empty($args)){ |
| 449 | $args = 'ID'; |
| 450 | } |
| 451 | |
| 452 | // allow id/post_id |
| 453 | if(in_array(strtolower($args), array('id', 'post_id'), true)){ |
| 454 | $args = 'ID'; |
| 455 | } |
| 456 | |
| 457 | // get post |
| 458 | $post = get_post($post_id, ARRAY_A); |
| 459 | |
| 460 | // validate post found |
| 461 | if(!$post){ |
| 462 | |
| 463 | // allow to return ID '0' if no post found |
| 464 | if($args === 'ID'){ |
| 465 | return $post_id; |
| 466 | } |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // additional fields |
| 472 | $post['permalink'] = get_permalink($post['ID']); |
| 473 | $post['admin_url'] = admin_url("post.php?post={$post['ID']}&action=edit"); |
| 474 | $post['post_author_data'] = $this->get_user_array($post['post_author']); |
| 475 | |
| 476 | // return |
| 477 | return $this->array_get($post, $args); |
| 478 | |
| 479 | } |
| 480 | )); |
| 481 | |
| 482 | // {term} |
| 483 | // {term:term_id} |
| 484 | $this->add_tag(array( |
| 485 | 'name' => 'term', |
| 486 | 'resolver' => function($args){ |
| 487 | |
| 488 | // extract |
| 489 | $keys = explode(':', $args); |
| 490 | $keys = array_merge(array('0'), $keys); |
| 491 | |
| 492 | // merge back |
| 493 | $args = implode(':', $keys); |
| 494 | |
| 495 | // fallback to {get_term} |
| 496 | $tag = $this->get_tag('get_term'); |
| 497 | |
| 498 | return call_user_func_array($tag['resolver'], array($args)); |
| 499 | |
| 500 | |
| 501 | } |
| 502 | )); |
| 503 | |
| 504 | // {get_term} |
| 505 | // {get_term:45} |
| 506 | // {get_term:45:term_id} |
| 507 | $this->add_tag(array( |
| 508 | 'name' => 'get_term', |
| 509 | 'resolver' => function($args){ |
| 510 | |
| 511 | // extract args |
| 512 | $keys = explode(':', $args); |
| 513 | $term_id = (int) array_shift($keys); |
| 514 | |
| 515 | if(empty($term_id)){ |
| 516 | |
| 517 | $term_id = null; // current queried object |
| 518 | |
| 519 | // check form context exists |
| 520 | $form = $this->get_context('form'); |
| 521 | if($form){ |
| 522 | |
| 523 | // decode post_id |
| 524 | $decoded = acf_decode_post_id($form['post_id']); |
| 525 | |
| 526 | // check if post |
| 527 | if($decoded['type'] === 'term'){ |
| 528 | $term_id = $decoded['id']; |
| 529 | } |
| 530 | |
| 531 | } |
| 532 | |
| 533 | } |
| 534 | |
| 535 | // merge back |
| 536 | $args = implode(':', $keys); |
| 537 | |
| 538 | // default to ID |
| 539 | if(empty($args)){ |
| 540 | $args = 'term_id'; |
| 541 | } |
| 542 | |
| 543 | // allow id/term_id |
| 544 | if(in_array(strtolower($args), array('id', 'term_id'), true)){ |
| 545 | $args = 'term_id'; |
| 546 | } |
| 547 | |
| 548 | // get object |
| 549 | if($term_id === null){ |
| 550 | $object = get_queried_object(); |
| 551 | }else{ |
| 552 | $object = get_term($term_id); |
| 553 | } |
| 554 | |
| 555 | // validate term found |
| 556 | if(!$object || is_wp_error($object) || !is_a($object, 'WP_Term')){ |
| 557 | |
| 558 | // allow to return ID '0' if no term found |
| 559 | if($args === 'term_id'){ |
| 560 | return $term_id; |
| 561 | } |
| 562 | |
| 563 | return false; |
| 564 | |
| 565 | } |
| 566 | |
| 567 | // convert to array |
| 568 | $term = (array) $object; |
| 569 | |
| 570 | // additional fields |
| 571 | $term['permalink'] = get_term_link($term['term_id']); |
| 572 | $term['admin_url'] = admin_url("term.php?taxonomy={$term['taxonomy']}&tag_ID={$term['term_id']}"); |
| 573 | |
| 574 | // return |
| 575 | return $this->array_get($term, $args); |
| 576 | |
| 577 | |
| 578 | } |
| 579 | )); |
| 580 | |
| 581 | // {user} |
| 582 | // {user:ID} |
| 583 | $this->add_tag(array( |
| 584 | 'name' => 'user', |
| 585 | 'resolver' => function($args){ |
| 586 | |
| 587 | // extract |
| 588 | $keys = explode(':', $args); |
| 589 | $keys = array_merge(array('0'), $keys); |
| 590 | |
| 591 | // merge back |
| 592 | $args = implode(':', $keys); |
| 593 | |
| 594 | // fallback to {get_user} |
| 595 | $tag = $this->get_tag('get_user'); |
| 596 | |
| 597 | return call_user_func_array($tag['resolver'], array($args)); |
| 598 | |
| 599 | |
| 600 | } |
| 601 | )); |
| 602 | |
| 603 | |
| 604 | // {get_user} |
| 605 | // {get_user:25} |
| 606 | // {get_user:25:ID} |
| 607 | $this->add_tag(array( |
| 608 | 'name' => 'get_user', |
| 609 | 'resolver' => function($args){ |
| 610 | |
| 611 | // extract args |
| 612 | $keys = explode(':', $args); |
| 613 | $user_id = (int) array_shift($keys); |
| 614 | |
| 615 | if(empty($user_id)){ |
| 616 | $user_id = get_current_user_id(); // current user |
| 617 | } |
| 618 | |
| 619 | // merge back |
| 620 | $args = implode(':', $keys); |
| 621 | |
| 622 | // default to ID |
| 623 | if(empty($args)){ |
| 624 | $args = 'ID'; |
| 625 | } |
| 626 | |
| 627 | // allow id/user_id |
| 628 | if(in_array(strtolower($args), array('id', 'user_id'), true)){ |
| 629 | $args = 'ID'; |
| 630 | } |
| 631 | |
| 632 | // allow to return ID '0' if not logged in |
| 633 | if($args === 'ID'){ |
| 634 | return $user_id; |
| 635 | } |
| 636 | |
| 637 | // get user array |
| 638 | $user = $this->get_user_array($user_id); |
| 639 | |
| 640 | // validate |
| 641 | if(!$user){ |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | // return |
| 646 | return $this->array_get($user, $args); |
| 647 | |
| 648 | |
| 649 | } |
| 650 | )); |
| 651 | |
| 652 | // {author} |
| 653 | // {author:ID} |
| 654 | $this->add_tag(array( |
| 655 | 'name' => 'author', |
| 656 | 'resolver' => function($args){ |
| 657 | |
| 658 | // default id |
| 659 | $user_id = 0; |
| 660 | |
| 661 | // default to ID |
| 662 | if(empty($args)){ |
| 663 | $args = 'ID'; |
| 664 | } |
| 665 | |
| 666 | // allow id/user_id |
| 667 | if(in_array(strtolower($args), array('id', 'user_id'), true)){ |
| 668 | $args = 'ID'; |
| 669 | } |
| 670 | |
| 671 | // get post |
| 672 | $tag = $this->get_tag('post'); |
| 673 | $post_id = call_user_func_array($tag['resolver'], array('')); |
| 674 | |
| 675 | $post = get_post($post_id, ARRAY_A); |
| 676 | |
| 677 | // validate post found |
| 678 | if(!$post){ |
| 679 | |
| 680 | // allow to return ID '0' if no post found |
| 681 | if($args === 'ID'){ |
| 682 | return $user_id; |
| 683 | } |
| 684 | |
| 685 | return false; |
| 686 | |
| 687 | } |
| 688 | |
| 689 | // get author id |
| 690 | $user_id = (int) $post['post_author']; |
| 691 | $user = $this->get_user_array($user_id); |
| 692 | |
| 693 | // validate |
| 694 | if(!$user){ |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | // return |
| 699 | return $this->array_get($user, $args); |
| 700 | |
| 701 | } |
| 702 | )); |
| 703 | |
| 704 | // {form} |
| 705 | // {form:post_id} |
| 706 | $this->add_tag(array( |
| 707 | 'name' => 'form', |
| 708 | 'resolver' => function($args){ |
| 709 | |
| 710 | // get form |
| 711 | $form = $this->get_context('form'); |
| 712 | if(!$form){ |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | // default to ID |
| 717 | if(empty($args)){ |
| 718 | $args = 'ID'; |
| 719 | } |
| 720 | |
| 721 | // allow id/form_id |
| 722 | if(in_array(strtolower($args), array('id', 'form_id'), true)){ |
| 723 | $args = 'ID'; |
| 724 | } |
| 725 | |
| 726 | // allow name/form_name |
| 727 | if(in_array(strtolower($args), array('name', 'form_name'), true)){ |
| 728 | $args = 'name'; |
| 729 | } |
| 730 | |
| 731 | // allow title/form_title |
| 732 | if(in_array(strtolower($args), array('title', 'form_title'), true)){ |
| 733 | $args = 'title'; |
| 734 | } |
| 735 | |
| 736 | // return |
| 737 | return $this->array_get($form, $args); |
| 738 | |
| 739 | } |
| 740 | )); |
| 741 | |
| 742 | // {current:form:post_id} |
| 743 | // deprecated |
| 744 | $this->add_tag(array( |
| 745 | 'name' => 'current', |
| 746 | 'condition' => function($args){ |
| 747 | return !acf_is_empty($args); |
| 748 | }, |
| 749 | 'resolver' => function($args){ |
| 750 | |
| 751 | // tag: {form:post_id} |
| 752 | $keys = explode(':', $args); |
| 753 | $name = array_shift($keys); // form |
| 754 | $args_left = implode(':', $keys); // post_id |
| 755 | |
| 756 | // deprecated warning |
| 757 | $deprecated_version = $name === 'form' ? '0.8.7.5' : '0.8.8'; |
| 758 | _deprecated_function('ACF Extended: "{current:' . $args . '}" template tag', $deprecated_version, "the new {" . $args . "} Template Tag"); |
| 759 | |
| 760 | // get fallback tag (form, post, term etc...) |
| 761 | $tag = $this->get_tag($name); |
| 762 | |
| 763 | // return tag resolver |
| 764 | if($tag && is_callable($tag['resolver'])){ |
| 765 | return call_user_func_array($tag['resolver'], array($args_left)); |
| 766 | } |
| 767 | |
| 768 | return false; |
| 769 | |
| 770 | } |
| 771 | )); |
| 772 | |
| 773 | // {generate_password} |
| 774 | $this->add_tag(array( |
| 775 | 'name' => 'generate_password', |
| 776 | 'resolver' => function($args){ |
| 777 | return wp_generate_password(8, false); |
| 778 | } |
| 779 | )); |
| 780 | |
| 781 | // {generated_id} |
| 782 | $this->add_tag(array( |
| 783 | 'name' => 'generated_id', |
| 784 | 'condition' => function($args){ |
| 785 | return (bool) $this->get_context('generated_id'); |
| 786 | }, |
| 787 | 'resolver' => function($args){ |
| 788 | return $this->get_context('generated_id'); |
| 789 | } |
| 790 | )); |
| 791 | |
| 792 | // {date} |
| 793 | // {date:+2 days +1 year} |
| 794 | // {date:+2 days +1 year:d/m/Y} |
| 795 | $this->add_tag(array( |
| 796 | 'name' => 'date', |
| 797 | 'resolver' => function($args){ |
| 798 | |
| 799 | // escape |
| 800 | $args = str_replace('\:', '[sep]', $args); |
| 801 | |
| 802 | // explode |
| 803 | $keys = explode(':', $args); |
| 804 | $date = array_shift($keys); |
| 805 | $format = array_shift($keys); |
| 806 | |
| 807 | // unespace |
| 808 | $date = str_replace('[sep]', ':', $date); |
| 809 | $format = str_replace('[sep]', ':', $format); |
| 810 | |
| 811 | $date = $date ? $date : 'now'; |
| 812 | $format = $format ? $format : 'Y-m-d'; |
| 813 | |
| 814 | return date($format, strtotime($date)); |
| 815 | } |
| 816 | )); |
| 817 | |
| 818 | } |
| 819 | |
| 820 | |
| 821 | /** |
| 822 | * parse |
| 823 | * |
| 824 | * @param $string |
| 825 | * @param $tmp_context |
| 826 | * |
| 827 | * @return mixed|string |
| 828 | */ |
| 829 | function parse($string, $tmp_context){ |
| 830 | |
| 831 | // array |
| 832 | if(is_array($string)){ |
| 833 | foreach(array_keys($string) as $key){ |
| 834 | $string[ $key ] = $this->parse($string[ $key ], $tmp_context); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | // validate |
| 839 | if(!is_string($string) || empty($string)){ |
| 840 | return $string; |
| 841 | } |
| 842 | |
| 843 | // add temp context |
| 844 | if(is_array($tmp_context)){ |
| 845 | $this->add_context($tmp_context); |
| 846 | } |
| 847 | |
| 848 | // match |
| 849 | while($this->match($string)){} |
| 850 | |
| 851 | // remove temp context |
| 852 | if(is_array($tmp_context)){ |
| 853 | $this->delete_context($tmp_context); |
| 854 | } |
| 855 | |
| 856 | // restore excluded tags |
| 857 | $string = str_replace('ACFE_ESCAPE_START', '{', $string); |
| 858 | $string = str_replace('ACFE_ESCAPE_END', '}', $string); |
| 859 | |
| 860 | // return |
| 861 | return $string; |
| 862 | |
| 863 | } |
| 864 | |
| 865 | |
| 866 | /** |
| 867 | * match |
| 868 | * |
| 869 | * @param $string |
| 870 | * |
| 871 | * @return bool |
| 872 | */ |
| 873 | function match(&$string){ |
| 874 | |
| 875 | // direct tags |
| 876 | // match field_65671b1d11f07 current_post etc... |
| 877 | $direct_tags = $this->get_tags(array('direct' => true)); |
| 878 | |
| 879 | // loop |
| 880 | foreach($direct_tags as $tag){ |
| 881 | |
| 882 | // default condition |
| 883 | $condition = $string === $tag['name']; |
| 884 | |
| 885 | // tag condition |
| 886 | if(is_callable($tag['condition'])){ |
| 887 | $condition = call_user_func_array($tag['condition'], array($string)); |
| 888 | } |
| 889 | |
| 890 | // check condition |
| 891 | if($condition && is_callable($tag['resolver'])){ |
| 892 | |
| 893 | // replace string |
| 894 | $string = call_user_func_array($tag['resolver'], array($string)); |
| 895 | |
| 896 | if($this->get_context('return') !== 'raw'){ |
| 897 | |
| 898 | // convert array into value |
| 899 | if(is_array($string)){ |
| 900 | $string = acfe_array_first($string, function($val){ |
| 901 | return is_string($val) || is_numeric($val) || is_bool($val); |
| 902 | }, false); |
| 903 | } |
| 904 | |
| 905 | } |
| 906 | |
| 907 | // stop loop |
| 908 | return false; |
| 909 | |
| 910 | } |
| 911 | |
| 912 | } |
| 913 | |
| 914 | // tags |
| 915 | // match {name} {name:value} {name:value:value2} etc... |
| 916 | preg_match_all('/[{\w: +-\\\]*(?<full>{(?<name>[\w]+)(?!:})(?::(?<value>.*?))?})}*/', $string, $matches); |
| 917 | |
| 918 | // keys |
| 919 | $keys = current($matches); |
| 920 | |
| 921 | // stop loop |
| 922 | if(empty($keys)){ |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | // loop matches |
| 927 | foreach(array_keys($keys) as $i){ |
| 928 | |
| 929 | // vars |
| 930 | $replace = ''; |
| 931 | $search = $matches['full'][ $i ]; // {field:my_field} |
| 932 | $name = trim($matches['name'][ $i ]); // field |
| 933 | $value = trim($matches['value'][ $i ]); // my_field |
| 934 | |
| 935 | // get tags |
| 936 | $tags = $this->get_tags(array('direct' => false, 'name' => $name)); |
| 937 | |
| 938 | if($tags){ |
| 939 | |
| 940 | // var |
| 941 | $resolver_executed = false; |
| 942 | |
| 943 | // loop tags |
| 944 | foreach($tags as $tag){ |
| 945 | |
| 946 | // default condition |
| 947 | $condition = true; |
| 948 | |
| 949 | // check condition |
| 950 | if(is_callable($tag['condition'])){ |
| 951 | $condition = call_user_func_array($tag['condition'], array($value)); |
| 952 | } |
| 953 | |
| 954 | // condition ok: call resolver |
| 955 | if($condition && is_callable($tag['resolver'])){ |
| 956 | |
| 957 | // execute resolver |
| 958 | $replace = call_user_func_array($tag['resolver'], array($value)); |
| 959 | |
| 960 | if($this->get_context('return') !== 'raw'){ |
| 961 | |
| 962 | // convert array into value |
| 963 | if(is_array($replace)){ |
| 964 | $replace = acfe_array_first($replace, function($val){ |
| 965 | return is_string($val) || is_numeric($val) || is_bool($val); |
| 966 | }, false); |
| 967 | } |
| 968 | |
| 969 | // escape brackets |
| 970 | if(!empty($replace) && is_string($replace)){ |
| 971 | $replace = str_replace('{', 'ACFE_ESCAPE_START', $replace); |
| 972 | $replace = str_replace('}', 'ACFE_ESCAPE_END', $replace); |
| 973 | } |
| 974 | |
| 975 | } |
| 976 | |
| 977 | // marker |
| 978 | $resolver_executed = true; |
| 979 | |
| 980 | } |
| 981 | |
| 982 | // resolver executed: stop tags loop |
| 983 | if($resolver_executed){ |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | } |
| 988 | |
| 989 | // condition/resolver not executed: add as excluded tag |
| 990 | // this will prevent current tag from being parsed again by next loop call |
| 991 | if(!$resolver_executed){ |
| 992 | |
| 993 | $replace = $search; |
| 994 | $replace = str_replace('{', 'ACFE_ESCAPE_START', $replace); |
| 995 | $replace = str_replace('}', 'ACFE_ESCAPE_END', $replace); |
| 996 | |
| 997 | } |
| 998 | |
| 999 | } |
| 1000 | |
| 1001 | // raw context + array |
| 1002 | if($this->get_context('return') === 'raw' && is_array($replace)){ |
| 1003 | |
| 1004 | // return array |
| 1005 | $string = $replace; |
| 1006 | |
| 1007 | // stop loop |
| 1008 | return false; |
| 1009 | |
| 1010 | } |
| 1011 | |
| 1012 | if($replace === null || $replace === false){ |
| 1013 | $replace = ''; |
| 1014 | } |
| 1015 | |
| 1016 | // replace |
| 1017 | $string = str_replace($search, $replace, $string); |
| 1018 | |
| 1019 | } |
| 1020 | |
| 1021 | // keep looping |
| 1022 | return true; |
| 1023 | |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | /** |
| 1028 | * array_get |
| 1029 | * |
| 1030 | * @param $array |
| 1031 | * @param $key |
| 1032 | * @param $default |
| 1033 | * |
| 1034 | * @return mixed|null |
| 1035 | */ |
| 1036 | function array_get($array, $key, $default = null){ |
| 1037 | |
| 1038 | $key = acfe_as_string($key, ':'); |
| 1039 | $key = str_replace(':', '.', $key); |
| 1040 | |
| 1041 | return acfe_get($array, $key, $default); |
| 1042 | |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | /** |
| 1047 | * get_user_array |
| 1048 | * |
| 1049 | * @param $user_id |
| 1050 | * |
| 1051 | * @return array|false |
| 1052 | */ |
| 1053 | function get_user_array($user_id){ |
| 1054 | |
| 1055 | // bail early if user id is 0 |
| 1056 | if(!$user_id){ |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | // user object |
| 1061 | $user = get_user_by('ID', $user_id); |
| 1062 | |
| 1063 | // validate |
| 1064 | if(!$user){ |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | // cast as array |
| 1069 | $user = (array) $user->data; |
| 1070 | |
| 1071 | // user meta |
| 1072 | $user_meta = get_user_meta($user_id); |
| 1073 | foreach($user_meta as $k => $v){ |
| 1074 | if(isset($v[0])){ |
| 1075 | $user[ $k ] = $v[0]; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | // additional fields |
| 1080 | $user['permalink'] = get_author_posts_url($user_id); |
| 1081 | $user['admin_url'] = admin_url("user-edit.php?user_id=$user_id"); |
| 1082 | |
| 1083 | // return |
| 1084 | return $user; |
| 1085 | |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | /** |
| 1090 | * get_field_key_from_field_groups |
| 1091 | * |
| 1092 | * @param $name |
| 1093 | * |
| 1094 | * @return mixed |
| 1095 | */ |
| 1096 | function get_field_key_from_field_groups($name){ |
| 1097 | |
| 1098 | $mapped = $this->get_context('mapped_fields'); |
| 1099 | |
| 1100 | if(!empty($mapped) && isset($mapped[ $name ])){ |
| 1101 | return $mapped[ $name ]; |
| 1102 | } |
| 1103 | |
| 1104 | return $name; |
| 1105 | |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | /** |
| 1110 | * add_tag |
| 1111 | * |
| 1112 | * @param $args |
| 1113 | */ |
| 1114 | function add_tag($args){ |
| 1115 | $this->tags[] = $args; |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | /** |
| 1120 | * get_tag |
| 1121 | * |
| 1122 | * @param $name |
| 1123 | * |
| 1124 | * @return array|false|mixed |
| 1125 | */ |
| 1126 | function get_tag($name){ |
| 1127 | return current($this->get_tags(array('name' => $name))); |
| 1128 | } |
| 1129 | |
| 1130 | |
| 1131 | /** |
| 1132 | * get_tags |
| 1133 | * |
| 1134 | * @return array |
| 1135 | */ |
| 1136 | function get_tags($args = array(), $operator = 'AND'){ |
| 1137 | |
| 1138 | // vars |
| 1139 | $tags = array(); |
| 1140 | |
| 1141 | // loop raw tags |
| 1142 | foreach($this->tags as $tag){ |
| 1143 | |
| 1144 | // validate tag |
| 1145 | $tag = $this->validate_tag($tag); |
| 1146 | |
| 1147 | // append |
| 1148 | if($tag){ |
| 1149 | $tags[] = $tag; |
| 1150 | } |
| 1151 | |
| 1152 | } |
| 1153 | |
| 1154 | // found tags |
| 1155 | if($tags){ |
| 1156 | |
| 1157 | // sort by priority |
| 1158 | $priority = array_column($tags, 'priority'); |
| 1159 | array_multisort($priority, SORT_ASC, $tags); |
| 1160 | |
| 1161 | // filter by args |
| 1162 | if($args){ |
| 1163 | |
| 1164 | $tags = wp_list_filter($tags, $args, $operator); |
| 1165 | $tags = array_values($tags); |
| 1166 | |
| 1167 | } |
| 1168 | |
| 1169 | return $tags; |
| 1170 | |
| 1171 | } |
| 1172 | |
| 1173 | // return |
| 1174 | return $tags; |
| 1175 | |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | /** |
| 1180 | * validate_tag |
| 1181 | * |
| 1182 | * @param $args |
| 1183 | * |
| 1184 | * @return array |
| 1185 | */ |
| 1186 | function validate_tag($args){ |
| 1187 | |
| 1188 | // default args |
| 1189 | $args = wp_parse_args($args, array( |
| 1190 | 'name' => '', |
| 1191 | 'priority' => 10, |
| 1192 | 'direct' => false, |
| 1193 | 'condition' => false, |
| 1194 | 'resolver' => false, |
| 1195 | )); |
| 1196 | |
| 1197 | // validate args |
| 1198 | $args['direct'] = (bool) $args['direct']; |
| 1199 | $args['priority'] = (int) $args['priority']; |
| 1200 | |
| 1201 | // return |
| 1202 | return $args; |
| 1203 | |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | /** |
| 1208 | * get_context |
| 1209 | * |
| 1210 | * @param $name |
| 1211 | * |
| 1212 | * @return array|mixed|null |
| 1213 | */ |
| 1214 | function has_context($name){ |
| 1215 | |
| 1216 | return acfe_get($this->context, $name) !== null; |
| 1217 | |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | /** |
| 1222 | * get_context |
| 1223 | * |
| 1224 | * @param $name |
| 1225 | * |
| 1226 | * @return array|mixed|null |
| 1227 | */ |
| 1228 | function get_context($name = false, $default = null){ |
| 1229 | |
| 1230 | if($name){ |
| 1231 | return acfe_get($this->context, $name, $default); |
| 1232 | } |
| 1233 | |
| 1234 | return $this->context; |
| 1235 | |
| 1236 | } |
| 1237 | |
| 1238 | |
| 1239 | /** |
| 1240 | * add_context |
| 1241 | * |
| 1242 | * @param $name |
| 1243 | * @param $value |
| 1244 | */ |
| 1245 | function add_context($name, $value = null){ |
| 1246 | |
| 1247 | // array |
| 1248 | if(is_array($name) && $value === null){ |
| 1249 | |
| 1250 | foreach($name as $key => $val){ |
| 1251 | $this->context[ $key ] = $val; |
| 1252 | } |
| 1253 | |
| 1254 | // normal |
| 1255 | }else{ |
| 1256 | $this->context[ $name ] = $value; |
| 1257 | } |
| 1258 | |
| 1259 | } |
| 1260 | |
| 1261 | |
| 1262 | /** |
| 1263 | * delete_context |
| 1264 | * |
| 1265 | * @param $name |
| 1266 | */ |
| 1267 | function delete_context($name){ |
| 1268 | |
| 1269 | // array |
| 1270 | if(is_array($name)){ |
| 1271 | |
| 1272 | if(acf_is_associative_array($name)){ |
| 1273 | foreach(array_keys($name) as $key){ |
| 1274 | unset($this->context[ $key ]); |
| 1275 | } |
| 1276 | }else{ |
| 1277 | foreach($name as $key){ |
| 1278 | unset($this->context[ $key ]); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | // normal |
| 1283 | }else{ |
| 1284 | unset($this->context[ $name ]); |
| 1285 | } |
| 1286 | |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | /** |
| 1291 | * clear_context |
| 1292 | */ |
| 1293 | function clear_context(){ |
| 1294 | $this->context = array(); |
| 1295 | } |
| 1296 | |
| 1297 | } |
| 1298 | |
| 1299 | acf_new_instance('acfe_template_tags'); |
| 1300 | |
| 1301 | endif; |
| 1302 | |
| 1303 | |
| 1304 | /** |
| 1305 | * acfe_add_tag |
| 1306 | * |
| 1307 | * @param $args |
| 1308 | */ |
| 1309 | function acfe_add_tag($args){ |
| 1310 | acf_get_instance('acfe_template_tags')->add_tag($args); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | /** |
| 1315 | * acfe_get_tag |
| 1316 | * |
| 1317 | * @param $name |
| 1318 | * |
| 1319 | * @return mixed |
| 1320 | */ |
| 1321 | function acfe_get_tag($name){ |
| 1322 | return acf_get_instance('acfe_template_tags')->get_tag($name); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | /** |
| 1327 | * acfe_get_tags |
| 1328 | * |
| 1329 | * @return mixed |
| 1330 | */ |
| 1331 | function acfe_get_tags($args = array(), $operator = 'AND'){ |
| 1332 | return acf_get_instance('acfe_template_tags')->get_tags($args, $operator); |
| 1333 | } |
| 1334 | |
| 1335 | |
| 1336 | /** |
| 1337 | * acfe_get_context |
| 1338 | * |
| 1339 | * @param $name |
| 1340 | * |
| 1341 | * @return mixed |
| 1342 | */ |
| 1343 | function acfe_get_context($name = false, $default = null){ |
| 1344 | return acf_get_instance('acfe_template_tags')->get_context($name, $default); |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | /** |
| 1349 | * acfe_add_context |
| 1350 | * |
| 1351 | * @param $name |
| 1352 | * @param $value |
| 1353 | */ |
| 1354 | function acfe_add_context($name, $value = null){ |
| 1355 | acf_get_instance('acfe_template_tags')->add_context($name, $value); |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | /** |
| 1360 | * acfe_delete_context |
| 1361 | * |
| 1362 | * @param $name |
| 1363 | */ |
| 1364 | function acfe_delete_context($name){ |
| 1365 | acf_get_instance('acfe_template_tags')->delete_context($name); |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | /** |
| 1370 | * acfe_clear_context |
| 1371 | */ |
| 1372 | function acfe_clear_context(){ |
| 1373 | acf_get_instance('acfe_template_tags')->clear_context(); |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | /** |
| 1378 | * acfe_parse_tags |
| 1379 | * |
| 1380 | * @param $text |
| 1381 | * @param $tmp_context |
| 1382 | * |
| 1383 | * @return mixed |
| 1384 | */ |
| 1385 | function acfe_parse_tags($text, $tmp_context = null){ |
| 1386 | return acf_get_instance('acfe_template_tags')->parse($text, $tmp_context); |
| 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | /** |
| 1391 | * acfe_apply_tags |
| 1392 | * |
| 1393 | * @param $text |
| 1394 | * @param $tmp_context |
| 1395 | * |
| 1396 | * @return mixed |
| 1397 | */ |
| 1398 | function acfe_apply_tags(&$text, $tmp_context = null){ |
| 1399 | $text = acfe_parse_tags($text, $tmp_context); |
| 1400 | return $text; |
| 1401 | } |