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-merchants.php

class-dfrapi-merchants.php in Datafeedr API 1.0.25, at classes/class-dfrapi-merchants.php

296 lines 10.3 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_Merchants' ) ) {
6
7 /**
8 * Configuration page.
9 */
10 class Dfrapi_Merchants {
11
12 private $page = 'dfrapi-merchants';
13 private $key;
14 private $all_networks;
15 private $users_networks;
16
17 public function __construct() {
18 $this->key = 'dfrapi_merchants';
19 $this->all_networks = dfrapi_api_get_all_networks();
20 $this->set_users_networks();
21 add_action( 'init', array( &$this, 'load_settings' ) );
22 add_action( 'admin_init', array( &$this, 'register_settings' ) );
23 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
24 add_action( 'admin_notices', array( $this, 'admin_notice' ) );
25 }
26
27 function admin_menu() {
28 add_submenu_page(
29 'dfrapi',
30 __( 'Merchants &#8212; Datafeedr API', DFRAPI_DOMAIN ),
31 __( 'Merchants', DFRAPI_DOMAIN ),
32 'manage_options',
33 $this->key,
34 array( $this, 'output' )
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( 'Merchants successfully updated!', DFRAPI_DOMAIN );
42 echo '</p></div>';
43 }
44 }
45
46 function output() {
47 echo '<div class="wrap" id="' . $this->key . '">';
48 echo '<h2>' . dfrapi_setting_pages( $this->page ) . ' &#8212; Datafeedr API</h2>';
49 if ( array_key_exists( 'dfrapi_api_error', $this->all_networks ) ) {
50 dfrapi_output_api_error( $this->all_networks );
51 } else {
52 echo '<form method="post" action="options.php">';
53 submit_button();
54 wp_nonce_field( 'update-options' );
55 settings_fields( $this->page );
56 do_settings_sections( $this->page);
57 submit_button();
58 echo '</form>';
59 echo '<div id="dfr_unload_message" style="display:none">' . __( 'You have unsaved changes', DFRAPI_DOMAIN ) . '</div>';
60 }
61 echo '</div>';
62 }
63
64 function load_settings() {
65 $this->options = (array) get_option( $this->key );
66 if ( is_string( $this->options['ids'] ) && ( $this->options['ids'] != '' ) ) {
67 $this->options['ids'] = explode( ",", $this->options['ids'] );
68 } elseif ( $this->options['ids'] == NULL ) {
69 $this->options['ids'] = array();
70 }
71 }
72
73 function register_settings() {
74 register_setting( $this->page, $this->key, array( $this, 'validate' ) );
75 add_settings_section( 'merchants', __( 'Select Merchants', DFRAPI_DOMAIN ), array( &$this, 'section_merchants_desc' ), $this->page );
76 add_settings_field( 'ids', __( 'Merchants', DFRAPI_DOMAIN ), array( &$this, 'field_merchant_ids' ), $this->page, 'merchants', array( 'label_for' => 'DFRAPI_HIDE_LABEL' ) );
77 }
78
79 function section_merchants_desc() {
80 echo __( 'Select merchants from your affiliate networks then click <strong>[Save Changes]</strong>.', DFRAPI_DOMAIN );
81 }
82
83 function field_merchant_ids() {
84 if ( is_array( $this->options['ids'] ) && !empty( $this->options['ids'] ) ) {
85 $ids = htmlspecialchars(implode(',', $this->options['ids']));
86 } else {
87 $ids = '';
88 }
89
90 echo "<input type='hidden' id='ids' name='".$this->key."[ids]' value='$ids' />";
91
92 foreach ( $this->users_networks as $user_network ) {
93 $network = $this->get_network_info( $user_network );
94
95 if ( empty( $network ) ) {
96 _e( 'No networks have been selected.', DFRAPI_DOMAIN );
97 break;
98 }
99
100 $num_networks_checked_in_network = $this->num_networks_checked_in_network( $network['_id'] );
101 $active = ( preg_match( "/num_checked_none/", $num_networks_checked_in_network ) ) ? '' : 'active';
102 echo '
103 <div class="network network_logo_30x30_' . dfrapi_group_name_to_css( $network ) . ' ' . $active .'" id="network_' . $network['_id'] . '">
104 <div class="meta">
105 <span class="name">' . $network['name'] . '</span>
106 <span class="status">
107 ' . $num_networks_checked_in_network . '
108 ' . $this->num_merchants_in_network( $network ) . '
109 ' . $this->num_products_in_network( $network ) . '
110 </span>
111 </div>
112 ' . $this->list_merchants( $network['_id'] ) . '
113 </div>
114 ';
115 }
116 }
117
118 function format_merchant($merchant, $selected) {
119
120 $no_products = ( $merchant['product_count'] < 1 ) ? 'no_products hidden' : '';
121
122 $button = '<span class="merchant_hint_remove">' . __( 'Click to remove', DFRAPI_DOMAIN ) . "</span>"
123 . '<span class="merchant_hint_add">' . __( 'Click to add', DFRAPI_DOMAIN ) . "</span>";
124
125 return '
126 <div class="merchant ' . $no_products . '" id="merchant_id_' . $merchant['_id'] . '">
127 <div class="merchant_hint">
128 ' . $button . '
129 </div>
130 <div class="merchant_name">
131 ' . $merchant['name'] . '
132 </div>
133 <div class="merchant_info">
134 ' . $this->num_products_in_network( $merchant ) . '
135 </div>
136 </div>
137 ';
138 }
139
140 function list_merchants( $network_id ) {
141
142 $merchants = dfrapi_api_get_all_merchants( $network_id );
143
144 if ( array_key_exists( 'dfrapi_api_error', $merchants ) ) {
145 return dfrapi_html_output_api_error( $merchants );
146 }
147
148 $html = '
149 <div style="display:none;" class="merchants" id="merchants_for_nid_' . $network_id . '">
150 <div class="merchant_actions">
151 <span class="filter_action">
152 ' . __( 'Search', DFRAPI_DOMAIN ) . ': <input type="text">
153 <a class="reset_search button" title="' . __( 'Clear search', DFRAPI_DOMAIN ) . '">&times;</a>
154 </span>
155 <span class="sep">|</span>
156 <span class="hide_action">
157 <a style="display:none" class="hide_empty_merchants button">' . __( 'Hide Empty Merchants', DFRAPI_DOMAIN ) . '</a>
158 <a class="show_empty_merchants button">' . __( 'Show Empty Merchants', DFRAPI_DOMAIN ) . '</a>
159 </span>
160 </div>
161 ';
162
163 $left = $right = '';
164
165 foreach ( $merchants as $merchant ) {
166 $selected = in_array( $merchant['_id'], $this->options['ids'] );
167 $row = $this->format_merchant($merchant, $selected);
168
169 if ( $selected ) {
170 $right .= $row;
171 } else {
172 $left .= $row;
173 }
174 } // foreach ( $merchants as $merchant )
175
176 $html .= '
177 <div class="dfrapi_panes">
178 <div class="dfrapi_pane_left">
179 <div class="dfrapi_pane_title">
180 <span>' . __( 'Available Merchants', DFRAPI_DOMAIN ) . '</span>
181 <a class="add_all button">' . __( 'Add All', DFRAPI_DOMAIN ) . '</a>
182 </span>
183
184 </div>
185 <div class="dfrapi_pane_content">
186 ' . $left . '
187 </div>
188 </div>
189 <div class="dfrapi_pane_right">
190 <div class="dfrapi_pane_title">
191 <span>' . __( 'Selected Merchants', DFRAPI_DOMAIN ) . '</span>
192 <a class="remove_all button">' . __( 'Remove All', DFRAPI_DOMAIN ) . '</a>
193 </div>
194 <div class="dfrapi_pane_content">
195 ' . $right . '
196 </div>
197 </div>
198 </div>
199 ';
200
201 $html .= "</div>";
202
203 return $html;
204 }
205
206 function get_network_info( $network_id ) {
207 foreach ( $this->all_networks as $network ) {
208 if ( $network['_id'] == $network_id ) {
209 return $network;
210 }
211 }
212 }
213
214 function set_users_networks() {
215 $network_ids = array();
216 $networks = (array) get_option( 'dfrapi_networks' );
217 foreach ( $networks['ids'] as $network ) {
218 if ( isset( $network['nid'] ) && !empty( $network['nid'] ) ) {
219 $network_ids[] = $network['nid'];
220 }
221 }
222 $this->users_networks = $network_ids;
223 }
224
225 function num_networks_checked_in_network( $network_id ) {
226 $count = 0;
227 $merchants = dfrapi_api_get_all_merchants( $network_id );
228 foreach ( $merchants as $merchant) {
229 if ( $merchant['source_id'] == $network_id ) {
230 if ( in_array( $merchant['_id'], $this->options['ids'] ) ) {
231 $count++;
232 }
233 }
234 }
235
236 $messages = $this->messages();
237
238 if ( $count > 0 ) {
239 return '<span class="num_checked_some">' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
240 } else {
241 return '<span class="num_checked_none">' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
242 }
243 }
244
245 function num_merchants_in_network( $network ) {
246 $count = ( $network['merchant_count'] > 0 ) ? $network['merchant_count'] : 0;
247 if ( $count > 0 ) {
248 $messages = $this->messages();
249 return '<span class="num_merchants">' . sprintf( translate_nooped_plural( $messages['num_merchants'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span> <span class="sep">/</span> ';
250 }
251 }
252
253 function num_products_in_network( $network ) {
254 $count = ( $network['product_count'] > 0 ) ? $network['product_count'] : 0;
255 $messages = $this->messages();
256 return '<span class="num_products">' . sprintf( translate_nooped_plural( $messages['num_products'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . '</span>';
257 }
258
259 function validate( $input ) {
260
261 if ( !isset( $input ) || !is_array( $input ) || empty( $input ) ) { return $input; }
262
263 $new_input = array();
264
265 foreach( $input as $key => $value ) {
266
267 // Validate "ids"
268 if ( $key == 'ids' ) {
269 if ( is_array( $value ) ) {
270 $new_input['ids'] = $value;
271 } else {
272 if ( trim( $value ) == '' ) {
273 $new_input['ids'] = array();
274 } else {
275 $new_input['ids'] = explode( ",", $value );
276 }
277 }
278 }
279
280 } // foreach
281
282 return $new_input;
283 }
284
285 function messages() {
286 return array(
287 'num_merchants' => _n_noop( '%s merchant', '%s merchants' ),
288 'num_products' => _n_noop( '%s product', '%s products' ),
289 'num_checked' => _n_noop( '%s merchant selected', '%s merchants selected' ),
290 );
291 }
292
293 } // class Dfrapi_Merchants
294
295 } // class_exists check
296