PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.8
Elementor Website Builder – more than just a page builder v3.25.8
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 / usage-reporter.php

usage-reporter.php in Elementor Website Builder – more than just a page builder 3.25.8, at modules/usage/usage-reporter.php

127 lines 3.0 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\Modules\System_Info\Reporters\Base;
5 use Elementor\Utils;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Elementor usage report.
13 *
14 * Elementor system report handler class responsible for generating a report for
15 * the user.
16 */
17 class Usage_Reporter extends Base {
18
19 const RECALC_ACTION = 'elementor_usage_recalc';
20
21 public function get_title() {
22 return esc_html__( 'Elements Usage', 'elementor' );
23 }
24
25 public function get_fields() {
26 return [
27 'usage' => '',
28 ];
29 }
30
31 public function print_html_label( $label ) {
32 $title = $this->get_title();
33
34 if ( empty( $_GET[ self::RECALC_ACTION ] ) ) { // phpcs:ignore -- nonce validation is not required here.
35 $nonce = wp_create_nonce( self::RECALC_ACTION );
36 $url = add_query_arg( [
37 self::RECALC_ACTION => 1,
38 '_wpnonce' => $nonce,
39 ] );
40
41 $title .= '<a id="elementor-usage-recalc" href="' . esc_url( $url ) . '#elementor-usage-recalc" class="box-title-tool">Recalculate</a>';
42 } else {
43 $title .= $this->get_remove_recalc_query_string_script();
44 }
45
46 parent::print_html_label( $title );
47 }
48
49 public function get_usage() {
50 /** @var Module $module */
51 $module = Module::instance();
52
53 if ( ! empty( $_GET[ self::RECALC_ACTION ] ) ) {
54 // phpcs:ignore
55 $nonce = Utils::get_super_global_value( $_GET, '_wpnonce' );
56
57 if ( ! wp_verify_nonce( $nonce, self::RECALC_ACTION ) ) {
58 wp_die( 'Invalid Nonce', 'Invalid Nonce', [
59 'back_link' => true,
60 ] );
61 }
62
63 $module->recalc_usage();
64 }
65
66 $usage = '';
67
68 foreach ( $module->get_formatted_usage() as $doc_type => $data ) {
69 $usage .= '<tr><td>' . $data['title'] . ' ( ' . $data['count'] . ' )</td><td>';
70
71 foreach ( $data['elements'] as $element => $count ) {
72 $usage .= $element . ': ' . $count . PHP_EOL;
73 }
74
75 $usage .= '</td></tr>';
76 }
77
78 return [
79 'value' => $usage,
80 ];
81 }
82
83 public function get_raw_usage() {
84 /** @var Module $module */
85 $module = Module::instance();
86 $usage = PHP_EOL;
87
88 foreach ( $module->get_formatted_usage( 'raw' ) as $doc_type => $data ) {
89 $usage .= "\t{$data['title']} : " . $data['count'] . PHP_EOL;
90
91 foreach ( $data['elements'] as $element => $count ) {
92 $usage .= "\t\t{$element} : {$count}" . PHP_EOL;
93 }
94 }
95
96 return [
97 'value' => $usage,
98 ];
99 }
100
101 /**
102 * Removes the "elementor_usage_recalc" param from the query string to avoid recalc every refresh.
103 * When using a redirect header in place of this approach it throws an error because some components have already output some content.
104 *
105 * @return string
106 */
107 private function get_remove_recalc_query_string_script() {
108 ob_start();
109 ?>
110 <script>
111 // Origin file: modules/usage/usage-reporter.php - get_remove_recalc_query_string_script()
112 {
113 const url = new URL( window.location );
114
115 url.hash = '';
116 url.searchParams.delete( 'elementor_usage_recalc' );
117 url.searchParams.delete( '_wpnonce' );
118
119 history.replaceState( '', window.title, url.toString() );
120 }
121 </script>
122 <?php
123
124 return ob_get_clean();
125 }
126 }
127