ApiController.php
6 days ago
AsyncGenerator.php
6 days ago
FeedValidator.php
6 months ago
POSIntegration.php
6 days ago
POSProductVisibilitySync.php
6 days ago
ProductMapper.php
6 days ago
POSIntegration.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * POS Catalog Integration class. |
| 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 Automattic\WooCommerce\Internal\ProductFeed\Feed\FeedInterface; |
| 14 | use Automattic\WooCommerce\Internal\ProductFeed\Feed\FeedValidatorInterface; |
| 15 | use Automattic\WooCommerce\Internal\ProductFeed\Feed\ResumableFeedInterface; |
| 16 | use Automattic\WooCommerce\Internal\ProductFeed\Integrations\IntegrationInterface; |
| 17 | use Automattic\WooCommerce\Internal\ProductFeed\Storage\JsonFileFeed; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * POS Catalog Integration |
| 25 | * |
| 26 | * @since 10.5.0 |
| 27 | */ |
| 28 | class POSIntegration implements IntegrationInterface { |
| 29 | /** |
| 30 | * Container instance. |
| 31 | * |
| 32 | * @var Container |
| 33 | */ |
| 34 | private Container $container; |
| 35 | |
| 36 | /** |
| 37 | * Dependency injector. |
| 38 | * |
| 39 | * @param Container $container Dependency container. |
| 40 | * @internal |
| 41 | */ |
| 42 | final public function init( Container $container ): void { |
| 43 | $this->container = $container; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritdoc} |
| 48 | */ |
| 49 | public function get_id(): string { |
| 50 | return 'pos'; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritdoc} |
| 55 | */ |
| 56 | public function get_product_feed_query_args(): array { |
| 57 | return array( |
| 58 | 'type' => array( 'simple', 'variable', 'variation' ), |
| 59 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 60 | 'tax_query' => array( |
| 61 | array( |
| 62 | 'taxonomy' => 'pos_product_visibility', |
| 63 | 'field' => 'slug', |
| 64 | 'terms' => 'pos-hidden', |
| 65 | 'operator' => 'NOT IN', |
| 66 | ), |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function register_hooks(): void { |
| 75 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 76 | $this->container->get( AsyncGenerator::class )->register_hooks(); |
| 77 | $this->container->get( POSProductVisibilitySync::class )->register_hooks(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Initialize the REST API. |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public function rest_api_init(): void { |
| 86 | // Only load the controller when necessary. |
| 87 | $this->container->get( ApiController::class )->register_routes(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * {@inheritdoc} |
| 92 | */ |
| 93 | public function activate(): void { |
| 94 | // At the moment, there are no activation steps for the POS catalog. |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * {@inheritdoc} |
| 99 | */ |
| 100 | public function deactivate(): void { |
| 101 | // At the moment, there are no deactivation steps for the POS catalog. |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritdoc} |
| 106 | * |
| 107 | * POS catalog feeds are generated in chunks across multiple processes, so this returns a feed that |
| 108 | * supports the resumable lifecycle. Narrowing the return type (a covariant override of the base |
| 109 | * {@see FeedInterface} contract) guarantees that at the language level for callers such as |
| 110 | * {@see AsyncGenerator}, instead of relying on a PHPDoc hint or a runtime check. |
| 111 | * |
| 112 | * @return ResumableFeedInterface |
| 113 | */ |
| 114 | public function create_feed(): ResumableFeedInterface { |
| 115 | return new JsonFileFeed( 'pos-catalog-feed' ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * {@inheritdoc} |
| 120 | */ |
| 121 | public function get_product_mapper(): ProductMapper { |
| 122 | return $this->container->get( ProductMapper::class ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
| 128 | public function get_feed_validator(): FeedValidatorInterface { |
| 129 | return $this->container->get( FeedValidator::class ); |
| 130 | } |
| 131 | } |
| 132 |