balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

module.php (8827B)


      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 	 * Get module name.
     23 	 *
     24 	 * Retrieve the system info module name.
     25 	 *
     26 	 * @since 2.9.0
     27 	 * @access public
     28 	 *
     29 	 * @return string Module name.
     30 	 */
     31 	public function get_name() {
     32 		return 'system-info';
     33 	}
     34 
     35 	/**
     36 	 * Required user capabilities.
     37 	 *
     38 	 * Holds the user capabilities required to manage Elementor menus.
     39 	 *
     40 	 * @since 2.9.0
     41 	 * @access private
     42 	 *
     43 	 * @var string
     44 	 */
     45 	private $capability = 'manage_options';
     46 
     47 	/**
     48 	 * Elementor system info reports.
     49 	 *
     50 	 * Holds an array of available reports in Elementor system info page.
     51 	 *
     52 	 * @since 2.9.0
     53 	 * @access private
     54 	 *
     55 	 * @var array
     56 	 */
     57 	private static $reports = [
     58 		'server' => [],
     59 		'wordpress' => [],
     60 		'theme' => [],
     61 		'user' => [],
     62 		'plugins' => [],
     63 		'network_plugins' => [],
     64 		'mu_plugins' => [],
     65 	];
     66 
     67 	/**
     68 	 * Main system info page constructor.
     69 	 *
     70 	 * Initializing Elementor system info page.
     71 	 *
     72 	 * @since 2.9.0
     73 	 * @access public
     74 	 */
     75 	public function __construct() {
     76 		$this->add_actions();
     77 	}
     78 
     79 	/**
     80 	 * Get default settings.
     81 	 *
     82 	 * Retrieve the default settings. Used to reset the report settings on
     83 	 * initialization.
     84 	 *
     85 	 * @since 2.9.0
     86 	 * @access protected
     87 	 *
     88 	 * @return array Default settings.
     89 	 */
     90 	protected function get_init_settings() {
     91 		$settings = [];
     92 
     93 		$reporter_properties = Base::get_properties_keys();
     94 
     95 		array_push( $reporter_properties, 'category', 'name', 'class_name' );
     96 
     97 		$settings['reporter_properties'] = $reporter_properties;
     98 
     99 		$settings['reportFilePrefix'] = '';
    100 
    101 		return $settings;
    102 	}
    103 
    104 	/**
    105 	 * Add actions.
    106 	 *
    107 	 * Register filters and actions for the main system info page.
    108 	 *
    109 	 * @since 2.9.0
    110 	 * @access private
    111 	 */
    112 	private function add_actions() {
    113 		add_action( 'admin_menu', [ $this, 'register_menu' ], 500 );
    114 		add_action( 'wp_ajax_elementor_system_info_download_file', [ $this, 'download_file' ] );
    115 	}
    116 
    117 	/**
    118 	 * Register admin menu.
    119 	 *
    120 	 * Add new Elementor system info admin menu.
    121 	 *
    122 	 * Fired by `admin_menu` action.
    123 	 *
    124 	 * @since 2.9.0
    125 	 * @access public
    126 	 */
    127 	public function register_menu() {
    128 		$system_info_text = esc_html__( 'System Info', 'elementor' );
    129 
    130 		add_submenu_page(
    131 			'elementor',
    132 			$system_info_text,
    133 			$system_info_text,
    134 			$this->capability,
    135 			'elementor-system-info',
    136 			[ $this, 'display_page' ]
    137 		);
    138 	}
    139 
    140 	/**
    141 	 * Display page.
    142 	 *
    143 	 * Output the content for the main system info page.
    144 	 *
    145 	 * @since 2.9.0
    146 	 * @access public
    147 	 */
    148 	public function display_page() {
    149 		$reports_info = self::get_allowed_reports();
    150 
    151 		$reports = $this->load_reports( $reports_info, 'html' );
    152 
    153 		$raw_reports = $this->load_reports( $reports_info, 'raw' );
    154 
    155 		?>
    156 		<div id="elementor-system-info">
    157 			<h3 class="wp-heading-inline"><?php echo esc_html__( 'System Info', 'elementor' ); ?></h3>
    158 			<div><?php $this->print_report( $reports, 'html' ); ?></div>
    159 			<h3><?php echo esc_html__( 'Copy & Paste Info', 'elementor' ); ?></h3>
    160 			<div id="elementor-system-info-raw">
    161 				<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>
    162 				<textarea id="elementor-system-info-raw-code" readonly>
    163 					<?php
    164 						unset( $raw_reports['wordpress']['report']['admin_email'] );
    165 
    166 						$this->print_report( $raw_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, 'raw' );
    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 	 * @param string $format - possible values: 'raw' or empty string, meaning 'html'
    241 	 *
    242 	 * @return array An array of system info reports.
    243 	 */
    244 	public function load_reports( $reports, $format = '' ) {
    245 		$result = [];
    246 
    247 		foreach ( $reports as $report_name => $report_info ) {
    248 			$reporter_params = [
    249 				'name' => $report_name,
    250 				'format' => $format,
    251 			];
    252 
    253 			$reporter_params = array_merge( $reporter_params, $report_info );
    254 
    255 			$reporter = $this->create_reporter( $reporter_params );
    256 
    257 			if ( ! $reporter instanceof Base ) {
    258 				continue;
    259 			}
    260 
    261 			$result[ $report_name ] = [
    262 				'report' => $reporter->get_report( $format ),
    263 				'label' => $reporter->get_title(),
    264 			];
    265 
    266 			if ( ! empty( $report_info['sub'] ) ) {
    267 				$result[ $report_name ]['sub'] = $this->load_reports( $report_info['sub'] );
    268 			}
    269 		}
    270 
    271 		return $result;
    272 	}
    273 
    274 	/**
    275 	 * Create a report.
    276 	 *
    277 	 * Register a new report that will be displayed in Elementor system info page.
    278 	 *
    279 	 * @param array $properties Report properties.
    280 	 *
    281 	 * @return \WP_Error|false|Base Base instance if the report was created,
    282 	 *                                       False or WP_Error otherwise.
    283 	 *@since 2.9.0
    284 	 * @access public
    285 	 *
    286 	 */
    287 	public function create_reporter( array $properties ) {
    288 		$properties = Model_Helper::prepare_properties( $this->get_settings( 'reporter_properties' ), $properties );
    289 
    290 		$reporter_class = $properties['class_name'] ? $properties['class_name'] : $this->get_reporter_class( $properties['name'] );
    291 
    292 		$reporter = new $reporter_class( $properties );
    293 
    294 		if ( ! ( $reporter instanceof Base ) ) {
    295 			return new \WP_Error( 'Each reporter must to be an instance or sub-instance of `Base` class.' );
    296 		}
    297 
    298 		if ( ! $reporter->is_enabled() ) {
    299 			return false;
    300 		}
    301 
    302 		return $reporter;
    303 	}
    304 
    305 	/**
    306 	 * Print report.
    307 	 *
    308 	 * Output the system info page reports using an output template.
    309 	 *
    310 	 * @since 2.9.0
    311 	 * @access public
    312 	 *
    313 	 * @param array  $reports  An array of system info reports.
    314 	 * @param string $template Output type from the templates folder. Available
    315 	 *                         templates are `raw` and `html`. Default is `raw`.
    316 	 */
    317 	public function print_report( $reports, $template = 'raw' ) {
    318 		static $tabs_count = 0;
    319 
    320 		static $required_plugins_properties = [
    321 			'Name',
    322 			'Version',
    323 			'URL',
    324 			'Author',
    325 		];
    326 
    327 		$template_path = __DIR__ . '/templates/' . $template . '.php';
    328 
    329 		require $template_path;
    330 	}
    331 
    332 	/**
    333 	 * Get allowed reports.
    334 	 *
    335 	 * Retrieve the available reports in Elementor system info page.
    336 	 *
    337 	 * @since 2.9.0
    338 	 * @access public
    339 	 * @static
    340 	 *
    341 	 * @return array Available reports in Elementor system info page.
    342 	 */
    343 	public static function get_allowed_reports() {
    344 		return self::$reports;
    345 	}
    346 
    347 	/**
    348 	 * Add report.
    349 	 *
    350 	 * Register a new report to Elementor system info page.
    351 	 *
    352 	 * @since 2.9.0
    353 	 * @access public
    354 	 * @static
    355 	 *
    356 	 * @param string $report_name The name of the report.
    357 	 * @param array  $report_info Report info.
    358 	 */
    359 	public static function add_report( $report_name, $report_info ) {
    360 		self::$reports[ $report_name ] = $report_info;
    361 	}
    362 }