Sin descripción

class-wc-admin-status.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * Debug/Status page
  4. *
  5. * @package WooCommerce\Admin\System Status
  6. * @version 2.2.0
  7. */
  8. use Automattic\Jetpack\Constants;
  9. use Automattic\WooCommerce\Utilities\ArrayUtil;
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * WC_Admin_Status Class.
  13. */
  14. class WC_Admin_Status {
  15. /**
  16. * Handles output of the reports page in admin.
  17. */
  18. public static function output() {
  19. include_once __DIR__ . '/views/html-admin-page-status.php';
  20. }
  21. /**
  22. * Handles output of report.
  23. */
  24. public static function status_report() {
  25. include_once __DIR__ . '/views/html-admin-page-status-report.php';
  26. }
  27. /**
  28. * Handles output of tools.
  29. */
  30. public static function status_tools() {
  31. if ( ! class_exists( 'WC_REST_System_Status_Tools_Controller' ) ) {
  32. wp_die( 'Cannot load the REST API to access WC_REST_System_Status_Tools_Controller.' );
  33. }
  34. $tools = self::get_tools();
  35. $tool_requires_refresh = false;
  36. if ( ! empty( $_GET['action'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'debug_action' ) ) { // WPCS: input var ok, sanitization ok.
  37. $tools_controller = new WC_REST_System_Status_Tools_Controller();
  38. $action = wc_clean( wp_unslash( $_GET['action'] ) ); // WPCS: input var ok.
  39. if ( array_key_exists( $action, $tools ) ) {
  40. $response = $tools_controller->execute_tool( $action );
  41. $tool = $tools[ $action ];
  42. $tool_requires_refresh = ArrayUtil::get_value_or_default( $tool, 'requires_refresh', false );
  43. $tool = array(
  44. 'id' => $action,
  45. 'name' => $tool['name'],
  46. 'action' => $tool['button'],
  47. 'description' => $tool['desc'],
  48. 'disabled' => ArrayUtil::get_value_or_default( $tool, 'disabled', false ),
  49. );
  50. $tool = array_merge( $tool, $response );
  51. /**
  52. * Fires after a WooCommerce system status tool has been executed.
  53. *
  54. * @param array $tool Details about the tool that has been executed.
  55. */
  56. do_action( 'woocommerce_system_status_tool_executed', $tool );
  57. } else {
  58. $response = array(
  59. 'success' => false,
  60. 'message' => __( 'Tool does not exist.', 'woocommerce' ),
  61. );
  62. }
  63. if ( $response['success'] ) {
  64. echo '<div class="updated inline"><p>' . esc_html( $response['message'] ) . '</p></div>';
  65. } else {
  66. echo '<div class="error inline"><p>' . esc_html( $response['message'] ) . '</p></div>';
  67. }
  68. }
  69. // Display message if settings settings have been saved.
  70. if ( isset( $_REQUEST['settings-updated'] ) ) { // WPCS: input var ok.
  71. echo '<div class="updated inline"><p>' . esc_html__( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>';
  72. }
  73. if ( $tool_requires_refresh ) {
  74. $tools = self::get_tools();
  75. }
  76. include_once __DIR__ . '/views/html-admin-page-status-tools.php';
  77. }
  78. /**
  79. * Get tools.
  80. *
  81. * @return array of tools
  82. */
  83. public static function get_tools() {
  84. $tools_controller = new WC_REST_System_Status_Tools_Controller();
  85. return $tools_controller->get_tools();
  86. }
  87. /**
  88. * Show the logs page.
  89. */
  90. public static function status_logs() {
  91. $log_handler = Constants::get_constant( 'WC_LOG_HANDLER' );
  92. if ( 'WC_Log_Handler_DB' === $log_handler ) {
  93. self::status_logs_db();
  94. } else {
  95. self::status_logs_file();
  96. }
  97. }
  98. /**
  99. * Show the log page contents for file log handler.
  100. */
  101. public static function status_logs_file() {
  102. $logs = self::scan_log_files();
  103. if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ] ) ) { // WPCS: input var ok, CSRF ok.
  104. $viewed_log = $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ]; // WPCS: input var ok, CSRF ok.
  105. } elseif ( ! empty( $logs ) ) {
  106. $viewed_log = current( $logs );
  107. }
  108. $handle = ! empty( $viewed_log ) ? self::get_log_file_handle( $viewed_log ) : '';
  109. if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok, CSRF ok.
  110. self::remove_log();
  111. }
  112. include_once __DIR__ . '/views/html-admin-page-status-logs.php';
  113. }
  114. /**
  115. * Show the log page contents for db log handler.
  116. */
  117. public static function status_logs_db() {
  118. if ( ! empty( $_REQUEST['flush-logs'] ) ) { // WPCS: input var ok, CSRF ok.
  119. self::flush_db_logs();
  120. }
  121. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['log'] ) ) { // WPCS: input var ok, CSRF ok.
  122. self::log_table_bulk_actions();
  123. }
  124. $log_table_list = new WC_Admin_Log_Table_List();
  125. $log_table_list->prepare_items();
  126. include_once __DIR__ . '/views/html-admin-page-status-logs-db.php';
  127. }
  128. /**
  129. * Retrieve metadata from a file. Based on WP Core's get_file_data function.
  130. *
  131. * @since 2.1.1
  132. * @param string $file Path to the file.
  133. * @return string
  134. */
  135. public static function get_file_version( $file ) {
  136. // Avoid notices if file does not exist.
  137. if ( ! file_exists( $file ) ) {
  138. return '';
  139. }
  140. // We don't need to write to the file, so just open for reading.
  141. $fp = fopen( $file, 'r' ); // @codingStandardsIgnoreLine.
  142. // Pull only the first 8kiB of the file in.
  143. $file_data = fread( $fp, 8192 ); // @codingStandardsIgnoreLine.
  144. // PHP will close file handle, but we are good citizens.
  145. fclose( $fp ); // @codingStandardsIgnoreLine.
  146. // Make sure we catch CR-only line endings.
  147. $file_data = str_replace( "\r", "\n", $file_data );
  148. $version = '';
  149. if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) {
  150. $version = _cleanup_header_comment( $match[1] );
  151. }
  152. return $version;
  153. }
  154. /**
  155. * Return the log file handle.
  156. *
  157. * @param string $filename Filename to get the handle for.
  158. * @return string
  159. */
  160. public static function get_log_file_handle( $filename ) {
  161. return substr( $filename, 0, strlen( $filename ) > 48 ? strlen( $filename ) - 48 : strlen( $filename ) - 4 );
  162. }
  163. /**
  164. * Scan the template files.
  165. *
  166. * @param string $template_path Path to the template directory.
  167. * @return array
  168. */
  169. public static function scan_template_files( $template_path ) {
  170. $files = @scandir( $template_path ); // @codingStandardsIgnoreLine.
  171. $result = array();
  172. if ( ! empty( $files ) ) {
  173. foreach ( $files as $key => $value ) {
  174. if ( ! in_array( $value, array( '.', '..' ), true ) ) {
  175. if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) {
  176. $sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value );
  177. foreach ( $sub_files as $sub_file ) {
  178. $result[] = $value . DIRECTORY_SEPARATOR . $sub_file;
  179. }
  180. } else {
  181. $result[] = $value;
  182. }
  183. }
  184. }
  185. }
  186. return $result;
  187. }
  188. /**
  189. * Scan the log files.
  190. *
  191. * @return array
  192. */
  193. public static function scan_log_files() {
  194. return WC_Log_Handler_File::get_log_files();
  195. }
  196. /**
  197. * Get latest version of a theme by slug.
  198. *
  199. * @param object $theme WP_Theme object.
  200. * @return string Version number if found.
  201. */
  202. public static function get_latest_theme_version( $theme ) {
  203. include_once ABSPATH . 'wp-admin/includes/theme.php';
  204. $api = themes_api(
  205. 'theme_information',
  206. array(
  207. 'slug' => $theme->get_stylesheet(),
  208. 'fields' => array(
  209. 'sections' => false,
  210. 'tags' => false,
  211. ),
  212. )
  213. );
  214. $update_theme_version = 0;
  215. // Check .org for updates.
  216. if ( is_object( $api ) && ! is_wp_error( $api ) ) {
  217. $update_theme_version = $api->version;
  218. } elseif ( strstr( $theme->{'Author URI'}, 'woothemes' ) ) { // Check WooThemes Theme Version.
  219. $theme_dir = substr( strtolower( str_replace( ' ', '', $theme->Name ) ), 0, 45 ); // @codingStandardsIgnoreLine.
  220. $theme_version_data = get_transient( $theme_dir . '_version_data' );
  221. if ( false === $theme_version_data ) {
  222. $theme_changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $theme_dir . '/changelog.txt' );
  223. $cl_lines = explode( "\n", wp_remote_retrieve_body( $theme_changelog ) );
  224. if ( ! empty( $cl_lines ) ) {
  225. foreach ( $cl_lines as $line_num => $cl_line ) {
  226. if ( preg_match( '/^[0-9]/', $cl_line ) ) {
  227. $theme_date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) );
  228. $theme_version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) );
  229. $theme_update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) );
  230. $theme_version_data = array(
  231. 'date' => $theme_date,
  232. 'version' => $theme_version,
  233. 'update' => $theme_update,
  234. 'changelog' => $theme_changelog,
  235. );
  236. set_transient( $theme_dir . '_version_data', $theme_version_data, DAY_IN_SECONDS );
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. if ( ! empty( $theme_version_data['version'] ) ) {
  243. $update_theme_version = $theme_version_data['version'];
  244. }
  245. }
  246. return $update_theme_version;
  247. }
  248. /**
  249. * Remove/delete the chosen file.
  250. */
  251. public static function remove_log() {
  252. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'remove_log' ) ) { // WPCS: input var ok, sanitization ok.
  253. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  254. }
  255. if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok.
  256. $log_handler = new WC_Log_Handler_File();
  257. $log_handler->remove( wp_unslash( $_REQUEST['handle'] ) ); // WPCS: input var ok, sanitization ok.
  258. }
  259. wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
  260. exit();
  261. }
  262. /**
  263. * Clear DB log table.
  264. *
  265. * @since 3.0.0
  266. */
  267. private static function flush_db_logs() {
  268. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok.
  269. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  270. }
  271. WC_Log_Handler_DB::flush();
  272. wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
  273. exit();
  274. }
  275. /**
  276. * Bulk DB log table actions.
  277. *
  278. * @since 3.0.0
  279. */
  280. private static function log_table_bulk_actions() {
  281. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok.
  282. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  283. }
  284. $log_ids = array_map( 'absint', (array) isset( $_REQUEST['log'] ) ? wp_unslash( $_REQUEST['log'] ) : array() ); // WPCS: input var ok, sanitization ok.
  285. if ( ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] ) || ( isset( $_REQUEST['action2'] ) && 'delete' === $_REQUEST['action2'] ) ) { // WPCS: input var ok, sanitization ok.
  286. WC_Log_Handler_DB::delete( $log_ids );
  287. wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
  288. exit();
  289. }
  290. }
  291. /**
  292. * Prints table info if a base table is not present.
  293. */
  294. private static function output_tables_info() {
  295. $missing_tables = WC_Install::verify_base_tables( false );
  296. if ( 0 === count( $missing_tables ) ) {
  297. return;
  298. }
  299. ?>
  300. <br>
  301. <strong style="color:#a00;">
  302. <span class="dashicons dashicons-warning"></span>
  303. <?php
  304. echo esc_html(
  305. sprintf(
  306. // translators: Comma seperated list of missing tables.
  307. __( 'Missing base tables: %s. Some WooCommerce functionality may not work as expected.', 'woocommerce' ),
  308. implode( ', ', $missing_tables )
  309. )
  310. );
  311. ?>
  312. </strong>
  313. <?php
  314. }
  315. /**
  316. * Prints the information about plugins for the system status report.
  317. * Used for both active and inactive plugins sections.
  318. *
  319. * @param array $plugins List of plugins to display.
  320. * @param array $untested_plugins List of plugins that haven't been tested with the current WooCommerce version.
  321. * @return void
  322. */
  323. private static function output_plugins_info( $plugins, $untested_plugins ) {
  324. $wc_version = Constants::get_constant( 'WC_VERSION' );
  325. if ( 'major' === Constants::get_constant( 'WC_SSR_PLUGIN_UPDATE_RELEASE_VERSION_TYPE' ) ) {
  326. // Since we're only testing against major, we don't need to show minor and patch version.
  327. $wc_version = $wc_version[0] . '.0';
  328. }
  329. foreach ( $plugins as $plugin ) {
  330. if ( ! empty( $plugin['name'] ) ) {
  331. // Link the plugin name to the plugin url if available.
  332. $plugin_name = esc_html( $plugin['name'] );
  333. if ( ! empty( $plugin['url'] ) ) {
  334. $plugin_name = '<a href="' . esc_url( $plugin['url'] ) . '" aria-label="' . esc_attr__( 'Visit plugin homepage', 'woocommerce' ) . '" target="_blank">' . $plugin_name . '</a>';
  335. }
  336. $has_newer_version = false;
  337. $version_string = $plugin['version'];
  338. $network_string = '';
  339. if ( strstr( $plugin['url'], 'woothemes.com' ) || strstr( $plugin['url'], 'woocommerce.com' ) ) {
  340. if ( ! empty( $plugin['version_latest'] ) && version_compare( $plugin['version_latest'], $plugin['version'], '>' ) ) {
  341. /* translators: 1: current version. 2: latest version */
  342. $version_string = sprintf( __( '%1$s (update to version %2$s is available)', 'woocommerce' ), $plugin['version'], $plugin['version_latest'] );
  343. }
  344. if ( false !== $plugin['network_activated'] ) {
  345. $network_string = ' &ndash; <strong style="color: black;">' . esc_html__( 'Network enabled', 'woocommerce' ) . '</strong>';
  346. }
  347. }
  348. $untested_string = '';
  349. if ( array_key_exists( $plugin['plugin'], $untested_plugins ) ) {
  350. $untested_string = ' &ndash; <strong style="color: #a00;">';
  351. /* translators: %s: version */
  352. $untested_string .= esc_html( sprintf( __( 'Installed version not tested with active version of WooCommerce %s', 'woocommerce' ), $wc_version ) );
  353. $untested_string .= '</strong>';
  354. }
  355. ?>
  356. <tr>
  357. <td><?php echo wp_kses_post( $plugin_name ); ?></td>
  358. <td class="help">&nbsp;</td>
  359. <td>
  360. <?php
  361. /* translators: %s: plugin author */
  362. printf( esc_html__( 'by %s', 'woocommerce' ), esc_html( $plugin['author_name'] ) );
  363. echo ' &ndash; ' . esc_html( $version_string ) . $untested_string . $network_string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  364. ?>
  365. </td>
  366. </tr>
  367. <?php
  368. }
  369. }
  370. }
  371. }