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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ProductFeed / Feed / ProductWalker.php
woocommerce / src / Internal / ProductFeed / Feed Last commit date
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