Automattic\WooCommerce\Admin\API\Reports\Categories

DataStore{}WC 1.0

API\Reports\Categories\DataStore.

Хуков нет.

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

$DataStore = new DataStore();
// use class methods

Методы

  1. ERROR: no method name found on line `);`
  2. ERROR: no method name found on line `*`
  3. protected page_records( $data, $page_no, $items_per_page )
  4. ERROR: no method name found on line ``
  5. ERROR: no method name found on line `*`
  6. ERROR: no method name found on line ``
  7. ERROR: no method name found on line `// This also needs to be put after the term_taxonomy JOIN so that we can match the correct term name.`
  8. ERROR: no method name found on line ``
  9. protected include_extended_info( &$categories_data, $query_args )
  10. ERROR: no method name found on line ``
  11. public add_order_by_params( $query_args, 'outer', 'default_results.category_id' )
  12. public explode( '.', str_replace( '`', '', $id_cell ) )
  13. ERROR: no method name found on line ``
  14. ERROR: no method name found on line ``
  15. ERROR: no method name found on line ``
  16. public add_sql_query_params( $query_args )
  17. ERROR: no method name found on line `// Limit is left out here so that the grouping in code by PHP can be applied correctly.`
  18. ERROR: no method name found on line `*`
  19. ERROR: no method name found on line `);`
  20. ERROR: no method name found on line `*/`
  21. ERROR: no method name found on line `}`
  22. public get_included_categories( $query_args )
  23. ERROR: no method name found on line `global $wpdb;`
  24. ERROR: no method name found on line ``
  25. ERROR: no method name found on line `* @param array $query_args Query parameters.`
  26. public get_query_statement()
  27. ERROR: no method name found on line ``
  28. ERROR: no method name found on line ``
  29. protected assign_report_columns()
  30. public get_db_table_name()
  31. ERROR: no method name found on line `);`
  32. ERROR: no method name found on line `'products_count' => 'intval',`
  33. ERROR: no method name found on line ``
  34. ERROR: no method name found on line ``
  35. ERROR: no method name found on line `*`
  36. ERROR: no method name found on line `* @override ReportsDataStore::$context`
  37. public array()
  38. ERROR: no method name found on line ``
  39. ERROR: no method name found on line `*`
  40. ERROR: no method name found on line `* @var string`
  41. ERROR: no method name found on line `* @override ReportsDataStore::$context`
  42. ERROR: no method name found on line `/**`
  43. ERROR: no method name found on line `*`
  44. ERROR: no method name found on line `protected $context = 'categories';`
  45. ERROR: no method name found on line ``
  46. ERROR: no method name found on line `* @override ReportsDataStore::$table_name`
  47. ERROR: no method name found on line ``
  48. ERROR: no method name found on line ``
  49. ERROR: no method name found on line ``
  50. ERROR: no method name found on line ``
  51. ERROR: no method name found on line ``
  52. ERROR: no method name found on line ``
  53. ERROR: no method name found on line ``
  54. ERROR: no method name found on line ``
  55. ERROR: no method name found on line ``
  56. ERROR: no method name found on line ``
  57. ERROR: no method name found on line `}`
  58. ERROR: no method name found on line ``
  59. ERROR: no method name found on line ``
  60. ERROR: no method name found on line ``
  61. ERROR: no method name found on line ``
  62. ERROR: no method name found on line ``
  63. ERROR: no method name found on line ``
  64. ERROR: no method name found on line ``
  65. ERROR: no method name found on line `$categories_query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared`
  66. ERROR: no method name found on line ``
  67. public add_sql_clause( 'from', $this->subquery->get_query_statement() )
  68. public get_default_query_vars()
  69. ERROR: no method name found on line ``
  70. ERROR: no method name found on line `* @see get_data`
  71. ERROR: no method name found on line `}`
  72. ERROR: no method name found on line ``
  73. ERROR: no method name found on line `'page_no' => 0,`
  74. ERROR: no method name found on line `*/`
  75. ERROR: no method name found on line ``
  76. ERROR: no method name found on line `}`
  77. ERROR: no method name found on line ``
  78. ERROR: no method name found on line `* @override ReportsDataStore::$context`
  79. ERROR: no method name found on line `}`
  80. ERROR: no method name found on line `* @param string $id_cell ID cell identifier, like `table_name.id_column_name`.`
  81. ERROR: no method name found on line `} else {`
  82. ERROR: no method name found on line `* @param string $order_by Sorting criterion.`
  83. ERROR: no method name found on line ``
  84. ERROR: no method name found on line ``
  85. public get_fields( $query_args )
  86. ERROR: no method name found on line `$categories_data[ $key ]['extended_info'] = $extended_info;`
  87. ERROR: no method name found on line ``
  88. ERROR: no method name found on line ``
  89. ERROR: no method name found on line ``
  90. ERROR: no method name found on line ``
  91. ERROR: no method name found on line ``
  92. ERROR: no method name found on line ``
  93. ERROR: no method name found on line ``
  94. ERROR: no method name found on line ``
  95. ERROR: no method name found on line ``
  96. ERROR: no method name found on line `*/`
  97. ERROR: no method name found on line ``
  98. ERROR: no method name found on line ``
  99. ERROR: no method name found on line ``
  100. protected get_included_categories_array( $query_args )
  101. ERROR: no method name found on line ``
  102. ERROR: no method name found on line `'total' => 0,`
  103. ERROR: no method name found on line `*`
  104. ERROR: no method name found on line ``
  105. ERROR: no method name found on line ``
  106. ERROR: no method name found on line ``
  107. ERROR: no method name found on line ``
  108. ERROR: no method name found on line ``
  109. ERROR: no method name found on line ``
  110. ERROR: no method name found on line ``
  111. ERROR: no method name found on line ``
  112. ERROR: no method name found on line `*`
  113. ERROR: no method name found on line ``
  114. ERROR: no method name found on line ``
  115. public get_noncached_data( $query_args )
  116. public JOIN ( {$ids_table} )
  117. ERROR: no method name found on line ``
  118. ERROR: no method name found on line `ARRAY_A`
  119. ERROR: no method name found on line ``
  120. ERROR: no method name found on line ``
  121. ERROR: no method name found on line ``
  122. ERROR: no method name found on line ``
  123. public array_map( array( $this, 'cast_numbers' ), $categories_data )
  124. ERROR: no method name found on line `*/`
  125. ERROR: no method name found on line ``
  126. ERROR: no method name found on line `* Maps ordering specified by the user to columns in the database/fields in the data.`
  127. ERROR: no method name found on line `* @param array $query_args Parameters supplied by the user.`
  128. ERROR: no method name found on line `/**`
  129. ERROR: no method name found on line `}`
  130. ERROR: no method name found on line ``
  131. ERROR: no method name found on line ``
  132. ERROR: no method name found on line ``
  133. ERROR: no method name found on line ``
  134. ERROR: no method name found on line ``
  135. ERROR: no method name found on line ``
  136. ERROR: no method name found on line ``
  137. ERROR: no method name found on line ``
  138. ERROR: no method name found on line ``
  139. ERROR: no method name found on line ``
  140. ERROR: no method name found on line ``
  141. ERROR: no method name found on line ``
  142. public implode( '`.`', $id_cell_segments )
  143. ERROR: no method name found on line ``
  144. ERROR: no method name found on line ``
  145. ERROR: no method name found on line ``
  146. ERROR: no method name found on line ``
  147. ERROR: no method name found on line ``
  148. ERROR: no method name found on line ``
  149. ERROR: no method name found on line ``
  150. ERROR: no method name found on line ``
  151. protected initialize_queries()
  152. ERROR: no method name found on line `global $wpdb;`
  153. public SUM(product_qty)
  154. ERROR: no method name found on line `// @todo How to handle custom product tables?`
  155. ERROR: no method name found on line ``
  156. ERROR: no method name found on line ``
  157. ERROR: no method name found on line ``
  158. ERROR: no method name found on line ``
  159. ERROR: no method name found on line ``
  160. ERROR: no method name found on line ``
  161. ERROR: no method name found on line ``
  162. ERROR: no method name found on line ``
  163. ERROR: no method name found on line ``
  164. ERROR: no method name found on line ``
  165. ERROR: no method name found on line ``
  166. ERROR: no method name found on line ``
  167. ERROR: no method name found on line ``
  168. protected normalize_order_by( $order_by )
  169. ERROR: no method name found on line ``
  170. ERROR: no method name found on line `'page_no' => 0,`
  171. ERROR: no method name found on line `$offset = ( $page_no - 1 ) * $items_per_page;`
  172. ERROR: no method name found on line ``
  173. ERROR: no method name found on line `* Returns the page of data according to page number and items per page.`
  174. ERROR: no method name found on line `return $defaults;`
  175. ERROR: no method name found on line `return $order_by;`
  176. ERROR: no method name found on line ``
  177. public if ( false !== strpos( $order_by_clause, '_terms' ) )
  178. ERROR: no method name found on line ``
  179. ERROR: no method name found on line ``
  180. ERROR: no method name found on line ``
  181. ERROR: no method name found on line ``
  182. ERROR: no method name found on line `* Data store context used to pass to filters.`
  183. public get_data()
  184. ERROR: no method name found on line ``
  185. ERROR: no method name found on line ``
  186. ERROR: no method name found on line ``
  187. ERROR: no method name found on line ``
  188. public ceil( $record_count / $query_args['per_page'] )
  189. ERROR: no method name found on line ``
  190. ERROR: no method name found on line ``
  191. ERROR: no method name found on line ``
  192. ERROR: no method name found on line ``
  193. ERROR: no method name found on line `/**`
  194. ERROR: no method name found on line ``
  195. ERROR: no method name found on line `}`
  196. ERROR: no method name found on line ``
  197. ERROR: no method name found on line ``
  198. ERROR: no method name found on line `// determines the order in which joins are concatenated.`
  199. ERROR: no method name found on line ``
  200. ERROR: no method name found on line ``
  201. public add_order_by_clause( $query_args, $this )
  202. ERROR: no method name found on line ``
  203. ERROR: no method name found on line ``
  204. ERROR: no method name found on line ``
  205. ERROR: no method name found on line ``
  206. ERROR: no method name found on line `*/`
  207. ERROR: no method name found on line `protected $context = 'categories';`
  208. public COUNT(DISTINCT {$table_name}.order_id)

Код DataStore{} WC 9.8.1

class DataStore extends ReportsDataStore implements DataStoreInterface {

	/**
	 * Table used to get the data.
	 *
	 * @override ReportsDataStore::$table_name
	 *
	 * @var string
	 */
	protected static $table_name = 'wc_order_product_lookup';

	/**
	 * Cache identifier.
	 *
	 * @override ReportsDataStore::$cache_key
	 *
	 * @var string
	 */
	protected $cache_key = 'categories';

	/**
	 * Order by setting used for sorting categories data.
	 *
	 * @var string
	 */
	private $order_by = '';

	/**
	 * Order setting used for sorting categories data.
	 *
	 * @var string
	 */
	private $order = '';

	/**
	 * Mapping columns to data type to return correct response types.
	 *
	 * @override ReportsDataStore::$column_types
	 *
	 * @var array
	 */
	protected $column_types = array(
		'category_id'    => 'intval',
		'items_sold'     => 'intval',
		'net_revenue'    => 'floatval',
		'orders_count'   => 'intval',
		'products_count' => 'intval',
	);

	/**
	 * Data store context used to pass to filters.
	 *
	 * @override ReportsDataStore::$context
	 *
	 * @var string
	 */
	protected $context = 'categories';

	/**
	 * Assign report columns once full table name has been assigned.
	 *
	 * @override ReportsDataStore::assign_report_columns()
	 */
	protected function assign_report_columns() {
		$table_name           = self::get_db_table_name();
		$this->report_columns = array(
			'items_sold'     => 'SUM(product_qty) as items_sold',
			'net_revenue'    => 'SUM(product_net_revenue) AS net_revenue',
			'orders_count'   => "COUNT(DISTINCT {$table_name}.order_id) as orders_count",
			'products_count' => "COUNT(DISTINCT {$table_name}.product_id) as products_count",
		);
	}

	/**
	 * Return the database query with parameters used for Categories report: time span and order status.
	 *
	 * @param array $query_args Query arguments supplied by the user.
	 */
	protected function add_sql_query_params( $query_args ) {
		global $wpdb;
		$order_product_lookup_table = self::get_db_table_name();

		$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );

		// join wp_order_product_lookup_table with relationships and taxonomies
		// @todo How to handle custom product tables?
		$this->subquery->add_sql_clause( 'left_join', "LEFT JOIN {$wpdb->term_relationships} ON {$order_product_lookup_table}.product_id = {$wpdb->term_relationships}.object_id" );
		// Adding this (inner) JOIN as a LEFT JOIN for ordering purposes. See comment in add_order_by_params().
		$this->subquery->add_sql_clause( 'left_join', "JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id" );

		$included_categories = $this->get_included_categories( $query_args );
		if ( $included_categories ) {
			$this->subquery->add_sql_clause( 'where', "AND {$wpdb->term_relationships}.term_taxonomy_id IN ({$included_categories})" );

			// Limit is left out here so that the grouping in code by PHP can be applied correctly.
			// This also needs to be put after the term_taxonomy JOIN so that we can match the correct term name.
			$this->add_order_by_params( $query_args, 'outer', 'default_results.category_id' );
		} else {
			$this->add_order_by_params( $query_args, 'inner', "{$wpdb->term_relationships}.term_taxonomy_id" );
		}

		$this->add_order_status_clause( $query_args, $order_product_lookup_table, $this->subquery );
		$this->subquery->add_sql_clause( 'where', "AND {$wpdb->term_taxonomy}.taxonomy = 'product_cat'" );
	}

	/**
	 * Fills ORDER BY clause of SQL request based on user supplied parameters.
	 *
	 * @param array  $query_args Parameters supplied by the user.
	 * @param string $from_arg   Target of the JOIN sql param.
	 * @param string $id_cell    ID cell identifier, like `table_name.id_column_name`.
	 */
	protected function add_order_by_params( $query_args, $from_arg, $id_cell ) {
		global $wpdb;

		// Sanitize input: guarantee that the id cell in the join is quoted with backticks.
		$id_cell_segments   = explode( '.', str_replace( '`', '', $id_cell ) );
		$id_cell_identifier = '`' . implode( '`.`', $id_cell_segments ) . '`';

		$lookup_table    = self::get_db_table_name();
		$order_by_clause = $this->add_order_by_clause( $query_args, $this );
		$this->add_orderby_order_clause( $query_args, $this );

		if ( false !== strpos( $order_by_clause, '_terms' ) ) {
			$join = "JOIN {$wpdb->terms} AS _terms ON {$id_cell_identifier} = _terms.term_id";
			if ( 'inner' === $from_arg ) {
				// Even though this is an (inner) JOIN, we're adding it as a `left_join` to
				// affect its order in the query statement. The SqlQuery::$sql_filters variable
				// determines the order in which joins are concatenated.
				// See: https://github.com/woocommerce/woocommerce-admin/blob/1f261998e7287b77bc13c3d4ee2e84b717da7957/src/API/Reports/SqlQuery.php#L46-L50.
				$this->subquery->add_sql_clause( 'left_join', $join );
			} else {
				$this->add_sql_clause( 'join', $join );
			}
		}
	}

	/**
	 * Maps ordering specified by the user to columns in the database/fields in the data.
	 *
	 * @override ReportsDataStore::normalize_order_by()
	 *
	 * @param string $order_by Sorting criterion.
	 * @return string
	 */
	protected function normalize_order_by( $order_by ) {
		if ( 'date' === $order_by ) {
			return 'time_interval';
		}
		if ( 'category' === $order_by ) {
			return '_terms.name';
		}
		return $order_by;
	}

	/**
	 * Returns an array of ids of included categories, based on query arguments from the user.
	 *
	 * @param array $query_args Parameters supplied by the user.
	 * @return array
	 */
	protected function get_included_categories_array( $query_args ) {
		if ( isset( $query_args['category_includes'] ) && is_array( $query_args['category_includes'] ) && count( $query_args['category_includes'] ) > 0 ) {
			return $query_args['category_includes'];
		}
		return array();
	}

	/**
	 * Returns the page of data according to page number and items per page.
	 *
	 * @param array   $data           Data to paginate.
	 * @param integer $page_no        Page number.
	 * @param integer $items_per_page Number of items per page.
	 * @return array
	 */
	protected function page_records( $data, $page_no, $items_per_page ) {
		$offset = ( $page_no - 1 ) * $items_per_page;
		return array_slice( $data, $offset, $items_per_page );
	}

	/**
	 * Enriches the category data.
	 *
	 * @param array $categories_data Categories data.
	 * @param array $query_args  Query parameters.
	 */
	protected function include_extended_info( &$categories_data, $query_args ) {
		foreach ( $categories_data as $key => $category_data ) {
			$extended_info = new \ArrayObject();
			if ( $query_args['extended_info'] ) {
				$extended_info['name'] = get_the_category_by_ID( $category_data['category_id'] );
			}
			$categories_data[ $key ]['extended_info'] = $extended_info;
		}
	}

	/**
	 * Get the default query arguments to be used by get_data().
	 * These defaults are only partially applied when used via REST API, as that has its own defaults.
	 *
	 * @override ReportsDataStore::get_default_query_vars()
	 *
	 * @return array Query parameters.
	 */
	public function get_default_query_vars() {
		$defaults                      = parent::get_default_query_vars();
		$defaults['category_includes'] = array();
		$defaults['extended_info']     = false;

		return $defaults;
	}

	/**
	 * Returns the report data based on normalized parameters.
	 * Will be called by `get_data` if there is no data in cache.
	 *
	 * @see get_data
	 * @override ReportsDataStore::get_noncached_data()
	 *
	 * @param array $query_args Query parameters.
	 * @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
	 */
	public function get_noncached_data( $query_args ) {
		global $wpdb;

		$table_name = self::get_db_table_name();
		$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'],
		);

		return $data;
	}

	/**
	 * Initialize query objects.
	 *
	 * @override ReportsDataStore::initialize_queries()
	 */
	protected function initialize_queries() {
		global $wpdb;
		$this->subquery = new SqlQuery( $this->context . '_subquery' );
		$this->subquery->add_sql_clause( 'select', "{$wpdb->term_taxonomy}.term_id as category_id," );
		$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
		$this->subquery->add_sql_clause( 'group_by', "{$wpdb->term_taxonomy}.term_id" );
	}
}