validate( $candidate ); if ( $result['errors'] ) { return new \WP_Error( 'loginstudio_invalid_styles', __( 'Invalid preview settings.', 'login-brush' ) ); } $settings = $result['settings']; if ( ! $settings['general']['enabled'] ) { return ''; } $rules = array(); $colors = array( array( 'background', 'color', 'body.login.loginstudio-active', 'background-color' ), ); foreach ( $colors as $mapping ) { if ( ! in_array( $mapping[0], $groups, true ) ) { continue; } $value = $settings[ $mapping[0] ][ $mapping[1] ]; if ( '' !== $value ) { $rules[] = $mapping[2] . '{' . $mapping[3] . ':' . $value . ';}'; } } $forms = 'body.login.loginstudio-active #login :is(#loginform,#lostpasswordform,#resetpassform,#registerform)'; if ( in_array( 'general', $groups, true ) ) { $this->declarations( $rules, 'body.login.loginstudio-active', $settings['general'], array( 'text_color' => 'color' ) ); $fonts = array( 'system' => 'system-ui,sans-serif', 'sans' => 'Arial,Helvetica,sans-serif', 'serif' => 'Georgia,serif', 'monospace' => 'monospace', ); if ( 'inherit' !== $settings['general']['font_family'] ) { $rules[] = 'body.login.loginstudio-active{font-family:' . $fonts[ $settings['general']['font_family'] ] . ';}'; $rules[] = $forms . ' :where(.input,#wp-submit){font-family:inherit;}'; } } if ( in_array( 'button', $groups, true ) ) { $this->button_rules( $rules, $settings['button'], $forms ); } if ( in_array( 'links', $groups, true ) ) { $links = 'body.login.loginstudio-active :is(#nav,#backtoblog,.privacy-policy-page-link) a'; $link_settings = $settings['links']; $fallback = ''; if ( in_array( 'background', $groups, true ) && '' !== $settings['background']['color'] ) { $background = $this->luminance( $settings['background']['color'] ); $fallback = $background + 0.05 >= sqrt( 1.05 * 0.05 ) ? '#000000' : '#ffffff'; // Only inherited core colors may change; explicit choices remain authoritative. foreach ( array( 'color' => array( '#50575e', '#2271b1' ), 'hover_color' => array( '#135e96', '#2271b1' ), ) as $key => $inherited ) { if ( '' !== $link_settings[ $key ] ) { continue; } foreach ( $inherited as $color ) { $foreground = $this->luminance( $color ); if ( ( max( $foreground, $background ) + 0.05 ) / ( min( $foreground, $background ) + 0.05 ) < 4.5 ) { $link_settings[ $key ] = $fallback; break; } } } } $this->declarations( $rules, $links, $link_settings, array( 'color' => 'color', 'font_size' => 'font-size', ) ); $this->declarations( $rules, $links . ':is(:hover,:focus-visible)', $link_settings, array( 'hover_color' => 'color' ) ); if ( '' === $settings['links']['hover_color'] && '' !== $link_settings['hover_color'] ) { // Equal fallback colors still need a visible pointer interaction cue. $rules[] = $links . ':hover{text-decoration:underline;}'; } if ( '' !== $link_settings['color'] || '' !== $link_settings['hover_color'] || null !== $link_settings['font_size'] ) { $rules[] = $links . '{overflow-wrap:anywhere;}'; $rules[] = $links . ':focus-visible{outline:2px solid ' . ( '' !== $fallback ? $fallback : 'var(--wp-admin-theme-color,#2271b1)' ) . ';outline-offset:2px;}'; $rules[] = '@media (forced-colors:active){' . $links . ':focus-visible{outline-color:Highlight;}}'; } } if ( in_array( 'messages', $groups, true ) ) { $messages = 'body.login.loginstudio-active #login > :is(#login_error,.message,.error,.success,.notice)'; $this->declarations( $rules, $messages, $settings['messages'], array( 'color' => 'color', 'background_color' => 'background-color', 'border_color' => 'border-color', 'font_size' => 'font-size', 'border_radius' => 'border-radius', ) ); } if ( in_array( 'remember_me', $groups, true ) ) { $remember = 'body.login.loginstudio-active #loginform .forgetmenot'; $this->declarations( $rules, $remember . ' label', $settings['remember_me'], array( 'color' => 'color', 'font_size' => 'font-size', ) ); $this->declarations( $rules, $remember . ' #rememberme', $settings['remember_me'], array( 'accent_color' => 'accent-color' ) ); if ( null !== $settings['remember_me']['font_size'] ) { $rules[] = $remember . ' label{overflow-wrap:anywhere;}'; } } if ( in_array( 'form', $groups, true ) ) { $this->form_rules( $rules, $settings['form'], $forms ); } if ( in_array( 'labels', $groups, true ) ) { $labels = $forms . ' label:is([for="user_login"],[for="user_pass"],[for="user_email"],[for="pass1"],[for="pass2"])'; $this->declarations( $rules, $labels, $settings['labels'], array( 'color' => 'color', 'font_size' => 'font-size', 'font_weight' => 'font-weight', ) ); } if ( in_array( 'fields', $groups, true ) ) { $this->field_rules( $rules, $settings['fields'], $forms ); } foreach ( array( 'background' => 'body.login.loginstudio-active', 'logo' => 'body.login.loginstudio-active #login h1 a', ) as $group => $selector ) { if ( ! in_array( $group, $groups, true ) ) { continue; } $image = $settings[ $group ]['image_id'] ? Media::image( $settings[ $group ]['image_id'] ) : false; if ( $settings[ $group ]['image_id'] ) { if ( false === $image ) { return new \WP_Error( 'loginstudio_missing_image', __( 'Preview image is unavailable.', 'login-brush' ) ); } $url = $image['url']; // CSS string escaping also excludes HTML style terminators in inline output. $url = preg_replace_callback( '/[\x00-\x20\x7f"\\\\<>]/', static function ( $parts ) { return '\\' . dechex( ord( $parts[0] ) ) . ' '; }, $url ); $rules[] = $selector . '{background-image:url("' . $url . '");}'; } if ( 'background' === $group && $image ) { foreach ( array( 'position', 'size', 'repeat', 'attachment' ) as $property ) { $value = $settings['background'][ $property ]; if ( 'inherit' !== $value ) { $value = 'position' === $property ? str_replace( '-', ' ', $value ) : $value; $rules[] = $selector . '{background-' . $property . ':' . $value . ';}'; } } } if ( 'logo' === $group ) { $width = $settings['logo']['width']; if ( $image || null !== $width ) { $ratio = $image ? $image['width'] . '/' . $image['height'] : '1/1'; $rules[] = $selector . '{' . ( null !== $width ? 'width:' . $width . 'px;' : '' ) . 'max-width:100%;height:auto;aspect-ratio:' . $ratio . ';background-size:contain;background-position:center;background-repeat:no-repeat;}'; } if ( null !== $settings['logo']['border_radius'] ) { $radius = $settings['logo']['border_radius']; // Core paints the image as this aspect-ratio-matched link's background. $rules[] = $selector . '{border-radius:' . ( '50%' === $radius ? '50%' : $radius . 'px' ) . ';}'; } if ( null !== $settings['logo']['spacing'] ) { $rules[] = $selector . '{margin-bottom:' . $settings['logo']['spacing'] . 'px;}'; } } } $this->editor_rules( $rules, $settings, $groups, $forms ); return implode( "\n", $rules ); } /** * Additional structured editor controls use the same preview/native selectors. * * @param array $rules Output declarations. * @param array $settings Normalized settings. * @param array $groups Enabled groups. * @param string $forms Native forms. */ private function editor_rules( array &$rules, array $settings, array $groups, $forms ) { $body = 'body.login.loginstudio-active'; $links = $body . ' :is(#nav,#backtoblog,.privacy-policy-page-link) a'; $messages = $body . ' #login > :is(#login_error,.message,.error,.success,.notice)'; $targets = array( 'general' => $body, 'labels' => $forms . ' label:is([for="user_login"],[for="user_pass"],[for="user_email"],[for="pass1"],[for="pass2"])', 'fields' => $forms . ' .input:is([type="text"],[type="password"],[type="email"])', 'button' => $forms . ' #wp-submit:not(:disabled):not(.disabled):not([aria-disabled="true"])', 'links' => $links, 'messages' => $messages, 'remember_me' => $body . ' #loginform .forgetmenot label', ); $fonts = array( 'system' => 'system-ui,sans-serif', 'sans' => 'Arial,Helvetica,sans-serif', 'serif' => 'Georgia,serif', 'monospace' => 'monospace', ); foreach ( $targets as $group => $selector ) { if ( ! in_array( $group, $groups, true ) ) { continue; } $values = $settings[ $group ]; if ( 'general' !== $group ) { foreach ( array( 'font_family', 'font_size', 'font_weight', 'line_height', 'letter_spacing', 'text_align' ) as $key ) { if ( null === $values[ $key ] || 'inherit' === $values[ $key ] ) { $values[ $key ] = $settings['general'][ $key ]; } } } $this->declarations( $rules, $selector, $values, array( 'font_size' => 'font-size', 'font_weight' => 'font-weight', 'letter_spacing' => 'letter-spacing', ) ); if ( 'inherit' !== $values['font_family'] ) { $rules[] = $selector . '{font-family:' . $fonts[ $values['font_family'] ] . ';}'; } if ( null !== $values['line_height'] ) { $rules[] = $selector . '{line-height:' . $values['line_height'] . '%;}'; } if ( 'inherit' !== $values['text_align'] ) { $alignment_selector = 'links' === $group ? $body . ' :is(#nav,#backtoblog,.privacy-policy-page-link)' : $selector; $rules[] = $alignment_selector . '{text-align:' . $values['text_align'] . ';}'; if ( 'labels' === $group ) { $rules[] = $selector . '{display:block;}'; } if ( 'remember_me' === $group ) { $justify = array( 'left' => 'flex-start', 'center' => 'center', 'right' => 'flex-end', ); $rules[] = $body . ' #loginform .forgetmenot{float:none;width:100%;display:flex;justify-content:' . $justify[ $values['text_align'] ] . ';}'; } } if ( 'messages' === $group ) { // Core notices give paragraphs their own typography instead of inheriting. $paragraph = ( null !== $values['font_size'] ? 'font-size:inherit;' : '' ) . ( null !== $values['line_height'] ? 'line-height:inherit;' : '' ); if ( '' !== $paragraph ) { $rules[] = $selector . ' p{' . $paragraph . '}'; } } } if ( in_array( 'general', $groups, true ) ) { $page = $settings['general']; $column = $body . ' #login'; if ( 'inherit' !== $page['content_alignment'] ) { $margins = array( 'left' => '20px auto 20px 20px', 'center' => '20px auto', 'right' => '20px 20px 20px auto', ); $rules[] = $column . '{margin:' . $margins[ $page['content_alignment'] ] . ';max-width:calc(100% - 40px);}'; } if ( 'inherit' !== $page['vertical_position'] ) { $positions = array( 'top' => 'flex-start', 'center' => 'center', 'bottom' => 'flex-end', ); // Auto margins absorb spare height, but never clip tall forms above the viewport. $rules[] = $body . '{height:auto;min-height:100vh;box-sizing:border-box;display:flex;flex-direction:column;justify-content:' . $positions[ $page['vertical_position'] ] . ';}'; $rules[] = $column . '{padding-top:0;flex-shrink:0;}'; } if ( null !== $page['page_spacing'] ) { $rules[] = $column . '{padding-top:min(' . $page['page_spacing'] . 'px,15vh);padding-bottom:20px;}'; } } if ( in_array( 'logo', $groups, true ) && 'inherit' !== $settings['logo']['alignment'] ) { $rules[] = $body . ' #login h1 a{margin-left:' . ( 'left' === $settings['logo']['alignment'] ? '0' : 'auto' ) . ';margin-right:' . ( 'right' === $settings['logo']['alignment'] ? '0' : 'auto' ) . ';}'; } if ( in_array( 'form', $groups, true ) && null !== $settings['form']['spacing'] ) { $rules[] = $forms . '{margin-top:' . $settings['form']['spacing'] . 'px;}'; } if ( in_array( 'button', $groups, true ) ) { $button = $settings['button']; $selector = $targets['button']; $this->declarations( $rules, $selector, $button, array( 'width' => 'width', 'border_width' => 'border-width', 'padding_x' => 'padding-inline', 'padding_y' => 'padding-block', ) ); if ( null !== $button['width'] || null !== $button['padding_x'] || null !== $button['padding_y'] ) { $rules[] = $selector . '{box-sizing:border-box;max-width:100%;height:auto;}'; } $this->declarations( $rules, $selector . ':focus-visible', $button, array( 'focus_color' => 'color', 'focus_background_color' => 'background-color', 'focus_border_color' => 'border-color', ) ); } if ( in_array( 'links', $groups, true ) ) { $this->declarations( $rules, $links . ':focus-visible', $settings['links'], array( 'focus_color' => 'color' ) ); } if ( in_array( 'messages', $groups, true ) ) { foreach ( array( 'error' => ':is(#login_error,.error,.notice-error)', 'success' => ':is(.success,.notice-success)', ) as $state => $selector ) { $this->declarations( $rules, $body . ' #login > ' . $selector, $settings['messages'], array( $state . '_color' => 'color', $state . '_background_color' => 'background-color', $state . '_border_color' => 'border-color', ) ); } } } /** * WCAG sRGB relative luminance, using only centrally validated hex colors. * * @param string $color Validated three- or six-digit hex color. * @return float */ private function luminance( $color ) { $hex = substr( $color, 1 ); if ( 3 === strlen( $hex ) ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } $values = array(); foreach ( array( 0, 2, 4 ) as $offset ) { $value = hexdec( substr( $hex, $offset, 2 ) ) / 255; $values[] = $value <= 0.04045 ? $value / 12.92 : pow( ( $value + 0.055 ) / 1.055, 2.4 ); } return $values[0] * 0.2126 + $values[1] * 0.7152 + $values[2] * 0.0722; } /** * Emit canonical scalar declarations; validation stays in Settings_Sanitizer. * * @param array $rules Output rules. * @param string $selector Fixed selector. * @param array $values Validated settings. * @param array $mapping Fixed setting/property map. */ private function declarations( array &$rules, $selector, array $values, array $mapping ) { $css = ''; foreach ( $mapping as $key => $property ) { $value = $values[ $key ]; if ( null !== $value && '' !== $value ) { $unit = is_int( $value ) && 'font-weight' !== $property ? 'px' : ''; $css .= $property . ':' . $value . $unit . ';'; } } if ( '' !== $css ) { $rules[] = $selector . '{' . $css . '}'; } } /** * Native submit controls, with independent keyboard focus and core disabled styles. * * @param array $rules Output rules. * @param array $button Validated button settings. * @param string $forms Known native forms. */ private function button_rules( array &$rules, array $button, $forms ) { if ( '' !== $button['text'] ) { $rules[] = $forms . ' button#wp-submit{box-sizing:border-box;max-width:100%;height:auto;line-height:1.5;white-space:normal;overflow-wrap:anywhere;}'; } $enabled = ':not(:disabled):not(.disabled):not([aria-disabled="true"])'; $selector = $forms . ' #wp-submit' . $enabled; $this->declarations( $rules, $selector, $button, array( 'background_color' => 'background-color', 'color' => 'color', 'border_color' => 'border-color', 'border_radius' => 'border-radius', 'font_size' => 'font-size', 'font_weight' => 'font-weight', ) ); $hover = $forms . ' #wp-submit:hover' . $enabled . ',' . $forms . ' #wp-submit:focus-visible' . $enabled; $this->declarations( $rules, $hover, $button, array( 'hover_background_color' => 'background-color', 'hover_color' => 'color', 'hover_border_color' => 'border-color', ) ); if ( null !== $button['height'] ) { $rules[] = $selector . '{height:auto;min-height:' . $button['height'] . 'px;}'; } $custom = array_filter( $button, static function ( $value ) { return null !== $value && '' !== $value && 'inherit' !== $value; } ); if ( $custom ) { $rules[] = $selector . '{box-sizing:border-box;max-width:100%;}'; $rules[] = $forms . ' #wp-submit:focus' . $enabled . '{outline:2px solid var(--wp-admin-theme-color,#2271b1);outline-offset:2px;}'; } } /** * Native credential cards only; the containing column supplies desired width. * * @param array $rules Output rules. * @param array $form Validated form settings. * @param string $selector Credential form selector. */ private function form_rules( array &$rules, array $form, $selector ) { $this->declarations( $rules, $selector, $form, array( 'background_color' => 'background-color', 'border_width' => 'border-width', 'border_color' => 'border-color', 'border_radius' => 'border-radius', ) ); if ( null !== $form['width'] ) { $rules[] = 'body.login.loginstudio-active:is(.login-action-login,.login-action-lostpassword,.login-action-rp,.login-action-resetpass,.login-action-register) #login{width:' . $form['width'] . 'px;max-width:calc(100% - 40px);}'; } if ( null !== $form['padding'] ) { // Keep the approved maximum usable on a narrow viewport without extra settings. $rules[] = $selector . '{padding:min(' . $form['padding'] . 'px,6vw);}'; if ( $form['padding'] < 4 ) { // Contain core floats without clipping the independent input focus outline. $rules[] = $selector . '{display:flow-root;overflow:visible;}'; } } if ( null !== $form['border_width'] || '' !== $form['border_color'] ) { $rules[] = $selector . '{border-style:solid;}'; } $shadows = array( 'none' => 'none', 'subtle' => '0 1px 3px rgba(0,0,0,.08)', 'medium' => '0 4px 16px rgba(0,0,0,.16)', 'strong' => '0 8px 32px rgba(0,0,0,.24)', ); if ( 'inherit' !== $form['shadow'] ) { $rules[] = $selector . '{box-shadow:' . $shadows[ $form['shadow'] ] . ';}'; } } /** * Credential inputs exclude checkbox, submit, hidden and password-eye controls. * * @param array $rules Output rules. * @param array $fields Validated input settings. * @param string $forms Credential form selector. */ private function field_rules( array &$rules, array $fields, $forms ) { $types = ':is([type="text"],[type="password"],[type="email"])'; $selector = $forms . ' .input' . $types; $focus = $forms . ' .input:focus' . $types; $this->declarations( $rules, $selector, $fields, array( 'color' => 'color', 'background_color' => 'background-color', 'border_color' => 'border-color', 'border_width' => 'border-width', 'border_radius' => 'border-radius', 'font_size' => 'font-size', 'padding' => 'padding', ) ); if ( null !== $fields['border_width'] || '' !== $fields['border_color'] ) { $rules[] = $selector . '{border-style:solid;}'; } $dimensions = null !== $fields['padding'] || null !== $fields['height'] || null !== $fields['font_size'] || null !== $fields['border_width']; if ( $dimensions ) { // Height is a minimum; line height, padding and zoom can grow the control. $height = max( 40, $fields['height'] ?? 40 ); $rules[] = $selector . '{box-sizing:border-box;width:100%;height:auto;min-height:' . $height . 'px;line-height:1.33333333;}'; // Grid contains the input margin so the native eye tracks the actual input box. $rules[] = $forms . ' .wp-pwd{display:grid;grid-template-columns:minmax(0,1fr);}'; $rules[] = $forms . ' .wp-pwd .password-input{padding-right:40px;}'; $rules[] = $forms . ' .wp-pwd .wp-hide-pw{top:0;height:calc(100% - 16px);}'; } if ( '' !== $fields['placeholder_color'] ) { $rules[] = $selector . '::placeholder{color:' . $fields['placeholder_color'] . ';}'; } $this->declarations( $rules, $focus, $fields, array( 'focus_border_color' => 'border-color', 'focus_background_color' => 'background-color', ) ); if ( '' !== $fields['border_color'] && '' === $fields['focus_border_color'] ) { $rules[] = $focus . '{border-color:var(--wp-admin-theme-color,#2271b1);}'; } $shadows = array( 'subtle' => '0 0 0 1px var(--wp-admin-theme-color,#2271b1)', 'medium' => '0 0 0 2px var(--wp-admin-theme-color,#2271b1)', 'strong' => '0 0 0 3px var(--wp-admin-theme-color,#2271b1)', ); if ( 'inherit' !== $fields['focus_shadow'] ) { $rules[] = $focus . '{box-shadow:' . $shadows[ $fields['focus_shadow'] ] . ';}'; } $custom = array_filter( $fields, static function ( $value ) { return null !== $value && '' !== $value && 'inherit' !== $value; } ); if ( $custom ) { // An independent visible ring survives zero-width borders and custom colors. $rules[] = $focus . '{outline:2px solid var(--wp-admin-theme-color,#2271b1);outline-offset:2px;}'; $rules[] = '@media (forced-colors:active){' . $focus . '{outline-color:Highlight;}}'; } } }