Apply code review

This commit is contained in:
Adrien Foulon 2019-03-13 17:52:28 +01:00 committed by GitHub
parent 7099aa5d1a
commit 80435d779f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 23 deletions

View File

@ -19,8 +19,14 @@ class WC_Admin_Report {
/** /**
* Init the static hooks of the class. * Init the static hooks of the class.
*/ */
public static function init_class() { protected static function add_update_transients_hook() {
add_action( 'shutdown', array( 'WC_Admin_Report', '_maybe_update_transients' ) ); static $done = false;
if ( $done ) {
return;
}
$done = true;
add_action( 'shutdown', array( 'WC_Admin_Report', 'maybe_update_transients' ) );
} }
/** /**
@ -330,10 +336,6 @@ class WC_Admin_Report {
$query = apply_filters( 'woocommerce_reports_get_order_report_query', $query ); $query = apply_filters( 'woocommerce_reports_get_order_report_query', $query );
$query = implode( ' ', $query ); $query = implode( ' ', $query );
$query_hash = md5( $query_type . $query ); $query_hash = md5( $query_type . $query );
$class = strtolower( get_class( $this ) );
if ( ! isset( self::$cached_results[ $class ] ) ) {
self::$cached_results[ $class ] = get_transient( strtolower( get_class( $this ) ) );
}
if ( $debug ) { if ( $debug ) {
echo '<pre>'; echo '<pre>';
@ -341,21 +343,54 @@ class WC_Admin_Report {
echo '</pre>'; echo '</pre>';
} }
if ( $debug || $nocache || false === self::$cached_results[ $class ] || ! isset( self::$cached_results[ $class ][ $query_hash ] ) ) { $result = $this->get_cached_query();
if ( $debug || $nocache || is_null( $result ) ) {
self::enable_big_selects();
$result = apply_filters( 'woocommerce_reports_get_order_report_data', $wpdb->$query_type( $query ), $data );
}
return $result;
}
/**
* Enables big mysql selects for reports, just once for this session
*/
protected static function enable_big_selects() {
static $big_selects = false; static $big_selects = false;
// Enable big selects for reports, just once for this session
global $wpdb;
if ( ! $big_selects ) { if ( ! $big_selects ) {
$wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' ); $wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' );
$big_selects = true; $big_selects = true;
} }
self::$transients_to_update[ $class ] = $class;
self::$cached_results[ $class ][ $query_hash ] = apply_filters( 'woocommerce_reports_get_order_report_data', $wpdb->$query_type( $query ), $data );
} }
$result = self::$cached_results[ $class ][ $query_hash ]; /**
* Get the cached query result or null if it's not in the cache
*
* @param $query_hash
*
* @return mixed
*/
protected function get_cached_query( $query_hash ) {
$class = strtolower( get_class( $this ) );
if ( ! isset( self::$cached_results[ $class ] ) ) {
self::$cached_results[ $class ] = get_transient( strtolower( get_class( $this ) ) );
}
if ( false === self::$cached_results[ $class ] || ! isset( self::$cached_results[ $class ][ $query_hash ] ) ) {
self::enable_big_selects();
self::add_update_transients_hook();
self::$transients_to_update[ $class ] = $class;
self::$cached_results[ $class ][ $query_hash ] = apply_filters( 'woocommerce_reports_get_order_report_data', $wpdb->$query_type( $query ), $data );
return $result; return self::$cached_results[ $class ][ $query_hash ];
}
return null;
} }
/** /**
@ -371,11 +406,11 @@ class WC_Admin_Report {
/** /**
* Function to update the modified transients at the end of the request. * Function to update the modified transients at the end of the request.
*/ */
public static function _maybe_update_transients() { public static function maybe_update_transients() {
foreach ( self::$transients_to_update as $key => $transient_name ) { foreach ( self::$transients_to_update as $key => $transient_name ) {
set_transient( $transient_name, self::$cached_results[ $transient_name ], DAY_IN_SECONDS ); set_transient( $transient_name, self::$cached_results[ $transient_name ], DAY_IN_SECONDS );
} }
//Reset the transients // Transients have been updated reset the list.
self::$transients_to_update = array(); self::$transients_to_update = array();
} }
@ -707,5 +742,3 @@ class WC_Admin_Report {
} }
} }
} }
WC_Admin_Report::init_class();