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( 'views/html-admin-page-status.php' ); 
  8.  
  9. /** 
  10. * Handles output of report. 
  11. */ 
  12. public static function status_report() { 
  13. include_once( '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.  
  25. switch ( $_GET['action'] ) { 
  26. case 'clear_transients' : 
  27. wc_delete_product_transients(); 
  28. wc_delete_shop_order_transients(); 
  29. WC_Cache_Helper::get_transient_version( 'shipping', true ); 
  30.  
  31. echo '<div class="updated inline"><p>' . __( 'Product Transients Cleared', 'woocommerce' ) . '</p></div>'; 
  32. break; 
  33. case 'clear_expired_transients' : 
  34.  
  35. /** 
  36. * Deletes all expired transients. The multi-table delete syntax is used. 
  37. * to delete the transient record from table a, and the corresponding. 
  38. * transient_timeout record from table b. 
  39. * Based on code inside core's upgrade_network() function. 
  40. */ 
  41. $sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b 
  42. WHERE a.option_name LIKE %s 
  43. AND a.option_name NOT LIKE %s 
  44. AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) 
  45. AND b.option_value < %d"; 
  46. $rows = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) ); 
  47.  
  48. $sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b 
  49. WHERE a.option_name LIKE %s 
  50. AND a.option_name NOT LIKE %s 
  51. AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) 
  52. AND b.option_value < %d"; 
  53. $rows2 = $wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); 
  54.  
  55. echo '<div class="updated inline"><p>' . sprintf( __( '%d Transients Rows Cleared', 'woocommerce' ), $rows + $rows2 ) . '</p></div>'; 
  56. break; 
  57. case 'reset_roles' : 
  58. // Remove then re-add caps and roles 
  59. WC_Install::remove_roles(); 
  60. WC_Install::create_roles(); 
  61.  
  62. echo '<div class="updated inline"><p>' . __( 'Roles successfully reset', 'woocommerce' ) . '</p></div>'; 
  63. break; 
  64. case 'recount_terms' : 
  65.  
  66. $product_cats = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); 
  67.  
  68. _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false ); 
  69.  
  70. $product_tags = get_terms( 'product_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); 
  71.  
  72. _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false ); 
  73.  
  74. echo '<div class="updated inline"><p>' . __( 'Terms successfully recounted', 'woocommerce' ) . '</p></div>'; 
  75. break; 
  76. case 'clear_sessions' : 
  77.  
  78. $wpdb->query( "TRUNCATE {$wpdb->prefix}woocommerce_sessions" ); 
  79.  
  80. wp_cache_flush(); 
  81.  
  82. echo '<div class="updated inline"><p>' . __( 'Sessions successfully cleared', 'woocommerce' ) . '</p></div>'; 
  83. break; 
  84. case 'install_pages' : 
  85. WC_Install::create_pages(); 
  86. echo '<div class="updated inline"><p>' . __( 'All missing WooCommerce pages was installed successfully.', 'woocommerce' ) . '</p></div>'; 
  87. break; 
  88. case 'delete_taxes' : 
  89.  
  90. $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rates;" ); 
  91. $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations;" ); 
  92. WC_Cache_Helper::incr_cache_prefix( 'taxes' ); 
  93.  
  94. echo '<div class="updated inline"><p>' . __( 'Tax rates successfully deleted', 'woocommerce' ) . '</p></div>'; 
  95. break; 
  96. case 'reset_tracking' : 
  97. delete_option( 'woocommerce_allow_tracking' ); 
  98. WC_Admin_Notices::add_notice( 'tracking' ); 
  99.  
  100. echo '<div class="updated inline"><p>' . __( 'Usage tracking settings successfully reset.', 'woocommerce' ) . '</p></div>'; 
  101. break; 
  102. default : 
  103. $action = esc_attr( $_GET['action'] ); 
  104. if ( isset( $tools[ $action ]['callback'] ) ) { 
  105. $callback = $tools[ $action ]['callback']; 
  106. $return = call_user_func( $callback ); 
  107. if ( $return === false ) { 
  108. $callback_string = is_array( $callback ) ? get_class( $callback[0] ) . '::' . $callback[1] : $callback; 
  109. echo '<div class="error inline"><p>' . sprintf( __( 'There was an error calling %s', 'woocommerce' ), $callback_string ) . '</p></div>'; 
  110. break; 
  111.  
  112. // Display message if settings settings have been saved 
  113. if ( isset( $_REQUEST['settings-updated'] ) ) { 
  114. echo '<div class="updated inline"><p>' . __( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>'; 
  115.  
  116. include_once( 'views/html-admin-page-status-tools.php' ); 
  117.  
  118. /** 
  119. * Get tools. 
  120. * @return array of tools 
  121. */ 
  122. public static function get_tools() { 
  123. $tools = array( 
  124. 'clear_transients' => array( 
  125. 'name' => __( 'WC Transients', 'woocommerce' ),  
  126. 'button' => __( 'Clear transients', 'woocommerce' ),  
  127. 'desc' => __( 'This tool will clear the product/shop transients cache.', 'woocommerce' ),  
  128. ),  
  129. 'clear_expired_transients' => array( 
  130. 'name' => __( 'Expired Transients', 'woocommerce' ),  
  131. 'button' => __( 'Clear expired transients', 'woocommerce' ),  
  132. 'desc' => __( 'This tool will clear ALL expired transients from WordPress.', 'woocommerce' ),  
  133. ),  
  134. 'recount_terms' => array( 
  135. 'name' => __( 'Term counts', 'woocommerce' ),  
  136. 'button' => __( 'Recount terms', 'woocommerce' ),  
  137. 'desc' => __( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', 'woocommerce' ),  
  138. ),  
  139. 'reset_roles' => array( 
  140. 'name' => __( 'Capabilities', 'woocommerce' ),  
  141. 'button' => __( 'Reset capabilities', 'woocommerce' ),  
  142. 'desc' => __( 'This tool will reset the admin, customer and shop_manager roles to default. Use this if your users cannot access all of the WooCommerce admin pages.', 'woocommerce' ),  
  143. ),  
  144. 'clear_sessions' => array( 
  145. 'name' => __( 'Customer Sessions', 'woocommerce' ),  
  146. 'button' => __( 'Clear all sessions', 'woocommerce' ),  
  147. 'desc' => __( '<strong class="red">Warning:</strong> This tool will delete all customer session data from the database, including any current live carts.', 'woocommerce' ),  
  148. ),  
  149. 'install_pages' => array( 
  150. 'name' => __( 'Install WooCommerce Pages', 'woocommerce' ),  
  151. 'button' => __( 'Install pages', 'woocommerce' ),  
  152. 'desc' => __( '<strong class="red">Note:</strong> This tool will install all the missing WooCommerce pages. Pages already defined and set up will not be replaced.', 'woocommerce' ),  
  153. ),  
  154. 'delete_taxes' => array( 
  155. 'name' => __( 'Delete all WooCommerce tax rates', 'woocommerce' ),  
  156. 'button' => __( 'Delete ALL tax rates', 'woocommerce' ),  
  157. 'desc' => __( '<strong class="red">Note:</strong> This option will delete ALL of your tax rates, use with caution.', 'woocommerce' ),  
  158. ),  
  159. 'reset_tracking' => array( 
  160. 'name' => __( 'Reset Usage Tracking Settings', 'woocommerce' ),  
  161. 'button' => __( 'Reset usage tracking settings', 'woocommerce' ),  
  162. 'desc' => __( 'This will reset your usage tracking settings, causing it to show the opt-in banner again and not sending any data.', 'woocommerce' ),  
  163. ); 
  164.  
  165. return apply_filters( 'woocommerce_debug_tools', $tools ); 
  166.  
  167. /** 
  168. * Show the logs page. 
  169. */ 
  170. public static function status_logs() { 
  171.  
  172. $logs = self::scan_log_files(); 
  173.  
  174. if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( $_REQUEST['log_file'] ) ] ) ) { 
  175. $viewed_log = $logs[ sanitize_title( $_REQUEST['log_file'] ) ]; 
  176. } elseif ( ! empty( $logs ) ) { 
  177. $viewed_log = current( $logs ); 
  178.  
  179. include_once( 'views/html-admin-page-status-logs.php' ); 
  180.  
  181. /** 
  182. * Retrieve metadata from a file. Based on WP Core's get_file_data function. 
  183. * @since 2.1.1 
  184. * @param string $file Path to the file 
  185. * @return string 
  186. */ 
  187. public static function get_file_version( $file ) { 
  188.  
  189. // Avoid notices if file does not exist 
  190. if ( ! file_exists( $file ) ) { 
  191. return ''; 
  192.  
  193. // We don't need to write to the file, so just open for reading. 
  194. $fp = fopen( $file, 'r' ); 
  195.  
  196. // Pull only the first 8kiB of the file in. 
  197. $file_data = fread( $fp, 8192 ); 
  198.  
  199. // PHP will close file handle, but we are good citizens. 
  200. fclose( $fp ); 
  201.  
  202. // Make sure we catch CR-only line endings. 
  203. $file_data = str_replace( "\r", "\n", $file_data ); 
  204. $version = ''; 
  205.  
  206. if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) 
  207. $version = _cleanup_header_comment( $match[1] ); 
  208.   
  209. return $version ; 
  210.   
  211. /** 
  212. * Scan the template files. 
  213. * @param string $template_path 
  214. * @return array 
  215. */ 
  216. public static function scan_template_files( $template_path ) { 
  217.  
  218. $files = @scandir( $template_path ); 
  219. $result = array(); 
  220.  
  221. if ( ! empty( $files ) ) { 
  222.  
  223. foreach ( $files as $key => $value ) { 
  224.  
  225. if ( ! in_array( $value, array( ".", ".." ) ) ) { 
  226.  
  227. if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) { 
  228. $sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value ); 
  229. foreach ( $sub_files as $sub_file ) { 
  230. $result[] = $value . DIRECTORY_SEPARATOR . $sub_file; 
  231. } else { 
  232. $result[] = $value; 
  233. return $result; 
  234.  
  235. /** 
  236. * Scan the log files. 
  237. * @return array 
  238. */ 
  239. public static function scan_log_files() { 
  240. $files = @scandir( WC_LOG_DIR ); 
  241. $result = array(); 
  242.  
  243. if ( ! empty( $files ) ) { 
  244.  
  245. foreach ( $files as $key => $value ) { 
  246.  
  247. if ( ! in_array( $value, array( '.', '..' ) ) ) { 
  248. if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) { 
  249. $result[ sanitize_title( $value ) ] = $value; 
  250.  
  251.  
  252. return $result; 
  253.  
  254. /** 
  255. * Get latest version of a theme by slug. 
  256. * @param object $theme WP_Theme object. 
  257. * @return string Version number if found. 
  258. */ 
  259. public static function get_latest_theme_version( $theme ) { 
  260. $api = themes_api( 'theme_information', array( 
  261. 'slug' => $theme->get_stylesheet(),  
  262. 'fields' => array( 
  263. 'sections' => false,  
  264. 'tags' => false,  
  265. ) ); 
  266.  
  267. $update_theme_version = 0; 
  268.  
  269. // Check .org for updates. 
  270. if ( is_object( $api ) && ! is_wp_error( $api ) ) { 
  271. $update_theme_version = $api->version; 
  272.  
  273. // Check WooThemes Theme Version. 
  274. } elseif ( strstr( $theme->{'Author URI'}, 'woothemes' ) ) { 
  275. $theme_dir = substr( strtolower( str_replace( ' ', '', $theme->Name ) ), 0, 45 ); 
  276.  
  277. if ( false === ( $theme_version_data = get_transient( $theme_dir . '_version_data' ) ) ) { 
  278. $theme_changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $theme_dir . '/changelog.txt' ); 
  279. $cl_lines = explode( "\n", wp_remote_retrieve_body( $theme_changelog ) ); 
  280. if ( ! empty( $cl_lines ) ) { 
  281. foreach ( $cl_lines as $line_num => $cl_line ) { 
  282. if ( preg_match( '/^[0-9]/', $cl_line ) ) { 
  283. $theme_date = str_replace( '.' , '-' , trim( substr( $cl_line , 0 , strpos( $cl_line , '-' ) ) ) ); 
  284. $theme_version = preg_replace( '~[^0-9, .]~' , '' , stristr( $cl_line , "version" ) ); 
  285. $theme_update = trim( str_replace( "*" , "" , $cl_lines[ $line_num + 1 ] ) ); 
  286. $theme_version_data = array( 'date' => $theme_date , 'version' => $theme_version , 'update' => $theme_update , 'changelog' => $theme_changelog ); 
  287. set_transient( $theme_dir . '_version_data', $theme_version_data , DAY_IN_SECONDS ); 
  288. break; 
  289.  
  290. if ( ! empty( $theme_version_data['version'] ) ) { 
  291. $update_theme_version = $theme_version_data['version']; 
  292.  
  293. return $update_theme_version;