Skip to content

Commit

Permalink
Merge pull request #140 from Automattic/alpha
Browse files Browse the repository at this point in the history
Release Oct 08
  • Loading branch information
dkoo authored Oct 8, 2024
2 parents c8070fa + 3f621b7 commit daa2295
Show file tree
Hide file tree
Showing 40 changed files with 2,300 additions and 611 deletions.
1 change: 1 addition & 0 deletions DEV_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Depending on what you want to do with the event, and where, implement one or mor

Examples:
* `canonical_url_updated` is triggered by the hub, and it has only the `process_in_node` method because the Hub will never receive it from a Node and won't do anything additional after it's triggered
* `order_changed`: is an event that the Nodes don't care about, so `process_in_node` is not present. And also, changes in Woo Orders need to be persisted in the central Woo dashboard, even for orders that belong to the Hub, so it uses `always_process_in_hub`.
* `user_updated`: is an event that can happen in any site and all other sites need to update their local users. In this case, you have the same thing happening for `process_in_node` and `post_process_in_hub`. It doesn't matter if it's coming from a Node to the Hub, from the Hub to a node, or from a Node to another Node. Every site will treat this event the same way.

4. Optional. Create a `event-log-item` specific class
Expand Down
9 changes: 9 additions & 0 deletions TESTING_SCENARIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ See the troubleshooting section in the [developer docs](DEV_NOTES.md) to learn h

`reader_registered` is covered in the instructions above

### `newspack_node_order_changed` and `newspack_node_subscription_changed`:

* In one of the Nodes, make a Woocommerce purchase.
* Check that the event show up in the Hub's Event Log
* In the Hub, go to the Subscriptions or Order panels under the "Newspack Network" menu and confirm you see the Order or Subscription there, and that its links point to the Node
* Back to the Node admin, make a change to the Order or Subscription (for example changing its status)
* Confirm the change shows up in the Event Log
* Confirm that the Order or Subscription in the panel shows the updated status

### `donation_new` and `donation_subscription_cancelled`

* In one of the Nodes, make a one-time donation using the Donation block.
Expand Down
23 changes: 14 additions & 9 deletions includes/class-accepted-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ class Accepted_Actions {
* @var array Array where the keys are the supported events and the values are the Incoming Events class names
*/
const ACTIONS = [
'reader_registered' => 'Reader_Registered',
'canonical_url_updated' => 'Canonical_Url_Updated',
'donation_new' => 'Donation_New',
'donation_subscription_cancelled' => 'Donation_Subscription_Cancelled',
'network_user_updated' => 'User_Updated',
'network_user_deleted' => 'User_Deleted',
'newspack_network_woo_membership_updated' => 'Woocommerce_Membership_Updated',
'network_manual_sync_user' => 'User_Manually_Synced',
'network_nodes_synced' => 'Nodes_Synced',
'reader_registered' => 'Reader_Registered',
'newspack_node_order_changed' => 'Order_Changed',
'newspack_node_subscription_changed' => 'Subscription_Changed',
'canonical_url_updated' => 'Canonical_Url_Updated',
'donation_new' => 'Donation_New',
'donation_subscription_cancelled' => 'Donation_Subscription_Cancelled',
'network_user_updated' => 'User_Updated',
'network_user_deleted' => 'User_Deleted',
'newspack_network_woo_membership_updated' => 'Woocommerce_Membership_Updated',
'network_manual_sync_user' => 'User_Manually_Synced',
'network_nodes_synced' => 'Nodes_Synced',
'newspack_network_membership_plan_updated' => 'Membership_Plan_Updated',
];

/**
Expand All @@ -56,5 +59,7 @@ class Accepted_Actions {
'newspack_network_woo_membership_updated',
'network_manual_sync_user',
'network_nodes_synced',
'newspack_node_subscription_changed',
'newspack_network_membership_plan_updated',
];
}
19 changes: 9 additions & 10 deletions includes/class-esp-metadata-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class Esp_Metadata_Sync {
*/
public static function init() {
\add_filter( 'newspack_ras_metadata_keys', [ __CLASS__, 'add_custom_metadata_fields' ] );
\add_filter( 'newspack_register_reader_metadata', [ __CLASS__, 'handle_custom_metadata_fields' ], 10, 2 );
\add_filter( 'newspack_data_events_reader_registered_metadata', [ __CLASS__, 'handle_custom_metadata_fields' ], 10, 2 );
\add_filter( 'newspack_esp_sync_contact', [ __CLASS__, 'handle_esp_sync_contact' ], 10, 2 );
\add_action( 'init', [ __CLASS__, 'register_listeners' ] );
}

Expand Down Expand Up @@ -56,19 +55,19 @@ public static function add_custom_metadata_fields( $metadata_fields ) {
}

/**
* Add handling for custom metadata fields. Only fire for newly created users.
* Add handling for custom metadata fields when syncing to ESP.
*
* @param array $metadata The contact metadata data.
* @param int|false $user_id Created user ID, or false if the user already exists.
* @param array $contact The contact metadata data.
*
* @return array The updated contact data.
*/
public static function handle_custom_metadata_fields( $metadata, $user_id ) {
if ( $user_id ) {
$metadata['network_registration_site'] = self::get_registration_site_meta( $user_id );
public static function handle_esp_sync_contact( $contact ) {
$user = get_user_by( 'email', $contact['email'] );
if ( ! $user ) {
return $contact;
}

return $metadata;
$contact['metadata']['network_registration_site'] = self::get_registration_site_meta( $user->ID );
return $contact;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion includes/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static function init() {
Hub\Pull_Endpoint::init();
Hub\Network_Data_Endpoint::init();
Hub\Event_Listeners::init();
Hub\Database\Subscriptions::init();
Hub\Database\Orders::init();
Hub\Newspack_Ads_GAM::init();
Hub\Connect_Node::init();
}
Expand Down Expand Up @@ -56,8 +58,9 @@ public static function init() {

Woocommerce_Memberships\Admin::init();
Woocommerce_Memberships\Events::init();

Woocommerce\Events::init();
Woocommerce_Memberships\Subscriptions_Integration::init();
Woocommerce_Subscriptions\Admin::init();

register_activation_hook( NEWSPACK_NETWORK_PLUGIN_FILE, [ __CLASS__, 'activation_hook' ] );
}
Expand Down
7 changes: 6 additions & 1 deletion includes/class-rest-authenticaton.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class Rest_Authenticaton {
* The callback is a function that will be called if a signed request to this endpoints is successfully verified.
*/
const ENDPOINTS = [
'get-woo-orders' => [
'endpoint' => '|^/wc/v3/orders/[0-9]+$|',
'callback' => [ __CLASS__, 'add_filter_for_woo_read_endpoints' ],
],
'get-woo-subscriptions' => [
'endpoint' => '|^/wc/v3/subscriptions|',
'endpoint' => '|^/wc/v3/subscriptions/[0-9]+$|',
'callback' => [ __CLASS__, 'add_filter_for_woo_read_endpoints' ],
],
'get-woo-membership-plans' => [
Expand Down Expand Up @@ -137,6 +141,7 @@ public static function rest_pre_dispatch( $response, $handler, $request ) {

foreach ( self::ENDPOINTS as $endpoint_id => $endpoint ) {
if ( preg_match( $endpoint['endpoint'], $request->get_route() ) ) {

Debugger::log( 'Route matched: ' . $request->get_route() );

$verified = self::verify_signature( $request, $endpoint_id, $secret_key );
Expand Down
75 changes: 75 additions & 0 deletions includes/cli/backfillers/class-membership-plan-updated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Data Backfiller for membership_plan_updated events.
*
* @package Newspack
*/

namespace Newspack_Network\Backfillers;

use Newspack_Network\Data_Backfill;
use Newspack_Network\Woocommerce_Memberships\Admin as Memberships_Admin;
use Newspack_Network\Woocommerce_Memberships\Events as Memberships_Events;
use WP_Cli;
use WC_Memberships_Membership_Plan;

/**
* Backfiller class.
*/
class Membership_Plan_Updated extends Abstract_Backfiller {

/**
* Gets the output line about the processed item being processed in verbose mode.
*
* @param \Newspack_Network\Incoming_Events\Abstract_Incoming_Event $event The event.
*
* @return string
*/
protected function get_processed_item_output( $event ) {
return sprintf( 'Membership Plan #%d', $event->get_id() );
}

/**
* Gets the events to be processed
*
* @return \Newspack_Network\Incoming_Events\Abstract_Incoming_Event[] $events An array of events.
*/
public function get_events() {

if ( ! class_exists( 'WC_Memberships_Membership_Plan' ) ) {
return [];
}

// Get all memberships created or updated between $start and $end.
$membership_plans = get_posts(
[
'post_type' => Memberships_Admin::MEMBERSHIP_PLANS_CPT,
'post_status' => 'any',
'numberposts' => -1,
'date_query' => [
'column' => 'post_modified_gmt',
'after' => $this->start,
'before' => $this->end,
'inclusive' => true,
],
]
);

$this->maybe_initialize_progress_bar( 'Processing membership plans', count( $membership_plans ) );

$events = [];
WP_CLI::line( '' );
WP_CLI::line( sprintf( 'Found %s membership plan(s) eligible for sync.', count( $membership_plans ) ) );
WP_CLI::line( '' );

foreach ( $membership_plans as $plan ) {
$membership_data = Memberships_Events::membership_plan_updated( $plan->ID );

$timestamp = strtotime( $plan->post_modified_gmt );

$events[] = new \Newspack_Network\Incoming_Events\Membership_Plan_Updated( get_bloginfo( 'url' ), $membership_data, $timestamp );
}

return $events;
}
}
67 changes: 67 additions & 0 deletions includes/cli/backfillers/class-order-changed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Data Backfiller for order_changed events.
*
* @package Newspack
*/

namespace Newspack_Network\Backfillers;

use Newspack_Network\Woocommerce\Events as Woo_Listeners;

/**
* Backfiller class.
*/
class Order_Changed extends Abstract_Backfiller {

/**
* Gets the output line about the processed item being processed in verbose mode.
*
* @param \Newspack_Network\Incoming_Events\Abstract_Incoming_Event $event The event.
*
* @return string
*/
protected function get_processed_item_output( $event ) {
return sprintf( 'Order #%d with status %s.', $event->get_id(), $event->get_status_after() );
}

/**
* Gets the events to be processed
*
* @return \Newspack_Network\Incoming_Events\Abstract_Incoming_Event[] $events An array of events.
*/
public function get_events() {
$params = [
'limit' => -1,
];

if ( $this->start || $this->end ) {
if ( ! $this->end ) {
$params['date_created'] = '>=' . $this->start;
} elseif ( ! $this->start ) {
$params['date_created'] = '<=' . $this->end;
} else {
$params['date_created'] = $this->start . '...' . $this->end;
}
}

$orders = wc_get_orders( $params );

$this->maybe_initialize_progress_bar( 'Processing orders', count( $orders ) );

$events = [];

foreach ( $orders as $order ) {

$order_data = Woo_Listeners::item_changed( $order->get_id(), '', $order->get_status(), $order );

$timestamp = strtotime( $order->get_date_created() );

$event = new \Newspack_Network\Incoming_Events\Order_Changed( get_bloginfo( 'url' ), $order_data, $timestamp );

$events[] = $event;
}

return $events;
}
}
78 changes: 78 additions & 0 deletions includes/cli/backfillers/class-subscription-changed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Data Backfiller for subscription_changed events.
*
* @package Newspack
*/

namespace Newspack_Network\Backfillers;

use Newspack_Network\Woocommerce\Events as Woo_Listeners;

/**
* Backfiller class.
*/
class Subscription_Changed extends Abstract_Backfiller {

/**
* Gets the output line about the processed item being processed in verbose mode.
*
* @param \Newspack_Network\Incoming_Events\Abstract_Incoming_Event $event The event.
*
* @return string
*/
protected function get_processed_item_output( $event ) {
return sprintf( 'Subscription #%d with status %s.', $event->get_id(), $event->get_status_after() );
}

/**
* Gets the events to be processed
*
* @return \Newspack_Network\Incoming_Events\Abstract_Incoming_Event[] $events An array of events.
*/
public function get_events() {
$params = [
'subscription_status' => 'any',
'subscriptions_per_page' => -1,
];

if ( $this->start || $this->end ) {
$params['meta_query'] = [ 'relation' => 'AND' ]; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query

if ( $this->start ) {
$params['meta_query'][] = [
'key' => wcs_get_date_meta_key( 'start' ),
'compare' => '>=',
'value' => $this->start,
];
}

if ( $this->end ) {
$params['meta_query'][] = [
'key' => wcs_get_date_meta_key( 'start' ),
'compare' => '<=',
'value' => $this->end,
];
}
}

$subscriptions = wcs_get_subscriptions( $params );

$this->maybe_initialize_progress_bar( 'Processing subscriptions', count( $subscriptions ) );

$events = [];

foreach ( $subscriptions as $subscription ) {

$subscription_data = Woo_Listeners::subscription_changed( $subscription->get_id(), '', $subscription->get_status(), $subscription );

$timestamp = strtotime( $subscription->get_date_created() );

$event = new \Newspack_Network\Incoming_Events\Subscription_Changed( get_bloginfo( 'url' ), $subscription_data, $timestamp );

$events[] = $event;
}

return $events;
}
}
2 changes: 1 addition & 1 deletion includes/hub/admin/class-event-log-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function extra_tablenav( $which ) {
</select>
<?php endif; ?>

<?php Nodes::network_sites_dropdown( $current_node ); ?>
<?php Nodes::nodes_dropdown( $current_node ); ?>

<input type="submit" name="filter_action" class="button" value="<?php esc_attr_e( 'Filter', 'newspack-network' ); ?>">

Expand Down
Loading

0 comments on commit daa2295

Please sign in to comment.