| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.0.1 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
use FPFramework\Helpers\StringHelper; |
| 21 |
use FPFramework\Helpers\ArrayHelper; |
| 22 |
use FPFramework\Helpers\FieldsHelper; |
| 23 |
|
| 24 |
abstract class Field |
| 25 |
{ |
| 26 |
/** |
| 27 |
* All field options. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected $options = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* Options specific for this field |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
protected $field_options = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* The filter of the field |
| 42 |
* |
| 43 |
* @var Filter |
| 44 |
*/ |
| 45 |
protected $filter; |
| 46 |
|
| 47 |
public function __construct($options = []) |
| 48 |
{ |
| 49 |
$this->options = $options; |
| 50 |
|
| 51 |
if (method_exists($this, 'setFieldOptions')) |
| 52 |
{ |
| 53 |
$this->setFieldOptions($this->options); |
| 54 |
} |
| 55 |
|
| 56 |
// merge default and given field options |
| 57 |
$this->combineOptions(); |
| 58 |
|
| 59 |
// prepare field early |
| 60 |
$this->prePrepareData(); |
| 61 |
|
| 62 |
// set value |
| 63 |
$this->options['value'] = $this->getValue(); |
| 64 |
|
| 65 |
// prepare field after value has been set |
| 66 |
$this->postPrepareData(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Prepares the field data after field default settings and any field specifc settings have been combined |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
protected function prePrepareData() |
| 75 |
{ |
| 76 |
$options = new Registry($this->options); |
| 77 |
|
| 78 |
$name = $options->get('name', ''); |
| 79 |
|
| 80 |
// prepare name |
| 81 |
if (!empty($name) && !$options->get('name_clean', false)) |
| 82 |
{ |
| 83 |
$name = $this->prepareName($options); |
| 84 |
} |
| 85 |
|
| 86 |
// 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'])) |
| 88 |
{ |
| 89 |
$name .= '[value]'; |
| 90 |
} |
| 91 |
$this->options['name'] = $name; |
| 92 |
|
| 93 |
// set input class |
| 94 |
if (is_array($options->get('input_class')) && count($options->get('input_class'))) |
| 95 |
{ |
| 96 |
$this->options['input_class'] = ' ' . implode(' ', $options->get('input_class', [])); |
| 97 |
} |
| 98 |
|
| 99 |
// set required attribute |
| 100 |
if ($options->get('required')) |
| 101 |
{ |
| 102 |
$this->options['required_attribute'] = ' required="required"'; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Prepares the field data after value has been set |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
protected function postPrepareData() |
| 112 |
{ |
| 113 |
// 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')) |
| 116 |
{ |
| 117 |
$this->options['extra_atts']['readonly'] = 'readonly'; |
| 118 |
} |
| 119 |
|
| 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 |
// generate extra attributes |
| 135 |
$this->options['extra_atts'] = \FPFramework\Helpers\FieldsHelper::getHTMLAttributes($this->options['extra_atts']); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* General filter value |
| 140 |
* |
| 141 |
* @param string $filter |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function filterValue($filter = \FPFramework\Base\Filter::defaultFilter) |
| 146 |
{ |
| 147 |
$filter = empty($filter) ? $this->options['filter'] : $filter; |
| 148 |
|
| 149 |
// set filter |
| 150 |
$this->filter = Filter::getInstance(); |
| 151 |
|
| 152 |
$this->options['value'] = $this->filter->clean($this->options['value'], $filter); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Renders a field |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function render() |
| 161 |
{ |
| 162 |
/** |
| 163 |
* Runs before the field renders |
| 164 |
*/ |
| 165 |
if (method_exists($this, 'onBeforeRender')) |
| 166 |
{ |
| 167 |
$this->onBeforeRender(); |
| 168 |
} |
| 169 |
|
| 170 |
$options = $this->getOptions(); |
| 171 |
|
| 172 |
// check whether we manually set to skip loading of the field |
| 173 |
if ($this->options['skip_render']) |
| 174 |
{ |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// render top section only if needed by field |
| 179 |
if ($options['render_top']) |
| 180 |
{ |
| 181 |
// get field top |
| 182 |
$options['field_top'] = $this->getFieldTop(); |
| 183 |
} |
| 184 |
|
| 185 |
// get field body |
| 186 |
$options['field_body'] = $this->getFieldBody(); |
| 187 |
|
| 188 |
// check if we need to render the whole group or only the field |
| 189 |
$render_file = !$options['render_group'] ? 'field_control' : 'tmpl'; |
| 190 |
|
| 191 |
// render field |
| 192 |
fpframework()->renderer->field->render($render_file, $options); |
| 193 |
|
| 194 |
/** |
| 195 |
* Runs after the field renders |
| 196 |
*/ |
| 197 |
if (method_exists($this, 'onAfterRender')) |
| 198 |
{ |
| 199 |
$this->onAfterRender(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Returns the field data (plugin and type). |
| 205 |
* |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
private function getFieldData() |
| 209 |
{ |
| 210 |
$options = $this->getOptions(); |
| 211 |
|
| 212 |
$framework = 'fpframework'; |
| 213 |
|
| 214 |
if (!isset($options['field_path']) || empty($options['field_path'])) |
| 215 |
{ |
| 216 |
return $framework; |
| 217 |
} |
| 218 |
|
| 219 |
// check if we have a layout override and use it instead of the given field path |
| 220 |
$field_path = isset($this->layout_override) ? $this->layout_override : $options['field_path']; |
| 221 |
|
| 222 |
$plugin = ''; |
| 223 |
|
| 224 |
// retrieve the name of the plugin from the given field source |
| 225 |
$path_data = explode('\\', $field_path); |
| 226 |
$path_data = array_filter($path_data); |
| 227 |
$path_data = array_values($path_data); |
| 228 |
|
| 229 |
// get plugin (fpframework or plugin name); |
| 230 |
$plugin = ltrim($path_data[0], '\\'); |
| 231 |
$plugin = strtolower($plugin); |
| 232 |
|
| 233 |
// ensure plugin(framework or plugin) function exists |
| 234 |
if (!function_exists($plugin)) |
| 235 |
{ |
| 236 |
return $framework; |
| 237 |
} |
| 238 |
|
| 239 |
// if a layout override exists, show the field from the override, otherwise show the given field |
| 240 |
$type = isset($this->layout_override) ? $path_data[count($path_data) - 1] : $options['type']; |
| 241 |
|
| 242 |
return [ |
| 243 |
'plugin' => $plugin, |
| 244 |
'type' => $type |
| 245 |
]; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Gets the field value |
| 250 |
* |
| 251 |
* @return mixed |
| 252 |
*/ |
| 253 |
public function getValue() |
| 254 |
{ |
| 255 |
global $pagenow; |
| 256 |
|
| 257 |
$options = $this->getOptions(); |
| 258 |
|
| 259 |
// if we were given a value directly, then use it |
| 260 |
if (!empty($options['value']) || $options['value'] == '0') |
| 261 |
{ |
| 262 |
return $options['value']; |
| 263 |
} |
| 264 |
|
| 265 |
// In a new Custom Post Type(CPT) Item |
| 266 |
if ($pagenow == 'post-new.php') |
| 267 |
{ |
| 268 |
return $options['default']; |
| 269 |
} |
| 270 |
|
| 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(); |
| 275 |
} |
| 276 |
|
| 277 |
return $this->getValueFromOptions(); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Get value from post meta |
| 282 |
* |
| 283 |
* @param array $meta |
| 284 |
* |
| 285 |
* @return string |
| 286 |
*/ |
| 287 |
private function getValueFromPostMeta($meta = []) |
| 288 |
{ |
| 289 |
$options = $this->getOptions(); |
| 290 |
|
| 291 |
if (!$meta) |
| 292 |
{ |
| 293 |
global $post; |
| 294 |
$meta = get_post_meta($post->ID, 'fpframework_meta_settings', true); |
| 295 |
} |
| 296 |
|
| 297 |
$default = isset($options['default']) ? $options['default'] : null; |
| 298 |
|
| 299 |
$name = $options['name']; |
| 300 |
|
| 301 |
if (empty($name)) |
| 302 |
{ |
| 303 |
return $default; |
| 304 |
} |
| 305 |
|
| 306 |
$val = FieldsHelper::findFieldValueInArray($name, $default, $meta); |
| 307 |
|
| 308 |
return $val; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Searching for value in the get_option() function |
| 313 |
* |
| 314 |
* @return string |
| 315 |
*/ |
| 316 |
private function getValueFromOptions() |
| 317 |
{ |
| 318 |
$options = $this->getOptions(); |
| 319 |
|
| 320 |
// get the setting name |
| 321 |
$name_initial = $options['name']; |
| 322 |
$name = explode('[', $name_initial); |
| 323 |
|
| 324 |
// setting data |
| 325 |
$data = get_option($name[0]); |
| 326 |
|
| 327 |
$default = isset($options['default']) ? $options['default'] : null; |
| 328 |
|
| 329 |
// no results, return default |
| 330 |
if (!is_array($data)) |
| 331 |
{ |
| 332 |
return $default; |
| 333 |
} |
| 334 |
|
| 335 |
return FieldsHelper::findFieldValueInArray($name_initial, $default, $data); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Replaces dots with []. |
| 340 |
* |
| 341 |
* @param string $name |
| 342 |
* |
| 343 |
* @return mixed |
| 344 |
*/ |
| 345 |
private function prepareName($options) |
| 346 |
{ |
| 347 |
$name = $options->get('name', null); |
| 348 |
if (!$name) |
| 349 |
{ |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
$prefix = $suffix = ''; |
| 354 |
|
| 355 |
$name_prefix = $options->get('name_prefix', null); |
| 356 |
|
| 357 |
// If we were given a name prefix and the name does not start with it, add prefix |
| 358 |
if ($name_prefix && !StringHelper::startsWith($name, $name_prefix)) |
| 359 |
{ |
| 360 |
$prefix = $name_prefix . '['; |
| 361 |
} |
| 362 |
// if no name prefix was given and if the first character of the name |
| 363 |
// does not start with open bracket then add prefix |
| 364 |
else if (!$name_prefix && $name[0] != '[') |
| 365 |
{ |
| 366 |
$prefix = '['; |
| 367 |
} |
| 368 |
|
| 369 |
$name = str_replace('.', '][', $name); |
| 370 |
$suffix = $name[strlen($name) - 1] == ']' ? '' : ']'; |
| 371 |
|
| 372 |
|
| 373 |
// Final Name |
| 374 |
return $prefix . $name . $suffix; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Sets field's value |
| 379 |
* |
| 380 |
* @param string $value |
| 381 |
* |
| 382 |
* @return void |
| 383 |
*/ |
| 384 |
public function setValue($value) |
| 385 |
{ |
| 386 |
$this->options['value'] = $value; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Set options value |
| 391 |
* |
| 392 |
* @param string $key |
| 393 |
* @param string $value |
| 394 |
* |
| 395 |
* @return void |
| 396 |
*/ |
| 397 |
public function setOptionsValue($key, $value) |
| 398 |
{ |
| 399 |
$this->options[$key] = $value; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Renders field top |
| 404 |
* Contains the Responsive Controls and the Units |
| 405 |
* |
| 406 |
* @return void |
| 407 |
*/ |
| 408 |
public function getFieldTop() |
| 409 |
{ |
| 410 |
$options = $this->getOptions(); |
| 411 |
|
| 412 |
// units |
| 413 |
if (count($options['units'])) |
| 414 |
{ |
| 415 |
$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; |
| 417 |
$units_field_data = [ |
| 418 |
'name' => rtrim($options['name_key'], ']') . '][unit]', |
| 419 |
'units' => (array) $options['units'], |
| 420 |
'value' => $unit_value, |
| 421 |
'units_relative_position' => $options['units_relative_position'], |
| 422 |
'render_top' => false, |
| 423 |
'render_group' => false |
| 424 |
]; |
| 425 |
$units = new \FPFramework\Base\Fields\Units($units_field_data); |
| 426 |
|
| 427 |
ob_start(); |
| 428 |
$units->render(); |
| 429 |
$render = ob_get_contents(); |
| 430 |
ob_end_clean(); |
| 431 |
$options['field_top_units'] = $render; |
| 432 |
} |
| 433 |
|
| 434 |
return fpframework()->renderer->field->render('field_top', $options, true); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Renders field body |
| 439 |
* |
| 440 |
* @return void |
| 441 |
*/ |
| 442 |
public function getFieldBody() |
| 443 |
{ |
| 444 |
// determine who will render the field (plugin or framework) |
| 445 |
$field_data = $this->getFieldData(); |
| 446 |
|
| 447 |
// render a specific field |
| 448 |
$plugin = isset($field_data['plugin']) ? $field_data['plugin'] : $field_data; |
| 449 |
$type = isset($field_data['type']) ? $field_data['type'] : $this->options['type']; |
| 450 |
|
| 451 |
return $plugin()->renderer->fields->render(strtolower($type), $this->getOptions(), true); |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Combines all options |
| 456 |
* |
| 457 |
* @return array |
| 458 |
*/ |
| 459 |
protected function combineOptions() |
| 460 |
{ |
| 461 |
$this->options = $this->getDefaultOptions($this->options); |
| 462 |
|
| 463 |
// merge both the base field options as well as the specific field options |
| 464 |
$this->options = ArrayHelper::arrayMerge($this->options, $this->field_options); |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Returns all options |
| 469 |
* |
| 470 |
* @return array |
| 471 |
*/ |
| 472 |
protected function getOptions() |
| 473 |
{ |
| 474 |
return $this->options; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Return specific option value |
| 479 |
* |
| 480 |
* @param string $option |
| 481 |
* |
| 482 |
* @return array |
| 483 |
*/ |
| 484 |
public function getOption($option = null) |
| 485 |
{ |
| 486 |
if (!$option && !is_string($option) && !isset($this->option[$option])) |
| 487 |
{ |
| 488 |
return null; |
| 489 |
} |
| 490 |
|
| 491 |
return $this->options[$option]; |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Gets the default options for a Field. |
| 496 |
* |
| 497 |
* @param array $options |
| 498 |
* |
| 499 |
* @return void |
| 500 |
*/ |
| 501 |
private function getDefaultOptions($options) |
| 502 |
{ |
| 503 |
$options = new Registry($options); |
| 504 |
|
| 505 |
$initial_name = $options->get('name', ''); |
| 506 |
|
| 507 |
return [ |
| 508 |
'name' => $initial_name, |
| 509 |
'name_key' => $options->get('name_key', $initial_name), |
| 510 |
'id' => $options->get('id', ''), |
| 511 |
'type' => $options->get('type', ''), |
| 512 |
'name_clean' => $options->get('name_clean', false), |
| 513 |
'name_prefix' => $options->get('name_prefix', \FPFramework\Admin\Includes\MetaboxManager::$fields_prefix), |
| 514 |
'field_path' => $options->get('field_path', ''), |
| 515 |
'label' => fpframework()->_($options->get('label', '')), |
| 516 |
'description' => fpframework()->_($options->get('description', '')), |
| 517 |
'description_class' => $options->get('description_class', ['top']), |
| 518 |
'value' => $options->get('value', null), |
| 519 |
'value_raw' => $options->get('value', null), |
| 520 |
'placeholder' => fpframework()->_($options->get('placeholder', '')), |
| 521 |
'default' => $options->get('default', null), |
| 522 |
'required' => $options->get('required', false), |
| 523 |
'required_attribute' => null, |
| 524 |
'class' => $options->get('class', null), |
| 525 |
'input_class' => $options->get('input_class', ''), |
| 526 |
'control_inner_class' => $options->get('control_inner_class', []), |
| 527 |
'input_parent_class' => $options->get('input_parent_class', []), |
| 528 |
'showon' => \FPFramework\Helpers\FormHelper::parseShowOnConditions($options->get('showon', null)), |
| 529 |
'filter' => $options->get('filter', 'sanitize_text_field'), |
| 530 |
'extra_atts' => $options->get('extra_atts', []), |
| 531 |
'render_group' => $options->get('render_group', true), |
| 532 |
'tooltip' => fpframework()->_($options->get('tooltip', '')), |
| 533 |
'units' => $options->get('units', []), |
| 534 |
'units_relative_position' => $options->get('units_relative_position', true), |
| 535 |
'units_default' => $options->get('units_default', null), |
| 536 |
'render_top' => $options->get('render_top', true), |
| 537 |
'skip_render' => $options->get('skip_render', false) |
| 538 |
]; |
| 539 |
} |
| 540 |
} |