1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file class-gravityview-field-unsubscribe.php |
4
|
|
|
* @since 2.5 |
5
|
|
|
* @package GravityView |
6
|
|
|
* @subpackage includes\fields |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class GravityView_Field_Unsubscribe extends GravityView_Field { |
10
|
|
|
|
11
|
|
|
var $name = 'unsubscribe'; |
12
|
|
|
|
13
|
|
|
var $group = 'gravityview'; |
14
|
|
|
|
15
|
|
|
var $is_searchable = false; |
16
|
|
|
|
17
|
|
|
var $contexts = array( 'single', 'multiple' ); |
18
|
|
|
|
19
|
|
|
public function __construct() { |
20
|
|
|
$this->label = esc_html__( 'Unsubscribe', 'gravityview' ); |
21
|
|
|
$this->description = esc_attr__( 'Unsubscribe from a Payment-based entry.', 'gravityview' ); |
22
|
|
|
|
23
|
|
|
$this->add_hooks(); |
24
|
|
|
|
25
|
|
|
parent::__construct(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Hooks called from constructor. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function add_hooks() { |
34
|
|
|
add_filter( 'gravityview_entry_default_fields', array( $this, 'filter_gravityview_entry_default_field' ), 10, 3 ); |
35
|
|
|
|
36
|
|
|
add_filter( 'gravityview/field/is_visible', array( $this, 'maybe_not_visible' ), 10, 2 ); |
37
|
|
|
|
38
|
|
|
add_filter( 'gravityview_field_entry_value_unsubscribe', array( $this, 'modify_entry_value_unsubscribe' ), 10, 4 ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Configure the field options. |
43
|
|
|
* |
44
|
|
|
* Called from the `gravityview_entry_default_fields` filter. |
45
|
|
|
* |
46
|
|
|
* Remove the logged in, new window and show as link options. |
47
|
|
|
* Add the allow unsubscribe for all admins option. |
48
|
|
|
* |
49
|
|
|
* @param array $field_options The options. |
50
|
|
|
* @param string $template_id The template ID. |
51
|
|
|
* @param int|string|float $field_id The field ID. |
52
|
|
|
* @param string $context The configuration context (edit, single, etc.) |
53
|
|
|
* @param string $input_type The input type. |
54
|
|
|
* @param int $form_id The form ID. |
55
|
|
|
* |
56
|
|
|
* @return array The field options. |
57
|
|
|
*/ |
58
|
|
|
public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { |
59
|
|
|
|
60
|
|
|
unset( $field_options['only_loggedin'] ); |
61
|
|
|
|
62
|
|
|
unset( $field_options['new_window'] ); |
63
|
|
|
|
64
|
|
|
unset( $field_options['show_as_link'] ); |
65
|
|
|
|
66
|
|
|
$add_options['unsub_all'] = array( |
|
|
|
|
67
|
|
|
'type' => 'checkbox', |
68
|
|
|
'label' => __( 'Allow admins to unsubscribe', 'gravityview' ), |
69
|
|
|
'desc' => __( 'Allow users with `gravityforms_edit_entries` to cancel subscriptions', 'gravityview' ), |
70
|
|
|
'value' => false, |
71
|
|
|
'merge_tags' => false, |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $field_options + $add_options; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Hide the field from the renderer. Perhaps. |
79
|
|
|
* |
80
|
|
|
* Called from `gravityview/field/is_visible` |
81
|
|
|
* |
82
|
|
|
* Hide the field for non-logged in users for sure. |
83
|
|
|
* |
84
|
|
|
* @param bool $visible Consider visible or not. |
85
|
|
|
* @param \GV\Field $field The field. |
86
|
|
|
* |
87
|
|
|
* @return bool Visible or not. |
88
|
|
|
*/ |
89
|
45 |
|
public function maybe_not_visible( $visible, $field ) { |
90
|
45 |
|
if ( $this->name !== $field->ID ) { |
91
|
44 |
|
return $visible; |
92
|
|
|
} |
93
|
1 |
|
return is_user_logged_in() ? $visible : false; |
94
|
|
|
} |
95
|
|
|
/** |
96
|
|
|
* Add the unsubsribe to the configuration fields. |
97
|
|
|
* |
98
|
|
|
* Only if a subscription feed is active for the current form. |
99
|
|
|
* |
100
|
|
|
* Called from `gravityview_entry_default_fields` |
101
|
|
|
* |
102
|
|
|
* @param array $entry_default_fields An array of available for configuration |
103
|
|
|
* @param array|int $form Form ID or array |
104
|
|
|
* @param string $context The configuration context (edit, single, etc.) |
105
|
|
|
* |
106
|
|
|
* @return array The array of available default fields. |
107
|
|
|
*/ |
108
|
|
|
public function filter_gravityview_entry_default_field( $entry_default_fields, $form, $context ) { |
109
|
|
|
|
110
|
|
|
if ( is_array( $form ) ) { |
111
|
|
|
return $entry_default_fields; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$feeds = GFAPI::get_feeds( null, $form ); |
115
|
|
|
|
116
|
|
|
if ( is_wp_error( $feeds ) ) { |
117
|
|
|
return $entry_default_fields; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
static $subscription_addons; |
121
|
|
|
|
122
|
|
|
if ( is_null( $subscription_addons ) ) { |
123
|
|
|
|
124
|
|
|
$registered = GFAddon::get_registered_addons(); |
125
|
|
|
|
126
|
|
|
foreach ( $registered as $addon ) { |
127
|
|
|
if ( method_exists( $addon, 'cancel_subscription' ) && is_callable( array( $addon, 'get_instance' ) ) ) { |
128
|
|
|
$addon = $addon::get_instance(); |
129
|
|
|
$subscription_addons[ $addon->get_slug() ] = $addon; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ( empty( $subscription_addons ) ) { |
135
|
|
|
return $entry_default_fields; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
foreach ( $feeds as $feed ) { |
139
|
|
|
if ( isset( $subscription_addons[ $feed['addon_slug'] ] ) && 'subscription' === \GV\Utils::get( $feed, 'meta/transactionType' ) ) { |
140
|
|
|
if ( ! isset( $entry_default_fields["{$this->name}"] ) && 'edit' !== $context ) { |
141
|
|
|
$entry_default_fields["{$this->name}"] = array( |
142
|
|
|
'label' => $this->label, |
143
|
|
|
'desc' => $this->description, |
144
|
|
|
'type' => $this->name, |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
break; // Feed found, field added |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $entry_default_fields; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Modify the render content. |
157
|
|
|
* |
158
|
|
|
* Called from `gravityview_field_entry_value_unsubscribe` |
159
|
|
|
* |
160
|
|
|
* @param string $output The output. |
161
|
|
|
* @param array $entry The entry. |
162
|
|
|
* @param array $field_settings The field settings. |
163
|
|
|
* @param \GV\Field $field The field. |
164
|
|
|
* |
165
|
|
|
* @return string The content. |
166
|
|
|
*/ |
167
|
2 |
|
public function modify_entry_value_unsubscribe( $output, $entry, $field_settings, $field ) { |
|
|
|
|
168
|
2 |
|
if ( ! is_user_logged_in() ) { |
169
|
1 |
|
return $output; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
$can_current_user_edit = is_numeric( $entry['created_by'] ) && ( wp_get_current_user()->ID === intval( $entry['created_by'] ) ); |
173
|
|
|
|
174
|
2 |
|
if ( ! $can_current_user_edit ) { |
175
|
1 |
|
if ( empty( $field_settings['unsub_all'] ) || ! \GVCommon::has_cap( 'gravityforms_edit_entries', $entry['id'] ) ) { |
176
|
1 |
|
return $output; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
if ( ! $status = \GV\Utils::get( $entry, 'payment_status' ) ) { |
181
|
1 |
|
return $output; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// @todo Move to init, or AJAXify, but make sure that the entry is in the View before allowing |
185
|
|
|
// @todo Also make sure we check caps if moved from here |
186
|
|
|
// @todo Also make sure test_GravityView_Field_Unsubscribe_unsubscribe_permissions is rewritten |
187
|
2 |
|
if ( $entry = $this->maybe_unsubscribe( $entry ) ) { |
188
|
2 |
|
if ( $entry['payment_status'] !== $status ) { |
189
|
|
|
// @todo Probably __( 'Unsubscribed', 'gravityview' ); |
190
|
1 |
|
return $entry['payment_status']; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
2 |
|
if ( 'active' !== mb_strtolower( $entry['payment_status'] ) ) { |
195
|
1 |
|
return $output; |
196
|
|
|
} |
197
|
|
|
|
198
|
2 |
|
global $wp; |
199
|
2 |
|
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) ); |
200
|
|
|
|
201
|
2 |
|
$link = add_query_arg( 'unsubscribe', wp_create_nonce( 'unsubscribe_' . $entry['id'] ), $current_url ); |
202
|
2 |
|
$link = add_query_arg( 'uid', $entry['id'], $link ); |
203
|
|
|
|
204
|
2 |
|
return sprintf( '<a href="%s">%s</a>', esc_url( $link ), esc_html__( 'Unsubscribe', 'gravityview' ) ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Try to unsubscribe from the entry. |
209
|
|
|
* |
210
|
|
|
* Called during a POST request. Checks nonce, feeds, entry ID. |
211
|
|
|
* Does not check user permissions. This is left as an exercise for the caller. |
212
|
|
|
* |
213
|
|
|
* Entry View inclusion is checked ad-hoc during the rendering of the field. |
214
|
|
|
* User permissions are also checked ad-hoc during the rendering process. |
215
|
|
|
* |
216
|
|
|
* @param array $entry The entry |
217
|
|
|
* |
218
|
|
|
* @return array $entry The entry |
219
|
|
|
*/ |
220
|
2 |
|
private function maybe_unsubscribe( $entry ) { |
221
|
|
|
|
222
|
2 |
|
if ( ! wp_verify_nonce( \GV\Utils::_REQUEST( 'unsubscribe' ), 'unsubscribe_' . $entry['id'] ) ) { |
223
|
2 |
|
return $entry; |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
if ( ( ! $uid = \GV\Utils::_REQUEST( 'uid' ) ) || ! is_numeric( $uid ) || ( intval( $uid ) !== intval( $entry['id'] ) ) ) { |
227
|
|
|
return $entry; |
228
|
|
|
} |
229
|
|
|
|
230
|
1 |
|
if ( ! $feeds = gform_get_meta( $uid, 'processed_feeds' ) ) { |
231
|
|
|
return $entry; |
232
|
|
|
} |
233
|
|
|
|
234
|
1 |
|
static $subscription_addons; |
235
|
|
|
|
236
|
1 |
|
if ( is_null( $subscription_addons ) ) { |
237
|
|
|
|
238
|
1 |
|
$registered = GFAddon::get_registered_addons(); |
239
|
|
|
|
240
|
1 |
|
foreach ( $registered as $addon ) { |
241
|
1 |
|
if ( method_exists( $addon, 'cancel_subscription' ) ) { |
242
|
1 |
|
$addon = $addon::get_instance(); |
243
|
1 |
|
$subscription_addons[ $addon->get_slug() ] = $addon; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
if ( empty( $subscription_addons ) ) { |
249
|
|
|
return $entry; |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
foreach ( $feeds as $slug => $feed_ids ) { |
253
|
|
|
|
254
|
1 |
|
if ( ! isset( $subscription_addons[ $slug ] ) ) { |
255
|
|
|
continue; |
256
|
|
|
} |
257
|
|
|
|
258
|
1 |
|
foreach ( $feed_ids as $feed_id ) { |
259
|
|
|
|
260
|
1 |
|
$feed = $subscription_addons[ $slug ]->get_feed( $feed_id ); |
261
|
|
|
|
262
|
1 |
|
if ( $feed && 'subscription' === \GV\Utils::get( $feed, 'meta/transactionType' ) ) { |
263
|
|
|
|
264
|
1 |
|
if ( $subscription_addons[ $slug ]->cancel( $entry, $feed ) ) { |
265
|
|
|
|
266
|
1 |
|
$subscription_addons[ $slug ]->cancel_subscription( $entry, $feed ); |
267
|
|
|
|
268
|
1 |
|
return \GFAPI::get_entry( $entry['id'] ); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $entry; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
new GravityView_Field_Unsubscribe; |
279
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.