1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @since 1.2 |
5
|
|
|
*/ |
6
|
|
|
class GravityView_Change_Entry_Creator { |
7
|
|
|
|
8
|
1 |
|
/* |
9
|
|
|
* @var int Number of users to show in the select element |
10
|
|
|
*/ |
11
|
|
|
const DEFAULT_NUMBER_OF_USERS = 100; |
12
|
|
|
|
13
|
1 |
|
function __construct() { |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
1 |
|
* @since 1.5.1 |
17
|
|
|
*/ |
18
|
|
|
add_action( 'gform_user_registered', array( $this, 'assign_new_user_to_lead' ), 10, 4 ); |
19
|
|
|
|
20
|
|
|
// ONLY ADMIN FROM HERE ON. |
21
|
|
|
if ( ! is_admin() ) { |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @filter `gravityview_disable_change_entry_creator` Disable the Change Entry Creator functionality |
27
|
|
|
* @since 1.7.4 |
28
|
|
|
* @param boolean $disable Disable the Change Entry Creator functionality. Default: false. |
29
|
|
|
*/ |
30
|
|
|
if ( apply_filters( 'gravityview_disable_change_entry_creator', false ) ) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Use `init` to fix bbPress warning |
36
|
|
|
* |
37
|
|
|
* @see https://bbpress.trac.wordpress.org/ticket/2309 |
38
|
|
|
*/ |
39
|
|
|
add_action( 'init', array( $this, 'load' ), 100 ); |
40
|
|
|
|
41
|
|
|
add_action( 'plugins_loaded', array( $this, 'prevent_conflicts' ) ); |
42
|
|
|
|
43
|
|
|
// Enqueues SelectWoo script and style. |
44
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_selectwoo_assets' ) ); |
45
|
|
|
|
46
|
|
|
// Ajax callback to get users to change entry creator. |
47
|
|
|
add_action( 'wp_ajax_entry_creator_get_users', array( $this, 'entry_creator_get_users' ) ); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Enqueue selectWoo script and style. |
53
|
|
|
* |
54
|
|
|
* @since 2.9.1 |
55
|
|
|
*/ |
56
|
|
|
function enqueue_selectwoo_assets() { |
|
|
|
|
57
|
|
|
|
58
|
|
|
$version = GravityView_Plugin::version; |
|
|
|
|
59
|
|
|
$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
60
|
|
|
|
61
|
|
|
wp_enqueue_script( 'gravityview-selectWoo', plugins_url( 'assets/lib/selectWoo/selectWoo.full.min.js', GRAVITYVIEW_FILE ), array(), $version ); |
62
|
|
|
wp_enqueue_style( 'gravityview-selectWoo', plugins_url( 'assets/lib/selectWoo/selectWoo.min.css', GRAVITYVIEW_FILE ), array(), $version ); |
63
|
|
|
|
64
|
|
|
wp_enqueue_script( 'gravityview_entry_creator', plugins_url( 'assets/js/admin-entry-creator' . $script_debug . '.js', GRAVITYVIEW_FILE ), array( 'gravityview-selectWoo' ), $version ); |
65
|
|
|
|
66
|
|
|
wp_localize_script( |
67
|
|
|
'gravityview_entry_creator', |
68
|
|
|
'GVEntryCreator', |
69
|
|
|
array( |
70
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
71
|
|
|
'action' => 'entry_creator_get_users', |
72
|
|
|
'language' => array( |
73
|
|
|
'search_placeholder' => esc_html__( 'Search for username or other user attribute', 'gravityview' ), |
74
|
|
|
), |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get users list for entry creator. |
81
|
|
|
* |
82
|
|
|
* @since 2.9.1 |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
|
function entry_creator_get_users() { |
|
|
|
|
86
|
|
|
|
87
|
|
|
$post_var = wp_parse_args( |
88
|
|
|
wp_unslash( $_POST ), |
89
|
|
|
array( |
90
|
|
|
'q' => '', |
91
|
|
|
'gv_nonce' => '', |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
if ( ! wp_verify_nonce( $post_var['gv_nonce'], 'gv_entry_creator' ) ) { |
96
|
|
|
die(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$search_string = $post_var['q']; |
100
|
|
|
|
101
|
|
|
if ( is_numeric( $search_string ) ) { |
102
|
|
|
$user_args = array( |
103
|
|
|
'search' => $search_string . '*', |
104
|
|
|
'search_columns' => array( 'ID' ), |
105
|
|
|
); |
106
|
|
|
} else { |
107
|
|
|
$user_args = array( |
108
|
|
|
'search' => '*' . $search_string . '*', |
109
|
|
|
'search_columns' => array( 'user_login', 'user_email', 'user_nicename', 'display_name' ), |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$users = GVCommon::get_users( 'change_entry_creator', $user_args ); |
114
|
|
|
|
115
|
|
|
echo wp_send_json( $users, 200 ); |
116
|
|
|
|
117
|
|
|
die(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* When an user is created using the User Registration add-on, assign the entry to them |
122
|
|
|
* |
123
|
|
|
* @since 1.5.1 |
124
|
|
|
* @param int $user_id WordPress User ID |
125
|
|
|
* @param array $config User registration feed configuration |
126
|
|
|
* @param array $entry GF Entry array |
127
|
|
|
* @param string $password User password |
128
|
|
|
* @return void |
129
|
|
|
* @uses RGFormsModel::update_lead_property() Modify the entry `created_by` field |
130
|
|
|
*/ |
131
|
|
|
function assign_new_user_to_lead( $user_id, $config, $entry = array(), $password = '' ) { |
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Disable assigning the new user to the entry by returning false. |
135
|
|
|
* |
136
|
|
|
* @param int $user_id WordPress User ID |
137
|
|
|
* @param array $config User registration feed configuration |
138
|
|
|
* @param array $entry GF Entry array |
139
|
|
|
*/ |
140
|
|
|
$assign_to_lead = apply_filters( 'gravityview_assign_new_user_to_entry', true, $user_id, $config, $entry ); |
141
|
|
|
|
142
|
|
|
// If filter returns false, do not process |
143
|
|
|
if ( empty( $assign_to_lead ) ) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Update the entry. The `false` prevents checking Akismet; `true` disables the user updated hook from firing |
148
|
|
|
$result = RGFormsModel::update_entry_property( (int) $entry['id'], 'created_by', (int) $user_id, false, true ); |
149
|
|
|
|
150
|
|
|
if ( false === $result ) { |
151
|
|
|
$status = __( 'Error', 'gravityview' ); |
152
|
|
|
global $wpdb; |
153
|
|
|
$note = sprintf( '%s: Failed to assign User ID #%d as the entry creator (Last database error: "%s")', $status, $user_id, $wpdb->last_error ); |
154
|
|
|
} else { |
155
|
|
|
$status = __( 'Success', 'gravityview' ); |
156
|
|
|
$note = sprintf( _x( '%s: Assigned User ID #%d as the entry creator.', 'First parameter: Success or error of the action. Second: User ID number', 'gravityview' ), $status, $user_id ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
gravityview()->log->debug( 'GravityView_Change_Entry_Creator[assign_new_user_to_lead] - {note}', array( 'note' => $note ) ); |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @filter `gravityview_disable_change_entry_creator_note` Disable adding a note when changing the entry creator |
163
|
|
|
* @since 1.21.5 |
164
|
|
|
* @param boolean $disable Disable the Change Entry Creator note. Default: false. |
165
|
|
|
*/ |
166
|
|
|
if ( apply_filters( 'gravityview_disable_change_entry_creator_note', false ) ) { |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
GravityView_Entry_Notes::add_note( $entry['id'], - 1, 'GravityView', $note, 'gravityview' ); |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Disable previous functionality; use this one as the canonical. |
176
|
|
|
* |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
function prevent_conflicts() { |
|
|
|
|
180
|
|
|
|
181
|
|
|
// Plugin that was provided here: |
182
|
|
|
// @link https://gravityview.co/support/documentation/201991205/ |
183
|
|
|
remove_action( "gform_entry_info", 'gravityview_change_entry_creator_form', 10 ); |
184
|
|
|
remove_action( "gform_after_update_entry", 'gravityview_update_entry_creator', 10 ); |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @since 3.6.3 |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
function load() { |
|
|
|
|
193
|
|
|
|
194
|
|
|
// Does GF exist? |
195
|
|
|
if ( ! class_exists( 'GFCommon' ) ) { |
196
|
|
|
return; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Can the user edit entries? |
200
|
|
|
if ( ! GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_entries' ) ) ) { |
201
|
|
|
return; |
202
|
1 |
|
} |
203
|
|
|
|
204
|
1 |
|
// If screen mode isn't set, then we're in the wrong place. |
205
|
|
|
if ( empty( $_REQUEST['screen_mode'] ) ) { |
206
|
|
|
return; |
207
|
|
|
} |
208
|
1 |
|
|
209
|
|
|
// Now, no validation is required in the methods; let's hook in. |
210
|
1 |
|
add_action( 'admin_init', array( &$this, 'set_screen_mode' ) ); |
211
|
|
|
|
212
|
1 |
|
add_action( "gform_entry_info", array( &$this, 'add_select' ), 10, 2 ); |
213
|
|
|
|
214
|
|
|
add_action( "gform_after_update_entry", array( &$this, 'update_entry_creator' ), 10, 2 ); |
215
|
1 |
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Allows for edit links to work with a link instead of a form (GET instead of POST) |
220
|
|
|
* |
221
|
|
|
* @return void |
222
|
1 |
|
*/ |
223
|
1 |
|
function set_screen_mode() { |
|
|
|
|
224
|
1 |
|
|
225
|
|
|
if ( 'view' === \GV\Utils::_POST( 'screen_mode' ) ) { |
226
|
|
|
return; |
227
|
|
|
} |
228
|
1 |
|
|
229
|
1 |
|
// If $_GET['screen_mode'] is set to edit, set $_POST value |
230
|
|
|
if ( \GV\Utils::_GET( 'screen_mode' ) === 'edit' ) { |
231
|
|
|
$_POST["screen_mode"] = 'edit'; |
232
|
|
|
} |
233
|
1 |
|
|
234
|
1 |
|
} |
235
|
1 |
|
|
236
|
1 |
|
/** |
237
|
|
|
* When the entry creator is changed, add a note to the entry |
238
|
1 |
|
* |
239
|
1 |
|
* @param array $form GF entry array |
240
|
|
|
* @param int $entry_id Entry ID |
241
|
1 |
|
* @return void |
242
|
|
|
*/ |
243
|
1 |
|
function update_entry_creator( $form, $entry_id ) { |
|
|
|
|
244
|
1 |
|
|
245
|
|
|
global $current_user; |
246
|
|
|
|
247
|
|
|
// Update the entry |
248
|
|
|
$created_by = absint( \GV\Utils::_POST( 'created_by' ) ); |
249
|
|
|
|
250
|
|
|
RGFormsModel::update_lead_property( $entry_id, 'created_by', $created_by ); |
251
|
|
|
|
252
|
|
|
// If the creator has changed, let's add a note about who it used to be. |
253
|
|
|
$originally_created_by = \GV\Utils::_POST( 'originally_created_by' ); |
254
|
|
|
|
255
|
|
|
// If there's no owner and there didn't used to be, keep going |
256
|
|
|
if ( empty( $originally_created_by ) && empty( $created_by ) ) { |
257
|
|
|
return; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// If the values have changed |
261
|
|
|
if ( absint( $originally_created_by ) !== absint( $created_by ) ) { |
262
|
|
|
|
263
|
|
|
$user_data = get_userdata( $current_user->ID ); |
264
|
|
|
|
265
|
|
|
$user_format = _x( '%s (ID #%d)', 'The name and the ID of users who initiated changes to entry ownership', 'gravityview' ); |
266
|
|
|
|
267
|
|
|
$original_name = $created_by_name = esc_attr_x( 'No User', 'To show that the entry was unassigned from an actual user to no user.', 'gravityview' ); |
268
|
|
|
|
269
|
|
|
if ( ! empty( $originally_created_by ) ) { |
270
|
|
|
$originally_created_by_user_data = get_userdata( $originally_created_by ); |
271
|
|
|
$original_name = sprintf( $user_format, $originally_created_by_user_data->display_name, $originally_created_by_user_data->ID ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ( ! empty( $created_by ) ) { |
275
|
|
|
$created_by_user_data = get_userdata( $created_by ); |
276
|
|
|
$created_by_name = sprintf( $user_format, $created_by_user_data->display_name, $created_by_user_data->ID ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
GravityView_Entry_Notes::add_note( $entry_id, $current_user->ID, $user_data->display_name, sprintf( __( 'Changed entry creator from %s to %s', 'gravityview' ), $original_name, $created_by_name ), 'note' ); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Output select element used to change the entry creator |
286
|
|
|
* |
287
|
|
|
* @param int $form_id GF Form ID |
288
|
|
|
* @param array $entry GF entry array |
289
|
|
|
* |
290
|
|
|
* @return void |
291
|
|
|
*/ |
292
|
|
|
function add_select( $form_id, $entry ) { |
|
|
|
|
293
|
|
|
|
294
|
|
|
if ( \GV\Utils::_POST( 'screen_mode' ) !== 'edit' ) { |
295
|
|
|
return; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$output = '<label for="change_created_by">'; |
299
|
|
|
$output .= esc_html__( 'Change Entry Creator:', 'gravityview' ); |
300
|
|
|
$output .= '</label>'; |
301
|
|
|
$output .= '<select name="created_by" id="change_created_by" class="widefat">'; |
302
|
|
|
|
303
|
|
|
$entry_creator_user_id = \GV\Utils::get( $entry, 'created_by' ); |
304
|
|
|
|
305
|
|
|
$entry_creator_user = GVCommon::get_users( 'change_entry_creator', array( 'include' => $entry_creator_user_id ) ); |
306
|
|
|
$entry_creator_user = isset( $entry_creator_user[0] ) ? $entry_creator_user[0] : array(); |
307
|
|
|
|
308
|
|
|
if ( empty( $entry_creator_user ) ) { |
309
|
|
|
$output .= '<option value="0"> — ' . esc_attr_x( 'No User', 'No user assigned to the entry', 'gravityview' ) . ' — </option>'; |
310
|
|
|
} else { |
311
|
|
|
$output .= '<option value="' . $entry_creator_user->ID . '" "selected">' . esc_attr( $entry_creator_user->display_name . ' (' . $entry_creator_user->user_nicename . ')' ) . '</option>'; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$all_users = GVCommon::get_users( 'change_entry_creator', array( 'number' => self::DEFAULT_NUMBER_OF_USERS ) ); |
315
|
|
|
foreach ( $all_users as $user ) { |
316
|
|
|
if ( $entry_creator_user_id === $user->ID ) { |
317
|
|
|
continue; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$output .= '<option value="' . $user->ID . '">' . esc_attr( $user->display_name . ' (' . $user->user_nicename . ')' ) . '</option>'; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$user_count = count_users(); |
324
|
|
|
|
325
|
|
|
if ( $user_count['total_users'] > self::DEFAULT_NUMBER_OF_USERS ) { |
326
|
|
|
$message = esc_html__( 'Use the search field to look through additional %d users.', '%d is replaced by user count', 'gravityview' ); |
327
|
|
|
$output .= '<option value="_disabled" disabled="disabled">' . sprintf( $message, $user_count['total_users'] - self::DEFAULT_NUMBER_OF_USERS ) . '</option>'; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$output .= '</select>'; |
331
|
|
|
$output .= '<input name="originally_created_by" value="' . esc_attr( $entry['created_by'] ) . '" type="hidden" />'; |
332
|
|
|
$output .= wp_nonce_field( 'gv_entry_creator', 'gv_entry_creator_nonce', false, false ); |
333
|
|
|
|
334
|
|
|
echo $output; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
new GravityView_Change_Entry_Creator; |
339
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.