1 <?php
  2 /**
  3  * Debug/Status page
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin/System Status
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Admin_Status' ) ) :
 14 
 15 /**
 16  * WC_Admin_Status Class
 17  */
 18 class WC_Admin_Status {
 19 
 20     /**
 21      * Handles output of the reports page in admin.
 22      */
 23     public function output() {
 24         $current_tab = ! empty( $_REQUEST['tab'] ) ? sanitize_title( $_REQUEST['tab'] ) : 'status';
 25 
 26         include_once( 'views/html-admin-page-status.php' );
 27     }
 28 
 29     /**
 30      * Handles output of report
 31      */
 32     public function status_report() {
 33         global $woocommerce, $wpdb;
 34 
 35         include_once( 'views/html-admin-page-status-report.php' );
 36     }
 37 
 38     /**
 39      * Handles output of tools
 40      */
 41     public function status_tools() {
 42         global $woocommerce, $wpdb;
 43 
 44         $tools = $this->get_tools();
 45 
 46         if ( ! empty( $_GET['action'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) ) {
 47 
 48             switch ( $_GET['action'] ) {
 49                 case "clear_transients" :
 50                     wc_delete_product_transients();
 51 
 52                     echo '<div class="updated"><p>' . __( 'Product Transients Cleared', 'woocommerce' ) . '</p></div>';
 53                 break;
 54                 case "clear_expired_transients" :
 55 
 56                     // http://w-shadow.com/blog/2012/04/17/delete-stale-transients/
 57                     $rows = $wpdb->query( "
 58                         DELETE
 59                             a, b
 60                         FROM
 61                             {$wpdb->options} a, {$wpdb->options} b
 62                         WHERE
 63                             a.option_name LIKE '_transient_%' AND
 64                             a.option_name NOT LIKE '_transient_timeout_%' AND
 65                             b.option_name = CONCAT(
 66                                 '_transient_timeout_',
 67                                 SUBSTRING(
 68                                     a.option_name,
 69                                     CHAR_LENGTH('_transient_') + 1
 70                                 )
 71                             )
 72                             AND b.option_value < UNIX_TIMESTAMP()
 73                     " );
 74 
 75                     $rows2 = $wpdb->query( "
 76                         DELETE
 77                             a, b
 78                         FROM
 79                             {$wpdb->options} a, {$wpdb->options} b
 80                         WHERE
 81                             a.option_name LIKE '_site_transient_%' AND
 82                             a.option_name NOT LIKE '_site_transient_timeout_%' AND
 83                             b.option_name = CONCAT(
 84                                 '_site_transient_timeout_',
 85                                 SUBSTRING(
 86                                     a.option_name,
 87                                     CHAR_LENGTH('_site_transient_') + 1
 88                                 )
 89                             )
 90                             AND b.option_value < UNIX_TIMESTAMP()
 91                     " );
 92 
 93                     echo '<div class="updated"><p>' . sprintf( __( '%d Transients Rows Cleared', 'woocommerce' ), $rows + $rows2 ) . '</p></div>';
 94 
 95                 break;
 96                 case "reset_roles" :
 97                     // Remove then re-add caps and roles
 98                     $installer = include( WC()->plugin_path() . '/includes/class-wc-install.php' );
 99                     $installer->remove_roles();
100                     $installer->create_roles();
101 
102                     echo '<div class="updated"><p>' . __( 'Roles successfully reset', 'woocommerce' ) . '</p></div>';
103                 break;
104                 case "recount_terms" :
105 
106                     $product_cats = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
107 
108                     _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false );
109 
110                     $product_tags = get_terms( 'product_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
111 
112                     _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false );
113 
114                     delete_transient( 'wc_term_counts' );
115 
116                     echo '<div class="updated"><p>' . __( 'Terms successfully recounted', 'woocommerce' ) . '</p></div>';
117                 break;
118                 case "clear_sessions" :
119 
120                     $wpdb->query( "
121                         DELETE FROM {$wpdb->options}
122                         WHERE option_name LIKE '_wc_session_%' OR option_name LIKE '_wc_session_expires_%'
123                     " );
124 
125                     wp_cache_flush();
126 
127                     echo '<div class="updated"><p>' . __( 'Sessions successfully cleared', 'woocommerce' ) . '</p></div>';
128                 break;
129                 case "install_pages" :
130                     WC_Install::create_pages();
131                     echo '<div class="updated"><p>' . __( 'All missing WooCommerce pages was installed successfully.', 'woocommerce' ) . '</p></div>';
132                 break;
133                 case "delete_taxes" :
134 
135                     $wpdb->query( "TRUNCATE " . $wpdb->prefix . "woocommerce_tax_rates" );
136 
137                     $wpdb->query( "TRUNCATE " . $wpdb->prefix . "woocommerce_tax_rate_locations" );
138 
139                     echo '<div class="updated"><p>' . __( 'Tax rates successfully deleted', 'woocommerce' ) . '</p></div>';
140                 break;
141                 default:
142                     $action = esc_attr( $_GET['action'] );
143                     if( isset( $tools[ $action ]['callback'] ) ) {
144                         $callback = $tools[ $action ]['callback'];
145                         $return = call_user_func( $callback );
146                         if( $return === false ) {
147                             if( is_array( $callback ) ) {
148                                 echo '<div class="error"><p>' . sprintf( __( 'There was an error calling %s::%s', 'woocommerce' ), get_class( $callback[0] ), $callback[1] ) . '</p></div>';
149 
150                             } else {
151                                 echo '<div class="error"><p>' . sprintf( __( 'There was an error calling %s', 'woocommerce' ), $callback ) . '</p></div>';
152                             }
153                         }
154                     }
155                 break;
156             }
157         }
158         
159         // Display message if settings settings have been saved
160         if ( isset( $_REQUEST['settings-updated'] ) ) {
161             echo '<div class="updated"><p>' . __( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>';
162         }
163 
164         include_once( 'views/html-admin-page-status-tools.php' );
165     }
166 
167     /**
168      * Get tools
169      *
170      * @return array of tools
171      */
172     public function get_tools() {
173         return apply_filters( 'woocommerce_debug_tools', array(
174             'clear_transients' => array(
175                 'name'      => __( 'WC Transients','woocommerce'),
176                 'button'    => __('Clear transients','woocommerce'),
177                 'desc'      => __( 'This tool will clear the product/shop transients cache.', 'woocommerce' ),
178             ),
179             'clear_expired_transients' => array(
180                 'name'      => __( 'Expired Transients','woocommerce'),
181                 'button'    => __('Clear expired transients','woocommerce'),
182                 'desc'      => __( 'This tool will clear ALL expired transients from WordPress.', 'woocommerce' ),
183             ),
184             'recount_terms' => array(
185                 'name'      => __('Term counts','woocommerce'),
186                 'button'    => __('Recount terms','woocommerce'),
187                 'desc'      => __( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', 'woocommerce' ),
188             ),
189             'reset_roles' => array(
190                 'name'      => __('Capabilities','woocommerce'),
191                 'button'    => __('Reset capabilities','woocommerce'),
192                 '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' ),
193             ),
194             'clear_sessions' => array(
195                 'name'      => __('Customer Sessions','woocommerce'),
196                 'button'    => __('Clear all sessions','woocommerce'),
197                 'desc'      => __( '<strong class="red">Warning:</strong> This tool will delete all customer session data from the database, including any current live carts.', 'woocommerce' ),
198             ),
199             'install_pages' => array(
200                 'name'    => __( 'Install WooCommerce Pages', 'woocommerce' ),
201                 'button'  => __( 'Install pages', 'woocommerce' ),
202                 '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' ),
203             ),
204             'delete_taxes' => array(
205                 'name'    => __( 'Delete all WooCommerce tax rates', 'woocommerce' ),
206                 'button'  => __( 'Delete ALL tax rates', 'woocommerce' ),
207                 'desc'    => __( '<strong class="red">Note:</strong> This option will delete ALL of your tax rates, use with caution.', 'woocommerce' ),
208             ),
209         ) );
210     }
211 
212     /**
213      * Retrieve metadata from a file. Based on WP Core's get_file_data function
214      *
215      * @since 2.1.1
216      * @param string $file Path to the file
217      * @param array $all_headers List of headers, in the format array('HeaderKey' => 'Header Name')
218      */
219     public function get_file_version( $file ) {
220         // We don't need to write to the file, so just open for reading.
221         $fp = fopen( $file, 'r' );
222 
223         // Pull only the first 8kiB of the file in.
224         $file_data = fread( $fp, 8192 );
225 
226         // PHP will close file handle, but we are good citizens.
227         fclose( $fp );
228 
229         // Make sure we catch CR-only line endings.
230         $file_data = str_replace( "\r", "\n", $file_data );
231         $version   = '';
232 
233         if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] )
234             $version = _cleanup_header_comment( $match[1] );
235 
236         return $version ;
237     }
238     
239     /**
240      * Scan the template files
241      *
242      * @access public
243      * @param string $template_path
244      * @return array
245      */
246     public function scan_template_files( $template_path ) {
247         $files         = scandir( $template_path );
248         $result        = array();
249         if ( $files ) {
250             foreach ( $files as $key => $value ) {
251                 if ( ! in_array( $value, array( ".",".." ) ) ) {
252                     if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) {
253                         $sub_files = $this->scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value );
254                         foreach ( $sub_files as $sub_file ) {
255                             $result[] = $value . DIRECTORY_SEPARATOR . $sub_file;
256                         }
257                     } else {
258                         $result[] = $value;
259                     }
260                 }
261             }
262         }
263         return $result;
264     }
265 }
266 
267 endif;
268 
269 return new WC_Admin_Status();
270 
WooCommerce API documentation generated by ApiGen 2.8.0