PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.0-dev2
Elementor Website Builder – more than just a page builder v3.20.0-dev2
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.20.0-dev2, at modules/usage/module.php

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