Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators

Controller::get_analytics_report_data()privateWC 1.0

Get analytics report data and endpoints.

Method of the class: Controller{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->get_analytics_report_data();

Controller::get_analytics_report_data() code WC 8.7.0

private function get_analytics_report_data() {
	$request  = new \WP_REST_Request( 'GET', '/wc-analytics/reports' );
	$response = rest_do_request( $request );

	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'];
		}
	}
}