query_vars['adscaleCategoriesCount'] = sanitize_key( wp_unslash( $_GET['adscaleCategoriesCount'] ) ); } // make sure endpoint request if ( ! isset( $wp->query_vars['adscaleCategoriesCount'] ) ) { return; } try { ob_start(); // Error handler self::setErrorHandler(); Helper::nocache_headers(); // Check Request (Guards) self::checkRequest(); ob_end_clean(); // Handle Request self::handleRequest(); // Default error Helper::send_response_formatted_error( self::makeErrorBody() ); } catch ( PluginApiException $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getApiErrorCode(), $ex->getMessage(), $ex->getApiErrorDetails() ) ); } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getCode(), $ex->getMessage() ) ); } } /** * Handle API request * */ public static function handleRequest() { $args = [ 'taxonomy' => [ 'product_cat' ], 'hide_empty' => false, 'fields' => 'count', ]; $terms_count = get_terms( $args ); if ( is_wp_error( $terms_count ) ) { Helper::send_response_formatted_error( self::makeErrorBody('S000','Unknown error', 'A product categories count error' ) ); } Helper::send_response_formatted_success( [ 'count' => (int) $terms_count, ] ); } }