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

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

384 lines 15.2 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_menu', array( $this, 'admin_menu' ) );
23 add_action( 'admin_notices', array( $this, 'admin_notice' ) );
24 }
25
26 function api_errors() {
27 if ( array_key_exists( 'dfrapi_api_error', $this->all_networks ) ) {
28 return $this->all_networks;
29 }
30 return false;
31 }
32
33 function admin_menu() {
34 add_submenu_page(
35 'dfrapi',
36 __( 'Networks &#8212; Datafeedr API', DFRAPI_DOMAIN ),
37 __( 'Networks', DFRAPI_DOMAIN ),
38 'manage_options',
39 $this->key,
40 array( $this, 'output' )
41 );
42 }
43
44 function admin_notice() {
45 if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true && isset( $_GET['page'] ) && $this->key == $_GET['page'] ) {
46 echo '<div class="updated"><p>';
47 _e( 'Networks successfully updated!', DFRAPI_DOMAIN );
48 echo '</p></div>';
49 }
50 }
51
52 function output() {
53 echo '<div class="wrap" id="' . $this->key . '">';
54 echo '<h2>' . dfrapi_setting_pages( $this->page ) . ' &#8212; Datafeedr API</h2>';
55 if ( $errors = $this->api_errors() ) {
56 echo dfrapi_html_output_api_error( $errors );
57 } else {
58 echo '<form method="post" action="options.php">';
59 wp_nonce_field( 'update-options' );
60 settings_fields( $this->page );
61 do_settings_sections( $this->page);
62 submit_button();
63 echo '</form>';
64 }
65 echo '<div id="dfr_unload_message" style="display:none">' . __( 'You have unsaved changes', DFRAPI_DOMAIN ) . '</div>';
66 echo '</div>';
67 }
68
69 function load_settings() {
70 $this->options = (array) get_option( $this->key );
71 $this->options = array_merge( array(
72 'ids' => array(),
73 ), $this->options );
74 }
75
76 function register_settings() {
77 register_setting( $this->page, $this->key, array( $this, 'validate' ) );
78 add_settings_section( 'networks', __( 'Select Networks', DFRAPI_DOMAIN ), array( &$this, 'section_networks_desc' ), $this->page );
79 add_settings_field( 'ids', __( 'Networks', DFRAPI_DOMAIN ), array( &$this, 'field_network_ids' ), $this->page, 'networks', array( 'label_for' => 'DFRAPI_HIDE_LABEL' ) );
80 }
81
82 function section_networks_desc() {
83 echo __( 'Select the affiliate networks you belong to, enter your affiliate ID for each then click <strong>[Save Changes]</strong>.', DFRAPI_DOMAIN );
84 }
85
86 function field_network_ids() {
87 $groups = $this->get_network_group_names();
88 foreach ( $groups as $group ) {
89 $num_networks_checked_in_group = $this->num_networks_checked_in_group( $group );
90 $active = ( $num_networks_checked_in_group != '' ) ? 'active' : '';
91 echo '
92 <div class="group network_logo_30x30_' . dfrapi_group_name_to_css( $group ) . ' ' . $active . '" id="group_' . dfrapi_group_name_to_css( $group ) . '">
93 <div class="meta">
94 <span class="name">' . $group . '</span>
95 <span class="status">
96 ' . $this->num_missing_affiliate_ids_in_group( $group ) . '
97 ' . $num_networks_checked_in_group . '
98 ' . $this->num_networks_in_group( $group ) . '
99 ' . $this->num_merchants_in_group( $group ) . '
100 ' . $this->num_products_in_group( $group ) . '
101 </span>
102 </div>
103 ' . $this->get_groups_networks( $group ) . '
104 </div>';
105 }
106 }
107
108 function get_groups_networks( $group_name ) {
109 $html = '
110 <div style="display:none;" class="networks">
111 <table class="wp-list-table widefat fixed networks_table" cellspacing="0">
112 <thead>
113 <tr>
114 <th class="checkbox_head"> &nbsp; </th>
115 <th class="networks_head">' . __( 'Network', DFRAPI_DOMAIN ) . '</th>
116 <th class="type_head">' . __( 'Type', DFRAPI_DOMAIN ) . '</th>
117 <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>
118 <th class="tid_head">' . __( 'Tracking ID', DFRAPI_DOMAIN ) . ' <a href="' . DFRAPI_HOME_URL . '/node/1113" 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>
119 </tr>
120 </thead>
121 <tbody>
122 ';
123
124 $i=0;
125 foreach ( $this->all_networks as $network ) {
126 $i++;
127 $checked = ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) ? ' checked="checked"' : '';
128 $type = ( $network['type'] == 'products' ) ? __( 'products', DFRAPI_DOMAIN ) : __( 'coupons', DFRAPI_DOMAIN );
129 $type_class = ( $network['type'] == 'products' ) ? ' dfrapi_label-info"' : ' dfrapi_label-success';
130 $no_products = ( $network['product_count'] < 1 ) ? 'no_products' : '';
131 $alternate = ( $i % 2 ) ? '' : ' alternate';
132
133 if ( $network['group'] == $group_name ) {
134 $html .= '
135 <tr
136 class="network ' . $no_products . $alternate . '"
137 id="network_id_' . $network['_id'] . '"
138 nid="' . $network['_id'] . '"
139 key="' . $this->key . '"
140 aid="' . @$this->options['ids'][$network['_id']]['aid'] . '"
141 tid="' . @$this->options['ids'][$network['_id']]['tid'] . '"
142 >
143 <td class="network_checkbox">
144 <input type="checkbox" id="nid_' . $network['_id'] . '" class="check_network" name="' . $this->key . '[ids][' . $network['_id'] . '][nid]" value="' . $network['_id'] . '"' . $checked . ' />
145 </td>
146 <td class="network_name">
147 <label for="nid_' . $network['_id'] . '">
148 ' . $network['name'] . '
149 <div class="network_info">
150 <span class="num_merchants">' . number_format( $network['merchant_count'] ) . ' ' . __( 'merchants', DFRAPI_DOMAIN ) . ' <span class="sep">/</span>
151 <span class="num_products">' . number_format( $network['product_count'] ) . ' ' . $type . '</span>
152 </div>
153 </label>
154 </td>
155 <td class="network_type">
156 <span class="dfrapi_label' . $type_class . '">' . ucfirst( $type ) . '</span>
157 </td>
158 ';
159
160 if ( $group_name == 'Zanox' ) {
161 $html .= '<td class="aid_input">' . $this->zanox_adspace( $network['_id'], @$this->options['ids'][$network['_id']]['aid'] ) . '</td>';
162 } else {
163 $html .= '<td class="aid_input"><input type="text" name="dfrapi_networks[ids][' . $network['_id'] . '][aid]" value="' . @$this->options['ids'][$network['_id']]['aid'] . '" class="aid_input_field" /></td>';
164 }
165
166 $html .= '
167 <td class="tid_input"><input type="text" name="dfrapi_networks[ids][' . $network['_id'] . '][tid]" value="' . @$this->options['ids'][$network['_id']]['tid'] . '" class="tid_input_field" /></td>
168 </tr>
169 ';
170 }
171 }
172
173 $html .= '
174 </tbody>
175 </table>
176 </div>
177 ';
178 return $html;
179 }
180
181 function get_network_group_names() {
182 $networks = $this->all_networks;
183 $groups = array();
184 foreach ( $networks as $network ) {
185 $groups[] = $network['group'];
186 }
187 return array_unique( $groups );
188 }
189
190 /**
191 * This returns <select> menu for adspaces.
192 */
193 function zanox_adspace( $nid, $selected_adspace ) {
194 $html = '';
195 $adspaces = $this->get_zanox_adspaces();
196 if ( isset( $adspaces['zanox_error'] ) ) {
197 if ( $adspaces['zanox_error'] == 'missing_keys' ) {
198 $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>';
199 } else {
200 $html .= '<pre>' . print_r( $adspaces['zanox_error'], TRUE ) . '</pre>';
201 }
202 } else {
203 $html .= '<select name="dfrapi_networks[ids][' . $nid . '][aid]">';
204 $html .= '<option value="">' . __( 'Select an adspace', DFRAPI_DOMAIN ) . '</option>';
205 foreach( $adspaces as $adspace ) {
206 $selected = selected( $selected_adspace, $adspace['id'], false );
207 $html .= '<option value="' . $adspace['id'] . '" ' . $selected . '>' . $adspace['name'] . '</option>';
208 }
209 $html .= '</select>';
210 }
211 return $html;
212 }
213
214 /**
215 * This returns adspaces for a Zanox user.
216 */
217 function get_zanox_adspaces() {
218 $option_name = 'dfrapi_zanox_adspaces';
219 $adspaces = get_transient( $option_name );
220 if ( false === $adspaces || empty ( $adspaces ) ) {
221 $zanox_keys = dfrapi_get_zanox_keys();
222 if ( !$zanox_keys ) {
223 return array( 'zanox_error' => 'missing_keys' );
224 } else {
225 $client = new Dfr_ZanoxAPIClient( $zanox_keys['connection_key'], $zanox_keys['secret_key'] );
226 $adspaces = $client->adspaces();
227 if ( $client->error() ) {
228 return array( 'zanox_error' => $client->error() );
229 } else {
230 set_transient( $option_name, $adspaces, HOUR_IN_SECONDS );
231 }
232 }
233 }
234 dfrapi_update_transient_whitelist( $option_name );
235 return $adspaces;
236 }
237
238 function num_missing_affiliate_ids_in_group( $group_name ) {
239 $count = 0;
240 foreach ( $this->all_networks as $network ) {
241 if ( $network['group'] == $group_name ) {
242 if ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) {
243 if ( trim( $this->options['ids'][$network['_id']]['aid'] ) == '' ) {
244 $count++;
245 }
246 }
247 }
248 }
249
250 if ( $count > 0 ) {
251 $messages = $this->messages();
252 return '<span class="num_missing">' . sprintf( translate_nooped_plural( $messages['num_missing'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
253 }
254 }
255
256 function num_networks_checked_in_group( $group_name ) {
257 $count = 0;
258 foreach ( $this->all_networks as $network ) {
259 if ( $network['group'] == $group_name ) {
260 if ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) {
261 $count++;
262 }
263 }
264 }
265
266 if ( $count > 0 ) {
267 $messages = $this->messages();
268 return '<span class="num_checked">' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
269 }
270 }
271
272 function num_networks_in_group( $group_name ) {
273 $count = 0;
274 foreach ( $this->all_networks as $network ) {
275 if ( $network['group'] == $group_name ) {
276 $count++;
277 }
278 }
279
280 if ( $count > 0 ) {
281 $messages = $this->messages();
282 return '<span class="num_networks">' . sprintf( translate_nooped_plural( $messages['num_networks'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
283 }
284 }
285
286 function num_merchants_in_group( $group_name ) {
287 $count = 0;
288 foreach ( $this->all_networks as $network ) {
289 if ( $network['group'] == $group_name ) {
290 $count += $network['merchant_count'];
291 }
292 }
293
294 if ( $count > 0 ) {
295 $messages = $this->messages();
296 return '<span class="num_merchants">' . sprintf( translate_nooped_plural( $messages['num_merchants'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
297 }
298 }
299
300 function num_products_in_group( $group_name ) {
301 $count = 0;
302 foreach ( $this->all_networks as $network ) {
303 if ( $network['group'] == $group_name ) {
304 $count += $network['product_count'];
305 }
306 }
307
308 if ( $count > 0 ) {
309 $messages = $this->messages();
310 return '<span class="num_products">' . sprintf( translate_nooped_plural( $messages['num_products'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span>';
311 }
312
313 }
314
315 function messages() {
316 return array(
317 'num_missing' => _n_noop( '%s missing affiliate ID', '%s missing affiliate IDs' ),
318 'num_checked' => _n_noop( '%s network selected', '%s networks selected' ),
319 'num_networks' => _n_noop( '%s network', '%s networks' ),
320 'num_merchants' => _n_noop( '%s merchant', '%s merchants' ),
321 'num_products' => _n_noop( '%s product', '%s products' ),
322 );
323 }
324
325 function map_link( $name ) {
326 $links = array(
327 'AdRecord' => 'http://www.datafeedr.com/docs/item/252',
328 'Adtraction' => 'http://www.datafeedr.com/docs/item/261',
329 'Affiliate4You' => 'http://www.datafeedr.com/docs/item/265',
330 'AffiliateWindow' => 'http://www.datafeedr.com/docs/item/101',
331 'Affiliator' => 'http://www.datafeedr.com/docs/item/270',
332 'Affilinet' => 'http://www.datafeedr.com/docs/item/104',
333 'Amazon Local' => 'http://www.datafeedr.com/docs/item/275',
334 'Avangate' => 'http://www.datafeedr.com/docs/item/188',
335 'AvantLink' => 'http://www.datafeedr.com/docs/item/190',
336 'Belboon' => 'http://www.datafeedr.com/docs/item/201',
337 'BettyMills' => 'http://www.datafeedr.com/docs/item/264',
338 'bol.com' => 'http://www.datafeedr.com/docs/item/253',
339 'ClickBank' => 'http://www.datafeedr.com/docs/item/106',
340 'ClixGalore' => 'http://www.datafeedr.com/docs/item/200',
341 'Commission Factory' => 'http://www.datafeedr.com/docs/item/193',
342 'Commission Junction' => 'http://www.datafeedr.com/docs/item/65',
343 'Commission Monster' => 'http://www.datafeedr.com/docs/item/109',
344 'Daisycon' => 'http://www.datafeedr.com/docs/item/196',
345 'DGM' => 'http://www.datafeedr.com/docs/item/263',
346 'Double.net' => 'http://www.datafeedr.com/docs/item/271',
347 'FlipKart' => 'http://www.datafeedr.com/docs/item/273',
348 'Impact Radius' => 'http://www.datafeedr.com/docs/item/268',
349 'LinkConnector' => 'http://www.datafeedr.com/docs/item/105',
350 'LinkShare' => 'http://www.datafeedr.com/docs/item/60',
351 'M4N' => 'http://www.datafeedr.com/docs/item/198',
352 'MyCommerce' => 'http://www.datafeedr.com/docs/item/111',
353 'OneNetworkDirect' => 'http://www.datafeedr.com/docs/item/112',
354 'Paid on Results' => 'http://www.datafeedr.com/docs/item/189',
355 'Partner-ads' => 'http://www.datafeedr.com/docs/item/262',
356 'PepperJam' => 'http://www.datafeedr.com/docs/item/103',
357 'RegNow' => 'http://www.datafeedr.com/docs/item/111',
358 'RevResponse' => 'http://www.datafeedr.com/docs/item/197',
359 'ShareASale' => 'http://www.datafeedr.com/docs/item/108',
360 'SuperClix' => 'http://www.datafeedr.com/docs/item/256',
361 'TradeDoubler' => 'http://www.datafeedr.com/docs/item/113',
362 'TradeTracker' => 'http://www.datafeedr.com/docs/item/195',
363 'Webgains' => 'http://www.datafeedr.com/docs/item/114',
364 'Zanox' => 'http://www.datafeedr.com/docs/item/269',
365 );
366 return $links[$name];
367 }
368
369 function validate( $input ) {
370 $new_input['ids'] = array();
371 if ( isset( $input['ids'] ) ) {
372 foreach ( $input['ids'] as $k => $v ) {
373 if ( isset( $v['nid'] ) ) {
374 $new_input['ids'][$k] = $v;
375 }
376 }
377 }
378 return $new_input;
379 }
380
381 } // class Dfrapi_Networks
382
383 } // class_exists check
384