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

feat(ras): unify ESP connector strategy for data events #3360

Merged
merged 38 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3037cd4
feat(ras): helper method for ESP master list
miguelpeixe Aug 21, 2024
063bc1b
fix: ensure bool return
miguelpeixe Aug 21, 2024
e17ccf0
feat(ras): woo sync tools
miguelpeixe Aug 22, 2024
499bd5b
chore: update textdomain
miguelpeixe Aug 22, 2024
3b5848d
chore: use namespace for WooCommerce_Connection
miguelpeixe Aug 22, 2024
3d09777
chore: rename class
miguelpeixe Aug 22, 2024
fdda98d
chore: remove superfluous check
miguelpeixe Aug 22, 2024
ba4292d
fix: method calling
miguelpeixe Aug 22, 2024
7658806
chore: woo sync is now an ESP sync
miguelpeixe Aug 22, 2024
5c4b971
feat(ras): unify ESP connector strategy for data events
miguelpeixe Aug 22, 2024
406bd7c
feat: check master list and make ::sync() protected
miguelpeixe Aug 22, 2024
24ecd80
chore: fix doc
miguelpeixe Aug 22, 2024
d8642c9
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 22, 2024
e5e9a15
chore: remove duplicate check
miguelpeixe Aug 22, 2024
8a7f0b2
chore: adjust wording
miguelpeixe Aug 22, 2024
d3b206c
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 22, 2024
b854ff1
feat: support custom context
miguelpeixe Aug 22, 2024
f808a92
feat: support custom context
miguelpeixe Aug 22, 2024
098dca9
chore: update context defaults
miguelpeixe Aug 22, 2024
64d6079
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 22, 2024
f80495c
chore: rename method
miguelpeixe Aug 23, 2024
cc14d08
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 23, 2024
3bd55da
fix: class access
miguelpeixe Aug 24, 2024
72735d8
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 24, 2024
fd27a50
chore: update method name
miguelpeixe Aug 26, 2024
44ca1e4
Merge branch 'trunk' into feat/woo-sync-tools
miguelpeixe Aug 27, 2024
d642822
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 27, 2024
a6c1365
fix: add membership_saved handler
miguelpeixe Aug 27, 2024
a256b54
Merge branch 'trunk' into feat/woo-sync-tools
miguelpeixe Aug 27, 2024
8d8f398
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 27, 2024
97b8056
fix: method calling and remove unused code
miguelpeixe Aug 27, 2024
a8656cd
fix: method calling
miguelpeixe Aug 27, 2024
6ff08d8
Merge branch 'trunk' into feat/woo-sync-tools
miguelpeixe Aug 28, 2024
245fafa
Merge branch 'feat/woo-sync-tools' into feat/esp-connector-with-sync
miguelpeixe Aug 28, 2024
9a2bdce
Merge branch 'trunk' into feat/esp-connector-with-sync
miguelpeixe Aug 29, 2024
373e157
fix: update methods and constant
miguelpeixe Aug 29, 2024
1d198b9
fix: remove "reader_registered" metadata filter and favor the sync fi…
miguelpeixe Aug 30, 2024
87a1203
feat: improve context message for newsletter_selection field update
leogermani Aug 30, 2024
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
14 changes: 6 additions & 8 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/class-plugin-manager.php';
include_once NEWSPACK_ABSPATH . 'includes/class-theme-manager.php';
include_once NEWSPACK_ABSPATH . 'includes/class-admin-plugins-screen.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-reader-activation-emails.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-reader-activation.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-reader-data.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-esp-sync.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-esp-sync-admin.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-utils.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-data-events.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-webhooks.php';
Expand All @@ -86,17 +91,10 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-popups.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-memberships.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/ga4/class-ga4.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-connector.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-mailchimp.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-activecampaign.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-esp-connector.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-woo-user-registration.php';
include_once NEWSPACK_ABSPATH . 'includes/class-api.php';
include_once NEWSPACK_ABSPATH . 'includes/class-profile.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-reader-activation-emails.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-reader-activation.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-reader-data.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-esp-sync.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-activation/class-esp-sync-admin.php';
include_once NEWSPACK_ABSPATH . 'includes/class-recaptcha.php';
include_once NEWSPACK_ABSPATH . 'includes/class-magic-link.php';
include_once NEWSPACK_ABSPATH . 'includes/reader-revenue/stripe/class-stripe-connection.php';
Expand Down
110 changes: 0 additions & 110 deletions includes/data-events/connectors/class-activecampaign.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
<?php
/**
* Abstract Newspack Data Events Connector class
* Newspack Data Events ESP Connector.
*
* @package Newspack
*/

namespace Newspack\Data_Events\Connectors;

use Newspack\Newspack_Newsletters;
use Newspack\Data_Events;
use Newspack\WooCommerce_Connection;
use Newspack\Newspack_Newsletters;
use Newspack\Reader_Activation;
use Newspack_Newsletters_Contacts;

defined( 'ABSPATH' ) || exit;

/**
* Standard methods shared by all connectors.
* ESP Connector Class.
*/
abstract class Connector {
class ESP_Connector extends Reader_Activation\ESP_Sync {
/**
* Initialize hooks.
*/
public static function init_hooks() {
add_action( 'init', [ __CLASS__, 'register_handlers' ] );
}

/**
* Register handlers.
*/
public static function register_handlers() {
if ( ! self::can_esp_sync() ) {
return;
}
Data_Events::register_handler( [ __CLASS__, 'reader_registered' ], 'reader_registered' );
Data_Events::register_handler( [ __CLASS__, 'reader_deleted' ], 'reader_deleted' );
Data_Events::register_handler( [ __CLASS__, 'reader_logged_in' ], 'reader_logged_in' );
Data_Events::register_handler( [ __CLASS__, 'order_completed' ], 'order_completed' );
Data_Events::register_handler( [ __CLASS__, 'subscription_updated' ], 'donation_subscription_changed' );
Data_Events::register_handler( [ __CLASS__, 'subscription_updated' ], 'product_subscription_changed' );
Data_Events::register_handler( [ __CLASS__, 'newsletter_updated' ], 'newsletter_subscribed' );
Data_Events::register_handler( [ __CLASS__, 'newsletter_updated' ], 'newsletter_updated' );
Data_Events::register_handler( [ __CLASS__, 'network_new_reader' ], 'network_new_reader' );
Data_Events::register_handler( [ __CLASS__, 'membership_saved' ], 'membership_saved' );
}

/**
* Handle a reader registering.
*
Expand All @@ -37,21 +65,13 @@ public static function reader_registered( $timestamp, $data, $client_id ) {
if ( isset( $data['metadata']['registration_method'] ) ) {
$metadata[ Newspack_Newsletters::get_metadata_key( 'registration_method' ) ] = $data['metadata']['registration_method'];
}
/**
* Filters the contact metadata sent to the ESP when a reader account is registered for the first time.
*
* @param array $metadata The contact metadata.
* @param int $user_id The ID of the user.
*
* @return array The modified contact metadata.
*/
$metadata = \apply_filters( 'newspack_data_events_reader_registered_metadata', $metadata, $data['user_id'] );
$contact = [

$contact = [
'email' => $data['email'],
'metadata' => $metadata,
];

static::put( $contact, 'RAS Reader registration' );
self::sync( $contact, 'RAS Reader registration' );
}

/**
Expand All @@ -75,7 +95,7 @@ public static function reader_logged_in( $timestamp, $data, $client_id ) {

$contact = WooCommerce_Connection::get_contact_from_customer( $customer );

static::put( $contact, 'RAS Reader login' );
self::sync( $contact, 'RAS Reader login' );
}

/**
Expand Down Expand Up @@ -103,7 +123,7 @@ public static function order_completed( $timestamp, $data, $client_id ) {
return;
}

static::put( $contact, 'RAS Order completed' );
self::sync( $contact, 'RAS Order completed' );
}

/**
Expand Down Expand Up @@ -132,7 +152,7 @@ public static function membership_saved( $timestamp, $data, $client_id ) {
}
}

static::put( $contact, 'RAS Woo Membership created or updated.' );
self::sync( $contact, 'RAS Woo Membership created or updated.' );
}

/**
Expand All @@ -153,7 +173,7 @@ public static function subscription_updated( $timestamp, $data, $client_id ) {
return;
}

static::put( $contact, sprintf( 'RAS Woo Subscription updated. Status changed from %s to %s', $data['status_before'], $data['status_after'] ) );
self::sync( $contact, sprintf( 'RAS Woo Subscription updated. Status changed from %s to %s', $data['status_before'], $data['status_after'] ) );
}

/**
Expand All @@ -164,11 +184,52 @@ public static function subscription_updated( $timestamp, $data, $client_id ) {
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_deleted( $timestamp, $data, $client_id ) {
if ( true === \Newspack\Reader_Activation::get_setting( 'sync_esp_delete' ) ) {
if ( true === Reader_Activation::get_setting( 'sync_esp_delete' ) ) {
return Newspack_Newsletters_Contacts::delete( $data['user_id'], 'RAS Reader deleted' );
}
}

/**
* Handle newsletter subscription update.
*
* @param int $timestamp Timestamp.
* @param array $data Data.
*/
public static function newsletter_updated( $timestamp, $data ) {
if ( empty( $data['user_id'] ) || empty( $data['email'] ) ) {
return;
}
$subscribed_lists = \Newspack_Newsletters_Subscription::get_contact_lists( $data['email'] );
if ( is_wp_error( $subscribed_lists ) || ! is_array( $subscribed_lists ) ) {
return;
}
$lists = \Newspack_Newsletters_Subscription::get_lists();
if ( is_wp_error( $lists ) ) {
return;
}
$lists_names = [];
foreach ( $subscribed_lists as $subscribed_list_id ) {
foreach ( $lists as $list ) {
if ( $list['id'] === $subscribed_list_id ) {
$lists_names[] = $list['name'];
}
}
}

$account_key = Newspack_Newsletters::get_metadata_key( 'account' );
$newsletter_selection_key = Newspack_Newsletters::get_metadata_key( 'newsletter_selection' );

$metadata = [
$account_key => $data['user_id'],
$newsletter_selection_key => implode( ', ', $lists_names ),
];
$contact = [
'email' => $data['email'],
'metadata' => $metadata,
];
self::sync( $contact, 'Updating newsletter_selection field after a change in the subscription lists.' );
}

/**
* Handle a a new network added in the Newspack Network plugin.
*
Expand Down Expand Up @@ -199,6 +260,7 @@ public static function network_new_reader( $timestamp, $data, $client_id ) {
$contact['metadata']['network_registration_site'] = $registration_site;

$site_url = get_site_url();
static::put( $contact, sprintf( 'RAS Newspack Network: User propagated from another site in the network. Propagated from %s to %s.', $registration_site, $site_url ) );
self::sync( $contact, sprintf( 'RAS Newspack Network: User propagated from another site in the network. Propagated from %s to %s.', $registration_site, $site_url ) );
}
}
ESP_Connector::init_hooks();
Loading