adapter = $adapter ?: new ADECK_WordPress_Adapter(); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] ); add_action( 'wp_footer', [ $this, 'inject_accessibility_panel' ] ); add_action( 'wp_head', [ $this, 'inject_color_scheme_meta' ], 1 ); } private function get_panel_template_path() { return ADECK_PLUGIN_DIR . 'templates/wordpress/panel.php'; } private function get_frontend_script_config() { if ( null === $this->frontend_script_config ) { $this->frontend_script_config = $this->adapter->get_frontend_script_config(); if ( is_array( $this->frontend_script_config ) && isset( $this->frontend_script_config['widget_config'] ) && is_array( $this->frontend_script_config['widget_config'] ) ) { $this->widget_bootstrap = $this->frontend_script_config['widget_config']; } } return $this->frontend_script_config; } private function get_widget_bootstrap() { if ( null === $this->widget_bootstrap ) { $script_config = $this->get_frontend_script_config(); if ( is_array( $script_config ) && isset( $script_config['widget_config'] ) && is_array( $script_config['widget_config'] ) ) { $this->widget_bootstrap = $script_config['widget_config']; } else { $this->widget_bootstrap = $this->adapter->build_widget_bootstrap(); } } return $this->widget_bootstrap; } private function get_panel_context() { if ( null === $this->panel_context ) { $this->panel_context = $this->adapter->get_frontend_panel_context(); } return $this->panel_context; } private function get_color_context() { if ( null === $this->color_context ) { $this->color_context = $this->adapter->get_frontend_color_context(); } return $this->color_context; } private function should_render_widget() { if ( null !== $this->should_render_widget ) { return $this->should_render_widget; } $this->should_render_widget = ! method_exists( $this->adapter, 'should_render_frontend_widget' ) || $this->adapter->should_render_frontend_widget(); return $this->should_render_widget; } private function build_theme_variable_map( array $context ) { return [ '--adeck-panel-bg' => $context['bg'], '--adeck-panel-text' => $context['txt'], '--adeck-panel-text-10' => $this->append_hex_alpha( $context['txt'], '10' ), '--adeck-panel-text-20' => $this->append_hex_alpha( $context['txt'], '20' ), '--adeck-panel-text-25' => $this->append_hex_alpha( $context['txt'], '25' ), '--adeck-panel-text-30' => $this->append_hex_alpha( $context['txt'], '30' ), '--adeck-panel-text-40' => $this->append_hex_alpha( $context['txt'], '40' ), '--adeck-panel-text-99' => $this->append_hex_alpha( $context['txt'], '99' ), '--adeck-header-bg' => $context['hbg'], '--adeck-primary' => $context['p'], '--adeck-primary-0a' => $this->append_hex_alpha( $context['p'], '0A' ), '--adeck-primary-0d' => $this->append_hex_alpha( $context['p'], '0D' ), '--adeck-primary-0f' => $this->append_hex_alpha( $context['p'], '0F' ), '--adeck-primary-1a' => $this->append_hex_alpha( $context['p'], '1A' ), '--adeck-primary-1f' => $this->append_hex_alpha( $context['p'], '1F' ), '--adeck-primary-24' => $this->append_hex_alpha( $context['p'], '24' ), '--adeck-primary-38' => $this->append_hex_alpha( $context['p'], '38' ), '--adeck-primary-55' => $this->append_hex_alpha( $context['p'], '55' ), '--adeck-reset-bg' => $context['rbg'], '--adeck-reset-text' => $context['rtxt'], '--adeck-reset-hover' => $context['rhov'], '--adeck-widget-bg' => $context['wbg'], '--adeck-launcher-width' => absint( $context['wsize']['width'] ) . 'px', '--adeck-launcher-height' => absint( $context['wsize']['height'] ) . 'px', '--adeck-launcher-icon' => absint( $context['wsize']['icon'] ) . 'px', '--adeck-launcher-hover' => absint( $context['wsize']['hover'] ) . 'px', ]; } private function append_hex_alpha( $color, $alpha_suffix ) { $normalized = is_string( $color ) ? trim( $color ) : ''; if ( ! preg_match( '/^#[0-9a-fA-F]{6}$/', $normalized ) ) { return $normalized; } return $normalized . strtoupper( $alpha_suffix ); } public function enqueue_assets() { if ( ! $this->should_render_widget() ) { return; } $style_path = ADECK_PLUGIN_DIR . 'assets/css/adeck-style.css'; $widget_core_path = ADECK_PLUGIN_DIR . 'assets/js/adeck-widget-core.js'; $widget_helpers_path = ADECK_PLUGIN_DIR . 'assets/js/adeck-widget-helpers.js'; $widget_boot_path = ADECK_PLUGIN_DIR . 'assets/js/adeck-script.js'; $style_ver = file_exists( $style_path ) ? (string) filemtime( $style_path ) : ADECK_VERSION; $widget_core_ver = file_exists( $widget_core_path ) ? (string) filemtime( $widget_core_path ) : ADECK_VERSION; $widget_helpers_ver = file_exists( $widget_helpers_path ) ? (string) filemtime( $widget_helpers_path ) : ADECK_VERSION; $widget_boot_ver = file_exists( $widget_boot_path ) ? (string) filemtime( $widget_boot_path ) : ADECK_VERSION; wp_enqueue_style( 'adeck-style', ADECK_PLUGIN_URL . 'assets/css/adeck-style.css', [], $style_ver ); wp_enqueue_script( 'adeck-widget-core', ADECK_PLUGIN_URL . 'assets/js/adeck-widget-core.js', [], $widget_core_ver, true ); wp_enqueue_script( 'adeck-widget-helpers', ADECK_PLUGIN_URL . 'assets/js/adeck-widget-helpers.js', [ 'adeck-widget-core' ], $widget_helpers_ver, true ); wp_enqueue_script( 'adeck-script', ADECK_PLUGIN_URL . 'assets/js/adeck-script.js', [ 'adeck-widget-core', 'adeck-widget-helpers' ], $widget_boot_ver, true ); $script_config = $this->get_frontend_script_config(); wp_localize_script( 'adeck-script', 'adeck_i18n', $script_config['messages'] ); wp_localize_script( 'adeck-script', 'adeckWidgetConfig', $script_config['widget_config'] ); wp_register_script( 'adeck-dark-mode-bootstrap', '', [], $widget_core_ver, false ); wp_enqueue_script( 'adeck-dark-mode-bootstrap' ); wp_add_inline_script( 'adeck-dark-mode-bootstrap', $this->build_dark_mode_bootstrap_script(), 'before' ); wp_add_inline_style( 'adeck-style', $this->build_custom_colors_css() ); } public function inject_accessibility_panel() { if ( ! $this->should_render_widget() ) { return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $this->adapter->renderer()->render_template( $this->get_panel_template_path(), $this->get_panel_context() ); return; } public function inject_color_scheme_meta() { if ( ! $this->should_render_widget() ) { return; } echo '' . "\n"; } private function build_dark_mode_bootstrap_script() { $widget_bootstrap = $this->get_widget_bootstrap(); $storage_keys = isset( $widget_bootstrap['storageKeys'] ) && is_array( $widget_bootstrap['storageKeys'] ) ? $widget_bootstrap['storageKeys'] : []; $theme_storage_key = isset( $storage_keys['theme'] ) ? $storage_keys['theme'] : 'adeck_theme'; $storage_namespace = isset( $widget_bootstrap['storageNamespace'] ) && is_string( $widget_bootstrap['storageNamespace'] ) ? trim( $widget_bootstrap['storageNamespace'] ) : ''; $resolved_theme_storage_key = '' !== $storage_namespace ? $storage_namespace . ':' . $theme_storage_key : $theme_storage_key; return '(function(){try{var storedTheme=window.localStorage.getItem(' . wp_json_encode( $resolved_theme_storage_key ) . ');if(storedTheme==="dark"){document.documentElement.setAttribute("data-adeck-theme","dark");document.documentElement.classList.add("adeck-dark-mode","adeck-dark-mode-preload");}}catch(error){}}());'; } private function build_custom_colors_css() { $context = $this->get_color_context(); $variables = $this->build_theme_variable_map( $context ); $rules = []; foreach ( $variables as $name => $value ) { $rules[] = $name . ':' . $value . ';'; } return ':root{' . implode( '', $rules ) . '}'; } }