* * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_iso_date( bool $required = true ): array { return [ 'required' => $required, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => static function ( mixed $value ): bool { if ( ! is_string( $value ) || $value === '' ) { return false; } $parsed = strtotime( $value ); return $parsed !== false; }, ]; } /** * Access level enum — `P` (public), `C` (confidential), `R` (private). * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_access_level(): array { $allowed = [ 'P', 'C', 'R' ]; return [ 'required' => false, 'type' => 'string', 'default' => 'P', 'enum' => $allowed, 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => static fn( mixed $value ): bool => is_string( $value ) && in_array( $value, $allowed, true ), ]; } /** * Positive integer (> 0). * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_positive_int( bool $required = true ): array { return [ 'required' => $required, 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => static function ( mixed $value ) use ( $required ): bool { // When optional, accept absent-equivalent values (null, 0, '0', '') // as "not provided" rather than rejecting the entire request. UIs // commonly include the field with an empty value when the user // hasn't set one (e.g. category_id when no category is chosen). if ( ! $required && ( $value === null || $value === 0 || $value === '0' || $value === '' ) ) { return true; } if ( is_int( $value ) ) { return $value > 0; } if ( ! is_string( $value ) || $value === '' ) { return false; } return ctype_digit( ltrim( $value, '-' ) ) && ! str_starts_with( $value, '-' ) && (int) $value > 0; }, ]; } /** * iCal RRULE string (RFC 5545). Empty string means "no recurrence". * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_rrule(): array { return [ 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => static function ( mixed $value ): bool { if ( ! is_string( $value ) ) { return false; } if ( $value === '' ) { return true; } // RFC 5545 requires FREQ as the first and only mandatory rule part, // and keywords are case-sensitive. return str_starts_with( $value, 'FREQ=' ); }, ]; } /** * WordPress user login — 1–60 chars after `sanitize_user(..., true)`. * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_login(): array { return [ 'required' => false, 'type' => 'string', 'sanitize_callback' => static fn( mixed $value ): string => sanitize_user( is_string( $value ) ? $value : '', true ), 'validate_callback' => static function ( mixed $value ): bool { if ( ! is_string( $value ) ) { return false; } // Empty string is a valid "global" owner sentinel (categories, // nonuser calendars, etc. distinguish "no owner" from a // specific user login). Treat it as "not provided" rather // than rejecting the request. $len = strlen( $value ); return $len === 0 || $len <= 60; }, ]; } /** * Hex color — `#rgb` or `#rrggbb`. Empty string accepted as "unset". * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_hex_color(): array { return [ 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => static function ( mixed $value ): string { $sanitized = sanitize_hex_color( is_string( $value ) ? $value : '' ); return is_string( $sanitized ) ? $sanitized : ''; }, 'validate_callback' => static function ( mixed $value ): bool { if ( ! is_string( $value ) ) { return false; } if ( $value === '' ) { return true; } return preg_match( '/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/', $value ) === 1; }, ]; } /** * Rich-text body — allowed HTML is filtered by `wp_kses_post()`. * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_rich_text(): array { return [ 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'wp_kses_post', ]; } /** * Generic bounded string — `sanitize_text_field` with a length cap. * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_string( int $max_length, bool $required = true ): array { return [ 'required' => $required, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => static function ( mixed $value ) use ( $max_length ): bool { return is_string( $value ) && strlen( $value ) <= $max_length; }, ]; } /** * Boolean — uses WP's built-in `rest_sanitize_boolean` so that string * forms (`"true"`, `"false"`, `"1"`, `"0"`, `"yes"`, `"on"`) coerce * correctly. * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_bool( bool $default = false ): array { return [ 'required' => false, 'type' => 'boolean', 'default' => $default, 'sanitize_callback' => 'rest_sanitize_boolean', ]; } /** * URL — sanitized with `esc_url_raw` and validated with * `FILTER_VALIDATE_URL`. * * @return array * * @psalm-suppress PossiblyUnusedMethod -- See trait-level note. */ protected function rest_arg_url( bool $required = true ): array { return [ 'required' => $required, 'type' => 'string', 'sanitize_callback' => 'esc_url_raw', 'validate_callback' => static function ( mixed $value ): bool { if ( ! is_string( $value ) || $value === '' ) { return false; } return filter_var( $value, FILTER_VALIDATE_URL ) !== false; }, ]; } }