Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin/API/Reports
File: GenericStatsController.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Admin\API\Reports;
[1] Fix | Delete
[2] Fix | Delete
defined( 'ABSPATH' ) || exit;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
[5] Fix | Delete
use WP_Error;
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Generic base for all stats controllers.
[9] Fix | Delete
*
[10] Fix | Delete
* {@see GenericController Generic Controller} extended to be shared as a generic base for all Analytics stats controllers.
[11] Fix | Delete
*
[12] Fix | Delete
* Besides the `GenericController` functionality, it adds conventional stats-specific collection params and item schema.
[13] Fix | Delete
* So, you may want to extend only your report-specific {@see get_item_properties_schema() get_item_properties_schema()}`.
[14] Fix | Delete
* It also uses the stats-specific {@see get_items() get_items()} method,
[15] Fix | Delete
* which packs report data into `totals` and `intervals`.
[16] Fix | Delete
*
[17] Fix | Delete
*
[18] Fix | Delete
* Minimalistic example:
[19] Fix | Delete
* <pre><code class="language-php">class StatsController extends GenericStatsController {
[20] Fix | Delete
* /** Route of your new REST endpoint. &ast;/
[21] Fix | Delete
* protected $rest_base = 'reports/my-thing/stats';
[22] Fix | Delete
* /** Define your proeprties schema. &ast;/
[23] Fix | Delete
* protected function get_item_properties_schema() {
[24] Fix | Delete
* return array(
[25] Fix | Delete
* 'my_property' => array(
[26] Fix | Delete
* 'title' => __( 'My property', 'my-extension' ),
[27] Fix | Delete
* 'type' => 'integer',
[28] Fix | Delete
* 'readonly' => true,
[29] Fix | Delete
* 'context' => array( 'view', 'edit' ),
[30] Fix | Delete
* 'description' => __( 'Amazing thing.', 'my-extension' ),
[31] Fix | Delete
* 'indicator' => true,
[32] Fix | Delete
* ),
[33] Fix | Delete
* );
[34] Fix | Delete
* }
[35] Fix | Delete
* /** Define overall schema. You can use the defaults,
[36] Fix | Delete
* * just remember to provide your title and call `add_additional_fields_schema`
[37] Fix | Delete
* * to run the filters
[38] Fix | Delete
* &ast;/
[39] Fix | Delete
* public function get_item_schema() {
[40] Fix | Delete
* $schema = parent::get_item_schema();
[41] Fix | Delete
* $schema['title'] = 'report_my_thing_stats';
[42] Fix | Delete
*
[43] Fix | Delete
* return $this->add_additional_fields_schema( $schema );
[44] Fix | Delete
* }
[45] Fix | Delete
* }
[46] Fix | Delete
* </code></pre>
[47] Fix | Delete
*
[48] Fix | Delete
* @extends GenericController
[49] Fix | Delete
*/
[50] Fix | Delete
abstract class GenericStatsController extends GenericController {
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Get the query params definition for collections.
[54] Fix | Delete
* Adds `fields` & `intervals` to the generic list.
[55] Fix | Delete
*
[56] Fix | Delete
* @override GenericController::get_collection_params()
[57] Fix | Delete
*
[58] Fix | Delete
* @return array
[59] Fix | Delete
*/
[60] Fix | Delete
public function get_collection_params() {
[61] Fix | Delete
$params = parent::get_collection_params();
[62] Fix | Delete
$params['fields'] = array(
[63] Fix | Delete
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
[64] Fix | Delete
'type' => 'array',
[65] Fix | Delete
'sanitize_callback' => 'wp_parse_slug_list',
[66] Fix | Delete
'validate_callback' => 'rest_validate_request_arg',
[67] Fix | Delete
'items' => array(
[68] Fix | Delete
'type' => 'string',
[69] Fix | Delete
),
[70] Fix | Delete
);
[71] Fix | Delete
$params['interval'] = array(
[72] Fix | Delete
'description' => __( 'Time interval to use for buckets in the returned data.', 'woocommerce' ),
[73] Fix | Delete
'type' => 'string',
[74] Fix | Delete
'default' => 'week',
[75] Fix | Delete
'enum' => array(
[76] Fix | Delete
'hour',
[77] Fix | Delete
'day',
[78] Fix | Delete
'week',
[79] Fix | Delete
'month',
[80] Fix | Delete
'quarter',
[81] Fix | Delete
'year',
[82] Fix | Delete
),
[83] Fix | Delete
'validate_callback' => 'rest_validate_request_arg',
[84] Fix | Delete
);
[85] Fix | Delete
[86] Fix | Delete
return $params;
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
/**
[90] Fix | Delete
* Get the report's item properties schema.
[91] Fix | Delete
* Will be used by `get_item_schema` as `totals` and `subtotals`.
[92] Fix | Delete
*
[93] Fix | Delete
* @return array
[94] Fix | Delete
*/
[95] Fix | Delete
abstract protected function get_item_properties_schema();
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Get the Report's schema, conforming to JSON Schema.
[99] Fix | Delete
*
[100] Fix | Delete
* Please note that it does not call add_additional_fields_schema,
[101] Fix | Delete
* as you may want to update the `title` first.
[102] Fix | Delete
*
[103] Fix | Delete
* @return array
[104] Fix | Delete
*/
[105] Fix | Delete
public function get_item_schema() {
[106] Fix | Delete
$data_values = $this->get_item_properties_schema();
[107] Fix | Delete
[108] Fix | Delete
$segments = array(
[109] Fix | Delete
'segments' => array(
[110] Fix | Delete
'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ),
[111] Fix | Delete
'type' => 'array',
[112] Fix | Delete
'context' => array( 'view', 'edit' ),
[113] Fix | Delete
'readonly' => true,
[114] Fix | Delete
'items' => array(
[115] Fix | Delete
'type' => 'object',
[116] Fix | Delete
'properties' => array(
[117] Fix | Delete
'segment_id' => array(
[118] Fix | Delete
'description' => __( 'Segment identificator.', 'woocommerce' ),
[119] Fix | Delete
'type' => 'integer',
[120] Fix | Delete
'context' => array( 'view', 'edit' ),
[121] Fix | Delete
'readonly' => true,
[122] Fix | Delete
),
[123] Fix | Delete
'subtotals' => array(
[124] Fix | Delete
'description' => __( 'Interval subtotals.', 'woocommerce' ),
[125] Fix | Delete
'type' => 'object',
[126] Fix | Delete
'context' => array( 'view', 'edit' ),
[127] Fix | Delete
'readonly' => true,
[128] Fix | Delete
'properties' => $data_values,
[129] Fix | Delete
),
[130] Fix | Delete
),
[131] Fix | Delete
),
[132] Fix | Delete
),
[133] Fix | Delete
);
[134] Fix | Delete
[135] Fix | Delete
$totals = array_merge( $data_values, $segments );
[136] Fix | Delete
[137] Fix | Delete
return array(
[138] Fix | Delete
'$schema' => 'http://json-schema.org/draft-04/schema#',
[139] Fix | Delete
'title' => 'report_stats',
[140] Fix | Delete
'type' => 'object',
[141] Fix | Delete
'properties' => array(
[142] Fix | Delete
'totals' => array(
[143] Fix | Delete
'description' => __( 'Totals data.', 'woocommerce' ),
[144] Fix | Delete
'type' => 'object',
[145] Fix | Delete
'context' => array( 'view', 'edit' ),
[146] Fix | Delete
'readonly' => true,
[147] Fix | Delete
'properties' => $totals,
[148] Fix | Delete
),
[149] Fix | Delete
'intervals' => array(
[150] Fix | Delete
'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ),
[151] Fix | Delete
'type' => 'array',
[152] Fix | Delete
'context' => array( 'view', 'edit' ),
[153] Fix | Delete
'readonly' => true,
[154] Fix | Delete
'items' => array(
[155] Fix | Delete
'type' => 'object',
[156] Fix | Delete
'properties' => array(
[157] Fix | Delete
'interval' => array(
[158] Fix | Delete
'description' => __( 'Type of interval.', 'woocommerce' ),
[159] Fix | Delete
'type' => 'string',
[160] Fix | Delete
'context' => array( 'view', 'edit' ),
[161] Fix | Delete
'readonly' => true,
[162] Fix | Delete
'enum' => array( 'day', 'week', 'month', 'year' ),
[163] Fix | Delete
),
[164] Fix | Delete
'date_start' => array(
[165] Fix | Delete
'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ),
[166] Fix | Delete
'type' => 'string',
[167] Fix | Delete
'format' => 'date-time',
[168] Fix | Delete
'context' => array( 'view', 'edit' ),
[169] Fix | Delete
'readonly' => true,
[170] Fix | Delete
),
[171] Fix | Delete
'date_start_gmt' => array(
[172] Fix | Delete
'description' => __( 'The date the report start, as GMT.', 'woocommerce' ),
[173] Fix | Delete
'type' => 'string',
[174] Fix | Delete
'format' => 'date-time',
[175] Fix | Delete
'context' => array( 'view', 'edit' ),
[176] Fix | Delete
'readonly' => true,
[177] Fix | Delete
),
[178] Fix | Delete
'date_end' => array(
[179] Fix | Delete
'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ),
[180] Fix | Delete
'type' => 'string',
[181] Fix | Delete
'format' => 'date-time',
[182] Fix | Delete
'context' => array( 'view', 'edit' ),
[183] Fix | Delete
'readonly' => true,
[184] Fix | Delete
),
[185] Fix | Delete
'date_end_gmt' => array(
[186] Fix | Delete
'description' => __( 'The date the report end, as GMT.', 'woocommerce' ),
[187] Fix | Delete
'type' => 'string',
[188] Fix | Delete
'format' => 'date-time',
[189] Fix | Delete
'context' => array( 'view', 'edit' ),
[190] Fix | Delete
'readonly' => true,
[191] Fix | Delete
),
[192] Fix | Delete
'subtotals' => array(
[193] Fix | Delete
'description' => __( 'Interval subtotals.', 'woocommerce' ),
[194] Fix | Delete
'type' => 'object',
[195] Fix | Delete
'context' => array( 'view', 'edit' ),
[196] Fix | Delete
'readonly' => true,
[197] Fix | Delete
'properties' => $totals,
[198] Fix | Delete
),
[199] Fix | Delete
),
[200] Fix | Delete
),
[201] Fix | Delete
),
[202] Fix | Delete
),
[203] Fix | Delete
);
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/**
[207] Fix | Delete
* Get the report data.
[208] Fix | Delete
*
[209] Fix | Delete
* Prepares query params, fetches the report data from the data store,
[210] Fix | Delete
* prepares it for the response, and packs it into the convention-conforming response object.
[211] Fix | Delete
*
[212] Fix | Delete
* @override GenericController::get_items()
[213] Fix | Delete
*
[214] Fix | Delete
* @throws \WP_Error When the queried data is invalid.
[215] Fix | Delete
* @param \WP_REST_Request $request Request data.
[216] Fix | Delete
* @return \WP_REST_Response|\WP_Error
[217] Fix | Delete
*/
[218] Fix | Delete
public function get_items( $request ) {
[219] Fix | Delete
$query_args = $this->prepare_reports_query( $request );
[220] Fix | Delete
try {
[221] Fix | Delete
$report_data = $this->get_datastore_data( $query_args );
[222] Fix | Delete
} catch ( ParameterException $e ) {
[223] Fix | Delete
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
$out_data = array(
[227] Fix | Delete
'totals' => $report_data->totals ? get_object_vars( $report_data->totals ) : null,
[228] Fix | Delete
'intervals' => array(),
[229] Fix | Delete
);
[230] Fix | Delete
[231] Fix | Delete
foreach ( $report_data->intervals as $interval_data ) {
[232] Fix | Delete
$item = $this->prepare_item_for_response( $interval_data, $request );
[233] Fix | Delete
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
return $this->add_pagination_headers(
[237] Fix | Delete
$request,
[238] Fix | Delete
$out_data,
[239] Fix | Delete
(int) $report_data->total,
[240] Fix | Delete
(int) $report_data->page_no,
[241] Fix | Delete
(int) $report_data->pages
[242] Fix | Delete
);
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function