PluginProbe
Datafeedr API / 1.0.125
Datafeedr API v1.0.125
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.125, at classes/class-dfrapi-merchants.php

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