PluginProbe
WooCommerce / 11.1.0-beta.1
WooCommerce v11.1.0-beta.1
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / API / ProductAttributes.php
ProductAttributes.php
169 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API Product Attributes Controller
4 *
5 * Handles requests to /products/attributes.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Product categories controller.
14 *
15 * @internal
16 * @extends WC_REST_Product_Attributes_Controller
17 */
18 class ProductAttributes extends \WC_REST_Product_Attributes_Controller {
19 use CustomAttributeTraits;
20
21 /**
22 * Endpoint namespace.
23 *
24 * @var string
25 */
26 protected $namespace = 'wc-analytics';
27
28 /**
29 * Register the routes for custom product attributes.
30 */
31 public function register_routes() {
32 parent::register_routes();
33
34 register_rest_route(
35 $this->namespace,
36 'products/attributes/(?P<slug>[a-z0-9_\-]+)',
37 array(
38 'args' => array(
39 'slug' => array(
40 'description' => __( 'Slug identifier for the resource.', 'woocommerce' ),
41 'type' => 'string',
42 ),
43 ),
44 array(
45 'methods' => \WP_REST_Server::READABLE,
46 'callback' => array( $this, 'get_item_by_slug' ),
47 'permission_callback' => array( $this, 'get_items_permissions_check' ),
48 ),
49 'schema' => array( $this, 'get_public_item_schema' ),
50 )
51 );
52 }
53
54 /**
55 * Get the query params for collections
56 *
57 * @return array
58 */
59 public function get_collection_params() {
60 $params = parent::get_collection_params();
61 $params['search'] = array(
62 'description' => __( 'Search by similar attribute name.', 'woocommerce' ),
63 'type' => 'string',
64 'validate_callback' => 'rest_validate_request_arg',
65 );
66
67 return $params;
68 }
69
70 /**
71 * Get the Attribute's schema, conforming to JSON Schema.
72 *
73 * @return array
74 */
75 public function get_item_schema() {
76 $schema = parent::get_item_schema();
77 // Custom attributes substitute slugs for numeric IDs.
78 $schema['properties']['id']['type'] = array( 'integer', 'string' );
79
80 return $schema;
81 }
82
83 /**
84 * Get a single attribute by it's slug.
85 *
86 * @param WP_REST_Request $request The API request.
87 * @return WP_REST_Response
88 */
89 public function get_item_by_slug( $request ) {
90 if ( empty( $request['slug'] ) ) {
91 return array();
92 }
93
94 $attributes = $this->get_custom_attribute_by_slug( $request['slug'] );
95
96 if ( is_wp_error( $attributes ) ) {
97 return $attributes;
98 }
99
100 $response_items = $this->format_custom_attribute_items_for_response( $attributes );
101
102 return reset( $response_items );
103 }
104
105 /**
106 * Format custom attribute items for response (mimic the structure of a taxonomy - backed attribute).
107 *
108 * @param array $custom_attributes - CustomAttributeTraits::get_custom_attributes().
109 * @return array
110 */
111 protected function format_custom_attribute_items_for_response( $custom_attributes ) {
112 $response = array();
113
114 foreach ( $custom_attributes as $attribute_key => $attribute_value ) {
115 $data = array(
116 'id' => $attribute_key,
117 'name' => $attribute_value['name'],
118 'slug' => $attribute_key,
119 'type' => 'select',
120 'order_by' => 'menu_order',
121 'has_archives' => false,
122 );
123
124 $item_response = rest_ensure_response( $data );
125 $item_response->add_links( $this->prepare_links( (object) array( 'attribute_id' => $attribute_key ) ) );
126 $item_response = $this->prepare_response_for_collection(
127 $item_response
128 );
129
130 $response[] = $item_response;
131 }
132
133 return $response;
134 }
135
136 /**
137 * Get all attributes, with support for searching (which includes custom attributes).
138 *
139 * @param WP_REST_Request $request The API request.
140 * @return WP_REST_Response
141 */
142 public function get_items( $request ) {
143 if ( empty( $request['search'] ) ) {
144 return parent::get_items( $request );
145 }
146
147 $search_string = $request['search'];
148 $custom_attributes = $this->get_custom_attributes( array( 'name' => $search_string ) );
149 $matching_attributes = $this->format_custom_attribute_items_for_response( $custom_attributes );
150 $taxonomy_attributes = wc_get_attribute_taxonomies();
151
152 foreach ( $taxonomy_attributes as $attribute_obj ) {
153 // Skip taxonomy attributes that didn't match the query.
154 if ( false === stripos( $attribute_obj->attribute_label, $search_string ) ) {
155 continue;
156 }
157
158 $attribute = $this->prepare_item_for_response( $attribute_obj, $request );
159 $matching_attributes[] = $this->prepare_response_for_collection( $attribute );
160 }
161
162 $response = rest_ensure_response( $matching_attributes );
163 $response->header( 'X-WP-Total', count( $matching_attributes ) );
164 $response->header( 'X-WP-TotalPages', 1 );
165
166 return $response;
167 }
168 }
169