get_template(); } /** * Whether Blocksy's native Colour Mode Switch extension is active. * * @return bool */ public static function has_blocksy_color_mode() { $extensions = get_option( 'blocksy_active_extensions', array() ); if ( is_array( $extensions ) && in_array( 'color-mode-switch', $extensions, true ) ) { return true; } if ( function_exists( 'blc_get_extensions_status' ) ) { $status = blc_get_extensions_status(); if ( is_array( $status ) && ! empty( $status['color-mode-switch'] ) ) { return true; } } return (bool) apply_filters( 'blc_ldt_has_blocksy_color_mode', false ); } /** * Whether the toggle should render on the current request. * * @return bool */ public static function should_render() { if ( is_admin() || wp_doing_ajax() || is_customize_preview() ) { return false; } $settings = BLC_LDT_Settings::get(); if ( empty( $settings['show_on_mobile'] ) && empty( $settings['show_on_desktop'] ) ) { return false; } return (bool) apply_filters( 'blc_ldt_should_render', true ); } /** * Warn in admin if the theme is not supported. */ public static function admin_theme_notice() { if ( self::is_target_theme() || ! current_user_can( 'switch_themes' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && 'plugins' !== $screen->id ) { return; } echo '

'; echo esc_html__( 'Blocksy Light/Dark Toggle is active but Blocksy is not the current theme. The toggle only appears with Blocksy.', 'blocksy-light-dark-toggle' ); echo '

'; } /** * Prevent flash of wrong theme by applying Blocksy classes before paint. */ public static function inline_preference_script() { if ( ! self::should_render() ) { return; } $scheme = self::get_initial_scheme(); $settings = BLC_LDT_Settings::get(); $storage_keys = array_merge( array( BLC_LDT_STORAGE ), self::BLOCKSY_STORAGE_KEYS ); ?> ' . esc_html( $css ) . ''; } /** * Resolve saved scheme for first paint (cookie or user meta only). * * @return string light|dark|empty string when none saved. */ public static function get_initial_scheme() { if ( isset( $_COOKIE[ BLC_LDT_COOKIE ] ) ) { $cookie = sanitize_key( wp_unslash( $_COOKIE[ BLC_LDT_COOKIE ] ) ); if ( in_array( $cookie, array( 'light', 'dark' ), true ) ) { return apply_filters( 'blc_ldt_initial_scheme', $cookie ); } } if ( is_user_logged_in() ) { $meta = get_user_meta( get_current_user_id(), BLC_LDT_STORAGE, true ); if ( in_array( $meta, array( 'light', 'dark' ), true ) ) { return apply_filters( 'blc_ldt_initial_scheme', $meta ); } } return apply_filters( 'blc_ldt_initial_scheme', '' ); } /** * Enqueue styles and toggle script. */ public static function enqueue_assets() { if ( ! self::should_render() ) { return; } if ( ! self::has_blocksy_color_mode() ) { wp_enqueue_style( 'blc-ldt-fallback', BLC_LDT_URL . 'assets/css/dark-fallback.css', array(), BLC_LDT_VERSION ); } wp_enqueue_style( 'blc-ldt-overrides', BLC_LDT_URL . 'assets/css/dark-overrides.css', self::has_blocksy_color_mode() ? array() : array( 'blc-ldt-fallback' ), BLC_LDT_VERSION ); wp_enqueue_style( 'blc-ldt-toggle', BLC_LDT_URL . 'assets/css/toggle.css', array( 'blc-ldt-overrides' ), BLC_LDT_VERSION ); wp_enqueue_script( 'blc-ldt-toggle', BLC_LDT_URL . 'assets/js/toggle.js', array(), BLC_LDT_VERSION, true ); $settings = BLC_LDT_Settings::get(); wp_localize_script( 'blc-ldt-toggle', 'blcLdt', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'blc_ldt_save' ), 'storageKey' => BLC_LDT_STORAGE, 'cookieName' => BLC_LDT_COOKIE, 'cookiePath' => COOKIEPATH ? COOKIEPATH : '/', 'cookieDomain' => COOKIE_DOMAIN, 'isLoggedIn' => is_user_logged_in(), 'hasBlocksyColorMode' => self::has_blocksy_color_mode(), 'blocksyStorageKeys' => self::BLOCKSY_STORAGE_KEYS, 'respectOsPreference' => ! empty( $settings['respect_os_preference'] ), 'enableAnimation' => ! empty( $settings['enable_animation'] ), 'i18n' => array( 'light' => __( 'Switch to light mode', 'blocksy-light-dark-toggle' ), 'dark' => __( 'Switch to dark mode', 'blocksy-light-dark-toggle' ), 'lightLabel' => __( 'Light', 'blocksy-light-dark-toggle' ), 'darkLabel' => __( 'Dark', 'blocksy-light-dark-toggle' ), ), ) ); } /** * Output floating toggle button. */ public static function render_toggle() { if ( ! self::should_render() ) { return; } $saved = self::get_initial_scheme(); $is_dark = 'dark' === $saved; ?> 'Invalid scheme' ), 400 ); } if ( is_user_logged_in() ) { update_user_meta( get_current_user_id(), BLC_LDT_STORAGE, $scheme ); } wp_send_json_success(); } }