hook = (string) add_submenu_page( 'themes.php', __( 'Custom CSS', 'adminkeep' ), __( 'Custom CSS', 'adminkeep' ), 'edit_theme_options', self::PAGE, array( $this, 'render' ) ); } /** * Load WordPress's own code editor on this screen only. * * When the user has switched syntax highlighting off in their profile, * wp_enqueue_code_editor() returns false and the plain textarea is the editor. * * @param string $hook_suffix Current admin page. * @return void */ public function enqueue_editor( $hook_suffix ) { if ( '' === $this->hook || $hook_suffix !== $this->hook ) { return; } $settings = wp_enqueue_code_editor( array( 'type' => 'text/css' ) ); if ( false === $settings ) { return; } wp_add_inline_script( 'code-editor', sprintf( 'jQuery( function() { wp.codeEditor.initialize( %s, %s ); } );', wp_json_encode( self::EDITOR_ID ), wp_json_encode( $settings ) ) ); wp_add_inline_style( 'code-editor', '.adminkeep-css-screen .CodeMirror{height:60vh;border:1px solid #c3c4c7;}' ); } /** * Save a submission. * * @return void */ public function handle_save() { if ( empty( $_POST[ self::SAVE_FIELD ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Checked below, once this is known to be our form. return; } if ( ! self::can_edit() ) { wp_die( esc_html__( 'You are not allowed to edit CSS on this site.', 'adminkeep' ), '', 403 ); } check_admin_referer( self::NONCE ); // Not sanitized as text: CSS legitimately holds >, quotes and backslashes. It is validated // for markup instead, and tags are stripped again on output. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $css = isset( $_POST[ self::CSS_FIELD ] ) && is_string( $_POST[ self::CSS_FIELD ] ) ? wp_unslash( $_POST[ self::CSS_FIELD ] ) : ''; $css = str_replace( "\r\n", "\n", $css ); $valid = CustomCss::validate( $css ); if ( is_wp_error( $valid ) ) { $this->refused = array( 'css' => $css, 'message' => $valid->get_error_message(), ); return; } update_option( CustomCss::OPTION, $css, true ); wp_safe_redirect( add_query_arg( 'updated', '1', self::url() ) ); exit; } /** * Render the screen. * * @return void */ public function render() { if ( ! self::can_edit() ) { return; } $css = null === $this->refused ? CustomCss::css() : $this->refused['css']; echo '
%s
%s
%s
', esc_html__( 'This CSS applies to the front end of your site on every theme. Your theme\'s own Additional CSS is separate and is not changed here.', 'adminkeep' ) ); echo '