Traits
4 weeks ago
AbstractDomainAbility.php
4 weeks ago
OrderAddNote.php
4 weeks ago
OrderUpdateStatus.php
4 weeks ago
OrdersQuery.php
4 weeks ago
ProductCreate.php
4 weeks ago
ProductDelete.php
4 weeks ago
ProductUpdate.php
4 weeks ago
ProductsQuery.php
4 weeks ago
ProductsQuery.php
212 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Products query ability definition file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Abilities\Domain; |
| 9 | |
| 10 | use Automattic\WooCommerce\Abilities\AbilityDefinition; |
| 11 | use Automattic\WooCommerce\Enums\ProductType; |
| 12 | use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\ProductAbilityTrait; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Registers the WooCommerce products query ability. |
| 18 | */ |
| 19 | class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition { |
| 20 | |
| 21 | use ProductAbilityTrait; |
| 22 | |
| 23 | /** |
| 24 | * Get the ability name. |
| 25 | * |
| 26 | * @return string |
| 27 | * |
| 28 | * @since 10.9.0 |
| 29 | */ |
| 30 | public static function get_name(): string { |
| 31 | return 'woocommerce/products-query'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the ability registration arguments. |
| 36 | * |
| 37 | * @return array |
| 38 | * |
| 39 | * @since 10.9.0 |
| 40 | */ |
| 41 | public static function get_registration_args(): array { |
| 42 | return array( |
| 43 | 'label' => __( 'Query products', 'woocommerce' ), |
| 44 | 'description' => __( |
| 45 | 'Find products by ID or common catalog filters.', |
| 46 | 'woocommerce' |
| 47 | ), |
| 48 | 'category' => 'woocommerce', |
| 49 | 'input_schema' => self::get_input_schema(), |
| 50 | 'output_schema' => self::get_collection_output_schema( 'products', self::get_product_output_schema() ), |
| 51 | 'execute_callback' => array( __CLASS__, 'execute' ), |
| 52 | 'permission_callback' => array( __CLASS__, 'can_query_products' ), |
| 53 | 'meta' => array( |
| 54 | 'show_in_rest' => true, |
| 55 | 'mcp' => array( |
| 56 | 'public' => true, |
| 57 | 'type' => 'tool', |
| 58 | ), |
| 59 | 'annotations' => array( |
| 60 | 'readonly' => true, |
| 61 | 'idempotent' => true, |
| 62 | 'destructive' => false, |
| 63 | ), |
| 64 | ), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Query products. |
| 70 | * |
| 71 | * @param array $input Ability input. |
| 72 | * @return array|\WP_Error |
| 73 | * |
| 74 | * @since 10.9.0 |
| 75 | */ |
| 76 | public static function execute( array $input ) { |
| 77 | if ( ! empty( $input['id'] ) ) { |
| 78 | $product = self::get_product_from_input( $input ); |
| 79 | |
| 80 | if ( is_wp_error( $product ) ) { |
| 81 | return $product; |
| 82 | } |
| 83 | |
| 84 | if ( $product->is_type( ProductType::VARIATION ) ) { |
| 85 | return new \WP_Error( |
| 86 | 'woocommerce_product_type_unsupported', |
| 87 | __( 'Product type is not supported by this ability.', 'woocommerce' ), |
| 88 | array( 'status' => 400 ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | return array( |
| 93 | 'products' => array( self::format_product_for_response( $product ) ), |
| 94 | 'total_pages' => 1, |
| 95 | 'page' => 1, |
| 96 | 'per_page' => 1, |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $page = (int) ( $input['page'] ?? 1 ); |
| 101 | $per_page = (int) ( $input['per_page'] ?? 10 ); |
| 102 | $args = array( |
| 103 | 'limit' => $per_page, |
| 104 | 'page' => $page, |
| 105 | 'paginate' => true, |
| 106 | 'return' => 'objects', |
| 107 | ); |
| 108 | |
| 109 | foreach ( array( 'status', 'sku', 'stock_status' ) as $field ) { |
| 110 | if ( ! empty( $input[ $field ] ) ) { |
| 111 | $args[ $field ] = wc_clean( $input[ $field ] ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if ( ! empty( $input['product_type_alias'] ) && is_scalar( $input['product_type_alias'] ) ) { |
| 116 | $product_type_alias = sanitize_text_field( (string) $input['product_type_alias'] ); |
| 117 | $type_args = self::get_product_query_args_for_alias( $product_type_alias ); |
| 118 | |
| 119 | if ( is_wp_error( $type_args ) ) { |
| 120 | return $type_args; |
| 121 | } |
| 122 | |
| 123 | $args = array_merge( $args, $type_args ); |
| 124 | } |
| 125 | |
| 126 | if ( ! empty( $input['search'] ) ) { |
| 127 | $args['s'] = wc_clean( $input['search'] ); |
| 128 | } |
| 129 | |
| 130 | $results = wc_get_products( $args ); |
| 131 | $products = is_object( $results ) && isset( $results->products ) ? $results->products : array(); |
| 132 | $pages = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : ( count( $products ) > 0 ? 1 : 0 ); |
| 133 | |
| 134 | return array( |
| 135 | 'products' => array_map( |
| 136 | static function ( $product ) { |
| 137 | return self::format_product_for_response( $product ); |
| 138 | }, |
| 139 | $products |
| 140 | ), |
| 141 | 'total_pages' => $pages, |
| 142 | 'page' => $page, |
| 143 | 'per_page' => $per_page, |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Check product read access. |
| 149 | * |
| 150 | * @param mixed $input Ability input. |
| 151 | * @return bool |
| 152 | * |
| 153 | * @since 10.9.0 |
| 154 | */ |
| 155 | public static function can_query_products( $input = array() ): bool { |
| 156 | $product_id = self::get_id_from_input( $input ); |
| 157 | |
| 158 | return wc_rest_check_post_permissions( 'product', 'read', $product_id ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the ability input schema. |
| 163 | * |
| 164 | * @return array |
| 165 | */ |
| 166 | private static function get_input_schema(): array { |
| 167 | return array( |
| 168 | 'type' => 'object', |
| 169 | 'properties' => array( |
| 170 | 'id' => array( |
| 171 | 'type' => 'integer', |
| 172 | 'minimum' => 1, |
| 173 | ), |
| 174 | 'search' => array( 'type' => 'string' ), |
| 175 | 'sku' => array( |
| 176 | 'type' => 'string', |
| 177 | 'description' => __( 'Limit results to products with SKUs that partially match this string. Use * to match products with any non-empty SKU.', 'woocommerce' ), |
| 178 | ), |
| 179 | 'status' => array( |
| 180 | 'type' => 'string', |
| 181 | 'enum' => self::get_product_query_status_slugs(), |
| 182 | ), |
| 183 | 'product_type_alias' => array( |
| 184 | 'type' => 'string', |
| 185 | 'description' => __( |
| 186 | 'Filter by supported agent-facing product type alias. physical maps to simple shippable, non-downloadable products; virtual maps to simple non-shipping, non-downloadable products; digital maps to simple virtual/downloadable products; affiliate maps to the external product type; grouped maps to grouped.', |
| 187 | 'woocommerce' |
| 188 | ), |
| 189 | 'enum' => self::get_supported_product_type_aliases(), |
| 190 | ), |
| 191 | 'stock_status' => array( |
| 192 | 'type' => 'string', |
| 193 | 'enum' => array_keys( wc_get_product_stock_status_options() ), |
| 194 | ), |
| 195 | 'page' => array( |
| 196 | 'type' => 'integer', |
| 197 | 'default' => 1, |
| 198 | 'minimum' => 1, |
| 199 | ), |
| 200 | 'per_page' => array( |
| 201 | 'type' => 'integer', |
| 202 | 'default' => 10, |
| 203 | 'minimum' => 1, |
| 204 | 'maximum' => 100, |
| 205 | ), |
| 206 | ), |
| 207 | 'additionalProperties' => false, |
| 208 | 'default' => array(), |
| 209 | ); |
| 210 | } |
| 211 | } |
| 212 |