*/ class PDATS_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function pdats_enqueue_styles( $hook ) { if ( 'toplevel_page_aitosocial-settings' !== $hook ) { return; } wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/ai-to-social-admin.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function pdats_enqueue_scripts( $hook ) { if ( 'toplevel_page_aitosocial-settings' !== $hook ) { return; } wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/ai-to-social-admin.js', array( 'jquery' ), $this->version, true ); } /** * Add plugin settings page for the admin area. * * @since 1.0.0 */ public function pdats_add_settings_page() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in PDATS_Settings_Page as all of the hooks are defined * in that particular class. * * The PDATS_Settings_Page will then create the relationship * between the defined hooks and the functions defined in this * class. */ add_menu_page( esc_html__('AItoSocial', 'aitosocial'), esc_html__('AItoSocial', 'aitosocial'), 'manage_options', $this->plugin_name.'-settings', array($this,'pdats_create_admin_page_html'), 'dashicons-share',50 ); } /** * Create admin settings page. * * @since 1.0.0 */ public function pdats_create_admin_page_html(){ if ( ! current_user_can( 'manage_options' ) ) { return; } // show message when updated $is_settings_updated = isset( $_GET['settings-updated'] ) ? sanitize_text_field( $_GET['settings-updated'] ) : ''; if ( ! empty( $is_settings_updated ) ) { add_settings_error( 'pdats_messages', 'pdats_message', esc_html__( 'Settings Saved', 'aitosocial' ), 'success' ); } // show error/update messages settings_errors( 'pdats_messages' ); ?>
plugin_name.'-setting' ); // $option_group( A settings group name. This must match the group name used in register_setting(), which is the page slug name on which the form is to appear. ). To display the hidden fields and handle security of your options form do_settings_sections( $this->plugin_name.'-setting' ); // $page The slug name of the page whose settings sections you want to output. This should match the page name used in add_settings_section(). submit_button(); ?>
' . esc_html__( 'Social share options settings', 'aitosocial' ) . '

'; } /** * Generate option page settings sections and fields. * * @since 1.0.0 */ public function pdats_add_settings_fields(){ $_show_on = $this->plugin_name . '_show_on'; add_settings_section( $this->plugin_name.'_setting_section', // $id - String for use in the 'id' attribute of tags. __( 'Settings', 'aitosocial' ), // $title - Title of the section. array($this, 'pdats_print_general_section_info'), // Function that fills the section with the desired content. The function should echo its output. $this->plugin_name.'-setting' // $page - The type of settings page on which to show the section (general, reading, writing, media etc.) ); $_select_style = $this->plugin_name.'_select_style'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $_select_style, // $id - String for use in the 'id' attribute of tags. __( 'Select Style', 'aitosocial' ), // Title of the field. array($this, 'pdats_create_radio_input'), //callback function for radio input $this->plugin_name.'-setting', //settings page on which to show the field $this->plugin_name.'_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'radio', "id" => $_select_style, 'desc' => __( 'Select social share buttons style', 'aitosocial' ), ) ); $_select_services = $this->plugin_name.'_select_services'; $service_options = array('facebook' => 'Facebook', 'twitter' => 'Twitter', 'reddit' => 'Reddit', 'linkedin' => 'LinkedIn', 'tumblr' => 'Tumblr', 'email' => 'Email'); add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $_select_services, // $id - String for use in the 'id' attribute of tags. __( 'Select Services', 'aitosocial' ), // Title of the field. array($this, 'pdats_create_checkbox_input'), //callback function for checkbox input $this->plugin_name.'-setting', //settings page on which to show the field $this->plugin_name.'_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'checkbox', "id" => $_select_services, "value" => $service_options, 'desc' => __( 'Select social media\'s from the list', 'aitosocial' ), ) ); $_select_position = $this->plugin_name.'_select_position'; $position_options = array( 'beforecontent' => __( 'Before Content', 'aitosocial' ), 'aftercontent' => __( 'After Content', 'aitosocial' ), ); add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $_select_position, // $id - String for use in the 'id' attribute of tags. __( 'Select Position', 'aitosocial' ), // Title of the field. array($this, 'pdats_create_checkbox_input'), //callback function for checkbox input $this->plugin_name.'-setting', //settings page on which to show the field $this->plugin_name.'_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'checkbox', "id" => $_select_position, "value" => $position_options, 'desc' => __( 'Select a position where you want to show the share icons.', 'aitosocial' ), ) ); $query_args = array( 'public' => true, ); $output = 'names'; $operator = 'and'; $post_types = get_post_types( $query_args, $output, $operator ); if ( ! empty( $post_types ) ) { unset( $post_types['attachment'] ); $post_types['archive'] = __( 'Archive', 'aitosocial' ); add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $_show_on, // $id - String for use in the 'id' attribute of tags. __( 'Show on', 'aitosocial' ), // Title of the field. array($this, 'pdats_create_checkbox_input'), //callback function for multiple checkbox input $this->plugin_name.'-setting', //settings page on which to show the field $this->plugin_name.'_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'checkbox', "id" => $_show_on, "value" => $post_types, 'desc' => __( 'Show social share icons in targeted post types', 'aitosocial' ), ) ); } $_exclude_on = $this->plugin_name . '_exclude_on'; add_settings_field( $_exclude_on, __( 'Exclude on', 'aitosocial' ), array( $this, 'pdats_create_text_input' ), $this->plugin_name . '-setting', $this->plugin_name . '_setting_section', array( 'type' => 'text', 'id' => $_exclude_on, 'desc' => __( 'Exclude social share from the specific posts, pages etc. Add comma( , ) separated ids.', 'aitosocial' ), ) ); $_show_counts = $this->plugin_name . '_show_counts'; add_settings_field( $_show_counts, __( 'Show Share Counts', 'aitosocial' ), array( $this, 'pdats_create_toggle_input' ), $this->plugin_name . '-setting', $this->plugin_name . '_setting_section', array( 'id' => $_show_counts, 'desc' => __( 'Display how many times each button has been clicked.', 'aitosocial' ), ) ); register_setting( $this->plugin_name . '-setting', $_select_style, array( $this, 'pdats_sanitize_style' ) ); register_setting( $this->plugin_name . '-setting', $_select_services, array( $this, 'pdats_sanitize_array_of_keys' ) ); register_setting( $this->plugin_name . '-setting', $_select_position, array( $this, 'pdats_sanitize_array_of_keys' ) ); register_setting( $this->plugin_name . '-setting', $_show_on, array( $this, 'pdats_sanitize_array_of_keys' ) ); register_setting( $this->plugin_name . '-setting', $_exclude_on, array( $this, 'pdats_sanitize_exclude_ids' ) ); register_setting( $this->plugin_name . '-setting', $_show_counts, array( $this, 'pdats_sanitize_checkbox' ) ); } /** * Sanitize the style option (must be one of the allowed values). * * @since 1.0.0 * @param string $value Raw input. * @return string Sanitized value. */ public function pdats_sanitize_style( $value ) { $allowed = array( 'horizontal-square', 'horizontal-border', 'horizontal-circular' ); $value = sanitize_text_field( $value ); return in_array( $value, $allowed, true ) ? $value : 'horizontal-square'; } /** * Sanitize an array of option keys (e.g. services, positions, post types). * * @since 1.0.0 * @param mixed $value Raw input (expected array). * @return array Sanitized array. */ public function pdats_sanitize_array_of_keys( $value ) { if ( ! is_array( $value ) ) { return array(); } return array_map( 'sanitize_key', $value ); } /** * Sanitize the comma-separated exclude IDs field. * * @since 1.0.0 * @param string $value Raw input. * @return string Sanitized comma-separated integer IDs. */ public function pdats_sanitize_exclude_ids( $value ) { $value = sanitize_text_field( $value ); if ( empty( $value ) ) { return ''; } $ids = array_map( 'absint', explode( ',', $value ) ); $ids = array_filter( $ids ); return implode( ',', $ids ); } /** * Sanitize a checkbox/toggle (returns '1' or '0'). * * @since 1.0.3 * @param mixed $value Raw input. * @return string '1' or '0'. */ public function pdats_sanitize_checkbox( $value ) { return ( '1' === (string) $value ) ? '1' : '0'; } /** * Render a single on/off toggle checkbox. * * @since 1.0.3 * @param array $args Field arguments. */ public function pdats_create_toggle_input( $args ) { $value = get_option( $args['id'], '0' ); echo ''; } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function pdats_create_text_input( $args ) { $default = isset( $args['default'] ) ? $args['default'] : false; echo ''; if ( ! empty( $args['desc'] ) ) { echo '

' . esc_html( $args['desc'] ) . '

'; } } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function pdats_create_radio_input( $args ) { $img_dir_url = plugin_dir_url( __FILE__ ) . 'images/'; $option = get_option( $args['id'] ); $value = isset( $option ) ? $option : null; $style_options = array( 'horizontal-border' => $img_dir_url . 'horizontal-border.png', 'horizontal-square' => $img_dir_url . 'horizontal-square.png', 'horizontal-circular' => $img_dir_url . 'horizontal-circular.png', ); $html = ''; foreach ( $style_options as $key => $val ) : $checked = ( isset( $value ) && $value === $key ) ? ' checked="checked" ' : ''; $html .= '

'; endforeach; if ( ! empty( $args['desc'] ) ) { $html .= '

' . esc_html( $args['desc'] ) . '

'; } echo wp_kses_post( $html ); } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function pdats_create_checkbox_input($args){ $service_options = ! empty( $args['value'] ) ? $args['value'] : array(); $options_val = get_option( $args['id'] ); $options = is_array( $options_val ) ? $options_val : array(); $html = ''; if ( ! empty( $service_options ) ) { foreach ( $service_options as $key => $service_option ) : $checked = in_array( $key, $options, true ) ? 'checked="checked"' : ''; $html .= ' ' . esc_html( ucfirst( $service_option ) ) . '  
'; endforeach; } if ( ! empty( $args['desc'] ) ) { $html .= '

' . esc_html( $args['desc'] ) . '

'; } echo wp_kses_post( $html ); } /** * Function kses allowed html tags. * * @since 1.0.0 */ public function pdats_kses_allowed_html_tags() { global $current_screen; if ( ! empty( $current_screen ) ) { $current_page = get_current_screen()->base; if ( isset( $current_page ) && 'toplevel_page_aitosocial-settings' === $current_page ) { global $allowedposttags; $allowed_atts = array( 'class' => array(), 'type' => array(), 'id' => array(), 'style' => array(), 'value' => array(), 'name' => array(), 'width' => array(), 'height' => array(), 'title' => array(), 'checked' => array(), ); $allowedposttags['input'] = $allowed_atts; } } } }