| 1 |
<?php |
| 2 |
namespace Elementor\Modules\KitElementsDefaults\Utils; |
| 3 |
|
| 4 |
use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager; |
| 5 |
use Elementor\Element_Base; |
| 6 |
use Elementor\Elements_Manager; |
| 7 |
use Elementor\Core\Base\Document; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Settings_Sanitizer { |
| 14 |
|
| 15 |
const SPECIAL_SETTINGS = [ |
| 16 |
'__dynamic__', |
| 17 |
'__globals__', |
| 18 |
]; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var Elements_Manager |
| 22 |
*/ |
| 23 |
private $elements_manager; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private $widget_types; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Element_Base | null |
| 32 |
*/ |
| 33 |
private $pending_element = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var array | null |
| 37 |
*/ |
| 38 |
private $pending_settings = null; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param Elements_Manager $elements_manager |
| 42 |
* @param array $widget_types |
| 43 |
*/ |
| 44 |
public function __construct( Elements_Manager $elements_manager, array $widget_types = [] ) { |
| 45 |
$this->elements_manager = $elements_manager; |
| 46 |
$this->widget_types = $widget_types; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param $type |
| 51 |
* |
| 52 |
* @return $this |
| 53 |
*/ |
| 54 |
public function for( $type ) { |
| 55 |
$this->pending_element = $this->create_element( $type ); |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param $settings |
| 62 |
* |
| 63 |
* @return $this |
| 64 |
*/ |
| 65 |
public function using( $settings ) { |
| 66 |
$this->pending_settings = $settings; |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return $this |
| 73 |
*/ |
| 74 |
public function reset() { |
| 75 |
$this->pending_element = null; |
| 76 |
$this->pending_settings = null; |
| 77 |
|
| 78 |
return $this; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public function is_prepared() { |
| 85 |
return $this->pending_element && is_array( $this->pending_settings ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return $this |
| 90 |
*/ |
| 91 |
public function remove_invalid_settings() { |
| 92 |
if ( ! $this->is_prepared() ) { |
| 93 |
return $this; |
| 94 |
} |
| 95 |
|
| 96 |
$valid_settings_keys = $this->get_valid_settings_keys( |
| 97 |
$this->pending_element->get_controls() |
| 98 |
); |
| 99 |
|
| 100 |
$this->pending_settings = $this->filter_invalid_settings( |
| 101 |
$this->pending_settings, |
| 102 |
array_merge( $valid_settings_keys, self::SPECIAL_SETTINGS ) |
| 103 |
); |
| 104 |
|
| 105 |
foreach ( self::SPECIAL_SETTINGS as $special_setting ) { |
| 106 |
if ( ! isset( $this->pending_settings[ $special_setting ] ) ) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
$this->pending_settings[ $special_setting ] = $this->filter_invalid_settings( |
| 111 |
$this->pending_settings[ $special_setting ], |
| 112 |
$valid_settings_keys |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
public function kses_deep() { |
| 120 |
if ( ! $this->is_prepared() ) { |
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
$this->pending_settings = map_deep( $this->pending_settings, function( $value ) { |
| 125 |
if ( ! is_string( $value ) ) { |
| 126 |
return $value; |
| 127 |
} |
| 128 |
|
| 129 |
return wp_kses_post( $value ); |
| 130 |
} ); |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param Document $document |
| 137 |
* |
| 138 |
* @return $this |
| 139 |
*/ |
| 140 |
public function prepare_for_export( Document $document ) { |
| 141 |
return $this->run_import_export_sanitize_process( $document, 'on_export' ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @param Document $document |
| 146 |
* |
| 147 |
* @return $this |
| 148 |
*/ |
| 149 |
public function prepare_for_import( Document $document ) { |
| 150 |
return $this->run_import_export_sanitize_process( $document, 'on_import' ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
public function get() { |
| 157 |
if ( ! $this->is_prepared() ) { |
| 158 |
return []; |
| 159 |
} |
| 160 |
|
| 161 |
$settings = $this->pending_settings; |
| 162 |
|
| 163 |
$this->reset(); |
| 164 |
|
| 165 |
return $settings; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param string $type |
| 170 |
* |
| 171 |
* @return Element_Base|null |
| 172 |
*/ |
| 173 |
private function create_element( $type ) { |
| 174 |
$is_widget = in_array( $type, $this->widget_types, true ); |
| 175 |
$is_inner_section = 'inner-section' === $type; |
| 176 |
|
| 177 |
if ( $is_inner_section ) { |
| 178 |
return $this->elements_manager->create_element_instance( [ |
| 179 |
'elType' => 'section', |
| 180 |
'isInner' => true, |
| 181 |
'id' => '0', |
| 182 |
] ); |
| 183 |
} |
| 184 |
|
| 185 |
if ( $is_widget ) { |
| 186 |
return $this->elements_manager->create_element_instance( [ |
| 187 |
'elType' => 'widget', |
| 188 |
'widgetType' => $type, |
| 189 |
'id' => '0', |
| 190 |
] ); |
| 191 |
} |
| 192 |
|
| 193 |
return $this->elements_manager->create_element_instance( [ |
| 194 |
'elType' => $type, |
| 195 |
'id' => '0', |
| 196 |
] ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @param Document $document |
| 201 |
* @param $process_type |
| 202 |
* |
| 203 |
* @return $this |
| 204 |
*/ |
| 205 |
private function run_import_export_sanitize_process( Document $document, $process_type ) { |
| 206 |
if ( ! $this->is_prepared() ) { |
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
$result = $document->process_element_import_export( |
| 211 |
$this->pending_element, |
| 212 |
$process_type, |
| 213 |
[ 'settings' => $this->pending_settings ] |
| 214 |
); |
| 215 |
|
| 216 |
if ( empty( $result['settings'] ) ) { |
| 217 |
return $this; |
| 218 |
} |
| 219 |
|
| 220 |
$this->pending_settings = $result['settings']; |
| 221 |
|
| 222 |
return $this; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get all the available settings of a specific element, including responsive settings. |
| 227 |
* |
| 228 |
* @param array $controls |
| 229 |
* |
| 230 |
* @return array |
| 231 |
*/ |
| 232 |
private function get_valid_settings_keys( $controls ) { |
| 233 |
if ( ! $controls ) { |
| 234 |
return []; |
| 235 |
} |
| 236 |
|
| 237 |
$control_keys = array_keys( $controls ); |
| 238 |
|
| 239 |
$optional_responsive_keys = [ |
| 240 |
Breakpoints_Manager::BREAKPOINT_KEY_MOBILE, |
| 241 |
Breakpoints_Manager::BREAKPOINT_KEY_MOBILE_EXTRA, |
| 242 |
Breakpoints_Manager::BREAKPOINT_KEY_TABLET, |
| 243 |
Breakpoints_Manager::BREAKPOINT_KEY_TABLET_EXTRA, |
| 244 |
Breakpoints_Manager::BREAKPOINT_KEY_LAPTOP, |
| 245 |
Breakpoints_Manager::BREAKPOINT_KEY_WIDESCREEN, |
| 246 |
]; |
| 247 |
|
| 248 |
$settings = []; |
| 249 |
|
| 250 |
foreach ( $control_keys as $control_key ) { |
| 251 |
// Add the responsive settings. |
| 252 |
foreach ( $optional_responsive_keys as $responsive_key ) { |
| 253 |
$settings[] = "{$control_key}_{$responsive_key}"; |
| 254 |
} |
| 255 |
// Add the setting itself (not responsive). |
| 256 |
$settings[] = $control_key; |
| 257 |
} |
| 258 |
|
| 259 |
return $settings; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Remove invalid settings. |
| 264 |
* |
| 265 |
* @param $settings |
| 266 |
* @param $valid_settings_keys |
| 267 |
* |
| 268 |
* @return array |
| 269 |
*/ |
| 270 |
private function filter_invalid_settings( $settings, $valid_settings_keys ) { |
| 271 |
return array_filter( |
| 272 |
$settings, |
| 273 |
function ( $setting_key ) use ( $valid_settings_keys ) { |
| 274 |
return in_array( $setting_key, $valid_settings_keys, true ); |
| 275 |
}, |
| 276 |
ARRAY_FILTER_USE_KEY |
| 277 |
); |
| 278 |
} |
| 279 |
} |
| 280 |
|