| 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\Modules\EditorOne\Classes\Menu_Data_Provider; |
| 8 |
use Elementor\Modules\System_Info\AdminMenuItems\Editor_One_System_Info_Menu; |
| 9 |
use Elementor\Modules\System_Info\AdminMenuItems\Editor_One_System_Menu; |
| 10 |
use Elementor\Plugin; |
| 11 |
use Elementor\Settings; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Elementor system info module. |
| 19 |
* |
| 20 |
* Elementor system info module handler class is responsible for registering and |
| 21 |
* managing Elementor system info reports. |
| 22 |
* |
| 23 |
* @since 2.9.0 |
| 24 |
*/ |
| 25 |
class Module extends BaseModule { |
| 26 |
|
| 27 |
/** |
| 28 |
* Get module name. |
| 29 |
* |
| 30 |
* Retrieve the system info module name. |
| 31 |
* |
| 32 |
* @since 2.9.0 |
| 33 |
* @access public |
| 34 |
* |
| 35 |
* @return string Module name. |
| 36 |
*/ |
| 37 |
public function get_name() { |
| 38 |
return 'system-info'; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Required user capabilities. |
| 43 |
* |
| 44 |
* Holds the user capabilities required to manage Elementor menus. |
| 45 |
* |
| 46 |
* @since 2.9.0 |
| 47 |
* @access private |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
private $capability = 'manage_options'; |
| 52 |
|
| 53 |
/** |
| 54 |
* Elementor system info reports. |
| 55 |
* |
| 56 |
* Holds an array of available reports in Elementor system info page. |
| 57 |
* |
| 58 |
* @since 2.9.0 |
| 59 |
* @access private |
| 60 |
* |
| 61 |
* @var array |
| 62 |
*/ |
| 63 |
private static $reports = [ |
| 64 |
'server' => [], |
| 65 |
'wordpress' => [], |
| 66 |
'theme' => [], |
| 67 |
'user' => [], |
| 68 |
'plugins' => [], |
| 69 |
'network_plugins' => [], |
| 70 |
'mu_plugins' => [], |
| 71 |
]; |
| 72 |
|
| 73 |
public function get_capability() { |
| 74 |
return $this->capability; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Main system info page constructor. |
| 79 |
* |
| 80 |
* Initializing Elementor system info page. |
| 81 |
* |
| 82 |
* @since 2.9.0 |
| 83 |
* @access public |
| 84 |
*/ |
| 85 |
public function __construct() { |
| 86 |
$this->add_actions(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get default settings. |
| 91 |
* |
| 92 |
* Retrieve the default settings. Used to reset the report settings on |
| 93 |
* initialization. |
| 94 |
* |
| 95 |
* @since 2.9.0 |
| 96 |
* @access protected |
| 97 |
* |
| 98 |
* @return array Default settings. |
| 99 |
*/ |
| 100 |
protected function get_init_settings() { |
| 101 |
$settings = []; |
| 102 |
|
| 103 |
$reporter_properties = Base::get_properties_keys(); |
| 104 |
|
| 105 |
array_push( $reporter_properties, 'category', 'name', 'class_name' ); |
| 106 |
|
| 107 |
$settings['reporter_properties'] = $reporter_properties; |
| 108 |
|
| 109 |
$settings['reportFilePrefix'] = ''; |
| 110 |
|
| 111 |
return $settings; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Add actions. |
| 116 |
* |
| 117 |
* Register filters and actions for the main system info page. |
| 118 |
* |
| 119 |
* @since 2.9.0 |
| 120 |
* @access private |
| 121 |
*/ |
| 122 |
private function add_actions() { |
| 123 |
add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) { |
| 124 |
$this->register_editor_one_menu( $menu_data_provider ); |
| 125 |
} ); |
| 126 |
|
| 127 |
add_action( 'wp_ajax_elementor_system_info_download_file', [ $this, 'download_file' ] ); |
| 128 |
} |
| 129 |
|
| 130 |
private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) { |
| 131 |
$menu_data_provider->register_menu( new Editor_One_System_Menu() ); |
| 132 |
$menu_data_provider->register_menu( new Editor_One_System_Info_Menu() ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Display page. |
| 137 |
* |
| 138 |
* Output the content for the main system info page. |
| 139 |
* |
| 140 |
* @since 2.9.0 |
| 141 |
* @access public |
| 142 |
*/ |
| 143 |
public function display_page() { |
| 144 |
$reports_info = self::get_allowed_reports(); |
| 145 |
|
| 146 |
$reports = $this->load_reports( $reports_info ); |
| 147 |
?> |
| 148 |
<div id="elementor-system-info"> |
| 149 |
<div class="elementor-system-info-header"> |
| 150 |
<h3 class="wp-heading-inline"><?php echo esc_html__( 'System Info', 'elementor' ); ?></h3> |
| 151 |
<form action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post"> |
| 152 |
<input type="hidden" name="action" value="elementor_system_info_download_file"> |
| 153 |
<input type="submit" data-id="elementor-system-info-download-file" class="button button-primary" value="<?php echo esc_attr__( 'Download System Info', 'elementor' ); ?>"> |
| 154 |
</form> |
| 155 |
</div> |
| 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" data-id="elementor-system-info-download-file" class="button button-primary" value="<?php echo esc_attr__( '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 do not have permission 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 |
public function create_reporter( array $properties ) { |
| 281 |
$properties = Model_Helper::prepare_properties( $this->get_settings( 'reporter_properties' ), $properties ); |
| 282 |
|
| 283 |
$reporter_class = $properties['class_name'] ? $properties['class_name'] : $this->get_reporter_class( $properties['name'] ); |
| 284 |
|
| 285 |
$reporter = new $reporter_class( $properties ); |
| 286 |
|
| 287 |
if ( ! ( $reporter instanceof Base ) ) { |
| 288 |
return new \WP_Error( 'Each reporter must to be an instance or sub-instance of `Base` class.' ); |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! $reporter->is_enabled() ) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
return $reporter; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Print report. |
| 300 |
* |
| 301 |
* Output the system info page reports using an output template. |
| 302 |
* |
| 303 |
* @since 2.9.0 |
| 304 |
* @access public |
| 305 |
* |
| 306 |
* @param array $reports An array of system info reports. |
| 307 |
* @param string $template Output type from the templates folder. Available |
| 308 |
* templates are `raw` and `html`. Default is `raw`. |
| 309 |
*/ |
| 310 |
public function print_report( $reports, $template = 'raw' ) { |
| 311 |
static $tabs_count = 0; |
| 312 |
|
| 313 |
$template_path = __DIR__ . '/templates/' . $template . '.php'; |
| 314 |
|
| 315 |
require $template_path; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Get allowed reports. |
| 320 |
* |
| 321 |
* Retrieve the available reports in Elementor system info page. |
| 322 |
* |
| 323 |
* @since 2.9.0 |
| 324 |
* @access public |
| 325 |
* @static |
| 326 |
* |
| 327 |
* @return array Available reports in Elementor system info page. |
| 328 |
*/ |
| 329 |
public static function get_allowed_reports() { |
| 330 |
do_action( 'elementor/system_info/get_allowed_reports' ); |
| 331 |
|
| 332 |
return self::$reports; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Add report. |
| 337 |
* |
| 338 |
* Register a new report to Elementor system info page. |
| 339 |
* |
| 340 |
* @since 2.9.0 |
| 341 |
* @access public |
| 342 |
* @static |
| 343 |
* |
| 344 |
* @param string $report_name The name of the report. |
| 345 |
* @param array $report_info Report info. |
| 346 |
*/ |
| 347 |
public static function add_report( $report_name, $report_info ) { |
| 348 |
self::$reports[ $report_name ] = $report_info; |
| 349 |
} |
| 350 |
} |
| 351 |
|