| 1 |
<?php |
| 2 |
namespace Elementor\TemplateLibrary; |
| 3 |
|
| 4 |
use Elementor\Controls_Stack; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Elementor template library source base. |
| 14 |
* |
| 15 |
* Elementor template library source base handler class is responsible for |
| 16 |
* initializing all the methods controlling the source of Elementor templates. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
* @abstract |
| 20 |
*/ |
| 21 |
abstract class Source_Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* User meta. |
| 25 |
* |
| 26 |
* Holds the current user meta data. |
| 27 |
* |
| 28 |
* @access private |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
private $user_meta; |
| 33 |
|
| 34 |
/** |
| 35 |
* Get template ID. |
| 36 |
* |
| 37 |
* Retrieve the template ID. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* @access public |
| 41 |
* @abstract |
| 42 |
*/ |
| 43 |
abstract public function get_id(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Get template title. |
| 47 |
* |
| 48 |
* Retrieve the template title. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* @access public |
| 52 |
* @abstract |
| 53 |
*/ |
| 54 |
abstract public function get_title(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Register template data. |
| 58 |
* |
| 59 |
* Used to register custom template data like a post type, a taxonomy or any |
| 60 |
* other data. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* @access public |
| 64 |
* @abstract |
| 65 |
*/ |
| 66 |
abstract public function register_data(); |
| 67 |
|
| 68 |
/** |
| 69 |
* Get templates. |
| 70 |
* |
| 71 |
* Retrieve templates from the template library. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* @access public |
| 75 |
* @abstract |
| 76 |
* |
| 77 |
* @param array $args Optional. Filter templates list based on a set of |
| 78 |
* arguments. Default is an empty array. |
| 79 |
*/ |
| 80 |
abstract public function get_items( $args = [] ); |
| 81 |
|
| 82 |
/** |
| 83 |
* Get template. |
| 84 |
* |
| 85 |
* Retrieve a single template from the template library. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* @access public |
| 89 |
* @abstract |
| 90 |
* |
| 91 |
* @param int $template_id The template ID. |
| 92 |
*/ |
| 93 |
abstract public function get_item( $template_id ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Get template data. |
| 97 |
* |
| 98 |
* Retrieve a single template data from the template library. |
| 99 |
* |
| 100 |
* @since 1.5.0 |
| 101 |
* @access public |
| 102 |
* @abstract |
| 103 |
* |
| 104 |
* @param array $args Custom template arguments. |
| 105 |
*/ |
| 106 |
abstract public function get_data( array $args ); |
| 107 |
|
| 108 |
/** |
| 109 |
* Delete template. |
| 110 |
* |
| 111 |
* Delete template from the database. |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* @access public |
| 115 |
* @abstract |
| 116 |
* |
| 117 |
* @param int $template_id The template ID. |
| 118 |
*/ |
| 119 |
abstract public function delete_template( $template_id ); |
| 120 |
|
| 121 |
/** |
| 122 |
* Save template. |
| 123 |
* |
| 124 |
* Save new or update existing template on the database. |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
* @access public |
| 128 |
* @abstract |
| 129 |
* |
| 130 |
* @param array $template_data The template data. |
| 131 |
*/ |
| 132 |
abstract public function save_item( $template_data ); |
| 133 |
|
| 134 |
/** |
| 135 |
* Update template. |
| 136 |
* |
| 137 |
* Update template on the database. |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* @access public |
| 141 |
* @abstract |
| 142 |
* |
| 143 |
* @param array $new_data New template data. |
| 144 |
*/ |
| 145 |
abstract public function update_item( $new_data ); |
| 146 |
|
| 147 |
/** |
| 148 |
* Export template. |
| 149 |
* |
| 150 |
* Export template to a file. |
| 151 |
* |
| 152 |
* @since 1.0.0 |
| 153 |
* @access public |
| 154 |
* @abstract |
| 155 |
* |
| 156 |
* @param int $template_id The template ID. |
| 157 |
*/ |
| 158 |
abstract public function export_template( $template_id ); |
| 159 |
|
| 160 |
/** |
| 161 |
* Template library source base constructor. |
| 162 |
* |
| 163 |
* Initializing the template library source base by registering custom |
| 164 |
* template data. |
| 165 |
* |
| 166 |
* @since 1.0.0 |
| 167 |
* @access public |
| 168 |
*/ |
| 169 |
public function __construct() { |
| 170 |
$this->register_data(); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Mark template as favorite. |
| 175 |
* |
| 176 |
* Update user meta containing his favorite templates. For a given template |
| 177 |
* ID, add the template to the favorite templates or remove it from the |
| 178 |
* favorites, based on the `favorite` parameter. |
| 179 |
* |
| 180 |
* @since 1.9.0 |
| 181 |
* @access public |
| 182 |
* |
| 183 |
* @param int $template_id The template ID. |
| 184 |
* @param bool $favorite Optional. Whether the template is marked as |
| 185 |
* favorite, or not. Default is true. |
| 186 |
* |
| 187 |
* @return int|bool User meta ID if the key didn't exist, true on successful |
| 188 |
* update, false on failure. |
| 189 |
*/ |
| 190 |
public function mark_as_favorite( $template_id, $favorite = true ) { |
| 191 |
$favorites_templates = $this->get_user_meta( 'favorites' ); |
| 192 |
|
| 193 |
if ( ! $favorites_templates ) { |
| 194 |
$favorites_templates = []; |
| 195 |
} |
| 196 |
|
| 197 |
if ( $favorite ) { |
| 198 |
$favorites_templates[ $template_id ] = $favorite; |
| 199 |
} elseif ( isset( $favorites_templates[ $template_id ] ) ) { |
| 200 |
unset( $favorites_templates[ $template_id ] ); |
| 201 |
} |
| 202 |
|
| 203 |
return $this->update_user_meta( 'favorites', $favorites_templates ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get current user meta. |
| 208 |
* |
| 209 |
* Retrieve Elementor meta data for the current user. |
| 210 |
* |
| 211 |
* @since 1.9.0 |
| 212 |
* @access public |
| 213 |
* |
| 214 |
* @param string $item Optional. User meta key. Default is null. |
| 215 |
* |
| 216 |
* @return null|array An array of user meta data, or null otherwise. |
| 217 |
*/ |
| 218 |
public function get_user_meta( $item = null ) { |
| 219 |
if ( null === $this->user_meta ) { |
| 220 |
$this->user_meta = get_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), true ); |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! $this->user_meta ) { |
| 224 |
$this->user_meta = []; |
| 225 |
} |
| 226 |
|
| 227 |
if ( $item ) { |
| 228 |
if ( isset( $this->user_meta[ $item ] ) ) { |
| 229 |
return $this->user_meta[ $item ]; |
| 230 |
} |
| 231 |
|
| 232 |
return null; |
| 233 |
} |
| 234 |
|
| 235 |
return $this->user_meta; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Update current user meta. |
| 240 |
* |
| 241 |
* Update user meta data based on meta key an value. |
| 242 |
* |
| 243 |
* @since 1.9.0 |
| 244 |
* @access public |
| 245 |
* |
| 246 |
* @param string $key Optional. User meta key. |
| 247 |
* @param mixed $value Optional. User meta value. |
| 248 |
* |
| 249 |
* @return int|bool User meta ID if the key didn't exist, true on successful |
| 250 |
* update, false on failure. |
| 251 |
*/ |
| 252 |
public function update_user_meta( $key, $value ) { |
| 253 |
$meta = $this->get_user_meta(); |
| 254 |
|
| 255 |
$meta[ $key ] = $value; |
| 256 |
|
| 257 |
$this->user_meta = $meta; |
| 258 |
|
| 259 |
return update_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), $meta ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Replace elements IDs. |
| 264 |
* |
| 265 |
* For any given Elementor content/data, replace the IDs with new randomly |
| 266 |
* generated IDs. |
| 267 |
* |
| 268 |
* @since 1.0.0 |
| 269 |
* @access protected |
| 270 |
* |
| 271 |
* @param array $content Any type of Elementor data. |
| 272 |
* |
| 273 |
* @return mixed Iterated data. |
| 274 |
*/ |
| 275 |
protected function replace_elements_ids( $content ) { |
| 276 |
return Plugin::$instance->db->iterate_data( $content, function( $element ) { |
| 277 |
$element['id'] = Utils::generate_random_string(); |
| 278 |
|
| 279 |
return $element; |
| 280 |
} ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Get Elementor library user meta prefix. |
| 285 |
* |
| 286 |
* Retrieve user meta prefix used to save Elementor data. |
| 287 |
* |
| 288 |
* @since 1.9.0 |
| 289 |
* @access protected |
| 290 |
* |
| 291 |
* @return string User meta prefix. |
| 292 |
*/ |
| 293 |
protected function get_user_meta_prefix() { |
| 294 |
return 'elementor_library_' . $this->get_id(); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Process content for export/import. |
| 299 |
* |
| 300 |
* Process the content and all the inner elements, and prepare all the |
| 301 |
* elements data for export/import. |
| 302 |
* |
| 303 |
* @since 1.5.0 |
| 304 |
* @access protected |
| 305 |
* |
| 306 |
* @param array $content A set of elements. |
| 307 |
* @param string $method Accepts either `on_export` to export data or |
| 308 |
* `on_import` to import data. |
| 309 |
* |
| 310 |
* @return mixed Processed content data. |
| 311 |
*/ |
| 312 |
protected function process_export_import_content( $content, $method ) { |
| 313 |
return Plugin::$instance->db->iterate_data( |
| 314 |
$content, function( $element_data ) use ( $method ) { |
| 315 |
$element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 316 |
|
| 317 |
// If the widget/element isn't exist, like a plugin that creates a widget but deactivated |
| 318 |
if ( ! $element ) { |
| 319 |
return null; |
| 320 |
} |
| 321 |
|
| 322 |
return $this->process_element_export_import_content( $element, $method ); |
| 323 |
} |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Process single element content for export/import. |
| 329 |
* |
| 330 |
* Process any given element and prepare the element data for export/import. |
| 331 |
* |
| 332 |
* @since 1.5.0 |
| 333 |
* @access protected |
| 334 |
* |
| 335 |
* @param Controls_Stack $element |
| 336 |
* @param string $method |
| 337 |
* |
| 338 |
* @return array Processed element data. |
| 339 |
*/ |
| 340 |
protected function process_element_export_import_content( Controls_Stack $element, $method ) { |
| 341 |
$element_data = $element->get_data(); |
| 342 |
|
| 343 |
if ( method_exists( $element, $method ) ) { |
| 344 |
// TODO: Use the internal element data without parameters. |
| 345 |
$element_data = $element->{$method}( $element_data ); |
| 346 |
} |
| 347 |
|
| 348 |
foreach ( $element->get_controls() as $control ) { |
| 349 |
$control_class = Plugin::$instance->controls_manager->get_control( $control['type'] ); |
| 350 |
|
| 351 |
// If the control isn't exist, like a plugin that creates the control but deactivated. |
| 352 |
if ( ! $control_class ) { |
| 353 |
return $element_data; |
| 354 |
} |
| 355 |
|
| 356 |
if ( method_exists( $control_class, $method ) ) { |
| 357 |
$element_data['settings'][ $control['name'] ] = $control_class->{$method}( $element->get_settings( $control['name'] ), $control ); |
| 358 |
} |
| 359 |
|
| 360 |
// On Export, check if the control has an argument 'export' => false. |
| 361 |
if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) { |
| 362 |
unset( $element_data['settings'][ $control['name'] ] ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return $element_data; |
| 367 |
} |
| 368 |
} |
| 369 |
|