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 / trunk / modules / system-info / module.php

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

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