Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ 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/class-woo-user-registration.php';
Expand Down
107 changes: 1 addition & 106 deletions includes/data-events/connectors/class-activecampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use Newspack\Data_Events;
use Newspack\Newspack_Newsletters;
use Newspack\Reader_Activation;
use Newspack\WooCommerce_Connection;
use Newspack\Donations;

defined( 'ABSPATH' ) || exit;

/**
* Main Class.
*/
class ActiveCampaign {
class ActiveCampaign extends Connector {
/**
* Constructor.
*/
Expand Down Expand Up @@ -61,109 +59,6 @@ public static function put( $contact ) {
return \Newspack_Newsletters_Subscription::add_contact( $contact, $master_list_id );
}

/**
* Handle a reader registering.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_registered( $timestamp, $data, $client_id ) {
$account_key = Newspack_Newsletters::get_metadata_key( 'account' );
$registration_date_key = Newspack_Newsletters::get_metadata_key( 'registration_date' );
$metadata = [
$account_key => $data['user_id'],
$registration_date_key => gmdate( Newspack_Newsletters::METADATA_DATE_FORMAT, $timestamp ),
];
if ( isset( $data['metadata']['current_page_url'] ) ) {
$metadata[ Newspack_Newsletters::get_metadata_key( 'registration_page' ) ] = $data['metadata']['current_page_url'];
}
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 = [
'email' => $data['email'],
'metadata' => $metadata,
];
self::put( $contact );
}

/**
* Sync reader data on login.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_logged_in( $timestamp, $data, $client_id ) {
if ( empty( $data['email'] ) || empty( $data['user_id'] ) ) {
return;
}

$customer = new \WC_Customer( $data['user_id'] );

// If user is not a Woo customer, don't need to sync them.
if ( ! $customer->get_order_count() ) {
return;
}

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

self::put( $contact );
}

/**
* Handle a completed order of any type.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function order_completed( $timestamp, $data, $client_id ) {
if ( ! isset( $data['platform_data']['order_id'] ) ) {
return;
}

$order_id = $data['platform_data']['order_id'];
$contact = WooCommerce_Connection::get_contact_from_order( $order_id, $data['referer'], true );

if ( ! $contact ) {
return;
}

self::put( $contact );
}

/**
* Handle a change in subscription status.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function subscription_updated( $timestamp, $data, $client_id ) {
if ( empty( $data['status_before'] ) || empty( $data['status_after'] ) || empty( $data['subscription_id'] ) ) {
return;
}

$contact = WooCommerce_Connection::get_contact_from_order( $data['subscription_id'] );

if ( ! $contact ) {
return;
}

self::put( $contact );
}

/**
* Handle newsletter subscription update.
*
Expand Down
122 changes: 122 additions & 0 deletions includes/data-events/connectors/class-connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Abstract Newspack Data Events Connector class
*
* @package Newspack
*/

namespace Newspack\Data_Events\Connectors;

use Newspack\Newspack_Newsletters;
use Newspack\WooCommerce_Connection;

defined( 'ABSPATH' ) || exit;

/**
* Standard methods shared by all connectors.
*/
abstract class Connector {
/**
* Handle a reader registering.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_registered( $timestamp, $data, $client_id ) {
$account_key = Newspack_Newsletters::get_metadata_key( 'account' );
$registration_date_key = Newspack_Newsletters::get_metadata_key( 'registration_date' );
$metadata = [
$account_key => $data['user_id'],
$registration_date_key => gmdate( Newspack_Newsletters::METADATA_DATE_FORMAT, $timestamp ),
];
if ( isset( $data['metadata']['current_page_url'] ) ) {
$metadata[ Newspack_Newsletters::get_metadata_key( 'registration_page' ) ] = $data['metadata']['current_page_url'];
}
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 = [
'email' => $data['email'],
'metadata' => $metadata,
];

static::put( $contact );
}

/**
* Sync reader data on login.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_logged_in( $timestamp, $data, $client_id ) {
if ( empty( $data['email'] ) || empty( $data['user_id'] ) ) {
return;
}

$customer = new \WC_Customer( $data['user_id'] );

// If user is not a Woo customer, don't need to sync them.
if ( ! $customer->get_order_count() ) {
return;
}

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

static::put( $contact );
}

/**
* Handle a completed order of any type.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function order_completed( $timestamp, $data, $client_id ) {
if ( ! isset( $data['platform_data']['order_id'] ) ) {
return;
}

$order_id = $data['platform_data']['order_id'];
$contact = WooCommerce_Connection::get_contact_from_order( $order_id, $data['referer'], true );

if ( ! $contact ) {
return;
}

static::put( $contact );
}

/**
* Handle a change in subscription status.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function subscription_updated( $timestamp, $data, $client_id ) {
if ( empty( $data['status_before'] ) || empty( $data['status_after'] ) || empty( $data['subscription_id'] ) ) {
return;
}

$contact = WooCommerce_Connection::get_contact_from_order( $data['subscription_id'] );

if ( ! $contact ) {
return;
}

static::put( $contact );
}
}
110 changes: 2 additions & 108 deletions includes/data-events/connectors/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
use Newspack\Mailchimp_API;
use Newspack\Newspack_Newsletters;
use Newspack\Reader_Activation;
use Newspack\WooCommerce_Connection;
use Newspack\Donations;

defined( 'ABSPATH' ) || exit;

/**
* Main Class.
*/
class Mailchimp {
class Mailchimp extends Connector {
/**
* Constructor.
*/
Expand Down Expand Up @@ -57,7 +55,7 @@ private static function get_audience_id() {
$audience_id = Reader_Activation::get_setting( 'mailchimp_audience_id' );
/** Attempt to use list ID from "Mailchimp for WooCommerce" */
if ( ! $audience_id && function_exists( 'mailchimp_get_list_id' ) ) {
$audience_id = mailchimp_get_list_id();
$audience_id = \mailchimp_get_list_id();
}
return ! empty( $audience_id ) ? $audience_id : false;
}
Expand Down Expand Up @@ -231,109 +229,5 @@ public static function put( $contact ) {
$payload
);
}

/**
* Handle a reader registering.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_registered( $timestamp, $data, $client_id ) {
$account_key = Newspack_Newsletters::get_metadata_key( 'account' );
$registration_date_key = Newspack_Newsletters::get_metadata_key( 'registration_date' );
$metadata = [
$account_key => $data['user_id'],
$registration_date_key => gmdate( Newspack_Newsletters::METADATA_DATE_FORMAT, $timestamp ),
];
if ( isset( $data['metadata']['current_page_url'] ) ) {
$metadata[ Newspack_Newsletters::get_metadata_key( 'registration_page' ) ] = $data['metadata']['current_page_url'];
}
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 = [
'email' => $data['email'],
'metadata' => $metadata,
];
self::put( $contact );
}

/**
* Sync reader data on login.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function reader_logged_in( $timestamp, $data, $client_id ) {
if ( empty( $data['email'] ) || empty( $data['user_id'] ) ) {
return;
}

$customer = new \WC_Customer( $data['user_id'] );

// If user is not a Woo customer, don't need to sync them.
if ( ! $customer->get_order_count() ) {
return;
}

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

self::put( $contact );
}

/**
* Handle a completed order of any type.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function order_completed( $timestamp, $data, $client_id ) {
if ( ! isset( $data['platform_data']['order_id'] ) ) {
return;
}

$order_id = $data['platform_data']['order_id'];
$contact = WooCommerce_Connection::get_contact_from_order( $order_id, $data['referer'], true );

if ( ! $contact ) {
return;
}

self::put( $contact );
}

/**
* Handle a change in subscription status.
*
* @param int $timestamp Timestamp of the event.
* @param array $data Data associated with the event.
* @param int $client_id ID of the client that triggered the event.
*/
public static function subscription_updated( $timestamp, $data, $client_id ) {
if ( empty( $data['status_before'] ) || empty( $data['status_after'] ) || empty( $data['subscription_id'] ) ) {
return;
}

$contact = WooCommerce_Connection::get_contact_from_order( $data['subscription_id'] );

if ( ! $contact ) {
return;
}

self::put( $contact );
}
}
new Mailchimp();