PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta2
Elementor Website Builder – more than just a page builder v4.3.0-beta2
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 / system-info / module.php

module.php in Elementor Website Builder – more than just a page builder 4.3.0-beta2, at modules/system-info/module.php

388 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\System_Info;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider;
6 use Elementor\Modules\System_Info\AdminMenuItems\Editor_One_System_Info_Menu;
7 use Elementor\Modules\System_Info\AdminMenuItems\Editor_One_System_Menu;
8 use Elementor\Modules\System_Info\Helpers\Model_Helper;
9 use Elementor\Modules\System_Info\Reporters\Base;
10 use Elementor\Modules\System_Info\Rest\Rest_Api;
11 use Elementor\Plugin;
12 use Elementor\Settings;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 /**
19 * Elementor system info module.
20 *
21 * Elementor system info module handler class is responsible for registering and
22 * managing Elementor system info reports.
23 *
24 * @since 2.9.0
25 */
26 class Module extends BaseModule {
27
28 /**
29 * Get module name.
30 *
31 * Retrieve the system info module name.
32 *
33 * @since 2.9.0
34 * @access public
35 *
36 * @return string Module name.
37 */
38 public function get_name() {
39 return 'system-info';
40 }
41
42 /**
43 * Required user capabilities.
44 *
45 * Holds the user capabilities required to manage Elementor menus.
46 *
47 * @since 2.9.0
48 * @access private
49 *
50 * @var string
51 */
52 private $capability = 'manage_options';
53
54 /**
55 * Elementor system info reports.
56 *
57 * Holds an array of available reports in Elementor system info page.
58 *
59 * @since 2.9.0
60 * @access private
61 *
62 * @var array
63 */
64 private static $reports = [
65 'server' => [],
66 'wordpress' => [],
67 'theme' => [],
68 'user' => [],
69 'plugins' => [],
70 'network_plugins' => [],
71 'mu_plugins' => [],
72 ];
73
74 public function get_capability() {
75 return $this->capability;
76 }
77
78 /**
79 * Main system info page constructor.
80 *
81 * Initializing Elementor system info page.
82 *
83 * @since 2.9.0
84 * @access public
85 */
86 public function __construct() {
87 $this->add_actions();
88 }
89
90 /**
91 * Get default settings.
92 *
93 * Retrieve the default settings. Used to reset the report settings on
94 * initialization.
95 *
96 * @since 2.9.0
97 * @access protected
98 *
99 * @return array Default settings.
100 */
101 protected function get_init_settings() {
102 $settings = [];
103
104 $reporter_properties = Base::get_properties_keys();
105
106 array_push( $reporter_properties, 'category', 'name', 'class_name' );
107
108 $settings['reporter_properties'] = $reporter_properties;
109
110 $settings['reportFilePrefix'] = '';
111
112 return $settings;
113 }
114
115 /**
116 * Add actions.
117 *
118 * Register filters and actions for the main system info page.
119 *
120 * @since 2.9.0
121 * @access private
122 */
123 private function add_actions() {
124 add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
125 $this->register_editor_one_menu( $menu_data_provider );
126 } );
127
128 add_action( 'wp_ajax_elementor_system_info_download_file', [ $this, 'download_file' ] );
129 add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
130 }
131
132 public function register_rest_routes(): void {
133 ( new Rest_Api() )->register_routes();
134 }
135
136 public function get_reports_data(): array {
137 $reports = $this->load_reports( self::get_allowed_reports() );
138
139 return $this->build_reports_data( $reports );
140 }
141
142 private function build_reports_data( array $reports ): array {
143 $data = [];
144
145 foreach ( $reports as $report_name => $report_details ) {
146 $report = $report_details['report']->get_report();
147
148 if ( is_wp_error( $report ) ) {
149 continue;
150 }
151
152 $report_data = [
153 'label' => $report_details['label'],
154 'report' => $report,
155 ];
156
157 if ( ! empty( $report_details['sub'] ) ) {
158 $report_data['sub'] = $this->build_reports_data( $report_details['sub'] );
159 }
160
161 $data[ $report_name ] = $report_data;
162 }
163
164 return $data;
165 }
166
167 private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) {
168 $menu_data_provider->register_menu( new Editor_One_System_Menu() );
169 $menu_data_provider->register_menu( new Editor_One_System_Info_Menu() );
170 }
171
172 /**
173 * Display page.
174 *
175 * Output the content for the main system info page.
176 *
177 * @since 2.9.0
178 * @access public
179 */
180 public function display_page() {
181 $reports_info = self::get_allowed_reports();
182
183 $reports = $this->load_reports( $reports_info );
184 ?>
185 <div id="elementor-system-info">
186 <div class="elementor-system-info-header">
187 <h3 class="wp-heading-inline"><?php echo esc_html__( 'System Info', 'elementor' ); ?></h3>
188 <form action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post">
189 <input type="hidden" name="action" value="elementor_system_info_download_file">
190 <input type="submit" data-id="elementor-system-info-download-file" class="button button-primary" value="<?php echo esc_attr__( 'Download System Info', 'elementor' ); ?>">
191 </form>
192 </div>
193 <div><?php $this->print_report( $reports, 'html' ); ?></div>
194 <h3><?php echo esc_html__( 'Copy & Paste Info', 'elementor' ); ?></h3>
195 <div id="elementor-system-info-raw">
196 <label id="elementor-system-info-raw-code-label" for="elementor-system-info-raw-code"><?php echo esc_html__( 'You can copy the below info as simple text with Ctrl+C / Ctrl+V:', 'elementor' ); ?></label>
197 <textarea id="elementor-system-info-raw-code" readonly>
198 <?php
199 $this->print_report( $reports, 'raw' );
200 ?>
201 </textarea>
202 <script>
203 var textarea = document.getElementById( 'elementor-system-info-raw-code' );
204 var selectRange = function() {
205 textarea.setSelectionRange( 0, textarea.value.length );
206 };
207 textarea.onfocus = textarea.onblur = textarea.onclick = selectRange;
208 textarea.onfocus();
209 </script>
210 </div>
211 <hr>
212 <form action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post">
213 <input type="hidden" name="action" value="elementor_system_info_download_file">
214 <input type="submit" data-id="elementor-system-info-download-file" class="button button-primary" value="<?php echo esc_attr__( 'Download System Info', 'elementor' ); ?>">
215 </form>
216 </div>
217 <?php
218 }
219
220 /**
221 * Download file.
222 *
223 * Download the reports files.
224 *
225 * Fired by `wp_ajax_elementor_system_info_download_file` action.
226 *
227 * @since 2.9.0
228 * @access public
229 */
230 public function download_file() {
231 if ( ! current_user_can( $this->capability ) ) {
232 wp_die( esc_html__( 'You do not have permission to download this file.', 'elementor' ) );
233 }
234
235 $reports_info = self::get_allowed_reports();
236 $reports = $this->load_reports( $reports_info );
237
238 $domain = parse_url( site_url(), PHP_URL_HOST );
239
240 header( 'Content-Type: text/plain' );
241 header( 'Content-Disposition:attachment; filename=system-info-' . $domain . '-' . gmdate( 'd-m-Y' ) . '.txt' );
242
243 $this->print_report( $reports );
244
245 die;
246 }
247
248 /**
249 * Get report class.
250 *
251 * Retrieve the class of the report for any given report type.
252 *
253 * @since 2.9.0
254 * @access public
255 *
256 * @param string $reporter_type The type of the report.
257 *
258 * @return string The class of the report.
259 */
260 public function get_reporter_class( $reporter_type ) {
261 return __NAMESPACE__ . '\Reporters\\' . ucfirst( $reporter_type );
262 }
263
264 /**
265 * Load reports.
266 *
267 * Retrieve the system info reports.
268 *
269 * @since 2.9.0
270 * @access public
271 *
272 * @param array $reports An array of system info reports.
273 *
274 * @return array An array of system info reports.
275 */
276 public function load_reports( $reports ) {
277 $result = [];
278
279 foreach ( $reports as $report_name => $report_info ) {
280 $reporter_params = [
281 'name' => $report_name,
282 ];
283
284 $reporter_params = array_merge( $reporter_params, $report_info );
285
286 $reporter = $this->create_reporter( $reporter_params );
287
288 if ( ! $reporter instanceof Base ) {
289 continue;
290 }
291
292 $result[ $report_name ] = [
293 'report' => $reporter,
294 'label' => $reporter->get_title(),
295 ];
296
297 if ( ! empty( $report_info['sub'] ) ) {
298 $result[ $report_name ]['sub'] = $this->load_reports( $report_info['sub'] );
299 }
300 }
301
302 return $result;
303 }
304
305 /**
306 * Create a report.
307 *
308 * Register a new report that will be displayed in Elementor system info page.
309 *
310 * @param array $properties Report properties.
311 *
312 * @return \WP_Error|false|Base Base instance if the report was created,
313 * False or WP_Error otherwise.
314 * @since 2.9.0
315 * @access public
316 */
317 public function create_reporter( array $properties ) {
318 $properties = Model_Helper::prepare_properties( $this->get_settings( 'reporter_properties' ), $properties );
319
320 $reporter_class = $properties['class_name'] ? $properties['class_name'] : $this->get_reporter_class( $properties['name'] );
321
322 $reporter = new $reporter_class( $properties );
323
324 if ( ! ( $reporter instanceof Base ) ) {
325 return new \WP_Error( 'Each reporter must to be an instance or sub-instance of `Base` class.' );
326 }
327
328 if ( ! $reporter->is_enabled() ) {
329 return false;
330 }
331
332 return $reporter;
333 }
334
335 /**
336 * Print report.
337 *
338 * Output the system info page reports using an output template.
339 *
340 * @since 2.9.0
341 * @access public
342 *
343 * @param array $reports An array of system info reports.
344 * @param string $template Output type from the templates folder. Available
345 * templates are `raw` and `html`. Default is `raw`.
346 */
347 public function print_report( $reports, $template = 'raw' ) {
348 static $tabs_count = 0;
349
350 $template_path = __DIR__ . '/templates/' . $template . '.php';
351
352 require $template_path;
353 }
354
355 /**
356 * Get allowed reports.
357 *
358 * Retrieve the available reports in Elementor system info page.
359 *
360 * @since 2.9.0
361 * @access public
362 * @static
363 *
364 * @return array Available reports in Elementor system info page.
365 */
366 public static function get_allowed_reports() {
367 do_action( 'elementor/system_info/get_allowed_reports' );
368
369 return self::$reports;
370 }
371
372 /**
373 * Add report.
374 *
375 * Register a new report to Elementor system info page.
376 *
377 * @since 2.9.0
378 * @access public
379 * @static
380 *
381 * @param string $report_name The name of the report.
382 * @param array $report_info Report info.
383 */
384 public static function add_report( $report_name, $report_info ) {
385 self::$reports[ $report_name ] = $report_info;
386 }
387 }
388