list = $quote_list; add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Register routes. */ public function register_routes() { register_rest_route( self::NAMESPACE, '/list', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_list' ), 'permission_callback' => array( $this, 'check_nonce' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'add_item' ), 'permission_callback' => array( $this, 'check_nonce' ), 'args' => array( 'product_id' => array( 'required' => true, 'type' => 'integer', ), 'variation_id' => array( 'type' => 'integer', 'default' => 0, ), 'quantity' => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1, ), 'attributes' => array( 'type' => 'object', 'default' => array(), ), ), ), ) ); register_rest_route( self::NAMESPACE, '/list/(?P[a-f0-9]{32})', array( array( 'methods' => 'PATCH', 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'check_nonce' ), 'args' => array( 'quantity' => array( 'required' => true, 'type' => 'integer', 'minimum' => 0, ), ), ), array( 'methods' => 'DELETE', 'callback' => array( $this, 'remove_item' ), 'permission_callback' => array( $this, 'check_nonce' ), ), ) ); } /** * Public endpoints, protected by the wp_rest nonce (works for guests too). * * @param \WP_REST_Request $request Request. * @return bool */ public function check_nonce( $request ) { return (bool) wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ); } /** * GET /list. * * @return \WP_REST_Response */ public function get_list() { return rest_ensure_response( $this->list_payload() ); } /** * POST /list. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function add_item( $request ) { $attributes = array(); foreach ( (array) $request['attributes'] as $name => $value ) { if ( 0 === strpos( $name, 'attribute_' ) ) { $attributes[ sanitize_title( $name ) ] = sanitize_text_field( $value ); } } $result = $this->list->add_item( $request['product_id'], $request['quantity'], $request['variation_id'], $attributes ); return new \WP_REST_Response( array_merge( array( 'result' => $result, 'message' => QuoteList::result_message( $result ), ), $this->list_payload() ), 'invalid' === $result ? 400 : 200 ); } /** * PATCH /list/{key}. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function update_item( $request ) { if ( ! $this->list->update_quantity( $request['key'], $request['quantity'] ) ) { return new \WP_Error( 'bbquote_not_found', __( 'Item not found.', 'boring-quote' ), array( 'status' => 404 ) ); } return rest_ensure_response( $this->list_payload() ); } /** * DELETE /list/{key}. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function remove_item( $request ) { if ( ! $this->list->remove_item( $request['key'] ) ) { return new \WP_Error( 'bbquote_not_found', __( 'Item not found.', 'boring-quote' ), array( 'status' => 404 ) ); } return rest_ensure_response( $this->list_payload() ); } /** * Standard list payload. * * @return array */ protected function list_payload() { $items = array(); foreach ( $this->list->get_items() as $key => $item ) { $product = QuoteList::item_product( $item ); if ( ! $product ) { continue; } $items[] = array( 'key' => $key, 'name' => wp_strip_all_tags( $product->get_name() ), 'quantity' => (int) $item['quantity'], 'permalink' => $product->get_permalink(), ); } return array( 'count' => count( $items ), 'items' => $items, 'page_url' => Plugin::get_page_url(), ); } }