-
Notifications
You must be signed in to change notification settings - Fork 3
/
wm-userremoved.php
149 lines (128 loc) · 4.26 KB
/
wm-userremoved.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Uncanny_Automator;
/**
* Class WM_USERREMOVED
*
* @package Uncanny_Automator
*/
class WM_USERREMOVED {
/**
* Integration code
*
* @var string
*/
public static $integration = 'WISHLISTMEMBER';
/**
* @var string
*/
private $trigger_code;
/**
* @var string
*/
private $trigger_meta;
/**
* Set up Automator trigger constructor.
*/
public function __construct() {
$this->trigger_code = 'WMUSERREMOVEDFROM';
$this->trigger_meta = 'WMMEMBERSHIPLEVELS';
$this->define_trigger();
}
/**
* Define and register the trigger by pushing it into the Automator object
*/
public function define_trigger() {
$trigger = array(
'author' => Automator()->get_author_name( $this->trigger_code ),
'support_link' => Automator()->get_author_support_link( $this->trigger_code, 'integration/wishlist-member/' ),
'integration' => self::$integration,
'code' => $this->trigger_code,
/* translators: Logged-in trigger - Wishlist Member */
'sentence' => sprintf( esc_attr__( 'A user is removed from {{a membership level:%1$s}}', 'uncanny-automator' ), $this->trigger_meta ),
/* translators: Logged-in trigger - Wishlist Member */
'select_option_name' => esc_attr__( 'A user is removed from {{a membership level}}', 'uncanny-automator' ),
'action' => 'wishlistmember_remove_user_levels',
'priority' => 99,
'accepted_args' => 3,
'validation_function' => array(
$this,
'remove_user_to_membership_level',
),
'options_callback' => array( $this, 'load_options' ),
);
Automator()->register->trigger( $trigger );
}
/**
* @return array[]
*/
public function load_options() {
return Automator()->utilities->keep_order_of_options(
array(
'options' => array(
Automator()->helpers->recipe->wishlist_member->options->wm_get_all_membership_levels( null, $this->trigger_meta ),
),
)
);
}
/**
* @param $user_id
* @param $remove_levels
* @param $new_levels
*/
public function remove_user_to_membership_level( $user_id, $remove_levels, $new_levels ) {
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
if ( empty( $user_id ) ) {
return;
}
$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
$required_level = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
$matched_recipe_ids = array();
//Add where Membership Level is set for trigger
foreach ( $recipes as $recipe_id => $recipe ) {
foreach ( $recipe['triggers'] as $trigger ) {
$trigger_id = $trigger['ID'];//return early for all products
if ( intval( '-1' ) === intval( $required_level[ $recipe_id ][ $trigger_id ] ) || in_array( $required_level[ $recipe_id ][ $trigger_id ], $remove_levels ) ) { //phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$matched_recipe_ids[] = array(
'recipe_id' => $recipe_id,
'trigger_id' => $trigger_id,
);
}
}
}
if ( ! empty( $matched_recipe_ids ) ) {
foreach ( $matched_recipe_ids as $matched_recipe_id ) {
$pass_args = array(
'code' => $this->trigger_code,
'meta' => $this->trigger_meta,
'user_id' => $user_id,
'recipe_to_match' => $matched_recipe_id['recipe_id'],
'trigger_to_match' => $matched_recipe_id['trigger_id'],
'ignore_post_id' => true,
'is_signed_in' => true,
);
$args = Automator()->maybe_add_trigger_entry( $pass_args, false );
if ( $args ) {
foreach ( $args as $result ) {
if ( true === $result['result'] ) {
$trigger_meta = array(
'user_id' => $user_id,
'trigger_id' => $result['args']['trigger_id'],
'trigger_log_id' => $result['args']['get_trigger_id'],
'run_number' => $result['args']['run_number'],
);
foreach ( $remove_levels as $level ) {
$level_details = wlmapi_get_level( $level );
$trigger_meta['meta_key'] = $this->trigger_meta;
$trigger_meta['meta_value'] = maybe_serialize( $level_details['level']['name'] );
Automator()->insert_trigger_meta( $trigger_meta );
}
Automator()->maybe_trigger_complete( $result['args'] );
}
}
}
}
}
}
}