| 1 |
<?php |
| 2 |
namespace Elementor\Modules\KitElementsDefaults\Utils; |
| 3 |
|
| 4 |
use Elementor\Element_Base; |
| 5 |
use Elementor\Elements_Manager; |
| 6 |
use Elementor\Core\Base\Document; |
| 7 |
use Elementor\Core\Utils\Collection; |
| 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 |
$controls = $this->pending_element->get_controls(); |
| 97 |
|
| 98 |
$this->pending_settings = ( new Collection( $this->pending_settings ) ) |
| 99 |
->only( array_merge( array_keys( $controls ), static::SPECIAL_SETTINGS ) ) |
| 100 |
->map( function ( $value, $key ) use ( $controls ) { |
| 101 |
if ( ! in_array( $key, static::SPECIAL_SETTINGS, true ) ) { |
| 102 |
return $value; |
| 103 |
} |
| 104 |
|
| 105 |
return array_intersect_key( $value, $controls ); |
| 106 |
} ) |
| 107 |
->all(); |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
public function kses_deep() { |
| 113 |
if ( ! $this->is_prepared() ) { |
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
$this->pending_settings = wp_kses_post_deep( $this->pending_settings ); |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @param Document $document |
| 124 |
* |
| 125 |
* @return $this |
| 126 |
*/ |
| 127 |
public function prepare_for_export( Document $document ) { |
| 128 |
return $this->run_import_export_sanitize_process( $document, 'on_export' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @param Document $document |
| 133 |
* |
| 134 |
* @return $this |
| 135 |
*/ |
| 136 |
public function prepare_for_import( Document $document ) { |
| 137 |
return $this->run_import_export_sanitize_process( $document, 'on_import' ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function get() { |
| 144 |
if ( ! $this->is_prepared() ) { |
| 145 |
return []; |
| 146 |
} |
| 147 |
|
| 148 |
$settings = $this->pending_settings; |
| 149 |
|
| 150 |
$this->reset(); |
| 151 |
|
| 152 |
return $settings; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param string $type |
| 157 |
* @param array $settings |
| 158 |
* |
| 159 |
* @return Element_Base|null |
| 160 |
*/ |
| 161 |
private function create_element( $type ) { |
| 162 |
$is_widget = in_array( $type, $this->widget_types, true ); |
| 163 |
$is_inner_section = 'inner-section' === $type; |
| 164 |
|
| 165 |
if ( $is_inner_section ) { |
| 166 |
return $this->elements_manager->create_element_instance( [ |
| 167 |
'elType' => 'section', |
| 168 |
'isInner' => true, |
| 169 |
'id' => '0', |
| 170 |
] ); |
| 171 |
} |
| 172 |
|
| 173 |
if ( $is_widget ) { |
| 174 |
return $this->elements_manager->create_element_instance( [ |
| 175 |
'elType' => 'widget', |
| 176 |
'widgetType' => $type, |
| 177 |
'id' => '0', |
| 178 |
] ); |
| 179 |
} |
| 180 |
|
| 181 |
return $this->elements_manager->create_element_instance( [ |
| 182 |
'elType' => $type, |
| 183 |
'id' => '0', |
| 184 |
] ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @param Document $document |
| 189 |
* @param $process_type |
| 190 |
* |
| 191 |
* @return $this |
| 192 |
*/ |
| 193 |
private function run_import_export_sanitize_process( Document $document, $process_type ) { |
| 194 |
if ( ! $this->is_prepared() ) { |
| 195 |
return $this; |
| 196 |
} |
| 197 |
|
| 198 |
$result = $document->process_element_import_export( |
| 199 |
$this->pending_element, |
| 200 |
$process_type, |
| 201 |
[ 'settings' => $this->pending_settings ] |
| 202 |
); |
| 203 |
|
| 204 |
if ( empty( $result['settings'] ) ) { |
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
$this->pending_settings = $result['settings']; |
| 209 |
|
| 210 |
return $this; |
| 211 |
} |
| 212 |
} |
| 213 |
|