1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
<?php namespace Elementor\TemplateLibrary;
use Elementor\Controls_Stack; use Elementor\Core\Settings\Page\Model; use Elementor\Plugin; use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
/** * Elementor template library source base. * * Elementor template library source base handler class is responsible for * initializing all the methods controlling the source of Elementor templates. * * @since 1.0.0 * @abstract */ abstract class Source_Base {
/** * User meta. * * Holds the current user meta data. * * @access private * * @var array */ private $user_meta;
/** * Get template ID. * * Retrieve the template ID. * * @since 1.0.0 * @access public * @abstract */ abstract public function get_id();
/** * Get template title. * * Retrieve the template title. * * @since 1.0.0 * @access public * @abstract */ abstract public function get_title();
/** * Register template data. * * Used to register custom template data like a post type, a taxonomy or any * other data. * * @since 1.0.0 * @access public * @abstract */ abstract public function register_data();
/** * Get templates. * * Retrieve templates from the template library. * * @since 1.0.0 * @access public * @abstract * * @param array $args Optional. Filter templates list based on a set of * arguments. Default is an empty array. */ abstract public function get_items( $args = [] );
/** * Get template. * * Retrieve a single template from the template library. * * @since 1.0.0 * @access public * @abstract * * @param int $template_id The template ID. */ abstract public function get_item( $template_id );
/** * Get template data. * * Retrieve a single template data from the template library. * * @since 1.5.0 * @access public * @abstract * * @param array $args Custom template arguments. */ abstract public function get_data( array $args );
/** * Delete template. * * Delete template from the database. * * @since 1.0.0 * @access public * @abstract * * @param int $template_id The template ID. */ abstract public function delete_template( $template_id );
/** * Save template. * * Save new or update existing template on the database. * * @since 1.0.0 * @access public * @abstract * * @param array $template_data The template data. */ abstract public function save_item( $template_data );
/** * Update template. * * Update template on the database. * * @since 1.0.0 * @access public * @abstract * * @param array $new_data New template data. */ abstract public function update_item( $new_data );
/** * Export template. * * Export template to a file. * * @since 1.0.0 * @access public * @abstract * * @param int $template_id The template ID. */ abstract public function export_template( $template_id );
/** * Template library source base constructor. * * Initializing the template library source base by registering custom * template data. * * @since 1.0.0 * @access public */ public function __construct() { $this->register_data(); }
/** * Mark template as favorite. * * Update user meta containing his favorite templates. For a given template * ID, add the template to the favorite templates or remove it from the * favorites, based on the `favorite` parameter. * * @since 1.9.0 * @access public * * @param int $template_id The template ID. * @param bool $favorite Optional. Whether the template is marked as * favorite, or not. Default is true. * * @return int|bool User meta ID if the key didn't exist, true on successful * update, false on failure. */ public function mark_as_favorite( $template_id, $favorite = true ) { $favorites_templates = $this->get_user_meta( 'favorites' );
if ( ! $favorites_templates ) { $favorites_templates = []; }
if ( $favorite ) { $favorites_templates[ $template_id ] = $favorite; } elseif ( isset( $favorites_templates[ $template_id ] ) ) { unset( $favorites_templates[ $template_id ] ); }
return $this->update_user_meta( 'favorites', $favorites_templates ); }
/** * Get current user meta. * * Retrieve Elementor meta data for the current user. * * @since 1.9.0 * @access public * * @param string $item Optional. User meta key. Default is null. * * @return null|array An array of user meta data, or null otherwise. */ public function get_user_meta( $item = null ) { if ( null === $this->user_meta ) { $this->user_meta = get_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), true ); }
if ( ! $this->user_meta ) { $this->user_meta = []; }
if ( $item ) { if ( isset( $this->user_meta[ $item ] ) ) { return $this->user_meta[ $item ]; }
return null; }
return $this->user_meta; }
/** * Update current user meta. * * Update user meta data based on meta key an value. * * @since 1.9.0 * @access public * * @param string $key Optional. User meta key. * @param mixed $value Optional. User meta value. * * @return int|bool User meta ID if the key didn't exist, true on successful * update, false on failure. */ public function update_user_meta( $key, $value ) { $meta = $this->get_user_meta();
$meta[ $key ] = $value;
$this->user_meta = $meta;
return update_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), $meta ); }
/** * Replace elements IDs. * * For any given Elementor content/data, replace the IDs with new randomly * generated IDs. * * @since 1.0.0 * @access protected * * @param array $content Any type of Elementor data. * * @return mixed Iterated data. */ protected function replace_elements_ids( $content ) { return Plugin::$instance->db->iterate_data( $content, function( $element ) { $element['id'] = Utils::generate_random_string();
return apply_filters( 'elementor/document/element/replace_id', $element ); } ); }
/** * Get Elementor library user meta prefix. * * Retrieve user meta prefix used to save Elementor data. * * @since 1.9.0 * @access protected * * @return string User meta prefix. */ protected function get_user_meta_prefix() { return 'elementor_library_' . $this->get_id(); }
/** * Process content for export/import. * * Process the content and all the inner elements, and prepare all the * elements data for export/import. * * @since 1.5.0 * @access protected * * @param array $content A set of elements. * @param string $method Accepts either `on_export` to export data or * `on_import` to import data. * * @return mixed Processed content data. */ protected function process_export_import_content( $content, $method ) { return Plugin::$instance->db->iterate_data( $content, function( $element_data ) use ( $method ) { $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
// If the widget/element isn't exist, like a plugin that creates a widget but deactivated if ( ! $element ) { return null; }
return $this->process_element_export_import_content( $element, $method ); } ); }
/** * Process single element content for export/import. * * Process any given element and prepare the element data for export/import. * * @since 1.5.0 * @access protected * * @param Controls_Stack $element * @param string $method * * @return array Processed element data. */ protected function process_element_export_import_content( Controls_Stack $element, $method ) { $element_data = $element->get_data();
if ( method_exists( $element, $method ) ) { // TODO: Use the internal element data without parameters. $element_data = $element->{$method}( $element_data ); }
foreach ( $element->get_controls() as $control ) { $control_class = Plugin::$instance->controls_manager->get_control( $control['type'] );
// If the control isn't exist, like a plugin that creates the control but deactivated. if ( ! $control_class ) { return $element_data; }
if ( method_exists( $control_class, $method ) ) { $element_data['settings'][ $control['name'] ] = $control_class->{$method}( $element->get_settings( $control['name'] ), $control ); }
// On Export, check if the control has an argument 'export' => false. if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) { unset( $element_data['settings'][ $control['name'] ] ); } }
return $element_data; }
public function get_item_children( array $args = [] ) { return []; }
public function search_templates( array $args = [] ) { return []; }
public function save_folder( array $folder_data = [] ) { return new \WP_Error( 'template_error', 'Folders cannot be created in this source' ); }
public function move_template_to_folder( array $folder_data = [] ) { return new \WP_Error( 'template_error', 'Templates cannot be moved in this source' ); }
public function move_bulk_templates_to_folder( array $folder_data = [] ) { return new \WP_Error( 'template_error', 'Templates cannot be moved in this source' ); }
public function save_bulk_items( array $args = [] ) { return []; }
public function get_bulk_items( array $args = [] ) { return []; }
/** * @param int $template_id * @return \Elementor\Core\Base\Document|\WP_Error */ public function create_document_for_preview( int $template_id ) { return new \WP_Error( 'template_error', 'Can not generate preview for this source' ); }
/** * @param int $template_id * @param mixed $data * @return string|\WP_Error * @throws \Exception */ public function save_item_preview( int $template_id, $data ) { return new \WP_Error( 'template_error', 'Cannot save previews for this source' ); }
/** * @param int $template_id * @param string $error * @return string|\WP_Error * @throws \Exception */ public function mark_preview_as_failed( int $template_id, string $error ) { return new \WP_Error( 'template_error', 'Cannot mark preview as failed for this source' ); }
/** * @param int[] $template_ids * @return bool|\WP_Error */ public function bulk_delete_items( array $template_ids ) { return new \WP_Error( 'template_error', 'Bulk delete action is not supported for this source' ); }
/** * @param int[] $template_ids * @return bool|\WP_Error */ public function bulk_undo_delete_items( array $template_ids ) { return new \WP_Error( 'template_error', 'Undo delete action is not supported for this source' ); }
/** * @return array|\WP_Error */ public function get_quota() { return new \WP_Error( 'template_error', 'This source does not support quotas' ); }
/** * @return array|\WP_Error */ public function import_template( $name, $path ) { return new \WP_Error( 'template_error', 'This source does not support import' ); }
public function supports_quota(): bool { return false; }
public function validate_quota( array $items ) { return new \WP_Error( 'quota_error', 'This source does not support quota validation' ); }
public function prepare_import_template_data( $file_path ) { $data = json_decode( Utils::file_get_contents( $file_path ), true );
if ( empty( $data ) ) { return new \WP_Error( 'file_error', 'Invalid File' ); }
$content = $data['content'];
if ( ! is_array( $content ) ) { return new \WP_Error( 'file_error', 'Invalid Content In File' ); }
$content = $this->process_export_import_content( $content, 'on_import' );
$content = apply_filters( "elementor/template_library/sources/{$this->get_id()}/import/elements", $content );
$page_settings = [];
if ( ! empty( $data['page_settings'] ) ) { $page = new Model( [ 'id' => 0, 'settings' => $data['page_settings'], ] );
$page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
if ( ! empty( $page_settings_data['settings'] ) ) { $page_settings = $page_settings_data['settings']; } }
return [ 'content' => $content, 'page_settings' => $page_settings, 'title' => $data['title'], 'type' => $data['type'], ]; }
/** * Send file headers. * * Set the file header when export template data to a file. * * @since 1.6.0 * @access protected * * @param string $file_name File name. * @param int $file_size File size. */ protected function send_file_headers( $file_name, $file_size ) { header( 'Content-Type: application/octet-stream' ); header( 'Content-Disposition: attachment; filename=' . $file_name ); header( 'Expires: 0' ); header( 'Cache-Control: must-revalidate' ); header( 'Pragma: public' ); header( 'Content-Length: ' . $file_size ); }
protected function filesize( $path ) { return filesize( $path ); }
protected function serve_zip( $zip_complete_path ): void { @ob_end_flush(); @readfile( $zip_complete_path ); }
protected function serve_file( string $file_content ): void { @ob_end_clean(); flush();
// PHPCS - Export widget json echo $file_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } }
|