WC_Admin_Status

WC_Admin_Status Class.

Defined (1)

The class is defined in the following location(s).

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