ProductAttributeTerms.php
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Product Attribute Terms Controller |
| 4 | * |
| 5 | * Handles requests to /products/attributes/<slug>/terms |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Product attribute terms controller. |
| 14 | * |
| 15 | * @internal |
| 16 | * @extends WC_REST_Product_Attribute_Terms_Controller |
| 17 | */ |
| 18 | class ProductAttributeTerms extends \WC_REST_Product_Attribute_Terms_Controller { |
| 19 | use CustomAttributeTraits; |
| 20 | |
| 21 | /** |
| 22 | * Endpoint namespace. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $namespace = 'wc-analytics'; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Register the routes for custom product attributes. |
| 31 | */ |
| 32 | public function register_routes() { |
| 33 | parent::register_routes(); |
| 34 | |
| 35 | register_rest_route( |
| 36 | $this->namespace, |
| 37 | 'products/attributes/(?P<slug>[a-z0-9_\-]+)/terms', |
| 38 | array( |
| 39 | 'args' => array( |
| 40 | 'slug' => array( |
| 41 | 'description' => __( 'Slug identifier for the resource.', 'woocommerce' ), |
| 42 | 'type' => 'string', |
| 43 | ), |
| 44 | ), |
| 45 | array( |
| 46 | 'methods' => \WP_REST_Server::READABLE, |
| 47 | 'callback' => array( $this, 'get_item_by_slug' ), |
| 48 | 'permission_callback' => array( $this, 'get_custom_attribute_permissions_check' ), |
| 49 | 'args' => $this->get_collection_params(), |
| 50 | ), |
| 51 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Check if a given request has access to read a custom attribute. |
| 58 | * |
| 59 | * @param WP_REST_Request $request Full details about the request. |
| 60 | * @return WP_Error|boolean |
| 61 | */ |
| 62 | public function get_custom_attribute_permissions_check( $request ) { |
| 63 | if ( ! wc_rest_check_manager_permissions( 'attributes', 'read' ) ) { |
| 64 | return new WP_Error( |
| 65 | 'woocommerce_rest_cannot_view', |
| 66 | __( 'Sorry, you cannot view this resource.', 'woocommerce' ), |
| 67 | array( |
| 68 | 'status' => rest_authorization_required_code(), |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the Attribute's schema, conforming to JSON Schema. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function get_item_schema() { |
| 82 | $schema = parent::get_item_schema(); |
| 83 | // Custom attributes substitute slugs for numeric IDs. |
| 84 | $schema['properties']['id']['type'] = array( 'integer', 'string' ); |
| 85 | |
| 86 | return $schema; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Query custom attribute values by slug. |
| 91 | * |
| 92 | * @param string $slug Attribute slug. |
| 93 | * @return array Attribute values, formatted for response. |
| 94 | */ |
| 95 | protected function get_custom_attribute_values( $slug ) { |
| 96 | global $wpdb; |
| 97 | |
| 98 | if ( empty( $slug ) ) { |
| 99 | return array(); |
| 100 | } |
| 101 | |
| 102 | $attribute_values = array(); |
| 103 | |
| 104 | // Get the attribute properties. |
| 105 | $attribute = $this->get_custom_attribute_by_slug( $slug ); |
| 106 | |
| 107 | if ( is_wp_error( $attribute ) ) { |
| 108 | return $attribute; |
| 109 | } |
| 110 | |
| 111 | // Find all attribute values assigned to products. |
| 112 | $query_results = $wpdb->get_results( |
| 113 | $wpdb->prepare( |
| 114 | "SELECT meta_value, COUNT(meta_id) AS product_count |
| 115 | FROM {$wpdb->postmeta} |
| 116 | WHERE meta_key = %s |
| 117 | AND meta_value != '' |
| 118 | GROUP BY meta_value", |
| 119 | 'attribute_' . esc_sql( $slug ) |
| 120 | ), |
| 121 | OBJECT_K |
| 122 | ); |
| 123 | |
| 124 | // Ensure all defined properties are in the response. |
| 125 | $defined_values = wc_get_text_attributes( $attribute[ $slug ]['value'] ); |
| 126 | |
| 127 | foreach ( $defined_values as $defined_value ) { |
| 128 | if ( array_key_exists( $defined_value, $query_results ) ) { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | $query_results[ $defined_value ] = (object) array( |
| 133 | 'meta_value' => $defined_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 134 | 'product_count' => 0, |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | foreach ( $query_results as $term_value => $term ) { |
| 139 | // Mimic the structure of a taxonomy-backed attribute values for response. |
| 140 | $data = array( |
| 141 | 'id' => $term_value, |
| 142 | 'name' => $term_value, |
| 143 | 'slug' => $term_value, |
| 144 | 'description' => '', |
| 145 | 'menu_order' => 0, |
| 146 | 'count' => (int) $term->product_count, |
| 147 | ); |
| 148 | |
| 149 | $response = rest_ensure_response( $data ); |
| 150 | $response->add_links( |
| 151 | array( |
| 152 | 'collection' => array( |
| 153 | 'href' => rest_url( |
| 154 | $this->namespace . '/products/attributes/' . $slug . '/terms' |
| 155 | ), |
| 156 | ), |
| 157 | ) |
| 158 | ); |
| 159 | $response = $this->prepare_response_for_collection( $response ); |
| 160 | |
| 161 | $attribute_values[ $term_value ] = $response; |
| 162 | } |
| 163 | |
| 164 | return array_values( $attribute_values ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get a single custom attribute. |
| 169 | * |
| 170 | * @param WP_REST_Request $request Full details about the request. |
| 171 | * @return WP_REST_Request|WP_Error |
| 172 | */ |
| 173 | public function get_item_by_slug( $request ) { |
| 174 | return $this->get_custom_attribute_values( $request['slug'] ); |
| 175 | } |
| 176 | } |
| 177 |