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

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

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