Completed
Pull Request — develop (#1523)
by Zack
19:41
created

enqueue_selectwoo_assets()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 0
loc 27
ccs 0
cts 12
cp 0
crap 30
rs 9.1768
c 0
b 0
f 0
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
		global $current_screen;
58
59
		if ( ! $current_screen || 'forms_page_gf_entries' !== $current_screen->base ) {
60
			return;
61
		}
62
63
		$version      = \GV\Plugin::$version;
64
		$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
65
66
		wp_enqueue_script( 'gravityview-selectWoo', plugins_url( 'assets/lib/selectWoo/selectWoo.full.min.js', GRAVITYVIEW_FILE ), array(), $version );
67
		wp_enqueue_style( 'gravityview-selectWoo', plugins_url( 'assets/lib/selectWoo/selectWoo.min.css', GRAVITYVIEW_FILE ), array(), $version );
68
69
		wp_enqueue_script( 'gravityview_entry_creator', plugins_url( 'assets/js/admin-entry-creator' . $script_debug . '.js', GRAVITYVIEW_FILE ), array( 'jquery', 'gravityview-selectWoo' ), $version );
70
71
		wp_localize_script(
72
			'gravityview_entry_creator',
73
			'GVEntryCreator',
74
			array(
75
				'ajaxurl'  => admin_url( 'admin-ajax.php' ),
76
				'action'   => 'entry_creator_get_users',
77
				'language' => array(
78
					'search_placeholder' => esc_html__( 'Search for username or other user attribute', 'gravityview' ),
79
				),
80
			)
81
		);
82
	}
83
84
	/**
85
	 * Get users list for entry creator.
86
	 *
87
	 * @since  2.9.1
88
	 *
89
	 */
90
	function entry_creator_get_users() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
92
		$post_var = wp_parse_args(
93
			wp_unslash( $_POST ),
94
			array(
95
				'q'        => '',
96
				'gv_nonce' => '',
97
			)
98
		);
99
100
		if ( ! wp_verify_nonce( $post_var['gv_nonce'], 'gv_entry_creator' ) ) {
101
			die();
102
		}
103
104
		$search_string = $post_var['q'];
105
106
		if ( is_numeric( $search_string ) ) {
107
			$user_args = array(
108
				'search'         => $search_string . '*',
109
				'search_columns' => array( 'ID' ),
110
			);
111
		} else {
112
			$user_args = array(
113
				'search'         => '*' . $search_string . '*',
114
				'search_columns' => array( 'user_login', 'user_email', 'user_nicename', 'display_name' ),
115
			);
116
		}
117
118
		$users = GVCommon::get_users( 'change_entry_creator', $user_args );
119
120
		wp_send_json( $users, 200 );
121
	}
122
123
	/**
124
	 * When an user is created using the User Registration add-on, assign the entry to them
125
	 *
126
	 * @since  1.5.1
127
	 * @param int    $user_id  WordPress User ID
128
	 * @param array  $config   User registration feed configuration
129
	 * @param array  $entry    GF Entry array
130
	 * @param string $password User password
131
	 * @return void
132
	 * @uses   RGFormsModel::update_lead_property() Modify the entry `created_by` field
133
	 */
134
	function assign_new_user_to_lead( $user_id, $config, $entry = array(), $password = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
135
136
		/**
137
		 * Disable assigning the new user to the entry by returning false.
138
		 *
139
		 * @param int   $user_id WordPress User ID
140
		 * @param array $config  User registration feed configuration
141
		 * @param array $entry   GF Entry array
142
		 */
143
		$assign_to_lead = apply_filters( 'gravityview_assign_new_user_to_entry', true, $user_id, $config, $entry );
144
145
		// If filter returns false, do not process
146
		if ( empty( $assign_to_lead ) ) {
147
			return;
148
		}
149
150
		// Update the entry. The `false` prevents checking Akismet; `true` disables the user updated hook from firing
151
		$result = RGFormsModel::update_entry_property( (int) $entry['id'], 'created_by', (int) $user_id, false, true );
152
153
		if ( false === $result ) {
154
			$status = __( 'Error', 'gravityview' );
155
			global $wpdb;
156
			$note = sprintf( '%s: Failed to assign User ID #%d as the entry creator (Last database error: "%s")', $status, $user_id, $wpdb->last_error );
157
		} else {
158
			$status = __( 'Success', 'gravityview' );
159
			$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 );
160
		}
161
162
		gravityview()->log->debug( 'GravityView_Change_Entry_Creator[assign_new_user_to_lead] - {note}', array( 'note' => $note ) );
163
164
		/**
165
		 * @filter `gravityview_disable_change_entry_creator_note` Disable adding a note when changing the entry creator
166
		 * @since  1.21.5
167
		 * @param boolean $disable Disable the Change Entry Creator note. Default: false.
168
		 */
169
		if ( apply_filters( 'gravityview_disable_change_entry_creator_note', false ) ) {
170
			return;
171
		}
172
173
		GravityView_Entry_Notes::add_note( $entry['id'], - 1, 'GravityView', $note, 'gravityview' );
174
175
	}
176
177
	/**
178
	 * Disable previous functionality; use this one as the canonical.
179
	 *
180
	 * @return void
181
	 */
182
	function prevent_conflicts() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
184
		// Plugin that was provided here:
185
		// @link https://gravityview.co/support/documentation/201991205/
186
		remove_action( "gform_entry_info", 'gravityview_change_entry_creator_form', 10 );
187
		remove_action( "gform_after_update_entry", 'gravityview_update_entry_creator', 10 );
188
189
	}
190
191
	/**
192
	 * @since  3.6.3
193
	 * @return void
194
	 */
195
	function load() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
196
197
		// Does GF exist?
198
		if ( ! class_exists( 'GFCommon' ) ) {
199
			return;
200
		}
201
202 1
		// Can the user edit entries?
203
		if ( ! GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_entries' ) ) ) {
204 1
			return;
205
		}
206
207
		// If screen mode isn't set, then we're in the wrong place.
208 1
		if ( empty( $_REQUEST['screen_mode'] ) ) {
209
			return;
210 1
		}
211
212 1
		// Now, no validation is required in the methods; let's hook in.
213
		add_action( 'admin_init', array( &$this, 'set_screen_mode' ) );
214
215 1
		add_action( 'gform_entry_info', array( &$this, 'add_select' ), 10, 2 );
216
217
		add_action( "gform_after_update_entry", array( &$this, 'update_entry_creator' ), 10, 2 );
218
219
	}
220
221
	/**
222 1
	 * Allows for edit links to work with a link instead of a form (GET instead of POST)
223 1
	 *
224 1
	 * @return void
225
	 */
226
	function set_screen_mode() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
227
228 1
		if ( 'view' === \GV\Utils::_POST( 'screen_mode' ) ) {
229 1
			return;
230
		}
231
232
		// If $_GET['screen_mode'] is set to edit, set $_POST value
233 1
		if ( \GV\Utils::_GET( 'screen_mode' ) === 'edit' ) {
234 1
			$_POST["screen_mode"] = 'edit';
235 1
		}
236 1
237
	}
238 1
239 1
	/**
240
	 * When the entry creator is changed, add a note to the entry
241 1
	 *
242
	 * @param array $form     GF entry array
243 1
	 * @param int   $entry_id Entry ID
244 1
	 * @return void
245
	 */
246
	function update_entry_creator( $form, $entry_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
247
248
		global $current_user;
249
250
		// Update the entry
251
		$created_by = absint( \GV\Utils::_POST( 'created_by' ) );
252
253
		RGFormsModel::update_lead_property( $entry_id, 'created_by', $created_by );
254
255
		// If the creator has changed, let's add a note about who it used to be.
256
		$originally_created_by = \GV\Utils::_POST( 'originally_created_by' );
257
258
		// If there's no owner and there didn't used to be, keep going
259
		if ( empty( $originally_created_by ) && empty( $created_by ) ) {
260
			return;
261
		}
262
263
		// If the values have changed
264
		if ( absint( $originally_created_by ) !== absint( $created_by ) ) {
265
266
			$user_data = get_userdata( $current_user->ID );
267
268
			$user_format = _x( '%s (ID #%d)', 'The name and the ID of users who initiated changes to entry ownership', 'gravityview' );
269
270
			$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' );
271
272
			if ( ! empty( $originally_created_by ) ) {
273
				$originally_created_by_user_data = get_userdata( $originally_created_by );
274
				$original_name                   = sprintf( $user_format, $originally_created_by_user_data->display_name, $originally_created_by_user_data->ID );
275
			}
276
277
			if ( ! empty( $created_by ) ) {
278
				$created_by_user_data = get_userdata( $created_by );
279
				$created_by_name      = sprintf( $user_format, $created_by_user_data->display_name, $created_by_user_data->ID );
280
			}
281
282
			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' );
283
		}
284
285
	}
286
287
	/**
288
	 * Output select element used to change the entry creator
289
	 *
290
	 * @param int   $form_id GF Form ID
291
	 * @param array $entry   GF entry array
292
	 *
293
	 * @return void
294
	 */
295
	function add_select( $form_id, $entry ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
296
297
		if ( 'edit' !== \GV\Utils::_POST( 'screen_mode' ) ) {
298
			return;
299
		}
300
301
		$output = '<label for="change_created_by">';
302
		$output .= esc_html__( 'Change Entry Creator:', 'gravityview' );
303
		$output .= '</label>';
304
		$output .= '<select name="created_by" id="change_created_by" class="widefat">';
305
306
		$entry_creator_user_id = \GV\Utils::get( $entry, 'created_by' );
307
308
		$entry_creator_user = GVCommon::get_users( 'change_entry_creator', array( 'include' => $entry_creator_user_id ) );
309
		$entry_creator_user = isset( $entry_creator_user[0] ) ? $entry_creator_user[0] : array();
310
311
		if ( empty( $entry_creator_user ) ) {
312
			$output .= '<option value="0"> &mdash; ' . esc_attr_x( 'No User', 'No user assigned to the entry', 'gravityview' ) . ' &mdash; </option>';
313
		} else {
314
			$output .= '<option value="' . $entry_creator_user->ID . '" "selected">' . esc_attr( $entry_creator_user->display_name . ' (' . $entry_creator_user->user_nicename . ')' ) . '</option>';
315
		}
316
317
		$all_users = GVCommon::get_users( 'change_entry_creator', array( 'number' => self::DEFAULT_NUMBER_OF_USERS ) );
318
		foreach ( $all_users as $user ) {
319
			if ( $entry_creator_user_id === $user->ID ) {
320
				continue;
321
			}
322
323
			$output .= '<option value="' . $user->ID . '">' . esc_attr( $user->display_name . ' (' . $user->user_nicename . ')' ) . '</option>';
324
		}
325
326
		$user_count      = count_users();
327
		$user_count      = $user_count['total_users'];
328
		$users_displayed = self::DEFAULT_NUMBER_OF_USERS + ( ! empty( $entry_creator_user ) ? 1 : 0 );
329
		if ( $user_count > $users_displayed ) {
330
			$message = esc_html_x( 'Use the input field above to search through the remaining %d users.', '%d is replaced by user count', 'gravityview' );
331
			$message = sprintf( $message, $user_count - $users_displayed );
332
			$output  .= '<option value="_user_count" disabled="disabled">' . $message . '</option>';
333
		}
334
335
		$output .= '</select>';
336
		$output .= '<input name="originally_created_by" value="' . esc_attr( $entry['created_by'] ) . '" type="hidden" />';
337
		$output .= wp_nonce_field( 'gv_entry_creator', 'gv_entry_creator_nonce', false, false );
338
339
		echo $output;
340
	}
341
}
342
343
new GravityView_Change_Entry_Creator;
344