Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators
Controller::get_analytics_report_data
Get analytics report data and endpoints.
Метод класса: Controller{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
// private - только в коде основоного (родительского) класса $result = $this->get_analytics_report_data();
Код Controller::get_analytics_report_data() Controller::get analytics report data WC 10.8.1
private function get_analytics_report_data() {
$request = new \WP_REST_Request( 'GET', '/wc-analytics/reports' );
/**
* Performance hack to strip the `rel=self` link from the report response as it is built by the Reports/Controller
* to avoid the expensive calls to WP_REST_Server::get_target_hints_for_link().
*
* @param WP_REST_Response $response The response object.
*
* @return mixed
*/
$remove_self_link_from_prepared_internal_response = function ( $response ) {
if ( is_callable( array( $response, 'remove_link' ) ) ) {
$response->remove_link( 'self' );
}
return $response;
};
add_filter( 'woocommerce_rest_prepare_report', $remove_self_link_from_prepared_internal_response );
$response = rest_do_request( $request );
remove_filter( 'woocommerce_rest_prepare_report', $remove_self_link_from_prepared_internal_response );
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== $response->get_status() ) {
return new \WP_Error( 'woocommerce_analytics_performance_indicators_result_failed', __( 'Sorry, fetching performance indicators failed.', 'woocommerce' ) );
}
$endpoints = $response->get_data();
foreach ( $endpoints as $endpoint ) {
if ( '/stats' === substr( $endpoint['slug'], -6 ) ) {
$request = new \WP_REST_Request( 'OPTIONS', $endpoint['path'] );
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
return $response;
}
$data = $response->get_data();
$prefix = substr( $endpoint['slug'], 0, -6 );
if ( empty( $data['schema']['properties']['totals']['properties'] ) ) {
continue;
}
foreach ( $data['schema']['properties']['totals']['properties'] as $property_key => $schema_info ) {
if ( empty( $schema_info['indicator'] ) || ! $schema_info['indicator'] ) {
continue;
}
$stat = $prefix . '/' . $property_key;
$this->allowed_stats[] = $stat;
$stat_label = empty( $schema_info['title'] ) ? $schema_info['description'] : $schema_info['title'];
$this->labels[ $stat ] = trim( $stat_label, '.' );
$this->formats[ $stat ] = isset( $schema_info['format'] ) ? $schema_info['format'] : 'number';
}
$this->endpoints[ $prefix ] = $endpoint['path'];
$this->urls[ $prefix ] = $endpoint['_links']['report'][0]['href'];
}
}
}