| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Form\Fields; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
abstract class FieldChoice extends Field |
| 20 |
{ |
| 21 |
public function __construct($options = []) |
| 22 |
{ |
| 23 |
parent::__construct($options); |
| 24 |
|
| 25 |
$this->options['choices'] = $this->getChoices(); |
| 26 |
} |
| 27 |
|
| 28 |
protected function getChoices() |
| 29 |
{ |
| 30 |
if (!$choices = $this->getOptionValue('choices')) |
| 31 |
{ |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$placeholder = $this->getOptionValue('placeholder'); |
| 36 |
|
| 37 |
foreach ($choices as &$choice) |
| 38 |
{ |
| 39 |
// Replace Smart Tags |
| 40 |
$choice = \FPFramework\Base\SmartTags\SmartTags::getInstance()->replace($choice); |
| 41 |
|
| 42 |
$label = !empty($choice['label']) ? trim($choice['label']) : firebox()->_('FB_CHOICE_LABEL'); |
| 43 |
$value = !isset($choice['value']) || $choice['value'] == '' ? wp_strip_all_tags($label) : $choice['value']; |
| 44 |
|
| 45 |
$choice = [ |
| 46 |
'label' => $label, |
| 47 |
'value' => $value, |
| 48 |
'image' => isset($choice['image']) ? $choice['image'] : '', |
| 49 |
'selected' => (isset($choice['default']) && $choice['default'] && !$placeholder) ? true : false |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
if ($placeholder) |
| 54 |
{ |
| 55 |
array_unshift($choices, array( |
| 56 |
'label' => trim($placeholder), |
| 57 |
'value' => '', |
| 58 |
'selected' => true, |
| 59 |
'disabled' => true |
| 60 |
)); |
| 61 |
} |
| 62 |
|
| 63 |
return $choices; |
| 64 |
} |
| 65 |
|
| 66 |
public function prepareValueHTML($value) |
| 67 |
{ |
| 68 |
if (is_string($value)) |
| 69 |
{ |
| 70 |
$decodedValue = json_decode($value, true); |
| 71 |
|
| 72 |
if (is_array($decodedValue)) |
| 73 |
{ |
| 74 |
$value = $decodedValue; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (is_array($value)) |
| 79 |
{ |
| 80 |
foreach ($value as &$value_) |
| 81 |
{ |
| 82 |
$value_ = $this->findChoiceLabelByValue($value_); |
| 83 |
} |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
$value = $this->findChoiceLabelByValue($value); |
| 88 |
} |
| 89 |
|
| 90 |
return parent::prepareValueHTML($value); |
| 91 |
} |
| 92 |
|
| 93 |
private function findChoiceLabelByValue($value) |
| 94 |
{ |
| 95 |
// In multiple choice fields, the value can't be empty. |
| 96 |
if ($value == '') |
| 97 |
{ |
| 98 |
return $value; |
| 99 |
} |
| 100 |
|
| 101 |
if ($choices = $this->getOptionValue('choices')) |
| 102 |
{ |
| 103 |
foreach ($choices as $choice) |
| 104 |
{ |
| 105 |
// We might lowercase both values? |
| 106 |
if ($choice['value'] == $value) |
| 107 |
{ |
| 108 |
return $choice['label']; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// If we can't assosiacte the given value with a label, return the raw value as a fallback. |
| 114 |
return $value; |
| 115 |
} |
| 116 |
} |