FeedInterface.php
2 weeks ago
FeedValidatorInterface.php
5 months ago
ProductLoader.php
5 months ago
ProductMapperInterface.php
5 months ago
ProductWalker.php
5 months ago
WalkerProgress.php
5 months ago
ProductWalker.php
289 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Product Walker class. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\Internal\ProductFeed |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\ProductFeed\Feed; |
| 11 | |
| 12 | use Automattic\WooCommerce\Internal\ProductFeed\Integrations\IntegrationInterface; |
| 13 | use Automattic\WooCommerce\Internal\ProductFeed\Utils\MemoryManager; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Walker for products. |
| 21 | * |
| 22 | * @since 10.5.0 |
| 23 | */ |
| 24 | class ProductWalker { |
| 25 | /** |
| 26 | * The product loader. |
| 27 | * |
| 28 | * @var ProductLoader |
| 29 | */ |
| 30 | private ProductLoader $product_loader; |
| 31 | |
| 32 | /** |
| 33 | * The product mapper. |
| 34 | * |
| 35 | * @var ProductMapperInterface |
| 36 | */ |
| 37 | private ProductMapperInterface $mapper; |
| 38 | |
| 39 | /** |
| 40 | * The feed. |
| 41 | * |
| 42 | * @var FeedInterface |
| 43 | */ |
| 44 | private FeedInterface $feed; |
| 45 | |
| 46 | /** |
| 47 | * The feed validator. |
| 48 | * |
| 49 | * @var FeedValidatorInterface |
| 50 | */ |
| 51 | private FeedValidatorInterface $validator; |
| 52 | |
| 53 | /** |
| 54 | * The memory manager. |
| 55 | * |
| 56 | * @var MemoryManager |
| 57 | */ |
| 58 | private MemoryManager $memory_manager; |
| 59 | |
| 60 | /** |
| 61 | * The number of products to iterate through per batch. |
| 62 | * |
| 63 | * @var int |
| 64 | */ |
| 65 | private int $per_page = 100; |
| 66 | |
| 67 | /** |
| 68 | * The time limit to extend the execution time limit per batch. |
| 69 | * |
| 70 | * @var int |
| 71 | */ |
| 72 | private int $time_limit = 0; |
| 73 | |
| 74 | /** |
| 75 | * The query arguments to apply to the product query. |
| 76 | * |
| 77 | * @var array |
| 78 | */ |
| 79 | private array $query_args; |
| 80 | |
| 81 | /** |
| 82 | * Class constructor. |
| 83 | * |
| 84 | * This class will not be available through DI. Instead, it needs to be instantiated directly. |
| 85 | * |
| 86 | * @param ProductMapperInterface $mapper The product mapper. |
| 87 | * @param FeedValidatorInterface $validator The feed validator. |
| 88 | * @param FeedInterface $feed The feed. |
| 89 | * @param ProductLoader $product_loader The product loader. |
| 90 | * @param MemoryManager $memory_manager The memory manager. |
| 91 | * @param array $query_args The query arguments. |
| 92 | */ |
| 93 | private function __construct( |
| 94 | ProductMapperInterface $mapper, |
| 95 | FeedValidatorInterface $validator, |
| 96 | FeedInterface $feed, |
| 97 | ProductLoader $product_loader, |
| 98 | MemoryManager $memory_manager, |
| 99 | array $query_args |
| 100 | ) { |
| 101 | $this->mapper = $mapper; |
| 102 | $this->validator = $validator; |
| 103 | $this->feed = $feed; |
| 104 | $this->product_loader = $product_loader; |
| 105 | $this->memory_manager = $memory_manager; |
| 106 | $this->query_args = $query_args; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a new instance of the ProductWalker class based on an integration. |
| 111 | * |
| 112 | * The walker will mostly be set up based on the integration. |
| 113 | * The feed is provided externally, as it might be based on the context (CLI, REST, Action Scheduler, etc.). |
| 114 | * |
| 115 | * @since 10.5.0 |
| 116 | * |
| 117 | * @param IntegrationInterface $integration The integration. |
| 118 | * @param FeedInterface $feed The feed. |
| 119 | * @return self The ProductWalker instance. |
| 120 | */ |
| 121 | public static function from_integration( |
| 122 | IntegrationInterface $integration, |
| 123 | FeedInterface $feed |
| 124 | ): self { |
| 125 | $query_args = array_merge( |
| 126 | array( |
| 127 | 'status' => array( 'publish' ), |
| 128 | 'return' => 'objects', |
| 129 | ), |
| 130 | $integration->get_product_feed_query_args() |
| 131 | ); |
| 132 | |
| 133 | /** |
| 134 | * Allows the base arguments for querying products for product feeds to be changed. |
| 135 | * |
| 136 | * Variable products are not included by default, as their variations will be included. |
| 137 | * |
| 138 | * @since 10.5.0 |
| 139 | * |
| 140 | * @param array $query_args The arguments to pass to wc_get_products(). |
| 141 | * @param IntegrationInterface $integration The integration that the query belongs to. |
| 142 | * @return array |
| 143 | */ |
| 144 | $query_args = apply_filters( |
| 145 | 'woocommerce_product_feed_args', |
| 146 | $query_args, |
| 147 | $integration |
| 148 | ); |
| 149 | |
| 150 | $instance = new self( |
| 151 | $integration->get_product_mapper(), |
| 152 | $integration->get_feed_validator(), |
| 153 | $feed, |
| 154 | wc_get_container()->get( ProductLoader::class ), |
| 155 | wc_get_container()->get( MemoryManager::class ), |
| 156 | $query_args |
| 157 | ); |
| 158 | |
| 159 | return $instance; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set the number of products to iterate through per batch. |
| 164 | * |
| 165 | * @since 10.5.0 |
| 166 | * |
| 167 | * @param int $batch_size The number of products to iterate through per batch. |
| 168 | * @return self |
| 169 | */ |
| 170 | public function set_batch_size( int $batch_size ): self { |
| 171 | if ( $batch_size < 1 ) { |
| 172 | $batch_size = 1; |
| 173 | } |
| 174 | |
| 175 | $this->per_page = $batch_size; |
| 176 | return $this; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set the time limit to extend the execution time limit per batch. |
| 181 | * |
| 182 | * @since 10.5.0 |
| 183 | * |
| 184 | * @param int $time_limit Time limit in seconds. |
| 185 | * @return self |
| 186 | */ |
| 187 | public function add_time_limit( int $time_limit ): self { |
| 188 | if ( $time_limit < 0 ) { |
| 189 | $time_limit = 0; |
| 190 | } |
| 191 | |
| 192 | $this->time_limit = $time_limit; |
| 193 | return $this; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Walks through all products. |
| 198 | * |
| 199 | * @since 10.5.0 |
| 200 | * |
| 201 | * @param callable $callback The callback to call after each batch of products is processed. |
| 202 | * @return int The total number of products processed. |
| 203 | */ |
| 204 | public function walk( ?callable $callback = null ): int { |
| 205 | $progress = null; |
| 206 | |
| 207 | // Instruct the feed to start. |
| 208 | $this->feed->start(); |
| 209 | |
| 210 | // Check how much memory is available at first. |
| 211 | $initial_available_memory = $this->memory_manager->get_available_memory(); |
| 212 | |
| 213 | do { |
| 214 | $result = $this->iterate( $this->query_args, $progress ? $progress->processed_batches + 1 : 1, $this->per_page ); |
| 215 | $iterated = count( $result->products ); |
| 216 | |
| 217 | // Only done when the progress is not set. Will be modified otherwise. |
| 218 | if ( is_null( $progress ) ) { |
| 219 | $progress = WalkerProgress::from_wc_get_products_result( $result ); |
| 220 | } |
| 221 | $progress->processed_items += $iterated; |
| 222 | ++$progress->processed_batches; |
| 223 | |
| 224 | if ( is_callable( $callback ) && $iterated > 0 ) { |
| 225 | $callback( $progress ); |
| 226 | } |
| 227 | |
| 228 | if ( $this->time_limit > 0 ) { |
| 229 | set_time_limit( $this->time_limit ); |
| 230 | } |
| 231 | |
| 232 | // We don't want to use more than half of the available memory at the beginning of the script. |
| 233 | $current_memory = $this->memory_manager->get_available_memory(); |
| 234 | if ( $initial_available_memory - $current_memory >= $initial_available_memory / 2 ) { |
| 235 | $this->memory_manager->flush_caches(); |
| 236 | } |
| 237 | } while ( |
| 238 | // If `wc_get_products()` returns less than the batch size, it was the last page. |
| 239 | $iterated === $this->per_page |
| 240 | |
| 241 | // For the cases where the above is true, make sure that we do not exceed the total number of pages. |
| 242 | && $progress->processed_batches < $progress->total_batch_count |
| 243 | ); |
| 244 | |
| 245 | // Instruct the feed to end. |
| 246 | $this->feed->end(); |
| 247 | |
| 248 | return $progress->processed_items; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Iterates through a batch of products. |
| 253 | * |
| 254 | * @param array $args The arguments to pass to wc_get_products(). |
| 255 | * @param int $page The page number to iterate through. |
| 256 | * @param int $limit The maximum number of products to iterate through. |
| 257 | * @return \stdClass The result of the query with properties: products, total, max_num_pages. |
| 258 | */ |
| 259 | private function iterate( array $args = array(), int $page = 1, int $limit = 100 ): \stdClass { |
| 260 | /** |
| 261 | * Result is always stdClass when paginate=true. |
| 262 | * |
| 263 | * @var \stdClass $result |
| 264 | */ |
| 265 | $result = $this->product_loader->get_products( |
| 266 | array_merge( |
| 267 | $args, |
| 268 | array( |
| 269 | 'page' => $page, |
| 270 | 'limit' => $limit, |
| 271 | 'paginate' => true, |
| 272 | ) |
| 273 | ) |
| 274 | ); |
| 275 | |
| 276 | foreach ( $result->products as $product ) { |
| 277 | $mapped_data = $this->mapper->map_product( $product ); |
| 278 | |
| 279 | if ( ! empty( $this->validator->validate_entry( $mapped_data, $product ) ) ) { |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | $this->feed->add_entry( $mapped_data ); |
| 284 | } |
| 285 | |
| 286 | return $result; |
| 287 | } |
| 288 | } |
| 289 |