'#1d4ed8', 'green' => '#15803d', 'red' => '#b91c1c', 'purple' => '#6d28d9', 'orange' => '#c2410c', 'teal' => '#0f766e', 'gray' => '#4b5563', 'black' => '#111827', ); /** * Hooks. */ public static function init(): void { add_action( 'init', array( __CLASS__, 'register_setting' ) ); add_action( 'init', array( __CLASS__, 'accent_style' ), 20 ); add_filter( 'body_class', array( __CLASS__, 'body_class' ) ); add_filter( 'tiny_mce_before_init', array( __CLASS__, 'tinymce' ) ); } /** * Same style class and accent in the classic editor, which already loads the element stylesheet, so elements * look while writing as they will on the page. * * @param mixed $init TinyMCE settings. * @return mixed */ public static function tinymce( $init ) { if ( ! is_array( $init ) ) { return $init; } $settings = self::settings(); $class = 'threece-style-' . $settings['style']; $init['body_class'] = trim( ( is_string( $init['body_class'] ?? null ) ? $init['body_class'] : '' ) . ' ' . $class ); if ( 'theme' !== $settings['accent'] ) { $style = 'body{--threece-element-accent:' . self::ACCENTS[ $settings['accent'] ] . '}'; $init['content_style'] = trim( ( is_string( $init['content_style'] ?? null ) ? $init['content_style'] : '' ) . ' ' . $style ); } return $init; } /** * Style class on the page body, so the element stylesheet can switch between the three styles. * * @param mixed $classes Body classes (other plugins may hand on anything). * @return string[] */ public static function body_class( $classes ): array { $classes = is_array( $classes ) ? $classes : array(); $classes[] = 'threece-style-' . self::settings()['style']; return $classes; } /** * A chosen accent travels with the element stylesheet: printed only on pages that use an element, and only as * one fixed value from the palette. "From the theme" prints nothing. */ public static function accent_style(): void { $accent = self::settings()['accent']; if ( 'theme' === $accent ) { return; } wp_add_inline_style( 'threece-elements', 'body{--threece-element-accent:' . self::ACCENTS[ $accent ] . '}' ); } /** * Registers the setting so every save goes through sanitize(). */ public static function register_setting(): void { register_setting( self::GROUP, self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'show_in_rest' => false, ) ); } /** * Accent colors by name. * * @return array */ public static function accents(): array { return self::ACCENTS; } /** * Names of the accent colors, in the order of ACCENTS. * * @return array */ public static function accent_labels(): array { return array( 'blue' => __( 'Blue', '3task-toolbar-for-classic-editor' ), 'green' => __( 'Green', '3task-toolbar-for-classic-editor' ), 'red' => __( 'Red', '3task-toolbar-for-classic-editor' ), 'purple' => __( 'Purple', '3task-toolbar-for-classic-editor' ), 'orange' => __( 'Orange', '3task-toolbar-for-classic-editor' ), 'teal' => __( 'Teal', '3task-toolbar-for-classic-editor' ), 'gray' => __( 'Gray', '3task-toolbar-for-classic-editor' ), 'black' => __( 'Black', '3task-toolbar-for-classic-editor' ), ); } /** * Names of the element styles, in the order of STYLES. * * @return array */ public static function style_labels(): array { return array( 'schlicht' => __( 'Plain', '3task-toolbar-for-classic-editor' ), 'weich' => __( 'Soft', '3task-toolbar-for-classic-editor' ), 'kontur' => __( 'Outline', '3task-toolbar-for-classic-editor' ), ); } /** * Keeps a known style and a known accent name; each field falls back on its own. Anything that is not an * array is not a form and gives the factory defaults. * * @param mixed $input Raw value. * @return array{style: string, accent: string} */ public static function sanitize( $input ): array { $settings = array( 'style' => 'schlicht', 'accent' => 'theme', ); if ( ! is_array( $input ) ) { return $settings; } $style = $input['style'] ?? ''; $accent = $input['accent'] ?? ''; if ( is_string( $style ) && in_array( $style, self::STYLES, true ) ) { $settings['style'] = $style; } if ( is_string( $accent ) && isset( self::ACCENTS[ $accent ] ) ) { $settings['accent'] = $accent; } return $settings; } /** * Saved settings, cleaned again so an old or broken option never reaches the page. * * @return array{style: string, accent: string} */ public static function settings(): array { return self::sanitize( get_option( self::OPTION, array() ) ); } }