ApiController.php
5 months ago
AsyncGenerator.php
4 months ago
FeedValidator.php
5 months ago
POSIntegration.php
5 months ago
POSProductVisibilitySync.php
5 months ago
ProductMapper.php
5 months ago
ApiController.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * POS Catalog API Controller. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\Internal\ProductFeed |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\ProductFeed\Integrations\POSCatalog; |
| 11 | |
| 12 | use Automattic\WooCommerce\Container; |
| 13 | use WP_REST_Request; |
| 14 | use WP_REST_Response; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * POS Catalog API Controller. |
| 22 | * |
| 23 | * @since 10.5.0 |
| 24 | */ |
| 25 | class ApiController { |
| 26 | const ROUTE_NAMESPACE = 'wc/pos/v1/catalog'; |
| 27 | |
| 28 | /** |
| 29 | * Container instance. |
| 30 | * |
| 31 | * @var Container |
| 32 | */ |
| 33 | private $container; |
| 34 | |
| 35 | /** |
| 36 | * Dependency injector. |
| 37 | * |
| 38 | * @param Container $container The container instance. Everything else will be dynamic. |
| 39 | * @internal |
| 40 | */ |
| 41 | final public function init( Container $container ): void { |
| 42 | $this->container = $container; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Register the routes for the API controller. |
| 47 | */ |
| 48 | public function register_routes(): void { |
| 49 | register_rest_route( |
| 50 | self::ROUTE_NAMESPACE, |
| 51 | '/create', |
| 52 | array( |
| 53 | 'methods' => 'POST', |
| 54 | 'callback' => array( $this, 'generate_feed' ), |
| 55 | 'permission_callback' => array( $this, 'is_authorized' ), |
| 56 | 'args' => array( |
| 57 | 'force' => array( |
| 58 | 'type' => 'boolean', |
| 59 | 'default' => false, |
| 60 | 'description' => 'Force regeneration of the feed. NOOP if generation is in progress.', |
| 61 | ), |
| 62 | '_product_fields' => array( |
| 63 | 'type' => 'string', |
| 64 | 'description' => 'Comma-separated list of fields to include for non-variable products.', |
| 65 | 'required' => false, |
| 66 | ), |
| 67 | '_variation_fields' => array( |
| 68 | 'type' => 'string', |
| 69 | 'description' => 'Comma-separated list of fields to include for variations.', |
| 70 | 'required' => false, |
| 71 | ), |
| 72 | ), |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks if the current user has the necessary permissions to access the API. |
| 79 | * |
| 80 | * @return bool True if the user has the necessary permissions, false otherwise. |
| 81 | */ |
| 82 | public function is_authorized() { |
| 83 | return is_user_logged_in() && ( |
| 84 | current_user_can( 'manage_woocommerce' ) || current_user_can( 'manage_options' ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Starts generating a feed. |
| 90 | * |
| 91 | * @param WP_REST_Request<array<string, mixed>> $request The request object. |
| 92 | * @return WP_REST_Response The response object. |
| 93 | */ |
| 94 | public function generate_feed( WP_REST_Request $request ): WP_REST_Response { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint |
| 95 | $generator = $this->container->get( AsyncGenerator::class ); |
| 96 | try { |
| 97 | $params = array(); |
| 98 | if ( null !== $request['_product_fields'] ) { |
| 99 | $params['_product_fields'] = $request['_product_fields']; |
| 100 | } |
| 101 | if ( null !== $request['_variation_fields'] ) { |
| 102 | $params['_variation_fields'] = $request['_variation_fields']; |
| 103 | } |
| 104 | |
| 105 | $response = $request->get_param( 'force' ) |
| 106 | ? $generator->force_regeneration( $params ) |
| 107 | : $generator->get_status( $params ); |
| 108 | |
| 109 | // Use the right datetime format. |
| 110 | if ( isset( $response['scheduled_at'] ) ) { |
| 111 | $response['scheduled_at'] = wc_rest_prepare_date_response( $response['scheduled_at'] ); |
| 112 | } |
| 113 | if ( isset( $response['completed_at'] ) ) { |
| 114 | $response['completed_at'] = wc_rest_prepare_date_response( $response['completed_at'] ); |
| 115 | } |
| 116 | |
| 117 | // Remove sensitive data from the response. |
| 118 | if ( isset( $response['action_id'] ) ) { |
| 119 | unset( $response['action_id'] ); |
| 120 | } |
| 121 | if ( isset( $response['path'] ) ) { |
| 122 | unset( $response['path'] ); |
| 123 | } |
| 124 | } catch ( \Exception $e ) { |
| 125 | wc_get_logger()->error( |
| 126 | 'Feed generation failed', |
| 127 | array( 'error' => $e->getMessage() ) |
| 128 | ); |
| 129 | return new WP_REST_Response( |
| 130 | array( |
| 131 | 'success' => false, |
| 132 | 'message' => __( 'An error occurred while generating the feed.', 'woocommerce' ), |
| 133 | ), |
| 134 | 500 |
| 135 | ); |
| 136 | } |
| 137 | return new WP_REST_Response( $response ); |
| 138 | } |
| 139 | } |
| 140 |