| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Social_Profiles_Helper. |
| 9 |
*/ |
| 10 |
class Social_Profiles_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* The fields for the person social profiles payload. |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
private $person_social_profile_fields = [ |
| 18 |
'facebook' => 'get_non_valid_url', |
| 19 |
'instagram' => 'get_non_valid_url', |
| 20 |
'linkedin' => 'get_non_valid_url', |
| 21 |
'myspace' => 'get_non_valid_url', |
| 22 |
'pinterest' => 'get_non_valid_url', |
| 23 |
'soundcloud' => 'get_non_valid_url', |
| 24 |
'tumblr' => 'get_non_valid_url', |
| 25 |
'twitter' => 'get_non_valid_twitter', |
| 26 |
'youtube' => 'get_non_valid_url', |
| 27 |
'wikipedia' => 'get_non_valid_url', |
| 28 |
]; |
| 29 |
|
| 30 |
/** |
| 31 |
* The fields for the organization social profiles payload. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
private $organization_social_profile_fields = [ |
| 36 |
'facebook_site' => 'get_non_valid_url', |
| 37 |
'twitter_site' => 'get_non_valid_twitter', |
| 38 |
'other_social_urls' => 'get_non_valid_url_array', |
| 39 |
]; |
| 40 |
|
| 41 |
/** |
| 42 |
* The Options_Helper instance. |
| 43 |
* |
| 44 |
* @var Options_Helper |
| 45 |
*/ |
| 46 |
protected $options_helper; |
| 47 |
|
| 48 |
/** |
| 49 |
* Social_Profiles_Helper constructor. |
| 50 |
* |
| 51 |
* @param Options_Helper $options_helper The WPSEO options helper. |
| 52 |
*/ |
| 53 |
public function __construct( Options_Helper $options_helper ) { |
| 54 |
$this->options_helper = $options_helper; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Gets the person social profiles stored in the database. |
| 59 |
* |
| 60 |
* @param int $person_id The id of the person. |
| 61 |
|
| 62 |
* @return array The person's social profiles. |
| 63 |
*/ |
| 64 |
public function get_person_social_profiles( $person_id ) { |
| 65 |
$social_profiles_fields = \array_keys( $this->person_social_profile_fields ); |
| 66 |
$person_social_profiles = \array_combine( $social_profiles_fields, \array_fill( 0, count( $social_profiles_fields ), '' ) ); |
| 67 |
|
| 68 |
// If no person has been selected, $person_id is set to false. |
| 69 |
if ( \is_numeric( $person_id ) ) { |
| 70 |
foreach ( \array_keys( $person_social_profiles ) as $field_name ) { |
| 71 |
$value = \get_user_meta( $person_id, $field_name, true ); |
| 72 |
// If $person_id is an integer but does not represent a valid user, get_user_meta returns false. |
| 73 |
if ( ! \is_bool( $value ) ) { |
| 74 |
$person_social_profiles[ $field_name ] = $value; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $person_social_profiles; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Stores the values for the person's social profiles. |
| 84 |
* |
| 85 |
* @param int $person_id The id of the person to edit. |
| 86 |
* @param array $social_profiles The array of the person's social profiles to be set. |
| 87 |
* |
| 88 |
* @return string[] An array of field names which failed to be saved in the db. |
| 89 |
*/ |
| 90 |
public function set_person_social_profiles( $person_id, $social_profiles ) { |
| 91 |
$failures = []; |
| 92 |
|
| 93 |
// First validate all social profiles, before even attempting to save them. |
| 94 |
foreach ( $this->person_social_profile_fields as $field_name => $validation_method ) { |
| 95 |
if ( ! isset( $social_profiles[ $field_name ] ) ) { |
| 96 |
$failures[] = $field_name; |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
if ( $social_profiles[ $field_name ] === '' ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
$value_to_validate = $social_profiles[ $field_name ]; |
| 105 |
$failures = \array_merge( $failures, \call_user_func( [ $this, $validation_method ], $value_to_validate, $field_name ) ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! empty( $failures ) ) { |
| 109 |
return $failures; |
| 110 |
} |
| 111 |
|
| 112 |
// All social profiles look good, now let's try to save them. |
| 113 |
foreach ( $this->person_social_profile_fields as $field_name => $validation_method ) { |
| 114 |
$social_profiles[ $field_name ] = $this->sanitize_social_field( $social_profiles[ $field_name ] ); |
| 115 |
\update_user_meta( $person_id, $field_name, $social_profiles[ $field_name ] ); |
| 116 |
} |
| 117 |
|
| 118 |
return $failures; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Stores the values for the organization's social profiles. |
| 123 |
* |
| 124 |
* @param array $social_profiles An array with the social profiles values to be saved in the db. |
| 125 |
* |
| 126 |
* @return string[] An array of field names which failed to be saved in the db. |
| 127 |
*/ |
| 128 |
public function set_organization_social_profiles( $social_profiles ) { |
| 129 |
$failures = []; |
| 130 |
|
| 131 |
// First validate all social profiles, before even attempting to save them. |
| 132 |
foreach ( $this->organization_social_profile_fields as $field_name => $validation_method ) { |
| 133 |
if ( ! isset( $social_profiles[ $field_name ] ) ) { |
| 134 |
$failures[] = $field_name; |
| 135 |
continue; |
| 136 |
} |
| 137 |
$social_profiles[ $field_name ] = $this->sanitize_social_field( $social_profiles[ $field_name ] ); |
| 138 |
|
| 139 |
$value_to_validate = $social_profiles[ $field_name ]; |
| 140 |
$failures = \array_merge( $failures, \call_user_func( [ $this, $validation_method ], $value_to_validate, $field_name ) ); |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! empty( $failures ) ) { |
| 144 |
return $failures; |
| 145 |
} |
| 146 |
|
| 147 |
// All social profiles look good, now let's try to save them. |
| 148 |
foreach ( \array_keys( $this->organization_social_profile_fields ) as $field_name ) { |
| 149 |
// Remove empty strings in Other Social URLs. |
| 150 |
if ( $field_name === 'other_social_urls' ) { |
| 151 |
$other_social_urls = \array_filter( |
| 152 |
$social_profiles[ $field_name ], |
| 153 |
static function( $other_social_url ) { |
| 154 |
return $other_social_url !== ''; |
| 155 |
} |
| 156 |
); |
| 157 |
|
| 158 |
$social_profiles[ $field_name ] = \array_values( $other_social_urls ); |
| 159 |
} |
| 160 |
|
| 161 |
$result = $this->options_helper->set( $field_name, $social_profiles[ $field_name ] ); |
| 162 |
if ( ! $result ) { |
| 163 |
/** |
| 164 |
* The value for Twitter might have been sanitised from URL to username. |
| 165 |
* If so, $result will be false. We should check if the option value is part of the received value. |
| 166 |
*/ |
| 167 |
if ( $field_name === 'twitter_site' ) { |
| 168 |
$current_option = $this->options_helper->get( $field_name ); |
| 169 |
if ( ! \strpos( $social_profiles[ $field_name ], 'twitter.com/' . $current_option ) ) { |
| 170 |
$failures[] = $field_name; |
| 171 |
} |
| 172 |
} |
| 173 |
else { |
| 174 |
$failures[] = $field_name; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ( ! empty( $failures ) ) { |
| 180 |
return $failures; |
| 181 |
} |
| 182 |
|
| 183 |
return []; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns a sanitized social field. |
| 188 |
* |
| 189 |
* @param string|array $social_field The social field to sanitize. |
| 190 |
* |
| 191 |
* @return string|array The sanitized social field. |
| 192 |
*/ |
| 193 |
protected function sanitize_social_field( $social_field ) { |
| 194 |
if ( \is_array( $social_field ) ) { |
| 195 |
foreach ( $social_field as $key => $value ) { |
| 196 |
$social_field[ $key ] = \sanitize_text_field( $value ); |
| 197 |
} |
| 198 |
|
| 199 |
return $social_field; |
| 200 |
} |
| 201 |
|
| 202 |
return \sanitize_text_field( $social_field ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Checks if the current user has the capability to edit a specific user. |
| 207 |
* |
| 208 |
* @param int $person_id The id of the person to edit. |
| 209 |
* |
| 210 |
* @return bool |
| 211 |
*/ |
| 212 |
public function can_edit_profile( $person_id ) { |
| 213 |
return \current_user_can( 'edit_user', $person_id ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Checks if url is not valid and returns the name of the setting if it's not. |
| 218 |
* |
| 219 |
* @param string $url The url to be validated. |
| 220 |
* @param string $url_setting The name of the setting to be updated with the url. |
| 221 |
* |
| 222 |
* @return array An array with the setting that the non-valid url is about to update. |
| 223 |
*/ |
| 224 |
protected function get_non_valid_url( $url, $url_setting ) { |
| 225 |
if ( $this->options_helper->validate_social_url( $url ) ) { |
| 226 |
return []; |
| 227 |
} |
| 228 |
|
| 229 |
return [ $url_setting ]; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Checks if urls in an array are not valid and return the name of the setting if one of them is not, including the non-valid url's index in the array |
| 234 |
* |
| 235 |
* @param array $urls The urls to be validated. |
| 236 |
* @param string $urls_setting The name of the setting to be updated with the urls. |
| 237 |
* |
| 238 |
* @return array An array with the settings that the non-valid urls are about to update, suffixed with a dash-separated index of the positions of those settings, eg. other_social_urls-2. |
| 239 |
*/ |
| 240 |
protected function get_non_valid_url_array( $urls, $urls_setting ) { |
| 241 |
$non_valid_url_array = []; |
| 242 |
|
| 243 |
foreach ( $urls as $key => $url ) { |
| 244 |
if ( ! $this->options_helper->validate_social_url( $url ) ) { |
| 245 |
$non_valid_url_array[] = $urls_setting . '-' . $key; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
return $non_valid_url_array; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Checks if the twitter value is not valid and returns the name of the setting if it's not. |
| 254 |
* |
| 255 |
* @param array $twitter_site The twitter value to be validated. |
| 256 |
* @param string $twitter_setting The name of the twitter setting to be updated with the value. |
| 257 |
* |
| 258 |
* @return array An array with the setting that the non-valid twitter value is about to update. |
| 259 |
*/ |
| 260 |
protected function get_non_valid_twitter( $twitter_site, $twitter_setting ) { |
| 261 |
if ( empty( $twitter_site ) || $this->options_helper->validate_twitter_id( $twitter_site, false ) ) { |
| 262 |
return []; |
| 263 |
} |
| 264 |
|
| 265 |
return [ $twitter_setting ]; |
| 266 |
} |
| 267 |
} |
| 268 |
|