*/ class Accessibility_Onetap_Settings_Manager { /** * Settings sections array. * * @var array */ protected $settings_sections = array(); /** * Settings fields array. * * @var array */ protected $settings_fields = array(); /** * Initialize __construct. */ public function __construct() { add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } /** * Enqueue scripts and styles for the admin area. * * This function ensures that necessary scripts and styles are loaded on * specific admin pages of the plugin. It loads the WordPress color picker, * media uploader, jQuery, and a custom JavaScript file for managing settings. * * @param string $hook The current admin page hook suffix. */ public function admin_enqueue_scripts( $hook ) { // Enqueue the WordPress color picker style. wp_enqueue_style( 'wp-color-picker' ); // Enqueue the media uploader script. wp_enqueue_media(); // Enqueue the WordPress color picker script. wp_enqueue_script( 'wp-color-picker' ); // Enqueue the jQuery library. wp_enqueue_script( 'jquery' ); // Conditionally enqueue the settings manager script for specific plugin admin pages. if ( 'toplevel_page_accessibility-onetap-settings' === $hook || 'onetap_page_accessibility-onetap-general-settings' === $hook || 'onetap_page_accessibility-onetap-alt-text' === $hook || 'onetap_page_accessibility-onetap-modules' === $hook || 'admin_page_onetap-module-labels' === $hook || 'onetap_page_accessibility-onetap-accessibility-status' === $hook ) { wp_enqueue_script( 'accessibility-plugin-onetap-pro-settings-manager', ACCESSIBILITY_ONETAP_PLUGINS_URL . 'assets/js/settings-manager.min.js', array( 'jquery' ), ACCESSIBILITY_ONETAP_VERSION, true ); } } /** * Set settings sections. * * @param array $sections setting sections array. */ public function set_sections( $sections ) { $this->settings_sections = $sections; return $this; } /** * Add a single section. * * @param array $section The section details. */ public function add_section( $section ) { $this->settings_sections[] = $section; return $this; } /** * Set settings fields. * * @param array $fields settings fields array. */ public function set_fields( $fields ) { $this->settings_fields = $fields; return $this; } /** * Add settings fields. * * @param array $section settings section array. * @param array $field settings fields array. */ public function add_field( $section, $field ) { $defaults = array( 'name' => '', 'label' => '', 'desc' => '', 'type' => 'text', ); $arg = wp_parse_args( $field, $defaults ); $this->settings_fields[ $section ][] = $arg; return $this; } /** * Initialize and registers the settings sections and fileds to WordPress. * * Usually this should be called at `admin_init` hook. * * This function gets the initiated settings sections and fields. Then * registers them to WordPress and ready for use. */ public function admin_init() { // register settings sections. foreach ( $this->settings_sections as $section ) { if ( false === get_option( $section['id'] ) ) { add_option( $section['id'] ); } if ( isset( $section['desc'] ) && ! empty( $section['desc'] ) ) { $section['desc'] = '
' . $section['desc'] . '
'; $callback = function () use ( $section ) { echo esc_html( str_replace( '"', '\"', $section['desc'] ) ); }; } elseif ( isset( $section['callback'] ) ) { $callback = $section['callback']; } else { $callback = null; } add_settings_section( $section['id'], $section['title'], $callback, $section['id'] ); } // register settings fields. foreach ( $this->settings_fields as $section => $field ) { foreach ( $field as $option ) { $anchor = isset( $option['anchor'] ) ? $option['anchor'] : ''; $color_list = isset( $option['color_list'] ) ? $option['color_list'] : ''; $device_visibility = isset( $option['device_visibility'] ) ? $option['device_visibility'] : ''; $feature_desc = isset( $option['feature_desc'] ) ? $option['feature_desc'] : ''; $feature_name = isset( $option['feature_name'] ) ? $option['feature_name'] : ''; $first_control = isset( $option['first_control'] ) ? $option['first_control'] : ''; $is_pro = isset( $option['is_pro'] ) ? $option['is_pro'] : ''; $label = isset( $option['label'] ) ? $option['label'] : ''; $last_control = isset( $option['last_control'] ) ? $option['last_control'] : ''; $name = $option['name']; $radio_image_list = isset( $option['radio_image_list'] ) ? $option['radio_image_list'] : ''; $radio_text_list = isset( $option['radio_text_list'] ) ? $option['radio_text_list'] : ''; $select_badge = isset( $option['select_badge'] ) ? $option['select_badge'] : ''; $select_options = isset( $option['select_options'] ) ? $option['select_options'] : ''; $setting_description = isset( $option['setting_description'] ) ? $option['setting_description'] : ''; $setting_title = isset( $option['setting_title'] ) ? $option['setting_title'] : ''; $show_save_button = isset( $option['show_save_button'] ) ? $option['show_save_button'] : ''; $status = isset( $option['status'] ) ? $option['status'] : ''; $switch_icon = isset( $option['switch_icon'] ) ? $option['switch_icon'] : ''; $switch_style = isset( $option['switch_style'] ) ? $option['switch_style'] : ''; $module_icon = isset( $option['module_icon'] ) ? $option['module_icon'] : ''; $module_style = isset( $option['module_style'] ) ? $option['module_style'] : ''; $copyable_icon = isset( $option['copyable_icon'] ) ? $option['copyable_icon'] : ''; $copyable_style = isset( $option['copyable_style'] ) ? $option['copyable_style'] : ''; $copy_text = isset( $option['copy_text'] ) ? $option['copy_text'] : ''; $button_text = isset( $option['button_text'] ) ? $option['button_text'] : ''; $button_icon = isset( $option['button_icon'] ) ? $option['button_icon'] : ''; $button_class = isset( $option['button_class'] ) ? $option['button_class'] : ''; $button_link = isset( $option['button_link'] ) ? $option['button_link'] : ''; $hide_setting_description = isset( $option['hide_setting_description'] ) ? (bool) $option['hide_setting_description'] : false; $type = isset( $option['type'] ) ? $option['type'] : 'text'; $callback = isset( $option['callback'] ) ? array( $this, $option['callback'] ) : array( $this, 'callback_' . $type ); $args = array( 'anchor' => $anchor, 'class' => isset( $option['class'] ) ? $option['class'] : $name, 'color_list' => $color_list, 'device_visibility' => $device_visibility, 'feature_desc' => $feature_desc, 'feature_name' => $feature_name, 'first_control' => $first_control, 'id' => $name, 'is_pro' => $is_pro, 'label' => $label, 'label_for' => "{$section}[{$name}]", 'last_control' => $last_control, 'max' => isset( $option['max'] ) ? $option['max'] : '', 'min' => isset( $option['min'] ) ? $option['min'] : '', 'name' => $label, 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '', 'radio_image_list' => $radio_image_list, 'radio_text_list' => $radio_text_list, 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '', 'section' => $section, 'select_badge' => $select_badge, 'select_options' => $select_options, 'setting_description' => $setting_description, 'setting_title' => $setting_title, 'show_save_button' => $show_save_button, 'status' => $status, 'step' => isset( $option['step'] ) ? $option['step'] : '', 'std' => isset( $option['default'] ) ? $option['default'] : '', 'switch_icon' => $switch_icon, 'switch_style' => $switch_style, 'module_icon' => $module_icon, 'module_style' => $module_style, 'copyable_icon' => $copyable_icon, 'copyable_style' => $copyable_style, 'copy_text' => $copy_text, 'button_text' => $button_text, 'button_icon' => $button_icon, 'button_class' => $button_class, 'button_link' => $button_link, 'hide_setting_description' => $hide_setting_description, 'type' => $type, 'units' => isset( $option['units'] ) ? $option['units'] : '', ); add_settings_field( "{$section}[{$name}]", $label, $callback, $section, $section, $args ); } } // creates our settings in the options table. foreach ( $this->settings_sections as $section ) { register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) ); } } /** * Get common allowed HTML tags for wp_kses. * * @return array Allowed HTML tags array. */ protected function get_allowed_html() { return array( 'h4' => array( 'class' => array(), ), 'p' => array( 'class' => array(), ), 'a' => array( 'class' => array(), 'href' => array(), 'target' => array(), 'title' => array(), 'data-image-id' => array(), ), 'div' => array( 'class' => array(), 'style' => array(), 'data-device' => array(), ), 'ul' => array( 'class' => array(), ), 'li' => array( 'class' => array(), 'data-color' => array(), 'style' => array(), ), 'img' => array( 'class' => array(), 'src' => array(), ), 'span' => array( 'id' => array(), 'class' => array(), 'license-status' => array(), ), 'label' => array( 'class' => array(), 'id' => array(), ), 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'style' => array(), 'placeholder' => array(), 'checked' => array(), 'min' => array(), 'max' => array(), 'step' => array(), 'license-status' => array(), 'license-key' => array(), 'readonly' => array(), ), 'textarea' => array( 'rows' => array(), 'cols' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'placeholder' => array(), 'data-image-id' => array(), ), 'select' => array( 'class' => array(), 'name' => array(), 'id' => array(), ), 'option' => array( 'value' => array(), 'selected' => array(), ), 'button' => array( 'type' => array(), 'class' => array(), 'style' => array(), 'title' => array(), 'data-image-id' => array(), 'aria-pressed' => array(), 'data-device-type' => array(), ), 'svg' => array( 'class' => array(), 'viewBox' => array(), 'width' => array(), 'height' => array(), 'fill' => array(), 'xmlns' => array(), ), 'g' => array( 'clip-path' => array(), ), 'clipPath' => array( 'id' => array(), 'rect' => array(), ), 'rect' => array( 'width' => array(), 'height' => array(), 'fill' => array(), 'transform' => array(), ), 'defs' => array(), 'path' => array( 'd' => array(), 'fill' => array(), 'stroke' => array(), 'stroke-width' => array(), 'stroke-linecap' => array(), 'stroke-linejoin' => array(), ), ); } /** * Parse common arguments for template rendering. * * @param array $args Settings field arguments. * @return array Parsed arguments. */ protected function parse_template_args( $args ) { $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $label = isset( $args['label'] ) ? $args['label'] : ''; $setting_title = isset( $args['setting_title'] ) ? $args['setting_title'] : ''; $setting_description = isset( $args['setting_description'] ) ? $args['setting_description'] : ''; $first_control = ! empty( $args['first_control'] ) ? ' first-control' : ''; $last_control = ! empty( $args['last_control'] ) ? ' last-control' : ''; $show_save_button = ! empty( $args['show_save_button'] ); $hide_setting_description = isset( $args['hide_setting_description'] ) ? (bool) $args['hide_setting_description'] : false; $type = isset( $args['type'] ) ? $args['type'] : 'default'; $classes = 'setting-control' . $first_control . ' ' . $last_control . ' ' . esc_attr( $type ); return array( 'value' => $value, 'label' => $label, 'setting_title' => $setting_title, 'setting_description' => $setting_description, 'first_control' => $first_control, 'last_control' => $last_control, 'show_save_button' => $show_save_button, 'hide_setting_description' => $hide_setting_description, 'type' => $type, 'classes' => $classes, 'args' => $args, ); } /** * Build common HTML structure for settings templates. * * @param array $parsed_args Parsed template arguments. * @param string $content Inner content for the template. * @return string Generated HTML. */ protected function build_template_structure( $parsed_args, $content = '' ) { // Build device visibility classes. $device_visibility_class = ''; $active_class = ''; $device_visibility_attr = ''; $viewport_class = ''; $hide_description_class = ''; if ( isset( $parsed_args['args']['device_visibility'] ) && ! empty( $parsed_args['args']['device_visibility'] ) ) { $device_visibility_class = $parsed_args['args']['device_visibility'] . ' device'; // Add data attribute for device when device_visibility is set. $device_visibility_attr = ' data-device="' . esc_attr( $parsed_args['args']['device_visibility'] ) . '"'; // Add preview viewport classes similar to the preview markup. $viewport_class = ' preview-viewport viewport-' . esc_attr( $parsed_args['args']['device_visibility'] ); // Add active class if device_visibility is desktop. if ( 'desktop' === $parsed_args['args']['device_visibility'] ) { $active_class = ' active'; } } // Add hide description class if hide_setting_description is true. if ( $parsed_args['hide_setting_description'] ) { $hide_description_class = ' hide-setting-description'; } $html = '
'; $html .= '
'; if ( $parsed_args['setting_title'] ) { $html .= '' . $parsed_args['setting_title'] . ''; } if ( $parsed_args['setting_description'] && ! $parsed_args['hide_setting_description'] ) { $html .= '' . $parsed_args['setting_description'] . ''; } if ( $parsed_args['label'] ) { $html .= '' . esc_html( $parsed_args['label'] ) . ''; } $html .= '
'; $html .= $content; $html .= '
'; // .boxes if ( $parsed_args['show_save_button'] ) { $html .= ''; } $html .= '
'; // .setting-control $html .= '
'; // .settings-group return $html; } /** * Render template with common structure. * * @param array $args Settings field arguments. * @param string $content Inner content for the template. * @param string $type Template type for default type fallback. */ protected function render_template( $args, $content = '', $type = 'default' ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = $type; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $type ); $html = $this->build_template_structure( $parsed_args, $content ); echo wp_kses( $html, $this->get_allowed_html() ); } /** * Callback function for rendering divider template in settings. * * This method generates HTML for a visual divider element used to separate * different sections or groups of settings. The divider provides visual * separation and improves the overall layout and readability of the settings page. * * @param array $args settings field args. */ public function callback_template_divider( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'divider'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build input HTML. $input_html = '
'; $input_html .= '
'; $input_html .= '
'; $this->render_template( $args, $input_html, 'divider' ); } /** * Callback function to render the save changes button template. * * This method creates a save changes button with proper styling and classes. * It parses the provided arguments, sets default values, and renders the button * using the template rendering system. * * @param array $args settings field args. */ public function callback_template_save_changes( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'submit'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build input HTML. $input_html = '
'; $input_html .= ''; $input_html .= '
'; $this->render_template( $args, $input_html, 'submit' ); } /** * Callback function for rendering switch template in settings. * * This method generates HTML for a toggle switch control with customizable content * including optional icon, PRO status badge, feature name, and description. * The switch supports various styling options and can be configured for different * feature types with conditional display of elements. * * @param array $args settings field args. */ public function callback_template_switch( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'switch'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build switch content attributes. $switch_icon = isset( $args['switch_icon'] ) ? esc_url( $args['switch_icon'] ) : ''; $is_pro = isset( $args['is_pro'] ) ? (bool) $args['is_pro'] : false; $feature_name = isset( $args['feature_name'] ) ? esc_html( $args['feature_name'] ) : ''; $feature_desc = isset( $args['feature_desc'] ) ? esc_html( $args['feature_desc'] ) : ''; $switch_style = isset( $args['switch_style'] ) ? esc_attr( $args['switch_style'] ) : ''; // Build PRO feature information attributes. $pro_title = isset( $args['pro_title'] ) ? esc_html( $args['pro_title'] ) : __( 'This is a PRO Feature', 'accessibility-onetap' ); $pro_desc = isset( $args['pro_desc'] ) ? esc_html( $args['pro_desc'] ) : __( ' to use this Feature.', 'accessibility-onetap' ); $pro_link_text = isset( $args['pro_link_text'] ) ? esc_html( $args['pro_link_text'] ) : __( 'Upgrade to Pro', 'accessibility-onetap' ); $upgrade_link = isset( $args['upgrade_link'] ) ? esc_url( $args['upgrade_link'] ) : 'https://wponetap.com/pricing/'; // Check if label exists and is not empty, then add padding-top-0 class (except for switch2 style). $label_class = ( isset( $args['label'] ) && ! empty( $args['label'] ) && 'switch2' !== $switch_style ) ? ' padding-top-0' : ''; // Build input HTML. $input_html = '
'; $input_html .= '
'; // Icon section - only show if switch_icon is provided. if ( ! empty( $switch_icon ) ) { $input_html .= '
'; $input_html .= 'icon'; $input_html .= '
'; } $input_html .= '
'; // Display feature name - only show if feature_name exists (regardless of is_pro status). if ( ! empty( $feature_name ) ) { $input_html .= '' . $feature_name . ( $is_pro ? '' . esc_html__( 'PRO', 'accessibility-onetap' ) . '' : '' ) . ''; } // Feature description - only show if feature_desc is provided (regardless of is_pro status). if ( ! empty( $feature_desc ) ) { $input_html .= '' . $feature_desc . ''; } $input_html .= '
'; $input_html .= '
'; $input_html .= '
'; // Show PRO feature information if is_pro is true. if ( $is_pro ) { $input_html .= '
'; $input_html .= '

' . $pro_title . '

'; $input_html .= '

'; $input_html .= '' . $pro_link_text . ' ' . $pro_desc; $input_html .= '

'; $input_html .= '
'; } $input_html .= '
'; $input_html .= ''; $input_html .= '
'; $input_html .= '
'; $input_html .= '
'; $this->render_template( $args, $input_html, 'switch' ); } /** * Callback function for rendering number input template in settings. * * This method generates HTML for a number input field with customizable attributes * such as min, max, step values, and optional save button. It supports various * styling options and accessibility features. * * @param array $args settings field args. */ public function callback_template_number( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'number'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build input attributes. $placeholder = ! empty( $args['placeholder'] ) ? 'placeholder="' . esc_attr( $args['placeholder'] ) . '"' : ''; $min = ( '' === $args['min'] ) ? '' : ' min="' . esc_attr( $args['min'] ) . '"'; $max = ( '' === $args['max'] ) ? '' : ' max="' . esc_attr( $args['max'] ) . '"'; $step = ( '' === $args['step'] ) ? '' : ' step="' . esc_attr( $args['step'] ) . '"'; // Build input HTML. $input_html = '
'; $input_html .= ''; $input_html .= '
'; $this->render_template( $args, $input_html, 'number' ); } /** * Callback function for rendering number slider template in settings. * * This method generates HTML for a number slider input field with range slider and number input. * It supports customizable min, max, and step values with proper sanitization for security. * The slider provides visual feedback while the number input allows precise value entry. * * @param array $args settings field args. */ public function callback_template_number_slider( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'number-slider'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build input attributes. $min = ( '' === $args['min'] ) ? '' : ' min="' . esc_attr( $args['min'] ) . '"'; $max = ( '' === $args['max'] ) ? '' : ' max="' . esc_attr( $args['max'] ) . '"'; $step = ( '' === $args['step'] ) ? '' : ' step="' . esc_attr( $args['step'] ) . '"'; // Get unit value with fallback to 'px'. $unit = isset( $args['units'] ) && ! empty( $args['units'] ) ? esc_attr( $args['units'] ) : 'px'; // Build input HTML. $input_html = '
'; $input_html .= '
'; $input_html .= ''; $input_html .= '
'; $input_html .= '
'; $input_html .= ''; $input_html .= '' . $unit . ''; $input_html .= '
'; $input_html .= '
'; $this->render_template( $args, $input_html, 'number-slider' ); } /** * Callback function for rendering text input template in settings. * * This method generates HTML for a text input field with customizable attributes * and optional save button. It supports various styling options and accessibility features. * * @param array $args settings field args. */ public function callback_template_text( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'text'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build input attributes. $placeholder = ! empty( $args['placeholder'] ) ? 'placeholder="' . esc_attr( $args['placeholder'] ) . '"' : ''; // Build input HTML. $input_html = '
'; $input_html .= ''; $input_html .= '
'; $this->render_template( $args, $input_html, 'text' ); } /** * Render a title and description section for settings forms. * * Outputs a settings section with optional title, description, label, and save button. * This template is used for displaying informational content or section headers * in the settings page without any input fields. * * @param array $args settings field args. */ public function callback_template_title_and_description( $args ) { $this->render_template( $args, '', 'title-and-description' ); } /** * Render a device preview selector for settings forms. * * Outputs a device preview section with desktop, tablet, and mobile options. * Allows users to preview how the accessibility widget will appear on different devices. * Includes SVG icons for each device type and optional save button. * * @param array $args settings field args. */ public function callback_template_devices_tabs( $args ) { // Build devices HTML. $devices_html = '
'; $devices_html .= '
'; // Desktop. $devices_html .= ''; // Tablet. $devices_html .= ''; // Mobile. $devices_html .= ''; $devices_html .= '
'; $devices_html .= '
'; $this->render_template( $args, $devices_html, 'devices' ); } /** * Render a radio field with image/icon options for settings forms. * * Outputs a group of radio buttons where each option is represented by an image/icon. * Optionally displays a 'Save Changes' button below the options. * * @param array $args settings field args. */ public function callback_template_radio_image( $args ) { $parsed_args = $this->parse_template_args( $args ); $radio_image_list = isset( $args['radio_image_list'] ) && is_array( $args['radio_image_list'] ) ? $args['radio_image_list'] : array(); // Build radio image HTML. $radio_html = ''; $index = 1; foreach ( $radio_image_list as $image_value => $image ) { $checked = ( $parsed_args['value'] === $image_value ) ? 'checked' : ''; $input_id = esc_attr( $args['id'] ) . esc_attr( str_replace( 'design-icon', '', $image_value ) ); $radio_html .= '
'; $radio_html .= ''; $radio_html .= '
'; ++$index; } $this->render_template( $args, $radio_html, 'radio-image' ); } /** * Render a radio field with text options for settings forms. * * Outputs a group of radio buttons where each option is represented by an text. * Optionally displays a 'Save Changes' button below the options. * * @param array $args settings field args. */ public function callback_template_radio_text( $args ) { $parsed_args = $this->parse_template_args( $args ); $radio_text_list = isset( $args['radio_text_list'] ) && is_array( $args['radio_text_list'] ) ? $args['radio_text_list'] : array(); // Build radio text HTML. $radio_html = ''; $index = 1; foreach ( $radio_text_list as $text_value => $text ) { $checked = ( $parsed_args['value'] === $text_value ) ? 'checked' : ''; $input_id = esc_attr( $args['id'] ) . esc_attr( str_replace( 'design-text', '', $text_value ) ); $radio_html .= '
'; $radio_html .= ''; $radio_html .= '
'; ++$index; } $this->render_template( $args, $radio_html, 'radio-text' ); } /** * Render a copyable text field for settings forms. * * Outputs a text field with copy functionality that allows users to copy text to clipboard. * This template is used for displaying text that can be copied, such as URLs or codes. * * @param array $args settings field args. */ public function callback_template_copyable_text( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'copyable_text'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build copyable text content attributes. $copyable_icon = isset( $args['copyable_icon'] ) ? esc_url( $args['copyable_icon'] ) : ''; $is_pro = isset( $args['is_pro'] ) ? (bool) $args['is_pro'] : false; $feature_name = isset( $args['feature_name'] ) ? esc_html( $args['feature_name'] ) : ''; $feature_desc = isset( $args['feature_desc'] ) ? esc_html( $args['feature_desc'] ) : ''; $copyable_style = isset( $args['copyable_style'] ) ? esc_attr( $args['copyable_style'] ) : ''; // Build PRO feature information attributes. $pro_title = isset( $args['pro_title'] ) ? esc_html( $args['pro_title'] ) : __( 'This is a PRO Feature', 'accessibility-onetap' ); $pro_desc = isset( $args['pro_desc'] ) ? esc_html( $args['pro_desc'] ) : __( ' to use this Feature.', 'accessibility-onetap' ); $pro_link_text = isset( $args['pro_link_text'] ) ? esc_html( $args['pro_link_text'] ) : __( 'Upgrade to Pro', 'accessibility-onetap' ); $upgrade_link = isset( $args['upgrade_link'] ) ? esc_url( $args['upgrade_link'] ) : 'https://wponetap.com/pricing/'; // Check if label exists and is not empty, then add padding-top-0 class (except for style2 style). $label_class = ( isset( $args['label'] ) && ! empty( $args['label'] ) && 'style2' !== $copyable_style ) ? ' padding-top-0' : ''; // Build input HTML. $input_html = '
'; $input_html .= '
'; // Icon section - only show if copyable_icon is provided. if ( ! empty( $copyable_icon ) ) { $input_html .= '
'; $input_html .= 'icon'; $input_html .= '
'; } $input_html .= '
'; // Display feature name - only show if feature_name exists (regardless of is_pro status). if ( ! empty( $feature_name ) ) { $input_html .= '' . $feature_name . ( $is_pro ? '' . esc_html__( 'PRO', 'accessibility-onetap' ) . '' : '' ) . ''; } // Feature description - only show if feature_desc is provided (regardless of is_pro status). if ( ! empty( $feature_desc ) ) { $input_html .= '' . $feature_desc . ''; } $input_html .= '
'; $input_html .= '
'; $input_html .= '
'; // Show PRO feature information if is_pro is true. if ( $is_pro ) { $input_html .= '
'; $input_html .= '

' . $pro_title . '

'; $input_html .= '

'; $input_html .= '' . $pro_link_text . ' ' . $pro_desc; $input_html .= '

'; $input_html .= '
'; } $input_html .= '
'; $input_html .= '
'; $input_html .= $args['copy_text']; $input_html .= '
'; $input_html .= ''; $input_html .= '
'; $input_html .= '
'; $this->render_template( $args, $input_html, 'copyable_text' ); } /** * Render a module labels input field for settings forms. * * Creates a custom input field specifically designed for module label customization. * The function supports both free and PRO features, with different visual indicators * and functionality based on the feature status. * * @param array $args settings field args. */ public function callback_template_module_labels( $args ) { $parsed_args = $this->parse_template_args( $args ); $camel_case_id = lcfirst( str_replace( ' ', '', ucwords( str_replace( array( '-', '_' ), ' ', $args['id'] ) ) ) ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'module-labels'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build module labels content attributes. $module_icon = isset( $args['module_icon'] ) ? esc_url( $args['module_icon'] ) : ''; $is_pro = isset( $args['is_pro'] ) ? (bool) $args['is_pro'] : false; $feature_name = isset( $args['feature_name'] ) ? esc_html( $args['feature_name'] ) : ''; $feature_desc = isset( $args['feature_desc'] ) ? esc_html( $args['feature_desc'] ) : ''; $module_style = isset( $args['module_style'] ) ? esc_attr( $args['module_style'] ) : ''; // Build PRO feature information attributes. $pro_title = isset( $args['pro_title'] ) ? esc_html( $args['pro_title'] ) : __( 'This is a PRO Feature', 'accessibility-onetap' ); $pro_desc = isset( $args['pro_desc'] ) ? esc_html( $args['pro_desc'] ) : __( ' to use this Feature.', 'accessibility-onetap' ); $pro_link_text = isset( $args['pro_link_text'] ) ? esc_html( $args['pro_link_text'] ) : __( 'Upgrade to Pro', 'accessibility-onetap' ); $upgrade_link = isset( $args['upgrade_link'] ) ? esc_url( $args['upgrade_link'] ) : 'https://wponetap.com/pricing/'; // Check if label exists and is not empty, then add padding-top-0 class (except for switch2 style). $label_class = ( isset( $args['label'] ) && ! empty( $args['label'] ) && 'switch2' !== $module_style ) ? ' padding-top-0' : ''; // Build input attributes. $placeholder_text = ! empty( $args['placeholder'] ) ? $args['placeholder'] : __( 'Enter Custom Label', 'accessibility-onetap' ); // Build input HTML. $input_html = '
'; $input_html .= '
'; // Icon section - only show if module_icon is provided. if ( ! empty( $module_icon ) ) { $input_html .= '
'; $input_html .= 'icon'; $input_html .= '
'; } $input_html .= '
'; // Display feature name - only show if feature_name exists (regardless of is_pro status). if ( ! empty( $feature_name ) ) { $input_html .= '' . $feature_name . ( $is_pro ? '' . esc_html__( 'PRO', 'accessibility-onetap' ) . '' : '' ) . ''; } // Feature description - only show if feature_desc is provided (regardless of is_pro status). if ( ! empty( $feature_desc ) ) { $input_html .= '' . $feature_desc . ''; } $input_html .= '
'; $input_html .= '
'; $input_html .= '
'; // Show PRO feature information if is_pro is true. if ( $is_pro ) { $input_html .= '
'; $input_html .= '

' . $pro_title . '

'; $input_html .= '

'; $input_html .= '' . $pro_link_text . ' ' . $pro_desc; $input_html .= '

'; $input_html .= '
'; } $input_html .= ''; $input_html .= ''; $input_html .= '
'; $input_html .= '
'; $this->render_template( $args, $input_html, 'module-labels' ); } /** * Render a select dropdown field for settings forms. * * Outputs a select dropdown with predefined options from the select_options array. * Includes optional title, description, label, and save button. * The select field allows users to choose from a list of predefined options. * * @param array $args settings field args. */ public function callback_template_select( $args ) { $parsed_args = $this->parse_template_args( $args ); $select_options = isset( $args['select_options'] ) ? $args['select_options'] : array(); $select_badge = isset( $args['select_badge'] ) ? $args['select_badge'] : ''; // Build select HTML. $select_html = '
'; $select_html .= ''; $select_html .= $select_badge ? ' ' . $select_badge . '' : ''; $select_html .= '
'; $this->render_template( $args, $select_html, 'select' ); } /** * Callback template for image list with alt text management. * * @param array $args Arguments for the template. */ public function callback_template_image_alt_list( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'alt-text'; // Get current page for pagination. // phpcs:disable WordPress.Security.NonceVerification.Recommended $current_page = isset( $_GET['alt_text_page'] ) ? max( 1, intval( $_GET['alt_text_page'] ) ) : 1; $per_page = 10; // Number of images per page. $offset = ( $current_page - 1 ) * $per_page; // Get images from WordPress Media Library. $image_list = $this->get_media_library_images( $per_page, $offset ); $total_images = $this->get_total_media_count(); $total_pages = ceil( $total_images / $per_page ); // Build image list HTML with dynamic data. $list_html = '
'; $list_html .= '
'; // Table header. $list_html .= '
'; $list_html .= '
'; $list_html .= '
' . __( 'Thumbnail', 'accessibility-onetap' ) . '
'; $list_html .= '
' . __( 'Alt Text', 'accessibility-onetap' ) . '
'; $list_html .= '
' . __( 'Uploaded', 'accessibility-onetap' ) . '
'; $list_html .= '
' . __( 'Actions', 'accessibility-onetap' ) . '
'; $list_html .= '
'; $list_html .= '
'; // Table body. $list_html .= '
'; if ( ! empty( $image_list ) ) { foreach ( $image_list as $image ) { $thumbnail_url = isset( $image['thumbnail'] ) ? $image['thumbnail'] : ''; $alt_text = isset( $image['alt_text'] ) ? $image['alt_text'] : ''; $upload_date = isset( $image['upload_date'] ) ? $image['upload_date'] : ''; $image_id = isset( $image['id'] ) ? $image['id'] : ''; $list_html .= '
'; // Thumbnail column. $list_html .= '
'; if ( $thumbnail_url ) { $list_html .= '
'; } $list_html .= '
'; // Alt text column. $list_html .= '
'; if ( $alt_text && 'edit' !== $alt_text ) { $list_html .= '' . esc_html( $alt_text ) . ''; } elseif ( 'edit' === $alt_text ) { $list_html .= ''; } else { $list_html .= '' . esc_html( $alt_text ) . ''; } $list_html .= '
'; // Uploaded column. $list_html .= '
'; $list_html .= '' . esc_html( $upload_date ) . ''; $list_html .= '
'; // Actions column. $list_html .= '
'; $list_html .= '
'; // AI button. $list_html .= ''; $list_html .= 'AltPilot.ai'; $list_html .= ''; // Edit button. $list_html .= ''; // Save button. $list_html .= ''; $list_html .= '
'; $list_html .= '
'; $list_html .= '
'; // End row. } } else { // No images found. $list_html .= '
'; $list_html .= '
'; $list_html .= '

' . __( 'No images found in Media Library.', 'accessibility-onetap' ) . '

'; $list_html .= '
'; $list_html .= '
'; } $list_html .= '
'; // End table-body. $list_html .= '
'; // End box-image-alt. // Pagination. if ( $total_pages > 1 ) { $list_html .= '
'; // Previous button. $list_html .= ''; // Page numbers. $list_html .= ''; // Next button. $list_html .= ''; $list_html .= '
'; // End box-navigation. } $list_html .= '
'; // End box. $this->render_template( $args, $list_html, 'image-alt-list' ); } /** * Get images from WordPress Media Library with pagination. * * @param int $per_page Number of images per page. * @param int $offset Offset for pagination. * @return array Array of image data. */ private function get_media_library_images( $per_page = 10, $offset = 0 ) { $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => $per_page, 'offset' => $offset, 'orderby' => 'ID', 'order' => 'DESC', ); $query = new WP_Query( $args ); $images = array(); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $attachment_id = get_the_ID(); $attachment = get_post( $attachment_id ); // Get thumbnail URL. $thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); $thumbnail_url = $thumbnail_url ? $thumbnail_url[0] : ''; // Get alt text. $alt_text = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( empty( $alt_text ) ) { $alt_text = '-'; } // Get upload date. $upload_date = get_the_date( 'd M Y, H:i', $attachment_id ); $images[] = array( 'id' => $attachment_id, 'thumbnail' => $thumbnail_url, 'alt_text' => $alt_text, 'upload_date' => $upload_date, ); } wp_reset_postdata(); } return $images; } /** * Get total count of images in Media Library. * * @return int Total number of images. */ private function get_total_media_count() { $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'fields' => 'ids', ); $query = new WP_Query( $args ); return $query->found_posts; } /** * Render a color picker field with predefined color options for settings forms. * * Outputs a color picker input with a list of predefined color options. * Includes a color preview display and allows users to select from predefined colors * or use the WordPress color picker for custom colors. * * @param array $args settings field args. */ public function callback_template_color( $args ) { $parsed_args = $this->parse_template_args( $args ); $color_list = isset( $args['color_list'] ) ? $args['color_list'] : array(); // Build color picker HTML. $color_html = '
'; $color_html .= ''; $color_html .= '
'; $color_html .= '
'; $color_html .= '' . esc_attr( $parsed_args['value'] ) . ''; $color_html .= '
'; $color_html .= '
'; if ( ! empty( $color_list ) ) { $color_html .= ''; } $color_html .= '
'; $this->render_template( $args, $color_html, 'color' ); } /** * Render a feature card for settings forms. * * Outputs a feature card with icon, description, PRO information, and action button. * This template is used for displaying feature information with copyable text and edit functionality. * * @param array $args settings field args. */ public function callback_template_feature_card( $args ) { $parsed_args = $this->parse_template_args( $args ); $parsed_args['type'] = isset( $args['type'] ) ? $args['type'] : 'feature_card'; $parsed_args['classes'] = 'setting-control' . $parsed_args['first_control'] . ' ' . $parsed_args['last_control'] . ' ' . esc_attr( $parsed_args['type'] ); // Build feature card content attributes. $feature_icon = isset( $args['feature_icon'] ) ? esc_url( $args['feature_icon'] ) : ''; $is_pro = isset( $args['is_pro'] ) ? (bool) $args['is_pro'] : false; $feature_name = isset( $args['feature_name'] ) ? esc_html( $args['feature_name'] ) : ''; $feature_desc = isset( $args['feature_desc'] ) ? esc_html( $args['feature_desc'] ) : ''; $feature_style = isset( $args['feature_style'] ) ? esc_attr( $args['feature_style'] ) : ''; // Build PRO feature information attributes. $pro_title = isset( $args['pro_title'] ) ? esc_html( $args['pro_title'] ) : __( 'This is a PRO Feature', 'accessibility-onetap' ); $pro_desc = isset( $args['pro_desc'] ) ? esc_html( $args['pro_desc'] ) : __( ' to use this Feature.', 'accessibility-onetap' ); $pro_link_text = isset( $args['pro_link_text'] ) ? esc_html( $args['pro_link_text'] ) : __( 'Upgrade to Pro', 'accessibility-onetap' ); $upgrade_link = isset( $args['upgrade_link'] ) ? esc_url( $args['upgrade_link'] ) : 'https://wponetap.com/pricing/'; // Build button attributes. $button_text = isset( $args['button_text'] ) ? esc_html( $args['button_text'] ) : ''; $button_icon = isset( $args['button_icon'] ) ? esc_url( $args['button_icon'] ) : ''; $button_class = isset( $args['button_class'] ) ? esc_attr( $args['button_class'] ) : ''; $button_link = isset( $args['button_link'] ) ? esc_url( $args['button_link'] ) : ''; // Check if label exists and is not empty, then add padding-top-0 class (except for style2 style). $label_class = ( isset( $args['label'] ) && ! empty( $args['label'] ) && 'style2' !== $feature_style ) ? ' padding-top-0' : ''; // Build input HTML. $input_html = '
'; $input_html .= '
'; // Icon section - only show if feature_icon is provided. if ( ! empty( $feature_icon ) ) { $input_html .= '
'; $input_html .= 'icon'; $input_html .= '
'; } $input_html .= '
'; // Display feature name - only show if feature_name exists (regardless of is_pro status). if ( ! empty( $feature_name ) ) { $input_html .= '' . $feature_name . ( $is_pro ? '' . esc_html__( 'PRO', 'accessibility-onetap' ) . '' : '' ) . ''; } // Feature description - only show if feature_desc is provided (regardless of is_pro status). if ( ! empty( $feature_desc ) ) { $input_html .= '' . $feature_desc . ''; } $input_html .= '
'; $input_html .= '
'; $input_html .= '
'; // Show PRO feature information if is_pro is true. if ( $is_pro ) { $input_html .= '
'; $input_html .= '

' . $pro_title . '

'; $input_html .= '

'; $input_html .= '' . $pro_link_text . ' ' . $pro_desc; $input_html .= '

'; $input_html .= '
'; } $input_html .= ''; $input_html .= '
'; $this->render_template( $args, $input_html, 'feature_card' ); } /** * Render a license key input field with status display and activation controls. * * Outputs a license key input field with status indicator and activation/deactivation button. * Includes license status display and proper validation attributes. * * @param array $args settings field args. */ public function callback_template_license( $args ) { $parsed_args = $this->parse_template_args( $args ); // Get license status and configuration. $license_status = $this->get_license_status_config(); // Build license input HTML. $html = $this->build_license_input_html( $args, $parsed_args, $license_status ); $this->render_template( $args, $html, 'text' ); } /** * Get license status configuration. * * @return array License status configuration. */ private function get_license_status_config() { $status = get_option( 'onetap_license_status' ); if ( $status ) { return array( 'label' => __( 'Active', 'accessibility-onetap' ), 'button_text' => __( 'Deactivate License', 'accessibility-onetap' ), 'status' => 1, 'readonly' => ' readonly="readonly"', ); } return array( 'label' => __( 'Inactive', 'accessibility-onetap' ), 'button_text' => __( 'Activate License', 'accessibility-onetap' ), 'status' => 0, 'readonly' => '', ); } /** * Build license input HTML. * * @param array $args Settings arguments. * @param array $parsed_args Parsed arguments. * @param array $license_status License status configuration. * @return string HTML output. */ private function build_license_input_html( $args, $parsed_args, $license_status ) { $html = '
'; // Compute readonly based on label being exactly "Active". $label_text = isset( $license_status['label'] ) ? (string) $license_status['label'] : ''; $is_active = ( '' !== $label_text && 'Active' === $label_text ); $readonly_attr = $is_active ? ' readonly="readonly"' : ''; // License status label. $html .= '' . esc_html( $license_status['label'] ) . ''; // License input field. $html .= ''; // License activation button. $button_class = 'save-changes license'; if ( $license_status['status'] ) { $button_class .= ' license-active'; } $html .= ''; $html .= '
'; return $html; } /** * Sanitize callback for Settings API. * * @param object $options sanitize options. */ public function sanitize_options( $options ) { if ( ! $options ) { $options = array(); } // Get all registered switch fields to check for missing checkboxes. $all_switch_fields = array(); foreach ( $this->settings_fields as $section => $section_options ) { foreach ( $section_options as $option ) { if ( isset( $option['type'] ) && 'switch' === $option['type'] ) { $all_switch_fields[ $option['name'] ] = $option; } } } // Handle missing checkbox values (set to 'off' if not present). foreach ( $all_switch_fields as $field_name => $field_config ) { if ( ! isset( $options[ $field_name ] ) ) { $options[ $field_name ] = 'off'; } } foreach ( $options as $option_slug => $option_value ) { $sanitize_callback = $this->get_sanitize_callback( $option_slug ); // If callback is set, call it. if ( $sanitize_callback ) { $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value ); continue; } } return $options; } /** * Get sanitization callback for given option slug. * * @param string $slug option slug. * * @return mixed string or bool false. */ public function get_sanitize_callback( $slug = '' ) { if ( empty( $slug ) ) { return false; } // Iterate over registered fields and see if we can find proper callback. foreach ( $this->settings_fields as $section => $options ) { foreach ( $options as $option ) { if ( $option['name'] !== $slug ) { continue; } // Return the callback name. return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false; } } return false; } /** * Get the value of a settings field. * * @param string $option settings field name. * @param string $section the section name this field belongs to. * @param string $default_value default value text if it's not found. * @return string */ public function get_option( $option, $section, $default_value = '' ) { $options = get_option( $section ); if ( isset( $options[ $option ] ) ) { return $options[ $option ]; } // For switch/checkbox type fields, return 'off' as default if no default is specified. if ( empty( $default_value ) ) { // Check if this is a switch field. foreach ( $this->settings_fields as $section_name => $section_options ) { foreach ( $section_options as $field ) { if ( $field['name'] === $option && isset( $field['type'] ) && 'switch' === $field['type'] ) { return 'off'; } } } } return $default_value; } /** * Show navigations as tab. * * Shows all the settings section labels as tab. */ public function show_navigation() { $html = ''; echo wp_kses( $html, array( 'h2' => array(), 'a' => array( 'href' => array(), 'class' => array(), 'id' => array(), ), ) ); } /** * Show the section settings forms. * * This function displays every sections in a different form. */ public function show_forms() { ?>
settings_sections as $form ) { ?>