PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/elementor/modules/system-info/module.php

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