*/ public const COLOURS = array( 'color_background' => '#f4f6f9', 'color_box' => '#ffffff', 'color_text' => '#1d2733', 'color_accent' => '#2271b1', ); /** * Defaults. * * @return array */ public static function defaults(): array { return array( 'enabled' => false, 'mode' => self::MODE_MAINTENANCE, 'title' => '', 'message' => '', 'allowed_ips' => array(), 'page_id' => 0, 'logo_id' => 0, 'image_id' => 0, 'image_style' => self::IMAGE_NONE, 'legal_public' => true, 'allowed_pages' => array(), ) + self::COLOURS; } /** * Cleans values before they are saved. * * @param array $values Values. * @return array */ public static function sanitize( array $values ): array { $values = array_merge( self::defaults(), $values ); $ips = is_array( $values['allowed_ips'] ) ? $values['allowed_ips'] : preg_split( '/\R/', (string) $values['allowed_ips'] ); $clean = array(); foreach ( (array) $ips as $ip ) { $ip = trim( (string) $ip ); if ( false !== filter_var( $ip, FILTER_VALIDATE_IP ) ) { $clean[] = $ip; } } $pages = array(); foreach ( (array) $values['allowed_pages'] as $page ) { $page = self::id( $page ); if ( $page > 0 ) { $pages[] = $page; } } $style = in_array( $values['image_style'], array( self::IMAGE_BACKGROUND, self::IMAGE_SIDE ), true ) ? $values['image_style'] : self::IMAGE_NONE; $result = array( 'enabled' => (bool) $values['enabled'], 'mode' => self::MODE_COMING_SOON === $values['mode'] ? self::MODE_COMING_SOON : self::MODE_MAINTENANCE, 'title' => substr( sanitize_text_field( (string) $values['title'] ), 0, 120 ), 'message' => substr( sanitize_textarea_field( (string) $values['message'] ), 0, 1000 ), 'allowed_ips' => array_slice( array_values( array_unique( $clean ) ), 0, 20 ), 'page_id' => self::id( $values['page_id'] ), 'logo_id' => self::id( $values['logo_id'] ), 'image_id' => self::id( $values['image_id'] ), 'image_style' => $style, 'legal_public' => (bool) $values['legal_public'], 'allowed_pages' => array_slice( array_values( array_unique( $pages ) ), 0, 50 ), ); foreach ( self::COLOURS as $key => $fallback ) { $colour = strtolower( trim( (string) $values[ $key ] ) ); $result[ $key ] = 1 === preg_match( '/^#[0-9a-f]{6}$/', $colour ) ? $colour : $fallback; } return $result; } /** * A positive post id, or 0. * * @param mixed $value Raw value. */ private static function id( $value ): int { return is_numeric( $value ) ? max( 0, (int) $value ) : 0; } /** * Title shown to visitors, with a plain default. * * @param array $values Saved values. */ public static function title( array $values ): string { if ( '' !== (string) $values['title'] ) { return (string) $values['title']; } return self::MODE_COMING_SOON === $values['mode'] ? __( 'Coming soon', 'forensix-digital' ) : __( 'We will be right back', 'forensix-digital' ); } /** * Message shown to visitors, with a plain default. * * @param array $values Saved values. */ public static function message( array $values ): string { if ( '' !== (string) $values['message'] ) { return (string) $values['message']; } return self::MODE_COMING_SOON === $values['mode'] ? __( 'This website is being built. Please come back soon.', 'forensix-digital' ) : __( 'The website is being updated. Please try again in a few minutes.', 'forensix-digital' ); } }