/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/API/ProductAttributes.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 167 lines · 93 code · 26 blank · 48 comment · 5 complexity · 650851cc02b1dc434fa58da862f5e78b MD5 · raw file

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