From 7a95aba52706736224a9c39715d4352641be36f2 Mon Sep 17 00:00:00 2001 From: abhijitb Date: Mon, 6 Feb 2023 20:24:56 +0530 Subject: [PATCH 1/5] fixed yelp and tiktok url issues --- includes/RestApi/SettingsController.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index 1663dbd4f..643d38e52 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -162,10 +162,12 @@ public function update_item( \WP_REST_Request $request ) { break; case 'other_social_urls': foreach ( $param_value as $param_key_osu => $param_url ) { - $param_value[ $param_key_osu ] = \sanitize_text_field( $param_url ); + // remove the key => value pair and make it a simple indexed array for yoast plugin compatibility + unset( $params[$param_key][ $param_key_osu ]); + $params[$param_key][] = \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]); + unset($params[$param_key][$param_key_osu]); continue; } } @@ -217,6 +219,24 @@ public function get_current_settings() { $social_data['twitter_site'] = 'https://www.twitter.com/' . $twitter_handle; } + // handle other social urls for onboarding + foreach( $social_data['other_social_urls'] as $index => $social_url ) { + // remove the indexed url from array + unset( $social_data[ 'other_social_urls' ][ $index ] ); + + switch( $social_url ) { + case ( -1 < stripos( $social_url, 'yelp.com' ) ): + $social_data[ 'other_social_urls' ][ 'yelp_url' ] = $social_url; + break; + case ( -1 < stripos( $social_url, 'tiktok.com' ) ): + $social_data[ 'other_social_urls' ][ 'tiktok_url' ] = $social_url; + break; + default: + // creating key value pairs for other social urls instead of indexed array + $social_data[ 'other_social_urls' ][ 'social_url_' . $index ] = $social_url; + break; + } + } return $social_data; } From 9168410ab10a6bb536cba6d783623200f746ee10 Mon Sep 17 00:00:00 2001 From: abhijitb Date: Mon, 6 Feb 2023 20:50:14 +0530 Subject: [PATCH 2/5] fixed errors reported by php lint --- includes/RestApi/SettingsController.php | 62 +++++++++++++------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index 643d38e52..9135b7a77 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -82,6 +82,8 @@ class SettingsController { /** * Store for invalid urls + * + * @var array */ protected $invalid_urls = array(); @@ -143,18 +145,19 @@ public function update_item( \WP_REST_Request $request ) { foreach ( $params as $param_key => $param_value ) { if ( ! array_key_exists( $param_key, $this->defaults ) ) { $this->invalid_urls[] = $param_key; - unset($params[$param_key]); + unset( $params[ $param_key ] ); continue; } // check for proper url - if ( in_array( $param_key, $this->social_urls_to_validate ) ) { - switch($param_key) { + if ( in_array( $param_key, $this->social_urls_to_validate, true ) ) { + switch ( $param_key ) { case 'twitter_site': - if( !empty($params['twitter_site'])) { - if( ( $twitter_id = $this->validate_twitter_id($params['twitter_site']) ) === false ) { + if ( ! empty( $params['twitter_site'] ) ) { + $twitter_id = $this->validate_twitter_id( $params['twitter_site'] ); + if ( false === $twitter_id ) { $this->invalid_urls[] = 'twitter_site'; - unset($params['twitter_site']); + unset( $params['twitter_site'] ); } else { $params['twitter_site'] = $twitter_id; } @@ -163,11 +166,11 @@ public function update_item( \WP_REST_Request $request ) { case 'other_social_urls': foreach ( $param_value as $param_key_osu => $param_url ) { // remove the key => value pair and make it a simple indexed array for yoast plugin compatibility - unset( $params[$param_key][ $param_key_osu ]); - $params[$param_key][] = \sanitize_text_field( $param_url ); + unset( $params[ $param_key ][ $param_key_osu ] ); + $params[ $param_key ][] = \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][$param_key_osu]); + unset( $params[ $param_key ][ $param_key_osu ] ); continue; } } @@ -176,7 +179,7 @@ public function update_item( \WP_REST_Request $request ) { $param[ $param_key ] = \sanitize_text_field( $param_value ); if ( ! empty( $param_value ) && ! \wp_http_validate_url( $param_value ) ) { $this->invalid_urls[] = $param_key; - unset($params[$param_key]); + unset( $params[ $param_key ] ); } break; } @@ -186,8 +189,8 @@ public function update_item( \WP_REST_Request $request ) { \update_option( $this->yoast_wp_options_key, $settings ); - if(!empty($this->invalid_urls)) { - $error_keys = implode( ", ", $this->invalid_urls ); + if ( ! empty( $this->invalid_urls ) ) { + $error_keys = implode( ', ', $this->invalid_urls ); return new \WP_Error( 'invalid_urls', "Invalid url(s) provided for {$error_keys}.", @@ -205,7 +208,8 @@ public function update_item( \WP_REST_Request $request ) { public function get_current_settings() { // incase yoast plugin is not installed then we need to save the values in the yoast_wp_options_key - if ( ( $social_data = \get_option( $this->yoast_wp_options_key ) ) === false ) { + $social_data = \get_option( $this->yoast_wp_options_key ); + if ( false === $social_data ) { // initialize an array with default values $social_data = $this->defaults; @@ -214,26 +218,26 @@ public function get_current_settings() { \add_option( $this->yoast_wp_options_key, $social_data ); } // add the full url for twitter cause only the handle is saved in the database - if( (!empty($social_data['twitter_site'])) && - ($twitter_handle = $this->validate_twitter_id($social_data['twitter_site'])) !== false ) { + $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; } // handle other social urls for onboarding - foreach( $social_data['other_social_urls'] as $index => $social_url ) { + foreach ( $social_data['other_social_urls'] as $index => $social_url ) { // remove the indexed url from array - unset( $social_data[ 'other_social_urls' ][ $index ] ); + unset( $social_data['other_social_urls'][ $index ] ); - switch( $social_url ) { + switch ( $social_url ) { case ( -1 < stripos( $social_url, 'yelp.com' ) ): - $social_data[ 'other_social_urls' ][ 'yelp_url' ] = $social_url; + $social_data['other_social_urls']['yelp_url'] = $social_url; break; case ( -1 < stripos( $social_url, 'tiktok.com' ) ): - $social_data[ 'other_social_urls' ][ 'tiktok_url' ] = $social_url; + $social_data['other_social_urls']['tiktok_url'] = $social_url; break; default: // creating key value pairs for other social urls instead of indexed array - $social_data[ 'other_social_urls' ][ 'social_url_' . $index ] = $social_url; + $social_data['other_social_urls'][ 'social_url_' . $index ] = $social_url; break; } } @@ -255,20 +259,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 ); @@ -277,7 +281,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(), From b6012583b51f627382806d071b0984e03882346f Mon Sep 17 00:00:00 2001 From: abhijitb Date: Wed, 8 Feb 2023 15:33:54 +0530 Subject: [PATCH 3/5] fixed empty value issue for other social urls and added regex check --- includes/RestApi/SettingsController.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index cd105e284..ae46553ae 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -155,16 +155,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 ) { - // remove the key => value pair and make it a simple indexed array for yoast plugin compatibility - unset( $params[ $param_key ][ $param_key_osu ] ); - $params[ $param_key ][] = \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 ][ $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 ); @@ -220,10 +221,10 @@ public function get_current_settings() { unset( $social_data['other_social_urls'][ $index ] ); switch ( $social_url ) { - case ( -1 < stripos( $social_url, 'yelp.com' ) ): + case ( preg_match( '/(?:https?:\/\/)?(www\.)?yelp\.com\//', $social_url ) ? true : false ): $social_data['other_social_urls']['yelp_url'] = $social_url; break; - case ( -1 < stripos( $social_url, 'tiktok.com' ) ): + case ( preg_match( '/(?:https?:\/\/)?(www\.)?tiktok\.com\//', $social_url ) ? true : false ): $social_data['other_social_urls']['tiktok_url'] = $social_url; break; default: From 43ebccd42df61cf253cfa2ec9403c2e4d984ff1c Mon Sep 17 00:00:00 2001 From: abhijitb Date: Wed, 8 Feb 2023 16:38:50 +0530 Subject: [PATCH 4/5] added support for mastodon social media platform and refactored code --- includes/RestApi/SettingsController.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index ae46553ae..05fdfbcc9 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -52,6 +52,7 @@ class SettingsController { 'youtube_url' => '', 'wikipedia_url' => '', 'other_social_urls' => array(), + 'mastodon_url' => '', ); /** @@ -69,6 +70,7 @@ class SettingsController { 'youtube_url', 'wikipedia_url', 'other_social_urls', + 'mastodon_url', ); /** @@ -215,24 +217,23 @@ public function get_current_settings() { $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 ) { - // remove the indexed url from array - unset( $social_data['other_social_urls'][ $index ] ); - switch ( $social_url ) { - case ( preg_match( '/(?:https?:\/\/)?(www\.)?yelp\.com\//', $social_url ) ? true : false ): - $social_data['other_social_urls']['yelp_url'] = $social_url; + case ( preg_match( '/(?:https?:\/\/)?(www\.)?yelp\.com/', $social_url ) ? true : false ): + $filtered_social_urls['yelp_url'] = $social_url; break; - case ( preg_match( '/(?:https?:\/\/)?(www\.)?tiktok\.com\//', $social_url ) ? true : false ): - $social_data['other_social_urls']['tiktok_url'] = $social_url; + case ( preg_match( '/(?:https?:\/\/)?(www\.)?tiktok\.com/', $social_url ) ? true : false ): + $filtered_social_urls['tiktok_url'] = $social_url; break; default: // creating key value pairs for other social urls instead of indexed array - $social_data['other_social_urls'][ 'social_url_' . $index ] = $social_url; + $filtered_social_urls[ 'social_url_' . $index ] = $social_url; break; } } + $social_data['other_social_urls'] = $filtered_social_urls; return $social_data; } From 9068f59e0e00b863068da57fa6db08a065de055d Mon Sep 17 00:00:00 2001 From: abhijitb Date: Wed, 8 Feb 2023 17:40:56 +0530 Subject: [PATCH 5/5] refactored code to use IF instead of SWITCH --- includes/RestApi/SettingsController.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index 05fdfbcc9..6280bd095 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -220,17 +220,14 @@ public function get_current_settings() { $filtered_social_urls = array(); // handle other social urls for onboarding foreach ( $social_data['other_social_urls'] as $index => $social_url ) { - switch ( $social_url ) { - case ( preg_match( '/(?:https?:\/\/)?(www\.)?yelp\.com/', $social_url ) ? true : false ): + if ( ! empty( $social_url ) ) { + if ( preg_match( '/(?:https?:\/\/)?(www\.)?yelp\.com/', $social_url ) ) { $filtered_social_urls['yelp_url'] = $social_url; - break; - case ( preg_match( '/(?:https?:\/\/)?(www\.)?tiktok\.com/', $social_url ) ? true : false ): + } elseif ( preg_match( '/(?:https?:\/\/)?(www\.)?tiktok\.com/', $social_url ) ) { $filtered_social_urls['tiktok_url'] = $social_url; - break; - default: - // creating key value pairs for other social urls instead of indexed array + } else { $filtered_social_urls[ 'social_url_' . $index ] = $social_url; - break; + } } } $social_data['other_social_urls'] = $filtered_social_urls;