PluginProbe
Datafeedr API / 1.0.25
Datafeedr API v1.0.25
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.25, at classes/class-dfrapi-networks.php

388 lines 15.4 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 $use_cache = wp_using_ext_object_cache( false );
220 $adspaces = get_transient( $option_name );
221 wp_using_ext_object_cache( $use_cache );
222 if ( false === $adspaces || empty ( $adspaces ) ) {
223 $zanox_keys = dfrapi_get_zanox_keys();
224 if ( !$zanox_keys ) {
225 return array( 'zanox_error' => 'missing_keys' );
226 } else {
227 $client = new Dfr_ZanoxAPIClient( $zanox_keys['connection_key'], $zanox_keys['secret_key'] );
228 $adspaces = $client->adspaces();
229 if ( $client->error() ) {
230 return array( 'zanox_error' => $client->error() );
231 } else {
232 $use_cache = wp_using_ext_object_cache( false );
233 set_transient( $option_name, $adspaces, HOUR_IN_SECONDS );
234 wp_using_ext_object_cache( $use_cache );
235 }
236 }
237 }
238 dfrapi_update_transient_whitelist( $option_name );
239 return $adspaces;
240 }
241
242 function num_missing_affiliate_ids_in_group( $group_name ) {
243 $count = 0;
244 foreach ( $this->all_networks as $network ) {
245 if ( $network['group'] == $group_name ) {
246 if ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) {
247 if ( trim( $this->options['ids'][$network['_id']]['aid'] ) == '' ) {
248 $count++;
249 }
250 }
251 }
252 }
253
254 if ( $count > 0 ) {
255 $messages = $this->messages();
256 return '<span class="num_missing">' . sprintf( translate_nooped_plural( $messages['num_missing'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
257 }
258 }
259
260 function num_networks_checked_in_group( $group_name ) {
261 $count = 0;
262 foreach ( $this->all_networks as $network ) {
263 if ( $network['group'] == $group_name ) {
264 if ( array_key_exists( $network['_id'], (array) $this->options['ids'] ) ) {
265 $count++;
266 }
267 }
268 }
269
270 if ( $count > 0 ) {
271 $messages = $this->messages();
272 return '<span class="num_checked">' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
273 }
274 }
275
276 function num_networks_in_group( $group_name ) {
277 $count = 0;
278 foreach ( $this->all_networks as $network ) {
279 if ( $network['group'] == $group_name ) {
280 $count++;
281 }
282 }
283
284 if ( $count > 0 ) {
285 $messages = $this->messages();
286 return '<span class="num_networks">' . sprintf( translate_nooped_plural( $messages['num_networks'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
287 }
288 }
289
290 function num_merchants_in_group( $group_name ) {
291 $count = 0;
292 foreach ( $this->all_networks as $network ) {
293 if ( $network['group'] == $group_name ) {
294 $count += $network['merchant_count'];
295 }
296 }
297
298 if ( $count > 0 ) {
299 $messages = $this->messages();
300 return '<span class="num_merchants">' . sprintf( translate_nooped_plural( $messages['num_merchants'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
301 }
302 }
303
304 function num_products_in_group( $group_name ) {
305 $count = 0;
306 foreach ( $this->all_networks as $network ) {
307 if ( $network['group'] == $group_name ) {
308 $count += $network['product_count'];
309 }
310 }
311
312 if ( $count > 0 ) {
313 $messages = $this->messages();
314 return '<span class="num_products">' . sprintf( translate_nooped_plural( $messages['num_products'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span>';
315 }
316
317 }
318
319 function messages() {
320 return array(
321 'num_missing' => _n_noop( '%s missing affiliate ID', '%s missing affiliate IDs' ),
322 'num_checked' => _n_noop( '%s network selected', '%s networks selected' ),
323 'num_networks' => _n_noop( '%s network', '%s networks' ),
324 'num_merchants' => _n_noop( '%s merchant', '%s merchants' ),
325 'num_products' => _n_noop( '%s product', '%s products' ),
326 );
327 }
328
329 function map_link( $name ) {
330 $links = array(
331 'AdRecord' => 'http://www.datafeedr.com/docs/item/252',
332 'Adtraction' => 'http://www.datafeedr.com/docs/item/261',
333 'Affiliate4You' => 'http://www.datafeedr.com/docs/item/265',
334 'AffiliateWindow' => 'http://www.datafeedr.com/docs/item/101',
335 'Affiliator' => 'http://www.datafeedr.com/docs/item/270',
336 'Affilinet' => 'http://www.datafeedr.com/docs/item/104',
337 'Amazon Local' => 'http://www.datafeedr.com/docs/item/275',
338 'Avangate' => 'http://www.datafeedr.com/docs/item/188',
339 'AvantLink' => 'http://www.datafeedr.com/docs/item/190',
340 'Belboon' => 'http://www.datafeedr.com/docs/item/201',
341 'BettyMills' => 'http://www.datafeedr.com/docs/item/264',
342 'bol.com' => 'http://www.datafeedr.com/docs/item/253',
343 'ClickBank' => 'http://www.datafeedr.com/docs/item/106',
344 'ClixGalore' => 'http://www.datafeedr.com/docs/item/200',
345 'Commission Factory' => 'http://www.datafeedr.com/docs/item/193',
346 'Commission Junction' => 'http://www.datafeedr.com/docs/item/65',
347 'Commission Monster' => 'http://www.datafeedr.com/docs/item/109',
348 'Daisycon' => 'http://www.datafeedr.com/docs/item/196',
349 'DGM' => 'http://www.datafeedr.com/docs/item/263',
350 'Double.net' => 'http://www.datafeedr.com/docs/item/271',
351 'FlipKart' => 'http://www.datafeedr.com/docs/item/273',
352 'Impact Radius' => 'http://www.datafeedr.com/docs/item/268',
353 'LinkConnector' => 'http://www.datafeedr.com/docs/item/105',
354 'LinkShare' => 'http://www.datafeedr.com/docs/item/60',
355 'M4N' => 'http://www.datafeedr.com/docs/item/198',
356 'MyCommerce' => 'http://www.datafeedr.com/docs/item/111',
357 'OneNetworkDirect' => 'http://www.datafeedr.com/docs/item/112',
358 'Paid on Results' => 'http://www.datafeedr.com/docs/item/189',
359 'Partner-ads' => 'http://www.datafeedr.com/docs/item/262',
360 'PepperJam' => 'http://www.datafeedr.com/docs/item/103',
361 'RegNow' => 'http://www.datafeedr.com/docs/item/111',
362 'RevResponse' => 'http://www.datafeedr.com/docs/item/197',
363 'ShareASale' => 'http://www.datafeedr.com/docs/item/108',
364 'SuperClix' => 'http://www.datafeedr.com/docs/item/256',
365 'TradeDoubler' => 'http://www.datafeedr.com/docs/item/113',
366 'TradeTracker' => 'http://www.datafeedr.com/docs/item/195',
367 'Webgains' => 'http://www.datafeedr.com/docs/item/114',
368 'Zanox' => 'http://www.datafeedr.com/docs/item/269',
369 );
370 return $links[$name];
371 }
372
373 function validate( $input ) {
374 $new_input['ids'] = array();
375 if ( isset( $input['ids'] ) ) {
376 foreach ( $input['ids'] as $k => $v ) {
377 if ( isset( $v['nid'] ) ) {
378 $new_input['ids'][$k] = $v;
379 }
380 }
381 }
382 return $new_input;
383 }
384
385 } // class Dfrapi_Networks
386
387 } // class_exists check
388