/inclusiveweb.php, now a headerless shim next to the main file. */ define( 'ACCESSONAUT_LEGACY_OPTION', 'inclusiveweb_settings' ); define( 'ACCESSONAUT_LEGACY_ENQUEUE_FILTER', 'inclusiveweb_widget_should_enqueue' ); define( 'ACCESSONAUT_LEGACY_PAGE', 'inclusiveweb' ); define( 'ACCESSONAUT_LEGACY_MAIN_FILE', 'inclusiveweb.php' ); require_once __DIR__ . '/appearance.php'; /** * Deleting an earlier copy runs its uninstall routine, which may remove the settings option. Keep a private copy on * every save and restore it on the next request, so switching from an old ZIP never loses the configuration. */ function accessonaut_backup_settings( $value ) { if ( is_array( $value ) ) { update_option( ACCESSONAUT_BACKUP_OPTION, $value, true ); } } add_action( 'add_option_' . ACCESSONAUT_OPTION, function ( $name, $value ) { accessonaut_backup_settings( $value ); }, 10, 2 ); add_action( 'update_option_' . ACCESSONAUT_OPTION, function ( $old_value, $value ) { accessonaut_backup_settings( $value ); }, 10, 2 ); /** One-time move of the settings stored by releases up to 1.4: copy to the new option, then delete the old one. */ function accessonaut_migrate_settings() { if ( false !== get_option( ACCESSONAUT_OPTION, false ) ) { return; } $legacy = get_option( ACCESSONAUT_LEGACY_OPTION, false ); if ( false === $legacy ) { return; } add_option( ACCESSONAUT_OPTION, $legacy ); if ( false !== get_option( ACCESSONAUT_OPTION, false ) ) { delete_option( ACCESSONAUT_LEGACY_OPTION ); } } add_action( 'plugins_loaded', 'accessonaut_restore_settings' ); function accessonaut_restore_settings() { accessonaut_migrate_settings(); $stored = get_option( ACCESSONAUT_OPTION, false ); if ( false !== $stored ) { // Self-healing: installations updated in place have settings but no copy yet. if ( is_array( $stored ) && false === get_option( ACCESSONAUT_BACKUP_OPTION, false ) ) { accessonaut_backup_settings( $stored ); } return; } $backup = get_option( ACCESSONAUT_BACKUP_OPTION, false ); /* * Restore the settings an earlier copy's uninstall routine removed. Without any settings (a site that was never * configured) store an empty option once: it is autoloaded, so later page loads read it from the options cache * instead of querying the missing option names on every request. */ add_option( ACCESSONAUT_OPTION, is_array( $backup ) ? $backup : array() ); } /** * Sites updated from 1.4 list /inclusiveweb.php as active. Replace it with the real main file (single site and * network activation), so the plugin stays active and WordPress shows one entry with the current basename. */ add_action( 'admin_init', 'accessonaut_migrate_plugin_basename' ); function accessonaut_migrate_plugin_basename() { $current = plugin_basename( ACCESSONAUT_PLUGIN_FILE ); $legacy = dirname( $current ) . '/' . ACCESSONAUT_LEGACY_MAIN_FILE; $active = get_option( 'active_plugins', array() ); if ( is_array( $active ) && in_array( $legacy, $active, true ) ) { $active = array_map( function ( $plugin ) use ( $legacy, $current ) { return $plugin === $legacy ? $current : $plugin; }, $active ); $active = array_values( array_unique( $active ) ); sort( $active ); update_option( 'active_plugins', $active ); } if ( function_exists( 'is_multisite' ) && is_multisite() ) { $network = get_site_option( 'active_sitewide_plugins', array() ); if ( is_array( $network ) && isset( $network[ $legacy ] ) ) { if ( ! isset( $network[ $current ] ) ) { $network[ $current ] = $network[ $legacy ]; } unset( $network[ $legacy ] ); update_site_option( 'active_sitewide_plugins', $network ); } } } /** Bundled German and Spanish translations (languages/). WordPress.org language packs take precedence once they exist. */ add_action( 'init', 'accessonaut_load_textdomain' ); function accessonaut_load_textdomain() { load_plugin_textdomain( 'accessonaut-accessibility', false, dirname( plugin_basename( ACCESSONAUT_PLUGIN_FILE ) ) . '/languages' ); } register_activation_hook( ACCESSONAUT_PLUGIN_FILE, 'accessonaut_activate' ); function accessonaut_activate() { accessonaut_migrate_settings(); $stored = get_option( ACCESSONAUT_OPTION, false ); if ( is_array( $stored ) ) { accessonaut_backup_settings( $stored ); } } /** This optional frontend connector never reads or changes WooCommerce orders. */ function accessonaut_declare_woocommerce_compatibility() { if ( class_exists( '\\Automattic\\WooCommerce\\Utilities\\FeaturesUtil' ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', ACCESSONAUT_PLUGIN_FILE, true ); \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', ACCESSONAUT_PLUGIN_FILE, true ); } } add_action( 'before_woocommerce_init', 'accessonaut_declare_woocommerce_compatibility' ); /** HTTPS origin only. No server-side request is made to this destination. */ function accessonaut_valid_origin( $value ) { return \Accessonaut\Shared\Connection::validOrigin( $value ); } function accessonaut_valid_project( $value ) { return \Accessonaut\Shared\Connection::validProject( $value ); } /** Validate saved values on every read, including changes outside the settings UI. */ function accessonaut_settings() { $stored = get_option( ACCESSONAUT_OPTION, array() ); $stored = is_array( $stored ) ? $stored : array(); $defaults = array( 'enabled' => false, 'service_url' => '', 'project_id' => '', 'position' => 'bottom-right', 'primary_color' => '#0f766e' ); $settings = array_merge( $defaults, array_intersect_key( $stored, $defaults ) ); $settings['enabled'] = true === $settings['enabled'] || '1' === $settings['enabled'] || 1 === $settings['enabled']; $settings['service_url'] = accessonaut_valid_origin( $settings['service_url'] ) ? untrailingslashit( $settings['service_url'] ) : ''; $settings['project_id'] = accessonaut_valid_project( $settings['project_id'] ) ? $settings['project_id'] : ''; $settings['position'] = in_array( $settings['position'], array( 'bottom-right', 'bottom-left', 'top-right', 'top-left' ), true ) ? $settings['position'] : 'bottom-right'; $settings['primary_color'] = is_string( $settings['primary_color'] ) && preg_match( '/\A#[a-fA-F0-9]{6}\z/', $settings['primary_color'] ) ? $settings['primary_color'] : '#0f766e'; // Preserve the appearance of 1.0 installs; new installs follow central settings. $legacy = ! array_key_exists( 'design_source', $stored ) && ( array_key_exists( 'position', $stored ) || array_key_exists( 'primary_color', $stored ) ); $settings['design_source'] = isset( $stored['design_source'] ) && in_array( $stored['design_source'], array( 'central', 'local' ), true ) ? $stored['design_source'] : ( $legacy ? 'local' : 'central' ); $display_defaults = array_merge( accessonaut_appearance_defaults(), array( 'position' => $settings['position'], 'primaryColor' => $settings['primary_color'] ) ); $settings['display_config'] = accessonaut_validate_appearance( $stored['display_config'] ?? array(), $display_defaults ); return $settings; } /** Retain the previous working destination on invalid input; never redirect it silently. */ function accessonaut_sanitize_settings( $input ) { $previous = accessonaut_settings(); if ( ! is_array( $input ) ) { add_settings_error( 'accessonaut', 'invalid_settings', __( 'Invalid settings. The previous configuration has been kept.', 'accessonaut-accessibility' ) ); return $previous; } $result = $previous; $result['enabled'] = isset( $input['enabled'] ) && in_array( $input['enabled'], array( '1', 1, true ), true ); foreach ( array( 'service_url', 'project_id' ) as $field ) { $value = isset( $input[ $field ] ) && is_string( $input[ $field ] ) ? trim( $input[ $field ] ) : ''; $valid = 'service_url' === $field ? accessonaut_valid_origin( $value ) : accessonaut_valid_project( $value ); if ( '' === $value || $valid ) { $result[ $field ] = 'service_url' === $field ? untrailingslashit( $value ) : $value; } else { add_settings_error( 'accessonaut', 'invalid_' . $field, 'service_url' === $field ? __( 'Enter an HTTPS service origin without a path, credentials, query or fragment. The previous origin has been kept.', 'accessonaut-accessibility' ) : __( 'Enter the public project ID from your Accessonaut account (letters, numbers, hyphen and underscore only, up to 128 characters).', 'accessonaut-accessibility' ) ); } } if ( isset( $input['position'] ) && in_array( $input['position'], array( 'bottom-right', 'bottom-left', 'top-right', 'top-left' ), true ) ) { $result['position'] = $input['position']; } if ( isset( $input['primary_color'] ) && is_string( $input['primary_color'] ) && preg_match( '/\A#[a-fA-F0-9]{6}\z/', $input['primary_color'] ) ) { $result['primary_color'] = $input['primary_color']; } if ( isset( $input['design_source'] ) && in_array( $input['design_source'], array( 'central', 'local' ), true ) ) { $result['design_source'] = $input['design_source']; } if ( isset( $input['display_config'] ) ) { $result['display_config'] = accessonaut_validate_appearance( $input['display_config'], $previous['display_config'], true ); $result['position'] = $result['display_config']['position']; $result['primary_color'] = $result['display_config']['primaryColor']; } else { $result['display_config']['position'] = $result['position']; $result['display_config']['primaryColor'] = $result['primary_color']; } if ( $result['enabled'] && ( '' === $result['service_url'] || '' === $result['project_id'] ) ) { $result['enabled'] = false; add_settings_error( 'accessonaut', 'missing_configuration', __( 'The widget remains disabled until a valid service URL and project ID are configured.', 'accessonaut-accessibility' ) ); } return $result; } add_action( 'admin_init', 'accessonaut_register_settings' ); function accessonaut_register_settings() { register_setting( 'accessonaut', ACCESSONAUT_OPTION, array( 'type' => 'array', 'sanitize_callback' => 'accessonaut_sanitize_settings', 'default' => array(), 'show_in_rest' => false ) ); } /** Old settings address (until 1.4): runs on admin_menu, before WordPress rejects the unknown page. */ add_action( 'admin_menu', 'accessonaut_redirect_legacy_page', 1 ); function accessonaut_redirect_legacy_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only redirect of a bookmarked settings address. $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; $target = accessonaut_legacy_page_redirect( $GLOBALS['pagenow'] ?? '', $page ); if ( '' !== $target ) { wp_safe_redirect( $target ); exit; } } function accessonaut_legacy_page_redirect( $screen, $page ) { return 'options-general.php' === $screen && ACCESSONAUT_LEGACY_PAGE === $page ? admin_url( 'options-general.php?page=accessonaut' ) : ''; } add_action( 'admin_menu', 'accessonaut_admin_menu' ); function accessonaut_admin_menu() { add_options_page( __( 'Accessonaut Accessibility', 'accessonaut-accessibility' ), 'Accessonaut', 'manage_options', 'accessonaut', 'accessonaut_render_settings' ); } add_action( 'admin_enqueue_scripts', 'accessonaut_admin_assets' ); function accessonaut_admin_assets( $hook ) { if ( 'settings_page_accessonaut' !== $hook || ! current_user_can( 'manage_options' ) ) { return; } wp_enqueue_style( 'accessonaut-admin', plugins_url( 'assets/admin.css', ACCESSONAUT_PLUGIN_FILE ), array(), ACCESSONAUT_VERSION ); wp_enqueue_script( 'accessonaut-admin', plugins_url( 'assets/admin.js', ACCESSONAUT_PLUGIN_FILE ), array(), ACCESSONAUT_VERSION, array( 'in_footer' => true, 'strategy' => 'defer' ) ); wp_localize_script( 'accessonaut-admin', 'accessonautAdminL10n', accessonaut_admin_script_strings() ); } add_filter( 'plugin_action_links_' . plugin_basename( ACCESSONAUT_PLUGIN_FILE ), 'accessonaut_settings_link' ); function accessonaut_settings_link( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'accessonaut-accessibility' ) . '' ); return $links; } function accessonaut_render_settings() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = accessonaut_settings(); ?>

· ·

true, 'strategy' => 'defer' ) ); } add_filter( 'script_loader_tag', 'accessonaut_script_attributes', 10, 3 ); function accessonaut_script_attributes( $tag, $handle, $src ) { if ( 'accessonaut-widget' !== $handle ) { return $tag; } $settings = accessonaut_settings(); if ( ! $settings['enabled'] || '' === $settings['service_url'] || '' === $settings['project_id'] || ! accessonaut_widget_allowed( $settings ) ) { return ''; } $display = $settings['display_config']; $attributes = array( 'id' => 'accessonaut-widget-js', 'src' => esc_url( $src ), 'data-project-id' => $settings['project_id'], 'data-language' => $display['language'], 'data-theme' => $display['theme'], 'data-icon' => $display['icon'], 'data-position' => $display['position'], 'data-primary-color' => $display['primaryColor'], 'data-design-source' => $settings['design_source'], ); if ( 'local' === $settings['design_source'] ) { $attributes['data-display-config'] = wp_json_encode( $display ); } $attributes['referrerpolicy'] = 'no-referrer'; $attributes['defer'] = true; // wp_get_script_tag() escapes every attribute value and applies the wp_script_attributes filter. return wp_get_script_tag( $attributes ); }