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

608 lines 15.4 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 // ' ? 1 : 0;' In sorters is compatibility for PHP8.0.
139 // Sort usage by title.
140 uasort( $usage, function( $a, $b ) {
141 return ( $a['title'] > $b['title'] ) ? 1 : 0;
142 } );
143
144 // If title includes '-' will have lower priority.
145 uasort( $usage, function( $a ) {
146 return strpos( $a['title'], '-' ) ? 1 : 0;
147 } );
148 }
149
150 return $usage;
151 }
152
153 /**
154 * Before document Save.
155 *
156 * Called on elementor/document/before_save, remove document from global & set saving flag.
157 *
158 * @param Document $document
159 * @param array $data new settings to save.
160 */
161 public function before_document_save( $document, $data ) {
162 $current_status = get_post_status( $document->get_post() );
163 $new_status = isset( $data['settings']['post_status'] ) ? $data['settings']['post_status'] : '';
164
165 if ( $current_status === $new_status ) {
166 $this->remove_from_global( $document );
167 }
168
169 $this->is_document_saving = true;
170 }
171
172 /**
173 * After document save.
174 *
175 * Called on elementor/document/after_save, adds document to global & clear saving flag.
176 *
177 * @param Document $document
178 */
179 public function after_document_save( $document ) {
180 if ( Document::STATUS_PUBLISH === $document->get_post()->post_status || Document::STATUS_PRIVATE === $document->get_post()->post_status ) {
181 $this->save_document_usage( $document );
182 }
183
184 $this->is_document_saving = false;
185 }
186
187 /**
188 * On status change.
189 *
190 * Called on transition_post_status.
191 *
192 * @param string $new_status
193 * @param string $old_status
194 * @param \WP_Post $post
195 */
196 public function on_status_change( $new_status, $old_status, $post ) {
197 if ( wp_is_post_autosave( $post ) ) {
198 return;
199 }
200
201 // If it's from elementor editor, the usage should be saved via `before_document_save`/`after_document_save`.
202 if ( $this->is_document_saving ) {
203 return;
204 }
205
206 $document = Plugin::$instance->documents->get( $post->ID );
207
208 if ( ! $document ) {
209 return;
210 }
211
212 $is_public_unpublish = 'publish' === $old_status && 'publish' !== $new_status;
213 $is_private_unpublish = 'private' === $old_status && 'private' !== $new_status;
214
215 if ( $is_public_unpublish || $is_private_unpublish ) {
216 $this->remove_from_global( $document );
217 }
218
219 $is_public_publish = 'publish' !== $old_status && 'publish' === $new_status;
220 $is_private_publish = 'private' !== $old_status && 'private' === $new_status;
221
222 if ( $is_public_publish || $is_private_publish ) {
223 $this->save_document_usage( $document );
224 }
225 }
226
227 /**
228 * On before delete post.
229 *
230 * Called on on_before_delete_post.
231 *
232 * @param int $post_id
233 */
234 public function on_before_delete_post( $post_id ) {
235 $document = Plugin::$instance->documents->get( $post_id );
236
237 if ( $document->get_id() !== $document->get_main_id() ) {
238 return;
239 }
240
241 $this->remove_from_global( $document );
242 }
243
244 /**
245 * Add's tracking data.
246 *
247 * Called on elementor/tracker/send_tracking_data_params.
248 *
249 * @param array $params
250 *
251 * @return array
252 */
253 public function add_tracking_data( $params ) {
254 $params['usages']['elements'] = get_option( self::OPTION_NAME );
255
256 return $params;
257 }
258
259 /**
260 * Recalculate usage.
261 *
262 * Recalculate usage for all elementor posts.
263 *
264 * @param int $limit
265 * @param int $offset
266 *
267 * @return int
268 */
269 public function recalc_usage( $limit = -1, $offset = 0 ) {
270 // While requesting recalc_usage, data should be deleted.
271 // if its in a batch the data should be deleted only on the first batch.
272 if ( 0 === $offset ) {
273 delete_option( self::OPTION_NAME );
274 }
275
276 $post_types = get_post_types( array( 'public' => true ) );
277
278 $query = new \WP_Query( [
279 'no_found_rows' => true,
280 'meta_key' => '_elementor_data',
281 'post_type' => $post_types,
282 'post_status' => [ 'publish', 'private' ],
283 'posts_per_page' => $limit,
284 'offset' => $offset,
285 ] );
286
287 foreach ( $query->posts as $post ) {
288 $document = Plugin::$instance->documents->get( $post->ID );
289
290 if ( ! $document ) {
291 continue;
292 }
293
294 $this->after_document_save( $document );
295 }
296
297 // Clear query memory before leave.
298 wp_cache_flush();
299
300 return count( $query->posts );
301 }
302
303 /**
304 * Increase controls count.
305 *
306 * Increase controls count, for each element.
307 *
308 * @param array &$element_ref
309 * @param string $tab
310 * @param string $section
311 * @param string $control
312 * @param int $count
313 */
314 private function increase_controls_count( &$element_ref, $tab, $section, $control, $count ) {
315 if ( ! isset( $element_ref['controls'][ $tab ] ) ) {
316 $element_ref['controls'][ $tab ] = [];
317 }
318
319 if ( ! isset( $element_ref['controls'][ $tab ][ $section ] ) ) {
320 $element_ref['controls'][ $tab ][ $section ] = [];
321 }
322
323 if ( ! isset( $element_ref['controls'][ $tab ][ $section ][ $control ] ) ) {
324 $element_ref['controls'][ $tab ][ $section ][ $control ] = 0;
325 }
326
327 $element_ref['controls'][ $tab ][ $section ][ $control ] += $count;
328 }
329
330 /**
331 * Add Controls
332 *
333 * Add's controls to this element_ref, returns changed controls count.
334 *
335 * @param array $settings_controls
336 * @param array $element_controls
337 * @param array &$element_ref
338 *
339 * @return int ($changed_controls_count).
340 */
341 private function add_controls( $settings_controls, $element_controls, &$element_ref ) {
342 $changed_controls_count = 0;
343
344 // Loop over all element settings.
345 foreach ( $settings_controls as $control => $value ) {
346 if ( empty( $element_controls[ $control ] ) ) {
347 continue;
348 }
349
350 $control_config = $element_controls[ $control ];
351
352 if ( ! isset( $control_config['section'], $control_config['default'] ) ) {
353 continue;
354 }
355
356 $tab = $control_config['tab'];
357 $section = $control_config['section'];
358
359 // If setting value is not the control default.
360 if ( $value !== $control_config['default'] ) {
361 $this->increase_controls_count( $element_ref, $tab, $section, $control, 1 );
362
363 $changed_controls_count++;
364 }
365 }
366
367 return $changed_controls_count;
368 }
369
370 /**
371 * Add general controls.
372 *
373 * Extract general controls to element ref, return clean `$settings_control`.
374 *
375 * @param array $settings_controls
376 * @param array &$element_ref
377 *
378 * @return array ($settings_controls).
379 */
380 private function add_general_controls( $settings_controls, &$element_ref ) {
381 if ( ! empty( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) ) {
382 $settings_controls = array_merge( $settings_controls, $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] );
383
384 // Add dynamic count to controls under `general` tab.
385 $this->increase_controls_count(
386 $element_ref,
387 self::GENERAL_TAB,
388 Manager::DYNAMIC_SETTING_KEY,
389 'count',
390 count( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] )
391 );
392 }
393
394 return $settings_controls;
395 }
396
397 /**
398 * Add to global.
399 *
400 * Add's usage to global (update database).
401 *
402 * @param string $doc_name
403 * @param array $doc_usage
404 */
405 private function add_to_global( $doc_name, $doc_usage ) {
406 $global_usage = get_option( self::OPTION_NAME, [] );
407
408 foreach ( $doc_usage as $element_type => $element_data ) {
409 if ( ! isset( $global_usage[ $doc_name ] ) ) {
410 $global_usage[ $doc_name ] = [];
411 }
412
413 if ( ! isset( $global_usage[ $doc_name ][ $element_type ] ) ) {
414 $global_usage[ $doc_name ][ $element_type ] = [
415 'count' => 0,
416 'controls' => [],
417 ];
418 }
419
420 $global_element_ref = &$global_usage[ $doc_name ][ $element_type ];
421 $global_element_ref['count'] += $element_data['count'];
422
423 if ( empty( $element_data['controls'] ) ) {
424 continue;
425 }
426
427 foreach ( $element_data['controls'] as $tab => $sections ) {
428 foreach ( $sections as $section => $controls ) {
429 foreach ( $controls as $control => $count ) {
430 $this->increase_controls_count( $global_element_ref, $tab, $section, $control, $count );
431 }
432 }
433 }
434 }
435
436 update_option( self::OPTION_NAME, $global_usage, false );
437 }
438
439 /**
440 * Remove from global.
441 *
442 * Remove's usage from global (update database).
443 *
444 * @param Document $document
445 */
446 private function remove_from_global( $document ) {
447 $prev_usage = $document->get_meta( self::META_KEY );
448
449 if ( empty( $prev_usage ) ) {
450 return;
451 }
452
453 $doc_name = $document->get_name();
454
455 $global_usage = get_option( self::OPTION_NAME, [] );
456
457 foreach ( $prev_usage as $element_type => $doc_value ) {
458 if ( isset( $global_usage[ $doc_name ][ $element_type ]['count'] ) ) {
459 $global_usage[ $doc_name ][ $element_type ]['count'] -= $prev_usage[ $element_type ]['count'];
460
461 if ( 0 === $global_usage[ $doc_name ][ $element_type ]['count'] ) {
462 unset( $global_usage[ $doc_name ][ $element_type ] );
463
464 if ( 0 === count( $global_usage[ $doc_name ] ) ) {
465 unset( $global_usage[ $doc_name ] );
466 }
467
468 continue;
469 }
470
471 foreach ( $prev_usage[ $element_type ]['controls'] as $tab => $sections ) {
472 foreach ( $sections as $section => $controls ) {
473 foreach ( $controls as $control => $count ) {
474 if ( isset( $global_usage[ $doc_name ][ $element_type ]['controls'][ $tab ][ $section ][ $control ] ) ) {
475 $section_ref = &$global_usage[ $doc_name ][ $element_type ]['controls'][ $tab ][ $section ];
476
477 $section_ref[ $control ] -= $count;
478
479 if ( 0 === $section_ref[ $control ] ) {
480 unset( $section_ref[ $control ] );
481 }
482 }
483 }
484 }
485 }
486 }
487 }
488
489 update_option( self::OPTION_NAME, $global_usage, false );
490
491 $document->delete_meta( self::META_KEY );
492 }
493
494 /**
495 * Get elements usage.
496 *
497 * Get's the current elements usage by passed elements array parameter.
498 *
499 * @param array $elements
500 *
501 * @return array
502 */
503 private function get_elements_usage( $elements ) {
504 $usage = [];
505
506 Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage ) {
507 if ( empty( $element['widgetType'] ) ) {
508 $type = $element['elType'];
509 $element_instance = Plugin::$instance->elements_manager->get_element_types( $type );
510 } else {
511 $type = $element['widgetType'];
512 $element_instance = Plugin::$instance->widgets_manager->get_widget_types( $type );
513 }
514
515 if ( ! isset( $usage[ $type ] ) ) {
516 $usage[ $type ] = [
517 'count' => 0,
518 'control_percent' => 0,
519 'controls' => [],
520 ];
521 }
522
523 $usage[ $type ]['count']++;
524
525 if ( ! $element_instance ) {
526 return $element;
527 }
528
529 $element_controls = $element_instance->get_controls();
530
531 if ( isset( $element['settings'] ) ) {
532 $settings_controls = $element['settings'];
533 $element_ref = &$usage[ $type ];
534
535 // Add dynamic values.
536 $settings_controls = $this->add_general_controls( $settings_controls, $element_ref );
537
538 $changed_controls_count = $this->add_controls( $settings_controls, $element_controls, $element_ref );
539
540 $percent = $changed_controls_count / ( count( $element_controls ) / 100 );
541
542 $usage[ $type ] ['control_percent'] = (int) round( $percent );
543 }
544
545 return $element;
546 } );
547
548 return $usage;
549 }
550
551 /**
552 * Save document usage.
553 *
554 * Save requested document usage, and update global.
555 *
556 * @param Document $document
557 */
558 private function save_document_usage( Document $document ) {
559 if ( ! $document::get_property( 'is_editable' ) && ! $document->is_built_with_elementor() ) {
560 return;
561 }
562
563 // Get data manually to avoid conflict with `\Elementor\Core\Base\Document::get_elements_data... convert_to_elementor`.
564 $data = $document->get_json_meta( '_elementor_data' );
565
566 if ( ! empty( $data ) ) {
567 try {
568 $usage = $this->get_elements_usage( $document->get_elements_raw_data( $data ) );
569
570 $document->update_meta( self::META_KEY, $usage );
571
572 $this->add_to_global( $document->get_name(), $usage );
573 } catch ( \Exception $exception ) {
574 return; // Do nothing.
575 };
576 }
577 }
578
579 /**
580 * Add system info report.
581 */
582 public function add_system_info_report() {
583 System_Info::add_report( 'usage', [
584 'file_name' => __DIR__ . '/usage-reporter.php',
585 'class_name' => __NAMESPACE__ . '\Usage_Reporter',
586 ] );
587 }
588
589 /**
590 * Usage module constructor.
591 *
592 * Initializing Elementor usage module.
593 *
594 * @access public
595 */
596 public function __construct() {
597 add_action( 'transition_post_status', [ $this, 'on_status_change' ], 10, 3 );
598 add_action( 'before_delete_post', [ $this, 'on_before_delete_post' ] );
599
600 add_action( 'elementor/document/before_save', [ $this, 'before_document_save' ], 10, 2 );
601 add_action( 'elementor/document/after_save', [ $this, 'after_document_save' ] );
602
603 add_filter( 'elementor/tracker/send_tracking_data_params', [ $this, 'add_tracking_data' ] );
604
605 add_action( 'admin_init', [ $this, 'add_system_info_report' ], 50 );
606 }
607 }
608