-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFeeds.php
More file actions
455 lines (405 loc) · 13.8 KB
/
Feeds.php
File metadata and controls
455 lines (405 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* Pinterest for WooCommerce Feeds related helper methods
*
* @package Pinterest_For_WooCommerce/Classes/
* @version 1.0.0
*/
namespace Automattic\WooCommerce\Pinterest;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\Exception\FeedNotFoundException;
use Automattic\WooCommerce\Pinterest\Exception\PinterestApiLocaleException;
use Automattic\WooCommerce\Pinterest\Notes\FeedDeletionFailure;
use Exception;
use Pinterest_For_Woocommerce;
use Throwable;
/**
* Class handling fetch methods for feed profiles.
*/
class Feeds {
/**
* Feed ACTIVE status which mirrors the Pinterest API feed status.
*/
const FEED_STATUS_ACTIVE = 'ACTIVE';
/**
* Feed INACTIVE status which mirrors the Pinterest API feed status.
*/
const FEED_STATUS_INACTIVE = 'INACTIVE';
/**
* Feed DELETED status which mirrors the Pinterest API feed status.
*/
const FEED_STATUS_DELETED = 'DELETED';
/**
* Feed DOES_NOT_EXIST status which is a custom status.
* Represents a feed that was never created.
* In case fetching the feed returns no results.
*/
const FEED_STATUS_DOES_NOT_EXIST = 'DOES_NOT_EXIST';
/**
* Feed COMPLETED status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_COMPLETED = 'COMPLETED';
/**
* Feed COMPLETED_EARLY status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_COMPLETED_EARLY = 'COMPLETED_EARLY';
/**
* Feed DISAPPROVED status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_DISAPPROVED = 'DISAPPROVED';
/**
* Feed STATUS_FAILED status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_FAILED = 'FAILED';
/**
* Feed PROCESSING status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_PROCESSING = 'PROCESSING';
/**
* Feed QUEUED_FOR_PROCESSING status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_QUEUED_FOR_PROCESSING = 'QUEUED_FOR_PROCESSING';
/**
* Feed UNDER_APPEAL status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_UNDER_APPEAL = 'UNDER_APPEAL';
/**
* Feed UNDER_REVIEW status which mirrors the Pinterest API feed processing result status.
*/
const FEED_PROCESSING_STATUS_UNDER_REVIEW = 'UNDER_REVIEW';
/**
* Create a new feed for the given ad account.
*
* @since 1.4.0
*
* @return string The Feed ID or an empty string if failed.
* @throws PinterestApiException Pinterest API Exception if there is an error creating the feed.
*/
public static function create_feed(): string {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
$configs = LocalFeedConfigs::get_instance()->get_configurations();
$config = reset( $configs );
$name = (string) parse_url( esc_url( get_site_url() ), PHP_URL_HOST );
$default_country = Pinterest_For_Woocommerce()::get_base_country();
$default_currency = get_woocommerce_currency();
try {
$default_locale = LocaleMapper::get_locale_for_api();
} catch ( PinterestApiLocaleException $e ) {
$default_locale = LocaleMapper::PINTEREST_DEFAULT_LOCALE;
}
/**
* Filters the default feed name: pinterest_for_woocommerce_unique_feed_name.
* This vale appears in the Catalogues - Data sources page at Pinterest.
*
* @since 1.4.0
*
* @param string $feed_name The default feed name.
*/
$feed_name = apply_filters(
'pinterest_for_woocommerce_unique_feed_name',
sprintf(
// translators: %1$s is a country ISO 2 code, %2$s is a currency ISO 3 code.
esc_html__( 'Created by Pinterest for WooCommerce at %1$s %2$s|%3$s|%4$s', 'pinterest-for-woocommerce' ),
esc_html( $name ),
esc_html( $default_country ),
esc_html( $default_locale ),
esc_html( $default_currency )
)
);
$data = array(
'name' => $feed_name,
'format' => 'XML',
'location' => $config['feed_url'],
'catalog_type' => 'RETAIL',
'default_currency' => $default_currency,
'default_locale' => $default_locale,
'default_country' => $default_country,
'default_availability' => 'IN_STOCK',
);
$cache_key = PINTEREST_FOR_WOOCOMMERCE_PREFIX . '_request_' . md5( wp_json_encode( $data ) . (string) $ad_account_id );
$cached_error = get_transient( $cache_key );
if ( false !== $cached_error ) {
// Faking Pinterest API response to 425 Too early.
throw new PinterestApiException(
sprintf(
/* translators: Pinterest API error code and message. 1: Cached error string. */
esc_html__(
'Previous request for the same action failed due to: %1$s. Delaying the next call to prevent repeating errors.',
'pinterest-for-woocommerce'
),
esc_html( $cached_error )
),
425
);
}
// Add preferred_processing_schedule to the data after generating cache key because time() is used.
$data['preferred_processing_schedule'] = array(
'time' => gmdate( 'H:i', time() + 5 * MINUTE_IN_SECONDS ),
'timezone' => 'Etc/UTC',
);
try {
$feed = APIV5::create_feed( $data, $ad_account_id );
} catch ( PinterestApiException $e ) {
$delay = Pinterest_For_Woocommerce()::get_data( 'create_feed_delay' ) ?? MINUTE_IN_SECONDS;
$message = sprintf( '%1$s - %2$s', esc_html( $e->get_pinterest_code() ), esc_html( $e->getMessage() ) );
set_transient( $cache_key, $message, $delay );
// Double the delay.
Pinterest_For_Woocommerce()::save_data(
'create_feed_delay',
min( $delay * 2, 6 * HOUR_IN_SECONDS )
);
throw $e;
}
static::invalidate_feeds_cache();
$feed_id = static::match_local_feed_configuration_to_registered_feeds( array( $feed ) );
// Clean the cached delay.
Pinterest_For_Woocommerce()::save_data( 'create_feed_delay', false );
return $feed_id;
}
/**
* Update a feed for the given ad account.
*
* @since 1.4.0
*
* @param string $feed_id The ID of the feed.
* @param array $data The data to update the feed with.
*
* @return array
* @throws Throwable PHP Exception if there is an error updating the feed.
*/
public static function update_feed( string $feed_id, array $data ) {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
$data = array_merge(
$data,
array(
'status' => 'ACTIVE',
'preferred_processing_schedule' => array(
'time' => gmdate( 'H:i', time() + 5 * MINUTE_IN_SECONDS ),
'timezone' => 'Etc/UTC',
),
'catalog_type' => 'RETAIL',
)
);
try {
return APIV5::update_feed( $feed_id, $data, $ad_account_id );
} catch ( Throwable $th ) {
Logger::log( $th->getMessage(), 'error' );
throw $th;
}
}
/**
* Get a specific merchant feed using the given arguments.
*
* @param string $feed_id The ID of the feed.
*
* @return array The feed profile object.
*
* @throws PinterestApiException Pinterest API Exception.
*/
public static function get_feed( $feed_id ) {
try {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
$feeds = APIV5::get_feeds( $ad_account_id );
foreach ( $feeds['items'] as $feed ) {
// Get the feed with the requested id if exists.
if ( $feed_id === $feed['id'] ) {
return $feed;
}
}
// No feed found.
return array();
} catch ( PinterestApiException $e ) {
Logger::log( $e->getMessage(), 'error' );
throw $e;
}
}
/**
* Get merchant's feeds.
*
* @return array The feed profile objects.
*/
public static function get_feeds(): array {
try {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
$feeds = APIV5::get_feeds( $ad_account_id );
return $feeds['items'] ?? array();
} catch ( PinterestApiException $e ) {
Logger::log( $e->getMessage(), 'error' );
return array();
}
}
/**
* Invalidate the merchant feeds cache.
*
* @since 1.4.0
*
* @return bool True if the cache was invalidated, false otherwise.
*/
public static function invalidate_feeds_cache() {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
return APIV5::invalidate_feeds_cache( $ad_account_id );
}
/**
* Verify if the local feed is already registered to the merchant.
* Return its ID if it is.
*
* @param array $feeds The list of feeds to check against. If not set, the list will be fetched from the API.
* @return string Returns the ID of the feed if properly registered or an empty string otherwise.
*/
public static function match_local_feed_configuration_to_registered_feeds( array $feeds = array() ): string {
if ( empty( $feeds ) ) {
$feeds = static::get_feeds();
}
$configs = LocalFeedConfigs::get_instance()->get_configurations();
$config = reset( $configs );
foreach ( $feeds as $feed ) {
/**
* Match feeds created by Pinterest for WooCommerce extension in both API v3 and v5 versions.
*
* V3 API created feeds have no name, it is set to null instead.
* V5 API created feeds have a name that starts with 'Created by Pinterest for WooCommerce'.
*
* When trying to match remote feed to a local configuration, we need to check both cases
* not to create a new feed if the feed was created by the extension in the past.
*/
$does_match = self::does_feed_match( $feed ) && ( $feed['location'] ?? '' ) === $config['feed_url'];
if ( $does_match ) {
return $feed['id'];
}
}
return '';
}
/**
* Tests if the feed is a match.
*
* @param array $feed A feed information array from Pinterest API response.
*
* @since 1.4.10
* @return bool
*/
private static function does_feed_match( array $feed ): bool {
$local_country = Pinterest_For_Woocommerce::get_base_country();
try {
$local_locale = LocaleMapper::get_locale_for_api();
} catch ( PinterestApiLocaleException $e ) {
$local_locale = LocaleMapper::PINTEREST_DEFAULT_LOCALE;
}
$does_match = ( $feed['default_country'] ?? '' ) === $local_country;
if ( ! $does_match ) {
return false;
}
$does_match = ( $feed['default_locale'] ?? '' ) === $local_locale;
if ( ! $does_match ) {
return false;
}
// Some sites may be misconfigured and return an HTTP scheme for the feed location URL. We force it to become HTTPS.
$force_https = str_replace( 'http:', 'https:', wp_get_upload_dir()['baseurl'] );
$feed_location = trailingslashit( $force_https ) . PINTEREST_FOR_WOOCOMMERCE_LOG_PREFIX . '-';
return 0 === strpos( $feed['location'] ?? '', $feed_location );
}
/**
* Compare remote feeds to local configuration to find a matching feed.
*
* @since 1.4.10
* @return string - Remote feed ID that matches.
* @throws FeedNotFoundException When there is no matching feed at Pinterest.
*/
public static function maybe_remote_feed(): string {
$feeds = self::get_feeds();
foreach ( $feeds as $feed ) {
if ( self::does_feed_match( $feed ) ) {
$last_dash_position = strrpos( $feed['location'], '-' ) + 1;
$last_dot_position = strrpos( $feed['location'], '.' );
$length_of_the_id = $last_dot_position - $last_dash_position;
return substr( $feed['location'], $last_dash_position, $length_of_the_id );
}
}
throw new FeedNotFoundException();
}
/**
* Check if the registered feed is enabled.
*
* @param string $feed_id The ID of the feed.
* @return bool True if the feed is active, false otherwise.
*
* @throws PinterestApiException Pinterest API Exception.
* @since 1.2.13
* @deprecated
*/
public static function is_local_feed_enabled( string $feed_id ): bool {
if ( empty( $feed_id ) ) {
return false;
}
$feed = static::get_feed( $feed_id );
return 'ACTIVE' === ( $feed['status'] ?? '' );
}
/**
* Delete the feed.
*
* @since 1.4.0
*
* @param string $feed_id The ID of the feed.
*
* @return bool
*/
public static function delete_feed( string $feed_id ) {
try {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
APIV5::delete_feed( $feed_id, $ad_account_id );
return true;
} catch ( PinterestApiException $e ) {
Logger::log( $e->getMessage(), 'error' );
if ( in_array(
(int) $e->get_pinterest_code(),
array(
PinterestApiException::MERCHANT_DISAPPROVED,
PinterestApiException::MERCHANT_UNDER_REVIEW,
PinterestApiException::CATALOGS_FEED_HAS_ACTIVE_PROMOTIONS,
)
) ) {
// Show Admin Notice.
FeedDeletionFailure::possibly_add_note( $e->get_pinterest_code() );
}
return false;
}
}
/**
* Get the latest report of the active feed related to the last attempt to process and ingest our feed.
*
* @since 1.4.0
*
* @param string $feed_id Pinterest feed ID.
*
* @return array The feed ingestion and processing report or empty array.
*/
public static function get_feed_recent_processing_results( $feed_id ): array {
try {
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
$feed_report = APIV5::get_feed_processing_results( $feed_id, $ad_account_id );
} catch ( PinterestApiException $e ) {
return array();
}
return $feed_report['items'][0] ?? array();
}
/**
* Get the feed report items issues.
*
* @since 1.4.0
*
* @param string $feed_processing_result_id The feed processing result ID.
* @param int $per_page The number of items to return per page. Default 25.
*
* @return array
*/
public static function get_feed_processing_result_items_issues( $feed_processing_result_id, $per_page = 25 ): array {
try {
$feed_report = APIV5::get_feed_processing_result_items_issues( $feed_processing_result_id, $per_page );
} catch ( PinterestApiException $e ) {
return array();
}
return $feed_report['items'] ?? array();
}
}