'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'show_in_rest' => false, ) ); } /** * Checkboxes of the tab "Writing": on only for "1" (or true); a missing field means unticked. Anything that * is not an array is not a form at all and keeps the factory defaults. * * @param mixed $input Raw value. * @return array{theme_styles: bool, clean_paste: bool} */ public static function sanitize( $input ) { $settings = array( 'theme_styles' => true, 'clean_paste' => true, ); if ( ! is_array( $input ) ) { return $settings; } foreach ( array_keys( $settings ) as $key ) { $value = $input[ $key ] ?? ''; $settings[ $key ] = true === $value || '1' === $value || 1 === $value; } return $settings; } /** * Settings with the factory defaults (all on) when nothing valid is stored. * * @return array{theme_styles: bool, clean_paste: bool} */ public static function settings() { $stored = get_option( self::OPTION, null ); return self::sanitize( is_array( $stored ) ? $stored : null ); } /** * CSS for the editor frame: font files and global styles of the theme, plus its content width. * Empty when switched off or when the theme has no theme.json. * * @return string */ public static function css() { if ( ! self::settings()['theme_styles'] || ! wp_theme_has_theme_json() ) { return ''; } ob_start(); wp_print_font_faces(); $fonts = (string) preg_replace( '#]*>#i', '', (string) ob_get_clean() ); $width = 'html body.mce-content-body{max-width:var(--wp--style--global--content-size, none);margin-left:auto;margin-right:auto;}'; return trim( $fonts . "\n" . wp_get_global_stylesheet() . "\n" . $width ); } /** * Passes the CSS to the TinyMCE plugin on the classic edit screen. * * @param string $hook_suffix Admin page. */ public static function enqueue( $hook_suffix ): void { if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) || ! current_user_can( 'edit_posts' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && $screen->is_block_editor() ) { return; } $css = self::css(); if ( '' === $css ) { return; } wp_register_script( self::HANDLE, false, array(), THREECE_VERSION, true ); wp_enqueue_script( self::HANDLE ); wp_add_inline_script( self::HANDLE, 'window.threeceEditorLook = ' . wp_json_encode( array( 'css' => $css ) ) . ';' ); } }