PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.1
Elementor Website Builder – more than just a page builder v3.1.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / usage / module.php

module.php in Elementor Website Builder – more than just a page builder 3.1.1, at modules/usage/module.php

606 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\Usage;
3
4 use Elementor\Core\Base\Document;
5 use Elementor\Core\Base\Module as BaseModule;
6 use Elementor\Core\DynamicTags\Manager;
7 use Elementor\Modules\System_Info\Module as System_Info;
8 use Elementor\Plugin;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 /**
15 * Elementor usage module.
16 *
17 * Elementor usage module handler class is responsible for registering and
18 * managing Elementor usage data.
19 *
20 */
21 class Module extends BaseModule {
22 const GENERAL_TAB = 'general';
23 const META_KEY = '_elementor_controls_usage';
24 const OPTION_NAME = 'elementor_controls_usage';
25
26 /**
27 * @var bool
28 */
29 private $is_document_saving = false;
30
31 /**
32 * Get module name.
33 *
34 * Retrieve the usage module name.
35 *
36 * @access public
37 *
38 * @return string Module name.
39 */
40 public function get_name() {
41 return 'usage';
42 }
43
44 /**
45 * Get doc type count.
46 *
47 * Get count of documents based on doc type
48 *
49 * Remove 'wp-' from $doc_type for BC, support doc type change since 2.7.0.
50 *
51 * @param \Elementor\Core\Documents_Manager $doc_class
52 * @param String $doc_type
53 *
54 * @return int
55 */
56 public function get_doc_type_count( $doc_class, $doc_type ) {
57 static $posts = null;
58 static $library = null;
59
60 if ( null === $posts ) {
61 $posts = \Elementor\Tracker::get_posts_usage();
62 }
63
64 if ( null === $library ) {
65 $library = \Elementor\Tracker::get_library_usage();
66 }
67
68 $posts_usage = $posts;
69
70 if ( $doc_class::get_property( 'show_in_library' ) ) {
71 $posts_usage = $library;
72 }
73
74 $doc_type_common = str_replace( 'wp-', '', $doc_type );
75
76 $doc_usage = isset( $posts_usage[ $doc_type_common ] ) ? $posts_usage[ $doc_type_common ] : 0;
77
78 return is_array( $doc_usage ) ? $doc_usage['publish'] : $doc_usage;
79 }
80
81 /**
82 * Get formatted usage.
83 *
84 * Retrieve formatted usage, for frontend.
85 *
86 * @param String format
87 *
88 * @return array
89 */
90 public function get_formatted_usage( $format = 'html' ) {
91 $usage = [];
92
93 foreach ( get_option( self::OPTION_NAME, [] ) as $doc_type => $elements ) {
94 $doc_class = Plugin::$instance->documents->get_document_type( $doc_type );
95
96 if ( 'html' === $format && $doc_class ) {
97 $doc_title = $doc_class::get_title();
98 } else {
99 $doc_title = $doc_type;
100 }
101
102 $doc_count = $this->get_doc_type_count( $doc_class, $doc_type );
103
104 $tab_group = $doc_class::get_property( 'admin_tab_group' );
105
106 if ( 'html' === $format && $tab_group ) {
107 $doc_title = ucwords( $tab_group ) . ' - ' . $doc_title;
108 }
109
110 // Replace element type with element title.
111 foreach ( $elements as $element_type => $data ) {
112 unset( $elements[ $element_type ] );
113
114 if ( in_array( $element_type, [ 'section', 'column' ], true ) ) {
115 continue;
116 }
117
118 $widget_instance = Plugin::$instance->widgets_manager->get_widget_types( $element_type );
119
120 if ( 'html' === $format && $widget_instance ) {
121 $widget_title = $widget_instance->get_title();
122 } else {
123 $widget_title = $element_type;
124 }
125
126 $elements[ $widget_title ] = $data['count'];
127 }
128
129 // Sort elements by key.
130 ksort( $elements );
131
132 $usage[ $doc_type ] = [
133 'title' => $doc_title,
134 'elements' => $elements,
135 'count' => $doc_count,
136 ];
137
138 // Sort usage by title.
139 uasort( $usage, function( $a, $b ) {
140 return ( $a['title'] > $b['title'] );
141 } );
142
143 // If title includes '-' will have lower priority.
144 uasort( $usage, function( $a ) {
145 return strpos( $a['title'], '-' );
146 } );
147 }
148
149 return $usage;
150 }
151
152 /**
153 * Before document Save.
154 *
155 * Called on elementor/document/before_save, remove document from global & set saving flag.
156 *
157 * @param Document $document
158 * @param array $data new settings to save.
159 */
160 public function before_document_save( $document, $data ) {
161 $current_status = get_post_status( $document->get_post() );
162 $new_status = isset( $data['settings']['post_status'] ) ? $data['settings']['post_status'] : '';
163
164 if ( $current_status === $new_status ) {
165 $this->remove_from_global( $document );
166 }
167
168 $this->is_document_saving = true;
169 }
170
171 /**
172 * After document save.
173 *
174 * Called on elementor/document/after_save, adds document to global & clear saving flag.
175 *
176 * @param Document $document
177 */
178 public function after_document_save( $document ) {
179 if ( Document::STATUS_PUBLISH === $document->get_post()->post_status || Document::STATUS_PRIVATE === $document->get_post()->post_status ) {
180 $this->save_document_usage( $document );
181 }
182
183 $this->is_document_saving = false;
184 }
185
186 /**
187 * On status change.
188 *
189 * Called on transition_post_status.
190 *
191 * @param string $new_status
192 * @param string $old_status
193 * @param \WP_Post $post
194 */
195 public function on_status_change( $new_status, $old_status, $post ) {
196 if ( wp_is_post_autosave( $post ) ) {
197 return;
198 }
199
200 // If it's from elementor editor, the usage should be saved via `before_document_save`/`after_document_save`.
201 if ( $this->is_document_saving ) {
202 return;
203 }
204
205 $document = Plugin::$instance->documents->get( $post->ID );
206
207 if ( ! $document ) {
208 return;
209 }
210
211 $is_public_unpublish = 'publish' === $old_status && 'publish' !== $new_status;
212 $is_private_unpublish = 'private' === $old_status && 'private' !== $new_status;
213
214 if ( $is_public_unpublish || $is_private_unpublish ) {
215 $this->remove_from_global( $document );
216 }
217
218 $is_public_publish = 'publish' !== $old_status && 'publish' === $new_status;
219 $is_private_publish = 'private' !== $old_status && 'private' === $new_status;
220
221 if ( $is_public_publish || $is_private_publish ) {
222 $this->save_document_usage( $document );
223 }
224 }
225
226 /**
227 * On before delete post.
228 *
229 * Called on on_before_delete_post.
230 *
231 * @param int $post_id
232 */
233 public function on_before_delete_post( $post_id ) {
234 $document = Plugin::$instance->documents->get( $post_id );
235
236 if ( $document->get_id() !== $document->get_main_id() ) {
237 return;
238 }
239
240 $this->remove_from_global( $document );
241 }
242
243 /**
244 * Add's tracking data.
245 *
246 * Called on elementor/tracker/send_tracking_data_params.
247 *
248 * @param array $params
249 *
250 * @return array
251 */
252 public function add_tracking_data( $params ) {
253 $params['usages']['elements'] = get_option( self::OPTION_NAME );
254
255 return $params;
256 }
257
258 /**
259 * Recalculate usage.
260 *
261 * Recalculate usage for all elementor posts.
262 *
263 * @param int $limit
264 * @param int $offset
265 *
266 * @return int
267 */
268 public function recalc_usage( $limit = -1, $offset = 0 ) {
269 // While requesting recalc_usage, data should be deleted.
270 // if its in a batch the data should be deleted only on the first batch.
271 if ( 0 === $offset ) {
272 delete_option( self::OPTION_NAME );
273 }
274
275 $post_types = get_post_types( array( 'public' => true ) );
276
277 $query = new \WP_Query( [
278 'meta_key' => '_elementor_data',
279 'post_type' => $post_types,
280 'post_status' => [ 'publish', 'private' ],
281 'posts_per_page' => $limit,
282 'offset' => $offset,
283 ] );
284
285 foreach ( $query->posts as $post ) {
286 $document = Plugin::$instance->documents->get( $post->ID );
287
288 if ( ! $document ) {
289 continue;
290 }
291
292 $this->after_document_save( $document );
293 }
294
295 // Clear query memory before leave.
296 wp_cache_flush();
297
298 return count( $query->posts );
299 }
300
301 /**
302 * Increase controls count.
303 *
304 * Increase controls count, for each element.
305 *
306 * @param array &$element_ref
307 * @param string $tab
308 * @param string $section
309 * @param string $control
310 * @param int $count
311 */
312 private function increase_controls_count( &$element_ref, $tab, $section, $control, $count ) {
313 if ( ! isset( $element_ref['controls'][ $tab ] ) ) {
314 $element_ref['controls'][ $tab ] = [];
315 }
316
317 if ( ! isset( $element_ref['controls'][ $tab ][ $section ] ) ) {
318 $element_ref['controls'][ $tab ][ $section ] = [];
319 }
320
321 if ( ! isset( $element_ref['controls'][ $tab ][ $section ][ $control ] ) ) {
322 $element_ref['controls'][ $tab ][ $section ][ $control ] = 0;
323 }
324
325 $element_ref['controls'][ $tab ][ $section ][ $control ] += $count;
326 }
327
328 /**
329 * Add Controls
330 *
331 * Add's controls to this element_ref, returns changed controls count.
332 *
333 * @param array $settings_controls
334 * @param array $element_controls
335 * @param array &$element_ref
336 *
337 * @return int ($changed_controls_count).
338 */
339 private function add_controls( $settings_controls, $element_controls, &$element_ref ) {
340 $changed_controls_count = 0;
341
342 // Loop over all element settings.
343 foreach ( $settings_controls as $control => $value ) {
344 if ( empty( $element_controls[ $control ] ) ) {
345 continue;
346 }
347
348 $control_config = $element_controls[ $control ];
349
350 if ( ! isset( $control_config['section'], $control_config['default'] ) ) {
351 continue;
352 }
353
354 $tab = $control_config['tab'];
355 $section = $control_config['section'];
356
357 // If setting value is not the control default.
358 if ( $value !== $control_config['default'] ) {
359 $this->increase_controls_count( $element_ref, $tab, $section, $control, 1 );
360
361 $changed_controls_count++;
362 }
363 }
364
365 return $changed_controls_count;
366 }
367
368 /**
369 * Add general controls.
370 *
371 * Extract general controls to element ref, return clean `$settings_control`.
372 *
373 * @param array $settings_controls
374 * @param array &$element_ref
375 *
376 * @return array ($settings_controls).
377 */
378 private function add_general_controls( $settings_controls, &$element_ref ) {
379 if ( ! empty( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) ) {
380 $settings_controls = array_merge( $settings_controls, $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] );
381
382 // Add dynamic count to controls under `general` tab.
383 $this->increase_controls_count(
384 $element_ref,
385 self::GENERAL_TAB,
386 Manager::DYNAMIC_SETTING_KEY,
387 'count',
388 count( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] )
389 );
390 }
391
392 return $settings_controls;
393 }
394
395 /**
396 * Add to global.
397 *
398 * Add's usage to global (update database).
399 *
400 * @param string $doc_name
401 * @param array $doc_usage
402 */
403 private function add_to_global( $doc_name, $doc_usage ) {
404 $global_usage = get_option( self::OPTION_NAME, [] );
405
406 foreach ( $doc_usage as $element_type => $element_data ) {
407 if ( ! isset( $global_usage[ $doc_name ] ) ) {
408 $global_usage[ $doc_name ] = [];
409 }
410
411 if ( ! isset( $global_usage[ $doc_name ][ $element_type ] ) ) {
412 $global_usage[ $doc_name ][ $element_type ] = [
413 'count' => 0,
414 'controls' => [],
415 ];
416 }
417
418 $global_element_ref = &$global_usage[ $doc_name ][ $element_type ];
419 $global_element_ref['count'] += $element_data['count'];
420
421 if ( empty( $element_data['controls'] ) ) {
422 continue;
423 }
424
425 foreach ( $element_data['controls'] as $tab => $sections ) {
426 foreach ( $sections as $section => $controls ) {
427 foreach ( $controls as $control => $count ) {
428 $this->increase_controls_count( $global_element_ref, $tab, $section, $control, $count );
429 }
430 }
431 }
432 }
433
434 update_option( self::OPTION_NAME, $global_usage, false );
435 }
436
437 /**
438 * Remove from global.
439 *
440 * Remove's usage from global (update database).
441 *
442 * @param Document $document
443 */
444 private function remove_from_global( $document ) {
445 $prev_usage = $document->get_meta( self::META_KEY );
446
447 if ( empty( $prev_usage ) ) {
448 return;
449 }
450
451 $doc_name = $document->get_name();
452
453 $global_usage = get_option( self::OPTION_NAME, [] );
454
455 foreach ( $prev_usage as $element_type => $doc_value ) {
456 if ( isset( $global_usage[ $doc_name ][ $element_type ]['count'] ) ) {
457 $global_usage[ $doc_name ][ $element_type ]['count'] -= $prev_usage[ $element_type ]['count'];
458
459 if ( 0 === $global_usage[ $doc_name ][ $element_type ]['count'] ) {
460 unset( $global_usage[ $doc_name ][ $element_type ] );
461
462 if ( 0 === count( $global_usage[ $doc_name ] ) ) {
463 unset( $global_usage[ $doc_name ] );
464 }
465
466 continue;
467 }
468
469 foreach ( $prev_usage[ $element_type ]['controls'] as $tab => $sections ) {
470 foreach ( $sections as $section => $controls ) {
471 foreach ( $controls as $control => $count ) {
472 if ( isset( $global_usage[ $doc_name ][ $element_type ]['controls'][ $tab ][ $section ][ $control ] ) ) {
473 $section_ref = &$global_usage[ $doc_name ][ $element_type ]['controls'][ $tab ][ $section ];
474
475 $section_ref[ $control ] -= $count;
476
477 if ( 0 === $section_ref[ $control ] ) {
478 unset( $section_ref[ $control ] );
479 }
480 }
481 }
482 }
483 }
484 }
485 }
486
487 update_option( self::OPTION_NAME, $global_usage, false );
488
489 $document->delete_meta( self::META_KEY );
490 }
491
492 /**
493 * Get elements usage.
494 *
495 * Get's the current elements usage by passed elements array parameter.
496 *
497 * @param array $elements
498 *
499 * @return array
500 */
501 private function get_elements_usage( $elements ) {
502 $usage = [];
503
504 Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage ) {
505 if ( empty( $element['widgetType'] ) ) {
506 $type = $element['elType'];
507 $element_instance = Plugin::$instance->elements_manager->get_element_types( $type );
508 } else {
509 $type = $element['widgetType'];
510 $element_instance = Plugin::$instance->widgets_manager->get_widget_types( $type );
511 }
512
513 if ( ! isset( $usage[ $type ] ) ) {
514 $usage[ $type ] = [
515 'count' => 0,
516 'control_percent' => 0,
517 'controls' => [],
518 ];
519 }
520
521 $usage[ $type ]['count']++;
522
523 if ( ! $element_instance ) {
524 return $element;
525 }
526
527 $element_controls = $element_instance->get_controls();
528
529 if ( isset( $element['settings'] ) ) {
530 $settings_controls = $element['settings'];
531 $element_ref = &$usage[ $type ];
532
533 // Add dynamic values.
534 $settings_controls = $this->add_general_controls( $settings_controls, $element_ref );
535
536 $changed_controls_count = $this->add_controls( $settings_controls, $element_controls, $element_ref );
537
538 $percent = $changed_controls_count / ( count( $element_controls ) / 100 );
539
540 $usage[ $type ] ['control_percent'] = (int) round( $percent );
541 }
542
543 return $element;
544 } );
545
546 return $usage;
547 }
548
549 /**
550 * Save document usage.
551 *
552 * Save requested document usage, and update global.
553 *
554 * @param Document $document
555 */
556 private function save_document_usage( Document $document ) {
557 if ( ! $document::get_property( 'is_editable' ) && ! $document->is_built_with_elementor() ) {
558 return;
559 }
560
561 // Get data manually to avoid conflict with `\Elementor\Core\Base\Document::get_elements_data... convert_to_elementor`.
562 $data = $document->get_json_meta( '_elementor_data' );
563
564 if ( ! empty( $data ) ) {
565 try {
566 $usage = $this->get_elements_usage( $document->get_elements_raw_data( $data ) );
567
568 $document->update_meta( self::META_KEY, $usage );
569
570 $this->add_to_global( $document->get_name(), $usage );
571 } catch ( \Exception $exception ) {
572 return; // Do nothing.
573 };
574 }
575 }
576
577 /**
578 * Add system info report.
579 */
580 public function add_system_info_report() {
581 System_Info::add_report( 'usage', [
582 'file_name' => __DIR__ . '/usage-reporter.php',
583 'class_name' => __NAMESPACE__ . '\Usage_Reporter',
584 ] );
585 }
586
587 /**
588 * Usage module constructor.
589 *
590 * Initializing Elementor usage module.
591 *
592 * @access public
593 */
594 public function __construct() {
595 add_action( 'transition_post_status', [ $this, 'on_status_change' ], 10, 3 );
596 add_action( 'before_delete_post', [ $this, 'on_before_delete_post' ] );
597
598 add_action( 'elementor/document/before_save', [ $this, 'before_document_save' ], 10, 2 );
599 add_action( 'elementor/document/after_save', [ $this, 'after_document_save' ] );
600
601 add_filter( 'elementor/tracker/send_tracking_data_params', [ $this, 'add_tracking_data' ] );
602
603 add_action( 'admin_init', [ $this, 'add_system_info_report' ], 50 );
604 }
605 }
606