AI
1 year ago
RateLimits
4 weeks ago
Reports
4 weeks ago
Templates
3 years ago
views
4 weeks ago
AnalyticsImports.php
5 months ago
Coupons.php
4 years ago
CustomAttributeTraits.php
4 years ago
Customers.php
4 years ago
Data.php
4 years ago
DataCountries.php
4 years ago
DataDownloadIPs.php
4 years ago
Experiments.php
4 years ago
Features.php
4 years ago
Init.php
4 weeks ago
LaunchYourStore.php
1 year ago
Leaderboards.php
1 year ago
Marketing.php
2 months ago
MarketingCampaignTypes.php
3 years ago
MarketingCampaigns.php
2 years ago
MarketingChannels.php
3 years ago
MarketingOverview.php
2 months ago
MarketingRecommendations.php
2 years ago
MobileAppMagicLink.php
3 years ago
MobileAppQRLogin.php
4 weeks ago
NoteActions.php
4 weeks ago
Notes.php
4 weeks ago
Notice.php
1 year ago
OnboardingFreeExtensions.php
1 year ago
OnboardingPlugins.php
4 weeks ago
OnboardingProductTypes.php
4 years ago
OnboardingProducts.php
2 years ago
OnboardingProfile.php
1 year ago
OnboardingTasks.php
9 months ago
OnboardingThemes.php
9 months ago
Options.php
4 weeks ago
Orders.php
1 year ago
PaymentGatewaySuggestions.php
2 months ago
Plugins.php
2 months ago
ProductAttributeTerms.php
4 years ago
ProductAttributes.php
4 years ago
ProductCategories.php
4 years ago
ProductForm.php
3 years ago
ProductReviews.php
4 years ago
ProductVariations.php
1 year ago
Products.php
1 year ago
ProductsLowInStock.php
4 months ago
SettingOptions.php
3 years ago
ShippingPartnerSuggestions.php
1 month ago
Taxes.php
4 years ago
Themes.php
2 years ago
ProductAttributes.php
169 lines
| 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 |