| 1 |
<?php |
| 2 |
|
| 3 |
function dfrapi_get_api_usage_percentage() { |
| 4 |
$account = get_option( 'dfrapi_account' ); |
| 5 |
if ( $account ) { |
| 6 |
if ( $account['max_requests'] > 0 ) { |
| 7 |
$percentage = floor( ( intval( $account['request_count'] ) / intval( $account['max_requests'] ) * 100 ) ); |
| 8 |
} else { |
| 9 |
$percentage = 0; |
| 10 |
} |
| 11 |
|
| 12 |
return $percentage; |
| 13 |
} |
| 14 |
|
| 15 |
return false; |
| 16 |
} |
| 17 |
|
| 18 |
add_action( 'init', 'dfrapi_email_user_about_usage' ); |
| 19 |
function dfrapi_email_user_about_usage() { |
| 20 |
|
| 21 |
$percentage = dfrapi_get_api_usage_percentage(); |
| 22 |
$status = get_option( 'dfrapi_account', array() ); |
| 23 |
|
| 24 |
$request_count = ( isset( $status['request_count'] ) ) ? abs( $status['request_count'] ) : 0; |
| 25 |
$remaining_requests = ( isset( $status['max_requests'] ) ) ? abs( $status['max_requests'] - $request_count ) : 0; |
| 26 |
|
| 27 |
$reset_date = ''; |
| 28 |
if ( isset( $status['bill_day'] ) ) { |
| 29 |
$today = date( 'j' ); |
| 30 |
$num_days = date( 't' ); |
| 31 |
if ( $status['bill_day'] > $num_days ) { |
| 32 |
$bill_day = $num_days; |
| 33 |
} else { |
| 34 |
$bill_day = $status['bill_day']; |
| 35 |
} |
| 36 |
if ( $bill_day == 0 ) { |
| 37 |
$reset_date .= '<em>' . __( 'Never', DFRAPI_DOMAIN ) . '</em>'; |
| 38 |
} elseif ( $today >= $bill_day ) { |
| 39 |
$reset_date .= date( 'F', strtotime( '+1 month' ) ) . ' ' . $bill_day . ', ' . date( 'Y', strtotime( '+1 month' ) ); |
| 40 |
} else { |
| 41 |
$reset_date .= date( 'F' ) . ' ' . $bill_day . ', ' . date( 'Y' ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
$default = array( |
| 46 |
'90_percent' => '', |
| 47 |
'100_percent' => '' |
| 48 |
); |
| 49 |
|
| 50 |
// Don't do anything if less than 90%. |
| 51 |
if ( $percentage < 90 ) { |
| 52 |
update_option( 'dfrapi_usage_notification_tracker', $default ); |
| 53 |
|
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$tracker = get_option( 'dfrapi_usage_notification_tracker', $default ); |
| 58 |
|
| 59 |
$params = array(); |
| 60 |
$params['to'] = get_bloginfo( 'admin_email' ); |
| 61 |
$params['message'] = "<p>" . __( "This is an automated message generated by: ", DFRAPI_DOMAIN ) . get_bloginfo( 'wpurl' ) . "</p>"; |
| 62 |
|
| 63 |
if ( $percentage >= 100 && empty( $tracker['100_percent'] ) ) { |
| 64 |
|
| 65 |
$params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Critical)', DFRAPI_DOMAIN ); |
| 66 |
|
| 67 |
$params['message'] .= "<p>" . __( "You have used <strong>100%</strong> of your allocated Datafeedr API requests for this period. <u>You are no longer able to query the Datafeedr API to get product information.</u>", DFRAPI_DOMAIN ) . "</p>"; |
| 68 |
$params['message'] .= "<p><strong>" . __( "What to do next?", DFRAPI_DOMAIN ) . "</strong></p>"; |
| 69 |
$params['message'] .= "<p>" . __( "We strongly recommend that you upgrade to prevent your product information from becoming outdated.", DFRAPI_DOMAIN ) . "</p>"; |
| 70 |
$params['message'] .= "<p><a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\"><strong>" . __( "UPGRADE NOW", DFRAPI_DOMAIN ) . "</strong></a></p>"; |
| 71 |
$params['message'] .= "<p>" . __( "Upgrading only takes a minute. You will have <strong>instant access</strong> to more API requests. Any remaining credit for your current plan will be applied to your new plan.", DFRAPI_DOMAIN ) . "</p>"; |
| 72 |
$params['message'] .= "<p>" . __( "You are under no obligation to upgrade. You may continue using your current plan for as long as you would like.", DFRAPI_DOMAIN ) . "</p>"; |
| 73 |
$params['message'] .= "<p>" . __( "If you have any questions about your account, please ", DFRAPI_DOMAIN ); |
| 74 |
$params['message'] .= "<a href=\"" . DFRAPI_EMAIL_US_URL . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\">" . __( "contact us", DFRAPI_DOMAIN ) . "</a>.</p>"; |
| 75 |
$params['message'] .= "<p>" . __( "Thanks,<br />Eric & Stefan<br />The Datafeedr Team", DFRAPI_DOMAIN ) . "</p>"; |
| 76 |
$params['message'] .= "<p>"; |
| 77 |
$params['message'] .= "<a href=\"" . admin_url( 'admin.php?page=dfrapi_account' ) . "\">" . __( "Account Information", DFRAPI_DOMAIN ) . "</a> | "; |
| 78 |
$params['message'] .= "<a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\">" . __( "Upgrade Account", DFRAPI_DOMAIN ) . "</a>"; |
| 79 |
$params['message'] .= "</p>"; |
| 80 |
|
| 81 |
$tracker['100_percent'] = 1; |
| 82 |
update_option( 'dfrapi_usage_notification_tracker', $tracker ); |
| 83 |
|
| 84 |
add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 85 |
wp_mail( $params['to'], $params['subject'], $params['message'] ); |
| 86 |
remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 87 |
|
| 88 |
|
| 89 |
} elseif ( $percentage >= 90 && $percentage < 100 && empty( $tracker['90_percent'] ) ) { |
| 90 |
|
| 91 |
$params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Warning)', DFRAPI_DOMAIN ); |
| 92 |
$params['message'] .= "<p>" . __( "You have used <strong>90%</strong> of your allocated Datafeedr API requests for this period.", DFRAPI_DOMAIN ) . "</p>"; |
| 93 |
$params['message'] .= "<p><strong>" . __( "API Usage", DFRAPI_DOMAIN ) . "</strong></p>"; |
| 94 |
$params['message'] .= "<ul>"; |
| 95 |
$params['message'] .= "<li>" . __( "API requests used: ", DFRAPI_DOMAIN ) . $request_count . "</li>"; |
| 96 |
$params['message'] .= "<li>" . __( "API requests remaining: ", DFRAPI_DOMAIN ) . $remaining_requests . "</li>"; |
| 97 |
$params['message'] .= "<li>" . __( "API requests will reset on: ", DFRAPI_DOMAIN ) . $reset_date . "</li>"; |
| 98 |
$params['message'] .= "</ul>"; |
| 99 |
$params['message'] .= "<p><strong>" . __( "What to do next?", DFRAPI_DOMAIN ) . "</strong></p>"; |
| 100 |
$params['message'] .= "<p>" . __( "We recommend that you upgrade to prevent your product information from becoming outdated.", DFRAPI_DOMAIN ) . "</p>"; |
| 101 |
$params['message'] .= "<p><a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\"><strong>" . __( "UPGRADE NOW", DFRAPI_DOMAIN ) . "</strong></a></p>"; |
| 102 |
$params['message'] .= "<p>" . __( "Upgrading only takes a minute. You will have <strong>instant access</strong> to more API requests. Any remaining credit for your current plan will be applied to your new plan.", DFRAPI_DOMAIN ) . "</p>"; |
| 103 |
$params['message'] .= "<p>" . __( "You are under no obligation to upgrade. You may continue using your current plan for as long as you would like.", DFRAPI_DOMAIN ) . "</p>"; |
| 104 |
$params['message'] .= "<p>" . __( "If you have any questions about your account, please ", DFRAPI_DOMAIN ); |
| 105 |
$params['message'] .= "<a href=\"" . DFRAPI_EMAIL_US_URL . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\">" . __( "contact us", DFRAPI_DOMAIN ) . "</a>.</p>"; |
| 106 |
$params['message'] .= "<p>" . __( "Thanks,<br />Eric & Stefan<br />The Datafeedr Team", DFRAPI_DOMAIN ) . "</p>"; |
| 107 |
$params['message'] .= "<p>"; |
| 108 |
$params['message'] .= "<a href=\"" . admin_url( 'admin.php?page=dfrapi_account' ) . "\">" . __( "Account Information", DFRAPI_DOMAIN ) . "</a> | "; |
| 109 |
$params['message'] .= "<a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\">" . __( "Upgrade Account", DFRAPI_DOMAIN ) . "</a>"; |
| 110 |
$params['message'] .= "</p>"; |
| 111 |
|
| 112 |
$tracker['90_percent'] = 1; |
| 113 |
update_option( 'dfrapi_usage_notification_tracker', $tracker ); |
| 114 |
|
| 115 |
add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 116 |
wp_mail( $params['to'], $params['subject'], $params['message'] ); |
| 117 |
remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' ); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
function dfrapi_set_html_content_type() { |
| 125 |
return 'text/html'; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Modify affiliate ID if product is a Zanox product. |
| 130 |
* Replaces $affiliate_id with "zmid". |
| 131 |
*/ |
| 132 |
add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_zanox_zmid', 10, 3 ); |
| 133 |
function dfrapi_get_zanox_zmid( $affiliate_id, $product, $networks ) { |
| 134 |
if ( isset( $product['source'] ) && preg_match( "/\bZanox\b/", $product['source'] ) ) { |
| 135 |
$zanox = dfrapi_api_get_zanox_zmid( $product['merchant_id'], $affiliate_id ); |
| 136 |
$affiliate_id = ( ! isset( $zanox[0]['zmid'] ) ) ? '___MISSING___' : $zanox[0]['zmid']; |
| 137 |
} |
| 138 |
|
| 139 |
return $affiliate_id; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Modify affiliate ID if product is a Partnerize product. |
| 144 |
* Replaces $affiliate_id with "camref". |
| 145 |
* |
| 146 |
* @return string Affiliate ID. |
| 147 |
* @since 1.0.66 |
| 148 |
* |
| 149 |
*/ |
| 150 |
add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_ph_camref', 10, 3 ); |
| 151 |
function dfrapi_get_ph_camref( $affiliate_id, $product, $networks ) { |
| 152 |
if ( isset( $product['source'] ) && preg_match( "/\bPartnerize\b/", $product['source'] ) ) { |
| 153 |
$ph = dfrapi_api_get_ph_camref( $product['merchant_id'] ); |
| 154 |
$affiliate_id = ( ! isset( $ph[0]['camref'] ) ) ? '___MISSING___' : $ph[0]['camref']; |
| 155 |
} |
| 156 |
|
| 157 |
return $affiliate_id; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Modify affiliate ID if product is a Effiliation product. |
| 162 |
* Replaces $affiliate_id with "affiliate ID". |
| 163 |
* |
| 164 |
* @return string Affiliate ID. |
| 165 |
* @since 1.0.81 |
| 166 |
* |
| 167 |
*/ |
| 168 |
function dfrapi_get_effiliation_affiliate_id( $affiliate_id, $product, $networks ) { |
| 169 |
if ( isset( $product['source'] ) && preg_match( "/\bEffiliation\b/", $product['source'] ) ) { |
| 170 |
$effiliation = dfrapi_api_get_effiliation_affiliate_id( $product['merchant_id'] ); |
| 171 |
$affiliate_id = $effiliation == 'dfrapi_unapproved_effiliation_merchant' ? '___MISSING___' : $effiliation; |
| 172 |
} |
| 173 |
|
| 174 |
return $affiliate_id; |
| 175 |
} |
| 176 |
|
| 177 |
add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_effiliation_affiliate_id', 10, 3 ); |
| 178 |
|
| 179 |
function dfrapi_get_zanox_keys() { |
| 180 |
|
| 181 |
$configuration = (array) get_option( 'dfrapi_configuration' ); |
| 182 |
|
| 183 |
$zanox_connection_key = false; |
| 184 |
$zanox_secret_key = false; |
| 185 |
|
| 186 |
if ( isset( $configuration['zanox_connection_key'] ) && ( $configuration['zanox_connection_key'] != '' ) ) { |
| 187 |
$zanox_connection_key = $configuration['zanox_connection_key']; |
| 188 |
} |
| 189 |
|
| 190 |
if ( isset( $configuration['zanox_secret_key'] ) && ( $configuration['zanox_secret_key'] != '' ) ) { |
| 191 |
$zanox_secret_key = $configuration['zanox_secret_key']; |
| 192 |
} |
| 193 |
|
| 194 |
if ( $zanox_connection_key && $zanox_secret_key ) { |
| 195 |
return array( |
| 196 |
'connection_key' => $zanox_connection_key, |
| 197 |
'secret_key' => $zanox_secret_key, |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get Partnerize Keys. |
| 206 |
* |
| 207 |
* @return array|bool Array of keys or false if they do not exist. |
| 208 |
* @since 1.0.66 |
| 209 |
* |
| 210 |
*/ |
| 211 |
function dfrapi_get_ph_keys() { |
| 212 |
|
| 213 |
$configuration = (array) get_option( 'dfrapi_configuration' ); |
| 214 |
|
| 215 |
$ph_application_key = false; |
| 216 |
$ph_user_api_key = false; |
| 217 |
$ph_publisher_id = false; |
| 218 |
|
| 219 |
if ( isset( $configuration['ph_application_key'] ) && ( $configuration['ph_application_key'] != '' ) ) { |
| 220 |
$ph_application_key = $configuration['ph_application_key']; |
| 221 |
} |
| 222 |
|
| 223 |
if ( isset( $configuration['ph_user_api_key'] ) && ( $configuration['ph_user_api_key'] != '' ) ) { |
| 224 |
$ph_user_api_key = $configuration['ph_user_api_key']; |
| 225 |
} |
| 226 |
|
| 227 |
if ( isset( $configuration['ph_publisher_id'] ) && ( $configuration['ph_publisher_id'] != '' ) ) { |
| 228 |
$ph_publisher_id = $configuration['ph_publisher_id']; |
| 229 |
} |
| 230 |
|
| 231 |
if ( $ph_application_key && $ph_user_api_key && $ph_publisher_id ) { |
| 232 |
return array( |
| 233 |
'application_key' => $ph_application_key, |
| 234 |
'user_api_key' => $ph_user_api_key, |
| 235 |
'publisher_id' => $ph_publisher_id, |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Get Effiliation Keys. |
| 244 |
* |
| 245 |
* @return array|bool Array of keys or false if they do not exist. |
| 246 |
* @since 1.0.81 |
| 247 |
* |
| 248 |
*/ |
| 249 |
function dfrapi_get_effiliation_keys() { |
| 250 |
|
| 251 |
$configuration = (array) get_option( 'dfrapi_configuration' ); |
| 252 |
|
| 253 |
$effiliation_key = false; |
| 254 |
|
| 255 |
if ( isset( $configuration['effiliation_key'] ) && ( $configuration['effiliation_key'] != '' ) ) { |
| 256 |
$effiliation_key = $configuration['effiliation_key']; |
| 257 |
} |
| 258 |
|
| 259 |
if ( $effiliation_key ) { |
| 260 |
return array( |
| 261 |
'effiliation_key' => $effiliation_key, |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Returns Amazon API key credentials if they exist. |
| 271 |
* |
| 272 |
* If the Amazon API keys such as the Access Key ID, Secret Access Key, Tracking ID and Locale all |
| 273 |
* exists, then this function returns them in array format. Otherwise it returns false. |
| 274 |
* |
| 275 |
* @return array|bool Returns array of values if all values exist, otherwise false. |
| 276 |
* @since 1.0.33 |
| 277 |
* |
| 278 |
*/ |
| 279 |
function dfrapi_get_amazon_keys() { |
| 280 |
|
| 281 |
$configuration = (array) get_option( 'dfrapi_configuration' ); |
| 282 |
|
| 283 |
$amazon_access_key_id = false; |
| 284 |
$amazon_secret_access_key = false; |
| 285 |
$amazon_tracking_id = false; |
| 286 |
$amazon_locale = false; |
| 287 |
|
| 288 |
if ( isset( $configuration['amazon_access_key_id'] ) && ( $configuration['amazon_access_key_id'] != '' ) ) { |
| 289 |
$amazon_access_key_id = $configuration['amazon_access_key_id']; |
| 290 |
} |
| 291 |
|
| 292 |
if ( isset( $configuration['amazon_secret_access_key'] ) && ( $configuration['amazon_secret_access_key'] != '' ) ) { |
| 293 |
$amazon_secret_access_key = $configuration['amazon_secret_access_key']; |
| 294 |
} |
| 295 |
|
| 296 |
if ( isset( $configuration['amazon_tracking_id'] ) && ( $configuration['amazon_tracking_id'] != '' ) ) { |
| 297 |
$amazon_tracking_id = $configuration['amazon_tracking_id']; |
| 298 |
} |
| 299 |
|
| 300 |
if ( isset( $configuration['amazon_locale'] ) && ( $configuration['amazon_locale'] != '' ) ) { |
| 301 |
$amazon_locale = $configuration['amazon_locale']; |
| 302 |
} |
| 303 |
|
| 304 |
if ( $amazon_access_key_id && $amazon_secret_access_key && $amazon_tracking_id && $amazon_locale ) { |
| 305 |
return array( |
| 306 |
'amazon_access_key_id' => $amazon_access_key_id, |
| 307 |
'amazon_secret_access_key' => $amazon_secret_access_key, |
| 308 |
'amazon_tracking_id' => $amazon_tracking_id, |
| 309 |
'amazon_locale' => $amazon_locale, |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns a link to a user page on v4.datafeedr.com. |
| 318 |
*/ |
| 319 |
function dfrapi_user_pages( $page ) { |
| 320 |
|
| 321 |
$pages = array( |
| 322 |
'edit' => 'https://members.datafeedr.com/account', |
| 323 |
'invoices' => 'https://members.datafeedr.com/invoices', |
| 324 |
'billing' => 'https://members.datafeedr.com/billing', |
| 325 |
'cancel' => 'https://members.datafeedr.com/', |
| 326 |
'change' => 'https://members.datafeedr.com/subscription', |
| 327 |
'signup' => 'https://members.datafeedr.com/subscribe', |
| 328 |
'summary' => 'https://members.datafeedr.com/subscription', |
| 329 |
'api' => 'https://members.datafeedr.com/api', |
| 330 |
'factory' => 'https://members.datafeedr.com/factory', |
| 331 |
); |
| 332 |
|
| 333 |
return $pages[ $page ]; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Adds option name to transient whitelist. This is so we know |
| 338 |
* all transient options that can be deleted when deleting the |
| 339 |
* API cache on Tools page. |
| 340 |
*/ |
| 341 |
function dfrapi_update_transient_whitelist( $option_name ) { |
| 342 |
$whitelist = get_option( 'dfrapi_transient_whitelist', array() ); |
| 343 |
$whitelist[] = $option_name; |
| 344 |
update_option( 'dfrapi_transient_whitelist', array_unique( $whitelist ) ); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Add affiliate ID and tracking ID to an affiliate link. |
| 349 |
* |
| 350 |
* @param array $product An array of a single Datafeedr $product. |
| 351 |
* |
| 352 |
* @return string A URL with affiliate ID inserted or an empty string if the affiliate ID is missing. |
| 353 |
*/ |
| 354 |
function dfrapi_url( $product ) { |
| 355 |
|
| 356 |
// Get all the user's selected networks. |
| 357 |
$networks = (array) get_option( 'dfrapi_networks' ); |
| 358 |
|
| 359 |
// Support added for Amazon in version 1.0.60 (2017-10-18) Ticket #15201 |
| 360 |
if ( substr( $product['source'], 0, 6 ) === "Amazon" ) { |
| 361 |
// Get the user's Amazon Associate Tag |
| 362 |
$affiliate_id = dfrapi_get_amazon_associate_tag(); |
| 363 |
} else { |
| 364 |
// Extract the affiliate ID from the $networks array. |
| 365 |
$affiliate_id = isset( $networks['ids'][ $product['source_id'] ]['aid'] ) ? $networks['ids'][ $product['source_id'] ]['aid'] : ''; |
| 366 |
} |
| 367 |
|
| 368 |
$affiliate_id = apply_filters( 'dfrapi_affiliate_id', $affiliate_id, $product, $networks ); |
| 369 |
$affiliate_id = trim( $affiliate_id ); |
| 370 |
|
| 371 |
// Extract the Tracking ID from the $networks array. |
| 372 |
$tracking_id = ( isset( $networks['ids'][ $product['source_id'] ]['tid'] ) ) ? $networks['ids'][ $product['source_id'] ]['tid'] : ''; |
| 373 |
$tracking_id = apply_filters( 'dfrapi_tracking_id', $tracking_id, $product, $networks ); |
| 374 |
$tracking_id = trim( $tracking_id ); |
| 375 |
|
| 376 |
// Affiliate ID is missing. Do action and return empty string. |
| 377 |
if ( $affiliate_id == '' ) { |
| 378 |
do_action( 'dfrapi_affiliate_id_is_missing', $product ); |
| 379 |
|
| 380 |
return ''; |
| 381 |
} |
| 382 |
|
| 383 |
// Determine which URL field to get: 'url' OR 'ref_url'. Return 'url' if $tracking_id is empty, otherwise, use 'ref_url'. |
| 384 |
$url = ( $tracking_id !== '' && isset( $product['ref_url'] ) ) ? $product['ref_url'] : $product['url']; |
| 385 |
|
| 386 |
// Apply filters to URL before affiliate & tracking ID insertion. |
| 387 |
$url = apply_filters( 'dfrapi_before_affiliate_id_insertion', $url, $product, $affiliate_id ); |
| 388 |
$url = apply_filters( 'dfrapi_before_tracking_id_insertion', $url, $product, $tracking_id ); |
| 389 |
|
| 390 |
// Replace placeholders in URL. |
| 391 |
$placeholders = array( "@@@", "###" ); |
| 392 |
$replacements = array( $affiliate_id, $tracking_id ); |
| 393 |
$url = str_replace( $placeholders, $replacements, $url ); |
| 394 |
|
| 395 |
// Apply filters to URL after affiliate & tracking ID insertion. |
| 396 |
$url = apply_filters( 'dfrapi_after_affiliate_id_insertion', $url, $product, $affiliate_id ); |
| 397 |
$url = apply_filters( 'dfrapi_after_tracking_id_insertion', $url, $product, $tracking_id ); |
| 398 |
|
| 399 |
// Return URL |
| 400 |
return $url; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Return Amazon Associate Tag (ie. Tracking ID). |
| 405 |
* |
| 406 |
* @return string Associate Tag or empty string if it does not exist. |
| 407 |
* @since 1.0.60 |
| 408 |
* |
| 409 |
*/ |
| 410 |
function dfrapi_get_amazon_associate_tag() { |
| 411 |
$config = get_option( 'dfrapi_configuration' ); |
| 412 |
|
| 413 |
return ( isset( $config['amazon_tracking_id'] ) ) ? $config['amazon_tracking_id'] : ''; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Add affiliate ID to impression URL. |
| 418 |
* |
| 419 |
* Since 1.0.39 |
| 420 |
* |
| 421 |
* @param $product - An array of a single's product's information. |
| 422 |
*/ |
| 423 |
function dfrapi_impression_url( $product ) { |
| 424 |
|
| 425 |
$impression_url = ( isset( $product['impressionurl'] ) ) ? trim( $product['impressionurl'] ) : false; |
| 426 |
|
| 427 |
if ( ! $impression_url ) { |
| 428 |
return ''; |
| 429 |
} |
| 430 |
|
| 431 |
// Get all the user's selected networks. |
| 432 |
$networks = (array) get_option( 'dfrapi_networks' ); |
| 433 |
|
| 434 |
// Extract the affiliate ID from the $networks array. |
| 435 |
$affiliate_id = $networks['ids'][ $product['source_id'] ]['aid']; |
| 436 |
$affiliate_id = apply_filters( 'dfrapi_affiliate_id', $affiliate_id, $product, $networks ); |
| 437 |
$affiliate_id = trim( $affiliate_id ); |
| 438 |
|
| 439 |
// Affiliate ID is missing. Do action and return empty string. |
| 440 |
if ( $affiliate_id == '' ) { |
| 441 |
do_action( 'dfrapi_affiliate_id_is_missing_impression', $product ); |
| 442 |
|
| 443 |
return ''; |
| 444 |
} |
| 445 |
|
| 446 |
// Apply filters to URL before affiliate & tracking ID insertion. |
| 447 |
$impression_url = apply_filters( 'dfrapi_before_affiliate_id_insertion_impression', $impression_url, $product, $affiliate_id ); |
| 448 |
|
| 449 |
// Replace placeholders in URL. |
| 450 |
$placeholders = array( "@@@" ); |
| 451 |
$replacements = array( $affiliate_id ); |
| 452 |
$impression_url = str_replace( $placeholders, $replacements, $impression_url ); |
| 453 |
|
| 454 |
// Apply filters to URL after affiliate & tracking ID insertion. |
| 455 |
$impression_url = apply_filters( 'dfrapi_after_affiliate_id_insertion_impression', $impression_url, $product, $affiliate_id ); |
| 456 |
|
| 457 |
// Return URL |
| 458 |
return $impression_url; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Output an error message generated by the API. |
| 463 |
*/ |
| 464 |
function dfrapi_output_api_error( $data ) { |
| 465 |
$error = @$data['dfrapi_api_error']; |
| 466 |
$params = @$data['dfrapi_api_error']['params']; |
| 467 |
?> |
| 468 |
<div class="dfrapi_api_error"> |
| 469 |
<div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div> |
| 470 |
<div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?> |
| 471 |
</div> |
| 472 |
<div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?> |
| 473 |
</div> |
| 474 |
<div class="dfrapi_class"> |
| 475 |
<strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div> |
| 476 |
<?php if ( is_array( $params ) ) : ?> |
| 477 |
<div class="dfrps_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> |
| 478 |
<span><?php echo dfrapi_display_api_request( $params ); ?></span></div> |
| 479 |
<?php endif; ?> |
| 480 |
</div> |
| 481 |
<?php |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Convert a currency code to sign. USD => $ |
| 486 |
* |
| 487 |
* @https://github.com/pelle/bux/blob/master/src/bux/currencies.clj |
| 488 |
* |
| 489 |
* Currently supported currencies: |
| 490 |
* |
| 491 |
* AUD Australia $ |
| 492 |
* BRL Brazil R$ |
| 493 |
* CAD Canada $ |
| 494 |
* CHF Switzerland Fr |
| 495 |
* DKK Denmark kr |
| 496 |
* EUR Belgium € |
| 497 |
* EUR Finland € |
| 498 |
* EUR France € |
| 499 |
* EUR Germany € |
| 500 |
* EUR Ireland € |
| 501 |
* EUR Italy € |
| 502 |
* EUR Netherlands € |
| 503 |
* EUR Spain € |
| 504 |
* GBP United Kingdom £ |
| 505 |
* HUF Hungary Fr |
| 506 |
* INR India ₹ |
| 507 |
* MYR Malaysia RM |
| 508 |
* NOK Norway kr |
| 509 |
* NZD New Zealand $ |
| 510 |
* PHP Philippines ₱ |
| 511 |
* PLN Poland zł |
| 512 |
* RON Romania L |
| 513 |
* RUB Russia ₽ |
| 514 |
* SEK Sweden kr |
| 515 |
* TRY Turkey ₤ |
| 516 |
* USD United States $ |
| 517 |
* |
| 518 |
* @param string $code 3-character ISO 4217 currency code. |
| 519 |
* |
| 520 |
* @return mixed|string |
| 521 |
*/ |
| 522 |
function dfrapi_currency_code_to_sign( $code ) { |
| 523 |
|
| 524 |
return dfrapi_currency( $code )->get_currency_symbol(); |
| 525 |
|
| 526 |
$map = array( |
| 527 |
'AUD' => '$', |
| 528 |
'BRL' => 'R$', |
| 529 |
'CAD' => '$', |
| 530 |
'CHF' => 'Fr', |
| 531 |
'DKK' => 'kr', |
| 532 |
'EUR' => '€', |
| 533 |
'GBP' => '£', |
| 534 |
'HUF' => 'Ft', |
| 535 |
'INR' => '₹', |
| 536 |
'MYR' => 'RM', |
| 537 |
'NOK' => 'kr', |
| 538 |
'NZD' => '$', |
| 539 |
'PLN' => 'zł', |
| 540 |
'PHP' => '₱', |
| 541 |
'RON' => 'L', |
| 542 |
'RUB' => '₽', |
| 543 |
'SEK' => 'kr', |
| 544 |
'TRY' => '₤', |
| 545 |
'USD' => '$', |
| 546 |
'CZK' => 'Kč', |
| 547 |
); |
| 548 |
|
| 549 |
$map = apply_filters( 'dfrapi_currency_sign_mapper', $map ); |
| 550 |
|
| 551 |
if ( isset ( $map[ $code ] ) ) { |
| 552 |
return $map[ $code ]; |
| 553 |
} else { |
| 554 |
return $map['USD']; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* This displays the API request in PHP format. |
| 560 |
*/ |
| 561 |
function dfrapi_display_api_request( $params = array() ) { |
| 562 |
|
| 563 |
$html = ''; |
| 564 |
|
| 565 |
if ( empty( $params ) ) { |
| 566 |
return $html; |
| 567 |
} |
| 568 |
|
| 569 |
$html .= '$search = $api->searchRequest();<br />'; |
| 570 |
foreach ( $params as $k => $v ) { |
| 571 |
|
| 572 |
// Handle query. |
| 573 |
if ( $k == 'query' ) { |
| 574 |
foreach ( $v as $query ) { |
| 575 |
if ( substr( $query, 0, 9 ) !== 'source_id' || substr( $query, 0, 11 ) !== 'merchant_id' ) { |
| 576 |
$query = str_replace( ",", ", ", $query ); |
| 577 |
} |
| 578 |
$html .= '$search->addFilter( \'' . ( $query ) . '\' );<br />'; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
// Handle sort. |
| 583 |
if ( $k == 'sort' ) { |
| 584 |
foreach ( $v as $sort ) { |
| 585 |
$html .= '$search->addSort( \'' . stripslashes( $sort ) . '\' );<br />'; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
// Handle limit. |
| 590 |
if ( $k == 'limit' ) { |
| 591 |
$html .= '$search->setLimit( \'' . stripslashes( $v ) . '\' );<br />'; |
| 592 |
} |
| 593 |
|
| 594 |
// Handle merchant_limit. |
| 595 |
if ( $k == 'merchant_limit' ) { |
| 596 |
$html .= '$search->setMerchantLimit( \'' . stripslashes( absint( $v ) ) . '\' );<br />'; |
| 597 |
} |
| 598 |
|
| 599 |
// Handle Offset. |
| 600 |
if ( $k == 'offset' ) { |
| 601 |
$html .= '$search->setOffset( \'' . stripslashes( $v ) . '\' );<br />'; |
| 602 |
} |
| 603 |
|
| 604 |
// Handle Exclude duplicates. |
| 605 |
if ( $k == 'exclude_duplicates' ) { |
| 606 |
$html .= '$search->excludeDuplicates( \'' . $v . '\' );<br />'; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
$html .= '$products = $search->execute();'; |
| 611 |
|
| 612 |
return $html; |
| 613 |
|
| 614 |
} |
| 615 |
|
| 616 |
function dfrapi_get_query_param( $query, $param ) { |
| 617 |
if ( is_array( $query ) && ! empty( $query ) ) { |
| 618 |
foreach ( $query as $k => $v ) { |
| 619 |
if ( $v['field'] == $param ) { |
| 620 |
return array( |
| 621 |
'field' => @$v['field'], |
| 622 |
'operator' => @$v['operator'], |
| 623 |
'value' => @$v['value'], |
| 624 |
); |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
return false; |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
* Converts a value in cents into a value with proper |
| 634 |
* decimal placement. |
| 635 |
* |
| 636 |
* Example: 14999 => 149.99 |
| 637 |
*/ |
| 638 |
function dfrapi_int_to_price( $price ) { |
| 639 |
return number_format( ( $price / 100 ), 2 ); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Converts decimal or none decimal prices into values in cents. |
| 644 |
* |
| 645 |
* assert(dfrapi_price_to_int('123') ==12300); |
| 646 |
* assert(dfrapi_price_to_int('123.4') ==12340); |
| 647 |
* assert(dfrapi_price_to_int('1234.56') ==123456); |
| 648 |
* assert(dfrapi_price_to_int('123,4') ==12340); |
| 649 |
* assert(dfrapi_price_to_int('1234,56') ==123456); |
| 650 |
* assert(dfrapi_price_to_int('1,234,567') ==123456700); |
| 651 |
* assert(dfrapi_price_to_int('1,234,567.8') ==123456780); |
| 652 |
* assert(dfrapi_price_to_int('1,234,567.89') ==123456789); |
| 653 |
* assert(dfrapi_price_to_int('1.234.567') ==123456700); |
| 654 |
* assert(dfrapi_price_to_int('1.234.567,8') ==123456780); |
| 655 |
* assert(dfrapi_price_to_int('1.234.567,89') ==123456789); |
| 656 |
* assert(dfrapi_price_to_int('FOO 123 BAR') ==12300); |
| 657 |
*/ |
| 658 |
function dfrapi_price_to_int( $price ) { |
| 659 |
$d = $price; |
| 660 |
$d = preg_replace( '~^[^\d.,]+~', '', $d ); |
| 661 |
$d = preg_replace( '~[^\d.,]+$~', '', $d ); |
| 662 |
|
| 663 |
// 123 => 12300 |
| 664 |
if ( preg_match( '~^(\d+)$~', $d, $m ) ) { |
| 665 |
return intval( $m[1] . '00' ); |
| 666 |
} |
| 667 |
|
| 668 |
// 123.4 => 12340, 123,45 => 12345 |
| 669 |
if ( preg_match( '~^(\d+)[.,](\d{1,2})$~', $d, $m ) ) { |
| 670 |
return intval( $m[1] . substr( $m[2] . '0000', 0, 2 ) ); |
| 671 |
} |
| 672 |
|
| 673 |
// 1,234,567.89 => 123456789 |
| 674 |
if ( preg_match( '~^((?:\d{1,3})(?:,\d{3})*)(\.\d{1,2})?$~', $d, $m ) ) { |
| 675 |
$f = isset( $m[2] ) ? $m[2] : '.'; |
| 676 |
|
| 677 |
return intval( str_replace( ',', '', $m[1] ) . substr( $f . '0000', 1, 2 ) ); |
| 678 |
} |
| 679 |
|
| 680 |
// 1.234.567,89 => 123456789 |
| 681 |
if ( preg_match( '~^((?:\d{1,3})(?:\.\d{3})*)(,\d{1,2})?$~', $d, $m ) ) { |
| 682 |
$f = isset( $m[2] ) ? $m[2] : '.'; |
| 683 |
|
| 684 |
return intval( str_replace( '.', '', $m[1] ) . substr( $f . '0000', 1, 2 ) ); |
| 685 |
} |
| 686 |
|
| 687 |
return null; |
| 688 |
} |
| 689 |
|
| 690 |
function dfrapi_html_output_api_error( $data ) { |
| 691 |
$error = $data['dfrapi_api_error']; |
| 692 |
$params = @$data['dfrapi_api_error']['params']; |
| 693 |
?> |
| 694 |
<div class="dfrapi_api_error"> |
| 695 |
<div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div> |
| 696 |
<div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?> |
| 697 |
</div> |
| 698 |
<div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?> |
| 699 |
</div> |
| 700 |
<div class="dfrapi_class"> |
| 701 |
<strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div> |
| 702 |
<?php if ( is_array( $params ) ) : ?> |
| 703 |
<div class="dfrapi_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> |
| 704 |
<span><?php echo dfrapi_helper_display_api_request( $params ); ?></span></div> |
| 705 |
<?php endif; ?> |
| 706 |
</div> |
| 707 |
<?php |
| 708 |
} |
| 709 |
|
| 710 |
function dfrapi_get_total_products_in_db( $formatted = true, $default = 0 ) { |
| 711 |
|
| 712 |
$account = get_option( 'dfrapi_account' ); |
| 713 |
$product_count = $default; |
| 714 |
|
| 715 |
if ( $account ) { |
| 716 |
if ( isset( $account['product_count'] ) ) { |
| 717 |
$product_count = intval( $account['product_count'] ); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
if ( $formatted && is_int( $product_count ) ) { |
| 722 |
$product_count = number_format( $product_count ); |
| 723 |
} |
| 724 |
|
| 725 |
return $product_count; |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
* Imports an image from a URL into the WordPress Media Library. |
| 730 |
* |
| 731 |
* @param string $url Image URL. |
| 732 |
* @param array $args Optional. An array of options. See Datafeedr_Image_Importer::default_args() |
| 733 |
* |
| 734 |
* @return Datafeedr_Image_Importer |
| 735 |
* @since 1.0.71 |
| 736 |
* |
| 737 |
*/ |
| 738 |
function datafeedr_import_image( $url, $args = array() ) { |
| 739 |
return ( new Datafeedr_Image_Importer( $url, $args ) )->import(); |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Returns true if the $string starts with one of the $patterns. Otherwise returns false. |
| 744 |
* |
| 745 |
* @param string $string The haystack. |
| 746 |
* @param string|array $patterns The patterns to search for in the beginning of the $string. |
| 747 |
* |
| 748 |
* @return bool True if string starts with the pattern(s) else returns false. |
| 749 |
* @since 1.0.78 |
| 750 |
* |
| 751 |
*/ |
| 752 |
function dfrapi_string_starts_with( $string, $patterns ) { |
| 753 |
$patterns = ( is_string( $patterns ) ) ? array( $patterns ) : $patterns; |
| 754 |
foreach ( $patterns as $pattern ) { |
| 755 |
$length = mb_strlen( $pattern ); |
| 756 |
if ( mb_substr( $string, 0, $length ) === $pattern ) { |
| 757 |
return true; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
return false; |
| 762 |
} |
| 763 |
|
| 764 |
/** |
| 765 |
* Includes HelpScout Beacon if enabled and if on a Datafeedr-specific page. |
| 766 |
* |
| 767 |
* @since 1.0.84 |
| 768 |
*/ |
| 769 |
function dfrapi_include_helpscout_beacon() { |
| 770 |
|
| 771 |
$options = get_option( 'dfrapi_configuration', [] ); |
| 772 |
|
| 773 |
if ( isset( $options['hs_beacon'] ) && 'off' == $options['hs_beacon'] ) { |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
if ( ! dfrapi_is_datafeedr_admin_page() ) { |
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
include_once DFRAPI_PATH . 'js/helpscout-beacon.php'; |
| 782 |
|
| 783 |
return; |
| 784 |
} |
| 785 |
|
| 786 |
add_action( 'admin_footer', 'dfrapi_include_helpscout_beacon' ); |
| 787 |
|
| 788 |
/** |
| 789 |
* Returns true if we are viewing a Datafeedr-specific page in the WordPress Admin Area. |
| 790 |
* |
| 791 |
* @return bool |
| 792 |
* @since 1.0.84 |
| 793 |
* |
| 794 |
* @global $pagenow |
| 795 |
* |
| 796 |
*/ |
| 797 |
function dfrapi_is_datafeedr_admin_page() { |
| 798 |
|
| 799 |
/** |
| 800 |
* For post edit pages (ie. post.php?post=1&action=edit). |
| 801 |
*/ |
| 802 |
$post_types = [ |
| 803 |
'datafeedr-productset', |
| 804 |
]; |
| 805 |
|
| 806 |
/** |
| 807 |
* For $_GET params (ie. admin.php?page=dfrps_configuration). |
| 808 |
*/ |
| 809 |
$params = [ |
| 810 |
'page' => [ |
| 811 |
'dfrapi', |
| 812 |
'dfrapi_networks', |
| 813 |
'dfrapi_merchants', |
| 814 |
'dfrapi_tools', |
| 815 |
'dfrapi_export', |
| 816 |
'dfrapi_import', |
| 817 |
'dfrapi_account', |
| 818 |
'dfrcs_options', |
| 819 |
'dfrps_configuration', |
| 820 |
'dfrps_tools', |
| 821 |
'dfrpswc_options', |
| 822 |
], |
| 823 |
'post_type' => [ |
| 824 |
'datafeedr-productset' |
| 825 |
] |
| 826 |
]; |
| 827 |
|
| 828 |
foreach ( $params as $k => $v ) { |
| 829 |
if ( isset( $_GET[ $k ] ) && in_array( $_GET[ $k ], $v ) ) { |
| 830 |
return true; |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
global $pagenow; |
| 835 |
|
| 836 |
if ( 'post.php' === $pagenow && in_array( get_post_type(), $post_types ) ) { |
| 837 |
return true; |
| 838 |
} |
| 839 |
|
| 840 |
return false; |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* @param integer $network_id |
| 845 |
* @param string $id_type |
| 846 |
* |
| 847 |
* @return WP_Error|string |
| 848 |
*/ |
| 849 |
function dfrapi_get_affiliate_and_tracking_id( $network_id, $id_type = 'aid' ) { |
| 850 |
|
| 851 |
static $networks = null; |
| 852 |
|
| 853 |
$key = 'ids'; |
| 854 |
$type = ( 'tid' == $id_type ) ? 'tid' : 'aid'; |
| 855 |
|
| 856 |
if ( null === $networks ) { |
| 857 |
$networks = get_option( 'dfrapi_networks', [] ); |
| 858 |
} |
| 859 |
|
| 860 |
if ( empty( $networks ) ) { |
| 861 |
return new WP_Error( |
| 862 |
'dfrapi_get_affiliate_id_no_networks', |
| 863 |
__( 'No networks selected.', 'datafeedr-api' ) |
| 864 |
); |
| 865 |
} |
| 866 |
|
| 867 |
if ( ! isset( $networks[ $key ] ) ) { |
| 868 |
return new WP_Error( |
| 869 |
'dfrapi_get_affiliate_id_no_network_ids', |
| 870 |
__( 'No network IDs selected.', 'datafeedr-api' ) |
| 871 |
); |
| 872 |
} |
| 873 |
|
| 874 |
if ( ! isset( $networks[ $key ][ $network_id ] ) ) { |
| 875 |
return new WP_Error( |
| 876 |
'dfrapi_get_affiliate_id_no_network_ids', |
| 877 |
__( 'No data for network with ID of ' . intval( $network_id ), 'datafeedr-api' ) |
| 878 |
); |
| 879 |
} |
| 880 |
|
| 881 |
if ( ! isset( $networks[ $key ][ $network_id ][ $type ] ) || empty( $networks[ $key ][ $network_id ][ $type ] ) ) { |
| 882 |
return new WP_Error( |
| 883 |
'dfrapi_get_affiliate_id_empty_type', |
| 884 |
__( 'No affiliate or tracking ID entered for network with ID of ' . intval( $network_id ), 'datafeedr-api' ) |
| 885 |
); |
| 886 |
} |
| 887 |
|
| 888 |
return $networks[ $key ][ $network_id ][ $type ]; |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
* @param array $merchants |
| 893 |
* @param array $network |
| 894 |
* |
| 895 |
* @return array|WP_Error|null |
| 896 |
*/ |
| 897 |
function dfrapi_remove_unapproved_awin_merchants( $merchants, $network ) { |
| 898 |
|
| 899 |
// Return if not in Awin network. |
| 900 |
if ( 10006 != $network['group_id'] ) { |
| 901 |
return $merchants; |
| 902 |
} |
| 903 |
|
| 904 |
$affiliate_id = dfrapi_get_affiliate_and_tracking_id( $network['_id'], 'aid' ); |
| 905 |
|
| 906 |
if ( is_wp_error( $affiliate_id ) ) { |
| 907 |
return new WP_Error( |
| 908 |
'missing_awin_affiliate_id', |
| 909 |
'Please enter your Awin affiliate ID for ' . esc_html( $network['name'] ) . ' <a href="' . admin_url( 'admin.php?page=dfrapi_networks' ) . '#group_affiliatewindow" target="_blank">here</a>.' |
| 910 |
); |
| 911 |
} |
| 912 |
|
| 913 |
static $awin_access_token = null; |
| 914 |
|
| 915 |
if ( null === $awin_access_token ) { |
| 916 |
|
| 917 |
$config = get_option( 'dfrapi_configuration', [] ); |
| 918 |
|
| 919 |
$awin_access_token = ( isset( $config['awin_access_token'] ) && ! empty( $config['awin_access_token'] ) ) ? |
| 920 |
trim( $config['awin_access_token'] ) : |
| 921 |
new WP_Error( |
| 922 |
'awin_access_token_missing', |
| 923 |
'Please enter your Awin API Token <a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" target="_blank">here</a>.' |
| 924 |
); |
| 925 |
} |
| 926 |
|
| 927 |
if ( is_wp_error( $awin_access_token ) ) { |
| 928 |
return $awin_access_token; |
| 929 |
} |
| 930 |
|
| 931 |
$url = null; |
| 932 |
$approved_program_ids = null; |
| 933 |
|
| 934 |
$url = sprintf( |
| 935 |
'https://api.awin.com/publishers/%1$s/programmes?relationship=joined&accessToken=%2$s', |
| 936 |
$affiliate_id, $awin_access_token |
| 937 |
); |
| 938 |
|
| 939 |
$response = wp_remote_get( $url ); |
| 940 |
|
| 941 |
if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
| 942 |
if ( isset( $response['response']['code'] ) && '200' == $response['response']['code'] ) { |
| 943 |
if ( isset( $response['body'] ) ) { |
| 944 |
$programs = json_decode( $response['body'], true ); |
| 945 |
$approved_program_ids = wp_list_pluck( $programs, 'id' ); |
| 946 |
} |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
if ( null === $approved_program_ids ) { |
| 951 |
return new WP_Error( |
| 952 |
'unable_to_retrieve_approved_awin_program_ids', |
| 953 |
'Unable to get your list of joined ' . esc_html( $network['name'] ) . ' programs. Please ensure your Awin Access Token is correct <a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" target="_blank">here</a> and your affiliate ID is correct <a href="' . admin_url( 'admin.php?page=dfrapi_networks' ) . '#group_affiliatewindow" target="_blank">here</a>.' ); |
| 954 |
} |
| 955 |
|
| 956 |
foreach ( $merchants as $key => $merchant ) { |
| 957 |
|
| 958 |
$approved = false; |
| 959 |
$suids = isset( $merchant['suids'] ) ? explode( ',', $merchant['suids'] ) : []; |
| 960 |
|
| 961 |
foreach ( $suids as $suid ) { |
| 962 |
if ( in_array( $suid, $approved_program_ids ) ) { |
| 963 |
$approved = true; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
if ( ! $approved ) { |
| 968 |
unset( $merchants[ $key ] ); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
return $merchants; |
| 973 |
} |
| 974 |
|
| 975 |
add_filter( 'dfrapi_list_merchants', 'dfrapi_remove_unapproved_awin_merchants', 10, 2 ); |
| 976 |
|
| 977 |
/** |
| 978 |
* @param array $merchants |
| 979 |
* @param array $network |
| 980 |
* |
| 981 |
* @return array|WP_Error|null |
| 982 |
* @since 1.0.102 |
| 983 |
*/ |
| 984 |
function dfrapi_disable_affiliate_gateway_merchant_selection_when_sid_empty( $merchants, $network ) { |
| 985 |
|
| 986 |
// Return if not in The Affiliate Gateway network. |
| 987 |
if ( 10033 != $network['group_id'] ) { |
| 988 |
return $merchants; |
| 989 |
} |
| 990 |
|
| 991 |
$sid = dfrapi_get_affiliate_gateway_sid(); |
| 992 |
|
| 993 |
if ( is_wp_error( $sid ) ) { |
| 994 |
return $sid; |
| 995 |
} |
| 996 |
|
| 997 |
return $merchants; |
| 998 |
} |
| 999 |
|
| 1000 |
add_filter( 'dfrapi_list_merchants', 'dfrapi_disable_affiliate_gateway_merchant_selection_when_sid_empty', 10, 2 ); |
| 1001 |
|
| 1002 |
/** |
| 1003 |
* Get The Affiliate Gateway SID from this page WordPress Admin Area > Datafeedr API > Configuration |
| 1004 |
* |
| 1005 |
* @return string|WP_Error |
| 1006 |
* @since 1.0.102 |
| 1007 |
*/ |
| 1008 |
function dfrapi_get_affiliate_gateway_sid() { |
| 1009 |
|
| 1010 |
static $sid = null; |
| 1011 |
|
| 1012 |
if ( null === $sid ) { |
| 1013 |
|
| 1014 |
$config = get_option( 'dfrapi_configuration', [] ); |
| 1015 |
|
| 1016 |
$sid = ( isset( $config['affiliate_gateway_sid'] ) && ! empty( $config['affiliate_gateway_sid'] ) ) ? |
| 1017 |
trim( $config['affiliate_gateway_sid'] ) : |
| 1018 |
new WP_Error( |
| 1019 |
'missing_affiliate_gateway_sid', |
| 1020 |
'Please enter your The Affiliate Gateway SID <a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" target="_blank">here</a>.' |
| 1021 |
); |
| 1022 |
} |
| 1023 |
|
| 1024 |
return $sid; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/** |
| 1028 |
* Insert SID into The Affiliate Gateway affiliate links. |
| 1029 |
* |
| 1030 |
* @param string $url |
| 1031 |
* @param array $product |
| 1032 |
* @param string $tracking_id |
| 1033 |
* |
| 1034 |
* @return string |
| 1035 |
*/ |
| 1036 |
function dfrapi_insert_affiliate_gateway_sid_into_affiliate_link( $url, $product, $tracking_id ) { |
| 1037 |
|
| 1038 |
if ( $product['source'] != 'The Affiliate Gateway' ) { |
| 1039 |
return $url; |
| 1040 |
} |
| 1041 |
|
| 1042 |
$sid = dfrapi_get_affiliate_gateway_sid(); |
| 1043 |
|
| 1044 |
if ( is_wp_error( $sid ) ) { |
| 1045 |
return $url; |
| 1046 |
} |
| 1047 |
|
| 1048 |
$url = str_replace( '{SID}', $sid, $url ); |
| 1049 |
|
| 1050 |
return $url; |
| 1051 |
} |
| 1052 |
|
| 1053 |
add_filter( 'dfrapi_after_tracking_id_insertion', 'dfrapi_insert_affiliate_gateway_sid_into_affiliate_link', 20, 3 ); |
| 1054 |
|
| 1055 |
/** |
| 1056 |
* @param array $merchants |
| 1057 |
* @param array $network |
| 1058 |
* |
| 1059 |
* @return array|WP_Error|null |
| 1060 |
* @since 1.0.124 |
| 1061 |
*/ |
| 1062 |
function dfrapi_disable_belboon_merchant_selection_when_aid_empty( $merchants, $network ) { |
| 1063 |
|
| 1064 |
// Return if not in Belboon network. |
| 1065 |
if ( 10007 != $network['group_id'] ) { |
| 1066 |
return $merchants; |
| 1067 |
} |
| 1068 |
|
| 1069 |
$aid = dfrapi_get_belboon_adspace_id(); |
| 1070 |
|
| 1071 |
if ( is_wp_error( $aid ) ) { |
| 1072 |
return $aid; |
| 1073 |
} |
| 1074 |
|
| 1075 |
return $merchants; |
| 1076 |
} |
| 1077 |
|
| 1078 |
add_filter( 'dfrapi_list_merchants', 'dfrapi_disable_belboon_merchant_selection_when_aid_empty', 10, 2 ); |
| 1079 |
|
| 1080 |
/** |
| 1081 |
* Get Belboon Adspace ID from this page WordPress Admin Area > Datafeedr API > Configuration |
| 1082 |
* |
| 1083 |
* @return string|WP_Error |
| 1084 |
* @since 1.0.124 |
| 1085 |
*/ |
| 1086 |
function dfrapi_get_belboon_adspace_id() { |
| 1087 |
|
| 1088 |
static $aid = null; |
| 1089 |
|
| 1090 |
if ( null === $aid ) { |
| 1091 |
|
| 1092 |
$config = get_option( 'dfrapi_configuration', [] ); |
| 1093 |
|
| 1094 |
$aid = ( isset( $config['belboon_aid'] ) && ! empty( $config['belboon_aid'] ) ) ? |
| 1095 |
trim( $config['belboon_aid'] ) : |
| 1096 |
new WP_Error( |
| 1097 |
'missing_belboon_aid', |
| 1098 |
'Please enter your Belboon Adspace ID <a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" target="_blank">here</a>.' |
| 1099 |
); |
| 1100 |
} |
| 1101 |
|
| 1102 |
return $aid; |
| 1103 |
} |
| 1104 |
|
| 1105 |
/** |
| 1106 |
* Insert Adspace ID into Belboon affiliate links. |
| 1107 |
* |
| 1108 |
* @param string $url |
| 1109 |
* @param array $product |
| 1110 |
* @param string $affiliate_id |
| 1111 |
* |
| 1112 |
* @return string |
| 1113 |
* @since 1.0.124 |
| 1114 |
*/ |
| 1115 |
function dfrapi_insert_belboon_adspace_id_into_affiliate_link( $url, $product, $affiliate_id ) { |
| 1116 |
|
| 1117 |
if ( strpos( $product['source'], 'Belboon' ) === false ) { |
| 1118 |
return $url; |
| 1119 |
} |
| 1120 |
|
| 1121 |
$aid = dfrapi_get_belboon_adspace_id(); |
| 1122 |
|
| 1123 |
if ( is_wp_error( $aid ) ) { |
| 1124 |
return $url; |
| 1125 |
} |
| 1126 |
|
| 1127 |
$url = str_replace( '{AID}', $aid, $url ); |
| 1128 |
|
| 1129 |
return $url; |
| 1130 |
} |
| 1131 |
|
| 1132 |
add_filter( 'dfrapi_before_affiliate_id_insertion', 'dfrapi_insert_belboon_adspace_id_into_affiliate_link', 20, 3 ); |
| 1133 |
|
| 1134 |
/** |
| 1135 |
* @param string $url |
| 1136 |
* @param string $method |
| 1137 |
* @param array $args |
| 1138 |
* |
| 1139 |
* @return SimpleXMLElement|WP_Error |
| 1140 |
*/ |
| 1141 |
function dfrapi_get_xml_response( $url, $method = 'GET', array $args = [] ) { |
| 1142 |
|
| 1143 |
$response = $method === 'GET' ? wp_remote_get( $url, $args ) : wp_remote_post( $url, $args ); |
| 1144 |
|
| 1145 |
if ( is_wp_error( $response ) ) { |
| 1146 |
return $response; |
| 1147 |
} |
| 1148 |
|
| 1149 |
$code = wp_remote_retrieve_response_code( $response ); |
| 1150 |
$body = wp_remote_retrieve_body( $response ); |
| 1151 |
|
| 1152 |
if ( $code < 200 || $code >= 300 ) { |
| 1153 |
return new WP_Error( $code, strip_tags( $body ) ); |
| 1154 |
} |
| 1155 |
|
| 1156 |
if ( ! strlen( $body ) ) { |
| 1157 |
return new WP_Error( 'connection_error', esc_html__( 'Empty response', 'datafeedr' ) ); |
| 1158 |
} |
| 1159 |
|
| 1160 |
$xml = simplexml_load_string( $body, null, LIBXML_NOCDATA ); |
| 1161 |
|
| 1162 |
if ( $xml->getName() == 'error' ) { |
| 1163 |
return new WP_Error( $code, esc_html( strval( $xml->message ) ) ); |
| 1164 |
} |
| 1165 |
|
| 1166 |
return $xml; |
| 1167 |
} |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* Determine if a given string ends with a given substring. |
| 1171 |
* |
| 1172 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1173 |
* |
| 1174 |
* @param string $haystack |
| 1175 |
* @param string|string[] $needles |
| 1176 |
* |
| 1177 |
* @return bool |
| 1178 |
*/ |
| 1179 |
function dfrapi_ends_with( $haystack, $needles ) { |
| 1180 |
foreach ( (array) $needles as $needle ) { |
| 1181 |
if ( $needle !== '' && substr( $haystack, - strlen( $needle ) ) === (string) $needle ) { |
| 1182 |
return true; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
return false; |
| 1187 |
} |
| 1188 |
|
| 1189 |
/** |
| 1190 |
* Determine if a given string starts with a given substring. |
| 1191 |
* |
| 1192 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1193 |
* |
| 1194 |
* @param string $haystack |
| 1195 |
* @param string|string[] $needles |
| 1196 |
* |
| 1197 |
* @return bool |
| 1198 |
*/ |
| 1199 |
function dfrapi_starts_with( $haystack, $needles ) { |
| 1200 |
foreach ( (array) $needles as $needle ) { |
| 1201 |
if ( (string) $needle !== '' && strncmp( $haystack, $needle, strlen( $needle ) ) === 0 ) { |
| 1202 |
return true; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
return false; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/** |
| 1210 |
* Returns the portion of string specified by the start and length parameters. |
| 1211 |
* |
| 1212 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1213 |
* |
| 1214 |
* @param string $string |
| 1215 |
* @param int $start |
| 1216 |
* @param int|null $length |
| 1217 |
* |
| 1218 |
* @return string |
| 1219 |
*/ |
| 1220 |
function dfrapi_substr( $string, $start, $length = null ) { |
| 1221 |
return mb_substr( $string, $start, $length, 'UTF-8' ); |
| 1222 |
} |
| 1223 |
|
| 1224 |
/** |
| 1225 |
* Get the portion of a string before the first occurrence of a given value. |
| 1226 |
* |
| 1227 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1228 |
* |
| 1229 |
* @param string $subject |
| 1230 |
* @param string $search |
| 1231 |
* |
| 1232 |
* @return string |
| 1233 |
*/ |
| 1234 |
function dfrapi_str_before( $subject, $search ) { |
| 1235 |
return $search === '' ? $subject : explode( $search, $subject )[0]; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/** |
| 1239 |
* Get the portion of a string before the last occurrence of a given value. |
| 1240 |
* |
| 1241 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1242 |
* |
| 1243 |
* @param string $subject |
| 1244 |
* @param string $search |
| 1245 |
* |
| 1246 |
* @return string |
| 1247 |
*/ |
| 1248 |
function dfrapi_str_before_last( $subject, $search ) { |
| 1249 |
|
| 1250 |
if ( $search === '' ) { |
| 1251 |
return $subject; |
| 1252 |
} |
| 1253 |
|
| 1254 |
$pos = mb_strrpos( $subject, $search ); |
| 1255 |
|
| 1256 |
if ( $pos === false ) { |
| 1257 |
return $subject; |
| 1258 |
} |
| 1259 |
|
| 1260 |
return dfrapi_substr( $subject, 0, $pos ); |
| 1261 |
} |
| 1262 |
|
| 1263 |
/** |
| 1264 |
* Get the portion of a string between two given values. |
| 1265 |
* |
| 1266 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1267 |
* |
| 1268 |
* @param string $subject |
| 1269 |
* @param string $from |
| 1270 |
* @param string $to |
| 1271 |
* |
| 1272 |
* @return string |
| 1273 |
*/ |
| 1274 |
function dfrapi_str_between( $subject, $from, $to ) { |
| 1275 |
if ( $from === '' || $to === '' ) { |
| 1276 |
return $subject; |
| 1277 |
} |
| 1278 |
|
| 1279 |
return dfrapi_str_before_last( dfrapi_str_after( $subject, $from ), $to ); |
| 1280 |
} |
| 1281 |
|
| 1282 |
/** |
| 1283 |
* Return the remainder of a string after the first occurrence of a given value. |
| 1284 |
* |
| 1285 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1286 |
* |
| 1287 |
* @param string $subject |
| 1288 |
* @param string $search |
| 1289 |
* |
| 1290 |
* @return string |
| 1291 |
*/ |
| 1292 |
function dfrapi_str_after( $subject, $search ) { |
| 1293 |
return $search === '' ? $subject : array_reverse( explode( $search, $subject, 2 ) )[0]; |
| 1294 |
} |
| 1295 |
|
| 1296 |
/** |
| 1297 |
* Return the remainder of a string after the last occurrence of a given value. |
| 1298 |
* |
| 1299 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1300 |
* |
| 1301 |
* @param string $subject |
| 1302 |
* @param string $search |
| 1303 |
* |
| 1304 |
* @return string |
| 1305 |
*/ |
| 1306 |
function dfrapi_str_after_last( $subject, $search ) { |
| 1307 |
|
| 1308 |
if ( $search === '' ) { |
| 1309 |
return $subject; |
| 1310 |
} |
| 1311 |
|
| 1312 |
$position = strrpos( $subject, (string) $search ); |
| 1313 |
|
| 1314 |
if ( $position === false ) { |
| 1315 |
return $subject; |
| 1316 |
} |
| 1317 |
|
| 1318 |
return substr( $subject, $position + strlen( $search ) ); |
| 1319 |
} |
| 1320 |
|
| 1321 |
/** |
| 1322 |
* Determine if a given string contains a given substring. |
| 1323 |
* |
| 1324 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1325 |
* |
| 1326 |
* @param string $haystack |
| 1327 |
* @param string|string[] $needles |
| 1328 |
* |
| 1329 |
* @return bool |
| 1330 |
*/ |
| 1331 |
function dfrapi_str_contains( $haystack, $needles ) { |
| 1332 |
foreach ( (array) $needles as $needle ) { |
| 1333 |
if ( $needle !== '' && mb_strpos( $haystack, $needle ) !== false ) { |
| 1334 |
return true; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
return false; |
| 1339 |
} |
| 1340 |
|
| 1341 |
/** |
| 1342 |
* Determine if a given string contains all array values. |
| 1343 |
* |
| 1344 |
* @link https://github.com/illuminate/support/blob/7.x/Str.php |
| 1345 |
* |
| 1346 |
* @param string $haystack |
| 1347 |
* @param string[] $needles |
| 1348 |
* |
| 1349 |
* @return bool |
| 1350 |
*/ |
| 1351 |
function dfrapi_str_contains_all( $haystack, array $needles ) { |
| 1352 |
foreach ( $needles as $needle ) { |
| 1353 |
if ( ! dfrapi_str_contains( $haystack, $needle ) ) { |
| 1354 |
return false; |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
return true; |
| 1359 |
} |
| 1360 |
|
| 1361 |
/** |
| 1362 |
* Returns an integer value of string by: |
| 1363 |
* - removing all non-numeric characters |
| 1364 |
* - retaining any negative sign |
| 1365 |
* - running through intval to clean up any other weirdness. |
| 1366 |
* |
| 1367 |
* This function is pretty forgiving. |
| 1368 |
* |
| 1369 |
* Examples: |
| 1370 |
* |
| 1371 |
* '123.45' => 12345 |
| 1372 |
* '-123.45' => -12345 |
| 1373 |
* 'foo' => 0 |
| 1374 |
* '-foo' => 0 |
| 1375 |
* '-123-56' => -123 |
| 1376 |
* '123-45' => 123 |
| 1377 |
* '-123,56' => -12356 |
| 1378 |
* '$-1.111,45' => -111145 |
| 1379 |
* '00012.34' => 1234 |
| 1380 |
* |
| 1381 |
* @param string|int|numeric $int |
| 1382 |
* |
| 1383 |
* @return int |
| 1384 |
*/ |
| 1385 |
function dfrapi_intify( $int ) { |
| 1386 |
return intval( preg_replace( '/[^0-9-]/', '', $int ) ); |
| 1387 |
} |
| 1388 |
|
| 1389 |
/** |
| 1390 |
* Returns an instance of the Dfrapi_Price class. |
| 1391 |
* |
| 1392 |
* @param mixed $value The value to use as the price. |
| 1393 |
* @param string $currency_code 3-character ISO 4217 currency code. |
| 1394 |
* @param mixed $context Optional. |
| 1395 |
* |
| 1396 |
* @return Dfrapi_Price |
| 1397 |
*/ |
| 1398 |
function dfrapi_price( $value, $currency_code, $context = null ) { |
| 1399 |
return new Dfrapi_Price( $value, dfrapi_currency( $currency_code, $context ), $context ); |
| 1400 |
} |
| 1401 |
|
| 1402 |
/** |
| 1403 |
* Returns an instance of the Dfrapi_Currency class. |
| 1404 |
* |
| 1405 |
* @param string $currency_code 3-character ISO 4217 currency code. |
| 1406 |
* @param mixed $context Optional. |
| 1407 |
* |
| 1408 |
* @return Dfrapi_Currency |
| 1409 |
*/ |
| 1410 |
function dfrapi_currency( $currency_code, $context = null ) { |
| 1411 |
return new Dfrapi_Currency( $currency_code, $context ); |
| 1412 |
} |
| 1413 |
|
| 1414 |
/** |
| 1415 |
* Returns the fully formatted price. |
| 1416 |
* |
| 1417 |
* @param mixed $value The value to use as the price. |
| 1418 |
* @param string $currency_code 3-character ISO 4217 currency code. |
| 1419 |
* @param mixed $context Optional. |
| 1420 |
* |
| 1421 |
* @return string |
| 1422 |
*/ |
| 1423 |
function dfrapi_get_price( $value, $currency_code, $context = null ) { |
| 1424 |
return dfrapi_price( $value, $currency_code, $context )->get_price(); |
| 1425 |
} |
| 1426 |
|