PluginProbe
Datafeedr API / 1.0.2
Datafeedr API v1.0.2
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 1.0.33 All 181 releases
datafeedr-api / functions / functions.php

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

408 lines 15.7 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_filter_affiliate_id', 'dfrapi_get_zanox_zmid', 10, 3 );
130 function dfrapi_get_zanox_zmid( $affiliate_id, $url, $product ) {
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 = $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 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
210 // Affiliate ID is missing. Do action and return empty string.
211 if ( $affiliate_id == '' ) {
212 do_action( 'dfrapi_affiliate_id_is_missing', $product );
213 return '';
214 }
215
216 // Set URL and apply filters.
217 $url = $product['url'];
218 $url = apply_filters( 'dfrapi_before_affiliate_id_insertion', $url, $product, $affiliate_id );
219 $affiliate_id = apply_filters( 'dfrapi_filter_affiliate_id', $affiliate_id, $url, $product );
220 $url = str_replace( "@@@", $affiliate_id, $url );
221 $url = apply_filters( 'dfrapi_after_affiliate_id_insertion', $url, $product, $affiliate_id );
222
223 // Return URL
224 return $url;
225 }
226
227 /**
228 * Output an error message generated by the API.
229 */
230 function dfrapi_output_api_error( $data ) {
231 $error = @$data['dfrapi_api_error'];
232 $params = @$data['dfrapi_api_error']['params'];
233 ?>
234 <div class="dfrapi_api_error">
235 <div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div>
236 <div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?></div>
237 <div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?></div>
238 <div class="dfrapi_class"><strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div>
239 <?php if ( is_array( $params ) ) : ?>
240 <div class="dfrps_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> <span><?php echo dfrapi_display_api_request( $params ); ?></span></div>
241 <?php endif; ?>
242 </div>
243 <?php
244 }
245
246 /**
247 * Convert a currency code to sign. USD => $
248 */
249 function dfrapi_currency_code_to_sign( $code ) {
250 $map = array(
251 'USD' => '&#36;',
252 'GBP' => '&pound;',
253 'EUR' => '&euro;',
254 'CAD' => '&#36;',
255 'AUD' => '&#36;',
256 'DKK' => 'kr',
257 'SEK' => 'kr',
258 'NOK' => 'kr',
259 'CHF' => 'CHF',
260 'NZD' => '&#36;',
261 'TRY' => '&#8356;',
262 );
263
264 $map = apply_filters( 'dfrapi_currency_sign_mapper', $map );
265
266 if ( isset ( $map[$code] ) ) {
267 return $map[$code];
268 } else {
269 return $map['USD'];
270 }
271 }
272
273 /**
274 * This displays the API request in PHP format.
275 */
276 function dfrapi_display_api_request( $params=array() ) {
277
278 $html = '';
279
280 if ( empty( $params ) ) { return $html; }
281
282 $html .= '$search = $api->searchRequest();<br />';
283 foreach ( $params as $k => $v ) {
284
285 // Handle query.
286 if ( $k == 'query' ) {
287 foreach ( $v as $query ) {
288 if ( substr( $query, 0, 9 ) !== 'source_id' || substr( $query, 0, 11 ) !== 'merchant_id' ) {
289 $query = str_replace( ",", ", ", $query );
290 }
291 $html .= '$search->addFilter( \''.( $query ).'\' );<br />';
292 }
293 }
294
295 // Handle sort.
296 if ( $k == 'sort' ) {
297 foreach ( $v as $sort ) {
298 $html .= '$search->addSort( \''.stripslashes( $sort ).'\' );<br />';
299 }
300 }
301
302 // Handle limit.
303 if ( $k == 'limit' ) {
304 $html .= '$search->setLimit( \''.stripslashes( $v ).'\' );<br />';
305 }
306
307 // Handle Offset.
308 if ( $k == 'offset' ) {
309 $html .= '$search->setOffset( \''.stripslashes( $v ).'\' );<br />';
310 }
311
312 // Handle Exclude duplicates.
313 if ( $k == 'exclude_duplicates' ) {
314 $html .= '$search->excludeDuplicates( \''. $v . '\' );<br />';
315 }
316 }
317
318 $html .= '$products = $search->execute();';
319 return $html;
320
321 }
322
323 function dfrapi_get_query_param( $query, $param ) {
324 if ( is_array( $query ) && !empty( $query ) ) {
325 foreach( $query as $k => $v ) {
326 if ( $v['field'] == $param ) {
327 return array(
328 'field' => @$v['field'],
329 'operator' => @$v['operator'],
330 'value' => @$v['value'],
331 );
332 }
333 }
334 }
335 return false;
336 }
337
338 /**
339 * Converts a value in cents into a value with proper
340 * decimal placement.
341 *
342 * Example: 14999 => 149.99
343 */
344 function dfrapi_int_to_price( $price ) {
345 return number_format( ( $price/100 ), 2 );
346 }
347
348 /**
349 * Converts decimal or none decimal prices into values in cents.
350 *
351 * assert(dfrapi_price_to_int('123') ==12300);
352 * assert(dfrapi_price_to_int('123.4') ==12340);
353 * assert(dfrapi_price_to_int('1234.56') ==123456);
354 * assert(dfrapi_price_to_int('123,4') ==12340);
355 * assert(dfrapi_price_to_int('1234,56') ==123456);
356 * assert(dfrapi_price_to_int('1,234,567') ==123456700);
357 * assert(dfrapi_price_to_int('1,234,567.8') ==123456780);
358 * assert(dfrapi_price_to_int('1,234,567.89') ==123456789);
359 * assert(dfrapi_price_to_int('1.234.567') ==123456700);
360 * assert(dfrapi_price_to_int('1.234.567,8') ==123456780);
361 * assert(dfrapi_price_to_int('1.234.567,89') ==123456789);
362 * assert(dfrapi_price_to_int('FOO 123 BAR') ==12300);
363 */
364 function dfrapi_price_to_int( $price ) {
365 $d = $price;
366 $d = preg_replace('~^[^\d.,]+~', '', $d);
367 $d = preg_replace('~[^\d.,]+$~', '', $d);
368
369 // 123 => 12300
370 if(preg_match('~^(\d+)$~', $d, $m))
371 return intval($m[1] . '00');
372
373 // 123.4 => 12340, 123,45 => 12345
374 if(preg_match('~^(\d+)[.,](\d{1,2})$~', $d, $m))
375 return intval($m[1] . substr($m[2] . '0000', 0, 2));
376
377 // 1,234,567.89 => 123456789
378 if(preg_match('~^((?:\d{1,3})(?:,\d{3})*)(\.\d{1,2})?$~', $d, $m)) {
379 $f = isset($m[2]) ? $m[2] : '.';
380 return intval(str_replace(',', '', $m[1]) . substr($f . '0000', 1, 2));
381 }
382
383 // 1.234.567,89 => 123456789
384 if(preg_match('~^((?:\d{1,3})(?:\.\d{3})*)(,\d{1,2})?$~', $d, $m)) {
385 $f = isset($m[2]) ? $m[2] : '.';
386 return intval(str_replace('.', '', $m[1]) . substr($f . '0000', 1, 2));
387 }
388
389 return NULL;
390 }
391
392 function dfrapi_html_output_api_error( $data ) {
393 $error = $data['dfrapi_api_error'];
394 $params = @$data['dfrapi_api_error']['params'];
395 ?>
396 <div class="dfrapi_api_error">
397 <div class="dfrapi_head"><?php _e( 'Datafeedr API Error', DFRAPI_DOMAIN ); ?></div>
398 <div class="dfrapi_msg"><strong><?php _e( 'Message:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['msg']; ?></div>
399 <div class="dfrapi_code"><strong><?php _e( 'Code:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['code']; ?></div>
400 <div class="dfrapi_class"><strong><?php _e( 'Class:', DFRAPI_DOMAIN ); ?></strong> <?php echo $error['class']; ?></div>
401 <?php if ( is_array( $params ) ) : ?>
402 <div class="dfrapi_query"><strong><?php _e( 'Query:', DFRAPI_DOMAIN ); ?></strong> <span><?php echo dfrapi_helper_display_api_request( $params ); ?></span></div>
403 <?php endif; ?>
404 </div>
405 <?php
406 }
407
408