From 02ec9db3bfa42c14a925ffa90d138155a690e2d2 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Wed, 6 Mar 2019 16:06:02 -0500 Subject: [PATCH] Remove blocks API endpoints (#477) * Remove blocks API endpoints * Remove API tests --- ...gpb-product-attribute-terms-controller.php | 182 --------- ...ass-wgpb-product-attributes-controller.php | 186 --------- ...ass-wgpb-product-categories-controller.php | 149 ------- includes/class-wgpb-products-controller.php | 386 ------------------ tests/api/products-attributes.php | 146 ------- tests/api/products-categories.php | 150 ------- tests/api/products-extra.php | 219 ---------- woocommerce-gutenberg-products-block.php | 24 -- 8 files changed, 1442 deletions(-) delete mode 100644 includes/class-wgpb-product-attribute-terms-controller.php delete mode 100644 includes/class-wgpb-product-attributes-controller.php delete mode 100644 includes/class-wgpb-product-categories-controller.php delete mode 100644 includes/class-wgpb-products-controller.php delete mode 100644 tests/api/products-attributes.php delete mode 100644 tests/api/products-categories.php delete mode 100644 tests/api/products-extra.php diff --git a/includes/class-wgpb-product-attribute-terms-controller.php b/includes/class-wgpb-product-attribute-terms-controller.php deleted file mode 100644 index 9b6773b56e7..00000000000 --- a/includes/class-wgpb-product-attribute-terms-controller.php +++ /dev/null @@ -1,182 +0,0 @@ -namespace, - '/' . $this->rest_base, - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_items_permissions_check' ), - 'args' => $this->get_collection_params(), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/(?P[\d]+)', - array( - 'args' => array( - 'id' => array( - 'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ), - 'type' => 'integer', - ), - ), - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'get_item_permissions_check' ), - 'args' => array( - 'context' => $this->get_context_param( - array( - 'default' => 'view', - ) - ), - ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - } - - /** - * Check permissions. - * - * @param WP_REST_Request $request Full details about the request. - * @param string $context Request context. - * @return bool|WP_Error - */ - protected function check_permissions( $request, $context = 'read' ) { - // Get taxonomy. - $taxonomy = $this->get_taxonomy( $request ); - if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) { - return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - - // Check permissions for a single term. - $id = intval( $request['id'] ); - if ( $id ) { - $term = get_term( $id, $taxonomy ); - - if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) { - return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - } - - return current_user_can( 'edit_posts' ); - } - - /** - * Prepare a single product category output for response. - * - * @param WP_Term $item Term object. - * @param WP_REST_Request $request Request instance. - * @return WP_REST_Response - */ - public function prepare_item_for_response( $item, $request ) { - // Get the attribute slug. - $attribute_id = absint( $request->get_param( 'attribute_id' ) ); - $attribute = wc_get_attribute( $attribute_id ); - - $data = array( - 'id' => (int) $item->term_id, - 'name' => $item->name, - 'slug' => $item->slug, - 'count' => (int) $item->count, - 'attribute' => array( - 'id' => $attribute->id, - 'name' => $attribute->name, - 'slug' => $attribute->slug, - ), - ); - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - $response = rest_ensure_response( $data ); - - $response->add_links( $this->prepare_links( $item, $request ) ); - - return $response; - } - - /** - * Get the Product's schema, conforming to JSON Schema. - * - * @return array - */ - public function get_item_schema() { - $raw_schema = parent::get_item_schema(); - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'product_block_category', - 'type' => 'object', - 'properties' => array(), - ); - - $schema['properties']['id'] = $raw_schema['properties']['id']; - $schema['properties']['name'] = $raw_schema['properties']['name']; - $schema['properties']['slug'] = $raw_schema['properties']['slug']; - $schema['properties']['count'] = $raw_schema['properties']['count']; - $schema['properties']['attribute'] = array( - 'description' => __( 'Attribute.', 'woo-gutenberg-products-block' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => array( - 'id' => array( - 'description' => __( 'Attribute ID.', 'woo-gutenberg-products-block' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'name' => array( - 'description' => __( 'Attribute name.', 'woo-gutenberg-products-block' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'slug' => array( - 'description' => __( 'Attribute slug.', 'woo-gutenberg-products-block' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - ), - ); - - return $this->add_additional_fields_schema( $schema ); - } -} diff --git a/includes/class-wgpb-product-attributes-controller.php b/includes/class-wgpb-product-attributes-controller.php deleted file mode 100644 index 6e79760ebd8..00000000000 --- a/includes/class-wgpb-product-attributes-controller.php +++ /dev/null @@ -1,186 +0,0 @@ -namespace, - '/' . $this->rest_base, - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_items_permissions_check' ), - 'args' => $this->get_collection_params(), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/(?P[\d]+)', - array( - 'args' => array( - 'id' => array( - 'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ), - 'type' => 'integer', - ), - ), - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'get_item_permissions_check' ), - 'args' => array( - 'context' => $this->get_context_param( - array( - 'default' => 'view', - ) - ), - ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - } - - /** - * Check if a given request has access to read the attributes. - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_Error|boolean - */ - public function get_items_permissions_check( $request ) { - if ( ! current_user_can( 'edit_posts' ) ) { - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) ); - } - - return true; - } - /** - * Check if a given request has access to read a attribute. - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_Error|boolean - */ - public function get_item_permissions_check( $request ) { - $taxonomy = $this->get_taxonomy( $request ); - - if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) { - return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - - if ( ! current_user_can( 'edit_posts' ) ) { - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) ); - } - - return true; - } - - /** - * Check permissions. - * - * @param WP_REST_Request $request Full details about the request. - * @param string $context Request context. - * @return bool|WP_Error - */ - protected function check_permissions( $request, $context = 'read' ) { - // Get taxonomy. - $taxonomy = $this->get_taxonomy( $request ); - if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) { - return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - - // Check permissions for a single term. - $id = intval( $request['id'] ); - if ( $id ) { - $term = get_term( $id, $taxonomy ); - - if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) { - return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - } - - return current_user_can( 'edit_posts' ); - } - - /** - * Prepare a single product category output for response. - * - * @param WP_Term $item Term object. - * @param WP_REST_Request $request Request instance. - * @return WP_REST_Response - */ - public function prepare_item_for_response( $item, $request ) { - $taxonomy = wc_attribute_taxonomy_name( $item->attribute_name ); - $data = array( - 'id' => (int) $item->attribute_id, - 'name' => $item->attribute_label, - 'slug' => $taxonomy, - 'count' => wp_count_terms( $taxonomy ), - ); - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - $response = rest_ensure_response( $data ); - - $response->add_links( $this->prepare_links( $item ) ); - - return $response; - } - - /** - * Get the Product's schema, conforming to JSON Schema. - * - * @return array - */ - public function get_item_schema() { - $raw_schema = parent::get_item_schema(); - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'product_block_attribute', - 'type' => 'object', - 'properties' => array(), - ); - - $schema['properties']['id'] = $raw_schema['properties']['id']; - $schema['properties']['name'] = $raw_schema['properties']['name']; - $schema['properties']['slug'] = $raw_schema['properties']['slug']; - $schema['properties']['count'] = array( - 'description' => __( 'Number of terms in the attribute taxonomy.', 'woo-gutenberg-products-block' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ); - - return $this->add_additional_fields_schema( $schema ); - } -} diff --git a/includes/class-wgpb-product-categories-controller.php b/includes/class-wgpb-product-categories-controller.php deleted file mode 100644 index b1329bb5cb8..00000000000 --- a/includes/class-wgpb-product-categories-controller.php +++ /dev/null @@ -1,149 +0,0 @@ -namespace, - '/' . $this->rest_base, - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_items_permissions_check' ), - 'args' => $this->get_collection_params(), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/(?P[\d]+)', - array( - 'args' => array( - 'id' => array( - 'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ), - 'type' => 'integer', - ), - ), - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'get_item_permissions_check' ), - 'args' => array( - 'context' => $this->get_context_param( - array( - 'default' => 'view', - ) - ), - ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - } - - /** - * Check permissions. - * - * @param WP_REST_Request $request Full details about the request. - * @param string $context Request context. - * @return bool|WP_Error - */ - protected function check_permissions( $request, $context = 'read' ) { - // Get taxonomy. - $taxonomy = $this->get_taxonomy( $request ); - if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) { - return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - - // Check permissions for a single term. - $id = intval( $request['id'] ); - if ( $id ) { - $term = get_term( $id, $taxonomy ); - - if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) { - return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); - } - } - - return current_user_can( 'edit_posts' ); - } - - /** - * Prepare a single product category output for response. - * - * @param WP_Term $item Term object. - * @param WP_REST_Request $request Request instance. - * @return WP_REST_Response - */ - public function prepare_item_for_response( $item, $request ) { - $data = array( - 'id' => (int) $item->term_id, - 'name' => $item->name, - 'slug' => $item->slug, - 'parent' => (int) $item->parent, - 'count' => (int) $item->count, - ); - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - $response = rest_ensure_response( $data ); - - $response->add_links( $this->prepare_links( $item, $request ) ); - - return $response; - } - - /** - * Get the Product's schema, conforming to JSON Schema. - * - * @return array - */ - public function get_item_schema() { - $raw_schema = parent::get_item_schema(); - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'product_block_category', - 'type' => 'object', - 'properties' => array(), - ); - - $schema['properties']['id'] = $raw_schema['properties']['id']; - $schema['properties']['name'] = $raw_schema['properties']['name']; - $schema['properties']['slug'] = $raw_schema['properties']['slug']; - $schema['properties']['parent'] = $raw_schema['properties']['parent']; - $schema['properties']['count'] = $raw_schema['properties']['count']; - - return $this->add_additional_fields_schema( $schema ); - } -} diff --git a/includes/class-wgpb-products-controller.php b/includes/class-wgpb-products-controller.php deleted file mode 100644 index 71e1aba3f71..00000000000 --- a/includes/class-wgpb-products-controller.php +++ /dev/null @@ -1,386 +0,0 @@ -namespace, - '/' . $this->rest_base, - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_items_permissions_check' ), - 'args' => $this->get_collection_params(), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/(?P[\d]+)', - array( - 'args' => array( - 'id' => array( - 'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ), - 'type' => 'integer', - ), - ), - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'get_item_permissions_check' ), - 'args' => array( - 'context' => $this->get_context_param( - array( - 'default' => 'view', - ) - ), - ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - } - - /** - * Check if a given request has access to read items. - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_Error|boolean - */ - public function get_items_permissions_check( $request ) { - if ( ! current_user_can( 'edit_posts' ) ) { - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) ); - } - - return true; - } - - /** - * Check if a given request has access to read an item. - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_Error|boolean - */ - public function get_item_permissions_check( $request ) { - if ( ! current_user_can( 'edit_posts' ) ) { - return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woo-gutenberg-products-block' ), array( 'status' => rest_authorization_required_code() ) ); - } - - return true; - } - - /** - * Get a collection of posts. - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_Error|WP_REST_Response - */ - public function get_items( $request ) { - $query_args = $this->prepare_objects_query( $request ); - $query_results = $this->get_objects( $query_args ); - - $objects = array(); - foreach ( $query_results['objects'] as $object ) { - $data = $this->prepare_object_for_response( $object, $request ); - $objects[] = $this->prepare_response_for_collection( $data ); - } - - $page = (int) $query_args['paged']; - $max_pages = $query_results['pages']; - - $response = rest_ensure_response( $objects ); - $response->header( 'X-WP-Total', $query_results['total'] ); - $response->header( 'X-WP-TotalPages', (int) $max_pages ); - - $base = $this->rest_base; - $attrib_prefix = '(?P<'; - if ( strpos( $base, $attrib_prefix ) !== false ) { - $attrib_names = array(); - preg_match( '/\(\?P<[^>]+>.*\)/', $base, $attrib_names, PREG_OFFSET_CAPTURE ); - foreach ( $attrib_names as $attrib_name_match ) { - $beginning_offset = strlen( $attrib_prefix ); - $attrib_name_end = strpos( $attrib_name_match[0], '>', $attrib_name_match[1] ); - $attrib_name = substr( $attrib_name_match[0], $beginning_offset, $attrib_name_end - $beginning_offset ); - if ( isset( $request[ $attrib_name ] ) ) { - $base = str_replace( "(?P<$attrib_name>[\d]+)", $request[ $attrib_name ], $base ); - } - } - } - $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ) ); - - if ( $page > 1 ) { - $prev_page = $page - 1; - if ( $prev_page > $max_pages ) { - $prev_page = $max_pages; - } - $prev_link = add_query_arg( 'page', $prev_page, $base ); - $response->link_header( 'prev', $prev_link ); - } - if ( $max_pages > $page ) { - $next_page = $page + 1; - $next_link = add_query_arg( 'page', $next_page, $base ); - $response->link_header( 'next', $next_link ); - } - - return $response; - } - - /** - * Get the images for a product or product variation. - * - * @param WC_Product|WC_Product_Variation $product Product instance. - * @return array - */ - protected function get_images( $product ) { - $images = array(); - $attachment_ids = array(); - - // Add featured image. - if ( has_post_thumbnail( $product->get_id() ) ) { - $attachment_ids[] = $product->get_image_id(); - } - - // Add gallery images. - $attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() ); - - // Build image data. - foreach ( $attachment_ids as $attachment_id ) { - $attachment_post = get_post( $attachment_id ); - if ( is_null( $attachment_post ) ) { - continue; - } - - $attachment = wp_get_attachment_image_src( $attachment_id, 'full' ); - if ( ! is_array( $attachment ) ) { - continue; - } - - $images[] = array( - 'id' => (int) $attachment_id, - 'src' => current( $attachment ), - 'name' => get_the_title( $attachment_id ), - 'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), - ); - } - - return $images; - } - - /** - * Prepare a single product output for response. - * - * @deprecated 3.0.0 - * - * @param WP_Post $post Post object. - * @param WP_REST_Request $request Request object. - * @return WP_REST_Response - */ - public function prepare_item_for_response( $post, $request ) { - $product = wc_get_product( $post ); - $data = $this->get_product_data( $product ); - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); - - $response->add_links( $this->prepare_links( $product, $request ) ); - - return $response; - } - - /** - * Make extra product orderby features supported by WooCommerce available to the WC API. - * This includes 'price', 'popularity', and 'rating'. - * - * @param WP_REST_Request $request Request data. - * @return array - */ - protected function prepare_objects_query( $request ) { - $args = parent::prepare_objects_query( $request ); - $operator_mapping = array( - 'in' => 'IN', - 'not_in' => 'NOT IN', - 'and' => 'AND', - ); - - $orderby = $request->get_param( 'orderby' ); - $order = $request->get_param( 'order' ); - $category_operator = $operator_mapping[ $request->get_param( 'category_operator' ) ]; - $attribute_operator = $operator_mapping[ $request->get_param( 'attribute_operator' ) ]; - $catalog_visibility = $request->get_param( 'catalog_visibility' ); - - $ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order ); - $args['orderby'] = $ordering_args['orderby']; - $args['order'] = $ordering_args['order']; - if ( $ordering_args['meta_key'] ) { - $args['meta_key'] = $ordering_args['meta_key']; // WPCS: slow query ok. - } - - if ( $category_operator && isset( $args['tax_query'] ) ) { - foreach ( $args['tax_query'] as $i => $tax_query ) { - if ( 'product_cat' === $tax_query['taxonomy'] ) { - $args['tax_query'][ $i ]['operator'] = $category_operator; - $args['tax_query'][ $i ]['include_children'] = 'AND' === $category_operator ? false : true; - } - } - } - - if ( $attribute_operator && isset( $args['tax_query'] ) ) { - foreach ( $args['tax_query'] as $i => $tax_query ) { - if ( in_array( $tax_query['taxonomy'], wc_get_attribute_taxonomy_names(), true ) ) { - $args['tax_query'][ $i ]['operator'] = $attribute_operator; - } - } - } - - if ( in_array( $catalog_visibility, array_keys( wc_get_product_visibility_options() ), true ) ) { - $exclude_from_catalog = 'search' === $catalog_visibility ? '' : 'exclude-from-catalog'; - $exclude_from_search = 'catalog' === $catalog_visibility ? '' : 'exclude-from-search'; - - $args['tax_query'][] = array( - 'taxonomy' => 'product_visibility', - 'field' => 'name', - 'terms' => array( $exclude_from_catalog, $exclude_from_search ), - 'operator' => 'hidden' === $catalog_visibility ? 'AND' : 'NOT IN', - ); - } - - return $args; - } - - /** - * Get product data. - * - * @param WC_Product $product Product instance. - * @param string $context Request context. - * Options: 'view' and 'edit'. - * @return array - */ - protected function get_product_data( $product, $context = 'view' ) { - $raw_data = parent::get_product_data( $product, $context ); - $data = array(); - - $data['id'] = $raw_data['id']; - $data['name'] = $raw_data['name']; - $data['permalink'] = $raw_data['permalink']; - $data['sku'] = $raw_data['sku']; - $data['description'] = $raw_data['description']; - $data['short_description'] = $raw_data['short_description']; - $data['price'] = $raw_data['price']; - $data['price_html'] = $raw_data['price_html']; - $data['images'] = $raw_data['images']; - - return $data; - } - - /** - * Update the collection params. - * - * Adds new options for 'orderby', and new parameters 'cat_operator', 'attr_operator'. - * - * @return array - */ - public function get_collection_params() { - $params = parent::get_collection_params(); - $params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'price', 'popularity', 'rating', 'menu_order' ) ); - $params['category_operator'] = array( - 'description' => __( 'Operator to compare product category terms.', 'woo-gutenberg-products-block' ), - 'type' => 'string', - 'enum' => array( 'in', 'not_in', 'and' ), - 'default' => 'in', - 'sanitize_callback' => 'sanitize_key', - 'validate_callback' => 'rest_validate_request_arg', - ); - $params['attribute_operator'] = array( - 'description' => __( 'Operator to compare product attribute terms.', 'woo-gutenberg-products-block' ), - 'type' => 'string', - 'enum' => array( 'in', 'not_in', 'and' ), - 'default' => 'in', - 'sanitize_callback' => 'sanitize_key', - 'validate_callback' => 'rest_validate_request_arg', - ); - $params['catalog_visibility'] = array( - 'description' => __( 'Determines if hidden or visible catalog products are shown.', 'woo-gutenberg-products-block' ), - 'type' => 'string', - 'enum' => array( 'visible', 'catalog', 'search', 'hidden' ), - 'sanitize_callback' => 'sanitize_key', - 'validate_callback' => 'rest_validate_request_arg', - ); - - return $params; - } - - /** - * Get the Product's schema, conforming to JSON Schema. - * - * @return array - */ - public function get_item_schema() { - $raw_schema = parent::get_item_schema(); - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'product_block_product', - 'type' => 'object', - 'properties' => array(), - ); - - $schema['properties']['id'] = $raw_schema['properties']['id']; - $schema['properties']['name'] = $raw_schema['properties']['name']; - $schema['properties']['permalink'] = $raw_schema['properties']['permalink']; - $schema['properties']['sku'] = $raw_schema['properties']['sku']; - $schema['properties']['description'] = $raw_schema['properties']['description']; - $schema['properties']['short_description'] = $raw_schema['properties']['short_description']; - $schema['properties']['price'] = $raw_schema['properties']['price']; - $schema['properties']['price_html'] = $raw_schema['properties']['price_html']; - $schema['properties']['images'] = array( - 'description' => $raw_schema['properties']['images']['description'], - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'items' => array( - 'type' => 'object', - 'properties' => array(), - ), - ); - - $images_schema = $raw_schema['properties']['images']['items']['properties']; - - $schema['properties']['images']['items']['properties']['id'] = $images_schema['id']; - $schema['properties']['images']['items']['properties']['src'] = $images_schema['src']; - $schema['properties']['images']['items']['properties']['name'] = $images_schema['name']; - $schema['properties']['images']['items']['properties']['alt'] = $images_schema['alt']; - - return $this->add_additional_fields_schema( $schema ); - } -} diff --git a/tests/api/products-attributes.php b/tests/api/products-attributes.php deleted file mode 100644 index d8d7af75ec7..00000000000 --- a/tests/api/products-attributes.php +++ /dev/null @@ -1,146 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - - // Create 2 product attributes with terms. - $attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) ); - $attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large' ) ); - - $red_term = get_term_by( 'slug', 'red', $attr_color['attribute_taxonomy'] ); - $blue_term = get_term_by( 'slug', 'blue', $attr_color['attribute_taxonomy'] ); - $yellow_term = get_term_by( 'slug', 'yellow', $attr_color['attribute_taxonomy'] ); - $small_term = get_term_by( 'slug', 'small', $attr_size['attribute_taxonomy'] ); - $medium_term = get_term_by( 'slug', 'medium', $attr_size['attribute_taxonomy'] ); - $large_term = get_term_by( 'slug', 'large', $attr_size['attribute_taxonomy'] ); - - $this->attr_term_ids = array( - 'red' => $red_term->term_id, - 'blue' => $blue_term->term_id, - 'yellow' => $yellow_term->term_id, - 'small' => $small_term->term_id, - 'medium' => $medium_term->term_id, - 'large' => $large_term->term_id, - ); - - $color = new WC_Product_Attribute(); - $color->set_id( $attr_color['attribute_id'] ); - $color->set_name( $attr_color['attribute_taxonomy'] ); - $color->set_visible( true ); - - $size = new WC_Product_Attribute(); - $size->set_id( $attr_size['attribute_id'] ); - $size->set_name( $attr_size['attribute_taxonomy'] ); - $size->set_visible( true ); - - // Create some products with a mix of attributes: - // - Product 1 – color: red, blue; size: medium. - // - Product 2 – color: blue; size: large, medium. - // - Product 3 – color: yellow. - $this->products = array(); - $color->set_options( [ $this->attr_term_ids['red'], $this->attr_term_ids['blue'] ] ); - $size->set_options( [ $this->attr_term_ids['medium'] ] ); - $this->products[0] = WC_Helper_Product::create_simple_product( false ); - $this->products[0]->set_attributes( [ $color, $size ] ); - $this->products[0]->save(); - - $color->set_options( [ $this->attr_term_ids['blue'] ] ); - $size->set_options( [ $this->attr_term_ids['medium'], $this->attr_term_ids['large'] ] ); - $this->products[1] = WC_Helper_Product::create_simple_product( false ); - $this->products[1]->set_attributes( [ $color, $size ] ); - $this->products[1]->save(); - - $color->set_options( [ $this->attr_term_ids['yellow'] ] ); - $this->products[2] = WC_Helper_Product::create_simple_product( false ); - $this->products[2]->set_attributes( [ $color ] ); - $this->products[2]->save(); - } - - /** - * Test getting products by a single attribute term. - * - * @since 1.2.0 - */ - public function test_get_products() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); - $request->set_param( 'attribute', 'pa_color' ); - $request->set_param( 'attribute_term', (string) $this->attr_term_ids['red'] ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response_products ) ); - } - - /** - * Test getting products by multiple terms in one attribute. - * - * @since 1.2.0 - */ - public function test_get_products_by_multiple_terms() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); - $request->set_param( 'attribute', 'pa_color' ); - $request->set_param( - 'attribute_term', - // Terms list needs to be a string. - $this->attr_term_ids['red'] . ',' . $this->attr_term_ids['yellow'] - ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $response_products ) ); - } - - /** - * Test getting products by multiple terms in one attribute, matching all. - * - * @since 1.2.0 - */ - public function test_get_products_by_multiple_terms_all() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); - $request->set_param( 'attribute', 'pa_size' ); - $request->set_param( - 'attribute_term', - // Terms list needs to be a string. - $this->attr_term_ids['medium'] . ',' . $this->attr_term_ids['large'] - ); - $request->set_param( 'attribute_operator', 'and' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response_products ) ); - } -} diff --git a/tests/api/products-categories.php b/tests/api/products-categories.php deleted file mode 100644 index 4e2a4845081..00000000000 --- a/tests/api/products-categories.php +++ /dev/null @@ -1,150 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'author', - ) - ); - - // Create 3 product categories. - $parent = wp_insert_term( 'Parent Category', 'product_cat' ); - $child = wp_insert_term( - 'Child Category', - 'product_cat', - array( 'parent' => $parent['term_id'] ) - ); - $single = wp_insert_term( 'Standalone Category', 'product_cat' ); - $this->categories = array( - 'parent' => $parent, - 'child' => $child, - 'single' => $single, - ); - - // Create two products, one with 'parent', and one with 'single'. - $this->products = array(); - $this->products[0] = WC_Helper_Product::create_simple_product( false ); - $this->products[0]->set_category_ids( array( $parent['term_id'] ) ); - $this->products[0]->save(); - - $this->products[1] = WC_Helper_Product::create_simple_product( false ); - $this->products[1]->set_category_ids( array( $single['term_id'] ) ); - $this->products[1]->save(); - - $this->products[2] = WC_Helper_Product::create_simple_product( false ); - $this->products[2]->set_category_ids( array( $child['term_id'], $single['term_id'] ) ); - $this->products[2]->save(); - - $this->products[3] = WC_Helper_Product::create_simple_product( false ); - $this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) ); - $this->products[3]->save(); - } - - /** - * Test product category intersection: Any product in either Single or Parent (4). - * - * @since 3.5.0 - */ - public function test_get_products_in_any_categories_parent() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['parent']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'in' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 4, count( $response_products ) ); - } - - /** - * Test product category intersection: Any product in either Single or Child (3). - * - * @since 3.5.0 - */ - public function test_get_products_in_any_categories_child() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'in' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 3, count( $response_products ) ); - } - - /** - * Test product category intersection: Any product in both Single and Child (1). - * - * @since 3.5.0 - */ - public function test_get_products_in_all_categories_child() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'and' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response_products ) ); - } - - /** - * Test product category intersection: Any product in both Single and Parent (1). - * - * @since 3.5.0 - */ - public function test_get_products_in_all_categories_parent() { - wp_set_current_user( $this->user ); - - $cats = $this->categories['parent']['term_id'] . ',' . $this->categories['single']['term_id']; - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'category', $cats ); - $request->set_param( 'category_operator', 'and' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $response_products ) ); - } -} diff --git a/tests/api/products-extra.php b/tests/api/products-extra.php deleted file mode 100644 index 03b39103777..00000000000 --- a/tests/api/products-extra.php +++ /dev/null @@ -1,219 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'author', - ) - ); - $this->contributor = $this->factory->user->create( - array( - 'role' => 'contributor', - ) - ); - $this->subscriber = $this->factory->user->create( - array( - 'role' => 'subscriber', - ) - ); - } - - /** - * Test route registration. - * - * @since 1.2.0 - */ - public function test_register_routes() { - $routes = $this->server->get_routes(); - - $this->assertArrayHasKey( '/wc-blocks/v1/products', $routes ); - $this->assertArrayHasKey( '/wc-blocks/v1/products/(?P[\d]+)', $routes ); - } - - /** - * Test getting products. - * - * @since 1.2.0 - */ - public function test_get_products() { - wp_set_current_user( $this->user ); - WC_Helper_Product::create_external_product(); - sleep( 1 ); // So both products have different timestamps. - $product = WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 2, count( $products ) ); - $this->assertEquals( 'Dummy Product', $products[0]['name'] ); - $this->assertEquals( 'DUMMY SKU', $products[0]['sku'] ); - $this->assertEquals( 'Dummy External Product', $products[1]['name'] ); - $this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] ); - } - - /** - * Test getting products as an contributor. - * - * @since 1.2.0 - */ - public function test_get_products_as_contributor() { - wp_set_current_user( $this->contributor ); - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) ); - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * Test getting products as an subscriber. - * - * @since 1.2.0 - */ - public function test_get_products_as_subscriber() { - wp_set_current_user( $this->subscriber ); - WC_Helper_Product::create_simple_product(); - $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) ); - $this->assertEquals( 403, $response->get_status() ); - } - - /** - * Test getting products with custom ordering. - * - * @since 1.2.0 - */ - public function test_get_products_order_by_price() { - wp_set_current_user( $this->user ); - WC_Helper_Product::create_external_product(); - sleep( 1 ); // So both products have different timestamps. - $product = WC_Helper_Product::create_simple_product( false ); // Prevent saving, since we save here. - // Customize the price, otherwise both are 10. - $product->set_props( - array( - 'regular_price' => 15, - 'price' => 15, - ) - ); - $product->save(); - - $request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ); - $request->set_param( 'orderby', 'price' ); - $request->set_param( 'order', 'asc' ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $products ) ); - - // The external product should be first, then the simple product. - $this->assertEquals( 'Dummy External Product', $products[0]['name'] ); - $this->assertEquals( 'Dummy Product', $products[1]['name'] ); - $this->assertEquals( '10', $products[0]['price'] ); - $this->assertEquals( '15', $products[1]['price'] ); - } - - /** - * Test product_visibility queries. - * - * @since 1.3.1 - */ - public function test_product_visibility() { - wp_set_current_user( $this->user ); - $visible_product = WC_Helper_Product::create_simple_product(); - $visible_product->set_name( 'Visible Product' ); - $visible_product->set_catalog_visibility( 'visible' ); - $visible_product->save(); - - $catalog_product = WC_Helper_Product::create_simple_product(); - $catalog_product->set_name( 'Catalog Product' ); - $catalog_product->set_catalog_visibility( 'catalog' ); - $catalog_product->save(); - - $search_product = WC_Helper_Product::create_simple_product(); - $search_product->set_name( 'Search Product' ); - $search_product->set_catalog_visibility( 'search' ); - $search_product->save(); - - $hidden_product = WC_Helper_Product::create_simple_product(); - $hidden_product->set_name( 'Hidden Product' ); - $hidden_product->set_catalog_visibility( 'hidden' ); - $hidden_product->save(); - - $query_params = array( - 'catalog_visibility' => 'visible', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $products ) ); - $this->assertEquals( 'Visible Product', $products[0]['name'] ); - - $query_params = array( - 'catalog_visibility' => 'catalog', - 'orderby' => 'id', - 'order' => 'asc', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $products ) ); - $this->assertEquals( 'Visible Product', $products[0]['name'] ); - $this->assertEquals( 'Catalog Product', $products[1]['name'] ); - - $query_params = array( - 'catalog_visibility' => 'search', - 'orderby' => 'id', - 'order' => 'asc', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $products ) ); - $this->assertEquals( 'Visible Product', $products[0]['name'] ); - $this->assertEquals( 'Search Product', $products[1]['name'] ); - - $query_params = array( - 'catalog_visibility' => 'hidden', - ); - $request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' ); - $request->set_query_params( $query_params ); - $response = $this->server->dispatch( $request ); - $products = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 1, count( $products ) ); - $this->assertEquals( 'Hidden Product', $products[0]['name'] ); - } -} diff --git a/woocommerce-gutenberg-products-block.php b/woocommerce-gutenberg-products-block.php index b32888b429f..bd475b28b2c 100644 --- a/woocommerce-gutenberg-products-block.php +++ b/woocommerce-gutenberg-products-block.php @@ -35,8 +35,6 @@ function wgpb_initialize() { if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && ! $files_exist ) { add_action( 'admin_notices', 'wgpb_plugins_notice' ); } - - add_action( 'rest_api_init', 'wgpb_register_api_routes' ); } add_action( 'woocommerce_loaded', 'wgpb_initialize' ); @@ -54,25 +52,3 @@ function wgpb_plugins_notice() { ); echo '

'; } - -/** - * Register extra API routes with functionality specific for product blocks. - */ -function wgpb_register_api_routes() { - include_once dirname( __FILE__ ) . '/includes/class-wgpb-products-controller.php'; - include_once dirname( __FILE__ ) . '/includes/class-wgpb-product-categories-controller.php'; - include_once dirname( __FILE__ ) . '/includes/class-wgpb-product-attributes-controller.php'; - include_once dirname( __FILE__ ) . '/includes/class-wgpb-product-attribute-terms-controller.php'; - - $products = new WGPB_Products_Controller(); - $products->register_routes(); - - $categories = new WGPB_Product_Categories_Controller(); - $categories->register_routes(); - - $attributes = new WGPB_Product_Attributes_Controller(); - $attributes->register_routes(); - - $attribute_terms = new WGPB_Product_Attribute_Terms_Controller(); - $attribute_terms->register_routes(); -}