| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Internals\Options |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Option: wpseo_social. |
| 10 |
*/ |
| 11 |
class WPSEO_Option_Social extends WPSEO_Option { |
| 12 |
|
| 13 |
/** |
| 14 |
* Option name. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public $option_name = 'wpseo_social'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Array of defaults for the option. |
| 22 |
* |
| 23 |
* Shouldn't be requested directly, use $this->get_defaults(); |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $defaults = [ |
| 28 |
// Form fields. |
| 29 |
'facebook_site' => '', // Text field. |
| 30 |
'instagram_url' => '', |
| 31 |
'linkedin_url' => '', |
| 32 |
'myspace_url' => '', |
| 33 |
'og_default_image' => '', // Text field. |
| 34 |
'og_default_image_id' => '', |
| 35 |
'og_frontpage_title' => '', // Text field. |
| 36 |
'og_frontpage_desc' => '', // Text field. |
| 37 |
'og_frontpage_image' => '', // Text field. |
| 38 |
'og_frontpage_image_id' => '', |
| 39 |
'opengraph' => true, |
| 40 |
'pinterest_url' => '', |
| 41 |
'pinterestverify' => '', |
| 42 |
'twitter' => true, |
| 43 |
'twitter_site' => '', // Text field. |
| 44 |
'twitter_card_type' => 'summary_large_image', |
| 45 |
'youtube_url' => '', |
| 46 |
'wikipedia_url' => '', |
| 47 |
]; |
| 48 |
|
| 49 |
/** |
| 50 |
* Array of sub-options which should not be overloaded with multi-site defaults. |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
public $ms_exclude = [ |
| 55 |
/* Privacy. */ |
| 56 |
'pinterestverify', |
| 57 |
]; |
| 58 |
|
| 59 |
/** |
| 60 |
* Array of allowed twitter card types. |
| 61 |
* |
| 62 |
* While we only have the options summary and summary_large_image in the |
| 63 |
* interface now, we might change that at some point. |
| 64 |
* |
| 65 |
* {@internal Uncomment any of these to allow them in validation *and* automatically |
| 66 |
* add them as a choice in the options page.}} |
| 67 |
* |
| 68 |
* @var array |
| 69 |
*/ |
| 70 |
public static $twitter_card_types = [ |
| 71 |
'summary' => '', |
| 72 |
'summary_large_image' => '', |
| 73 |
// 'photo' => '', |
| 74 |
// 'gallery' => '', |
| 75 |
// 'app' => '', |
| 76 |
// 'player' => '', |
| 77 |
// 'product' => '', |
| 78 |
]; |
| 79 |
|
| 80 |
/** |
| 81 |
* Add the actions and filters for the option. |
| 82 |
*/ |
| 83 |
protected function __construct() { |
| 84 |
parent::__construct(); |
| 85 |
|
| 86 |
add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the singleton instance of this class. |
| 91 |
* |
| 92 |
* @return object |
| 93 |
*/ |
| 94 |
public static function get_instance() { |
| 95 |
if ( ! ( self::$instance instanceof self ) ) { |
| 96 |
self::$instance = new self(); |
| 97 |
} |
| 98 |
|
| 99 |
return self::$instance; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Translate/set strings used in the option defaults. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
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 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Validate the option. |
| 114 |
* |
| 115 |
* @param array $dirty New value for the option. |
| 116 |
* @param array $clean Clean value for the option, normally the defaults. |
| 117 |
* @param array $old Old value of the option. |
| 118 |
* |
| 119 |
* @return array Validated clean value for the option to be saved to the database. |
| 120 |
*/ |
| 121 |
protected function validate_option( $dirty, $clean, $old ) { |
| 122 |
|
| 123 |
foreach ( $clean as $key => $value ) { |
| 124 |
switch ( $key ) { |
| 125 |
/* Text fields. */ |
| 126 |
case 'og_frontpage_desc': |
| 127 |
case 'og_frontpage_title': |
| 128 |
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { |
| 129 |
$clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] ); |
| 130 |
} |
| 131 |
break; |
| 132 |
|
| 133 |
case 'og_default_image_id': |
| 134 |
case 'og_frontpage_image_id': |
| 135 |
if ( isset( $dirty[ $key ] ) ) { |
| 136 |
$clean[ $key ] = (int) $dirty[ $key ]; |
| 137 |
|
| 138 |
if ( $dirty[ $key ] === '' ) { |
| 139 |
$clean[ $key ] = $dirty[ $key ]; |
| 140 |
} |
| 141 |
} |
| 142 |
break; |
| 143 |
|
| 144 |
/* URL text fields - no ftp allowed. */ |
| 145 |
case 'facebook_site': |
| 146 |
case 'instagram_url': |
| 147 |
case 'linkedin_url': |
| 148 |
case 'myspace_url': |
| 149 |
case 'pinterest_url': |
| 150 |
case 'og_default_image': |
| 151 |
case 'og_frontpage_image': |
| 152 |
case 'youtube_url': |
| 153 |
case 'wikipedia_url': |
| 154 |
$this->validate_url( $key, $dirty, $old, $clean ); |
| 155 |
break; |
| 156 |
|
| 157 |
case 'pinterestverify': |
| 158 |
$this->validate_verification_string( $key, $dirty, $old, $clean ); |
| 159 |
break; |
| 160 |
|
| 161 |
/* Twitter user name. */ |
| 162 |
case 'twitter_site': |
| 163 |
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { |
| 164 |
$twitter_id = sanitize_text_field( ltrim( $dirty[ $key ], '@' ) ); |
| 165 |
|
| 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 ) ) { |
| 176 |
$clean[ $key ] = $twitter_id; |
| 177 |
} |
| 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 ] !== '' ) { |
| 183 |
$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 |
} |
| 187 |
} |
| 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 |
} |
| 201 |
unset( $twitter_id ); |
| 202 |
|
| 203 |
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] ); |
| 204 |
} |
| 205 |
break; |
| 206 |
|
| 207 |
case 'twitter_card_type': |
| 208 |
if ( isset( $dirty[ $key ], self::$twitter_card_types[ $dirty[ $key ] ] ) && $dirty[ $key ] !== '' ) { |
| 209 |
$clean[ $key ] = $dirty[ $key ]; |
| 210 |
} |
| 211 |
break; |
| 212 |
|
| 213 |
/* Boolean fields. */ |
| 214 |
case 'opengraph': |
| 215 |
case 'twitter': |
| 216 |
$clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false ); |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return $clean; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Clean a given option value. |
| 226 |
* |
| 227 |
* @param array $option_value Old (not merged with defaults or filtered) option value to |
| 228 |
* clean according to the rules for this option. |
| 229 |
* @param string|null $current_version Optional. Version from which to upgrade, if not set, |
| 230 |
* version specific upgrades will be disregarded. |
| 231 |
* @param array|null $all_old_option_values Optional. Only used when importing old options to have |
| 232 |
* access to the real old values, in contrast to the saved ones. |
| 233 |
* |
| 234 |
* @return array Cleaned option. |
| 235 |
*/ |
| 236 |
protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) { |
| 237 |
|
| 238 |
/* Move options from very old option to this one. */ |
| 239 |
$old_option = null; |
| 240 |
if ( isset( $all_old_option_values ) ) { |
| 241 |
// Ok, we have an import. |
| 242 |
if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== [] ) { |
| 243 |
$old_option = $all_old_option_values['wpseo_indexation']; |
| 244 |
} |
| 245 |
} |
| 246 |
else { |
| 247 |
$old_option = get_option( 'wpseo_indexation' ); |
| 248 |
} |
| 249 |
|
| 250 |
if ( is_array( $old_option ) && $old_option !== [] ) { |
| 251 |
$move = [ |
| 252 |
'opengraph', |
| 253 |
]; |
| 254 |
foreach ( $move as $key ) { |
| 255 |
if ( isset( $old_option[ $key ] ) && ! isset( $option_value[ $key ] ) ) { |
| 256 |
$option_value[ $key ] = $old_option[ $key ]; |
| 257 |
} |
| 258 |
} |
| 259 |
unset( $move, $key ); |
| 260 |
} |
| 261 |
unset( $old_option ); |
| 262 |
|
| 263 |
|
| 264 |
return $option_value; |
| 265 |
} |
| 266 |
} |
| 267 |
|