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

fix(esp-sync): sync Connected Account field #3414

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions includes/oauth/class-google-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public static function api_google_login_register( $request ) {
];

if ( $existing_user ) {
// Update user meta with connected account info.
\update_user_meta( $existing_user->ID, Reader_Activation::CONNECTED_ACCOUNT, 'google' );

// Log the user in.
$result = Reader_Activation::set_current_reader( $existing_user->ID );
$message = __( 'Thank you for signing in!', 'newspack-plugin' );
Expand Down
1 change: 1 addition & 0 deletions includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class Reader_Activation {
const EMAIL_VERIFIED = 'np_reader_email_verified';
const WITHOUT_PASSWORD = 'np_reader_without_password';
const REGISTRATION_METHOD = 'np_reader_registration_method';
const CONNECTED_ACCOUNT = 'np_reader_connected_account';
const READER_SAVED_GENERIC_DISPLAY_NAME = 'np_reader_saved_generic_display_name';

/**
Expand Down
63 changes: 53 additions & 10 deletions includes/reader-activation/sync/class-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Newspack\Reader_Activation\Sync;

use Newspack\Reader_Activation;
use Newspack\Logger;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -319,21 +320,42 @@ private static function get_utm_key( $key ) {
}

/**
* Normalizes contact metadata keys before syncing to ESP.
* Add user's registration-related data to the given metadata.
* These won't be included in every sync request, but they might be stored as user meta.
*
* @param array $contact Contact data.
* @return array Normalized contact data.
* @param array $metadata Metadata to add to.
*
* @return array Metadata with registration data added.
*/
public static function normalize_contact_data( $contact ) {
if ( ! isset( $contact['metadata'] ) ) {
$contact['metadata'] = [];
private static function add_registration_data( $metadata ) {
$user = self::has_key( 'account', $metadata ) ? \get_user_by( 'id', self::get_key_value( 'account', $metadata ) ) : false;
if ( ! $user ) {
return $metadata;
}

$metadata = $contact['metadata'];
$normalized_metadata = [];
$raw_keys = self::get_raw_keys();
$prefixed_keys = self::get_prefixed_keys();
$registration_method = self::has_key( 'registration_method', $metadata ) ? self::get_key_value( 'registration_method', $metadata ) : \get_user_meta( $user->ID, Reader_Activation::REGISTRATION_METHOD, true );
if ( ! empty( $registration_method ) ) {
$metadata['registration_method'] = $registration_method;
}

$connected_account = self::has_key( 'connected_account', $metadata ) ? self::get_key_value( 'connected_account', $metadata ) : \get_user_meta( $user->ID, Reader_Activation::CONNECTED_ACCOUNT, true );
if ( ! empty( $connected_account ) && in_array( $connected_account, Reader_Activation::SSO_REGISTRATION_METHODS ) ) {
$metadata['connected_account'] = $connected_account;
} elseif ( ! empty( $registration_method ) && in_array( $registration_method, Reader_Activation::SSO_REGISTRATION_METHODS ) ) {
$metadata['connected_account'] = $registration_method;
}

return $metadata;
}

/**
* Add UTM fields to the given metadata.
*
* @param array $metadata Metadata to add to.
*
* @return array Metadata with UTM fields added.
*/
private static function add_utm_data( $metadata ) {
// Capture UTM params and signup/payment page URLs as meta for registration or payment.
if ( self::has_key( 'current_page_url', $metadata ) || self::has_key( 'payment_page', $metadata ) ) {
$is_payment = self::has_key( 'payment_page', $metadata );
Expand Down Expand Up @@ -364,6 +386,27 @@ public static function normalize_contact_data( $contact ) {
}
}

return $metadata;
}

/**
* Normalizes contact metadata keys before syncing to ESP.
*
* @param array $contact Contact data.
* @return array Normalized contact data.
*/
public static function normalize_contact_data( $contact ) {
if ( ! isset( $contact['metadata'] ) ) {
$contact['metadata'] = [];
}

$metadata = $contact['metadata'];
$metadata = self::add_registration_data( $metadata );
$metadata = self::add_utm_data( $metadata );
$raw_keys = self::get_raw_keys();
$prefixed_keys = self::get_prefixed_keys();
$normalized_metadata = [];

// Keys allowed to pass through without prefixing.
$allowed_keys = [ 'status', 'status_if_new' ];

Expand Down
2 changes: 1 addition & 1 deletion includes/reader-activation/sync/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static function get_contact_from_customer( $customer, $payment_page_url =
$last_name = $customer->get_billing_last_name();
$full_name = trim( "$first_name $last_name" );
$contact = [
'email' => $customer->get_billing_email(),
'email' => ! empty( $customer->get_billing_email() ) ? $customer->get_billing_email() : $customer->get_email(),
'metadata' => $metadata,
];
if ( ! empty( $full_name ) ) {
Expand Down