WC_API_Server::serve_request PHP Method

serve_request() public method

Matches the current server URI to a route and runs the first matching callback then outputs a JSON representation of the returned value.
Since: 2.1
public serve_request ( )
    public function serve_request()
    {
        do_action('woocommerce_api_server_before_serve', $this);
        $this->header('Content-Type', $this->handler->get_content_type(), true);
        // the API is enabled by default
        if (!apply_filters('woocommerce_api_enabled', true, $this) || 'no' === get_option('woocommerce_api_enabled')) {
            $this->send_status(404);
            echo $this->handler->generate_response(array('errors' => array('code' => 'woocommerce_api_disabled', 'message' => 'The WooCommerce API is disabled on this site')));
            return;
        }
        $result = $this->check_authentication();
        // if authorization check was successful, dispatch the request
        if (!is_wp_error($result)) {
            $result = $this->dispatch();
        }
        // handle any dispatch errors
        if (is_wp_error($result)) {
            $data = $result->get_error_data();
            if (is_array($data) && isset($data['status'])) {
                $this->send_status($data['status']);
            }
            $result = $this->error_to_array($result);
        }
        // This is a filter rather than an action, since this is designed to be
        // re-entrant if needed
        $served = apply_filters('woocommerce_api_serve_request', false, $result, $this);
        if (!$served) {
            if ('HEAD' === $this->method) {
                return;
            }
            echo $this->handler->generate_response($result);
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Handle legacy v2 REST API requests.
  *
  * @since 2.4
  * @deprecated 2.6.0
  */
 private function handle_v2_rest_api_request()
 {
     include_once 'api/legacy/v2/class-wc-api-exception.php';
     include_once 'api/legacy/v2/class-wc-api-server.php';
     include_once 'api/legacy/v2/interface-wc-api-handler.php';
     include_once 'api/legacy/v2/class-wc-api-json-handler.php';
     include_once 'api/legacy/v2/class-wc-api-authentication.php';
     $this->authentication = new WC_API_Authentication();
     include_once 'api/legacy/v2/class-wc-api-resource.php';
     include_once 'api/legacy/v2/class-wc-api-coupons.php';
     include_once 'api/legacy/v2/class-wc-api-customers.php';
     include_once 'api/legacy/v2/class-wc-api-orders.php';
     include_once 'api/legacy/v2/class-wc-api-products.php';
     include_once 'api/legacy/v2/class-wc-api-reports.php';
     include_once 'api/legacy/v2/class-wc-api-webhooks.php';
     // allow plugins to load other response handlers or resource classes.
     do_action('woocommerce_api_loaded');
     $this->server = new WC_API_Server($GLOBALS['wp']->query_vars['wc-api-route']);
     // Register available resources for legacy v2 REST API request.
     $api_classes = apply_filters('woocommerce_api_classes', array('WC_API_Customers', 'WC_API_Orders', 'WC_API_Products', 'WC_API_Coupons', 'WC_API_Reports', 'WC_API_Webhooks'));
     foreach ($api_classes as $api_class) {
         $this->{$api_class} = new $api_class($this->server);
     }
     // Fire off the request.
     $this->server->serve_request();
 }