-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass-data-listeners.php
122 lines (112 loc) · 3.96 KB
/
class-data-listeners.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Newspack Network Data Listeners.
*
* @package Newspack
*/
namespace Newspack_Network;
use Newspack\Data_Events;
/**
* Class to register additional listeners to the Newspack Data Events API
*/
class Data_Listeners {
/**
* Initialize this class and register hooks
*
* @return void
*/
public static function init() {
add_action( 'init', [ __CLASS__, 'register_listeners' ] );
}
/**
* Register the listeners to the Newspack Data Events API
*
* @return void
*/
public static function register_listeners() {
if ( ! class_exists( 'Newspack\Data_Events' ) ) {
return;
}
Data_Events::register_listener( 'woocommerce_subscription_status_changed', 'newspack_node_subscription_changed', [ __CLASS__, 'item_changed' ] );
Data_Events::register_listener( 'woocommerce_order_status_changed', 'newspack_node_order_changed', [ __CLASS__, 'item_changed' ] );
Data_Events::register_listener( 'newspack_network_user_updated', 'network_user_updated', [ __CLASS__, 'user_updated' ] );
Data_Events::register_listener( 'delete_user', 'network_user_deleted', [ __CLASS__, 'user_deleted' ] );
Data_Events::register_listener( 'newspack_network_nodes_synced', 'network_nodes_synced', [ __CLASS__, 'nodes_synced' ] );
}
/**
* Callback for the Data Events API listeners
*
* @param int $item_id The Subscription or Order ID.
* @param string $status_from The status before the change.
* @param string $status_to The status after the change.
* @param object $item The Subscription or Order object.
* @return array
*/
public static function item_changed( $item_id, $status_from, $status_to, $item ) {
$relationship = 'normal';
if ( function_exists( 'wcs_order_contains_subscription' ) ) {
if ( wcs_order_contains_subscription( $item_id, 'renewal' ) ) {
$relationship = 'renewal';
} elseif ( wcs_order_contains_subscription( $item_id, 'resubscribe' ) ) {
$relationship = 'resubscribe';
} elseif ( wcs_order_contains_subscription( $item_id, 'parent' ) ) {
$relationship = 'parent';
}
}
$result = [
'id' => $item_id,
'user_id' => $item->get_customer_id(),
'user_name' => '',
'email' => $item->get_billing_email(),
'status_before' => $status_from,
'status_after' => $status_to,
'formatted_total' => wp_strip_all_tags( $item->get_formatted_order_total() ),
'payment_count' => method_exists( $item, 'get_payment_count' ) ? $item->get_payment_count() : 1,
'subscription_relationship' => $relationship,
];
$user = $item->get_user();
if ( $user ) {
$result['user_name'] = $user->display_name;
}
return $result;
}
/**
* Filters the user data for the event being triggered
*
* @param array $user_data The user data.
* @return array
*/
public static function user_updated( $user_data ) {
return $user_data;
}
/**
* Filters the user data for the event being triggered
*
* @param int $id ID of the user to delete.
* @param int|null $reassign ID of the user to reassign posts and links to.
* Default null, for no reassignment.
* @param WP_User $user WP_User object of the user to delete.
* @return array
*/
public static function user_deleted( $id, $reassign, $user ) {
$should_delete = apply_filters( 'newspack_network_process_user_deleted', true, $user->user_email );
if ( ! $should_delete ) {
Debugger::log( 'User deletion with email: ' . $user->user_email . ' was skipped due to filter use.' );
return;
}
// Prevent deletion-related changes triggering a 'network_user_updated' event.
User_Update_Watcher::$enabled = false;
return [
'email' => $user->user_email,
];
}
/**
* Filters the nodes data for the event being triggered
*
* @param array $nodes_data The nodes data.
* @return array
*/
public static function nodes_synced( $nodes_data ) {
return [ 'nodes_data' => $nodes_data ];
}
}