| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FirePlugins Framework |
| 4 | - * @version 1.0.7 | |
| 4 | + * @version 1.2.6 | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FPFramework\Base; |
| @@ -23,8 +23,20 @@ | ||
| 23 | 23 | |
| 24 | 24 | abstract class Field |
| 25 | 25 | { |
| 26 | 26 | /** |
| 27 | + * WordPress page for creating new posts | |
| 28 | + * @var string | |
| 29 | + */ | |
| 30 | + const PAGE_NEW_POST = 'post-new.php'; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * WordPress page for editing posts | |
| 34 | + * @var string | |
| 35 | + */ | |
| 36 | + const PAGE_EDIT_POST = 'post.php'; | |
| 37 | + | |
| 38 | + /** | |
| 27 | 39 | * All field options. |
| 28 | 40 | * |
| 29 | 41 | * @var array |
| 30 | 42 | */ |
| @@ -60,9 +72,9 @@ | ||
| 60 | 72 | $this->prePrepareData(); |
| 61 | 73 | |
| 62 | 74 | // set value |
| 63 | 75 | $this->options['value'] = $this->getValue(); |
| 64 | - | |
| 76 | + | |
| 65 | 77 | // prepare field after value has been set |
| 66 | 78 | $this->postPrepareData(); |
| 67 | 79 | } |
| 68 | 80 | |
| @@ -73,8 +85,10 @@ | ||
| 73 | 85 | */ |
| 74 | 86 | protected function prePrepareData() |
| 75 | 87 | { |
| 76 | 88 | $options = new Registry($this->options); |
| 89 | + | |
| 90 | + $this->options['value_raw'] = $this->options['value']; | |
| 77 | 91 | |
| 78 | 92 | $name = $options->get('name', ''); |
| 79 | 93 | |
| 80 | 94 | // prepare name |
| @@ -83,9 +97,10 @@ | ||
| 83 | 97 | $name = $this->prepareName($options); |
| 84 | 98 | } |
| 85 | 99 | |
| 86 | 100 | // add [value] to the name of the field if it has units |
| 87 | - if (count($options->get('units', [])) && !in_array($options->get('type'), ['Units', 'ResponsiveControl', 'Dimensions'])) | |
| 101 | + // (cast: count() throws a TypeError on non-countables since PHP 8.0) | |
| 102 | + if (count((array) $options->get('units', [])) && !in_array($options->get('type'), ['Units', 'ResponsiveControl', 'Dimensions'])) | |
| 88 | 103 | { |
| 89 | 104 | $name .= '[value]'; |
| 90 | 105 | } |
| 91 | 106 | $this->options['name'] = $name; |
| @@ -110,28 +125,14 @@ | ||
| 110 | 125 | */ |
| 111 | 126 | protected function postPrepareData() |
| 112 | 127 | { |
| 113 | 128 | // add the "disable" class to the input if the selected unit was "auto" or if no unit value is set and the default unit is set to "auto" |
| 114 | - if (isset($this->options['value']->unit) && $this->options['value']->unit == 'auto' || | |
| 115 | - (!isset($this->options['value']->unit) && isset($this->options['units_default']) && $this->options['units_default'] == 'auto')) | |
| 129 | + if (isset($this->options['unit']) && $this->options['unit'] == 'auto' || | |
| 130 | + (!isset($this->options['unit']) && isset($this->options['units_default']) && $this->options['units_default'] == 'auto')) | |
| 116 | 131 | { |
| 117 | 132 | $this->options['extra_atts']['readonly'] = 'readonly'; |
| 118 | 133 | } |
| 119 | 134 | |
| 120 | - // Identify value based on whether the value is in a responsive control or directly set | |
| 121 | - $value = $this->options['value']; | |
| 122 | - $default = $this->options['default']; | |
| 123 | - // Is responsive control | |
| 124 | - if (is_object($value)) | |
| 125 | - { | |
| 126 | - $this->options['value'] = isset($value->value) ? (is_null($value->value) ? $default : $value->value) : null; | |
| 127 | - } | |
| 128 | - // Is directly set | |
| 129 | - else | |
| 130 | - { | |
| 131 | - $this->options['value'] = is_null($value) ? $default : $value; | |
| 132 | - } | |
| 133 | - | |
| 134 | 135 | // generate extra attributes |
| 135 | 136 | $this->options['extra_atts'] = \FPFramework\Helpers\FieldsHelper::getHTMLAttributes($this->options['extra_atts']); |
| 136 | 137 | } |
| 137 | 138 | |
| @@ -245,37 +246,62 @@ | ||
| 245 | 246 | ]; |
| 246 | 247 | } |
| 247 | 248 | |
| 248 | 249 | /** |
| 249 | - * Gets the field value | |
| 250 | + * Extracts the value from a unit-enabled field value | |
| 250 | 251 | * |
| 251 | - * @return mixed | |
| 252 | + * @param mixed $value The value to process | |
| 253 | + * | |
| 254 | + * @return mixed The processed value | |
| 252 | 255 | */ |
| 256 | + protected function extractUnitValue($value) | |
| 257 | + { | |
| 258 | + if ($this->hasUnits() && !$this instanceof \FPFramework\Base\Fields\Units && isset($value->value)) | |
| 259 | + { | |
| 260 | + return $value->value; | |
| 261 | + } | |
| 262 | + return $value; | |
| 263 | + } | |
| 264 | + | |
| 265 | + /** | |
| 266 | + * Gets the field value. The value resolution sequence is: | |
| 267 | + * 1. Directly set value in options | |
| 268 | + * 2. Value based on current page context (new post, edit post) | |
| 269 | + * 3. Value from WordPress options | |
| 270 | + * | |
| 271 | + * For fields with units, the value is extracted from the unit structure. | |
| 272 | + * | |
| 273 | + * @return mixed The field value | |
| 274 | + */ | |
| 253 | 275 | public function getValue() |
| 254 | 276 | { |
| 255 | 277 | global $pagenow; |
| 256 | 278 | |
| 257 | 279 | $options = $this->getOptions(); |
| 280 | + $value = null; | |
| 258 | 281 | |
| 259 | - // if we were given a value directly, then use it | |
| 260 | - if (!empty($options['value']) || $options['value'] == '0') | |
| 282 | + // First try to get value directly from options | |
| 283 | + if (!empty($options['value']) || $options['value'] === '0') | |
| 261 | 284 | { |
| 262 | - return $options['value']; | |
| 285 | + return $this->extractUnitValue($options['value']); | |
| 263 | 286 | } |
| 264 | 287 | |
| 265 | - // In a new Custom Post Type(CPT) Item | |
| 266 | - if ($pagenow == 'post-new.php') | |
| 288 | + // Then try to get value based on current page context | |
| 289 | + switch ($pagenow) | |
| 267 | 290 | { |
| 268 | - return $options['default']; | |
| 269 | - } | |
| 291 | + case self::PAGE_NEW_POST: | |
| 292 | + $value = $this->extractUnitValue($options['default']); | |
| 293 | + break; | |
| 270 | 294 | |
| 271 | - // Editing a CPT Item, we need to fetch the data from the post meta | |
| 272 | - if ($pagenow == 'post.php') | |
| 273 | - { | |
| 274 | - return $this->getValueFromPostMeta(); | |
| 295 | + case self::PAGE_EDIT_POST: | |
| 296 | + $value = $this->extractUnitValue($this->getValueFromPostMeta()); | |
| 297 | + break; | |
| 298 | + | |
| 299 | + default: | |
| 300 | + $value = $this->extractUnitValue($this->getValueFromOptions()); | |
| 275 | 301 | } |
| 276 | 302 | |
| 277 | - return $this->getValueFromOptions(); | |
| 303 | + return $value; | |
| 278 | 304 | } |
| 279 | 305 | |
| 280 | 306 | /** |
| 281 | 307 | * Get value from post meta |
| @@ -287,14 +313,8 @@ | ||
| 287 | 313 | private function getValueFromPostMeta($meta = []) |
| 288 | 314 | { |
| 289 | 315 | $options = $this->getOptions(); |
| 290 | 316 | |
| 291 | - if (!$meta) | |
| 292 | - { | |
| 293 | - global $post; | |
| 294 | - $meta = get_post_meta($post->ID, 'fpframework_meta_settings', true); | |
| 295 | - } | |
| 296 | - | |
| 297 | 317 | $default = isset($options['default']) ? $options['default'] : null; |
| 298 | 318 | |
| 299 | 319 | $name = $options['name']; |
| 300 | 320 | |
| @@ -302,8 +322,14 @@ | ||
| 302 | 322 | { |
| 303 | 323 | return $default; |
| 304 | 324 | } |
| 305 | 325 | |
| 326 | + if (!$meta) | |
| 327 | + { | |
| 328 | + global $post; | |
| 329 | + $meta = $post ? get_post_meta($post->ID, 'fpframework_meta_settings', true) : []; | |
| 330 | + } | |
| 331 | + | |
| 306 | 332 | $val = FieldsHelper::findFieldValueInArray($name, $default, $meta); |
| 307 | 333 | |
| 308 | 334 | return $val; |
| 309 | 335 | } |
| @@ -368,9 +394,8 @@ | ||
| 368 | 394 | |
| 369 | 395 | $name = str_replace('.', '][', $name); |
| 370 | 396 | $suffix = $name[strlen($name) - 1] == ']' ? '' : ']'; |
| 371 | 397 | |
| 372 | - | |
| 373 | 398 | // Final Name |
| 374 | 399 | return $prefix . $name . $suffix; |
| 375 | 400 | } |
| 376 | 401 | |
| @@ -399,12 +424,22 @@ | ||
| 399 | 424 | $this->options[$key] = $value; |
| 400 | 425 | } |
| 401 | 426 | |
| 402 | 427 | /** |
| 428 | + * Checks whether the field contains units. | |
| 429 | + * | |
| 430 | + * @return bool | |
| 431 | + */ | |
| 432 | + protected function hasUnits() | |
| 433 | + { | |
| 434 | + return is_array($this->options['units']) && count($this->options['units']); | |
| 435 | + } | |
| 436 | + | |
| 437 | + /** | |
| 403 | 438 | * Renders field top |
| 404 | 439 | * Contains the Responsive Controls and the Units |
| 405 | 440 | * |
| 406 | - * @return void | |
| 441 | + * @return string | |
| 407 | 442 | */ |
| 408 | 443 | public function getFieldTop() |
| 409 | 444 | { |
| 410 | 445 | $options = $this->getOptions(); |
| @@ -412,9 +447,27 @@ | ||
| 412 | 447 | // units |
| 413 | 448 | if (count($options['units'])) |
| 414 | 449 | { |
| 415 | 450 | $default_units_value = isset($options['units_default']) ? $options['units_default'] : null; |
| 416 | - $unit_value = isset($options['value_raw']->unit) ? $options['value_raw']->unit : $default_units_value; | |
| 451 | + | |
| 452 | + // First check if unit is directly set in options | |
| 453 | + $unit_value = isset($options['unit']) && !empty($options['unit']) ? $options['unit'] : null; | |
| 454 | + | |
| 455 | + // Check if unit exists in value_raw | |
| 456 | + if (!$unit_value && isset($options['value_raw']) && is_object($options['value_raw']) && isset($options['value_raw']->unit)) | |
| 457 | + { | |
| 458 | + $unit_value = $options['value_raw']->unit; | |
| 459 | + } | |
| 460 | + | |
| 461 | + // If no unit found, check in default.unit if it exists | |
| 462 | + if (!$unit_value && isset($options['default']) && is_object($options['default']) && isset($options['default']->unit)) | |
| 463 | + { | |
| 464 | + $unit_value = $options['default']->unit; | |
| 465 | + } | |
| 466 | + | |
| 467 | + // If still no unit found, use the default units value | |
| 468 | + $unit_value = $unit_value ?: $default_units_value; | |
| 469 | + | |
| 417 | 470 | $units_field_data = [ |
| 418 | 471 | 'name' => rtrim($options['name_key'], ']') . '][unit]', |
| 419 | 472 | 'units' => (array) $options['units'], |
| 420 | 473 | 'value' => $unit_value, |
| @@ -436,9 +489,9 @@ | ||
| 436 | 489 | |
| 437 | 490 | /** |
| 438 | 491 | * Renders field body |
| 439 | 492 | * |
| 440 | - * @return void | |
| 493 | + * @return string | |
| 441 | 494 | */ |
| 442 | 495 | public function getFieldBody() |
| 443 | 496 | { |
| 444 | 497 | // determine who will render the field (plugin or framework) |
| @@ -446,9 +499,9 @@ | ||
| 446 | 499 | |
| 447 | 500 | // render a specific field |
| 448 | 501 | $plugin = isset($field_data['plugin']) ? $field_data['plugin'] : $field_data; |
| 449 | 502 | $type = isset($field_data['type']) ? $field_data['type'] : $this->options['type']; |
| 450 | - | |
| 503 | + | |
| 451 | 504 | return $plugin()->renderer->fields->render(strtolower($type), $this->getOptions(), true); |
| 452 | 505 | } |
| 453 | 506 | |
| 454 | 507 | /** |
| @@ -482,9 +535,9 @@ | ||
| 482 | 535 | * @return array |
| 483 | 536 | */ |
| 484 | 537 | public function getOption($option = null) |
| 485 | 538 | { |
| 486 | - if (!$option && !is_string($option) && !isset($this->option[$option])) | |
| 539 | + if (!$option || !is_string($option) || !isset($this->options[$option])) | |
| 487 | 540 | { |
| 488 | 541 | return null; |
| 489 | 542 | } |
| 490 | 543 | |
| @@ -503,8 +556,10 @@ | ||
| 503 | 556 | $options = new Registry($options); |
| 504 | 557 | |
| 505 | 558 | $initial_name = $options->get('name', ''); |
| 506 | 559 | |
| 560 | + $name_prefix = $options->get('name_prefix', \FPFramework\Helpers\FieldsHelper::$fields_prefix); | |
| 561 | + | |
| 507 | 562 | return [ |
| 508 | 563 | 'name' => $initial_name, |
| 509 | 564 | 'name_key' => $options->get('name_key', $initial_name), |
| 510 | 565 | 'id' => $options->get('id', ''), |
| @@ -509,15 +564,15 @@ | ||
| 509 | 564 | 'name_key' => $options->get('name_key', $initial_name), |
| 510 | 565 | 'id' => $options->get('id', ''), |
| 511 | 566 | 'type' => $options->get('type', ''), |
| 512 | 567 | 'name_clean' => $options->get('name_clean', false), |
| 513 | - 'name_prefix' => $options->get('name_prefix', \FPFramework\Admin\Includes\MetaboxManager::$fields_prefix), | |
| 568 | + 'name_prefix' => $name_prefix, | |
| 514 | 569 | 'field_path' => $options->get('field_path', ''), |
| 515 | 570 | 'label' => fpframework()->_($options->get('label', '')), |
| 516 | 571 | 'description' => fpframework()->_($options->get('description', '')), |
| 517 | 572 | 'description_class' => $options->get('description_class', ['top']), |
| 573 | + 'value_raw' => $options->get('value_raw', null), | |
| 518 | 574 | 'value' => $options->get('value', null), |
| 519 | - 'value_raw' => $options->get('value', null), | |
| 520 | 575 | 'placeholder' => fpframework()->_($options->get('placeholder', '')), |
| 521 | 576 | 'default' => $options->get('default', null), |
| 522 | 577 | 'required' => $options->get('required', false), |
| 523 | 578 | 'required_attribute' => null, |
| @@ -524,13 +579,14 @@ | ||
| 524 | 579 | 'class' => $options->get('class', null), |
| 525 | 580 | 'input_class' => $options->get('input_class', ''), |
| 526 | 581 | 'control_inner_class' => $options->get('control_inner_class', []), |
| 527 | 582 | 'input_parent_class' => $options->get('input_parent_class', []), |
| 528 | - 'showon' => \FPFramework\Helpers\FormHelper::parseShowOnConditions($options->get('showon', null)), | |
| 583 | + 'showon' => \FPFramework\Helpers\FormHelper::parseShowOnConditions($options->get('showon', null), $name_prefix), | |
| 529 | 584 | 'filter' => $options->get('filter', 'sanitize_text_field'), |
| 530 | 585 | 'extra_atts' => $options->get('extra_atts', []), |
| 531 | 586 | 'render_group' => $options->get('render_group', true), |
| 532 | 587 | 'tooltip' => fpframework()->_($options->get('tooltip', '')), |
| 588 | + 'unit' => $options->get('unit', null), | |
| 533 | 589 | 'units' => $options->get('units', []), |
| 534 | 590 | 'units_relative_position' => $options->get('units_relative_position', true), |
| 535 | 591 | 'units_default' => $options->get('units_default', null), |
| 536 | 592 | 'render_top' => $options->get('render_top', true), |