settings_base = 'wpaf_'; /** * Initializes the settings. * * @since 1.0 */ add_action( 'admin_init', array( $this, 'init' ) ); /** * Registering the plugin settings. * * @since 1.0 */ add_action( 'admin_init', array( $this, 'register_settings' ) ); /** * Adding the settings page to the menu. * * @since 1.0 */ add_action( 'admin_menu', array( $this, 'add_menu_item' ) ); /** * Adding the links to plugins list page. * * @since 1.0 */ add_filter( 'plugin_action_links', array( __CLASS__, 'add_settings_link' ), 10, 2 ); /** * Load stylesheets and scripts. * * @since 1.0 */ add_action( 'admin_enqueue_scripts', array( $this, 'load_stylesheet_script' ) ); } /** * Add settings link to plugin list table. * * @param array $links Existing links * * @return array Modified links * @since 1.0 */ public static function add_settings_link( $links, $file ) { static $plugin; if ( ! $plugin ) { $plugin = defined( 'AFFCOUPON_FILE' ) ? plugin_basename( AFFCOUPON_FILE ) : 'affcoupon/affcoupon.php'; } if ( $file == $plugin ) { $settings_link = '' . esc_html__( 'Settings', 'affcoupon' ) . ''; array_unshift( $links, $settings_link ); } return $links; } /** * Initialise settings by adding the fields. * * @return void * @since 1.0 */ public function init() { $this->settings = $this->settings_fields(); } /** * Build settings fields. * * @return array Fields to be displayed on settings page * @since 1.0 */ private function settings_fields() { global $wp_post_statuses; $post_statuses = []; foreach ( $wp_post_statuses as $key => $val ) { $post_statuses[ $key ] = $key; } // currently available template names $template_names = WPAF_Form_Shortcode_Pro::getAvailableTemplateNames( WPAF_Meta_Boxes_Fields_Pro::getFields() ); $template_names_keyed = []; $template_names_keyed['all'] = 'all'; foreach ( $template_names as $name ) { $template_names_keyed[ $name ] = $name; } /** * Tabs setting. * * @since 1.0 */ $settings['tabs'] = array( array( 'id' => 'general', 'title' => __( 'General Settings', 'affcoupon' ), 'description' => __( 'These are some general coupon settings. You can use the default settings or set your own ones.', 'affcoupon' ) ), array( 'id' => 'design', 'title' => __( 'Coupons Design', 'affcoupon' ), 'description' => __( 'Coupons Design for coupon templates and other elements.', 'affcoupon' ) ), array( 'id' => 'hide-coupon', 'title' => __( 'Show/Hide Settings', 'affcoupon' ), 'description' => __( 'Settings when coupon is hidden.', 'affcoupon' ) ), array( 'id' => 'voting', 'title' => __( 'Share and Voting', 'affcoupon' ), 'description' => __( 'Configure Voting Settings for Your Coupons.', 'affcoupon' ) ), array( 'id' => 'pop-up', 'title' => __( 'Pop-up Settings', 'affcoupon' ), 'description' => __( 'Settings for pop up which opens when user clicks to show code.', 'affcoupon' ), ), array( 'id' => 'settings-extra', 'title' => __( 'Extras', 'affcoupon' ), 'description' => __( 'These are some extra settings. You can use the default settings or set your own ones.', 'affcoupon' ) ), array( 'id' => 'form-shortcode-settings', 'title' => __( 'Form Shortcode', 'affcoupon' ), 'description' => __( 'Form shortcode settings', 'affcoupon' ) ) ); /** * Tabs content. * * @since 1.0 */ //each array is a content to a tab $settings['tabs_content'] = array( array( array( 'id' => 'featured-image', 'label' => __( 'Featured Image', 'affcoupon' ), 'description' => __( 'If there\'s no featured image selected, choose whether to use Category Image or Vendor Image as the featured image.', 'affcoupon' ), 'type' => 'select', 'options' => array( 'category' => __( 'Category Image', 'affcoupon' ), 'vendor' => __( 'Vendor Image', 'affcoupon' ) ), 'default' => 'category' ), array( 'id' => 'archive-munu-categories', 'label' => __( 'Archive Menu Categories', 'affcoupon' ), 'description' => __( 'Choose the Type of Navigation on Category or Vendor for Archive.', 'affcoupon' ), 'type' => 'select', 'options' => array( 'category' => __( 'Category Navigation', 'affcoupon' ), 'vendor' => __( 'Vendor Navigation', 'affcoupon' ) ), 'default' => 'category' ), array( 'id' => 'words-count', 'label' => __( 'Words Count to Add More/Less Link', 'affcoupon' ), 'description' => __( 'If coupon description is more than this count, More/Less link will be added. Default is 30 words.', 'affcoupon' ), 'type' => 'number', 'default' => 30, 'placeholder' => 30, ), array( 'id' => 'coupon-hover-text', 'label' => __( 'Coupon Button Hover Text', 'affcoupon' ), 'description' => __( 'Text to show when user hovers on the coupon button. Default is Click To Copy Coupon', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Click To Copy Coupon', 'affcoupon' ), ), array( 'id' => 'deal-hover-text', 'label' => __( 'Deal Button Hover Text', 'affcoupon' ), 'description' => __( 'Text to show when user hovers on the deal button. Default is Click Here To Get This Deal', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Click Here To Get This Deal', 'affcoupon' ), ), array( 'id' => 'expire-text', 'label' => __( 'Expire Text', 'affcoupon' ), 'description' => __( 'Text to show before expire date. Default is \'Expires on:\' ', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Expires on:', 'affcoupon' ) ), array( 'id' => 'expired-text', 'label' => __( 'Expired Text', 'affcoupon' ), 'description' => __( 'Text to show before expired date. Default is \'Expired on:\' ', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Expired on:', 'affcoupon' ) ), array( 'id' => 'no-expiry-message', 'label' => __( 'No Expiration Text', 'affcoupon' ), 'description' => __( 'Text to show if coupon or deal never expires. Default is \'Doesn\'t expire\'.', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Doesn\'t Expire', 'affcoupon' ) ), array( 'id' => 'expiry-date-format', 'label' => __( 'Expiration Date Format', 'affcoupon' ), 'description' => __( 'Choose the date format for expiration date.', 'affcoupon' ), 'type' => 'select', 'options' => array( 'dd-mm-yy' => 'dd-mm-yy', 'mm/dd/yy' => 'mm/dd/yy', 'yy/mm/dd' => 'yy/mm/dd' ), 'default' => 'mm/dd/yy' ), array( 'id' => 'hide-expired-coupon', 'label' => __( 'Hide expired Coupon', 'affcoupon' ), 'type' => 'checkbox', 'description' => __( 'Hide Coupon when it\'s expired. Default - Not to hide', 'affcoupon' ), 'default' => '' ), array( 'id' => 'coupon-title-tag', 'label' => __( 'Coupon Title Tag', 'affcoupon' ), 'description' => __( 'Choose the heading tag to be used for Coupon Title', 'affcoupon' ), 'type' => 'select', 'options' => array( 'h1' => 'H1', 'h2' => 'H2', 'h3' => 'H3', 'h4' => 'H4', 'h5' => 'H5', 'h6' => 'H6', ), 'default' => 'h1', ), array( 'id' => 'coupon-print-link', 'label' => __( 'Show Print Links', 'affcoupon' ), 'description' => __( 'Enable Print Links for Coupons, Deals and Images', 'affcoupon' ), 'type' => 'checkbox', 'default' => '' ), ), array( array( 'id' => 'coupon-type-bg-color', 'label' => __( 'Coupon Type Color', 'affcoupon' ), 'description' => __( 'Coupon Type Background Color in Default Template.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#21ba45' ), array( 'id' => 'deal-type-bg-color', 'label' => __( 'Deals Type Color', 'affcoupon' ), 'description' => __( 'Deals Type Background Color in Default Template.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#ea4c89' ), array( 'id' => 'dt-border-color', 'label' => __( 'Border Color', 'affcoupon' ), 'description' => __( 'Border Color in Default Template.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#000000' ), array( 'id' => 'coupon-border-radius', 'label' => __( 'Border Radious', 'affcoupon' ), 'description' => __( 'Border Radious (PX) in Default Template.', 'affcoupon' ), 'type' => 'number', 'default' => '#000000', 'placeholder' => __( '8', 'affcoupon' ), 'default' => '8' ), array( 'id' => 'pagination-color', 'label' => __( 'Pagination Color', 'affcoupon' ), 'description' => __( 'Color for Pagination Numbers.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#56b151' ), array( 'id' => 'hidden-coupon-button-color', 'label' => __( 'Hidden Coupon Button Color', 'affcoupon' ), 'description' => __( 'The color of the button when coupon is hidden.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#2185d0', ), array( 'id' => 'copy-button-bg-color', 'label' => __( 'Copy Button Color', 'affcoupon' ), 'description' => __( 'Background color for the copy button in Pop Up.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#20e8ac' ), array( 'id' => 'coupon-popup-bg-color', 'label' => __( 'Coupon Code Background Color', 'affcoupon' ), 'description' => __( 'Background color of the coupon code in the Pop up. Pop up is shown when users click to show code.', 'affcoupon' ), 'type' => 'colorpicker', 'default' => '#20e8ac30' ), ), array( array( 'id' => 'hidden-coupon-text', 'label' => __( 'Hidden Coupon Text', 'affcoupon' ), 'description' => __( 'Text to show when coupon is hidden. Default is \'Show Code\' ', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Show Code', 'affcoupon' ), ), array( 'id' => 'hidden-coupon-hover-text', 'label' => __( 'Hidden Coupon Hover Text', 'affcoupon' ), 'description' => __( 'Text to show when user hovers on the hidden coupon. Default is \'Click Here to Show Code\'', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Click Here to Show Code', 'affcoupon' ), ), ), array( array( 'id' => 'coupon-social-share', 'label' => __( 'Social Share Buttons', 'affcoupon' ), 'description' => __( 'Enable Social Share Buttons in Coupons', 'affcoupon' ), 'type' => 'checkbox', 'default' => '' ), array( 'id' => 'coupon-vote-system', 'label' => __( 'Vote Buttons', 'affcoupon' ), 'description' => __( 'Enable Voting Buttons in Coupons', 'affcoupon' ), 'type' => 'checkbox', 'default' => '' ), array( 'id' => 'coupon-vote-success', 'label' => __( 'Voting Success Message', 'affcoupon' ), 'description' => __( 'Message to Show After User has Voted Successfully', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'You have voted successfully!', 'affcoupon' ) ), array( 'id' => 'coupon-vote-fail', 'label' => __( 'Voting Failed Message', 'affcoupon' ), 'description' => __( 'Message to Show If Voting Fails', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Voting Failed!', 'affcoupon' ) ), array( 'id' => 'coupon-vote-already', 'label' => __( 'Already Voted Message', 'affcoupon' ), 'description' => __( 'Message to Show If User has Voted Already', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'You have voted already!', 'affcoupon' ) ) ), array( array( 'id' => 'copy-button-text', 'label' => __( 'Copy Button Text', 'affcoupon' ), 'description' => __( 'Text to show in Copy button when pop up is shown. Default is \'Copy\'.', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Copy', 'affcoupon' ), ), array( 'id' => 'after-copy-text', 'label' => __( 'After Copy Text', 'affcoupon' ), 'description' => __( 'Text to show after coupon is copied. Default is \'Copied\'.', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Copied', 'affcoupon' ), ), array( 'id' => 'text-to-show', 'label' => __( 'Text to Show Below Coupon Code', 'affcoupon' ), 'description' => __( 'Select what to show below the coupon code in Pop-up. Default is Description.', 'affcoupon' ), 'type' => 'select', 'options' => array( 'description' => 'Description', 'custom-text' => 'Custom Text' ), 'default' => 'description' ), array( 'id' => 'custom-text', 'label' => __( 'Custom Text', 'affcoupon' ), 'description' => __( 'Custom text to show below the coupon code. Default is \'Click on Copy to Copy the Coupon Code.\' ', 'affcoupon' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'Click on Copy to Copy the Coupon Code.', 'affcoupon' ), ), array( 'id' => 'popup-goto-link', 'label' => __( 'Enable \'Go To Offer\' button', 'affcoupon' ), 'type' => 'checkbox', 'description' => __( 'Show a button with a link to offer or affiliate link site. Default is \'Go To Offer\'.', 'affcoupon' ), 'default' => 'on' ), array( 'id' => 'popup-goto-custom-text', 'label' => __( 'Custom Text for Button', 'affcoupon' ), 'type' => 'text', 'description' => __( 'Custom text to show instead of \'Go to Offer\'.', 'affcoupon' ), 'placeholder' => __( 'Go To Offer', 'affcoupon' ), 'default' => '' ), ), array( array( 'id' => 'coupon-link-target', 'label' => __( 'Affiliate Link in Current Tab', 'affcoupon' ), 'description' => __( 'Enabling it will open the affiliate link in current tab. By default it opens in a new tab.', 'affcoupon' ), 'type' => 'checkbox', 'default' => '' ), array( 'id' => 'coupon-link-featured-img', 'label' => __( 'Link the Featured Image', 'affcoupon' ), 'description' => __( 'Enabling it will link the featured image to affiliate link. By default only the image is shown, it is not linked.', 'affcoupon' ), 'type' => 'checkbox', 'default' => '' ), array( 'id' => 'disable-coupon-title-link', 'label' => __( 'Disable Link in Coupon Title', 'affcoupon' ), 'description' => __( 'Disable the coupon title link. By default it\'s linked to the link/affiliate link you put when you create a coupon.', 'affcoupon' ), 'type' => 'checkbox', 'default' => '', ), array( 'id' => 'disable-menu-archive-code', 'label' => __( 'Disable Navigation', 'affcoupon' ), 'description' => __( 'Disable the categories navigation in archive shortcode.', 'affcoupon' ), 'type' => 'checkbox', 'default' => '', ), array( 'id' => 'hide-archive-thumbnail', 'label' => __( 'Hide Featured Image', 'affcoupon' ), 'description' => __( 'Hide the featured image in Default Grid Style of Category and Archive Shortcode.', 'affcoupon' ), 'type' => 'checkbox', 'default' => '', ), array( 'id' => 'dt-coupon-type-text', 'label' => __( 'Coupon Type Name', 'affcoupon' ), 'description' => __( 'Text to Show for Coupon Type Name in Default Template. Default is - Coupon.', 'affcoupon' ), 'type' => 'text', 'default' => __( 'Coupon', 'affcoupon' ), 'placeholder' => __( 'Coupon', 'affcoupon' ) ), array( 'id' => 'dt-deal-type-text', 'label' => __( 'Deal Type Name', 'affcoupon' ), 'description' => __( 'Text to Show for Deal Type Name in Default Template. Default is - Deal.', 'affcoupon' ), 'type' => 'text', 'default' => __( 'Deal', 'affcoupon' ), 'placeholder' => __( 'Deal', 'affcoupon' ) ), array( 'id' => 'all-coupon-text', 'label' => __( 'All Coupons Text', 'affcoupon' ), 'description' => __( '"All Coupons" text in Archive Shortcode Navigation menu. Deafult is - All Coupons', 'affcoupon' ), 'type' => 'text', 'placeholder' => __( 'All Coupons', 'affcoupon' ), 'default' => '' ), ), array( array( 'id' => 'form-shortcode-allowed-roles', 'label' => __( 'Allowed User Roles', 'affcoupon' ), 'description' => __( 'Allowed user roles to submit coupons using the form.', 'affcoupon' ), 'type' => 'checkbox_multi', 'options' => wp_roles()->role_names, 'default' => [ 'administrator' ], ), array( 'id' => 'form-shortcode-coupon-status', 'label' => __( 'Coupon Status', 'affcoupon' ), 'description' => __( 'Status of user submitted coupons, default is "publish".', 'affcoupon' ), 'type' => 'select', 'options' => $post_statuses, 'default' => 'publish' ), array( 'id' => 'form-shortcode-split-form', 'label' => __( 'Split Form', 'affcoupon' ), 'description' => __( 'Split the form into smaller segments or show all fields.', 'affcoupon' ), 'type' => 'select', 'options' => array( 'full' => 'full', 'split' => 'split' ), 'default' => 'split' ), array( 'id' => 'form-shortcode-enable-thrash', 'label' => __( 'Enable Coupon Delete', 'affcoupon' ), 'description' => __( 'Give the users the ability to delete their own coupons.', 'affcoupon' ), 'type' => 'checkbox', 'default' => '', ), array( 'id' => 'form-shortcode-enable-new-terms', 'label' => __( 'Enable New Term Creation', 'affcoupon' ), 'description' => __( 'Give the users the ability to create new terms (Categories & Vendors).', 'affcoupon' ), 'type' => 'checkbox', 'default' => '', ), array( 'id' => 'form-shortcode-default-template', 'label' => __( 'Default Template', 'affcoupon' ), 'description' => __( 'Default template for users to use. Select "all" to enable selection from available templates.', 'affcoupon' ), 'type' => 'select', 'options' => $template_names_keyed, 'default' => 'all' ), ) ); $settings = apply_filters( 'wpaf_coupon_settings_fields', $settings ); $settings = apply_filters( 'wpaf_coupon_settings_fields', $settings ); return $settings; } /** * Add settings page to admin menu * @return void */ public function add_menu_item() { global $wpaf_settings_page; /** * Adding the settings page under our main menu item. * * @since 1.0 */ $wpaf_settings_page = add_submenu_page( 'edit.php?post_type=wpaf_coupons', __( 'AffCoupon Settings', 'affcoupon' ), __( 'Settings', 'affcoupon' ), 'manage_options', 'wpaf_coupon_settings', array( $this, 'settings_page' ) ); } /** * Loads the stylesheets on the settings page. * * @param $hook * * @since 1.0.2 */ public function load_stylesheet_script( $hook ) { global $wpaf_settings_page; if ( $hook != $wpaf_settings_page ) { return; } else { wp_enqueue_style( 'wpaf-admin-style', WPAF_Plugin::instance()->plugin_assets . 'admin/css/' . WPAF_Assets::wpaf_version_correct( 'dir' ) . 'admin' . WPAF_Assets::wpaf_version_correct( 'suffix' ) . '.css', false, WPAF_Plugin::PLUGIN_VERSION ); // color Picker wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); } } /** * Register plugin settings. * * @return void * @since 1.0 */ public function register_settings() { /** * Registering settings, adding section. * * @since 1.0 */ if ( is_array( $this->settings ) ) { /** * display tabs * @since 1.0 */ //to cover tabs with div global $wpaf_last; $wpaf_last = count( $this->settings['tabs'] ); $i = 0; // display tabs foreach ( $this->settings['tabs'] as $data ) { add_settings_section( $i, '', array( $this, 'settings_section' ), 'wpaf_settings' ); $i ++; } /** * display tabs content * @since 1.0 */ $i = 0; foreach ( $this->settings['tabs_content'] as $tab_content ) { foreach ( $tab_content as $field ) { $validation = ''; if ( isset( $field['callback'] ) ) { $validation = $field['callback']; } else { $validation = function ( $value ) use ( $field ) { return $this->sanitize_setting_field( $value, $field ); }; } $option_name = $this->settings_base . $field['id']; register_setting( 'wpaf_settings', $option_name, array( 'sanitize_callback' => $validation, ) ); add_settings_field( $field['id'], $field['label'], array( $this, 'display_field' ), 'wpaf_settings', $i, array( 'field' => $field ) ); } $i ++; } } } /** * Sanitize settings values by field definition. * * @param mixed $value Submitted value. * @param array $field Field definition. * * @return mixed */ private function sanitize_setting_field( $value, $field ) { $type = isset( $field['type'] ) ? $field['type'] : 'text'; if ( is_array( $value ) && ! in_array( $type, array( 'checkbox_multi', 'select_multi' ), true ) ) { $value = reset( $value ); } switch ( $type ) { case 'number': return absint( $value ); case 'colorpicker': return WPAF_Assets::wpaf_sanitize_css_color( $value ); case 'checkbox': return 'on' === $value ? 'on' : ''; case 'checkbox_multi': case 'select_multi': if ( ! is_array( $value ) ) { return array(); } $allowed = isset( $field['options'] ) && is_array( $field['options'] ) ? array_map( 'strval', array_keys( $field['options'] ) ) : array(); $value = array_map( 'strval', wp_unslash( $value ) ); return array_values( array_intersect( $value, $allowed ) ); case 'select': case 'radio': $value = sanitize_text_field( wp_unslash( $value ) ); $allowed = isset( $field['options'] ) && is_array( $field['options'] ) ? array_map( 'strval', array_keys( $field['options'] ) ) : array(); return in_array( (string) $value, $allowed, true ) ? $value : ''; case 'textarea': return sanitize_textarea_field( wp_unslash( $value ) ); case 'hidden': case 'password': case 'text_secret': case 'text': default: return sanitize_text_field( wp_unslash( $value ) ); } } /** * Settings section description. * * @param $section * * @since 1.0 */ public function settings_section( $section ) { global $wpaf_last; $active = ""; //before first tab if ( $section['id'] == 0 ) { $active = 'active'; echo "
"; } } /** * Generate HTML for displaying fields. * * @param array $args Field data * * @return void * @since 1.0 */ public function display_field( $args ) { $field = $args['field']; $output = ''; $option_name = $this->settings_base . $field['id']; $option = get_option( $option_name ); $data = ''; if ( isset( $field['default'] ) ) { $data = $field['default']; if ( $option ) { $data = $option; } } switch ( $field['type'] ) { case 'text': case 'password': case 'number': $output .= '' . "\n"; break; case 'hidden': $output .= '' . "\n"; break; case 'colorpicker': $data = WPAF_Assets::wpaf_sanitize_css_color( $data ); $output .= '