PluginProbe
Datafeedr API / 1.0.17
Datafeedr API v1.0.17
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.17, at functions/functions.php

441 lines 17.0 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 function dfrapi_currency_code_to_sign( $code ) {
265 $map = array(
266 'USD' => '&#36;',
267 'GBP' => '&pound;',
268 'EUR' => '&euro;',
269 'CAD' => '&#36;',
270 'AUD' => '&#36;',
271 'DKK' => 'kr',
272 'SEK' => 'kr',
273 'NOK' => 'kr',
274 'CHF' => 'CHF',
275 'NZD' => '&#36;',
276 'TRY' => '&#8356;',
277 );
278
279 $map = apply_filters( 'dfrapi_currency_sign_mapper', $map );
280
281 if ( isset ( $map[$code] ) ) {
282 return $map[$code];
283 } else {
284 return $map['USD'];
285 }
286 }
287
288 /**
289 * This displays the API request in PHP format.
290 */
291 function dfrapi_display_api_request( $params=array() ) {
292
293 $html = '';
294
295 if ( empty( $params ) ) { return $html; }
296
297 $html .= '$search = $api->searchRequest();<br />';
298 foreach ( $params as $k => $v ) {
299
300 // Handle query.
301 if ( $k == 'query' ) {
302 foreach ( $v as $query ) {
303 if ( substr( $query, 0, 9 ) !== 'source_id' || substr( $query, 0, 11 ) !== 'merchant_id' ) {
304 $query = str_replace( ",", ", ", $query );
305 }
306 $html .= '$search->addFilter( \''.( $query ).'\' );<br />';
307 }
308 }
309
310 // Handle sort.
311 if ( $k == 'sort' ) {
312 foreach ( $v as $sort ) {
313 $html .= '$search->addSort( \''.stripslashes( $sort ).'\' );<br />';
314 }
315 }
316
317 // Handle limit.
318 if ( $k == 'limit' ) {
319 $html .= '$search->setLimit( \''.stripslashes( $v ).'\' );<br />';
320 }
321
322 // Handle Offset.
323 if ( $k == 'offset' ) {
324 $html .= '$search->setOffset( \''.stripslashes( $v ).'\' );<br />';
325 }
326
327 // Handle Exclude duplicates.
328 if ( $k == 'exclude_duplicates' ) {
329 $html .= '$search->excludeDuplicates( \''. $v . '\' );<br />';
330 }
331 }
332
333 $html .= '$products = $search->execute();';
334 return $html;
335
336 }
337
338 function dfrapi_get_query_param( $query, $param ) {
339 if ( is_array( $query ) && !empty( $query ) ) {
340 foreach( $query as $k => $v ) {
341 if ( $v['field'] == $param ) {
342 return array(
343 'field' => @$v['field'],
344 'operator' => @$v['operator'],
345 'value' => @$v['value'],
346 );
347 }
348 }
349 }
350 return false;
351 }
352
353 /**
354 * Converts a value in cents into a value with proper
355 * decimal placement.
356 *
357 * Example: 14999 => 149.99
358 */
359 function dfrapi_int_to_price( $price ) {
360 return number_format( ( $price/100 ), 2 );
361 }
362
363 /**
364 * Converts decimal or none decimal prices into values in cents.
365 *
366 * assert(dfrapi_price_to_int('123') ==12300);
367 * assert(dfrapi_price_to_int('123.4') ==12340);
368 * assert(dfrapi_price_to_int('1234.56') ==123456);
369 * assert(dfrapi_price_to_int('123,4') ==12340);
370 * assert(dfrapi_price_to_int('1234,56') ==123456);
371 * assert(dfrapi_price_to_int('1,234,567') ==123456700);
372 * assert(dfrapi_price_to_int('1,234,567.8') ==123456780);
373 * assert(dfrapi_price_to_int('1,234,567.89') ==123456789);
374 * assert(dfrapi_price_to_int('1.234.567') ==123456700);
375 * assert(dfrapi_price_to_int('1.234.567,8') ==123456780);
376 * assert(dfrapi_price_to_int('1.234.567,89') ==123456789);
377 * assert(dfrapi_price_to_int('FOO 123 BAR') ==12300);
378 */
379 function dfrapi_price_to_int( $price ) {
380 $d = $price;
381 $d = preg_replace('~^[^\d.,]+~', '', $d);
382 $d = preg_replace('~[^\d.,]+$~', '', $d);
383
384 // 123 => 12300
385 if(preg_match('~^(\d+)$~', $d, $m))
386 return intval($m[1] . '00');
387
388 // 123.4 => 12340, 123,45 => 12345
389 if(preg_match('~^(\d+)[.,](\d{1,2})$~', $d, $m))
390 return intval($m[1] . substr($m[2] . '0000', 0, 2));
391
392 // 1,234,567.89 => 123456789
393 if(preg_match('~^((?:\d{1,3})(?:,\d{3})*)(\.\d{1,2})?$~', $d, $m)) {
394 $f = isset($m[2]) ? $m[2] : '.';
395 return intval(str_replace(',', '', $m[1]) . substr($f . '0000', 1, 2));
396 }
397
398 // 1.234.567,89 => 123456789
399 if(preg_match('~^((?:\d{1,3})(?:\.\d{3})*)(,\d{1,2})?$~', $d, $m)) {
400 $f = isset($m[2]) ? $m[2] : '.';
401 return intval(str_replace('.', '', $m[1]) . substr($f . '0000', 1, 2));
402 }
403
404 return NULL;
405 }
406
407 function dfrapi_html_output_api_error( $data ) {
408 $error = $data['dfrapi_api_error'];
409 $params = @$data['dfrapi_api_error']['params'];
410 ?>
411 <div class="dfrapi_api_error">
412 <div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div>
413 <div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?></div>
414 <div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?></div>
415 <div class="dfrapi_class"><strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div>
416 <?php if ( is_array( $params ) ) : ?>
417 <div class="dfrapi_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> <span><?php echo dfrapi_helper_display_api_request( $params ); ?></span></div>
418 <?php endif; ?>
419 </div>
420 <?php
421 }
422
423 function dfrapi_get_total_products_in_db( $formatted=TRUE, $default=0 ) {
424
425 $account = get_option( 'dfrapi_account' );
426 $product_count = $default;
427
428 if ( $account ) {
429 if ( isset( $account['product_count'] ) ) {
430 $product_count = intval( $account['product_count'] );
431 }
432 }
433
434 if ( $formatted && is_int( $product_count ) ) {
435 $product_count = number_format( $product_count );
436 }
437
438 return $product_count;
439 }
440
441