| @@ -43,8 +43,10 @@ | ||
| 43 | 43 | 'twitter_site' => '', // Text field. |
| 44 | 44 | 'twitter_card_type' => 'summary_large_image', |
| 45 | 45 | 'youtube_url' => '', |
| 46 | 46 | 'wikipedia_url' => '', |
| 47 | + 'other_social_urls' => [], | |
| 48 | + 'mastodon_url' => '', | |
| 47 | 49 | ]; |
| 48 | 50 | |
| 49 | 51 | /** |
| 50 | 52 | * Array of sub-options which should not be overloaded with multi-site defaults. |
| @@ -67,10 +69,10 @@ | ||
| 67 | 69 | * |
| 68 | 70 | * @var array |
| 69 | 71 | */ |
| 70 | 72 | public static $twitter_card_types = [ |
| 71 | - 'summary' => '', | |
| 72 | 73 | 'summary_large_image' => '', |
| 74 | + // 'summary' => '', | |
| 73 | 75 | // 'photo' => '', |
| 74 | 76 | // 'gallery' => '', |
| 75 | 77 | // 'app' => '', |
| 76 | 78 | // 'player' => '', |
| @@ -104,10 +106,9 @@ | ||
| 104 | 106 | * |
| 105 | 107 | * @return void |
| 106 | 108 | */ |
| 107 | 109 | public function translate_defaults() { |
| 108 | - self::$twitter_card_types['summary'] = __( 'Summary', 'wordpress-seo' ); | |
| 109 | - self::$twitter_card_types['summary_large_image'] = __( 'Summary with large image', 'wordpress-seo' ); | |
| 110 | + self::$twitter_card_types['summary_large_image'] = 'Summary with large image'; | |
| 110 | 111 | } |
| 111 | 112 | |
| 112 | 113 | /** |
| 113 | 114 | * Validate the option. |
| @@ -150,8 +151,9 @@ | ||
| 150 | 151 | case 'og_default_image': |
| 151 | 152 | case 'og_frontpage_image': |
| 152 | 153 | case 'youtube_url': |
| 153 | 154 | case 'wikipedia_url': |
| 155 | + case 'mastodon_url': | |
| 154 | 156 | $this->validate_url( $key, $dirty, $old, $clean ); |
| 155 | 157 | break; |
| 156 | 158 | |
| 157 | 159 | case 'pinterestverify': |
| @@ -160,44 +162,18 @@ | ||
| 160 | 162 | |
| 161 | 163 | /* Twitter user name. */ |
| 162 | 164 | case 'twitter_site': |
| 163 | 165 | if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { |
| 164 | - $twitter_id = sanitize_text_field( ltrim( $dirty[ $key ], '@' ) ); | |
| 166 | + $twitter_id = $this->validate_twitter_id( $dirty[ $key ] ); | |
| 165 | 167 | |
| 166 | - /* | |
| 167 | - * From the Twitter documentation about twitter screen names: | |
| 168 | - * Typically a maximum of 15 characters long, but some historical accounts | |
| 169 | - * may exist with longer names. | |
| 170 | - * A username can only contain alphanumeric characters (letters A-Z, numbers 0-9) | |
| 171 | - * with the exception of underscores. | |
| 172 | - * | |
| 173 | - * @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames | |
| 174 | - */ | |
| 175 | - if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) { | |
| 168 | + if ( $twitter_id ) { | |
| 176 | 169 | $clean[ $key ] = $twitter_id; |
| 177 | 170 | } |
| 178 | - elseif ( preg_match( '`^http(?:s)?://(?:www\.)?twitter\.com/(?P<handle>[A-Za-z0-9_]{1,25})/?$`', $twitter_id, $matches ) ) { | |
| 179 | - $clean[ $key ] = $matches['handle']; | |
| 180 | - } | |
| 181 | - else { | |
| 182 | - if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) { | |
| 171 | + elseif ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) { | |
| 183 | 172 | $twitter_id = sanitize_text_field( ltrim( $old[ $key ], '@' ) ); |
| 184 | - if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) { | |
| 185 | - $clean[ $key ] = $twitter_id; | |
| 186 | - } | |
| 173 | + if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) { | |
| 174 | + $clean[ $key ] = $twitter_id; | |
| 187 | 175 | } |
| 188 | - if ( function_exists( 'add_settings_error' ) ) { | |
| 189 | - add_settings_error( | |
| 190 | - $this->group_name, // Slug title of the setting. | |
| 191 | - $key, // Suffix-ID for the error message box. | |
| 192 | - sprintf( | |
| 193 | - /* translators: %s expands to a twitter user name. */ | |
| 194 | - __( '%s does not seem to be a valid Twitter Username. Please correct.', 'wordpress-seo' ), | |
| 195 | - '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' | |
| 196 | - ), // The error message. | |
| 197 | - 'error' // Message type. | |
| 198 | - ); | |
| 199 | - } | |
| 200 | 176 | } |
| 201 | 177 | unset( $twitter_id ); |
| 202 | 178 | |
| 203 | 179 | Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] ); |
| @@ -214,8 +190,45 @@ | ||
| 214 | 190 | case 'opengraph': |
| 215 | 191 | case 'twitter': |
| 216 | 192 | $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false ); |
| 217 | 193 | break; |
| 194 | + | |
| 195 | + /* Array fields. */ | |
| 196 | + case 'other_social_urls': | |
| 197 | + if ( isset( $dirty[ $key ] ) ) { | |
| 198 | + $items = $dirty[ $key ]; | |
| 199 | + if ( ! is_array( $items ) ) { | |
| 200 | + $items = json_decode( $dirty[ $key ], true ); | |
| 201 | + } | |
| 202 | + | |
| 203 | + if ( is_array( $items ) ) { | |
| 204 | + foreach ( $items as $item_key => $item ) { | |
| 205 | + $validated_url = $this->validate_social_url( $item ); | |
| 206 | + | |
| 207 | + if ( $validated_url === false ) { | |
| 208 | + // Restore the previous URL values, if any. | |
| 209 | + $old_urls = ( isset( $old[ $key ] ) ) ? $old[ $key ] : []; | |
| 210 | + foreach ( $old_urls as $old_item_key => $old_url ) { | |
| 211 | + if ( $old_url !== '' ) { | |
| 212 | + $url = WPSEO_Utils::sanitize_url( $old_url ); | |
| 213 | + if ( $url !== '' ) { | |
| 214 | + $clean[ $key ][ $old_item_key ] = $url; | |
| 215 | + } | |
| 216 | + } | |
| 217 | + } | |
| 218 | + break; | |
| 219 | + } | |
| 220 | + | |
| 221 | + // The URL format is valid, let's sanitize it. | |
| 222 | + $url = WPSEO_Utils::sanitize_url( $validated_url ); | |
| 223 | + if ( $url !== '' ) { | |
| 224 | + $clean[ $key ][ $item_key ] = $url; | |
| 225 | + } | |
| 226 | + } | |
| 227 | + } | |
| 228 | + } | |
| 229 | + | |
| 230 | + break; | |
| 218 | 231 | } |
| 219 | 232 | } |
| 220 | 233 | |
| 221 | 234 | return $clean; |
| @@ -221,8 +234,52 @@ | ||
| 221 | 234 | return $clean; |
| 222 | 235 | } |
| 223 | 236 | |
| 224 | 237 | /** |
| 238 | + * Validates a social URL. | |
| 239 | + * | |
| 240 | + * @param string $url The url to be validated. | |
| 241 | + * | |
| 242 | + * @return string|false The validated URL or false if the URL is not valid. | |
| 243 | + */ | |
| 244 | + public function validate_social_url( $url ) { | |
| 245 | + $validated_url = filter_var( WPSEO_Utils::sanitize_url( trim( $url ) ), FILTER_VALIDATE_URL ); | |
| 246 | + | |
| 247 | + return $validated_url; | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * Validates a twitter id. | |
| 252 | + * | |
| 253 | + * @param string $twitter_id The twitter id to be validated. | |
| 254 | + * @param bool $strip_at_sign Whether or not to strip the `@` sign. | |
| 255 | + * | |
| 256 | + * @return string|false The validated twitter id or false if it is not valid. | |
| 257 | + */ | |
| 258 | + public function validate_twitter_id( $twitter_id, $strip_at_sign = true ) { | |
| 259 | + $twitter_id = ( $strip_at_sign ) ? sanitize_text_field( ltrim( $twitter_id, '@' ) ) : sanitize_text_field( $twitter_id ); | |
| 260 | + | |
| 261 | + /* | |
| 262 | + * From the Twitter documentation about twitter screen names: | |
| 263 | + * Typically a maximum of 15 characters long, but some historical accounts | |
| 264 | + * may exist with longer names. | |
| 265 | + * A username can only contain alphanumeric characters (letters A-Z, numbers 0-9) | |
| 266 | + * with the exception of underscores. | |
| 267 | + * | |
| 268 | + * @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames | |
| 269 | + */ | |
| 270 | + if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) { | |
| 271 | + return $twitter_id; | |
| 272 | + } | |
| 273 | + | |
| 274 | + if ( preg_match( '`^http(?:s)?://(?:www\.)?(?:twitter|x)\.com/(?P<handle>[A-Za-z0-9_]{1,25})/?$`', $twitter_id, $matches ) ) { | |
| 275 | + return $matches['handle']; | |
| 276 | + } | |
| 277 | + | |
| 278 | + return false; | |
| 279 | + } | |
| 280 | + | |
| 281 | + /** | |
| 225 | 282 | * Clean a given option value. |
| 226 | 283 | * |
| 227 | 284 | * @param array $option_value Old (not merged with defaults or filtered) option value to |
| 228 | 285 | * clean according to the rules for this option. |
| @@ -258,9 +315,8 @@ | ||
| 258 | 315 | } |
| 259 | 316 | unset( $move, $key ); |
| 260 | 317 | } |
| 261 | 318 | unset( $old_option ); |
| 262 | - | |
| 263 | 319 | |
| 264 | 320 | return $option_value; |
| 265 | 321 | } |
| 266 | 322 | } |