/includes/admin/class-wc-admin-status.php

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