| 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', 'datafeedr-api' ), |
| 492 |
'price' => 0, |
| 493 |
'finalprice' => 0, |
| 494 |
'description' => __( 'This product is either temporarily or permanently unavailable.', 'datafeedr-api' ), |
| 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 |
$response['score'] = $search->getQueryScore(); |
| 512 |
|
| 513 |
// Return it! |
| 514 |
return $response; |
| 515 |
|
| 516 |
} catch ( Exception $err ) { |
| 517 |
return dfrapi_api_error( $err ); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Returns a $response array containing: |
| 523 |
* - query: the query passed to the function. |
| 524 |
* - excluded: ids of excluded products. |
| 525 |
* - products: array of products. |
| 526 |
* - last_status: value of $api->lastStatus(). |
| 527 |
* - found_count: value of $search->getFoundCount(). |
| 528 |
* - params: value of $search->getParams(). |
| 529 |
* |
| 530 |
* Example of $query array(): |
| 531 |
* |
| 532 |
* |
| 533 |
* $query[] = array( |
| 534 |
* 'value' => 'shoes', |
| 535 |
* 'field' => 'any', |
| 536 |
* 'operator' => 'contain' |
| 537 |
* ); |
| 538 |
* |
| 539 |
* $query[] = array( |
| 540 |
* 'value' => 'image', |
| 541 |
* 'field' => 'duplicates', |
| 542 |
* 'operator' => 'is' |
| 543 |
* ); |
| 544 |
* |
| 545 |
* $query[] = array( |
| 546 |
* 'field' => 'sort', |
| 547 |
* 'operator' => '+saleprice' |
| 548 |
* ); |
| 549 |
* |
| 550 |
* |
| 551 |
* If the API throws an exception, that will return dfrapi_api_error( $err, $params ); |
| 552 |
* |
| 553 |
* @param array $query The complete query to pass to the API. |
| 554 |
* @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin. |
| 555 |
* @param int $page The page number for returning products. This is used to figure the offset. |
| 556 |
* @param array $excluded An array of product IDs to exclude from being returned. |
| 557 |
*/ |
| 558 |
function dfrapi_api_get_products_by_query( $query, $ppp = 20, $page = 1, $excluded = array() ) { |
| 559 |
|
| 560 |
$response = array(); |
| 561 |
|
| 562 |
// Return false if no $query. |
| 563 |
if ( empty( $query ) ) { |
| 564 |
return $response; |
| 565 |
} |
| 566 |
|
| 567 |
// Make sure $page is a positive integer. |
| 568 |
$page = intval( abs( $page ) ); |
| 569 |
|
| 570 |
// Make sure $ppp is a positive integer. |
| 571 |
$ppp = intval( abs( $ppp ) ); |
| 572 |
|
| 573 |
// Make sure $ppp is not greater than "max_length". |
| 574 |
$account = (array) get_option( 'dfrapi_account' ); |
| 575 |
if ( $ppp > $account['max_length'] ) { |
| 576 |
$ppp = $account['max_length']; |
| 577 |
} |
| 578 |
|
| 579 |
// The maximum number of results a request to the API can return. |
| 580 |
// Changing this will only break your site. It's not overridable. |
| 581 |
$max_total = $account['max_total']; |
| 582 |
|
| 583 |
// Detemine query limit (if exists). |
| 584 |
$query_limit = dfrapi_api_get_query_param( $query, 'limit' ); |
| 585 |
$query_limit = ( $query_limit ) |
| 586 |
? $query_limit['value'] |
| 587 |
: false; |
| 588 |
|
| 589 |
// No query shall try to return more than 10,000 products. |
| 590 |
if ( $query_limit && ( $query_limit > $max_total ) ) { |
| 591 |
$query_limit = $max_total; |
| 592 |
} |
| 593 |
|
| 594 |
// Detemine merchant limit (if exists). |
| 595 |
$merchant_limit = dfrapi_api_get_query_param( $query, 'merchant_limit' ); |
| 596 |
$merchant_limit = ( $merchant_limit ) |
| 597 |
? absint( $merchant_limit['value'] ) |
| 598 |
: 0; |
| 599 |
|
| 600 |
// Determine offset. |
| 601 |
$offset = ( ( $page - 1 ) * $ppp ); |
| 602 |
|
| 603 |
// If offset is greater than 10,000 return empty array(); |
| 604 |
if ( $offset >= $max_total ) { |
| 605 |
return array(); |
| 606 |
} |
| 607 |
|
| 608 |
// Factor in query limit |
| 609 |
if ( $query_limit ) { |
| 610 |
if ( ( $ppp + $offset ) > $query_limit ) { |
| 611 |
$ppp = ( $query_limit - $offset ); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
// Make sure $limit doesn't go over 10,000. |
| 616 |
if ( ( $offset + $ppp ) > $max_total ) { |
| 617 |
$ppp = ( $max_total - $offset ); |
| 618 |
} |
| 619 |
|
| 620 |
// If $ppp is negative, return empty array(); |
| 621 |
if ( $ppp < 1 ) { |
| 622 |
return $response; |
| 623 |
} |
| 624 |
|
| 625 |
try { |
| 626 |
|
| 627 |
// Initialize API. |
| 628 |
$api = dfrapi_api( dfrapi_get_transport_method() ); |
| 629 |
if ( ! $api ) { |
| 630 |
return $response; |
| 631 |
} |
| 632 |
|
| 633 |
$search = $api->searchRequest(); |
| 634 |
|
| 635 |
// Get filters |
| 636 |
$filters = dfrapi_api_query_to_filters( $query ); |
| 637 |
if ( isset( $filters['error'] ) ) { |
| 638 |
throw new DatafeedrError( $filters['error'], 0 ); |
| 639 |
} |
| 640 |
|
| 641 |
// Loop thru filters. |
| 642 |
foreach ( $filters as $filter ) { |
| 643 |
$search->addFilter( $filter ); |
| 644 |
} |
| 645 |
|
| 646 |
// Exclude duplicates. |
| 647 |
$duplicates = dfrapi_api_get_query_param( $query, 'duplicates' ); |
| 648 |
if ( $duplicates ) { |
| 649 |
$excludes = $duplicates['value']; |
| 650 |
$search->excludeDuplicates( $excludes ); |
| 651 |
} |
| 652 |
|
| 653 |
// Exclude blocked products. |
| 654 |
$excluded = (array) $excluded; |
| 655 |
if ( ! empty( $excluded ) ) { |
| 656 |
$search->addFilter( 'id !IN ' . implode( ",", $excluded ) ); |
| 657 |
} |
| 658 |
|
| 659 |
// Sort products. |
| 660 |
$sort = dfrapi_api_get_query_param( $query, 'sort' ); |
| 661 |
if ( $sort && strlen( $sort['operator'] ) ) { |
| 662 |
$search->addSort( $sort['operator'] ); |
| 663 |
} |
| 664 |
|
| 665 |
// Set Merchant Limit |
| 666 |
$search->setMerchantLimit( $merchant_limit ); |
| 667 |
|
| 668 |
// Set limits and offset. |
| 669 |
$search->setLimit( $ppp ); |
| 670 |
$search->setOffset( $offset ); |
| 671 |
|
| 672 |
// Execute query. |
| 673 |
$products = $search->execute(); |
| 674 |
|
| 675 |
// Update API status |
| 676 |
dfrapi_api_update_status( $api ); |
| 677 |
|
| 678 |
// Build $response array(). |
| 679 |
$response['query'] = $query; |
| 680 |
$response['excluded'] = $excluded; |
| 681 |
$response['products'] = $products; |
| 682 |
$response['last_status'] = $api->lastStatus(); |
| 683 |
$response['found_count'] = $search->getResultCount(); |
| 684 |
$response['params'] = $search->getParams(); |
| 685 |
$response['score'] = $search->getQueryScore(); |
| 686 |
|
| 687 |
// Return it! |
| 688 |
return $response; |
| 689 |
|
| 690 |
} catch ( Exception $err ) { |
| 691 |
$params = $search->getParams(); |
| 692 |
|
| 693 |
return dfrapi_api_error( $err, $params ); |
| 694 |
|
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
function dfrapi_get_effiliation_product_feeds_url( $api_key ) { |
| 700 |
return sprintf( 'http://apiv2.effiliation.com/apiv2/productfeeds.xml?key=%s&filter=mines&type=33&fields=0001010000110001', $api_key ); |
| 701 |
} |
| 702 |
|
| 703 |
function dfrapi_request_effiliation_affiliate_ids( $api_key = null ) { |
| 704 |
|
| 705 |
$option_name = 'effiliation_affiliate_ids'; |
| 706 |
$use_cache = wp_using_ext_object_cache( false ); |
| 707 |
$affiliate_ids = get_transient( $option_name ); |
| 708 |
wp_using_ext_object_cache( $use_cache ); |
| 709 |
|
| 710 |
if ( $affiliate_ids ) { |
| 711 |
return $affiliate_ids; |
| 712 |
} |
| 713 |
|
| 714 |
$keys = dfrapi_get_effiliation_keys(); |
| 715 |
$api_key = $api_key ?: $keys['effiliation_key']; |
| 716 |
$method = 'GET'; |
| 717 |
$url = dfrapi_get_effiliation_product_feeds_url( $api_key ); |
| 718 |
|
| 719 |
$xml = dfrapi_get_xml_response( $url, $method, [ 'timeout' => 30 ] ); |
| 720 |
|
| 721 |
if ( is_wp_error( $xml ) ) { |
| 722 |
return $xml; |
| 723 |
} |
| 724 |
|
| 725 |
$affiliate_ids = []; |
| 726 |
|
| 727 |
foreach ( $xml->feed as $e ) { |
| 728 |
$item = json_decode( json_encode( $e ), true ); |
| 729 |
$suid = sanitize_text_field( $item['id_affilieur'] ); |
| 730 |
|
| 731 |
$affiliate_ids[ $suid ]['suid'] = ( $suid ); |
| 732 |
$affiliate_ids[ $suid ]['affiliate_id'] = sanitize_text_field( $item['id_compteur'] ); |
| 733 |
} |
| 734 |
|
| 735 |
$use_cache = wp_using_ext_object_cache( false ); |
| 736 |
set_transient( $option_name, $affiliate_ids, ( MINUTE_IN_SECONDS * 20 ) ); |
| 737 |
wp_using_ext_object_cache( $use_cache ); |
| 738 |
dfrapi_update_transient_whitelist( $option_name ); |
| 739 |
|
| 740 |
return $affiliate_ids; |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* @param $merchant_id |
| 745 |
* |
| 746 |
* @return mixed|string |
| 747 |
* @throws Exception |
| 748 |
*/ |
| 749 |
function dfrapi_get_affiliate_id_for_effiliation_merchant( $merchant_id ) { |
| 750 |
$merchants = dfrapi_api_get_merchants_by_id( $merchant_id ); |
| 751 |
$merchant = isset( $merchants[0] ) ? $merchants[0] : [ 'suids' => '' ]; |
| 752 |
$affiliate_ids = dfrapi_request_effiliation_affiliate_ids(); |
| 753 |
|
| 754 |
if ( is_wp_error( $affiliate_ids ) ) { |
| 755 |
throw new Exception( 'Unable to query Effiliation at this time. Please try again in 15 minutes.' ); |
| 756 |
} |
| 757 |
|
| 758 |
if ( ! isset( $affiliate_ids[ $merchant['suids'] ]['affiliate_id'] ) ) { |
| 759 |
throw new Exception( 'Suid does not exist for affiliate ID.' ); |
| 760 |
} |
| 761 |
|
| 762 |
return $affiliate_ids[ $merchant['suids'] ]['affiliate_id']; |
| 763 |
} |
| 764 |
|