PluginProbe
Datafeedr API / 1.1.2
Datafeedr API v1.1.2
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
← All changes | functions/api.php +439 -161 1.0.21.1.2 View file →
@@ -4,43 +4,66 @@
4 4 $api = dfrapi_api( dfrapi_get_transport_method() );
5 5 try {
6 6 $status = $api->getStatus();
7 7 dfrapi_api_update_status( $api );
8 +
8 9 return $status;
9 - } catch( Exception $err ) {
10 + } catch ( Exception $err ) {
10 11 return dfrapi_api_error( $err );
11 12 }
12 -
13 +
13 14 }
14 15
16 +/**
17 + * Removed configuration. Always returns 'wordpress'. 2017-02-21 10:23:10
18 + */
15 19 function dfrapi_get_transport_method() {
16 - $configuration = (array) get_option( 'dfrapi_configuration' );
17 - $transport = ( isset( $configuration['transport_method'] ) ) ? $configuration['transport_method'] : $transport;
18 - return $transport;
20 + return 'wordpress';
21 + // $configuration = (array) get_option( 'dfrapi_configuration' );
22 + // $transport = ( isset( $configuration['transport_method'] ) ) ? $configuration['transport_method'] : $transport;
23 + // return $transport;
19 24 }
20 25
21 26 /**
22 27 * This instantiates the Datafeedr API Library and returns the $api object.
23 28 */
24 -function dfrapi_api( $transport='curl', $timeout=0, $returnObjects=FALSE ) {
25 -
29 +function dfrapi_api( $transport = 'curl', $timeout = 0, $returnObjects = false ) {
30 +
26 31 $configuration = (array) get_option( 'dfrapi_configuration' );
27 32
28 - $access_id = false;
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;
29 39 $secret_key = false;
30 - $transport = dfrapi_get_transport_method();
31 -
40 + $transport = dfrapi_get_transport_method();
41 +
32 42 if ( isset( $configuration['access_id'] ) && ( $configuration['access_id'] != '' ) ) {
33 43 $access_id = $configuration['access_id'];
34 44 }
35 -
45 +
36 46 if ( isset( $configuration['secret_key'] ) && ( $configuration['secret_key'] != '' ) ) {
37 47 $secret_key = $configuration['secret_key'];
38 - }
39 -
48 + }
49 +
40 50 if ( $access_id && $secret_key ) {
41 - $api = new DatafeedrApi( $access_id, $secret_key, $transport, $timeout, $returnObjects );
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 +
42 64 return $api;
65 +
43 66 } else {
44 67 return false;
45 68 }
46 69 }
@@ -47,24 +70,24 @@
47 70
48 71 /**
49 72 * Creates an associate array with the API's error details.
50 73 */
51 -function dfrapi_api_error( $error, $params=FALSE ) {
52 -
74 +function dfrapi_api_error( $error, $params = false ) {
75 +
53 76 // Change "request_count" to "max_requests" because sometimes there's
54 77 // not even enough API requests left to update the Account info with
55 78 // the most update to date information.
56 79 if ( $error->getCode() == 301 ) {
57 - $account = get_option( 'dfrapi_account', array() );
80 + $account = get_option( 'dfrapi_account', array() );
58 81 $account['request_count'] = $account['max_requests'];
59 82 update_option( 'dfrapi_account', $account );
60 83 }
61 84
62 - return array(
85 + return array(
63 86 'dfrapi_api_error' => array(
64 - 'class' => get_class( $error ),
65 - 'code' => $error->getCode(),
66 - 'msg' => $error->getMessage(),
87 + 'class' => get_class( $error ),
88 + 'code' => $error->getCode(),
89 + 'msg' => $error->getMessage(),
67 90 'params' => $params,
68 91 )
69 92 );
70 93 }
@@ -71,11 +94,12 @@
71 94
72 95 /**
73 96 * Creates the proper API request from the $query.
74 97 */
75 -function dfrapi_api_query_to_filters( $query, $useSelected=TRUE ) {
76 - $sform = new Dfrapi_SearchForm();
77 - return $sform->makeFilters( $query, $useSelected );
98 +function dfrapi_api_query_to_filters( $query, $useSelected = true ) {
99 + $sform = new Dfrapi_SearchForm();
100 +
101 + return $sform->makeFilters( $query, $useSelected );
78 102 }
79 103
80 104 /**
81 105 * Returns a parameter value from the $query array.
@@ -80,19 +104,20 @@
80 104 /**
81 105 * Returns a parameter value from the $query array.
82 106 */
83 107 function dfrapi_api_get_query_param( $query, $param ) {
84 - if ( is_array( $query ) && !empty( $query ) ) {
85 - foreach( $query as $k => $v ) {
108 + if ( is_array( $query ) && ! empty( $query ) ) {
109 + foreach ( $query as $k => $v ) {
86 110 if ( $v['field'] == $param ) {
87 111 return array(
88 - 'field' => @$v['field'],
89 - 'operator' => @$v['operator'],
90 - 'value' => @$v['value'],
112 + 'field' => @$v['field'],
113 + 'operator' => @$v['operator'],
114 + 'value' => @$v['value'],
91 115 );
92 116 }
93 117 }
94 118 }
119 +
95 120 return false;
96 121 }
97 122
98 123 /**
@@ -100,43 +125,48 @@
100 125 * API status information for this user.
101 126 */
102 127 function dfrapi_api_update_status( &$api ) {
103 128 if ( $status = $api->lastStatus() ) {
104 - $account = get_option( 'dfrapi_account', array() );
105 - $account['user_id'] = $status['user_id'];
106 - $account['plan_id'] = $status['plan_id'];
107 - $account['bill_day'] = $status['bill_day'];
108 - $account['max_total'] = $status['max_total'];
109 - $account['max_length'] = $status['max_length'];
110 - $account['max_requests'] = $status['max_requests'];
111 - $account['request_count'] = $status['request_count'];
112 - $account['network_count'] = $status['network_count'];
113 - $account['product_count'] = $status['product_count'];
114 - $account['merchant_count'] = $status['merchant_count'];
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'];
115 140 update_option( 'dfrapi_account', $account );
116 141 }
117 142 }
118 143
119 144 /**
120 - * This returns all affiliate networks' information.
145 + * This returns all affiliate networks' information.
121 146 * This accepts an array of source_ids (network ids)
122 147 * to return a subset of networks.
123 148 */
124 -function dfrapi_api_get_all_networks( $nids=array() ) {
149 +function dfrapi_api_get_all_networks( $nids = array() ) {
125 150 $option_name = 'dfrapi_all_networks';
126 - $networks = get_transient( $option_name );
151 + $use_cache = wp_using_ext_object_cache( false );
152 + $networks = get_transient( $option_name );
153 + wp_using_ext_object_cache( $use_cache );
127 154 if ( false === $networks || empty ( $networks ) ) {
128 155 $api = dfrapi_api( dfrapi_get_transport_method() );
129 156 try {
130 - $networks = $api->getNetworks( $nids, TRUE );
157 + $networks = $api->getNetworks( $nids, true );
131 158 dfrapi_api_set_network_types( $networks );
132 159 dfrapi_api_update_status( $api );
133 - } catch( Exception $err ) {
160 + } catch ( Exception $err ) {
134 161 return dfrapi_api_error( $err );
135 162 }
163 + $use_cache = wp_using_ext_object_cache( false );
136 164 set_transient( $option_name, $networks, DAY_IN_SECONDS );
165 + wp_using_ext_object_cache( $use_cache );
137 166 }
138 167 dfrapi_update_transient_whitelist( $option_name );
168 +
139 169 return $networks;
140 170 }
141 171
142 172 /**
@@ -142,43 +172,126 @@
142 172 /**
143 173 * Returns a Zanox zmid value.
144 174 */
145 175 function dfrapi_api_get_zanox_zmid( $merchant_id, $adspace_id ) {
146 - $option_name = 'zmid_' . $merchant_id . $adspace_id;
147 - $zanox = get_transient( $option_name );
148 - if ( false === $zanox || empty ( $zanox ) ) {
149 - $api = dfrapi_api( dfrapi_get_transport_method() );
150 - try {
151 - $zanox_keys = dfrapi_get_zanox_keys();
152 - $zanox = $api->getZanoxMerchantIds( $merchant_id, $adspace_id, $zanox_keys['connection_key'] );
153 - } catch( Exception $err ) {
154 - return dfrapi_api_error( $err );
155 - }
156 - set_transient( $option_name, $zanox, WEEK_IN_SECONDS );
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;
157 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 +
158 203 dfrapi_update_transient_whitelist( $option_name );
159 - return $zanox;
204 +
205 + return $zmid;
160 206 }
161 207
208 +/**
209 + * Returns a Partnerize camref value.
210 + */
211 +function dfrapi_api_get_ph_camref( $merchant_id ) {
162 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 +
163 245 /**
164 - * This creates 2 options in the options table each time the option
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
165 278 * "dfrapi_all_networks" is updated with new network information from the API.
166 - *
279 + *
167 280 * - dfrapi_product_networks
168 281 * - dfrapi_coupon_networks
169 - *
282 + *
170 283 * These are just helper options to figure out if a network is a "product"
171 284 * network or a "coupon" network.
172 285 */
173 286 function dfrapi_api_set_network_types( $networks ) {
174 287 $product_networks = array();
175 - $coupon_networks = array();
176 - foreach( $networks as $network ) {
288 + $coupon_networks = array();
289 + foreach ( $networks as $network ) {
177 290 if ( $network['type'] == 'products' ) {
178 - $product_networks[$network['_id']] = $network;
291 + $product_networks[ $network['_id'] ] = $network;
179 292 } elseif ( $network['type'] == 'coupons' ) {
180 - $coupon_networks[$network['_id']] = $network;
293 + $coupon_networks[ $network['_id'] ] = $network;
181 294 }
182 295 }
183 296 update_option( 'dfrapi_product_networks', $product_networks );
184 297 update_option( 'dfrapi_coupon_networks', $coupon_networks );
@@ -185,57 +298,69 @@
185 298 }
186 299
187 300 /**
188 301 * This stores all merchants for a given source_id ($nid).
189 - *
302 + *
190 303 * It is possible to pass "all" to this function however this creates
191 304 * memory_limit errors when memory is set to less than 64MB.
192 305 */
193 306 function dfrapi_api_get_all_merchants( $nid ) {
194 307 $option_name = 'dfrapi_all_merchants_for_nid_' . $nid;
195 - $merchants = get_transient( $option_name );
196 - if ( false === $merchants || empty ( $merchants ) ) {
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 ) ) {
197 312 $api = dfrapi_api( dfrapi_get_transport_method() );
198 313 try {
199 - $merchants = $api->getMerchants( array( intval( $nid ) ), TRUE );
314 + $merchants = $api->getMerchants( array( intval( $nid ) ), true );
200 315 dfrapi_api_update_status( $api );
201 - } catch( Exception $err ) {
316 + } catch ( Exception $err ) {
202 317 return dfrapi_api_error( $err );
203 318 }
319 + $use_cache = wp_using_ext_object_cache( false );
204 320 set_transient( $option_name, $merchants, DAY_IN_SECONDS );
205 - }
321 + wp_using_ext_object_cache( $use_cache );
322 + }
206 323 dfrapi_update_transient_whitelist( $option_name );
324 +
207 325 return $merchants;
208 326 }
209 327
210 328 /**
211 - * This retuns merchant or merchants' information by merchant_id or
329 + * This returns merchant or merchants' information by merchant_id or
212 330 * an array of merchant IDs.
213 331 */
214 -function dfrapi_api_get_merchants_by_id( $ids, $includeEmpty=FALSE ) {
332 +function dfrapi_api_get_merchants_by_id( $ids, $includeEmpty = false ) {
215 333 $name = false;
216 334 if ( is_array( $ids ) ) {
217 335 sort( $ids, SORT_NUMERIC );
218 336 $id_string = implode( ",", $ids );
219 - $name = md5( $id_string );
337 + $name = md5( $id_string );
220 338 } elseif ( $ids != '' ) {
221 339 $name = trim( $ids );
222 340 }
223 - if ( !$name ) { return; }
224 - $name = substr( $name, 0, 20 );
341 + if ( ! $name ) {
342 + return;
343 + }
344 + $name = substr( $name, 0, 20 );
225 345 $option_name = 'dfrapi_merchants_byid_' . $name;
226 - $merchants = get_transient( $option_name );
346 + $use_cache = wp_using_ext_object_cache( false );
347 + $merchants = get_transient( $option_name );
348 + wp_using_ext_object_cache( $use_cache );
227 349 if ( false === $merchants || empty ( $merchants ) ) {
228 350 $api = dfrapi_api( dfrapi_get_transport_method() );
229 351 try {
230 352 $merchants = $api->getMerchantsById( $ids, $includeEmpty );
231 353 dfrapi_api_update_status( $api );
232 - } catch( Exception $err ) {
354 + } catch ( Exception $err ) {
233 355 return dfrapi_api_error( $err );
234 356 }
357 + $use_cache = wp_using_ext_object_cache( false );
235 358 set_transient( $option_name, $merchants, DAY_IN_SECONDS );
236 - }
359 + wp_using_ext_object_cache( $use_cache );
360 + }
237 361 dfrapi_update_transient_whitelist( $option_name );
362 +
238 363 return $merchants;
239 364 }
240 365
241 366 /**
@@ -243,28 +368,30 @@
243 368 * - ids: the query passed to the function.
244 369 * - products: array of products.
245 370 * - last_status: value of $api->lastStatus().
246 371 * - found_count: value of $search->getFoundCount().
247 - *
372 + *
248 373 * If the API throws an exception, that will return dfrapi_api_error( $err );
249 - *
374 + *
250 375 * @param array $ids An array of product IDs.
251 376 * @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin.
252 377 * @param int $page The page number for returning products. This is used to figure the offset.
253 378 */
254 -function dfrapi_api_get_products_by_id( $ids, $ppp=20, $page=1 ) {
379 +function dfrapi_api_get_products_by_id( $ids, $ppp = 20, $page = 1 ) {
255 380
256 381 $response = array();
257 382
258 383 // Return false if no $ids or no $postid
259 - if ( empty( $ids ) ) { return $response; }
260 -
384 + if ( empty( $ids ) ) {
385 + return $response;
386 + }
387 +
261 388 // Make sure $page is a positive integer.
262 389 $page = intval( abs( $page ) );
263 -
390 +
264 391 // Make sure $ppp is a positive integer.
265 392 $ppp = intval( abs( $ppp ) );
266 -
393 +
267 394 // Make sure $ppp is not greater than "max_length".
268 395 $account = (array) get_option( 'dfrapi_account' );
269 396 if ( $ppp > $account['max_length'] ) {
270 397 $ppp = $account['max_length'];
@@ -272,51 +399,121 @@
272 399
273 400 // The maximum number of results a request to the API can return.
274 401 // Changing this will only break your site. It's not overridable.
275 402 $max_total = $account['max_total'];
276 -
403 +
277 404 // Determine offset.
278 405 $offset = ( ( $page - 1 ) * $ppp );
279 -
406 +
280 407 // Make sure $limit doesn't go over 10,000.
281 - if ( ( $offset + $ppp ) > $max_total ) {
408 + if ( ( $offset + $ppp ) > $max_total ) {
282 409 $ppp = ( $max_total - $offset );
283 410 }
284 -
411 +
285 412 // If $ppp is negative, return empty array();
286 413 if ( $ppp < 1 ) {
287 414 return array();
288 415 }
289 -
416 +
290 417 // If offset is greater than 10,000 return empty array();
291 - if ( $offset >= ( $max_total - $ppp ) ) {
418 + if ( $offset >= ( $max_total - $ppp ) ) {
292 419 return array();
293 420 }
294 -
421 +
295 422 try {
423 +
296 424 // Initialize API.
297 425 $api = dfrapi_api( dfrapi_get_transport_method() );
298 - if ( !$api ) { return $response; }
299 -
426 + if ( ! $api ) {
427 + return $response;
428 + }
429 +
300 430 // Get a range of product IDs to query.
301 431 $id_range = array_slice( $ids, $offset, $ppp );
302 -
303 - // Query API
304 - $products = $api->getProducts( $id_range );
305 -
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 +
306 502 // Update API status
307 503 dfrapi_api_update_status( $api );
308 -
504 +
309 505 // Build $response array().
310 - $response['ids'] = $ids;
311 - $response['products'] = $products;
312 - $response['last_status'] = $api->lastStatus();
313 - $response['found_count'] = count( $ids );
314 -
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 +
315 512 // Return it!
316 513 return $response;
317 -
318 - } catch( Exception $err ) {
514 +
515 + } catch ( Exception $err ) {
319 516 return dfrapi_api_error( $err );
320 517 }
321 518 }
322 519
@@ -327,50 +524,52 @@
327 524 * - products: array of products.
328 525 * - last_status: value of $api->lastStatus().
329 526 * - found_count: value of $search->getFoundCount().
330 527 * - params: value of $search->getParams().
331 - *
528 + *
332 529 * Example of $query array():
333 - *
334 - *
530 + *
531 + *
335 532 * $query[] = array(
336 - * 'value' => 'shoes',
337 - * 'field' => 'any',
338 - * 'operator' => 'contain'
533 + * 'value' => 'shoes',
534 + * 'field' => 'any',
535 + * 'operator' => 'contain'
339 536 * );
340 537 *
341 538 * $query[] = array(
342 - * 'value' => 'image',
343 - * 'field' => 'duplicates',
344 - * 'operator' => 'is'
539 + * 'value' => 'image',
540 + * 'field' => 'duplicates',
541 + * 'operator' => 'is'
345 542 * );
346 543 *
347 544 * $query[] = array(
348 - * 'field' => 'sort',
349 - * 'operator' => '+saleprice'
545 + * 'field' => 'sort',
546 + * 'operator' => '+saleprice'
350 547 * );
351 - *
352 - *
548 + *
549 + *
353 550 * If the API throws an exception, that will return dfrapi_api_error( $err, $params );
354 - *
551 + *
355 552 * @param array $query The complete query to pass to the API.
356 553 * @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin.
357 554 * @param int $page The page number for returning products. This is used to figure the offset.
358 555 * @param array $excluded An array of product IDs to exclude from being returned.
359 556 */
360 -function dfrapi_api_get_products_by_query( $query, $ppp=20, $page=1, $excluded=array() ) {
557 +function dfrapi_api_get_products_by_query( $query, $ppp = 20, $page = 1, $excluded = array() ) {
361 558
362 559 $response = array();
363 560
364 561 // Return false if no $query.
365 - if ( empty( $query ) ) { return $response; }
366 -
562 + if ( empty( $query ) ) {
563 + return $response;
564 + }
565 +
367 566 // Make sure $page is a positive integer.
368 567 $page = intval( abs( $page ) );
369 -
568 +
370 569 // Make sure $ppp is a positive integer.
371 570 $ppp = intval( abs( $ppp ) );
372 -
571 +
373 572 // Make sure $ppp is not greater than "max_length".
374 573 $account = (array) get_option( 'dfrapi_account' );
375 574 if ( $ppp > $account['max_length'] ) {
376 575 $ppp = $account['max_length'];
@@ -381,25 +580,31 @@
381 580 $max_total = $account['max_total'];
382 581
383 582 // Detemine query limit (if exists).
384 583 $query_limit = dfrapi_api_get_query_param( $query, 'limit' );
385 - $query_limit = ( $query_limit )
584 + $query_limit = ( $query_limit )
386 585 ? $query_limit['value']
387 586 : false;
388 -
587 +
389 588 // No query shall try to return more than 10,000 products.
390 589 if ( $query_limit && ( $query_limit > $max_total ) ) {
391 590 $query_limit = $max_total;
392 591 }
393 -
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 +
394 599 // Determine offset.
395 600 $offset = ( ( $page - 1 ) * $ppp );
396 -
601 +
397 602 // If offset is greater than 10,000 return empty array();
398 - if ( $offset >= $max_total ) {
603 + if ( $offset >= $max_total ) {
399 604 return array();
400 605 }
401 -
606 +
402 607 // Factor in query limit
403 608 if ( $query_limit ) {
404 609 if ( ( $ppp + $offset ) > $query_limit ) {
405 610 $ppp = ( $query_limit - $offset );
@@ -404,35 +609,40 @@
404 609 if ( ( $ppp + $offset ) > $query_limit ) {
405 610 $ppp = ( $query_limit - $offset );
406 611 }
407 612 }
408 -
613 +
409 614 // Make sure $limit doesn't go over 10,000.
410 - if ( ( $offset + $ppp ) > $max_total ) {
615 + if ( ( $offset + $ppp ) > $max_total ) {
411 616 $ppp = ( $max_total - $offset );
412 617 }
413 -
618 +
414 619 // If $ppp is negative, return empty array();
415 620 if ( $ppp < 1 ) {
416 621 return $response;
417 622 }
418 -
623 +
419 624 try {
420 -
625 +
421 626 // Initialize API.
422 627 $api = dfrapi_api( dfrapi_get_transport_method() );
423 - if ( !$api ) { return $response; }
424 -
628 + if ( ! $api ) {
629 + return $response;
630 + }
631 +
425 632 $search = $api->searchRequest();
426 -
633 +
427 634 // Get filters
428 635 $filters = dfrapi_api_query_to_filters( $query );
429 -
636 + if ( isset( $filters['error'] ) ) {
637 + throw new DatafeedrError( $filters['error'], 0 );
638 + }
639 +
430 640 // Loop thru filters.
431 641 foreach ( $filters as $filter ) {
432 642 $search->addFilter( $filter );
433 643 }
434 -
644 +
435 645 // Exclude duplicates.
436 646 $duplicates = dfrapi_api_get_query_param( $query, 'duplicates' );
437 647 if ( $duplicates ) {
438 648 $excludes = $duplicates['value'];
@@ -437,44 +647,112 @@
437 647 if ( $duplicates ) {
438 648 $excludes = $duplicates['value'];
439 649 $search->excludeDuplicates( $excludes );
440 650 }
441 -
651 +
442 652 // Exclude blocked products.
443 653 $excluded = (array) $excluded;
444 - if ( !empty( $excluded ) ) {
445 - $search->addFilter('id !IN ' . implode( ",", $excluded ) );
654 + if ( ! empty( $excluded ) ) {
655 + $search->addFilter( 'id !IN ' . implode( ",", $excluded ) );
446 656 }
447 -
657 +
448 658 // Sort products.
449 659 $sort = dfrapi_api_get_query_param( $query, 'sort' );
450 - if( $sort && strlen( $sort['operator'] ) ) {
660 + if ( $sort && strlen( $sort['operator'] ) ) {
451 661 $search->addSort( $sort['operator'] );
452 662 }
453 -
663 +
664 + // Set Merchant Limit
665 + $search->setMerchantLimit( $merchant_limit );
666 +
454 667 // Set limits and offset.
455 - $search->setLimit( $ppp );
668 + $search->setLimit( $ppp );
456 669 $search->setOffset( $offset );
457 -
670 +
458 671 // Execute query.
459 672 $products = $search->execute();
460 -
673 +
461 674 // Update API status
462 675 dfrapi_api_update_status( $api );
463 -
676 +
464 677 // Build $response array().
465 - $response['query'] = $query;
466 - $response['excluded'] = $excluded;
467 - $response['products'] = $products;
468 - $response['last_status'] = $api->lastStatus();
469 - $response['found_count'] = $search->getFoundCount();
470 - $response['params'] = $search->getParams();
471 -
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 +
472 686 // Return it!
473 687 return $response;
474 -
475 - } catch( Exception $err ) {
688 +
689 + } catch ( Exception $err ) {
476 690 $params = $search->getParams();
691 +
477 692 return dfrapi_api_error( $err, $params );
478 -
693 +
479 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 ( ! isset( $affiliate_ids[ $merchant['suids'] ]['affiliate_id'] ) ) {
754 + throw new Exception( 'Suid does not exist for affiliate ID.' );
755 + }
756 +
757 + return $affiliate_ids[ $merchant['suids'] ]['affiliate_id'];
480 758 }