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

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