| 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 ( 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 ); |
| 157 |
} |
| 158 |
dfrapi_update_transient_whitelist( $option_name ); |
| 159 |
return $zanox; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* This creates 2 options in the options table each time the option |
| 165 |
* "dfrapi_all_networks" is updated with new network information from the API. |
| 166 |
* |
| 167 |
* - dfrapi_product_networks |
| 168 |
* - dfrapi_coupon_networks |
| 169 |
* |
| 170 |
* These are just helper options to figure out if a network is a "product" |
| 171 |
* network or a "coupon" network. |
| 172 |
*/ |
| 173 |
function dfrapi_api_set_network_types( $networks ) { |
| 174 |
$product_networks = array(); |
| 175 |
$coupon_networks = array(); |
| 176 |
foreach( $networks as $network ) { |
| 177 |
if ( $network['type'] == 'products' ) { |
| 178 |
$product_networks[$network['_id']] = $network; |
| 179 |
} elseif ( $network['type'] == 'coupons' ) { |
| 180 |
$coupon_networks[$network['_id']] = $network; |
| 181 |
} |
| 182 |
} |
| 183 |
update_option( 'dfrapi_product_networks', $product_networks ); |
| 184 |
update_option( 'dfrapi_coupon_networks', $coupon_networks ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* This stores all merchants for a given source_id ($nid). |
| 189 |
* |
| 190 |
* It is possible to pass "all" to this function however this creates |
| 191 |
* memory_limit errors when memory is set to less than 64MB. |
| 192 |
*/ |
| 193 |
function dfrapi_api_get_all_merchants( $nid ) { |
| 194 |
$option_name = 'dfrapi_all_merchants_for_nid_' . $nid; |
| 195 |
$merchants = get_transient( $option_name ); |
| 196 |
if ( false === $merchants || empty ( $merchants ) ) { |
| 197 |
$api = dfrapi_api( dfrapi_get_transport_method() ); |
| 198 |
try { |
| 199 |
$merchants = $api->getMerchants( array( intval( $nid ) ), TRUE ); |
| 200 |
dfrapi_api_update_status( $api ); |
| 201 |
} catch( Exception $err ) { |
| 202 |
return dfrapi_api_error( $err ); |
| 203 |
} |
| 204 |
set_transient( $option_name, $merchants, DAY_IN_SECONDS ); |
| 205 |
} |
| 206 |
dfrapi_update_transient_whitelist( $option_name ); |
| 207 |
return $merchants; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* This retuns merchant or merchants' information by merchant_id or |
| 212 |
* an array of merchant IDs. |
| 213 |
*/ |
| 214 |
function dfrapi_api_get_merchants_by_id( $ids, $includeEmpty=FALSE ) { |
| 215 |
$name = false; |
| 216 |
if ( is_array( $ids ) ) { |
| 217 |
sort( $ids, SORT_NUMERIC ); |
| 218 |
$id_string = implode( ",", $ids ); |
| 219 |
$name = md5( $id_string ); |
| 220 |
} elseif ( $ids != '' ) { |
| 221 |
$name = trim( $ids ); |
| 222 |
} |
| 223 |
if ( !$name ) { return; } |
| 224 |
$name = substr( $name, 0, 20 ); |
| 225 |
$option_name = 'dfrapi_merchants_byid_' . $name; |
| 226 |
$merchants = get_transient( $option_name ); |
| 227 |
if ( false === $merchants || empty ( $merchants ) ) { |
| 228 |
$api = dfrapi_api( dfrapi_get_transport_method() ); |
| 229 |
try { |
| 230 |
$merchants = $api->getMerchantsById( $ids, $includeEmpty ); |
| 231 |
dfrapi_api_update_status( $api ); |
| 232 |
} catch( Exception $err ) { |
| 233 |
return dfrapi_api_error( $err ); |
| 234 |
} |
| 235 |
set_transient( $option_name, $merchants, DAY_IN_SECONDS ); |
| 236 |
} |
| 237 |
dfrapi_update_transient_whitelist( $option_name ); |
| 238 |
return $merchants; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Returns a $response array containing: |
| 243 |
* - ids: the query passed to the function. |
| 244 |
* - products: array of products. |
| 245 |
* - last_status: value of $api->lastStatus(). |
| 246 |
* - found_count: value of $search->getFoundCount(). |
| 247 |
* |
| 248 |
* If the API throws an exception, that will return dfrapi_api_error( $err ); |
| 249 |
* |
| 250 |
* @param array $ids An array of product IDs. |
| 251 |
* @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin. |
| 252 |
* @param int $page The page number for returning products. This is used to figure the offset. |
| 253 |
*/ |
| 254 |
function dfrapi_api_get_products_by_id( $ids, $ppp=20, $page=1 ) { |
| 255 |
|
| 256 |
$response = array(); |
| 257 |
|
| 258 |
// Return false if no $ids or no $postid |
| 259 |
if ( empty( $ids ) ) { return $response; } |
| 260 |
|
| 261 |
// Make sure $page is a positive integer. |
| 262 |
$page = intval( abs( $page ) ); |
| 263 |
|
| 264 |
// Make sure $ppp is a positive integer. |
| 265 |
$ppp = intval( abs( $ppp ) ); |
| 266 |
|
| 267 |
// Make sure $ppp is not greater than "max_length". |
| 268 |
$account = (array) get_option( 'dfrapi_account' ); |
| 269 |
if ( $ppp > $account['max_length'] ) { |
| 270 |
$ppp = $account['max_length']; |
| 271 |
} |
| 272 |
|
| 273 |
// The maximum number of results a request to the API can return. |
| 274 |
// Changing this will only break your site. It's not overridable. |
| 275 |
$max_total = $account['max_total']; |
| 276 |
|
| 277 |
// Determine offset. |
| 278 |
$offset = ( ( $page - 1 ) * $ppp ); |
| 279 |
|
| 280 |
// Make sure $limit doesn't go over 10,000. |
| 281 |
if ( ( $offset + $ppp ) > $max_total ) { |
| 282 |
$ppp = ( $max_total - $offset ); |
| 283 |
} |
| 284 |
|
| 285 |
// If $ppp is negative, return empty array(); |
| 286 |
if ( $ppp < 1 ) { |
| 287 |
return array(); |
| 288 |
} |
| 289 |
|
| 290 |
// If offset is greater than 10,000 return empty array(); |
| 291 |
if ( $offset >= ( $max_total - $ppp ) ) { |
| 292 |
return array(); |
| 293 |
} |
| 294 |
|
| 295 |
try { |
| 296 |
// Initialize API. |
| 297 |
$api = dfrapi_api( dfrapi_get_transport_method() ); |
| 298 |
if ( !$api ) { return $response; } |
| 299 |
|
| 300 |
// Get a range of product IDs to query. |
| 301 |
$id_range = array_slice( $ids, $offset, $ppp ); |
| 302 |
|
| 303 |
// Query API |
| 304 |
$products = $api->getProducts( $id_range ); |
| 305 |
|
| 306 |
// Update API status |
| 307 |
dfrapi_api_update_status( $api ); |
| 308 |
|
| 309 |
// Build $response array(). |
| 310 |
$response['ids'] = $ids; |
| 311 |
$response['products'] = $products; |
| 312 |
$response['last_status'] = $api->lastStatus(); |
| 313 |
$response['found_count'] = count( $ids ); |
| 314 |
|
| 315 |
// Return it! |
| 316 |
return $response; |
| 317 |
|
| 318 |
} catch( Exception $err ) { |
| 319 |
return dfrapi_api_error( $err ); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Returns a $response array containing: |
| 325 |
* - query: the query passed to the function. |
| 326 |
* - excluded: ids of excluded products. |
| 327 |
* - products: array of products. |
| 328 |
* - last_status: value of $api->lastStatus(). |
| 329 |
* - found_count: value of $search->getFoundCount(). |
| 330 |
* - params: value of $search->getParams(). |
| 331 |
* |
| 332 |
* Example of $query array(): |
| 333 |
* |
| 334 |
* |
| 335 |
* $query[] = array( |
| 336 |
* 'value' => 'shoes', |
| 337 |
* 'field' => 'any', |
| 338 |
* 'operator' => 'contain' |
| 339 |
* ); |
| 340 |
* |
| 341 |
* $query[] = array( |
| 342 |
* 'value' => 'image', |
| 343 |
* 'field' => 'duplicates', |
| 344 |
* 'operator' => 'is' |
| 345 |
* ); |
| 346 |
* |
| 347 |
* $query[] = array( |
| 348 |
* 'field' => 'sort', |
| 349 |
* 'operator' => '+saleprice' |
| 350 |
* ); |
| 351 |
* |
| 352 |
* |
| 353 |
* If the API throws an exception, that will return dfrapi_api_error( $err, $params ); |
| 354 |
* |
| 355 |
* @param array $query The complete query to pass to the API. |
| 356 |
* @param int $ppp The number of products to return in 1 API request. Max is dictated by API, not plugin. |
| 357 |
* @param int $page The page number for returning products. This is used to figure the offset. |
| 358 |
* @param array $excluded An array of product IDs to exclude from being returned. |
| 359 |
*/ |
| 360 |
function dfrapi_api_get_products_by_query( $query, $ppp=20, $page=1, $excluded=array() ) { |
| 361 |
|
| 362 |
$response = array(); |
| 363 |
|
| 364 |
// Return false if no $query. |
| 365 |
if ( empty( $query ) ) { return $response; } |
| 366 |
|
| 367 |
// Make sure $page is a positive integer. |
| 368 |
$page = intval( abs( $page ) ); |
| 369 |
|
| 370 |
// Make sure $ppp is a positive integer. |
| 371 |
$ppp = intval( abs( $ppp ) ); |
| 372 |
|
| 373 |
// Make sure $ppp is not greater than "max_length". |
| 374 |
$account = (array) get_option( 'dfrapi_account' ); |
| 375 |
if ( $ppp > $account['max_length'] ) { |
| 376 |
$ppp = $account['max_length']; |
| 377 |
} |
| 378 |
|
| 379 |
// The maximum number of results a request to the API can return. |
| 380 |
// Changing this will only break your site. It's not overridable. |
| 381 |
$max_total = $account['max_total']; |
| 382 |
|
| 383 |
// Detemine query limit (if exists). |
| 384 |
$query_limit = dfrapi_api_get_query_param( $query, 'limit' ); |
| 385 |
$query_limit = ( $query_limit ) |
| 386 |
? $query_limit['value'] |
| 387 |
: false; |
| 388 |
|
| 389 |
// No query shall try to return more than 10,000 products. |
| 390 |
if ( $query_limit && ( $query_limit > $max_total ) ) { |
| 391 |
$query_limit = $max_total; |
| 392 |
} |
| 393 |
|
| 394 |
// Determine offset. |
| 395 |
$offset = ( ( $page - 1 ) * $ppp ); |
| 396 |
|
| 397 |
// If offset is greater than 10,000 return empty array(); |
| 398 |
if ( $offset >= $max_total ) { |
| 399 |
return array(); |
| 400 |
} |
| 401 |
|
| 402 |
// Factor in query limit |
| 403 |
if ( $query_limit ) { |
| 404 |
if ( ( $ppp + $offset ) > $query_limit ) { |
| 405 |
$ppp = ( $query_limit - $offset ); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
// Make sure $limit doesn't go over 10,000. |
| 410 |
if ( ( $offset + $ppp ) > $max_total ) { |
| 411 |
$ppp = ( $max_total - $offset ); |
| 412 |
} |
| 413 |
|
| 414 |
// If $ppp is negative, return empty array(); |
| 415 |
if ( $ppp < 1 ) { |
| 416 |
return $response; |
| 417 |
} |
| 418 |
|
| 419 |
try { |
| 420 |
|
| 421 |
// Initialize API. |
| 422 |
$api = dfrapi_api( dfrapi_get_transport_method() ); |
| 423 |
if ( !$api ) { return $response; } |
| 424 |
|
| 425 |
$search = $api->searchRequest(); |
| 426 |
|
| 427 |
// Get filters |
| 428 |
$filters = dfrapi_api_query_to_filters( $query ); |
| 429 |
|
| 430 |
// Loop thru filters. |
| 431 |
foreach ( $filters as $filter ) { |
| 432 |
$search->addFilter( $filter ); |
| 433 |
} |
| 434 |
|
| 435 |
// Exclude duplicates. |
| 436 |
$duplicates = dfrapi_api_get_query_param( $query, 'duplicates' ); |
| 437 |
if ( $duplicates ) { |
| 438 |
$excludes = $duplicates['value']; |
| 439 |
$search->excludeDuplicates( $excludes ); |
| 440 |
} |
| 441 |
|
| 442 |
// Exclude blocked products. |
| 443 |
$excluded = (array) $excluded; |
| 444 |
if ( !empty( $excluded ) ) { |
| 445 |
$search->addFilter('id !IN ' . implode( ",", $excluded ) ); |
| 446 |
} |
| 447 |
|
| 448 |
// Sort products. |
| 449 |
$sort = dfrapi_api_get_query_param( $query, 'sort' ); |
| 450 |
if( $sort && strlen( $sort['operator'] ) ) { |
| 451 |
$search->addSort( $sort['operator'] ); |
| 452 |
} |
| 453 |
|
| 454 |
// Set limits and offset. |
| 455 |
$search->setLimit( $ppp ); |
| 456 |
$search->setOffset( $offset ); |
| 457 |
|
| 458 |
// Execute query. |
| 459 |
$products = $search->execute(); |
| 460 |
|
| 461 |
// Update API status |
| 462 |
dfrapi_api_update_status( $api ); |
| 463 |
|
| 464 |
// 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 |
|
| 472 |
// Return it! |
| 473 |
return $response; |
| 474 |
|
| 475 |
} catch( Exception $err ) { |
| 476 |
$params = $search->getParams(); |
| 477 |
return dfrapi_api_error( $err, $params ); |
| 478 |
|
| 479 |
} |
| 480 |
} |
| 481 |
|