| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.1.146 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base\Fields; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Base\Field; |
| 20 |
use FPFramework\Libs\Registry; |
| 21 |
|
| 22 |
class Toggle extends Field |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Set specific field options |
| 26 |
* |
| 27 |
* @param array $options |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
protected function setFieldOptions($options) |
| 32 |
{ |
| 33 |
$options = new Registry($options); |
| 34 |
|
| 35 |
$default_choices = [ |
| 36 |
'0' => 'FPF_NO', |
| 37 |
'1' => 'FPF_YES' |
| 38 |
]; |
| 39 |
|
| 40 |
$this->field_options = [ |
| 41 |
'type' => 'Toggle', |
| 42 |
'choices' => $options->get('choices', $default_choices), |
| 43 |
'filter' => $options->get('filter', 'sanitize_key') |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Prepares the field data after value has been set |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
protected function postPrepareData() |
| 53 |
{ |
| 54 |
parent::postPrepareData(); |
| 55 |
|
| 56 |
$checked = $this->options['value'] || $this->options['value'] == '0' ? $this->options['value'] : $this->options['default']; |
| 57 |
$choices_keys = is_array($this->options['choices']) && count($this->options['choices']) ? array_keys($this->options['choices']) : []; |
| 58 |
$this->options['checked'] = !$checked && $checked != '0' && $choices_keys && is_array($choices_keys) && count($choices_keys) ? $choices_keys[0] : $checked; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Runs before field renders. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function onBeforeRender() |
| 67 |
{ |
| 68 |
// CSS |
| 69 |
wp_register_style( |
| 70 |
'fpframework-toggle-field', |
| 71 |
FPF_MEDIA_URL . 'admin/css/fpf_toggle.css', |
| 72 |
[], |
| 73 |
FPF_VERSION, |
| 74 |
false |
| 75 |
); |
| 76 |
wp_enqueue_style('fpframework-toggle-field'); |
| 77 |
|
| 78 |
// JS |
| 79 |
wp_register_script( |
| 80 |
'fpframework-toggles-field', |
| 81 |
FPF_MEDIA_URL . 'admin/js/fpf_toggles.js', |
| 82 |
[], |
| 83 |
FPF_VERSION, |
| 84 |
false |
| 85 |
); |
| 86 |
wp_enqueue_script('fpframework-toggles-field'); |
| 87 |
} |
| 88 |
} |