Automattic\WooCommerce\Internal\Admin\Orders

ListTable::get_and_maybe_update_months_filter_cache()protectedWC 1.0

Get order year-months cache. We cache the results in the options table, since these results will change very infrequently. We use the heuristic to always return current year-month when getting from cache to prevent an additional query.

Метод класса: ListTable{}

Хуков нет.

Возвращает

Массив. List of year-months.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_and_maybe_update_months_filter_cache(): array;

Код ListTable::get_and_maybe_update_months_filter_cache() WC 9.3.3

protected function get_and_maybe_update_months_filter_cache(): array {
	global $wpdb;

	// We cache in the options table since it's won't be invalidated soon.
	$cache_option_value_name = 'wc_' . $this->order_type . '_list_table_months_filter_cache_value';
	$cache_option_date_name  = 'wc_' . $this->order_type . '_list_table_months_filter_cache_date';

	$cached_timestamp = get_option( $cache_option_date_name, 0 );

	// new day, new cache.
	if ( 0 === $cached_timestamp || gmdate( 'j', time() ) !== gmdate( 'j', $cached_timestamp ) || ( time() - $cached_timestamp ) > 60 * 60 * 24 ) {
		$cached_value = false;
	} else {
		$cached_value = get_option( $cache_option_value_name );
	}

	if ( false !== $cached_value ) {
		// Always add current year month for cache stability. This allows us to not hydrate the cache on every order update.
		$current_year_month        = new \stdClass();
		$current_year_month->year  = gmdate( 'Y', time() );
		$current_year_month->month = gmdate( 'n', time() );
		if ( count( $cached_value ) === 0 || (
			$cached_value[0]->year !== $current_year_month->year ||
			$cached_value[0]->month !== $current_year_month->month )
		) {
			array_unshift( $cached_value, $current_year_month );
		}
		return $cached_value;
	}

	$orders_table = esc_sql( OrdersTableDataStore::get_orders_table_name() );
	$utc_offset   = wc_timezone_offset();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$order_dates = $wpdb->get_results(
		$wpdb->prepare(
			"
				SELECT DISTINCT YEAR( t.date_created_local ) AS year,
								MONTH( t.date_created_local ) AS month
				FROM ( SELECT DATE_ADD( date_created_gmt, INTERVAL $utc_offset SECOND ) AS date_created_local FROM $orders_table WHERE type = %s AND status != 'trash' ) t
				ORDER BY year DESC, month DESC
			",
			$this->order_type
		)
	);

	update_option( $cache_option_date_name, time() );
	update_option( $cache_option_value_name, $order_dates );

	return $order_dates;
}