*/ class Frontend { /** * The ID of this plugin. * * @since 3.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 3.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Admin modules of the plugin * * @var array */ private static $modules; /** * Currently active modules * * @var array */ private static $active_modules; /** * Existing modules * * @var array */ public static $existing_modules; /** * Initialize the class and set its properties. * * @since 3.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; self::$modules = $this->get_default_modules(); $this->load_modules(); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 1 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_checker_assets' ), 2 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_toolbar_widget' ), 3 ); add_action( 'wp_head', array( $this, 'inject_checker_icon' ), 999 ); } /** * Get the default modules array * * @since 3.0.0 * @return array */ public function get_default_modules() { $modules = array( 'fixes' ); return $modules; } /** * Load all the modules * * @return void */ public function load_modules() { foreach ( self::$modules as $module ) { $parts = explode( '_', $module ); $class = implode( '_', $parts ); $class_name = 'WebYes\AccessibilityPlus\Lite\\Frontend\\Modules\\' . ucfirst( $module ) . '\\' . ucfirst( $class ); if ( class_exists( $class_name ) ) { $module_obj = new $class_name( $module ); if ( $module_obj instanceof $class_name ) { if ( $module_obj->is_active() ) { self::$active_modules[ $module ] = true; } } } } } /** * Enqueue front end scripts * * @return void */ public function enqueue_scripts() { $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'assets/js/script' . $suffix . '.js', array(), $this->version, true ); $config = Settings::get_instance()->get(); $config = apply_filters( 'wya11y_config', $config ); wp_localize_script( $this->plugin_name, '_wyA11yConfig', $config ); } /** * Enqueue checker assets (content script and dashboard bundle) * * @return void */ public function enqueue_checker_assets() { $settings = Settings::get_instance(); $checker_settings = $settings->get( 'checker' ); // Only enqueue if checker is enabled if ( empty( $checker_settings['enabled'] ) ) { return; } // Check user capability - only load assets for users who can edit posts (Editors and above) if ( ! current_user_can( 'edit_posts' ) ) { return; } // Enqueue content script (vanilla JS, no build) wp_enqueue_script( 'wya11y-content-script', plugin_dir_url( __FILE__ ) . 'assets/checker/js/content-script.js', array(), $this->version, true ); // Check if built checker bundle exists $checker_js_path = dirname( __FILE__ ) . '/../admin/app/dist/assets/checker.js'; $checker_css_path = dirname( __FILE__ ) . '/../admin/app/dist/assets/react.css'; $checker_js = null; $checker_css = null; if ( file_exists( $checker_js_path ) ) { $checker_js = 'assets/checker.js'; wp_enqueue_script( 'wya11y-checker-dashboard', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/app/dist/' . $checker_js, array(), $this->version, true ); // Add filter to add type="module" attribute for this script add_filter( 'script_loader_tag', array( $this, 'add_module_type_to_checker' ), 10, 3 ); } if ( file_exists( $checker_css_path ) ) { $checker_css = 'assets/react.css'; wp_enqueue_style( 'wya11y-checker-styles', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/app/dist/' . $checker_css, array(), $this->version ); } // Localize script with config $position = isset( $checker_settings['placement']['position'] ) ? $checker_settings['placement']['position'] : 'right'; wp_localize_script( 'wya11y-content-script', 'wya11yChecker', array( 'position' => $position, 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wya11y_checker' ), 'assetsUrl' => plugin_dir_url( __FILE__ ) . 'assets/checker/', 'dashboardUrl' => $checker_js ? plugin_dir_url( dirname( __FILE__ ) ) . 'admin/app/dist/' . $checker_js : '', 'dashboardCssUrl' => $checker_css ? plugin_dir_url( dirname( __FILE__ ) ) . 'admin/app/dist/' . $checker_css : '', 'version' => $this->version ) ); } /** * Enqueue toolbar widget script with proper WordPress enqueuing and localization * * @return void */ public function enqueue_toolbar_widget() { $settings = Settings::get_instance(); $toolbar_settings = $settings->get( 'toolbar' ); // Check if toolbar is enabled if ( (empty( $toolbar_settings['enabled']) && ! $toolbar_settings['enabled']) || (! isset($toolbar_settings['features']) && ! is_array($toolbar_settings['features']) ) ){ return; } // Prepare the configuration for the widget $hide_on_small_screen = isset( $toolbar_settings['features']['hideOnSmallScreens'] ) ? $toolbar_settings['features']['hideOnSmallScreens'] : false; $allowed_settings_options = array( 'font-size' => 'fontSize', 'high-contrast' => 'contrast', 'grayscale' => 'grayscale', ); $allowed_settings = array(); $is_feature_enabled = false; foreach ( $allowed_settings_options as $key => $value ) { if( isset( $toolbar_settings['features'][ $value ] ) && ! empty( $toolbar_settings['features'][ $value ] ) && $toolbar_settings['features'][ $value ] === true ){ $allowed_settings[ $key ] = true; $is_feature_enabled = true; }else{ $allowed_settings[ $key ] = false; } } if(!$is_feature_enabled){ return; } // Prepare configuration array $config = array( 'iconId' => 'default', 'toolbarSettings' => array( 'hideFromSmallScreen' => isset( $toolbar_settings['features']['hideOnSmallScreens'] ) ? $toolbar_settings['features']['hideOnSmallScreens'] : false, ), 'position' => array( 'mobile' => isset( $toolbar_settings['placement']['position'] ) ? $toolbar_settings['placement']['position'] : 'bottom-right', 'desktop' => isset( $toolbar_settings['placement']['position'] ) ? $toolbar_settings['placement']['position'] : 'bottom-right', 'vertical' => isset( $toolbar_settings['placement']['verticalOffset'] ) ? $toolbar_settings['placement']['verticalOffset'] : 20, ), 'allowedSettings' => $allowed_settings, 'language' => array( 'default' => get_locale(), 'selected' => array(), ), 'translations' => array( 'Accessibility toolbar' => __( 'Accessibility toolbar', 'accessibility-plus' ), 'Adjust Font Sizing' => __( 'Adjust Font Sizing', 'accessibility-plus' ), 'High contrast' => __( 'High contrast', 'accessibility-plus' ), 'Grayscale' => __( 'Grayscale', 'accessibility-plus' ), ), ); // Enqueue the widget script with proper versioning wp_enqueue_script( 'accessibility-plus-widget', plugin_dir_url( __FILE__ ) . 'assets/js/widget.min.js', array(), // No dependencies $this->version, // Use plugin version for cache busting true // Load in footer ); // Localize script to pass configuration wp_localize_script( 'accessibility-plus-widget', '_accessibilityPlusConfig', $config ); } /** * Inject accessibility checker icon in the footer * * Only displayed to users with 'edit_posts' capability (Editors and Administrators) * to ensure only users who can make changes to content can access the scanner. * * @return void */ public function inject_checker_icon() { $settings = Settings::get_instance(); $checker_settings = $settings->get( 'checker' ); // Check if checker is enabled if ( empty( $checker_settings['enabled'] ) ) { return; } // Check user capability - only show to users who can edit posts (Editors and above) if ( ! current_user_can( 'edit_posts' ) ) { return; } // Get position settings $position = isset( $checker_settings['placement']['position'] ) ? $checker_settings['placement']['position'] : 'right'; // Output the checker icon HTML ?>