key = 'dfrapi_merchants'; $this->all_networks = dfrapi_api_get_all_networks(); $this->set_users_networks(); add_action( 'init', array( &$this, 'load_settings' ) ); add_action( 'admin_init', array( &$this, 'register_settings' ) ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'admin_notices', array( $this, 'admin_notice' ) ); } function admin_menu() { add_submenu_page( 'dfrapi', __( 'Merchants — Datafeedr API', DFRAPI_DOMAIN ), __( 'Merchants', DFRAPI_DOMAIN ), 'manage_options', $this->key, array( $this, 'output' ) ); } function admin_notice() { if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true && isset( $_GET['page'] ) && $this->key == $_GET['page'] ) { echo '

'; _e( 'Merchants successfully updated!', DFRAPI_DOMAIN ); echo '

'; } } function output() { echo '
'; echo '

' . dfrapi_setting_pages( $this->page ) . ' — Datafeedr API

'; if ( array_key_exists( 'dfrapi_api_error', $this->all_networks ) ) { dfrapi_output_api_error( $this->all_networks ); } else { echo '
'; submit_button(); wp_nonce_field( 'update-options' ); settings_fields( $this->page ); do_settings_sections( $this->page); submit_button(); echo '
'; echo ''; } echo '
'; } function load_settings() { $this->options = (array) get_option( $this->key ); if ( is_string( $this->options['ids'] ) && ( $this->options['ids'] != '' ) ) { $this->options['ids'] = explode( ",", $this->options['ids'] ); } elseif ( $this->options['ids'] == NULL ) { $this->options['ids'] = array(); } } function register_settings() { register_setting( $this->page, $this->key, array( $this, 'validate' ) ); add_settings_section( 'merchants', __( 'Select Merchants', DFRAPI_DOMAIN ), array( &$this, 'section_merchants_desc' ), $this->page ); add_settings_field( 'ids', __( 'Merchants', DFRAPI_DOMAIN ), array( &$this, 'field_merchant_ids' ), $this->page, 'merchants', array( 'label_for' => 'DFRAPI_HIDE_LABEL' ) ); } function section_merchants_desc() { echo __( 'Select merchants from your affiliate networks then click [Save Changes].', DFRAPI_DOMAIN ); } function field_merchant_ids() { if ( is_array( $this->options['ids'] ) && !empty( $this->options['ids'] ) ) { $ids = htmlspecialchars(implode(',', $this->options['ids'])); } else { $ids = ''; } echo ""; foreach ( $this->users_networks as $user_network ) { $network = $this->get_network_info( $user_network ); if ( empty( $network ) ) { _e( 'No networks have been selected.', DFRAPI_DOMAIN ); break; } $num_networks_checked_in_network = $this->num_networks_checked_in_network( $network['_id'] ); $active = ( preg_match( "/num_checked_none/", $num_networks_checked_in_network ) ) ? '' : 'active'; echo '
' . $network['name'] . ' ' . $num_networks_checked_in_network . ' ' . $this->num_merchants_in_network( $network ) . ' ' . $this->num_products_in_network( $network ) . '
' . $this->list_merchants( $network['_id'] ) . '
'; } } function format_merchant($merchant, $selected) { $no_products = ( $merchant['product_count'] < 1 ) ? 'no_products hidden' : ''; $button = '' . __( 'Click to remove', DFRAPI_DOMAIN ) . "" . '' . __( 'Click to add', DFRAPI_DOMAIN ) . ""; return '
' . $button . '
' . $merchant['name'] . '
' . $this->num_products_in_network( $merchant ) . '
'; } function list_merchants( $network_id ) { $merchants = dfrapi_api_get_all_merchants( $network_id ); if ( array_key_exists( 'dfrapi_api_error', $merchants ) ) { return dfrapi_html_output_api_error( $merchants ); } $html = ' "; return $html; } function get_network_info( $network_id ) { foreach ( $this->all_networks as $network ) { if ( $network['_id'] == $network_id ) { return $network; } } } function set_users_networks() { $network_ids = array(); $networks = (array) get_option( 'dfrapi_networks' ); foreach ( $networks['ids'] as $network ) { if ( isset( $network['nid'] ) && !empty( $network['nid'] ) ) { $network_ids[] = $network['nid']; } } $this->users_networks = $network_ids; } function num_networks_checked_in_network( $network_id ) { $count = 0; $merchants = dfrapi_api_get_all_merchants( $network_id ); foreach ( $merchants as $merchant) { if ( $merchant['source_id'] == $network_id ) { if ( in_array( $merchant['_id'], $this->options['ids'] ) ) { $count++; } } } $messages = $this->messages(); if ( $count > 0 ) { return '' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . ' / '; } else { return '' . sprintf( translate_nooped_plural( $messages['num_checked'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . ' / '; } } function num_merchants_in_network( $network ) { $count = ( $network['merchant_count'] > 0 ) ? $network['merchant_count'] : 0; if ( $count > 0 ) { $messages = $this->messages(); return '' . sprintf( translate_nooped_plural( $messages['num_merchants'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . ' / '; } } function num_products_in_network( $network ) { $count = ( $network['product_count'] > 0 ) ? $network['product_count'] : 0; $messages = $this->messages(); return '' . sprintf( translate_nooped_plural( $messages['num_products'], $count, DFRAPI_DOMAIN ), number_format( $count ) ) . ''; } function validate( $input ) { if ( !isset( $input ) || !is_array( $input ) || empty( $input ) ) { return $input; } $new_input = array(); foreach( $input as $key => $value ) { // Validate "ids" if ( $key == 'ids' ) { if ( is_array( $value ) ) { $new_input['ids'] = $value; } else { if ( trim( $value ) == '' ) { $new_input['ids'] = array(); } else { $new_input['ids'] = explode( ",", $value ); } } } } // foreach return $new_input; } function messages() { return array( 'num_merchants' => _n_noop( '%s merchant', '%s merchants' ), 'num_products' => _n_noop( '%s product', '%s products' ), 'num_checked' => _n_noop( '%s merchant selected', '%s merchants selected' ), ); } } // class Dfrapi_Merchants } // class_exists check