PluginProbe
Datafeedr API / 1.0.21
Datafeedr API v1.0.21
1.4.3 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 All 182 releases
datafeedr-api / functions / api.php

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

485 lines 13.8 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 return $status;
9 } catch( Exception $err ) {
10 return dfrapi_api_error( $err );
11 }
12
13 }
14
15 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;
19 }
20
21 /**
22 * This instantiates the Datafeedr API Library and returns the $api object.
23 */
24 function dfrapi_api( $transport='curl', $timeout=0, $returnObjects=FALSE ) {
25
26 $configuration = (array) get_option( 'dfrapi_configuration' );
27
28 $access_id = false;
29 $secret_key = false;
30 $transport = dfrapi_get_transport_method();
31
32 if ( isset( $configuration['access_id'] ) && ( $configuration['access_id'] != '' ) ) {
33 $access_id = $configuration['access_id'];
34 }
35
36 if ( isset( $configuration['secret_key'] ) && ( $configuration['secret_key'] != '' ) ) {
37 $secret_key = $configuration['secret_key'];
38 }
39
40 if ( $access_id && $secret_key ) {
41 $api = new DatafeedrApi( $access_id, $secret_key, $transport, $timeout, $returnObjects );
42 return $api;
43 } else {
44 return false;
45 }
46 }
47
48 /**
49 * Creates an associate array with the API's error details.
50 */
51 function dfrapi_api_error( $error, $params=FALSE ) {
52
53 // Change "request_count" to "max_requests" because sometimes there's
54 // not even enough API requests left to update the Account info with
55 // the most update to date information.
56 if ( $error->getCode() == 301 ) {
57 $account = get_option( 'dfrapi_account', array() );
58 $account['request_count'] = $account['max_requests'];
59 update_option( 'dfrapi_account', $account );
60 }
61
62 return array(
63 'dfrapi_api_error' => array(
64 'class' => get_class( $error ),
65 'code' => $error->getCode(),
66 'msg' => $error->getMessage(),
67 'params' => $params,
68 )
69 );
70 }
71
72 /**
73 * Creates the proper API request from the $query.
74 */
75 function dfrapi_api_query_to_filters( $query, $useSelected=TRUE ) {
76 $sform = new Dfrapi_SearchForm();
77 return $sform->makeFilters( $query, $useSelected );
78 }
79
80 /**
81 * Returns a parameter value from the $query array.
82 */
83 function dfrapi_api_get_query_param( $query, $param ) {
84 if ( is_array( $query ) && !empty( $query ) ) {
85 foreach( $query as $k => $v ) {
86 if ( $v['field'] == $param ) {
87 return array(
88 'field' => @$v['field'],
89 'operator' => @$v['operator'],
90 'value' => @$v['value'],
91 );
92 }
93 }
94 }
95 return false;
96 }
97
98 /**
99 * This updates the "dfrapi_account" option with the most recent
100 * API status information for this user.
101 */
102 function dfrapi_api_update_status( &$api ) {
103 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'];
115 update_option( 'dfrapi_account', $account );
116 }
117 }
118
119 /**
120 * This returns all affiliate networks' information.
121 * This accepts an array of source_ids (network ids)
122 * to return a subset of networks.
123 */
124 function dfrapi_api_get_all_networks( $nids=array() ) {
125 $option_name = 'dfrapi_all_networks';
126 $networks = get_transient( $option_name );
127 if ( false === $networks || empty ( $networks ) ) {
128 $api = dfrapi_api( dfrapi_get_transport_method() );
129 try {
130 $networks = $api->getNetworks( $nids, TRUE );
131 dfrapi_api_set_network_types( $networks );
132 dfrapi_api_update_status( $api );
133 } catch( Exception $err ) {
134 return dfrapi_api_error( $err );
135 }
136 set_transient( $option_name, $networks, DAY_IN_SECONDS );
137 }
138 dfrapi_update_transient_whitelist( $option_name );
139 return $networks;
140 }
141
142 /**
143 * Returns a Zanox zmid value.
144 */
145 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 ( $zanox === FALSE || 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 // If $zanox is empty, that means this user is not approved by this merchant.
154 if ( empty( $zanox ) ) {
155 $zanox = 'dfrapi_unapproved_zanox_merchant';
156 }
157 } catch( Exception $err ) {
158 return dfrapi_api_error( $err );
159 }
160 set_transient( $option_name, $zanox, WEEK_IN_SECONDS );
161 }
162 dfrapi_update_transient_whitelist( $option_name );
163 return $zanox;
164 }
165
166 /**
167 * This creates 2 options in the options table each time the option
168 * "dfrapi_all_networks" is updated with new network information from the API.
169 *
170 * - dfrapi_product_networks
171 * - dfrapi_coupon_networks
172 *
173 * These are just helper options to figure out if a network is a "product"
174 * network or a "coupon" network.
175 */
176 function dfrapi_api_set_network_types( $networks ) {
177 $product_networks = array();
178 $coupon_networks = array();
179 foreach( $networks as $network ) {
180 if ( $network['type'] == 'products' ) {
181 $product_networks[$network['_id']] = $network;
182 } elseif ( $network['type'] == 'coupons' ) {
183 $coupon_networks[$network['_id']] = $network;
184 }
185 }
186 update_option( 'dfrapi_product_networks', $product_networks );
187 update_option( 'dfrapi_coupon_networks', $coupon_networks );
188 }
189
190 /**
191 * This stores all merchants for a given source_id ($nid).
192 *
193 * It is possible to pass "all" to this function however this creates
194 * memory_limit errors when memory is set to less than 64MB.
195 */
196 function dfrapi_api_get_all_merchants( $nid ) {
197 $option_name = 'dfrapi_all_merchants_for_nid_' . $nid;
198 $merchants = get_transient( $option_name );
199 if ( false === $merchants || empty ( $merchants ) ) {
200 $api = dfrapi_api( dfrapi_get_transport_method() );
201 try {
202 $merchants = $api->getMerchants( array( intval( $nid ) ), TRUE );
203 dfrapi_api_update_status( $api );
204 } catch( Exception $err ) {
205 return dfrapi_api_error( $err );
206 }
207 set_transient( $option_name, $merchants, DAY_IN_SECONDS );
208 }
209 dfrapi_update_transient_whitelist( $option_name );
210 return $merchants;
211 }
212
213 /**
214 * This retuns merchant or merchants' information by merchant_id or
215 * an array of merchant IDs.
216 */
217 function dfrapi_api_get_merchants_by_id( $ids, $includeEmpty=FALSE ) {
218 $name = false;
219 if ( is_array( $ids ) ) {
220 sort( $ids, SORT_NUMERIC );
221 $id_string = implode( ",", $ids );
222 $name = md5( $id_string );
223 } elseif ( $ids != '' ) {
224 $name = trim( $ids );
225 }
226 if ( !$name ) { return; }
227 $name = substr( $name, 0, 20 );
228 $option_name = 'dfrapi_merchants_byid_' . $name;
229 $merchants = get_transient( $option_name );
230 if ( false === $merchants || empty ( $merchants ) ) {
231 $api = dfrapi_api( dfrapi_get_transport_method() );
232 try {
233 $merchants = $api->getMerchantsById( $ids, $includeEmpty );
234 dfrapi_api_update_status( $api );
235 } catch( Exception $err ) {
236 return dfrapi_api_error( $err );
237 }
238 set_transient( $option_name, $merchants, DAY_IN_SECONDS );
239 }
240 dfrapi_update_transient_whitelist( $option_name );
241 return $merchants;
242 }
243
244 /**
245 * Returns a $response array containing:
246 * - ids: the query passed to the function.
247 * - products: array of products.
248 * - last_status: value of $api->lastStatus().
249 * - found_count: value of $search->getFoundCount().
250 *
251 * If the API throws an exception, that will return dfrapi_api_error( $err );
252 *
253 * @param array $ids An array of product IDs.
254 * @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin.
255 * @param int $page The page number for returning products. This is used to figure the offset.
256 */
257 function dfrapi_api_get_products_by_id( $ids, $ppp=20, $page=1 ) {
258
259 $response = array();
260
261 // Return false if no $ids or no $postid
262 if ( empty( $ids ) ) { return $response; }
263
264 // Make sure $page is a positive integer.
265 $page = intval( abs( $page ) );
266
267 // Make sure $ppp is a positive integer.
268 $ppp = intval( abs( $ppp ) );
269
270 // Make sure $ppp is not greater than "max_length".
271 $account = (array) get_option( 'dfrapi_account' );
272 if ( $ppp > $account['max_length'] ) {
273 $ppp = $account['max_length'];
274 }
275
276 // The maximum number of results a request to the API can return.
277 // Changing this will only break your site. It's not overridable.
278 $max_total = $account['max_total'];
279
280 // Determine offset.
281 $offset = ( ( $page - 1 ) * $ppp );
282
283 // Make sure $limit doesn't go over 10,000.
284 if ( ( $offset + $ppp ) > $max_total ) {
285 $ppp = ( $max_total - $offset );
286 }
287
288 // If $ppp is negative, return empty array();
289 if ( $ppp < 1 ) {
290 return array();
291 }
292
293 // If offset is greater than 10,000 return empty array();
294 if ( $offset >= ( $max_total - $ppp ) ) {
295 return array();
296 }
297
298 try {
299 // Initialize API.
300 $api = dfrapi_api( dfrapi_get_transport_method() );
301 if ( !$api ) { return $response; }
302
303 // Get a range of product IDs to query.
304 $id_range = array_slice( $ids, $offset, $ppp );
305
306 // Query API
307 $products = $api->getProducts( $id_range );
308
309 // Update API status
310 dfrapi_api_update_status( $api );
311
312 // Build $response array().
313 $response['ids'] = $ids;
314 $response['products'] = $products;
315 $response['last_status'] = $api->lastStatus();
316 $response['found_count'] = count( $ids );
317
318 // Return it!
319 return $response;
320
321 } catch( Exception $err ) {
322 return dfrapi_api_error( $err );
323 }
324 }
325
326 /**
327 * Returns a $response array containing:
328 * - query: the query passed to the function.
329 * - excluded: ids of excluded products.
330 * - products: array of products.
331 * - last_status: value of $api->lastStatus().
332 * - found_count: value of $search->getFoundCount().
333 * - params: value of $search->getParams().
334 *
335 * Example of $query array():
336 *
337 *
338 * $query[] = array(
339 * 'value' => 'shoes',
340 * 'field' => 'any',
341 * 'operator' => 'contain'
342 * );
343 *
344 * $query[] = array(
345 * 'value' => 'image',
346 * 'field' => 'duplicates',
347 * 'operator' => 'is'
348 * );
349 *
350 * $query[] = array(
351 * 'field' => 'sort',
352 * 'operator' => '+saleprice'
353 * );
354 *
355 *
356 * If the API throws an exception, that will return dfrapi_api_error( $err, $params );
357 *
358 * @param array $query The complete query to pass to the API.
359 * @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin.
360 * @param int $page The page number for returning products. This is used to figure the offset.
361 * @param array $excluded An array of product IDs to exclude from being returned.
362 */
363 function dfrapi_api_get_products_by_query( $query, $ppp=20, $page=1, $excluded=array() ) {
364
365 $response = array();
366
367 // Return false if no $query.
368 if ( empty( $query ) ) { return $response; }
369
370 // Make sure $page is a positive integer.
371 $page = intval( abs( $page ) );
372
373 // Make sure $ppp is a positive integer.
374 $ppp = intval( abs( $ppp ) );
375
376 // Make sure $ppp is not greater than "max_length".
377 $account = (array) get_option( 'dfrapi_account' );
378 if ( $ppp > $account['max_length'] ) {
379 $ppp = $account['max_length'];
380 }
381
382 // The maximum number of results a request to the API can return.
383 // Changing this will only break your site. It's not overridable.
384 $max_total = $account['max_total'];
385
386 // Detemine query limit (if exists).
387 $query_limit = dfrapi_api_get_query_param( $query, 'limit' );
388 $query_limit = ( $query_limit )
389 ? $query_limit['value']
390 : false;
391
392 // No query shall try to return more than 10,000 products.
393 if ( $query_limit && ( $query_limit > $max_total ) ) {
394 $query_limit = $max_total;
395 }
396
397 // Determine offset.
398 $offset = ( ( $page - 1 ) * $ppp );
399
400 // If offset is greater than 10,000 return empty array();
401 if ( $offset >= $max_total ) {
402 return array();
403 }
404
405 // Factor in query limit
406 if ( $query_limit ) {
407 if ( ( $ppp + $offset ) > $query_limit ) {
408 $ppp = ( $query_limit - $offset );
409 }
410 }
411
412 // Make sure $limit doesn't go over 10,000.
413 if ( ( $offset + $ppp ) > $max_total ) {
414 $ppp = ( $max_total - $offset );
415 }
416
417 // If $ppp is negative, return empty array();
418 if ( $ppp < 1 ) {
419 return $response;
420 }
421
422 try {
423
424 // Initialize API.
425 $api = dfrapi_api( dfrapi_get_transport_method() );
426 if ( !$api ) { return $response; }
427
428 $search = $api->searchRequest();
429
430 // Get filters
431 $filters = dfrapi_api_query_to_filters( $query );
432
433 // Loop thru filters.
434 foreach ( $filters as $filter ) {
435 $search->addFilter( $filter );
436 }
437
438 // Exclude duplicates.
439 $duplicates = dfrapi_api_get_query_param( $query, 'duplicates' );
440 if ( $duplicates ) {
441 $excludes = $duplicates['value'];
442 $search->excludeDuplicates( $excludes );
443 }
444
445 // Exclude blocked products.
446 $excluded = (array) $excluded;
447 if ( !empty( $excluded ) ) {
448 $search->addFilter('id !IN ' . implode( ",", $excluded ) );
449 }
450
451 // Sort products.
452 $sort = dfrapi_api_get_query_param( $query, 'sort' );
453 if( $sort && strlen( $sort['operator'] ) ) {
454 $search->addSort( $sort['operator'] );
455 }
456
457 // Set limits and offset.
458 $search->setLimit( $ppp );
459 $search->setOffset( $offset );
460
461 // Execute query.
462 $products = $search->execute();
463
464 // Update API status
465 dfrapi_api_update_status( $api );
466
467 // Build $response array().
468 $response['query'] = $query;
469 $response['excluded'] = $excluded;
470 $response['products'] = $products;
471 $response['last_status'] = $api->lastStatus();
472 //$response['found_count'] = $search->getFoundCount(); Old, returned wrong value (#8672)
473 $response['found_count'] = $search->getResultCount();
474 $response['params'] = $search->getParams();
475
476 // Return it!
477 return $response;
478
479 } catch( Exception $err ) {
480 $params = $search->getParams();
481 return dfrapi_api_error( $err, $params );
482
483 }
484 }
485