Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordpres.../src/helpers
File: social-profiles-helper.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Helpers;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Class Social_Profiles_Helper.
[5] Fix | Delete
*/
[6] Fix | Delete
class Social_Profiles_Helper {
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* The fields for the person social profiles payload.
[10] Fix | Delete
*
[11] Fix | Delete
* @var array
[12] Fix | Delete
*/
[13] Fix | Delete
private $person_social_profile_fields = [
[14] Fix | Delete
'facebook' => 'get_non_valid_url',
[15] Fix | Delete
'instagram' => 'get_non_valid_url',
[16] Fix | Delete
'linkedin' => 'get_non_valid_url',
[17] Fix | Delete
'myspace' => 'get_non_valid_url',
[18] Fix | Delete
'pinterest' => 'get_non_valid_url',
[19] Fix | Delete
'soundcloud' => 'get_non_valid_url',
[20] Fix | Delete
'tumblr' => 'get_non_valid_url',
[21] Fix | Delete
'twitter' => 'get_non_valid_twitter',
[22] Fix | Delete
'youtube' => 'get_non_valid_url',
[23] Fix | Delete
'wikipedia' => 'get_non_valid_url',
[24] Fix | Delete
];
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* The fields for the organization social profiles payload.
[28] Fix | Delete
*
[29] Fix | Delete
* @var array
[30] Fix | Delete
*/
[31] Fix | Delete
private $organization_social_profile_fields = [
[32] Fix | Delete
'facebook_site' => 'get_non_valid_url',
[33] Fix | Delete
'twitter_site' => 'get_non_valid_twitter',
[34] Fix | Delete
'other_social_urls' => 'get_non_valid_url_array',
[35] Fix | Delete
];
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* The Options_Helper instance.
[39] Fix | Delete
*
[40] Fix | Delete
* @var Options_Helper
[41] Fix | Delete
*/
[42] Fix | Delete
protected $options_helper;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Social_Profiles_Helper constructor.
[46] Fix | Delete
*
[47] Fix | Delete
* @param Options_Helper $options_helper The WPSEO options helper.
[48] Fix | Delete
*/
[49] Fix | Delete
public function __construct( Options_Helper $options_helper ) {
[50] Fix | Delete
$this->options_helper = $options_helper;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Gets the person social profile fields supported by us.
[55] Fix | Delete
*
[56] Fix | Delete
* @return array The social profile fields.
[57] Fix | Delete
*/
[58] Fix | Delete
public function get_person_social_profile_fields() {
[59] Fix | Delete
/**
[60] Fix | Delete
* Filter: Allow changes to the social profiles fields available for a person.
[61] Fix | Delete
*
[62] Fix | Delete
* @param array $person_social_profile_fields The social profile fields.
[63] Fix | Delete
*/
[64] Fix | Delete
$person_social_profile_fields = \apply_filters( 'wpseo_person_social_profile_fields', $this->person_social_profile_fields );
[65] Fix | Delete
[66] Fix | Delete
return (array) $person_social_profile_fields;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Gets the organization social profile fields supported by us.
[71] Fix | Delete
*
[72] Fix | Delete
* @return array The organization profile fields.
[73] Fix | Delete
*/
[74] Fix | Delete
public function get_organization_social_profile_fields() {
[75] Fix | Delete
/**
[76] Fix | Delete
* Filter: Allow changes to the social profiles fields available for an organization.
[77] Fix | Delete
*
[78] Fix | Delete
* @param array $organization_social_profile_fields The social profile fields.
[79] Fix | Delete
*/
[80] Fix | Delete
$organization_social_profile_fields = \apply_filters( 'wpseo_organization_social_profile_fields', $this->organization_social_profile_fields );
[81] Fix | Delete
[82] Fix | Delete
return (array) $organization_social_profile_fields;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Gets the person social profiles stored in the database.
[87] Fix | Delete
*
[88] Fix | Delete
* @param int $person_id The id of the person.
[89] Fix | Delete
*
[90] Fix | Delete
* @return array The person's social profiles.
[91] Fix | Delete
*/
[92] Fix | Delete
public function get_person_social_profiles( $person_id ) {
[93] Fix | Delete
$social_profile_fields = \array_keys( $this->get_person_social_profile_fields() );
[94] Fix | Delete
$person_social_profiles = \array_combine( $social_profile_fields, \array_fill( 0, \count( $social_profile_fields ), '' ) );
[95] Fix | Delete
[96] Fix | Delete
// If no person has been selected, $person_id is set to false.
[97] Fix | Delete
if ( \is_numeric( $person_id ) ) {
[98] Fix | Delete
foreach ( \array_keys( $person_social_profiles ) as $field_name ) {
[99] Fix | Delete
$value = \get_user_meta( $person_id, $field_name, true );
[100] Fix | Delete
// If $person_id is an integer but does not represent a valid user, get_user_meta returns false.
[101] Fix | Delete
if ( ! \is_bool( $value ) ) {
[102] Fix | Delete
$person_social_profiles[ $field_name ] = $value;
[103] Fix | Delete
}
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
return $person_social_profiles;
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Gets the organization social profiles stored in the database.
[112] Fix | Delete
*
[113] Fix | Delete
* @return array<string, string> The social profiles for the organization.
[114] Fix | Delete
*/
[115] Fix | Delete
public function get_organization_social_profiles() {
[116] Fix | Delete
$organization_social_profiles_fields = \array_keys( $this->get_organization_social_profile_fields() );
[117] Fix | Delete
$organization_social_profiles = [];
[118] Fix | Delete
[119] Fix | Delete
foreach ( $organization_social_profiles_fields as $field_name ) {
[120] Fix | Delete
$default_value = '';
[121] Fix | Delete
if ( $field_name === 'other_social_urls' ) {
[122] Fix | Delete
$default_value = [];
[123] Fix | Delete
}
[124] Fix | Delete
$social_profile_value = $this->options_helper->get( $field_name, $default_value );
[125] Fix | Delete
[126] Fix | Delete
if ( $field_name === 'other_social_urls' ) {
[127] Fix | Delete
$other_social_profiles = \array_map( '\urldecode', \array_filter( $social_profile_value ) );
[128] Fix | Delete
$organization_social_profiles['other_social_urls'] = $other_social_profiles;
[129] Fix | Delete
continue;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
if ( $field_name === 'twitter_site' && $social_profile_value !== '' ) {
[133] Fix | Delete
$organization_social_profiles[ $field_name ] = 'https://x.com/' . $social_profile_value;
[134] Fix | Delete
continue;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
$organization_social_profiles[ $field_name ] = \urldecode( $social_profile_value );
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
return $organization_social_profiles;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Stores the values for the person's social profiles.
[145] Fix | Delete
*
[146] Fix | Delete
* @param int $person_id The id of the person to edit.
[147] Fix | Delete
* @param array $social_profiles The array of the person's social profiles to be set.
[148] Fix | Delete
*
[149] Fix | Delete
* @return string[] An array of field names which failed to be saved in the db.
[150] Fix | Delete
*/
[151] Fix | Delete
public function set_person_social_profiles( $person_id, $social_profiles ) {
[152] Fix | Delete
$failures = [];
[153] Fix | Delete
$person_social_profile_fields = $this->get_person_social_profile_fields();
[154] Fix | Delete
[155] Fix | Delete
// First validate all social profiles, before even attempting to save them.
[156] Fix | Delete
foreach ( $person_social_profile_fields as $field_name => $validation_method ) {
[157] Fix | Delete
if ( ! isset( $social_profiles[ $field_name ] ) ) {
[158] Fix | Delete
// Just skip social profiles that were not passed.
[159] Fix | Delete
continue;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
if ( $social_profiles[ $field_name ] === '' ) {
[163] Fix | Delete
continue;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
$value_to_validate = $social_profiles[ $field_name ];
[167] Fix | Delete
$failures = \array_merge( $failures, \call_user_func( [ $this, $validation_method ], $value_to_validate, $field_name ) );
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
if ( ! empty( $failures ) ) {
[171] Fix | Delete
return $failures;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
// All social profiles look good, now let's try to save them.
[175] Fix | Delete
foreach ( \array_keys( $person_social_profile_fields ) as $field_name ) {
[176] Fix | Delete
if ( ! isset( $social_profiles[ $field_name ] ) ) {
[177] Fix | Delete
// Just skip social profiles that were not passed.
[178] Fix | Delete
continue;
[179] Fix | Delete
}
[180] Fix | Delete
$social_profiles[ $field_name ] = $this->sanitize_social_field( $social_profiles[ $field_name ] );
[181] Fix | Delete
\update_user_meta( $person_id, $field_name, $social_profiles[ $field_name ] );
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
return $failures;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Stores the values for the organization's social profiles.
[189] Fix | Delete
*
[190] Fix | Delete
* @param array $social_profiles An array with the social profiles values to be saved in the db.
[191] Fix | Delete
*
[192] Fix | Delete
* @return string[] An array of field names which failed to be saved in the db.
[193] Fix | Delete
*/
[194] Fix | Delete
public function set_organization_social_profiles( $social_profiles ) {
[195] Fix | Delete
$failures = [];
[196] Fix | Delete
$organization_social_profile_fields = $this->get_organization_social_profile_fields();
[197] Fix | Delete
[198] Fix | Delete
// First validate all social profiles, before even attempting to save them.
[199] Fix | Delete
foreach ( $organization_social_profile_fields as $field_name => $validation_method ) {
[200] Fix | Delete
if ( ! isset( $social_profiles[ $field_name ] ) ) {
[201] Fix | Delete
// Just skip social profiles that were not passed.
[202] Fix | Delete
continue;
[203] Fix | Delete
}
[204] Fix | Delete
$social_profiles[ $field_name ] = $this->sanitize_social_field( $social_profiles[ $field_name ] );
[205] Fix | Delete
[206] Fix | Delete
$value_to_validate = $social_profiles[ $field_name ];
[207] Fix | Delete
$failures = \array_merge( $failures, \call_user_func( [ $this, $validation_method ], $value_to_validate, $field_name ) );
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
if ( ! empty( $failures ) ) {
[211] Fix | Delete
return $failures;
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
// All social profiles look good, now let's try to save them.
[215] Fix | Delete
foreach ( \array_keys( $organization_social_profile_fields ) as $field_name ) {
[216] Fix | Delete
if ( ! isset( $social_profiles[ $field_name ] ) ) {
[217] Fix | Delete
// Just skip social profiles that were not passed.
[218] Fix | Delete
continue;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
// Remove empty strings in Other Social URLs.
[222] Fix | Delete
if ( $field_name === 'other_social_urls' ) {
[223] Fix | Delete
$other_social_urls = \array_filter(
[224] Fix | Delete
$social_profiles[ $field_name ],
[225] Fix | Delete
static function ( $other_social_url ) {
[226] Fix | Delete
return $other_social_url !== '';
[227] Fix | Delete
}
[228] Fix | Delete
);
[229] Fix | Delete
[230] Fix | Delete
$social_profiles[ $field_name ] = \array_values( $other_social_urls );
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
$result = $this->options_helper->set( $field_name, $social_profiles[ $field_name ] );
[234] Fix | Delete
if ( ! $result ) {
[235] Fix | Delete
/**
[236] Fix | Delete
* The value for Twitter might have been sanitised from URL to username.
[237] Fix | Delete
* If so, $result will be false. We should check if the option value is part of the received value.
[238] Fix | Delete
*/
[239] Fix | Delete
if ( $field_name === 'twitter_site' ) {
[240] Fix | Delete
$current_option = $this->options_helper->get( $field_name );
[241] Fix | Delete
if ( ! \strpos( $social_profiles[ $field_name ], 'twitter.com/' . $current_option ) && ! \strpos( $social_profiles[ $field_name ], 'x.com/' . $current_option ) ) {
[242] Fix | Delete
$failures[] = $field_name;
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
else {
[246] Fix | Delete
$failures[] = $field_name;
[247] Fix | Delete
}
[248] Fix | Delete
}
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
if ( ! empty( $failures ) ) {
[252] Fix | Delete
return $failures;
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
return [];
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
/**
[259] Fix | Delete
* Returns a sanitized social field.
[260] Fix | Delete
*
[261] Fix | Delete
* @param string|array $social_field The social field to sanitize.
[262] Fix | Delete
*
[263] Fix | Delete
* @return string|array The sanitized social field.
[264] Fix | Delete
*/
[265] Fix | Delete
protected function sanitize_social_field( $social_field ) {
[266] Fix | Delete
if ( \is_array( $social_field ) ) {
[267] Fix | Delete
foreach ( $social_field as $key => $value ) {
[268] Fix | Delete
$social_field[ $key ] = \sanitize_text_field( $value );
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
return $social_field;
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
return \sanitize_text_field( $social_field );
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
/**
[278] Fix | Delete
* Checks if url is not valid and returns the name of the setting if it's not.
[279] Fix | Delete
*
[280] Fix | Delete
* @param string $url The url to be validated.
[281] Fix | Delete
* @param string $url_setting The name of the setting to be updated with the url.
[282] Fix | Delete
*
[283] Fix | Delete
* @return array An array with the setting that the non-valid url is about to update.
[284] Fix | Delete
*/
[285] Fix | Delete
protected function get_non_valid_url( $url, $url_setting ) {
[286] Fix | Delete
if ( $this->options_helper->is_social_url_valid( $url ) ) {
[287] Fix | Delete
return [];
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
return [ $url_setting ];
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
/**
[294] Fix | Delete
* 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
[295] Fix | Delete
*
[296] Fix | Delete
* @param array $urls The urls to be validated.
[297] Fix | Delete
* @param string $urls_setting The name of the setting to be updated with the urls.
[298] Fix | Delete
*
[299] Fix | Delete
* @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.
[300] Fix | Delete
*/
[301] Fix | Delete
protected function get_non_valid_url_array( $urls, $urls_setting ) {
[302] Fix | Delete
$non_valid_url_array = [];
[303] Fix | Delete
[304] Fix | Delete
foreach ( $urls as $key => $url ) {
[305] Fix | Delete
if ( ! $this->options_helper->is_social_url_valid( $url ) ) {
[306] Fix | Delete
$non_valid_url_array[] = $urls_setting . '-' . $key;
[307] Fix | Delete
}
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
return $non_valid_url_array;
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
/**
[314] Fix | Delete
* Checks if the twitter value is not valid and returns the name of the setting if it's not.
[315] Fix | Delete
*
[316] Fix | Delete
* @param array $twitter_site The twitter value to be validated.
[317] Fix | Delete
* @param string $twitter_setting The name of the twitter setting to be updated with the value.
[318] Fix | Delete
*
[319] Fix | Delete
* @return array An array with the setting that the non-valid twitter value is about to update.
[320] Fix | Delete
*/
[321] Fix | Delete
protected function get_non_valid_twitter( $twitter_site, $twitter_setting ) {
[322] Fix | Delete
if ( $this->options_helper->is_twitter_id_valid( $twitter_site, false ) ) {
[323] Fix | Delete
return [];
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
return [ $twitter_setting ];
[327] Fix | Delete
}
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function