PluginProbe
Datafeedr API / 1.2.7
Datafeedr API v1.2.7
1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 All 181 releases
datafeedr-api / functions / api.php

api.php in Datafeedr API 1.2.7, at functions/api.php

763 lines 21.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function dfrapi_api_get_status() {
4 $api = dfrapi_api( dfrapi_get_transport_method() );
5 try {
6 $status = $api->getStatus();
7 dfrapi_api_update_status( $api );
8
9 return $status;
10 } catch ( Exception $err ) {
11 return dfrapi_api_error( $err );
12 }
13
14 }
15
16 /**
17 * Removed configuration. Always returns 'wordpress'. 2017-02-21 10:23:10
18 */
19 function dfrapi_get_transport_method() {
20 return 'wordpress';
21 // $configuration = (array) get_option( 'dfrapi_configuration' );
22 // $transport = ( isset( $configuration['transport_method'] ) ) ? $configuration['transport_method'] : $transport;
23 // return $transport;
24 }
25
26 /**
27 * This instantiates the Datafeedr API Library and returns the $api object.
28 */
29 function dfrapi_api( $transport = 'curl', $timeout = 0, $returnObjects = false ) {
30
31 $configuration = (array) get_option( 'dfrapi_configuration' );
32
33 if ( isset( $configuration['disable_api'] ) && ( $configuration['disable_api'] == 'yes' ) ) {
34 $configuration['disable_api'] = 'no';
35 update_option( 'dfrapi_configuration', $configuration );
36 }
37
38 $access_id = false;
39 $secret_key = false;
40 $transport = dfrapi_get_transport_method();
41
42 if ( isset( $configuration['access_id'] ) && ( $configuration['access_id'] != '' ) ) {
43 $access_id = $configuration['access_id'];
44 }
45
46 if ( isset( $configuration['secret_key'] ) && ( $configuration['secret_key'] != '' ) ) {
47 $secret_key = $configuration['secret_key'];
48 }
49
50 if ( $access_id && $secret_key ) {
51
52 $options = array(
53 'transport' => 'wordpress',
54 'timeout' => 60,
55 'returnObjects' => false,
56 'retry' => 3, // The number of retries if an API request times-out.
57 'retryTimeout' => 5, // The number of seconds to wait between retries.
58 );
59
60 $options = apply_filters( 'dfrapi_api_options', $options );
61
62 $api = new DatafeedrApi( $access_id, $secret_key, $options );
63
64 return $api;
65
66 } else {
67 return false;
68 }
69 }
70
71 /**
72 * Creates an associate array with the API's error details.
73 */
74 function dfrapi_api_error( $error, $params = false ) {
75
76 // Change "request_count" to "max_requests" because sometimes there's
77 // not even enough API requests left to update the Account info with
78 // the most update to date information.
79 if ( $error->getCode() == 301 ) {
80 $account = get_option( 'dfrapi_account', array() );
81 $account['request_count'] = $account['max_requests'];
82 update_option( 'dfrapi_account', $account );
83 }
84
85 return array(
86 'dfrapi_api_error' => array(
87 'class' => get_class( $error ),
88 'code' => $error->getCode(),
89 'msg' => $error->getMessage(),
90 'params' => $params,
91 )
92 );
93 }
94
95 /**
96 * Creates the proper API request from the $query.
97 */
98 function dfrapi_api_query_to_filters( $query, $useSelected = true ) {
99 $sform = new Dfrapi_SearchForm();
100
101 return $sform->makeFilters( $query, $useSelected );
102 }
103
104 /**
105 * Returns a parameter value from the $query array.
106 */
107 function dfrapi_api_get_query_param( $query, $param ) {
108 if ( is_array( $query ) && ! empty( $query ) ) {
109 foreach ( $query as $k => $v ) {
110 if ( $v['field'] == $param ) {
111 return array(
112 'field' => isset( $v['field'] ) ? $v['field'] : '',
113 'operator' => isset( $v['operator'] ) ? $v['operator'] : '',
114 'value' => isset( $v['value'] ) ? $v['value'] : '',
115 );
116 }
117 }
118 }
119
120 return false;
121 }
122
123 /**
124 * This updates the "dfrapi_account" option with the most recent
125 * API status information for this user.
126 */
127 function dfrapi_api_update_status( &$api ) {
128 if ( $status = $api->lastStatus() ) {
129 $account = get_option( 'dfrapi_account', array() );
130 $account['user_id'] = $status['user_id'];
131 $account['plan_id'] = $status['plan_id'];
132 $account['bill_day'] = $status['bill_day'];
133 $account['max_total'] = $status['max_total'];
134 $account['max_length'] = $status['max_length'];
135 $account['max_requests'] = $status['max_requests'];
136 $account['request_count'] = $status['request_count'];
137 $account['network_count'] = $status['network_count'];
138 $account['product_count'] = $status['product_count'];
139 $account['merchant_count'] = $status['merchant_count'];
140 update_option( 'dfrapi_account', $account );
141 }
142 }
143
144 /**
145 * This returns all affiliate networks' information.
146 * This accepts an array of source_ids (network ids)
147 * to return a subset of networks.
148 */
149 function dfrapi_api_get_all_networks( $nids = array() ) {
150 $option_name = 'dfrapi_all_networks';
151 $use_cache = wp_using_ext_object_cache( false );
152 $networks = get_transient( $option_name );
153 wp_using_ext_object_cache( $use_cache );
154 if ( false === $networks || empty ( $networks ) ) {
155 $api = dfrapi_api( dfrapi_get_transport_method() );
156 try {
157 $networks = $api->getNetworks( $nids, true );
158 dfrapi_api_set_network_types( $networks );
159 dfrapi_api_update_status( $api );
160 } catch ( Exception $err ) {
161 return dfrapi_api_error( $err );
162 }
163 $use_cache = wp_using_ext_object_cache( false );
164 set_transient( $option_name, $networks, DAY_IN_SECONDS );
165 wp_using_ext_object_cache( $use_cache );
166 }
167 dfrapi_update_transient_whitelist( $option_name );
168
169 return $networks;
170 }
171
172 /**
173 * Returns a Zanox zmid value.
174 */
175 function dfrapi_api_get_zanox_zmid( $merchant_id, $adspace_id ) {
176
177 $option_name = 'zmid_' . $merchant_id . '_' . $adspace_id;
178 $use_cache = wp_using_ext_object_cache( false );
179 $zmid = get_transient( $option_name );
180 wp_using_ext_object_cache( $use_cache );
181
182 if ( $zmid ) {
183 return $zmid;
184 }
185
186 $keys = dfrapi_get_zanox_keys();
187 $api = dfrapi_api();
188
189 try {
190 $zmid = $api->getZanoxMerchantIds(
191 $merchant_id,
192 $adspace_id,
193 $keys['connection_key']
194 );
195 } catch ( Exception $err ) {
196 $zmid = 'dfrapi_unapproved_zanox_merchant';
197 }
198
199 $use_cache = wp_using_ext_object_cache( false );
200 set_transient( $option_name, $zmid, WEEK_IN_SECONDS );
201 wp_using_ext_object_cache( $use_cache );
202
203 dfrapi_update_transient_whitelist( $option_name );
204
205 return $zmid;
206 }
207
208 /**
209 * Returns a Partnerize camref value.
210 */
211 function dfrapi_api_get_ph_camref( $merchant_id ) {
212
213 $option_name = 'camref_' . $merchant_id;
214 $use_cache = wp_using_ext_object_cache( false );
215 $camref = get_transient( $option_name );
216 wp_using_ext_object_cache( $use_cache );
217
218 if ( $camref ) {
219 return $camref;
220 }
221
222 $keys = dfrapi_get_ph_keys();
223 $api = dfrapi_api();
224
225 try {
226 $camref = $api->getPerformanceHorizonCamrefs(
227 $merchant_id,
228 $keys['application_key'],
229 $keys['user_api_key'],
230 $keys['publisher_id']
231 );
232 } catch ( Exception $err ) {
233 $camref = 'dfrapi_unapproved_ph_merchant';
234 }
235
236 $use_cache = wp_using_ext_object_cache( false );
237 set_transient( $option_name, $camref, WEEK_IN_SECONDS );
238 wp_using_ext_object_cache( $use_cache );
239
240 dfrapi_update_transient_whitelist( $option_name );
241
242 return $camref;
243 }
244
245 /**
246 * Returns a Effiliation affiliate ID.
247 *
248 * @since 1.0.81
249 */
250 function dfrapi_api_get_effiliation_affiliate_id( $merchant_id ) {
251
252 $option_name = 'effiliation_' . $merchant_id;
253 $use_cache = wp_using_ext_object_cache( false );
254 $affiliate_id = get_transient( $option_name );
255 wp_using_ext_object_cache( $use_cache );
256
257 if ( $affiliate_id ) {
258 return $affiliate_id;
259 }
260
261 try {
262 $affiliate_id = dfrapi_get_affiliate_id_for_effiliation_merchant( $merchant_id );
263 } catch ( Exception $err ) {
264 $affiliate_id = 'dfrapi_unapproved_effiliation_merchant';
265 }
266
267 $use_cache = wp_using_ext_object_cache( false );
268 set_transient( $option_name, $affiliate_id, WEEK_IN_SECONDS );
269 wp_using_ext_object_cache( $use_cache );
270
271 dfrapi_update_transient_whitelist( $option_name );
272
273 return $affiliate_id;
274 }
275
276 /**
277 * This creates 2 options in the options table each time the option
278 * "dfrapi_all_networks" is updated with new network information from the API.
279 *
280 * - dfrapi_product_networks
281 * - dfrapi_coupon_networks
282 *
283 * These are just helper options to figure out if a network is a "product"
284 * network or a "coupon" network.
285 */
286 function dfrapi_api_set_network_types( $networks ) {
287 $product_networks = array();
288 $coupon_networks = array();
289 foreach ( $networks as $network ) {
290 if ( $network['type'] == 'products' ) {
291 $product_networks[ $network['_id'] ] = $network;
292 } elseif ( $network['type'] == 'coupons' ) {
293 $coupon_networks[ $network['_id'] ] = $network;
294 }
295 }
296 update_option( 'dfrapi_product_networks', $product_networks );
297 update_option( 'dfrapi_coupon_networks', $coupon_networks );
298 }
299
300 /**
301 * This stores all merchants for a given source_id ($nid).
302 *
303 * It is possible to pass "all" to this function however this creates
304 * memory_limit errors when memory is set to less than 64MB.
305 */
306 function dfrapi_api_get_all_merchants( $nid ) {
307 $option_name = 'dfrapi_all_merchants_for_nid_' . $nid;
308 $use_cache = wp_using_ext_object_cache( false );
309 $merchants = get_transient( $option_name );
310 wp_using_ext_object_cache( $use_cache );
311 if ( false === $merchants || empty ( $merchants ) ) {
312 $api = dfrapi_api( dfrapi_get_transport_method() );
313 try {
314 $merchants = $api->getMerchants( array( intval( $nid ) ), true );
315 dfrapi_api_update_status( $api );
316 } catch ( Exception $err ) {
317 return dfrapi_api_error( $err );
318 }
319 $use_cache = wp_using_ext_object_cache( false );
320 set_transient( $option_name, $merchants, DAY_IN_SECONDS );
321 wp_using_ext_object_cache( $use_cache );
322 }
323 dfrapi_update_transient_whitelist( $option_name );
324
325 return $merchants;
326 }
327
328 /**
329 * This returns merchant or merchants' information by merchant_id or
330 * an array of merchant IDs.
331 */
332 function dfrapi_api_get_merchants_by_id( $ids, $includeEmpty = false ) {
333 $name = false;
334 if ( is_array( $ids ) ) {
335 sort( $ids, SORT_NUMERIC );
336 $id_string = implode( ",", $ids );
337 $name = md5( $id_string );
338 } elseif ( $ids != '' ) {
339 $name = trim( $ids );
340 }
341 if ( ! $name ) {
342 return;
343 }
344 $name = substr( $name, 0, 20 );
345 $option_name = 'dfrapi_merchants_byid_' . $name;
346 $use_cache = wp_using_ext_object_cache( false );
347 $merchants = get_transient( $option_name );
348 wp_using_ext_object_cache( $use_cache );
349 if ( false === $merchants || empty ( $merchants ) ) {
350 $api = dfrapi_api( dfrapi_get_transport_method() );
351 try {
352 $merchants = $api->getMerchantsById( $ids, $includeEmpty );
353 dfrapi_api_update_status( $api );
354 } catch ( Exception $err ) {
355 return dfrapi_api_error( $err );
356 }
357 $use_cache = wp_using_ext_object_cache( false );
358 set_transient( $option_name, $merchants, DAY_IN_SECONDS );
359 wp_using_ext_object_cache( $use_cache );
360 }
361 dfrapi_update_transient_whitelist( $option_name );
362
363 return $merchants;
364 }
365
366 /**
367 * Returns a $response array containing:
368 * - ids: the query passed to the function.
369 * - products: array of products.
370 * - last_status: value of $api->lastStatus().
371 * - found_count: value of $search->getFoundCount().
372 *
373 * If the API throws an exception, that will return dfrapi_api_error( $err );
374 *
375 * @param array $ids An array of product IDs.
376 * @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin.
377 * @param int $page The page number for returning products. This is used to figure the offset.
378 */
379 function dfrapi_api_get_products_by_id( $ids, $ppp = 20, $page = 1 ) {
380
381 $response = array();
382
383 // Return false if no $ids or no $postid
384 if ( empty( $ids ) ) {
385 return $response;
386 }
387
388 // Make sure $page is a positive integer.
389 $page = intval( abs( $page ) );
390
391 // Make sure $ppp is a positive integer.
392 $ppp = intval( abs( $ppp ) );
393
394 // Make sure $ppp is not greater than "max_length".
395 $account = (array) get_option( 'dfrapi_account' );
396 if ( $ppp > $account['max_length'] ) {
397 $ppp = $account['max_length'];
398 }
399
400 // The maximum number of results a request to the API can return.
401 // Changing this will only break your site. It's not overridable.
402 $max_total = $account['max_total'];
403
404 // Determine offset.
405 $offset = ( ( $page - 1 ) * $ppp );
406
407 // Make sure $limit doesn't go over 10,000.
408 if ( ( $offset + $ppp ) > $max_total ) {
409 $ppp = ( $max_total - $offset );
410 }
411
412 // If $ppp is negative, return empty array();
413 if ( $ppp < 1 ) {
414 return array();
415 }
416
417 // If offset is greater than 10,000 return empty array();
418 if ( $offset >= ( $max_total - $ppp ) ) {
419 return array();
420 }
421
422 try {
423
424 // Initialize API.
425 $api = dfrapi_api( dfrapi_get_transport_method() );
426 if ( ! $api ) {
427 return $response;
428 }
429
430 // Get a range of product IDs to query.
431 $id_range = array_slice( $ids, $offset, $ppp );
432
433 // Return immediately if $id_range is empty.
434 if ( empty( $id_range ) ) {
435 $response['ids'] = array();
436 $response['products'] = array();
437 $response['last_status'] = $api->lastStatus();
438 $response['found_count'] = 0;
439
440 return $response;
441 }
442
443 // Begin query
444 $search = $api->searchRequest();
445
446 // Get filters
447 $filters = dfrapi_api_query_to_filters( array() );
448 if ( isset( $filters['error'] ) ) {
449 throw new DatafeedrError( $filters['error'], 0 );
450 }
451
452 // Loop thru filters.
453 foreach ( $filters as $filter ) {
454 $search->addFilter( $filter );
455 }
456
457 $search->addFilter( 'id IN ' . implode( ",", $id_range ) );
458 $search->setLimit( $ppp );
459 $products = $search->execute();
460
461 // Keep track of IDs which were returned via the API to compare with $id_range (unreturned)
462 $included_ids = array();
463 if ( ! empty( $products ) ) {
464 foreach ( $products as $product ) {
465 $included_ids[] = $product['_id'];
466 }
467 }
468
469 // Excluded product IDs.
470 $excluded_ids = array_diff( $id_range, $included_ids );
471
472 // Add "message" values to excluded IDs if there are some.
473 $excluded_products = array();
474 if ( ! empty( $included_ids ) && ! empty( $excluded_ids ) ) {
475 foreach ( $excluded_ids as $excluded_id ) {
476
477 $wc_url = add_query_arg(
478 array(
479 's' => $excluded_id,
480 'post_status' => 'trash',
481 'post_type' => 'product',
482 ),
483 admin_url( 'edit.php' )
484 );
485
486 // Do not add a 'url' field to this array or the unavailable product WILL be imported.
487 // See /datafeedr-product-sets/classes/class-dfrps-update.php:73
488 $excluded_products[] = array(
489 '_id' => $excluded_id,
490 '_wc_url' => $wc_url,
491 'name' => $excluded_id . ' - ' . __( 'Unavailable', DFRAPI_DOMAIN ),
492 'price' => 0,
493 'finalprice' => 0,
494 'description' => __( 'This product is either temporarily or permanently unavailable.', DFRAPI_DOMAIN ),
495 'image' => DFRAPI_URL . 'images/icons/noimage.png',
496 'merchant' => 'n/a',
497 'source' => 'n/a',
498 );
499 }
500 }
501
502 // Update API status
503 dfrapi_api_update_status( $api );
504
505 // Build $response array().
506 $response['ids'] = $ids;
507 $response['products'] = array_merge( $products, $excluded_products );
508 $response['last_status'] = $api->lastStatus();
509 $response['found_count'] = count( $ids );
510 $response['params'] = $search->getParams();
511
512 // Return it!
513 return $response;
514
515 } catch ( Exception $err ) {
516 return dfrapi_api_error( $err );
517 }
518 }
519
520 /**
521 * Returns a $response array containing:
522 * - query: the query passed to the function.
523 * - excluded: ids of excluded products.
524 * - products: array of products.
525 * - last_status: value of $api->lastStatus().
526 * - found_count: value of $search->getFoundCount().
527 * - params: value of $search->getParams().
528 *
529 * Example of $query array():
530 *
531 *
532 * $query[] = array(
533 * 'value' => 'shoes',
534 * 'field' => 'any',
535 * 'operator' => 'contain'
536 * );
537 *
538 * $query[] = array(
539 * 'value' => 'image',
540 * 'field' => 'duplicates',
541 * 'operator' => 'is'
542 * );
543 *
544 * $query[] = array(
545 * 'field' => 'sort',
546 * 'operator' => '+saleprice'
547 * );
548 *
549 *
550 * If the API throws an exception, that will return dfrapi_api_error( $err, $params );
551 *
552 * @param array $query The complete query to pass to the API.
553 * @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin.
554 * @param int $page The page number for returning products. This is used to figure the offset.
555 * @param array $excluded An array of product IDs to exclude from being returned.
556 */
557 function dfrapi_api_get_products_by_query( $query, $ppp = 20, $page = 1, $excluded = array() ) {
558
559 $response = array();
560
561 // Return false if no $query.
562 if ( empty( $query ) ) {
563 return $response;
564 }
565
566 // Make sure $page is a positive integer.
567 $page = intval( abs( $page ) );
568
569 // Make sure $ppp is a positive integer.
570 $ppp = intval( abs( $ppp ) );
571
572 // Make sure $ppp is not greater than "max_length".
573 $account = (array) get_option( 'dfrapi_account' );
574 if ( $ppp > $account['max_length'] ) {
575 $ppp = $account['max_length'];
576 }
577
578 // The maximum number of results a request to the API can return.
579 // Changing this will only break your site. It's not overridable.
580 $max_total = $account['max_total'];
581
582 // Detemine query limit (if exists).
583 $query_limit = dfrapi_api_get_query_param( $query, 'limit' );
584 $query_limit = ( $query_limit )
585 ? $query_limit['value']
586 : false;
587
588 // No query shall try to return more than 10,000 products.
589 if ( $query_limit && ( $query_limit > $max_total ) ) {
590 $query_limit = $max_total;
591 }
592
593 // Detemine merchant limit (if exists).
594 $merchant_limit = dfrapi_api_get_query_param( $query, 'merchant_limit' );
595 $merchant_limit = ( $merchant_limit )
596 ? absint( $merchant_limit['value'] )
597 : 0;
598
599 // Determine offset.
600 $offset = ( ( $page - 1 ) * $ppp );
601
602 // If offset is greater than 10,000 return empty array();
603 if ( $offset >= $max_total ) {
604 return array();
605 }
606
607 // Factor in query limit
608 if ( $query_limit ) {
609 if ( ( $ppp + $offset ) > $query_limit ) {
610 $ppp = ( $query_limit - $offset );
611 }
612 }
613
614 // Make sure $limit doesn't go over 10,000.
615 if ( ( $offset + $ppp ) > $max_total ) {
616 $ppp = ( $max_total - $offset );
617 }
618
619 // If $ppp is negative, return empty array();
620 if ( $ppp < 1 ) {
621 return $response;
622 }
623
624 try {
625
626 // Initialize API.
627 $api = dfrapi_api( dfrapi_get_transport_method() );
628 if ( ! $api ) {
629 return $response;
630 }
631
632 $search = $api->searchRequest();
633
634 // Get filters
635 $filters = dfrapi_api_query_to_filters( $query );
636 if ( isset( $filters['error'] ) ) {
637 throw new DatafeedrError( $filters['error'], 0 );
638 }
639
640 // Loop thru filters.
641 foreach ( $filters as $filter ) {
642 $search->addFilter( $filter );
643 }
644
645 // Exclude duplicates.
646 $duplicates = dfrapi_api_get_query_param( $query, 'duplicates' );
647 if ( $duplicates ) {
648 $excludes = $duplicates['value'];
649 $search->excludeDuplicates( $excludes );
650 }
651
652 // Exclude blocked products.
653 $excluded = (array) $excluded;
654 if ( ! empty( $excluded ) ) {
655 $search->addFilter( 'id !IN ' . implode( ",", $excluded ) );
656 }
657
658 // Sort products.
659 $sort = dfrapi_api_get_query_param( $query, 'sort' );
660 if ( $sort && strlen( $sort['operator'] ) ) {
661 $search->addSort( $sort['operator'] );
662 }
663
664 // Set Merchant Limit
665 $search->setMerchantLimit( $merchant_limit );
666
667 // Set limits and offset.
668 $search->setLimit( $ppp );
669 $search->setOffset( $offset );
670
671 // Execute query.
672 $products = $search->execute();
673
674 // Update API status
675 dfrapi_api_update_status( $api );
676
677 // Build $response array().
678 $response['query'] = $query;
679 $response['excluded'] = $excluded;
680 $response['products'] = $products;
681 $response['last_status'] = $api->lastStatus();
682 //$response['found_count'] = $search->getFoundCount(); Old, returned wrong value (#8672)
683 $response['found_count'] = $search->getResultCount();
684 $response['params'] = $search->getParams();
685
686 // Return it!
687 return $response;
688
689 } catch ( Exception $err ) {
690 $params = $search->getParams();
691
692 return dfrapi_api_error( $err, $params );
693
694 }
695 }
696
697
698 function dfrapi_get_effiliation_product_feeds_url( $api_key ) {
699 return sprintf( 'http://apiv2.effiliation.com/apiv2/productfeeds.xml?key=%s&filter=mines&type=33&fields=0001010000110001', $api_key );
700 }
701
702 function dfrapi_request_effiliation_affiliate_ids( $api_key = null ) {
703
704 $option_name = 'effiliation_affiliate_ids';
705 $use_cache = wp_using_ext_object_cache( false );
706 $affiliate_ids = get_transient( $option_name );
707 wp_using_ext_object_cache( $use_cache );
708
709 if ( $affiliate_ids ) {
710 return $affiliate_ids;
711 }
712
713 $keys = dfrapi_get_effiliation_keys();
714 $api_key = $api_key ?: $keys['effiliation_key'];
715 $method = 'GET';
716 $url = dfrapi_get_effiliation_product_feeds_url( $api_key );
717
718 $xml = dfrapi_get_xml_response( $url, $method, [ 'timeout' => 30 ] );
719
720 if ( is_wp_error( $xml ) ) {
721 return $xml;
722 }
723
724 $affiliate_ids = [];
725
726 foreach ( $xml->feed as $e ) {
727 $item = json_decode( json_encode( $e ), true );
728 $suid = sanitize_text_field( $item['id_affilieur'] );
729
730 $affiliate_ids[ $suid ]['suid'] = ( $suid );
731 $affiliate_ids[ $suid ]['affiliate_id'] = sanitize_text_field( $item['id_compteur'] );
732 }
733
734 $use_cache = wp_using_ext_object_cache( false );
735 set_transient( $option_name, $affiliate_ids, ( MINUTE_IN_SECONDS * 20 ) );
736 wp_using_ext_object_cache( $use_cache );
737 dfrapi_update_transient_whitelist( $option_name );
738
739 return $affiliate_ids;
740 }
741
742 /**
743 * @param $merchant_id
744 *
745 * @return mixed|string
746 * @throws Exception
747 */
748 function dfrapi_get_affiliate_id_for_effiliation_merchant( $merchant_id ) {
749 $merchants = dfrapi_api_get_merchants_by_id( $merchant_id );
750 $merchant = isset( $merchants[0] ) ? $merchants[0] : [ 'suids' => '' ];
751 $affiliate_ids = dfrapi_request_effiliation_affiliate_ids();
752
753 if ( is_wp_error( $affiliate_ids ) ) {
754 throw new Exception( 'Unable to query Effiliation at this time. Please try again in 15 minutes.' );
755 }
756
757 if ( ! isset( $affiliate_ids[ $merchant['suids'] ]['affiliate_id'] ) ) {
758 throw new Exception( 'Suid does not exist for affiliate ID.' );
759 }
760
761 return $affiliate_ids[ $merchant['suids'] ]['affiliate_id'];
762 }
763