AI
1 year ago
Agentic
4 months ago
AbstractCartRoute.php
4 months ago
AbstractRoute.php
3 months ago
AbstractTermsRoute.php
4 months ago
Batch.php
4 months ago
Cart.php
1 year ago
CartAddItem.php
4 months ago
CartApplyCoupon.php
1 year ago
CartCoupons.php
3 months ago
CartCouponsByCode.php
1 year ago
CartExtensions.php
2 years ago
CartItems.php
2 years ago
CartItemsByKey.php
2 years ago
CartRemoveCoupon.php
1 year ago
CartRemoveItem.php
4 months ago
CartSelectShippingRate.php
5 months ago
CartUpdateCustomer.php
4 months ago
CartUpdateItem.php
4 months ago
Checkout.php
1 month ago
CheckoutOrder.php
1 year ago
Order.php
7 months ago
Patterns.php
1 year ago
ProductAttributeTerms.php
1 month ago
ProductAttributes.php
1 year ago
ProductAttributesById.php
1 year ago
ProductBrands.php
1 year ago
ProductBrandsById.php
1 year ago
ProductCategories.php
1 year ago
ProductCategoriesById.php
1 year ago
ProductCollectionData.php
11 months ago
ProductReviews.php
4 months ago
ProductTags.php
1 year ago
Products.php
3 months ago
ProductsById.php
3 months ago
ProductsBySlug.php
3 months ago
ShopperListItems.php
1 month ago
ShopperListItemsByKey.php
1 month ago
ShopperLists.php
1 month ago
ShopperListsBySlug.php
1 month ago
ShopperListsNonceCheck.php
1 month ago
AbstractTermsRoute.php
168 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Utilities\Pagination; |
| 5 | use WP_Term_Query; |
| 6 | |
| 7 | /** |
| 8 | * AbstractTermsRoute class. |
| 9 | */ |
| 10 | abstract class AbstractTermsRoute extends AbstractRoute { |
| 11 | /** |
| 12 | * The routes schema. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | const SCHEMA_TYPE = 'term'; |
| 17 | |
| 18 | /** |
| 19 | * Get the query params for collections of attributes. |
| 20 | * |
| 21 | * @return array |
| 22 | */ |
| 23 | public function get_collection_params() { |
| 24 | $params = array(); |
| 25 | $params['context'] = $this->get_context_param(); |
| 26 | $params['context']['default'] = 'view'; |
| 27 | |
| 28 | $params['page'] = array( |
| 29 | 'description' => __( 'Current page of the collection.', 'woocommerce' ), |
| 30 | 'type' => 'integer', |
| 31 | 'default' => 1, |
| 32 | 'sanitize_callback' => 'absint', |
| 33 | 'validate_callback' => 'rest_validate_request_arg', |
| 34 | 'minimum' => 1, |
| 35 | ); |
| 36 | |
| 37 | $params['per_page'] = array( |
| 38 | 'description' => __( 'Maximum number of items to be returned in result set. Defaults to no limit if left blank.', 'woocommerce' ), |
| 39 | 'type' => 'integer', |
| 40 | 'minimum' => 0, |
| 41 | 'sanitize_callback' => 'absint', |
| 42 | 'validate_callback' => 'rest_validate_request_arg', |
| 43 | ); |
| 44 | |
| 45 | $params['search'] = array( |
| 46 | 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ), |
| 47 | 'type' => 'string', |
| 48 | 'sanitize_callback' => 'sanitize_text_field', |
| 49 | 'validate_callback' => 'rest_validate_request_arg', |
| 50 | ); |
| 51 | |
| 52 | $params['exclude'] = array( |
| 53 | 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
| 54 | 'type' => 'array', |
| 55 | 'items' => array( |
| 56 | 'type' => 'integer', |
| 57 | ), |
| 58 | 'default' => array(), |
| 59 | 'sanitize_callback' => 'wp_parse_id_list', |
| 60 | ); |
| 61 | |
| 62 | $params['include'] = array( |
| 63 | 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
| 64 | 'type' => 'array', |
| 65 | 'items' => array( |
| 66 | 'type' => 'integer', |
| 67 | ), |
| 68 | 'default' => array(), |
| 69 | 'sanitize_callback' => 'wp_parse_id_list', |
| 70 | ); |
| 71 | |
| 72 | $params['order'] = array( |
| 73 | 'description' => __( 'Sort ascending or descending.', 'woocommerce' ), |
| 74 | 'type' => 'string', |
| 75 | 'default' => 'asc', |
| 76 | 'enum' => array( 'asc', 'desc' ), |
| 77 | 'validate_callback' => 'rest_validate_request_arg', |
| 78 | ); |
| 79 | |
| 80 | $params['orderby'] = array( |
| 81 | 'description' => __( 'Sort by term property.', 'woocommerce' ), |
| 82 | 'type' => 'string', |
| 83 | 'default' => 'name', |
| 84 | 'enum' => array( |
| 85 | 'name', |
| 86 | 'slug', |
| 87 | 'count', |
| 88 | ), |
| 89 | 'validate_callback' => 'rest_validate_request_arg', |
| 90 | ); |
| 91 | |
| 92 | $params['hide_empty'] = array( |
| 93 | 'description' => __( 'If true, empty terms will not be returned.', 'woocommerce' ), |
| 94 | 'type' => 'boolean', |
| 95 | 'default' => true, |
| 96 | ); |
| 97 | |
| 98 | $params['parent'] = array( |
| 99 | 'description' => __( 'Limit results to terms with a specific parent (hierarchical taxonomies only).', 'woocommerce' ), |
| 100 | 'type' => 'integer', |
| 101 | 'sanitize_callback' => 'absint', |
| 102 | 'validate_callback' => 'rest_validate_request_arg', |
| 103 | ); |
| 104 | |
| 105 | return $params; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get terms matching passed in args. |
| 110 | * |
| 111 | * @param string $taxonomy Taxonomy to get terms from. |
| 112 | * @param \WP_REST_Request $request Request object. |
| 113 | * |
| 114 | * @return \WP_REST_Response |
| 115 | */ |
| 116 | protected function get_terms_response( $taxonomy, $request ) { |
| 117 | $page = (int) $request['page']; |
| 118 | $per_page = $request['per_page'] ? (int) $request['per_page'] : 0; |
| 119 | $prepared_args = array( |
| 120 | 'taxonomy' => $taxonomy, |
| 121 | 'exclude' => $request['exclude'], |
| 122 | 'include' => $request['include'], |
| 123 | 'order' => $request['order'], |
| 124 | 'orderby' => $request['orderby'], |
| 125 | 'hide_empty' => (bool) $request['hide_empty'], |
| 126 | 'number' => $per_page, |
| 127 | 'offset' => $per_page > 0 ? ( $page - 1 ) * $per_page : 0, |
| 128 | 'search' => $request['search'], |
| 129 | ); |
| 130 | |
| 131 | if ( isset( $request['parent'] ) && is_taxonomy_hierarchical( $taxonomy ) ) { |
| 132 | $prepared_args['parent'] = (int) $request['parent']; |
| 133 | } |
| 134 | |
| 135 | $term_query = new WP_Term_Query(); |
| 136 | $objects = $term_query->query( $prepared_args ); |
| 137 | $return = []; |
| 138 | |
| 139 | foreach ( $objects as $object ) { |
| 140 | $data = $this->prepare_item_for_response( $object, $request ); |
| 141 | $return[] = $this->prepare_response_for_collection( $data ); |
| 142 | } |
| 143 | |
| 144 | $response = rest_ensure_response( $return ); |
| 145 | |
| 146 | // See if pagination is needed before calculating. |
| 147 | if ( $per_page > 0 && ( count( $objects ) === $per_page || $page > 1 ) ) { |
| 148 | $term_count = $this->get_term_count( $taxonomy, $prepared_args ); |
| 149 | $response = ( new Pagination() )->add_headers( $response, $request, $term_count, ceil( $term_count / $per_page ) ); |
| 150 | } |
| 151 | |
| 152 | return $response; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get count of terms for current query. |
| 157 | * |
| 158 | * @param string $taxonomy Taxonomy to get terms from. |
| 159 | * @param array $args Array of args to pass to wp_count_terms. |
| 160 | * @return int |
| 161 | */ |
| 162 | protected function get_term_count( $taxonomy, $args ) { |
| 163 | $count_args = $args; |
| 164 | unset( $count_args['number'], $count_args['offset'] ); |
| 165 | return (int) wp_count_terms( $taxonomy, $count_args ); |
| 166 | } |
| 167 | } |
| 168 |