Skip to content

Commit

Permalink
Refactored the listener and fixed some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pls78 committed Oct 16, 2023
1 parent 360de7c commit ca658ab
Showing 1 changed file with 69 additions and 28 deletions.
97 changes: 69 additions & 28 deletions includes/Listeners/Yoast.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
class Yoast extends Listener {
// We don't want to track these fields
private $skip_fields = [ 'company_logo_id', 'person_logo_id', 'description' ];
private $site_representation_skip_fields = [ 'company_logo_id', 'person_logo_id', 'description' ];

// The names used for Hiive events tracking are different from the names used for the Yoast options
private $map = [
private $site_representation_map = [
'company_or_person' => 'site_representation',
'company_name' => 'organization_name',
'company_logo' => 'organization_logo',
Expand All @@ -19,6 +19,12 @@ class Yoast extends Listener {
'website_name' => 'website_name',
];

private $social_profiles_map = [
'facebook_site' => 'facebook_profile',
'twitter_site' => 'twitter_profile',
'other_social_urls' => 'other_social_urls',
];

/**
* Register the hooks for the listener
*
Expand All @@ -27,7 +33,7 @@ class Yoast extends Listener {
public function register_hooks() {
// First time configuration
add_action('wpseo_ftc_post_update_site_representation', array( $this, 'site_representation_updated' ), 10, 3 );
add_action('wpseo_ftc_post_update_social_profiles', array( $this, 'site_representation_updated' ), 10, 3 );
add_action('wpseo_ftc_post_update_social_profiles', array( $this, 'social_profiles_updated' ), 10, 3 );
add_action('wpseo_ftc_post_update_enable_tracking', array( $this, 'tracking_updated' ), 10, 3 );
}

Expand All @@ -46,10 +52,10 @@ public function site_representation_updated( $new_values, $old_values, $failures
return;
}

$mapped_new_values = $this->map_site_representation_params_names_to_hiive_names( $new_values );
$mapped_old_values = $this->map_site_representation_params_names_to_hiive_names( $old_values );
$mapped_failures = $this->map_site_representation_failures_to_hiive_names( $failures );
$mapped_new_values = $this->map_params_names_to_hiive_names( $new_values, $this->site_representation_map, $this->site_representation_skip_fields );
$mapped_old_values = $this->map_params_names_to_hiive_names( $old_values, $this->site_representation_map, $this->site_representation_skip_fields );
$mapped_failures = $this->map_failures_to_hiive_names( $failures, $this->site_representation_map, $this->site_representation_skip_fields );

foreach ($mapped_new_values as $key => $value) {
$this->maybe_push_event( $key, $value, $mapped_old_values[ $key ], \in_array( $key, $mapped_failures ), 'ftc_site_representation' );
}
Expand All @@ -64,14 +70,23 @@ public function site_representation_updated( $new_values, $old_values, $failures
*
* @return void
*/
public function personal_profiles_updated( $new_values, $old_values, $failures ) {
public function social_profiles_updated( $new_values, $old_values, $failures ) {
// Yoast stores only twitter username, and $new_values stores the pre-processed values
if ( strpos( $new_values[ 'twitter_site' ], 'twitter.com/' ) !== false ) {
$new_values[ 'twitter_site' ] = (explode( 'twitter.com/', $new_values[ 'twitter_site' ])[ 1 ] );
}

// All the options are unchanged, opt out
if ( $new_values === $old_values ) {
return;
}

foreach ($new_values as $key => $value) {
$this->maybe_push_event( $key, $value, $old_values[ $key ], \in_array( $key, $failures ), 'ftc_personal_profiles' );
$mapped_new_values = $this->map_params_names_to_hiive_names( $new_values, $this->social_profiles_map );
$mapped_old_values = $this->map_params_names_to_hiive_names( $old_values, $this->social_profiles_map );
$mapped_failures = $this->map_failures_to_hiive_names( $failures, $this->social_profiles_map );

foreach ($mapped_new_values as $key => $value) {
$this->maybe_push_event( $key, $value, $mapped_old_values[ $key ], \in_array( $key, $mapped_failures ), 'ftc_personal_profiles' );
}
}

Expand All @@ -85,18 +100,12 @@ public function personal_profiles_updated( $new_values, $old_values, $failures )
* @return void
*/
public function tracking_updated( $new_value, $old_value, $failed ) {
$category = 'ftc_tracking';

if ( $failed ) {
$this->push( "failed_usage_tracking", [ 'category' => $category ] );
// All the options are unchanged, opt out
if ( $new_value === $old_value ) {
return;
}


// All the options are unchanged, opt out
if ( $new_value !== $old_value ) {
$this->push( "changed_usage_tracking", [ 'category' => $category ] );
}
$this->maybe_push_event( 'usage_tracking', $new_value, $old_value, $failed, 'ftc_tracking' );
}

/**
Expand All @@ -120,7 +129,20 @@ private function maybe_push_event( $key, $value, $old_value, $failure, $category
// The option value changed
if ( $value !== $old_value ) {
// The option was set for the first time
if (strlen($old_value) === 0 ) {

// name is a special case, because it represents the company_or_person_user_id which is initialised to false, and the first time the user saves the site representation step
// is set either to 0 if the site represents an organisation, or to an integer > 0 if the site represents a person
if ( $key === 'name' ) {
if ( $old_value === false && $value === 0 ) {
return;
}
}

// Again, name is a special case, because if its old value was 0 and a value different that 0 is being received, it means that the user
// switched from organisation to person, and then the person id is being set.
// Once the name is assigned an integer > 0, it can never go back to 0, even if the user switches back to organisation
// ( it "caches" the last user id that was set)
if ( ( $this->is_param_empty( $old_value) ) || ( $key === 'name' && $old_value === 0 ) ){
$this->push( "set_$key", [ 'category' => $category] );
return;
}
Expand All @@ -144,19 +166,21 @@ private function maybe_push_event( $key, $value, $old_value, $failure, $category
/**
* Maps the param names to the names used for Hiive events tracking.
*
* @param array $params The params to map.
* @param array $params The params to map.
* @param array $map The map to use.
* @param array $skip_fields The fields to skip.
*
* @return array The mapped params.
*/
private function map_site_representation_params_names_to_hiive_names( $params ) {
private function map_params_names_to_hiive_names( $params, $map, $skip_fields=[] ) {
$mapped_params = [];

foreach ( $params as $param_name => $param_value ) {
if ( in_array( $param_name, $this->skip_fields, true ) ) {
if ( in_array( $param_name, $skip_fields, true ) ) {
continue;
}

$new_name = $this->map[ $param_name ];
$new_name = $map[ $param_name ];
$mapped_params[ $new_name ] = $param_value;
}

Expand All @@ -166,21 +190,38 @@ private function map_site_representation_params_names_to_hiive_names( $params )
/**
* Maps the names of the params which failed the update to the names used for Hiive events tracking.
*
* @param array $failures The params names to map.
* @param array $failures The params names to map.
* @param array $map The map to use.
* @param array $skip_fields The fields to skip.
*
* @return array The mapped params names.
*/
private function map_site_representation_failures_to_hiive_names( $failures ) {
private function map_failures_to_hiive_names( $failures, $map, $skip_fields=[] ) {
$mapped_failures = [];

foreach ( $failures as $failed_filed_name) {
if ( in_array( $failed_filed_name, $this->skip_fields, true ) ) {
if ( in_array( $failed_filed_name, $skip_fields, true ) ) {
continue;
}

$mapped_failures = $this->map[ $failed_filed_name ];
$mapped_failures = $map[ $failed_filed_name ];
}

return $mapped_failures;
}

/**
* Checks whether a param is empty.
*
* @param mixed $param The param to check.
*
* @return bool Whether the param is empty.
*/
private function is_param_empty( $param ) {
if ( is_array( $param ) ) {
return ( count( $param ) === 0 );
}

return ( strlen( $param ) === 0 );
}
}

0 comments on commit ca658ab

Please sign in to comment.