|
15 | 15 | * @since 3.4.0 |
16 | 16 | * |
17 | 17 | * @see WP_Customize_Setting |
| 18 | + * |
| 19 | + * @phpstan-type Header_Image_Data array{ |
| 20 | + * attachment_id?: int, |
| 21 | + * url?: string, |
| 22 | + * thumbnail_url?: string, |
| 23 | + * timestamp?: int, |
| 24 | + * width?: int, |
| 25 | + * height?: int, |
| 26 | + * alt_text?: string, |
| 27 | + * attachment_parent?: int, |
| 28 | + * } |
18 | 29 | */ |
19 | 30 | final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting { |
20 | 31 |
|
@@ -57,4 +68,163 @@ public function update( $value ) { |
57 | 68 | $custom_image_header->set_header_image( $value ); |
58 | 69 | } |
59 | 70 | } |
| 71 | + |
| 72 | + /** |
| 73 | + * Sanitizes a header value. |
| 74 | + * |
| 75 | + * The value is expected to be one of the following: |
| 76 | + * |
| 77 | + * - An array of header image data, with the keys `attachment_id`, `url`, `thumbnail_url`, `timestamp`, `width`, |
| 78 | + * `height`, `alt_text`, and `attachment_parent`, as supplied by {@see get_uploaded_header_images()}. Any other |
| 79 | + * key is discarded. |
| 80 | + * - An array with a `choice` key, being the legacy format in which any of the other accepted values is nested. |
| 81 | + * - The string `remove-header`, `random-default-image`, or `random-uploaded-image`. |
| 82 | + * - A string corresponding to one of the keys for the array returned by {@see get_uploaded_header_images()}, or |
| 83 | + * one of the keys for the array passed into {@see register_default_headers()}. |
| 84 | + * |
| 85 | + * @since 7.1.1 |
| 86 | + * |
| 87 | + * @see WP_Customize_Header_Image_Setting::update() |
| 88 | + * @see Custom_Image_Header::set_header_image() |
| 89 | + * |
| 90 | + * @param mixed $value Value to sanitize. |
| 91 | + * @return array|string|WP_Error|null Sanitized value, or `null`/`WP_Error` if invalid. The array holds |
| 92 | + * the header image data, or that data nested under a `choice` key, |
| 93 | + * before the `customize_sanitize_header_image_data` filter, which |
| 94 | + * may return anything, is applied to it. |
| 95 | + * |
| 96 | + * @phpstan-return array<mixed, mixed>|string|WP_Error|null |
| 97 | + */ |
| 98 | + public function sanitize( $value ) { |
| 99 | + /* |
| 100 | + * The update() method unwraps the legacy `choice` format before handing the value off to |
| 101 | + * Custom_Image_Header::set_header_image(), so the nested value is what must be sanitized. |
| 102 | + */ |
| 103 | + if ( is_array( $value ) && isset( $value['choice'] ) ) { |
| 104 | + $choice = $this->sanitize_choice( $value['choice'] ); |
| 105 | + if ( is_null( $choice ) || is_wp_error( $choice ) ) { |
| 106 | + return $choice; |
| 107 | + } |
| 108 | + $value = array( 'choice' => $choice ); |
| 109 | + } else { |
| 110 | + $value = $this->sanitize_choice( $value ); |
| 111 | + if ( is_null( $value ) || is_wp_error( $value ) ) { |
| 112 | + return $value; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return parent::sanitize( $value ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sanitizes a header image choice. |
| 121 | + * |
| 122 | + * This is the value which is ultimately passed to {@see Custom_Image_Header::set_header_image()}, whether |
| 123 | + * supplied at the top level of the setting value or nested under its legacy `choice` key. |
| 124 | + * |
| 125 | + * @since 7.1.1 |
| 126 | + * |
| 127 | + * @param mixed $value Value to sanitize. |
| 128 | + * @return array|string|WP_Error|null Sanitized value, or `null`/`WP_Error` if invalid. |
| 129 | + * |
| 130 | + * @phpstan-return Header_Image_Data|string|WP_Error|null |
| 131 | + */ |
| 132 | + private function sanitize_choice( $value ) { |
| 133 | + // Custom_Image_Header::set_header_image() accepts an object in place of an array. |
| 134 | + if ( is_object( $value ) ) { |
| 135 | + $value = (array) $value; |
| 136 | + } |
| 137 | + |
| 138 | + if ( is_string( $value ) ) { |
| 139 | + return sanitize_text_field( $value ); |
| 140 | + } |
| 141 | + |
| 142 | + if ( ! is_array( $value ) ) { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + /* |
| 147 | + * The sanitized value is assembled member by member rather than filtered down from the |
| 148 | + * supplied one, so that nothing but the members below can end up in it. |
| 149 | + */ |
| 150 | + $sanitized = array(); |
| 151 | + |
| 152 | + if ( isset( $value['attachment_id'] ) ) { |
| 153 | + if ( ! is_scalar( $value['attachment_id'] ) ) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + $attachment_id = absint( $value['attachment_id'] ); |
| 157 | + |
| 158 | + /* |
| 159 | + * A supplied attachment must be an existing image, since its ID is written to postmeta and its |
| 160 | + * data displayed. Note that an ID of zero must be skipped rather than looked up, as |
| 161 | + * get_post_mime_type() falls back to the global post when passed an empty value. |
| 162 | + */ |
| 163 | + if ( $attachment_id > 0 ) { |
| 164 | + $mime_type = get_post_mime_type( $attachment_id ); |
| 165 | + if ( ! is_string( $mime_type ) || ! str_starts_with( $mime_type, 'image/' ) ) { |
| 166 | + return null; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + $sanitized['attachment_id'] = $attachment_id; |
| 171 | + } |
| 172 | + |
| 173 | + if ( isset( $value['url'] ) ) { |
| 174 | + if ( ! is_string( $value['url'] ) ) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + $sanitized['url'] = sanitize_url( $value['url'] ); |
| 178 | + if ( '' === $sanitized['url'] ) { |
| 179 | + return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) ); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if ( isset( $value['thumbnail_url'] ) ) { |
| 184 | + if ( ! is_string( $value['thumbnail_url'] ) ) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + $sanitized['thumbnail_url'] = sanitize_url( $value['thumbnail_url'] ); |
| 188 | + if ( '' === $sanitized['thumbnail_url'] ) { |
| 189 | + return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) ); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + if ( isset( $value['timestamp'] ) ) { |
| 194 | + if ( ! is_scalar( $value['timestamp'] ) ) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + $sanitized['timestamp'] = absint( $value['timestamp'] ); |
| 198 | + } |
| 199 | + |
| 200 | + if ( isset( $value['width'] ) ) { |
| 201 | + if ( ! is_scalar( $value['width'] ) ) { |
| 202 | + return null; |
| 203 | + } |
| 204 | + $sanitized['width'] = absint( $value['width'] ); |
| 205 | + } |
| 206 | + |
| 207 | + if ( isset( $value['height'] ) ) { |
| 208 | + if ( ! is_scalar( $value['height'] ) ) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + $sanitized['height'] = absint( $value['height'] ); |
| 212 | + } |
| 213 | + |
| 214 | + if ( isset( $value['alt_text'] ) ) { |
| 215 | + if ( ! is_string( $value['alt_text'] ) ) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + $sanitized['alt_text'] = sanitize_text_field( $value['alt_text'] ); |
| 219 | + } |
| 220 | + |
| 221 | + if ( isset( $value['attachment_parent'] ) ) { |
| 222 | + if ( ! is_scalar( $value['attachment_parent'] ) ) { |
| 223 | + return null; |
| 224 | + } |
| 225 | + $sanitized['attachment_parent'] = absint( $value['attachment_parent'] ); |
| 226 | + } |
| 227 | + |
| 228 | + return $sanitized; |
| 229 | + } |
60 | 230 | } |
0 commit comments