class-wc-admin-status.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * Debug/Status page
  4. *
  5. * @package WooCommerce/Admin/System Status
  6. * @version 2.2.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * WC_Admin_Status Class.
  11. */
  12. class WC_Admin_Status {
  13. /**
  14. * Handles output of the reports page in admin.
  15. */
  16. public static function output() {
  17. include_once dirname( __FILE__ ) . '/views/html-admin-page-status.php';
  18. }
  19. /**
  20. * Handles output of report.
  21. */
  22. public static function status_report() {
  23. include_once dirname( __FILE__ ) . '/views/html-admin-page-status-report.php';
  24. }
  25. /**
  26. * Handles output of tools.
  27. */
  28. public static function status_tools() {
  29. $tools = self::get_tools();
  30. if ( ! empty( $_GET['action'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'debug_action' ) ) { // WPCS: input var ok, sanitization ok.
  31. $tools_controller = new WC_REST_System_Status_Tools_Controller();
  32. $action = wc_clean( wp_unslash( $_GET['action'] ) ); // WPCS: input var ok.
  33. if ( array_key_exists( $action, $tools ) ) {
  34. $response = $tools_controller->execute_tool( $action );
  35. } else {
  36. $response = array(
  37. 'success' => false,
  38. 'message' => __( 'Tool does not exist.', 'woocommerce' ),
  39. );
  40. }
  41. if ( $response['success'] ) {
  42. echo '<div class="updated inline"><p>' . esc_html( $response['message'] ) . '</p></div>';
  43. } else {
  44. echo '<div class="error inline"><p>' . esc_html( $response['message'] ) . '</p></div>';
  45. }
  46. }
  47. // Display message if settings settings have been saved.
  48. if ( isset( $_REQUEST['settings-updated'] ) ) { // WPCS: input var ok.
  49. echo '<div class="updated inline"><p>' . esc_html__( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>';
  50. }
  51. include_once dirname( __FILE__ ) . '/views/html-admin-page-status-tools.php';
  52. }
  53. /**
  54. * Get tools.
  55. *
  56. * @return array of tools
  57. */
  58. public static function get_tools() {
  59. $tools_controller = new WC_REST_System_Status_Tools_Controller();
  60. return $tools_controller->get_tools();
  61. }
  62. /**
  63. * Show the logs page.
  64. */
  65. public static function status_logs() {
  66. if ( defined( 'WC_LOG_HANDLER' ) && 'WC_Log_Handler_DB' === WC_LOG_HANDLER ) {
  67. self::status_logs_db();
  68. } else {
  69. self::status_logs_file();
  70. }
  71. }
  72. /**
  73. * Show the log page contents for file log handler.
  74. */
  75. public static function status_logs_file() {
  76. $logs = self::scan_log_files();
  77. if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ] ) ) { // WPCS: input var ok, CSRF ok.
  78. $viewed_log = $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ]; // WPCS: input var ok, CSRF ok.
  79. } elseif ( ! empty( $logs ) ) {
  80. $viewed_log = current( $logs );
  81. }
  82. $handle = ! empty( $viewed_log ) ? self::get_log_file_handle( $viewed_log ) : '';
  83. if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok, CSRF ok.
  84. self::remove_log();
  85. }
  86. include_once 'views/html-admin-page-status-logs.php';
  87. }
  88. /**
  89. * Show the log page contents for db log handler.
  90. */
  91. public static function status_logs_db() {
  92. if ( ! empty( $_REQUEST['flush-logs'] ) ) { // WPCS: input var ok, CSRF ok.
  93. self::flush_db_logs();
  94. }
  95. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['log'] ) ) { // WPCS: input var ok, CSRF ok.
  96. self::log_table_bulk_actions();
  97. }
  98. $log_table_list = new WC_Admin_Log_Table_List();
  99. $log_table_list->prepare_items();
  100. include_once 'views/html-admin-page-status-logs-db.php';
  101. }
  102. /**
  103. * Retrieve metadata from a file. Based on WP Core's get_file_data function.
  104. *
  105. * @since 2.1.1
  106. * @param string $file Path to the file.
  107. * @return string
  108. */
  109. public static function get_file_version( $file ) {
  110. // Avoid notices if file does not exist.
  111. if ( ! file_exists( $file ) ) {
  112. return '';
  113. }
  114. // We don't need to write to the file, so just open for reading.
  115. $fp = fopen( $file, 'r' ); // @codingStandardsIgnoreLine.
  116. // Pull only the first 8kiB of the file in.
  117. $file_data = fread( $fp, 8192 ); // @codingStandardsIgnoreLine.
  118. // PHP will close file handle, but we are good citizens.
  119. fclose( $fp ); // @codingStandardsIgnoreLine.
  120. // Make sure we catch CR-only line endings.
  121. $file_data = str_replace( "\r", "\n", $file_data );
  122. $version = '';
  123. if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) {
  124. $version = _cleanup_header_comment( $match[1] );
  125. }
  126. return $version;
  127. }
  128. /**
  129. * Return the log file handle.
  130. *
  131. * @param string $filename Filename to get the handle for.
  132. * @return string
  133. */
  134. public static function get_log_file_handle( $filename ) {
  135. return substr( $filename, 0, strlen( $filename ) > 48 ? strlen( $filename ) - 48 : strlen( $filename ) - 4 );
  136. }
  137. /**
  138. * Scan the template files.
  139. *
  140. * @param string $template_path Path to the template directory.
  141. * @return array
  142. */
  143. public static function scan_template_files( $template_path ) {
  144. $files = @scandir( $template_path ); // @codingStandardsIgnoreLine.
  145. $result = array();
  146. if ( ! empty( $files ) ) {
  147. foreach ( $files as $key => $value ) {
  148. if ( ! in_array( $value, array( '.', '..' ), true ) ) {
  149. if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) {
  150. $sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value );
  151. foreach ( $sub_files as $sub_file ) {
  152. $result[] = $value . DIRECTORY_SEPARATOR . $sub_file;
  153. }
  154. } else {
  155. $result[] = $value;
  156. }
  157. }
  158. }
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Scan the log files.
  164. *
  165. * @return array
  166. */
  167. public static function scan_log_files() {
  168. return WC_Log_Handler_File::get_log_files();
  169. }
  170. /**
  171. * Get latest version of a theme by slug.
  172. *
  173. * @param object $theme WP_Theme object.
  174. * @return string Version number if found.
  175. */
  176. public static function get_latest_theme_version( $theme ) {
  177. include_once ABSPATH . 'wp-admin/includes/theme.php';
  178. $api = themes_api(
  179. 'theme_information',
  180. array(
  181. 'slug' => $theme->get_stylesheet(),
  182. 'fields' => array(
  183. 'sections' => false,
  184. 'tags' => false,
  185. ),
  186. )
  187. );
  188. $update_theme_version = 0;
  189. // Check .org for updates.
  190. if ( is_object( $api ) && ! is_wp_error( $api ) ) {
  191. $update_theme_version = $api->version;
  192. } elseif ( strstr( $theme->{'Author URI'}, 'woothemes' ) ) { // Check WooThemes Theme Version.
  193. $theme_dir = substr( strtolower( str_replace( ' ', '', $theme->Name ) ), 0, 45 ); // @codingStandardsIgnoreLine.
  194. $theme_version_data = get_transient( $theme_dir . '_version_data' );
  195. if ( false === $theme_version_data ) {
  196. $theme_changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $theme_dir . '/changelog.txt' );
  197. $cl_lines = explode( "\n", wp_remote_retrieve_body( $theme_changelog ) );
  198. if ( ! empty( $cl_lines ) ) {
  199. foreach ( $cl_lines as $line_num => $cl_line ) {
  200. if ( preg_match( '/^[0-9]/', $cl_line ) ) {
  201. $theme_date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) );
  202. $theme_version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) );
  203. $theme_update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) );
  204. $theme_version_data = array(
  205. 'date' => $theme_date,
  206. 'version' => $theme_version,
  207. 'update' => $theme_update,
  208. 'changelog' => $theme_changelog,
  209. );
  210. set_transient( $theme_dir . '_version_data', $theme_version_data, DAY_IN_SECONDS );
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. if ( ! empty( $theme_version_data['version'] ) ) {
  217. $update_theme_version = $theme_version_data['version'];
  218. }
  219. }
  220. return $update_theme_version;
  221. }
  222. /**
  223. * Remove/delete the chosen file.
  224. */
  225. public static function remove_log() {
  226. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'remove_log' ) ) { // WPCS: input var ok, sanitization ok.
  227. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  228. }
  229. if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok.
  230. $log_handler = new WC_Log_Handler_File();
  231. $log_handler->remove( wp_unslash( $_REQUEST['handle'] ) ); // WPCS: input var ok, sanitization ok.
  232. }
  233. wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
  234. exit();
  235. }
  236. /**
  237. * Clear DB log table.
  238. *
  239. * @since 3.0.0
  240. */
  241. private static function flush_db_logs() {
  242. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok.
  243. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  244. }
  245. WC_Log_Handler_DB::flush();
  246. wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
  247. exit();
  248. }
  249. /**
  250. * Bulk DB log table actions.
  251. *
  252. * @since 3.0.0
  253. */
  254. private static function log_table_bulk_actions() {
  255. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok.
  256. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  257. }
  258. $log_ids = array_map( 'absint', (array) isset( $_REQUEST['log'] ) ? wp_unslash( $_REQUEST['log'] ) : array() ); // WPCS: input var ok, sanitization ok.
  259. if ( ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] ) || ( isset( $_REQUEST['action2'] ) && 'delete' === $_REQUEST['action2'] ) ) { // WPCS: input var ok, sanitization ok.
  260. WC_Log_Handler_DB::delete( $log_ids );
  261. wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
  262. exit();
  263. }
  264. }
  265. }