PluginProbe
Datafeedr API / 1.0.29
Datafeedr API v1.0.29
1.4.3 1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 All 182 releases
datafeedr-api / functions / functions.php

functions.php in Datafeedr API 1.0.29, at functions/functions.php

472 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return $percentage;
12 }
13 return false;
14 }
15
16 add_action( 'init', 'dfrapi_email_user_about_usage' );
17 function dfrapi_email_user_about_usage() {
18
19 $percentage = dfrapi_get_api_usage_percentage();
20 $status = get_option( 'dfrapi_account', array() );
21
22 $request_count = ( isset( $status['request_count'] ) ) ? number_format( $status['request_count'] ) : 0;
23 $remaining_requests = ( isset( $status['max_requests'] ) ) ? number_format( $status['max_requests'] - $request_count ) : 0;
24
25 $reset_date = '';
26 if ( isset( $status['bill_day'] ) ) {
27 $today = date('j');
28 $num_days = date('t');
29 if ( $status['bill_day'] > $num_days ) {
30 $bill_day = $num_days;
31 } else {
32 $bill_day = $status['bill_day'];
33 }
34 if ( $bill_day == 0 ) {
35 $reset_date .= '<em>' . __( 'Never', DFRAPI_DOMAIN ) . '</em>';
36 } elseif ( $today >= $bill_day ) {
37 $reset_date .= date('F', strtotime('+1 month')) . ' ' . $bill_day . ', ' . date('Y', strtotime('+1 month'));
38 } else {
39 $reset_date .= date('F') . ' ' . $bill_day . ', ' . date('Y');
40 }
41 }
42
43 $default = array(
44 '90_percent' => '',
45 '100_percent' => ''
46 );
47
48 // Don't do anything if less than 90%.
49 if ( $percentage < 90 ) {
50 update_option( 'dfrapi_usage_notification_tracker', $default );
51 return;
52 }
53
54 $tracker = get_option( 'dfrapi_usage_notification_tracker', $default );
55
56 $params = array();
57 $params['to'] = get_bloginfo( 'admin_email' );
58 $params['message'] = "<p>" . __( "This is an automated message generated by: ", DFRAPI_DOMAIN ) . get_bloginfo( 'wpurl' ) . "</p>";
59
60 if ( $percentage >= 100 && empty( $tracker['100_percent'] ) ) {
61
62 $params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Critical)', DFRAPI_DOMAIN );
63
64 $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>";
65 $params['message'] .= "<p><strong>" . __( "What to do next?", DFRAPI_DOMAIN ) . "</strong></p>";
66 $params['message'] .= "<p>" . __( "We strongly recommend that you upgrade to prevent your product information from becoming outdated.", DFRAPI_DOMAIN ) . "</p>";
67 $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>";
68 $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>";
69 $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>";
70 $params['message'] .= "<p>" . __( "If you have any questions about your account, please ", DFRAPI_DOMAIN );
71 $params['message'] .= "<a href=\"" . DFRAPI_EMAIL_US_URL . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\">" . __( "contact us", DFRAPI_DOMAIN ) . "</a>.</p>";
72 $params['message'] .= "<p>" . __( "Thanks,<br />Eric &amp; Stefan<br />The Datafeedr Team", DFRAPI_DOMAIN ) . "</p>";
73 $params['message'] .= "<p>";
74 $params['message'] .= "<a href=\"" . admin_url( 'admin.php?page=dfrapi_account' ) . "\">" . __( "Account Information", DFRAPI_DOMAIN ) . "</a> | ";
75 $params['message'] .= "<a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade100percentnotice\">" . __( "Upgrade Account", DFRAPI_DOMAIN ) . "</a>";
76 $params['message'] .= "</p>";
77
78 $tracker['100_percent'] = 1;
79 update_option( 'dfrapi_usage_notification_tracker', $tracker );
80
81 add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' );
82 wp_mail( $params['to'], $params['subject'], $params['message'] );
83 remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' );
84
85
86 } elseif ( $percentage >= 90 && $percentage < 100 && empty( $tracker['90_percent'] ) ) {
87
88 $params['subject'] = get_bloginfo( 'name' ) . __( ': Datafeedr API Usage (Warning)', DFRAPI_DOMAIN );
89 $params['message'] .= "<p>" . __( "You have used <strong>90%</strong> of your allocated Datafeedr API requests for this period.", DFRAPI_DOMAIN ) . "</p>";
90 $params['message'] .= "<p><strong>" . __( "API Usage", DFRAPI_DOMAIN ) . "</strong></p>";
91 $params['message'] .= "<ul>";
92 $params['message'] .= "<li>" . __( "API requests used: ", DFRAPI_DOMAIN ) . $request_count . "</li>";
93 $params['message'] .= "<li>" . __( "API requests remaining: ", DFRAPI_DOMAIN ) . $remaining_requests . "</li>";
94 $params['message'] .= "<li>" . __( "API requests will reset on: ", DFRAPI_DOMAIN ) . $reset_date . "</li>";
95 $params['message'] .= "</ul>";
96 $params['message'] .= "<p><strong>" . __( "What to do next?", DFRAPI_DOMAIN ) . "</strong></p>";
97 $params['message'] .= "<p>" . __( "We recommend that you upgrade to prevent your product information from becoming outdated.", DFRAPI_DOMAIN ) . "</p>";
98 $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>";
99 $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>";
100 $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>";
101 $params['message'] .= "<p>" . __( "If you have any questions about your account, please ", DFRAPI_DOMAIN );
102 $params['message'] .= "<a href=\"" . DFRAPI_EMAIL_US_URL . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\">" . __( "contact us", DFRAPI_DOMAIN ) . "</a>.</p>";
103 $params['message'] .= "<p>" . __( "Thanks,<br />Eric &amp; Stefan<br />The Datafeedr Team", DFRAPI_DOMAIN ) . "</p>";
104 $params['message'] .= "<p>";
105 $params['message'] .= "<a href=\"" . admin_url( 'admin.php?page=dfrapi_account' ) . "\">" . __( "Account Information", DFRAPI_DOMAIN ) . "</a> | ";
106 $params['message'] .= "<a href=\"" . dfrapi_user_pages( 'change' ) . "?utm_source=email&utm_medium=link&utm_campaign=upgrade90percentnotice\">" . __( "Upgrade Account", DFRAPI_DOMAIN ) . "</a>";
107 $params['message'] .= "</p>";
108
109 $tracker['90_percent'] = 1;
110 update_option( 'dfrapi_usage_notification_tracker', $tracker );
111
112 add_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' );
113 wp_mail( $params['to'], $params['subject'], $params['message'] );
114 remove_filter( 'wp_mail_content_type', 'dfrapi_set_html_content_type' );
115
116 }
117
118 return;
119 }
120
121 function dfrapi_set_html_content_type() {
122 return 'text/html';
123 }
124
125 /**
126 * Modify affiliate ID if product is a Zanox product.
127 * Replaces $affiliate_id with "zmid".
128 */
129 add_filter( 'dfrapi_affiliate_id', 'dfrapi_get_zanox_zmid', 10, 3 );
130 function dfrapi_get_zanox_zmid( $affiliate_id, $product, $networks ) {
131 if ( isset( $product['source'] ) && preg_match( "/\bZanox\b/", $product['source'] ) ) {
132 $zanox = dfrapi_api_get_zanox_zmid( $product['merchant_id'], $affiliate_id );
133 $affiliate_id = ( !isset( $zanox[0]['zmid']) ) ? '___MISSING___' : $zanox[0]['zmid'];
134 }
135 return $affiliate_id;
136 }
137
138 function dfrapi_get_zanox_keys() {
139
140 $configuration = (array) get_option( 'dfrapi_configuration' );
141
142 $zanox_connection_key = false;
143 $zanox_secret_key = false;
144
145 if ( isset( $configuration['zanox_connection_key'] ) && ( $configuration['zanox_connection_key'] != '' ) ) {
146 $zanox_connection_key = $configuration['zanox_connection_key'];
147 }
148
149 if ( isset( $configuration['zanox_secret_key'] ) && ( $configuration['zanox_secret_key'] != '' ) ) {
150 $zanox_secret_key = $configuration['zanox_secret_key'];
151 }
152
153 if ( $zanox_connection_key && $zanox_secret_key ) {
154 return array(
155 'connection_key'=> $zanox_connection_key,
156 'secret_key' => $zanox_secret_key,
157 );
158 }
159
160 return false;
161 }
162
163 /**
164 * Returns a link to a user page on v4.datafeedr.com.
165 */
166 function dfrapi_user_pages( $page ) {
167
168 $pages = array(
169 'edit' => 'edit',
170 'summary' => 'subscription',
171 'invoices' => 'subscription/invoices',
172 'billing' => 'subscription/billing',
173 'change' => 'subscription/change',
174 'cancel' => 'subscription/cancel',
175 'signup' => 'subscription/signup',
176 );
177
178 $account = get_option( 'dfrapi_account', array() );
179
180 if ( empty( $account ) ) { return false; }
181
182 return DFRAPI_HOME_URL . '/user/' . $account['user_id'] . '/' . $pages[$page];
183 }
184
185 /**
186 * Adds option name to transient whitelist. This is so we know
187 * all transient options that can be deleted when deleting the
188 * API cache on Tools page.
189 */
190 function dfrapi_update_transient_whitelist( $option_name ) {
191 $whitelist = get_option( 'dfrapi_transient_whitelist', array() );
192 $whitelist[] = $option_name;
193 update_option( 'dfrapi_transient_whitelist', array_unique( $whitelist ) );
194 }
195
196 /**
197 * Add affiliate ID and tracking ID to an affiliate link.
198 *
199 * @param $product - An array of a single's product's information.
200 */
201 function dfrapi_url( $product ) {
202
203 // Get all the user's selected networks.
204 $networks = (array) get_option( 'dfrapi_networks' );
205
206 // Extract the affiliate ID from the $networks array.
207 $affiliate_id = $networks['ids'][$product['source_id']]['aid'];
208 $affiliate_id = apply_filters( 'dfrapi_affiliate_id', $affiliate_id, $product, $networks );
209 $affiliate_id = trim( $affiliate_id );
210
211 // Extract the Tracking ID from the $networks array.
212 $tracking_id = ( isset( $networks['ids'][$product['source_id']]['tid'] ) ) ? $networks['ids'][$product['source_id']]['tid'] : '';
213 $tracking_id = apply_filters( 'dfrapi_tracking_id', $tracking_id, $product, $networks );
214 $tracking_id = trim( $tracking_id );
215
216 // Affiliate ID is missing. Do action and return empty string.
217 if ( $affiliate_id == '' ) {
218 do_action( 'dfrapi_affiliate_id_is_missing', $product );
219 return '';
220 }
221
222 // Determine which URL field to get: 'url' OR 'ref_url'. Return 'url' if $tracking_id is empty, otherwise, use 'ref_url'.
223 $url = ( $tracking_id == '' ) ? $product['url'] : $product['ref_url'];
224
225 // Apply filters to URL before affiliate & tracking ID insertion.
226 $url = apply_filters( 'dfrapi_before_affiliate_id_insertion', $url, $product, $affiliate_id );
227 $url = apply_filters( 'dfrapi_before_tracking_id_insertion', $url, $product, $tracking_id );
228
229 // Replace placeholders in URL.
230 $placeholders = array( "@@@", "###" );
231 $replacements = array( $affiliate_id, $tracking_id );
232 $url = str_replace( $placeholders, $replacements, $url );
233
234 // Apply filters to URL after affiliate & tracking ID insertion.
235 $url = apply_filters( 'dfrapi_after_affiliate_id_insertion', $url, $product, $affiliate_id );
236 $url = apply_filters( 'dfrapi_after_tracking_id_insertion', $url, $product, $tracking_id );
237
238 // Return URL
239 return $url;
240 }
241
242 /**
243 * Output an error message generated by the API.
244 */
245 function dfrapi_output_api_error( $data ) {
246 $error = @$data['dfrapi_api_error'];
247 $params = @$data['dfrapi_api_error']['params'];
248 ?>
249 <div class="dfrapi_api_error">
250 <div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div>
251 <div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?></div>
252 <div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?></div>
253 <div class="dfrapi_class"><strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div>
254 <?php if ( is_array( $params ) ) : ?>
255 <div class="dfrps_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> <span><?php echo dfrapi_display_api_request( $params ); ?></span></div>
256 <?php endif; ?>
257 </div>
258 <?php
259 }
260
261 /**
262 * Convert a currency code to sign. USD => $
263 *
264 * @https://github.com/pelle/bux/blob/master/src/bux/currencies.clj
265 *
266 * Currently supported currencies:
267 *
268 * AUD Australia &#36;
269 * BRL Brazil R$
270 * CAD Canada &#36;
271 * CHF Switzerland Fr
272 * DKK Denmark kr
273 * EUR Belgium &euro;
274 * EUR Finland &euro;
275 * EUR France &euro;
276 * EUR Germany &euro;
277 * EUR Ireland &euro;
278 * EUR Italy &euro;
279 * EUR Netherlands &euro;
280 * EUR Spain &euro;
281 * GBP United Kingdom &pound;
282 * INR India &#8377;
283 * NOK Norway kr
284 * NZD New Zealand &#36;
285 * PLN Poland zł
286 * SEK Sweden kr
287 * TRY Turkey &#8356;
288 * USD United States &#36;
289 *
290 */
291 function dfrapi_currency_code_to_sign( $code ) {
292
293 $map = array(
294 'AUD' => '&#36;',
295 'BRL' => 'R$',
296 'CAD' => '&#36;',
297 'CHF' => 'Fr',
298 'DKK' => 'kr',
299 'EUR' => '&euro;',
300 'GBP' => '&pound;',
301 'INR' => '&#8377;',
302 'NOK' => 'kr',
303 'NZD' => '&#36;',
304 'PLN' => 'zł',
305 'SEK' => 'kr',
306 'TRY' => '&#8356;',
307 'USD' => '&#36;',
308 );
309
310 $map = apply_filters( 'dfrapi_currency_sign_mapper', $map );
311
312 if ( isset ( $map[$code] ) ) {
313 return $map[$code];
314 } else {
315 return $map['USD'];
316 }
317 }
318
319 /**
320 * This displays the API request in PHP format.
321 */
322 function dfrapi_display_api_request( $params=array() ) {
323
324 $html = '';
325
326 if ( empty( $params ) ) { return $html; }
327
328 $html .= '$search = $api->searchRequest();<br />';
329 foreach ( $params as $k => $v ) {
330
331 // Handle query.
332 if ( $k == 'query' ) {
333 foreach ( $v as $query ) {
334 if ( substr( $query, 0, 9 ) !== 'source_id' || substr( $query, 0, 11 ) !== 'merchant_id' ) {
335 $query = str_replace( ",", ", ", $query );
336 }
337 $html .= '$search->addFilter( \''.( $query ).'\' );<br />';
338 }
339 }
340
341 // Handle sort.
342 if ( $k == 'sort' ) {
343 foreach ( $v as $sort ) {
344 $html .= '$search->addSort( \''.stripslashes( $sort ).'\' );<br />';
345 }
346 }
347
348 // Handle limit.
349 if ( $k == 'limit' ) {
350 $html .= '$search->setLimit( \''.stripslashes( $v ).'\' );<br />';
351 }
352
353 // Handle Offset.
354 if ( $k == 'offset' ) {
355 $html .= '$search->setOffset( \''.stripslashes( $v ).'\' );<br />';
356 }
357
358 // Handle Exclude duplicates.
359 if ( $k == 'exclude_duplicates' ) {
360 $html .= '$search->excludeDuplicates( \''. $v . '\' );<br />';
361 }
362 }
363
364 $html .= '$products = $search->execute();';
365 return $html;
366
367 }
368
369 function dfrapi_get_query_param( $query, $param ) {
370 if ( is_array( $query ) && !empty( $query ) ) {
371 foreach( $query as $k => $v ) {
372 if ( $v['field'] == $param ) {
373 return array(
374 'field' => @$v['field'],
375 'operator' => @$v['operator'],
376 'value' => @$v['value'],
377 );
378 }
379 }
380 }
381 return false;
382 }
383
384 /**
385 * Converts a value in cents into a value with proper
386 * decimal placement.
387 *
388 * Example: 14999 => 149.99
389 */
390 function dfrapi_int_to_price( $price ) {
391 return number_format( ( $price/100 ), 2 );
392 }
393
394 /**
395 * Converts decimal or none decimal prices into values in cents.
396 *
397 * assert(dfrapi_price_to_int('123') ==12300);
398 * assert(dfrapi_price_to_int('123.4') ==12340);
399 * assert(dfrapi_price_to_int('1234.56') ==123456);
400 * assert(dfrapi_price_to_int('123,4') ==12340);
401 * assert(dfrapi_price_to_int('1234,56') ==123456);
402 * assert(dfrapi_price_to_int('1,234,567') ==123456700);
403 * assert(dfrapi_price_to_int('1,234,567.8') ==123456780);
404 * assert(dfrapi_price_to_int('1,234,567.89') ==123456789);
405 * assert(dfrapi_price_to_int('1.234.567') ==123456700);
406 * assert(dfrapi_price_to_int('1.234.567,8') ==123456780);
407 * assert(dfrapi_price_to_int('1.234.567,89') ==123456789);
408 * assert(dfrapi_price_to_int('FOO 123 BAR') ==12300);
409 */
410 function dfrapi_price_to_int( $price ) {
411 $d = $price;
412 $d = preg_replace('~^[^\d.,]+~', '', $d);
413 $d = preg_replace('~[^\d.,]+$~', '', $d);
414
415 // 123 => 12300
416 if(preg_match('~^(\d+)$~', $d, $m))
417 return intval($m[1] . '00');
418
419 // 123.4 => 12340, 123,45 => 12345
420 if(preg_match('~^(\d+)[.,](\d{1,2})$~', $d, $m))
421 return intval($m[1] . substr($m[2] . '0000', 0, 2));
422
423 // 1,234,567.89 => 123456789
424 if(preg_match('~^((?:\d{1,3})(?:,\d{3})*)(\.\d{1,2})?$~', $d, $m)) {
425 $f = isset($m[2]) ? $m[2] : '.';
426 return intval(str_replace(',', '', $m[1]) . substr($f . '0000', 1, 2));
427 }
428
429 // 1.234.567,89 => 123456789
430 if(preg_match('~^((?:\d{1,3})(?:\.\d{3})*)(,\d{1,2})?$~', $d, $m)) {
431 $f = isset($m[2]) ? $m[2] : '.';
432 return intval(str_replace('.', '', $m[1]) . substr($f . '0000', 1, 2));
433 }
434
435 return NULL;
436 }
437
438 function dfrapi_html_output_api_error( $data ) {
439 $error = $data['dfrapi_api_error'];
440 $params = @$data['dfrapi_api_error']['params'];
441 ?>
442 <div class="dfrapi_api_error">
443 <div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div>
444 <div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?></div>
445 <div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?></div>
446 <div class="dfrapi_class"><strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div>
447 <?php if ( is_array( $params ) ) : ?>
448 <div class="dfrapi_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> <span><?php echo dfrapi_helper_display_api_request( $params ); ?></span></div>
449 <?php endif; ?>
450 </div>
451 <?php
452 }
453
454 function dfrapi_get_total_products_in_db( $formatted=TRUE, $default=0 ) {
455
456 $account = get_option( 'dfrapi_account' );
457 $product_count = $default;
458
459 if ( $account ) {
460 if ( isset( $account['product_count'] ) ) {
461 $product_count = intval( $account['product_count'] );
462 }
463 }
464
465 if ( $formatted && is_int( $product_count ) ) {
466 $product_count = number_format( $product_count );
467 }
468
469 return $product_count;
470 }
471
472