Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4acd965
Revert "fix(memberships-sync): handle active subs from other nodes (#…
leogermani Jul 31, 2024
cfe823a
Revert "feat(admin): subscriptions view"
leogermani Jul 31, 2024
05116ae
Revert "feat: remove hub's subscriptions and orders"
leogermani Jul 31, 2024
a6db4a7
feat: remove api requests from woo item changed
leogermani Jul 31, 2024
8679b5d
feat: add backfillers for order and subscription_changed
leogermani Aug 1, 2024
f9d0ef6
feat: add user subscriptions metadata
leogermani Aug 1, 2024
9140a8e
fix: ensure assoc array when processing webhook data
leogermani Aug 6, 2024
372fafa
Merge branch 'epic/subscription-membership-sync' into add/subscriptio…
leogermani Aug 7, 2024
5091f8b
feat: add product id in the array keys
leogermani Aug 7, 2024
cca0976
feat: add membership plan updated event and backfiller
leogermani Aug 5, 2024
a06e3ff
feat: add product id to array jeys
leogermani Aug 7, 2024
dbc12f1
feat: add subscription products to woo central dashboard
leogermani Aug 23, 2024
2adaaea
fix: remove duplicate listeners
leogermani Aug 23, 2024
075ecdd
Merge branch 'add/subscriptions-backfillers' into add/membership-plan…
leogermani Aug 23, 2024
7b432ad
Merge branch 'trunk' into epic/subscription-membership-sync
leogermani Aug 23, 2024
2201958
Merge branch 'epic/subscription-membership-sync' into add/subscriptio…
leogermani Aug 23, 2024
43da0ac
Merge branch 'add/subscriptions-backfillers' into add/membership-plan…
leogermani Aug 23, 2024
4aff1f4
Merge pull request #123 from Automattic/add/subscriptions-backfillers
leogermani Aug 26, 2024
d78ce22
Merge pull request #124 from Automattic/add/membership-plan-updated
leogermani Aug 26, 2024
ca80f60
Revert "Revert "fix(memberships-sync): handle active subs from other …
leogermani Sep 4, 2024
9ad92a5
remove merge leftovers
leogermani Sep 4, 2024
519ad12
fix: fix constant name
leogermani Sep 4, 2024
acb61ff
Merge pull request #134 from Automattic/de-revert-subscription-handling
leogermani Sep 9, 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
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',
];
}
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