CSS. */ private const STYLES = array( 'cover' => 'center / cover no-repeat fixed', 'contain' => 'center / contain no-repeat fixed', 'tile' => 'top left / auto repeat', 'center' => 'center / auto no-repeat fixed', ); /** * Hook everything. * * @return void */ public function register(): void { add_action( 'login_enqueue_scripts', array( $this, 'apply' ) ); } /** * Layout choices for the settings field. * * @return array */ public static function style_choices(): array { return array( 'cover' => __( 'Fill the screen', 'all-in-one-utilities' ), 'contain' => __( 'Fit the whole image', 'all-in-one-utilities' ), 'tile' => __( 'Tile (repeat)', 'all-in-one-utilities' ), 'center' => __( 'Center, original size', 'all-in-one-utilities' ), ); } /** * The saved background. * * @return array{color:string,image:string,style:string} Hex color, image URL ('' for none), and layout. */ public static function values(): array { $image_id = absint( aiou_get_setting( 'login', self::SECTION, 'image', 0 ) ); $style = (string) aiou_get_setting( 'login', self::SECTION, 'style', 'cover' ); return array( 'color' => (string) sanitize_hex_color( (string) aiou_get_setting( 'login', self::SECTION, 'color', '' ) ), 'image' => $image_id ? (string) wp_get_attachment_image_url( $image_id, 'full' ) : '', 'style' => isset( self::STYLES[ $style ] ) ? $style : 'cover', ); } /** * Add the background to core's own `login` stylesheet. * * @return void */ public function apply(): void { $values = self::values(); if ( '' === $values['color'] && '' === $values['image'] ) { return; } /** * Filters whether the Login Page background stylesheet is added. * Pro's design templates return false and apply the same color and * image themselves. * * @since 2.0.0 * * @param bool $apply Whether to add it. */ if ( ! apply_filters( 'aiou_login_background_css', true ) ) { return; } $rules = array(); if ( '' !== $values['color'] ) { $rules[] = 'background-color:' . $values['color']; } if ( '' !== $values['image'] ) { $rules[] = 'background:' . ( '' !== $values['color'] ? $values['color'] . ' ' : '' ) . 'url("' . esc_url_raw( $values['image'] ) . '") ' . self::STYLES[ $values['style'] ]; } $css = 'body.login{' . implode( ';', $rules ) . ';}'; // The links under the form (#nav, #backtoblog, the privacy link) are // dark gray in core, and vanish on a dark color or a photo: make them // light there. A photo's brightness isn't known, so a soft shadow // keeps them readable on light parts of it too. if ( '' !== $values['image'] ) { $css .= self::LINKS_SELECTOR . '{color:#fff;text-shadow:0 1px 3px rgba(0,0,0,.65);}' . self::LINKS_HOVER . '{color:#fff;text-decoration:underline;}'; } elseif ( self::is_dark( $values['color'] ) ) { $css .= self::LINKS_SELECTOR . '{color:rgba(255,255,255,.85);}' . self::LINKS_HOVER . '{color:#fff;}'; } wp_add_inline_style( 'login', $css ); } /** * The links under the login form. * * @var string */ private const LINKS_SELECTOR = '.login #nav a,.login #backtoblog a,.login .privacy-policy-page-link a,.login #nav'; /** * Their hover/focus state. * * @var string */ private const LINKS_HOVER = '.login #nav a:hover,.login #nav a:focus,.login #backtoblog a:hover,.login #backtoblog a:focus,.login .privacy-policy-page-link a:hover,.login .privacy-policy-page-link a:focus'; /** * Whether a hex color is dark enough that dark text on it is hard to read * (relative luminance below 0.4). * * @param string $hex Color like #0b3b2e or #abc. * @return bool */ private static function is_dark( string $hex ): bool { $hex = ltrim( $hex, '#' ); if ( 3 === strlen( $hex ) ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } if ( ! preg_match( '/^[0-9a-f]{6}$/i', $hex ) ) { return false; } $channel = static function ( string $pair ): float { $c = hexdec( $pair ) / 255; return $c <= 0.03928 ? $c / 12.92 : ( ( $c + 0.055 ) / 1.055 ) ** 2.4; }; $luminance = 0.2126 * $channel( substr( $hex, 0, 2 ) ) + 0.7152 * $channel( substr( $hex, 2, 2 ) ) + 0.0722 * $channel( substr( $hex, 4, 2 ) ); return $luminance < 0.4; } }