0, 'allowed_ips' => array(), 'title' => '', 'message' => '', 'accent' => '#2563eb', 'background' => '#f1f5f9', 'logo_id' => 0, ); } public static function all() { $saved = get_option( self::OPTION, array() ); return array_merge( self::defaults(), is_array( $saved ) ? $saved : array() ); } public static function enabled() { return ! ( defined( 'ESCUDER_MAINTENANCE_BYPASS' ) && true === ESCUDER_MAINTENANCE_BYPASS ) && ! empty( self::all()['enabled'] ); } public static function sanitize( $input ) { if ( ! is_array( $input ) ) { return new \WP_Error( 'invalid_settings', __( 'Invalid settings. No changes were saved.', 'escuder-maintenance' ) ); } $ips = Ip_Address::parse( isset( $input['allowed_ips'] ) ? $input['allowed_ips'] : '' ); if ( is_wp_error( $ips ) ) { return $ips; } foreach ( array( 'title', 'message', 'accent', 'background', 'logo_id', 'enabled' ) as $key ) { if ( isset( $input[ $key ] ) && ! is_scalar( $input[ $key ] ) ) { return new \WP_Error( 'invalid_settings', __( 'Invalid settings. No changes were saved.', 'escuder-maintenance' ) ); } } $logo_id = isset( $input['logo_id'] ) ? absint( $input['logo_id'] ) : 0; if ( $logo_id && ! wp_attachment_is_image( $logo_id ) ) { return new \WP_Error( 'invalid_logo', __( 'Choose an image from the Media Library for the logo.', 'escuder-maintenance' ) ); } $defaults = self::defaults(); $result = array( 'enabled' => isset( $input['enabled'] ) && '1' === (string) $input['enabled'] ? 1 : 0, 'allowed_ips' => $ips, 'title' => isset( $input['title'] ) ? sanitize_text_field( $input['title'] ) : '', 'message' => isset( $input['message'] ) ? sanitize_textarea_field( $input['message'] ) : '', 'logo_id' => $logo_id, ); if ( strlen( $result['title'] ) > 600 || strlen( $result['message'] ) > 12000 ) { return new \WP_Error( 'invalid_length', __( 'The title or message is too long. No changes were saved.', 'escuder-maintenance' ) ); } foreach ( array( 'accent', 'background' ) as $key ) { $color = isset( $input[ $key ] ) ? sanitize_hex_color( $input[ $key ] ) : $defaults[ $key ]; if ( ! $color ) { return new \WP_Error( 'invalid_color', __( 'Choose a valid hexadecimal color.', 'escuder-maintenance' ) ); } $result[ $key ] = $color; } return $result; } public static function title( $options ) { return '' !== $options['title'] ? $options['title'] : __( 'Site under construction', 'escuder-maintenance' ); } public static function message( $options ) { return '' !== $options['message'] ? $options['message'] : __( 'We are preparing something new. Please come back soon.', 'escuder-maintenance' ); } }