| 1 |
<?php |
| 2 |
|
| 3 |
namespace UiCoreElements; |
| 4 |
|
| 5 |
use Elementor\Widget_Base; |
| 6 |
use UiCoreElements\Helper; |
| 7 |
|
| 8 |
abstract class UiCoreWidget extends Widget_Base |
| 9 |
{ |
| 10 |
|
| 11 |
/** |
| 12 |
* Get widget categories. |
| 13 |
* |
| 14 |
* custom_condition need to always return true if $this->is_edit_mode() is true |
| 15 |
* @return object ['assets-name' => ['condition' => ['key' => 'value'] , 'deps' => ['global-handler-name','other-handler-name'], 'external' => true]] |
| 16 |
*/ |
| 17 |
public abstract function get_styles(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Get widget categories. |
| 21 |
* |
| 22 |
* custom_condition need to always return true if $this->is_edit_mode() is true |
| 23 |
* @return object ['assets-name' => ['condition' => ['key' => 'value'] ,'custom_condition' => true , 'deps' => ['global-handler-name','other-handler-name'], 'external' => true]] |
| 24 |
*/ |
| 25 |
public abstract function get_scripts(); |
| 26 |
|
| 27 |
public function get_style_depends() |
| 28 |
{ |
| 29 |
$assets = $this->get_styles(); |
| 30 |
$final_list = $this->parse_asset_list($assets, 'style'); |
| 31 |
|
| 32 |
//TODO: combine all the styles into one file and return it |
| 33 |
return $final_list; |
| 34 |
} |
| 35 |
|
| 36 |
public function get_script_depends() |
| 37 |
{ |
| 38 |
$assets = $this->get_scripts(); |
| 39 |
$final_list = $this->parse_asset_list($assets, 'script'); |
| 40 |
|
| 41 |
//TODO: combine all the styles into one file and return it |
| 42 |
return $final_list; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private function parse_asset_list($list, $type = 'style') |
| 48 |
{ |
| 49 |
$final_list = []; |
| 50 |
foreach ($list as $key => $value) { |
| 51 |
$deps = (isset($value) && isset($value['deps'])) ? $value['deps'] : []; |
| 52 |
$external = (isset($value) && isset($value['external'])) ? $value['external'] : false; |
| 53 |
$name = $this->get_asset_name($key, $value); |
| 54 |
|
| 55 |
//if name is not empty then we need to add it to the list |
| 56 |
if ($name) { |
| 57 |
$method_name = "register_widget_$type"; |
| 58 |
$final_list[] = Helper::$method_name($name, $deps, $external); |
| 59 |
} |
| 60 |
} |
| 61 |
return $final_list; |
| 62 |
} |
| 63 |
|
| 64 |
private function get_asset_name($key, $value) |
| 65 |
{ |
| 66 |
//check the condition/s |
| 67 |
if (\is_array($value) && !empty($value)) { |
| 68 |
|
| 69 |
//custom condition |
| 70 |
if (isset($value['custom_condition'])) { |
| 71 |
if ($value['custom_condition']) { |
| 72 |
return $key; |
| 73 |
} else { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
//check if the condition is true using elementor's function (always return true if we are in edit mode) |
| 79 |
if ($this->is_edit_mode() || $this->is_control_visible($value, $this->get_settings())) { |
| 80 |
return $key; |
| 81 |
} else { |
| 82 |
return ''; |
| 83 |
} |
| 84 |
} |
| 85 |
// if list is only declaring the assets without any condition then we need to return the key |
| 86 |
return $value; |
| 87 |
} |
| 88 |
protected function is_edit_mode() |
| 89 |
{ |
| 90 |
$elementor_instance = \Elementor\Plugin::instance(); |
| 91 |
if ($elementor_instance->preview->is_preview_mode() || $elementor_instance->editor->is_edit_mode()) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Verify if the control exists and if matches the given value. |
| 100 |
* Usefull for controls that may not be returned, in the settings stack, under certain conditions. |
| 101 |
* |
| 102 |
* @param string $control The control slug. |
| 103 |
* @param string|null $value Optional. The value to check against. |
| 104 |
* @param string $operator Optional. The operator to use for comparing `control` and `value`. Default is '==='. |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
* @since 1.0.15 |
| 108 |
*/ |
| 109 |
protected function is_option(string $control, $value = null, $operator = '==='): bool |
| 110 |
{ |
| 111 |
$option = $this->get_settings_for_display($control); |
| 112 |
|
| 113 |
if (isset($option)) { |
| 114 |
|
| 115 |
if (!isset($value)) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
switch ($operator) { |
| 120 |
case '===': |
| 121 |
return $option === $value; |
| 122 |
case '!==': |
| 123 |
return $option !== $value; |
| 124 |
case '<': |
| 125 |
return $option < $value; |
| 126 |
case '<=': |
| 127 |
return $option <= $value; |
| 128 |
case '>': |
| 129 |
return $option > $value; |
| 130 |
case '>=': |
| 131 |
return $option >= $value; |
| 132 |
default: |
| 133 |
return false; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Verify if the control exists and get both `size` and `unit` values, if requested. |
| 142 |
* |
| 143 |
* @param string $control The control slug. |
| 144 |
* @param array $fallback The fallback value to return if the control does not exist. Should have `size` and, if requested, `unit` keys. |
| 145 |
* @param bool $use_unit If true, the unit will also be checked and returned. |
| 146 |
* |
| 147 |
* @return string `size` and, if requested, `unit` values from the control or the fallback value. |
| 148 |
* @since 1.0.15 |
| 149 |
*/ |
| 150 |
public function get_option_size($control, array $fallback, $use_unit = false) |
| 151 |
{ |
| 152 |
$option = $this->get_settings_for_display($control); |
| 153 |
|
| 154 |
if (isset($option) && ! empty($option['size'])) { |
| 155 |
$size = $control['size']; |
| 156 |
$unit = $use_unit && isset($option['unit']) ? $option['unit'] : ''; |
| 157 |
return $size . $unit; |
| 158 |
} |
| 159 |
|
| 160 |
$fallback_size = $fallback['size']; |
| 161 |
$fallback_unit = $use_unit ? $fallback['unit'] : ''; |
| 162 |
return $fallback_size . $fallback_unit; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Determine if the widget content is dynamic. Dynamic widgets always have |
| 167 |
* their render content refreshed on each page load. |
| 168 |
*/ |
| 169 |
protected function is_dynamic_content(): bool |
| 170 |
{ |
| 171 |
return true; |
| 172 |
} |
| 173 |
} |
| 174 |
|