Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings api - tiktok and yelp url fixes #169

Merged
merged 6 commits into from
Feb 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions includes/RestApi/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SettingsController {
'youtube_url' => '',
'wikipedia_url' => '',
'other_social_urls' => array(),
'mastodon_url' => '',
);

/**
Expand All @@ -69,6 +70,7 @@ class SettingsController {
'youtube_url',
'wikipedia_url',
'other_social_urls',
'mastodon_url',
);

/**
Expand Down Expand Up @@ -141,12 +143,12 @@ public function update_item( \WP_REST_Request $request ) {
}

// check for proper url
if ( in_array( $param_key, $this->social_urls_to_validate ) ) {
if ( in_array( $param_key, $this->social_urls_to_validate, true ) ) {
switch ( $param_key ) {
case 'twitter_site':
if ( ! empty( $params['twitter_site'] ) ) {
$twitter_id = $this->validate_twitter_id( $params['twitter_site'] );
if ( ( $twitter_id ) === false ) {
if ( false === $twitter_id ) {
$this->invalid_urls[] = 'twitter_site';
unset( $params['twitter_site'] );
} else {
Expand All @@ -155,14 +157,17 @@ public function update_item( \WP_REST_Request $request ) {
}
break;
case 'other_social_urls':
$filtered_social_urls = array();
foreach ( $param_value as $param_key_osu => $param_url ) {
$param_value[ $param_key_osu ] = \sanitize_text_field( $param_url );
if ( ! empty( $param_url ) && ! \wp_http_validate_url( $param_url ) ) {
$this->invalid_urls[] = $param_key_osu;
unset( $params[ $param_key_osu ] );
continue;
if ( ! empty( $param_url ) ) {
if ( \wp_http_validate_url( $param_url ) ) {
$filtered_social_urls[] = \sanitize_text_field( $param_url );
} else {
$this->invalid_urls[] = $param_key_osu;
}
}
}
$params[ $param_key ] = $filtered_social_urls;
break;
default:
$param[ $param_key ] = \sanitize_text_field( $param_value );
Expand Down Expand Up @@ -206,13 +211,26 @@ public function get_current_settings() {
// update database
\add_option( Options::get_option_name( 'wpseo_social', false ), $social_data );
}
$twitter_handle = $this->validate_twitter_id( $social_data['twitter_site'] );
// add the full url for twitter cause only the handle is saved in the database
if ( ! empty( $social_data['twitter_site'] ) &&
( false !== $twitter_handle ) ) {
$twitter_handle = $this->validate_twitter_id( $social_data['twitter_site'] );
if ( ( ! empty( $social_data['twitter_site'] ) ) && ( false !== $twitter_handle ) ) {
$social_data['twitter_site'] = 'https://www.twitter.com/' . $twitter_handle;
}

$filtered_social_urls = array();
// handle other social urls for onboarding
foreach ( $social_data['other_social_urls'] as $index => $social_url ) {
if ( ! empty( $social_url ) ) {
if ( preg_match( '/(?:https?:\/\/)?(www\.)?yelp\.com/', $social_url ) ) {
$filtered_social_urls['yelp_url'] = $social_url;
} elseif ( preg_match( '/(?:https?:\/\/)?(www\.)?tiktok\.com/', $social_url ) ) {
$filtered_social_urls['tiktok_url'] = $social_url;
} else {
$filtered_social_urls[ 'social_url_' . $index ] = $social_url;
}
}
}
$social_data['other_social_urls'] = $filtered_social_urls;
return $social_data;

}
Expand All @@ -231,20 +249,20 @@ public function initialize() {
);
}

// Update wp_options
// Update wp_options
$init_options = Options::get_initialization_options();
foreach ( $init_options as $option_key => $option_value ) {
\update_option( Options::get_option_name( $option_key, false ), $option_value );
\update_option( Options::get_option_name( $option_key, false ), $option_value );
}
// Can't be part of initialization constants as they are static.
// Can't be part of initialization constants as they are static.
\update_option( Options::get_option_name( 'install_date', false ), gmdate( 'M d, Y' ) );

// Flush permalinks
// Flush permalinks
flush_rewrite_rules();

// Add constants to the WordPress configuration (wp-config.php)
$wp_config_constants = Config::get_wp_config_initialization_constants();
$wp_config = new WP_Config();
// Add constants to the WordPress configuration (wp-config.php)
$wp_config_constants = Config::get_wp_config_initialization_constants();
$wp_config = new WP_Config();
foreach ( $wp_config_constants as $constant_key => $constant_value ) {
if ( $wp_config->constant_exists( $constant_key ) ) {
$wp_config->update_constant( $constant_key, $constant_value );
Expand All @@ -253,7 +271,7 @@ public function initialize() {
$wp_config->add_constant( $constant_key, $constant_value );
}

\update_option( Options::get_option_name( 'settings_initialized' ), true );
\update_option( Options::get_option_name( 'settings_initialized' ), true );

return new \WP_REST_Response(
array(),
Expand Down