|
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
|
|
|
public function maybe_not_visible( $visible, $field ) { |
|
|
|
|
|
|
90
|
|
|
return is_user_logged_in() ? $visible : false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Add the unsubsribe to the configuration fields. |
|
95
|
|
|
* |
|
96
|
|
|
* Only if a subscription feed is active for the current form. |
|
97
|
|
|
* |
|
98
|
|
|
* Called from `gravityview_entry_default_fields` |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $entry_default_fields An array of available for configuration. |
|
101
|
|
|
* @param array $form The form. |
|
102
|
|
|
* @param string $context The configuration context (edit, single, etc.) |
|
103
|
|
|
* |
|
104
|
|
|
* @return array The array of available default fields. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function filter_gravityview_entry_default_field( $entry_default_fields, $form, $context ) { |
|
107
|
|
|
|
|
108
|
|
|
$feeds = GFAPI::get_feeds( null, $form ); |
|
109
|
|
|
|
|
110
|
|
|
if ( is_wp_error( $feeds ) ) { |
|
111
|
|
|
return $entry_default_fields; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
static $subscription_addons; |
|
115
|
|
|
|
|
116
|
|
|
if ( is_null( $subscription_addons ) ) { |
|
117
|
|
|
|
|
118
|
|
|
$registered = GFAddon::get_registered_addons(); |
|
119
|
|
|
|
|
120
|
|
|
foreach ( $registered as $addon ) { |
|
121
|
|
|
if ( method_exists( $addon, 'cancel_subscription' ) && is_callable( array( $addon, 'get_instance' ) ) ) { |
|
122
|
|
|
$addon = $addon::get_instance(); |
|
123
|
|
|
$subscription_addons[ $addon->get_slug() ] = $addon; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ( empty( $subscription_addons ) ) { |
|
129
|
|
|
return $entry_default_fields; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
foreach ( $feeds as $feed ) { |
|
133
|
|
|
if ( isset( $subscription_addons[ $feed['addon_slug'] ] ) && 'subscription' === \GV\Utils::get( $feed, 'meta/transactionType' ) ) { |
|
134
|
|
|
if ( ! isset( $entry_default_fields["{$this->name}"] ) && 'edit' !== $context ) { |
|
135
|
|
|
$entry_default_fields["{$this->name}"] = array( |
|
136
|
|
|
'label' => $this->label, |
|
137
|
|
|
'desc' => $this->description, |
|
138
|
|
|
'type' => $this->name, |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
break; // Feed found, field added |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $entry_default_fields; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Modify the render content. |
|
151
|
|
|
* |
|
152
|
|
|
* Called from `gravityview_field_entry_value_unsubscribe` |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $output The output. |
|
155
|
|
|
* @param array $entry The entry. |
|
156
|
|
|
* @param array $field_settings The field settings. |
|
157
|
|
|
* @param \GV\Field $field The field. |
|
158
|
|
|
* |
|
159
|
|
|
* @return string The content. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function modify_entry_value_unsubscribe( $output, $entry, $field_settings, $field ) { |
|
|
|
|
|
|
162
|
|
|
if ( ! is_user_logged_in() ) { |
|
163
|
|
|
return $output; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$can_current_user_edit = is_numeric( $entry['created_by'] ) && ( wp_get_current_user()->ID === intval( $entry['created_by'] ) ); |
|
167
|
|
|
|
|
168
|
|
|
if ( ! $can_current_user_edit ) { |
|
169
|
|
|
if ( empty( $field_settings['unsub_all'] ) || ! \GVCommon::has_cap( 'gravityforms_edit_entries', $entry['id'] ) ) { |
|
170
|
|
|
return $output; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ( ! $status = \GV\Utils::get( $entry, 'payment_status' ) ) { |
|
175
|
|
|
return $output; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// @todo Move to init, or AJAXify, but make sure that the entry is in the View before allowing |
|
179
|
|
|
// @todo Also make sure we check caps if moved from here |
|
180
|
|
|
// @todo Also make sure test_GravityView_Field_Unsubscribe_unsubscribe_permissions is rewritten |
|
181
|
|
|
if ( $entry = $this->maybe_unsubscribe( $entry ) ) { |
|
182
|
|
|
if ( $entry['payment_status'] !== $status ) { |
|
183
|
|
|
// @todo Probably __( 'Unsubscribed', 'gravityview' ); |
|
184
|
|
|
return $entry['payment_status']; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if ( 'active' !== mb_strtolower( $entry['payment_status'] ) ) { |
|
189
|
|
|
return $output; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
global $wp; |
|
193
|
|
|
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) ); |
|
194
|
|
|
|
|
195
|
|
|
$link = add_query_arg( 'unsubscribe', wp_create_nonce( 'unsubscribe_' . $entry['id'] ), $current_url ); |
|
196
|
|
|
$link = add_query_arg( 'uid', $entry['id'], $link ); |
|
197
|
|
|
|
|
198
|
|
|
return sprintf( '<a href="%s">%s</a>', esc_url( $link ), esc_html__( 'Unsubscribe', 'gravityview' ) ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Try to unsubscribe from the entry. |
|
203
|
|
|
* |
|
204
|
|
|
* Called during a POST request. Checks nonce, feeds, entry ID. |
|
205
|
|
|
* Does not check user permissions. This is left as an exercise for the caller. |
|
206
|
|
|
* |
|
207
|
|
|
* Entry View inclusion is checked ad-hoc during the rendering of the field. |
|
208
|
|
|
* User permissions are also checked ad-hoc during the rendering process. |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $entry The entry |
|
211
|
|
|
* |
|
212
|
|
|
* @return array $entry The entry |
|
213
|
|
|
*/ |
|
214
|
|
|
private function maybe_unsubscribe( $entry ) { |
|
215
|
|
|
|
|
216
|
|
|
if ( ! wp_verify_nonce( \GV\Utils::_REQUEST( 'unsubscribe' ), 'unsubscribe_' . $entry['id'] ) ) { |
|
217
|
|
|
return $entry; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if ( ( ! $uid = \GV\Utils::_REQUEST( 'uid' ) ) || ! is_numeric( $uid ) || ( intval( $uid ) !== intval( $entry['id'] ) ) ) { |
|
221
|
|
|
return $entry; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
if ( ! $feeds = gform_get_meta( $uid, 'processed_feeds' ) ) { |
|
225
|
|
|
return $entry; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
static $subscription_addons; |
|
229
|
|
|
|
|
230
|
|
|
if ( is_null( $subscription_addons ) ) { |
|
231
|
|
|
|
|
232
|
|
|
$registered = GFAddon::get_registered_addons(); |
|
233
|
|
|
|
|
234
|
|
|
foreach ( $registered as $addon ) { |
|
235
|
|
|
if ( method_exists( $addon, 'cancel_subscription' ) ) { |
|
236
|
|
|
$addon = $addon::get_instance(); |
|
237
|
|
|
$subscription_addons[ $addon->get_slug() ] = $addon; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if ( empty( $subscription_addons ) ) { |
|
243
|
|
|
return $entry; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
foreach ( $feeds as $slug => $feed_ids ) { |
|
247
|
|
|
|
|
248
|
|
|
if ( ! isset( $subscription_addons[ $slug ] ) ) { |
|
249
|
|
|
continue; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
foreach ( $feed_ids as $feed_id ) { |
|
253
|
|
|
|
|
254
|
|
|
$feed = $subscription_addons[ $slug ]->get_feed( $feed_id ); |
|
255
|
|
|
|
|
256
|
|
|
if ( $feed && 'subscription' === \GV\Utils::get( $feed, 'meta/transactionType' ) ) { |
|
257
|
|
|
|
|
258
|
|
|
if ( $subscription_addons[ $slug ]->cancel( $entry, $feed ) ) { |
|
259
|
|
|
|
|
260
|
|
|
$subscription_addons[ $slug ]->cancel_subscription( $entry, $feed ); |
|
261
|
|
|
|
|
262
|
|
|
return \GFAPI::get_entry( $entry['id'] ); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $entry; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
new GravityView_Field_Unsubscribe; |
|
273
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.