'slug', 'title' => 'Affiliate Link Base', 'title_i18n' => true, 'type' => 'text', 'tab' => 'general', 'default' => 'go', 'description' => 'You can change the default base part \'%1$s/go/%2$s\' of your redirect link to something else', 'description_i18n' => true, 'description_has_sprintf' => true, ), array( 'name' => 'affiliate_api_key', 'title' => 'Wecantrack API Key', 'title_i18n' => true, 'type' => 'password', 'tab' => 'general', 'default' => '', 'description' => 'Enter your wecantrack API key to utilise our affiliate link generator.', 'description_i18n' => true, ), array( 'name' => 'category', 'title' => 'Show Category in Link URL', 'title_i18n' => true, 'type' => 'checkbox', 'tab' => 'general', 'description' => 'Show the link category slug in the affiliate link URL', 'description_i18n' => true, ), array( 'name' => 'default', 'title' => 'Default URL for Redirect', 'title_i18n' => true, 'type' => 'text', 'tab' => '', 'default' => get_home_url(), 'description' => 'Enter the default URL for redirect if correct URL not set', 'description_i18n' => true, ), array( 'name' => 'nofollow', 'title' => 'Nofollow Affiliate Links', 'title_i18n' => true, 'type' => 'checkbox', 'tab' => 'defaults', 'description' => 'Add "X-Robots-Tag: noindex, nofollow" to HTTP headers', 'description_i18n' => true, ), array( 'name' => 'redirect', 'title' => 'Redirect Type', 'title_i18n' => true, 'type' => 'radio', 'tab' => 'defaults', 'default' => '301', 'description' => 'Set redirection HTTP status code', 'description_i18n' => true, 'values' => array( '301' => array( 'label' => '301 Moved Permanently', 'i18n' => true ), '302' => array( 'label' => '302 Found', 'i18n' => true ), '307' => array( 'label' => '307 Temporary Redirect', 'i18n' => true ), ), ), ); } public static function get_default_tabs() { return array( 'general' => 'General', 'defaults' => 'Defaults', ); } /** * Apply translations to fields after text domain is loaded. */ public function apply_field_translations() { if ( self::$translations_applied ) { return; } foreach ( self::$fields as &$field ) { // Translate title if needed if ( ! empty( $field['title_i18n'] ) && $field['title_i18n'] === true ) { $field['title'] = __( $field['title'], 'affiliate-links' ); } // Translate description if needed if ( ! empty( $field['description_i18n'] ) && $field['description_i18n'] === true ) { if ( ! empty( $field['description_has_sprintf'] ) ) { $field['description'] = sprintf( /* translators: 1: Open tag strong 2: Close tag strong */ __( $field['description'], 'affiliate-links' ), '', '' ); } else { $field['description'] = __( $field['description'], 'affiliate-links' ); } } // Translate radio/select values if needed if ( ! empty( $field['values'] ) && is_array( $field['values'] ) ) { foreach ( $field['values'] as $key => &$value ) { if ( is_array( $value ) && ! empty( $value['i18n'] ) && $value['i18n'] === true ) { $field['values'][$key] = __( $value['label'], 'affiliate-links' ); } } } } self::$translations_applied = true; } public static function get_field( $field_name ) { foreach ( self::$fields as $field ) { if ( $field['name'] == $field_name ) { return $field; } } return ''; } public static function get_field_attr( $field_name, $attr ) { $field = self::get_field( $field_name ); if ( $field && isset( $field[ $attr ] ) ) { return $field[ $attr ]; } return ''; } public static function add_field( $field ) { // If translations have already been applied and the field contains translatable text, // apply translations immediately if ( self::$translations_applied ) { if ( ! empty( $field['title_i18n'] ) && $field['title_i18n'] === true ) { $field['title'] = __( $field['title'], 'affiliate-links' ); } if ( ! empty( $field['description_i18n'] ) && $field['description_i18n'] === true ) { $field['description'] = __( $field['description'], 'affiliate-links' ); } } array_push( self::$fields, $field ); } public function remove_filed( $field_name ) { foreach ( self::$fields as $key => $field ) { if ( $field['name'] == $field_name ) { unset( self::$fields[ $key ] ); } } } public static function add_tab( $tab_name, $tab_title ) { self::$tabs[ $tab_name ] = $tab_title; } public static function remove_tab( $tab_name ) { if ( isset( self::$tabs[ $tab_name ] ) ) { unset( self::$tabs[ $tab_name ] ); } } public function add_admin_menu() { add_submenu_page( 'edit.php?post_type=affiliate-links', 'Affiliate Links Settings', 'Settings', 'manage_options', self::SETTINGS_PAGE, array( $this, 'affiliate_links_options_page' ) ); } /** * Add settings links */ function add_action_links( $links ) { $links[] = '' . esc_html__( 'Settings', 'affiliate-links' ) . ''; return $links; } /** * Register plugin settings. */ public function settings_init() { $current_tab = $this->get_current_tab(); register_setting( self::SETTINGS_PAGE, 'affiliate_links_settings', array( $this, 'affiliate_links_save_value', ) ); add_settings_section( 'affiliate_links_' . $current_tab, '', array( $this, 'render_affiliate_links_' . $current_tab, ), self::SETTINGS_PAGE ); foreach ( self::$fields as $field ) { if ( isset( $field['tab'] ) && $current_tab == $field['tab'] ) { add_settings_field( $field['name'], $field['title'], // Already translated in apply_field_translations() array( $this, 'render_' . $field['type'] . '_field' ), self::SETTINGS_PAGE, self::SETTINGS_PAGE . '_' . $field['tab'], $field ); } } } public function affiliate_links_save_value( $input ) { $af_setting = get_option( 'affiliate_links_settings' ); $field_value = ! empty( $af_setting ) ? $af_setting : array(); $defaults = array( 'category' => array( 'tab' => 'general', 'value' => 0 ), 'nofollow' => array( 'tab' => 'defaults', 'value' => 0 ), ); foreach ( $defaults as $name => $data ) { if ( $data['tab'] == $this->get_current_tab() && ! isset( $input['category'] ) ) { unset( $field_value[ $name ] ); } if ( $data['tab'] == $this->get_current_tab() && ! isset( $input['nofollow'] ) ) { unset( $field_value[ $name ] ); } } $input = array_replace( $field_value, $input ); return $input; } public function render_affiliate_links_general() { submit_button(); } public function render_affiliate_links_defaults() { submit_button(); } /** * Generate text input field. */ public function render_text_field($args) { $value = self::get_option( $args['name'] ); ?>

array() ) ); ?>

> $value ) { ?> >

flush_rules(); $current_tab = $this->get_current_tab(); ?> '; $this->do_settings_fields( $page, $section['id'] ); echo ''; if ( $section['callback'] ) { call_user_func( $section['callback'], $section ); } } } public function do_settings_fields( $page, $section ) { global $wp_settings_fields; if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { return; } foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) { $class = ''; if ( ! empty( $field['args']['class'] ) ) { $class = ' class="' . esc_attr( $field['args']['class'] ) . '"'; } printf( '', $class ); if ( ! empty( $field['args']['label_for'] ) ) { echo ''; } else { echo '' . esc_html( $field['title'] ) . ''; } echo ''; call_user_func( $field['callback'], $field['args'] ); echo ''; echo ''; } } public function render_password_field($args) { $value = self::get_option($args['name']); ?>

array())); ?>

Affiliate_Links::$post_type, 'page' => self::SETTINGS_PAGE, 'tab' => $tab, ), admin_url( 'edit.php' ) ); } public static function get_option( $option ) { if ( isset( Affiliate_Links::$settings[ $option ] ) ) { return Affiliate_Links::$settings[ $option ]; } return self::get_field_attr( $option, 'default' ); } }