PluginProbe
Datafeedr API / 1.1.2
Datafeedr API v1.1.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 / classes / class-dfrapi-networks.php

class-dfrapi-networks.php in Datafeedr API 1.1.2, at classes/class-dfrapi-networks.php

415 lines 19.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if ( ! class_exists( 'Dfrapi_Networks' ) ) {
6
7 /**
8 * Configuration page.
9 */
10 class Dfrapi_Networks {
11
12 private $page = 'dfrapi-networks';
13 private $key;
14 private $all_networks;
15
16 public function __construct() {
17
18 $this->key = 'dfrapi_networks';
19 $this->all_networks = dfrapi_api_get_all_networks();
20 add_action( 'init', array( $this, 'load_settings' ) );
21 add_action( 'admin_init', array( $this, 'register_settings' ) );
22 add_action( 'admin_notices', array( $this, 'admin_notice' ) );
23 add_action( 'admin_notices', array( $this, 'api_errors' ) );
24 }
25
26 function api_errors() {
27 if ( array_key_exists( 'dfrapi_api_error', $this->all_networks ) ) {
28 $html = '';
29 $html .= '<div class="notice notice-error">';
30 $html .= '<p>';
31 $html .= $this->all_networks;
32 $html .= '</p>';
33 $html .= '</div>';
34 echo $html;
35 }
36 }
37
38 function admin_notice() {
39 if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true && isset( $_GET['page'] ) && $this->key == $_GET['page'] ) {
40 echo '<div class="updated"><p>';
41 _e( 'Networks successfully updated!', DFRAPI_DOMAIN );
42 echo '</p></div>';
43 }
44 }
45
46 function load_settings() {
47 $this->options = (array) get_option( $this->key );
48 $this->options = array_merge( array(
49 'ids' => array(),
50 ), $this->options );
51 }
52
53 function register_settings() {
54 register_setting( $this->page, $this->key, array( $this, 'validate' ) );
55 add_settings_section( 'networks', __( 'Select Networks', DFRAPI_DOMAIN ), array( &$this, 'section_networks_desc' ), $this->page );
56 add_settings_field( 'ids', __( 'Networks', DFRAPI_DOMAIN ), array( &$this, 'field_network_ids' ), $this->page, 'networks', array( 'label_for' => 'DFRAPI_HIDE_LABEL' ) );
57 }
58
59 function section_networks_desc() {
60 echo __( 'Select the affiliate networks you belong to, enter your affiliate ID for each then click <strong>[Save Changes]</strong>.', DFRAPI_DOMAIN );
61 }
62
63 function field_network_ids() {
64 $groups = $this->get_network_group_names();
65 foreach ( $groups as $group ) {
66 $num_networks_checked_in_group = $this->num_networks_checked_in_group( $group );
67 $active = ( $num_networks_checked_in_group != '' ) ? 'active' : '';
68 $group_name = ( 'AffiliateWindow' == $group ) ? 'Awin' : $group;
69 echo '
70 <div class="group network_logo_30x30_' . dfrapi_group_name_to_css( $group ) . ' ' . $active . '" id="group_' . dfrapi_group_name_to_css( $group ) . '">
71 <div class="meta">
72 <span class="name">' . $group_name . '</span>
73 <span class="status">
74 ' . $this->num_missing_affiliate_ids_in_group( $group ) . '
75 ' . $num_networks_checked_in_group . '
76 ' . $this->num_networks_in_group( $group ) . '
77 ' . $this->num_merchants_in_group( $group ) . '
78 ' . $this->num_products_in_group( $group ) . '
79 </span>
80 </div>
81 ' . $this->get_groups_networks( $group ) . '
82 </div>';
83 }
84 }
85
86 function get_groups_networks( $group_name ) {
87 $html = '
88 <div style="display:none;" class="networks">
89 <table class="wp-list-table widefat fixed networks_table" cellspacing="0">
90 <thead>
91 <tr>
92 <th class="checkbox_head"> &nbsp; </th>
93 <th class="networks_head">' . __( 'Network', DFRAPI_DOMAIN ) . '</th>
94 <th class="type_head">' . __( 'Type', DFRAPI_DOMAIN ) . '</th>
95 <th class="aid_head">' . __( 'Affiliate ID', DFRAPI_DOMAIN ) . ' <a href="' .$this->map_link( $group_name ) .'" target="_blank" title="' . __( 'Learn how to find your affiliate ID from ', DFRAPI_DOMAIN ) . $group_name . __( ' (opens in new window).', DFRAPI_DOMAIN ) . '"><img src="' . DFRAPI_URL . 'images/icons/help.png" alt="' . __( 'more info', DFRAPI_DOMAIN ) . '" style="vertical-align: middle" /></a> <small style="font-weight:normal;color:#a00;">(' . __( 'required', DFRAPI_DOMAIN ) . ')</small></th>
96 <th class="tid_head">' . __( 'Tracking ID', DFRAPI_DOMAIN ) . ' <a href="https://datafeedrapi.helpscoutdocs.com/article/212-tracking-ids" target="_blank" title="' . __( 'Learn more about this field (opens in new window).', DFRAPI_DOMAIN ) . '"><img src="' . DFRAPI_URL . 'images/icons/help.png" alt="' . __( 'more info', DFRAPI_DOMAIN ) . '" style="vertical-align: middle" /></a> <small style="font-weight:normal;color:#999;">(' . __( 'optional', DFRAPI_DOMAIN ) . ')</small></th>
97 </tr>
98 </thead>
99 <tbody>
100 ';
101
102 $i=0;
103
104 foreach ( $this->all_networks as $network ) {
105
106 $i++;
107 $checked = ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) ? ' checked="checked"' : '';
108 $type = ( $network['type'] == 'products' ) ? __( 'products', DFRAPI_DOMAIN ) : __( 'coupons', DFRAPI_DOMAIN );
109 $type_class = ( $network['type'] == 'products' ) ? ' dfrapi_label-info"' : ' dfrapi_label-success';
110 $no_products = ( $network['product_count'] < 1 ) ? 'no_products' : '';
111 $alternate = ( $i % 2 ) ? '' : ' alternate';
112
113 if ( $network['group'] == $group_name ) {
114
115 $aid = ( isset( $this->options['ids'][ $network['_id'] ]['aid'] ) ) ? $this->options['ids'][ $network['_id'] ]['aid'] : '';
116 $tid = ( isset( $this->options['ids'][ $network['_id'] ]['tid'] ) ) ? $this->options['ids'][ $network['_id'] ]['tid'] : '';
117
118 $html .= '
119 <tr
120 class="network ' . $no_products . $alternate . '"
121 id="network_id_' . $network['_id'] . '"
122 nid="' . $network['_id'] . '"
123 key="' . $this->key . '"
124 aid="' . $aid . '"
125 tid="' . $tid . '"
126 >
127 <td class="network_checkbox">
128 <input type="checkbox" id="nid_' . $network['_id'] . '" class="check_network" name="' . $this->key . '[ids][' . $network['_id'] . '][nid]" value="' . $network['_id'] . '"' . $checked . ' />
129 </td>
130 <td class="network_name">
131 <label for="nid_' . $network['_id'] . '">
132 ' . $network['name'] . '
133 <div class="network_info">
134 <span class="num_merchants">' . number_format( $network['merchant_count'] ) . ' ' . __( 'merchants', DFRAPI_DOMAIN ) . ' <span class="sep">/</span>
135 <span class="num_products">' . number_format( $network['product_count'] ) . ' ' . $type . '</span>
136 </div>
137 </label>
138 </td>
139 <td class="network_type">
140 <span class="dfrapi_label' . $type_class . '">' . ucfirst( $type ) . '</span>
141 </td>
142 ';
143
144 if ( $network['group_id'] == 10037 ) {
145 $html .= '<td class="aid_input">' . $this->zanox_adspace( $network['_id'], $aid ) . '</td>';
146 } elseif ( $network['group_id'] == 10027 ) {
147 $url = admin_url( 'admin.php?page=dfrapi' );
148 $html .= '<td class="aid_input"><a href="' . $url . '" target="_blank">Add/Edit Partnerize Keys</a></td>';
149 } elseif ( $network['group_id'] == 10017 ) {
150 $url = admin_url( 'admin.php?page=dfrapi' );
151 $html .= '<td class="aid_input"><a href="' . $url . '" target="_blank">Add/Edit Effiliation Key</a></td>';
152 } elseif ( $network['group_id'] == 10007 && is_wp_error( dfrapi_get_belboon_adspace_id() ) ) {
153 $url = admin_url( 'admin.php?page=dfrapi' );
154 $html .= '<td class="aid_input"><a href="' . $url . '" target="_blank">Your Belboon Adspace ID is required before you can enter your affiliate ID. Enter Adspace ID.</a></td>';
155 } elseif ( $network['group_id'] == 10033 && is_wp_error( dfrapi_get_affiliate_gateway_sid() ) ) {
156 $url = admin_url( 'admin.php?page=dfrapi' );
157 $html .= '<td class="aid_input"><a href="' . $url . '" target="_blank">Your Affiliate Gateway SID is required before you can enter your affiliate ID. Enter SID.</a></td>';
158 } else {
159 $html .= '<td class="aid_input"><input type="text" name="dfrapi_networks[ids][' . $network['_id'] . '][aid]" value="' . $aid . '" class="aid_input_field" /></td>';
160 }
161
162 if ( $group_name == 'NoTrackingIDOption' ) {
163 $html .= '<td class="tid_input"><small>n/a</small></td>';
164 } else {
165 $html .= '<td class="tid_input"><input type="text" name="dfrapi_networks[ids][' . $network['_id'] . '][tid]" value="' . $tid . '" class="tid_input_field" /></td>';
166 }
167
168 $html .= '</tr>';
169 }
170 }
171
172 $html .= '
173 </tbody>
174 </table>
175 </div>
176 ';
177 return $html;
178 }
179
180 function get_network_group_names() {
181 $networks = $this->all_networks;
182 $groups = array();
183 foreach ( $networks as $network ) {
184 $groups[] = $network['group'];
185 }
186 return array_unique( $groups );
187 }
188
189 /**
190 * This returns <select> menu for adspaces.
191 */
192 function zanox_adspace( $nid, $selected_adspace ) {
193 $html = '';
194 $adspaces = $this->get_zanox_adspaces();
195 if ( isset( $adspaces['zanox_error'] ) ) {
196 if ( $adspaces['zanox_error'] == 'missing_keys' ) {
197 $html .= '<span><a href="' . admin_url( 'admin.php?page=dfrapi' ) . '" class="dfrapi_warning">' . __( 'Please add your Zanox Connection &amp; Secret Key', DFRAPI_DOMAIN ) . '</a>.</span>';
198 } else {
199 $html .= '<pre>' . print_r( $adspaces['zanox_error'], TRUE ) . '</pre>';
200 }
201 } else {
202 $html .= '<select name="dfrapi_networks[ids][' . $nid . '][aid]">';
203 $html .= '<option value="">' . __( 'Select an adspace', DFRAPI_DOMAIN ) . '</option>';
204 foreach( $adspaces as $adspace ) {
205 $selected = selected( $selected_adspace, $adspace['id'], false );
206 $html .= '<option value="' . $adspace['id'] . '" ' . $selected . '>' . $adspace['name'] . '</option>';
207 }
208 $html .= '</select>';
209 }
210 return $html;
211 }
212
213 /**
214 * This returns adspaces for a Zanox user.
215 */
216 function get_zanox_adspaces() {
217 $option_name = 'dfrapi_zanox_adspaces';
218 $use_cache = wp_using_ext_object_cache( false );
219 $adspaces = get_transient( $option_name );
220 wp_using_ext_object_cache( $use_cache );
221 if ( false === $adspaces || empty ( $adspaces ) ) {
222 $zanox_keys = dfrapi_get_zanox_keys();
223 if ( !$zanox_keys ) {
224 return array( 'zanox_error' => 'missing_keys' );
225 } else {
226 $client = new Dfr_ZanoxAPIClient( $zanox_keys['connection_key'], $zanox_keys['secret_key'] );
227 $adspaces = $client->adspaces();
228 if ( $client->error() ) {
229 return array( 'zanox_error' => $client->error() );
230 } else {
231 $use_cache = wp_using_ext_object_cache( false );
232 set_transient( $option_name, $adspaces, HOUR_IN_SECONDS );
233 wp_using_ext_object_cache( $use_cache );
234 }
235 }
236 }
237 dfrapi_update_transient_whitelist( $option_name );
238 return $adspaces;
239 }
240
241 function num_missing_affiliate_ids_in_group( $group_name ) {
242 $count = 0;
243 foreach ( $this->all_networks as $network ) {
244
245 if ( $network['group_id'] == 10027 ) {
246 continue;
247 }
248
249 if ( $network['group_id'] == 10017 ) {
250 continue;
251 }
252
253 if ( $network['group'] == $group_name ) {
254 if ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) {
255 if ( trim( $this->options['ids'][ $network['_id'] ]['aid'] ) == '' ) {
256 $count ++;
257 }
258 }
259 }
260 }
261
262 if ( $count > 0 ) {
263 $messages = $this->messages();
264
265 return '<span class="num_missing">' . sprintf( translate_nooped_plural( $messages['num_missing'],
266 $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
267 }
268
269 return '';
270 }
271
272 function num_networks_checked_in_group( $group_name ) {
273 $count = 0;
274 foreach ( $this->all_networks as $network ) {
275 if ( $network['group'] == $group_name ) {
276 if ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) {
277 $count++;
278 }
279 }
280 }
281
282 if ( $count > 0 ) {
283 $messages = $this->messages();
284 return '<span class="num_checked">' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
285 }
286 }
287
288 function num_networks_in_group( $group_name ) {
289 $count = 0;
290 foreach ( $this->all_networks as $network ) {
291 if ( $network['group'] == $group_name ) {
292 $count++;
293 }
294 }
295
296 if ( $count > 0 ) {
297 $messages = $this->messages();
298 return '<span class="num_networks">' . sprintf( translate_nooped_plural( $messages['num_networks'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
299 }
300 }
301
302 function num_merchants_in_group( $group_name ) {
303 $count = 0;
304 foreach ( $this->all_networks as $network ) {
305 if ( $network['group'] == $group_name ) {
306 $count += $network['merchant_count'];
307 }
308 }
309
310 if ( $count > 0 ) {
311 $messages = $this->messages();
312 return '<span class="num_merchants">' . sprintf( translate_nooped_plural( $messages['num_merchants'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
313 }
314 }
315
316 function num_products_in_group( $group_name ) {
317 $count = 0;
318 foreach ( $this->all_networks as $network ) {
319 if ( $network['group'] == $group_name ) {
320 $count += $network['product_count'];
321 }
322 }
323
324 if ( $count > 0 ) {
325 $messages = $this->messages();
326 return '<span class="num_products">' . sprintf( translate_nooped_plural( $messages['num_products'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span>';
327 }
328
329 }
330
331 function messages() {
332 return array(
333 'num_missing' => _n_noop( '%s missing affiliate ID', '%s missing affiliate IDs' ),
334 'num_checked' => _n_noop( '%s network selected', '%s networks selected' ),
335 'num_networks' => _n_noop( '%s network', '%s networks' ),
336 'num_merchants' => _n_noop( '%s merchant', '%s merchants' ),
337 'num_products' => _n_noop( '%s product', '%s products' ),
338 );
339 }
340
341 function map_link( $name ) {
342 $links = array(
343 '2Performant' => 'https://datafeedrapi.helpscoutdocs.com/article/240-how-to-find-your-2performant-affiliate-id',
344 'Admitad' => 'https://datafeedrapi.helpscoutdocs.com/article/241-how-to-find-your-admitad-affiliate-id',
345 'ADCELL' => 'https://datafeedrapi.helpscoutdocs.com/article/235-how-to-find-your-adcell-affiliate-id',
346 'Adrecord' => 'https://datafeedrapi.helpscoutdocs.com/article/115-how-to-find-your-adrecord-affiliate-id',
347 'Adtraction' => 'https://datafeedrapi.helpscoutdocs.com/article/116-how-to-find-your-adtraction-affiliate-id',
348 'Affiliate4You' => 'http://www.datafeedr.com/docs/item/265',
349 'Awin' => 'https://datafeedrapi.helpscoutdocs.com/article/120-how-to-find-your-affiliate-window-affiliate-id',
350 'Affiliator' => 'http://www.datafeedr.com/docs/item/270',
351 'Affilinet' => 'https://datafeedrapi.helpscoutdocs.com/article/121-how-to-find-your-affilinet-affiliate-id',
352 'Amazon Local' => 'http://www.datafeedr.com/docs/item/275',
353 'APD' => 'https://datafeedrapi.helpscoutdocs.com/article/133-how-to-find-your-apd-affiliate-id',
354 'Avangate' => 'https://datafeedrapi.helpscoutdocs.com/article/122-how-to-find-your-avangate-affiliate-id',
355 'AvantLink' => 'https://datafeedrapi.helpscoutdocs.com/article/123-how-to-find-your-avantlink-affiliate-id',
356 'Belboon' => 'https://datafeedrapi.helpscoutdocs.com/article/125-how-to-find-your-belboon-affiliate-id',
357 'BettyMills' => 'https://datafeedrapi.helpscoutdocs.com/article/129-how-to-find-your-betty-mills-affiliate-id',
358 'bol.com' => 'https://datafeedrapi.helpscoutdocs.com/article/186-how-to-find-your-bol-com-affiliate-id',
359 'ClickBank' => 'https://datafeedrapi.helpscoutdocs.com/article/130-how-to-find-your-clickbank-affiliate-id',
360 'ClixGalore' => 'https://datafeedrapi.helpscoutdocs.com/article/131-how-to-find-your-clixgalore-affiliate-id',
361 'Commission Factory' => 'https://datafeedrapi.helpscoutdocs.com/article/132-how-to-find-your-commission-factory-affiliate-id',
362 'Commission Junction' => 'https://datafeedrapi.helpscoutdocs.com/article/128-how-to-find-your-cj-affiliate-id',
363 'Commission Monster' => 'http://www.datafeedr.com/docs/item/109',
364 'Connexity' => 'https://datafeedrapi.helpscoutdocs.com/article/178-how-to-find-your-connexity-affiliate-id',
365 'Daisycon' => 'https://datafeedrapi.helpscoutdocs.com/article/135-how-to-find-your-daisycon-affiliate-id',
366 'DGM' => 'http://www.datafeedr.com/docs/item/263',
367 'Double.net' => 'https://datafeedrapi.helpscoutdocs.com/article/136-how-to-find-your-double-net-affiliate-id',
368 'Effiliation' => 'https://datafeedrapi.helpscoutdocs.com/article/211-how-to-find-your-effiliation-api-key',
369 'FamilyBlend' => 'https://datafeedrapi.helpscoutdocs.com/article/117-how-to-find-your-familyblend-affiliate-id',
370 'FlexOffers' => 'https://datafeedrapi.helpscoutdocs.com/article/209-how-to-find-your-flexoffers-affiliate-id',
371 'FlipKart' => 'https://datafeedrapi.helpscoutdocs.com/article/137-how-to-find-your-flipkart-affiliate-id',
372 'Impact' => 'https://datafeedrapi.helpscoutdocs.com/article/134-how-to-find-your-impact-radius-affiliate-id',
373 'LinkConnector' => 'https://datafeedrapi.helpscoutdocs.com/article/138-how-to-find-your-linkconnector-affiliate-id',
374 'Rakuten' => 'https://datafeedrapi.helpscoutdocs.com/article/139-how-to-find-your-rakuten-affiliate-id',
375 'M4N' => 'http://www.datafeedr.com/docs/item/198',
376 'MyCommerce' => 'http://www.datafeedr.com/docs/item/111',
377 'OneNetworkDirect' => 'https://datafeedrapi.helpscoutdocs.com/article/140-how-to-find-your-onenetworkdirect-affiliate-id',
378 'Optimise' => 'https://datafeedrapi.helpscoutdocs.com/article/141-how-to-find-your-optimise-affiliate-id',
379 'Paid on Results' => 'https://datafeedrapi.helpscoutdocs.com/article/142-how-to-find-your-paid-on-results-affiliate-id',
380 'Partner-ads' => 'https://datafeedrapi.helpscoutdocs.com/article/143-how-to-find-your-partner-ads-affiliate-id',
381 'Partnerize' => 'https://datafeedrapi.helpscoutdocs.com/article/195-how-to-find-your-partnerize-publisher-id-and-api-keys',
382 'PepperJam' => 'https://datafeedrapi.helpscoutdocs.com/article/127-how-to-find-your-ebay-enterprise-affiliate-network-affiliate-id',
383 'PerformanceHorizon' => 'https://datafeedrapi.helpscoutdocs.com/article/195-how-to-find-your-performance-horizon-publisher-id-and-api-keys',
384 'RegNow' => 'http://www.datafeedr.com/docs/item/111',
385 'RevResponse' => 'https://datafeedrapi.helpscoutdocs.com/article/144-how-to-find-your-revresponse-affiliate-id',
386 'ShareASale' => 'https://datafeedrapi.helpscoutdocs.com/article/126-how-to-find-your-shareasale-affiliate-id',
387 'Shopello' => 'https://datafeedrapi.helpscoutdocs.com/article/171-how-to-find-your-shopello-affiliate-id',
388 'Snapdeal' => 'https://datafeedrapi.helpscoutdocs.com/article/172-how-to-find-your-snapdeal-affiliate-id',
389 'SuperClix' => 'https://datafeedrapi.helpscoutdocs.com/article/145-how-to-find-your-superclix-affiliate-id',
390 'The Affiliate Gateway' => 'https://datafeedrapi.helpscoutdocs.com/article/225-how-to-find-your-affiliate-gateway-affiliate-id',
391 'TradeDoubler' => 'https://datafeedrapi.helpscoutdocs.com/article/146-how-to-find-your-tradedoubler-affiliate-id',
392 'TradeTracker' => 'https://datafeedrapi.helpscoutdocs.com/article/148-how-to-find-your-tradetracker-affiliate-id',
393 'Webgains' => 'https://datafeedrapi.helpscoutdocs.com/article/147-how-to-find-your-webgains-affiliate-id',
394 'Zanox' => 'https://datafeedrapi.helpscoutdocs.com/article/149-how-to-find-your-zanox-api-keys',
395 );
396
397 return isset( $links[ $name ] ) ? $links[ $name ] : DFRAPI_DOCS_URL;
398 }
399
400 function validate( $input ) {
401 $new_input['ids'] = array();
402 if ( isset( $input['ids'] ) ) {
403 foreach ( $input['ids'] as $k => $v ) {
404 if ( isset( $v['nid'] ) ) {
405 $new_input['ids'][$k] = $v;
406 }
407 }
408 }
409 return $new_input;
410 }
411
412 } // class Dfrapi_Networks
413
414 } // class_exists check
415