catalog = $catalog; $this->products = $products; $this->settings = $settings; } /** * {@inheritdoc} */ protected function capability(): string { return Capabilities::MANAGE_ORDERS; } /** * {@inheritdoc} */ protected function collection_args(): array { $args = $this->list_params( array_keys( ProductRepository::ORDERBY ), 'sort_order' ); $args['order']['default'] = 'asc'; $args['per_page']['default'] = 100; return array_merge( $args, array( 'group_id' => array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ), 'type' => array( 'type' => 'string', 'default' => '', 'enum' => array_merge( array( '' ), ProductType::all() ), 'validate_callback' => 'rest_validate_request_arg', ), 'status' => array( 'type' => 'string', 'default' => '', 'enum' => array_merge( array( '' ), ProductStatus::all() ), 'validate_callback' => 'rest_validate_request_arg', ), ) ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return array{items: array>, total: int} */ protected function query_items( WP_REST_Request $request ): array { return $this->products->query( array( 'search' => $request['search'], 'group_id' => $request['group_id'], 'type' => $request['type'], 'status' => $request['status'], 'orderby' => $request['orderby'], 'order' => $request['order'], 'page' => $request['page'], 'per_page' => $request['per_page'], ) ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int $id Item id. * @return array|null */ protected function find_item( int $id ): ?array { return $this->products->find( $id ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param array $body Request body. * @return array|\WP_Error */ protected function create_resource( array $body ) { return $this->catalog->create_product( $body ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int $id Item id. * @param array $body Request body. * @return array|\WP_Error */ protected function update_resource( int $id, array $body ) { return $this->catalog->update_product( $id, $body ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int $id Item id. * @return true|\WP_Error */ protected function delete_resource( int $id ) { return $this->catalog->delete_product( $id ); } /** * {@inheritdoc} */ protected function supports_reorder(): bool { return true; } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int[] $ids Ids in the wanted order. * @return void */ protected function reorder_resource( array $ids ): void { $this->catalog->reorder_products( $ids ); } /** * {@inheritdoc} */ protected function not_found_message(): string { return __( 'Product not found.', 'hostkhata' ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param array $item Item from the repository. * @return array */ protected function present( array $item ): array { $prices = array_map( function ( array $price ) { $price['price_formatted'] = $this->settings->format_money( $price['price'] ); $price['setup_fee_formatted'] = $this->settings->format_money( $price['setup_fee'] ); return $price; }, $item['prices'] ); return array( 'id' => $item['id'], 'group_id' => $item['group_id'], 'group_name' => $item['group_name'], 'type' => $item['type'], 'name' => $item['name'], 'slug' => $item['slug'], 'description' => $item['description'], 'features' => $item['features'], 'custom_fields' => $item['custom_fields'], 'addon_ids' => $item['addon_ids'], 'tax_class' => $item['tax_class'], 'stock_limit' => $item['stock_limit'], 'is_popular' => $item['is_popular'], 'status' => $item['status'], 'sort_order' => $item['sort_order'], 'prices' => $prices, ); } }