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