Automattic\WooCommerce\Admin\API
ActivityPanelCounts::get_count_via
Run one of the existing count endpoints internally and read its total off the response, so the counting logic itself (query building, permission checks) isn't duplicated here.
Method of the class: ActivityPanelCounts{}
No Hooks.
Returns
int|null. Null when the sub-request failed, so callers can tell "unknown" apart from a real zero count.
Usage
// private - for code of main (parent) class only $result = $this->get_count_via( $route, $params );
- $route(string) (required)
- REST route to call, e.g.
'/wc-analytics/orders'. - $params(array) (required)
- Query params for the sub-request.
ActivityPanelCounts::get_count_via() ActivityPanelCounts::get count via code WC 11.3.0
private function get_count_via( $route, $params ) {
$sub_request = new \WP_REST_Request( 'GET', $route );
foreach ( $params as $key => $value ) {
$sub_request->set_param( $key, $value );
}
$response = rest_do_request( $sub_request );
if ( $response->is_error() ) {
wc_get_logger()->warning(
sprintf( 'Activity Panel counts sub-request to %s failed.', $route ),
array( 'source' => 'activity-panel-counts' )
);
return null;
}
$headers = $response->get_headers();
if ( isset( $headers['X-WP-Total'] ) ) {
return (int) $headers['X-WP-Total'];
}
$data = $response->get_data();
return isset( $data['total'] ) ? (int) $data['total'] : null;
}