Automattic\WooCommerce\Admin\API\Reports\Categories

DataStore::get_data()publicWC 1.0

Returns the report data based on parameters supplied by the user.

Method of the class: DataStore{}

No Hooks.

Return

stdClass|WP_Error. Data.

Usage

$DataStore = new DataStore();
$DataStore->get_data( $query_args );
$query_args(array) (required)
Query parameters.

DataStore::get_data() code WC 8.7.0

public function get_data( $query_args ) {
	global $wpdb;

	$table_name = self::get_db_table_name();

	// These defaults are only partially applied when used via REST API, as that has its own defaults.
	$defaults   = array(
		'per_page'          => get_option( 'posts_per_page' ),
		'page'              => 1,
		'order'             => 'DESC',
		'orderby'           => 'date',
		'before'            => TimeInterval::default_before(),
		'after'             => TimeInterval::default_after(),
		'fields'            => '*',
		'category_includes' => array(),
		'extended_info'     => false,
	);
	$query_args = wp_parse_args( $query_args, $defaults );
	$this->normalize_timezones( $query_args, $defaults );

	/*
	 * We need to get the cache key here because
	 * parent::update_intervals_sql_params() modifies $query_args.
	 */
	$cache_key = $this->get_cache_key( $query_args );
	$data      = $this->get_cached_data( $cache_key );

	if ( false === $data ) {
		$this->initialize_queries();

		$data = (object) array(
			'data'    => array(),
			'total'   => 0,
			'pages'   => 0,
			'page_no' => 0,
		);

		$this->subquery->add_sql_clause( 'select', $this->selected_columns( $query_args ) );
		$included_categories = $this->get_included_categories_array( $query_args );
		$this->add_sql_query_params( $query_args );

		if ( count( $included_categories ) > 0 ) {
			$fields    = $this->get_fields( $query_args );
			$ids_table = $this->get_ids_table( $included_categories, 'category_id' );

			$this->add_sql_clause( 'select', $this->format_join_selections( array_merge( array( 'category_id' ), $fields ), array( 'category_id' ) ) );
			$this->add_sql_clause( 'from', '(' );
			$this->add_sql_clause( 'from', $this->subquery->get_query_statement() );
			$this->add_sql_clause( 'from', ") AS {$table_name}" );
			$this->add_sql_clause(
				'right_join',
				"RIGHT JOIN ( {$ids_table} ) AS default_results
				ON default_results.category_id = {$table_name}.category_id"
			);

			$categories_query = $this->get_query_statement();
		} else {
			$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
			$categories_query = $this->subquery->get_query_statement();
		}
		$categories_data = $wpdb->get_results(
			$categories_query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			ARRAY_A
		);

		if ( null === $categories_data ) {
			return new \WP_Error( 'woocommerce_analytics_categories_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ), array( 'status' => 500 ) );
		}

		$record_count = count( $categories_data );
		$total_pages  = (int) ceil( $record_count / $query_args['per_page'] );
		if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
			return $data;
		}

		$categories_data = $this->page_records( $categories_data, $query_args['page'], $query_args['per_page'] );
		$this->include_extended_info( $categories_data, $query_args );
		$categories_data = array_map( array( $this, 'cast_numbers' ), $categories_data );
		$data            = (object) array(
			'data'    => $categories_data,
			'total'   => $record_count,
			'pages'   => $total_pages,
			'page_no' => (int) $query_args['page'],
		);

		$this->set_cached_data( $cache_key, $data );
	}

	return $data;
}