GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-change-entry-creator.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @since 1.2
5  */
7 
8  /*
9  * @var int Number of users to show in the select element
10  */
12 
13  function __construct() {
14 
15  /**
16  * @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  // Enqueue and allow selectWoo UI assets
44  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_selectwoo_assets' ) );
45  add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict' ) );
46  add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict' ) );
47 
48  // Ajax callback to get users to change entry creator.
49  add_action( 'wp_ajax_entry_creator_get_users', array( $this, 'entry_creator_get_users' ) );
50  }
51 
52  /**
53  * Enqueue selectWoo script and style.
54  *
55  * @since 2.9.1
56  */
58 
59  if ( ! class_exists( 'GFForms' ) ) {
60  return;
61  }
62 
63  if ( ! in_array( GFForms::get_page(), array( 'entry_detail_edit' ) ) ) {
64  return;
65  }
66 
67  $version = \GV\Plugin::$version;
68  $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
69 
70  if ( gravityview()->plugin->is_GF_25() ) {
71  wp_deregister_script( 'gform_selectwoo' );
72  wp_dequeue_script( 'gform_selectwoo' );
73  }
74 
75  wp_enqueue_script( 'gravityview_selectwoo', plugins_url( 'assets/lib/selectWoo/selectWoo.full.min.js', GRAVITYVIEW_FILE ), array(), $version );
76  wp_enqueue_style( 'gravityview_selectwoo', plugins_url( 'assets/lib/selectWoo/selectWoo.min.css', GRAVITYVIEW_FILE ), array(), $version );
77 
78  wp_enqueue_script( 'gravityview_entry_creator', plugins_url( 'assets/js/admin-entry-creator' . $script_debug . '.js', GRAVITYVIEW_FILE ), array( 'jquery', 'gravityview_selectwoo' ), $version );
79 
80  wp_localize_script(
81  'gravityview_entry_creator',
82  'GVEntryCreator',
83  array(
84  'ajaxurl' => admin_url( 'admin-ajax.php' ),
85  'action' => 'entry_creator_get_users',
86  'gf25' => (bool) gravityview()->plugin->is_GF_25(),
87  'language' => array(
88  'search_placeholder' => esc_html__( 'Search by ID, login, email, or name.', 'gk-gravityview' ),
89  ),
90  )
91  );
92  }
93 
94  /**
95  * Get users list for entry creator.
96  *
97  * @since 2.9.1
98  *
99  */
101 
102  $post_var = wp_parse_args(
103  wp_unslash( $_POST ),
104  array(
105  'q' => '',
106  'gv_nonce' => '',
107  )
108  );
109 
110  if ( ! wp_verify_nonce( $post_var['gv_nonce'], 'gv_entry_creator' ) ) {
111  die();
112  }
113 
114  $search_string = $post_var['q'];
115 
116  if ( is_numeric( $search_string ) ) {
117  $user_args = array(
118  'search' => $search_string . '*',
119  'search_columns' => array( 'ID' ),
120  );
121  } else {
122  $user_args = array(
123  'search' => '*' . $search_string . '*',
124  'search_columns' => array( 'user_login', 'user_email', 'user_nicename', 'display_name' ),
125  );
126  }
127 
128  $users = GVCommon::get_users( 'change_entry_creator', $user_args );
129 
130  wp_send_json( $users, 200 );
131  }
132 
133  /**
134  * When an user is created using the User Registration add-on, assign the entry to them
135  *
136  * @since 1.5.1
137  * @param int $user_id WordPress User ID
138  * @param array $config User registration feed configuration
139  * @param array $entry GF Entry array
140  * @param string $password User password
141  * @return void
142  * @uses RGFormsModel::update_lead_property() Modify the entry `created_by` field
143  */
144  function assign_new_user_to_lead( $user_id, $config, $entry = array(), $password = '' ) {
145 
146  /**
147  * Disable assigning the new user to the entry by returning false.
148  *
149  * @param int $user_id WordPress User ID
150  * @param array $config User registration feed configuration
151  * @param array $entry GF Entry array
152  */
153  $assign_to_lead = apply_filters( 'gravityview_assign_new_user_to_entry', true, $user_id, $config, $entry );
154 
155  // If filter returns false, do not process
156  if ( empty( $assign_to_lead ) ) {
157  return;
158  }
159 
160  // Update the entry. The `false` prevents checking Akismet; `true` disables the user updated hook from firing
161  $result = RGFormsModel::update_entry_property( (int) $entry['id'], 'created_by', (int) $user_id, false, true );
162 
163  if ( false === $result ) {
164  $status = __( 'Error', 'gk-gravityview' );
165  global $wpdb;
166  $note = sprintf( '%s: Failed to assign User ID #%d as the entry creator (Last database error: "%s")', $status, $user_id, $wpdb->last_error );
167  } else {
168  $status = __( 'Success', 'gk-gravityview' );
169  $note = sprintf( _x( '%s: Assigned User ID #%d as the entry creator.', 'First parameter: Success or error of the action. Second: User ID number', 'gk-gravityview' ), $status, $user_id );
170  }
171 
172  gravityview()->log->debug( 'GravityView_Change_Entry_Creator[assign_new_user_to_lead] - {note}', array( 'note' => $note ) );
173 
174  /**
175  * @filter `gravityview_disable_change_entry_creator_note` Disable adding a note when changing the entry creator
176  * @since 1.21.5
177  * @param boolean $disable Disable the Change Entry Creator note. Default: false.
178  */
179  if ( apply_filters( 'gravityview_disable_change_entry_creator_note', false ) ) {
180  return;
181  }
182 
183  GravityView_Entry_Notes::add_note( $entry['id'], - 1, 'GravityView', $note, 'gravityview' );
184 
185  }
186 
187  /**
188  * Disable previous functionality; use this one as the canonical.
189  *
190  * @return void
191  */
192  function prevent_conflicts() {
193 
194  // Plugin that was provided here:
195  // @link https://gravityview.co/support/documentation/201991205/
196  remove_action( "gform_entry_info", 'gravityview_change_entry_creator_form', 10 );
197  remove_action( "gform_after_update_entry", 'gravityview_update_entry_creator', 10 );
198 
199  }
200 
201  /**
202  * @since 3.6.3
203  * @return void
204  */
205  function load() {
206 
207  // Does GF exist?
208  if ( ! class_exists( 'GFCommon' ) ) {
209  return;
210  }
211 
212  // Can the user edit entries?
213  if ( ! GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_entries' ) ) ) {
214  return;
215  }
216 
217  // If screen mode isn't set, then we're in the wrong place.
218  if ( empty( $_REQUEST['screen_mode'] ) ) {
219  return;
220  }
221 
222  // Now, no validation is required in the methods; let's hook in.
223  add_action( 'admin_init', array( &$this, 'set_screen_mode' ) );
224 
225  add_action( 'gform_entry_info', array( &$this, 'add_select' ), 10, 2 );
226 
227  add_action( "gform_after_update_entry", array( &$this, 'update_entry_creator' ), 10, 2 );
228 
229  }
230 
231  /**
232  * Allows for edit links to work with a link instead of a form (GET instead of POST)
233  *
234  * @return void
235  */
236  function set_screen_mode() {
237 
238  if ( 'view' === \GV\Utils::_POST( 'screen_mode' ) ) {
239  return;
240  }
241 
242  // If $_GET['screen_mode'] is set to edit, set $_POST value
243  if ( \GV\Utils::_GET( 'screen_mode' ) === 'edit' ) {
244  $_POST["screen_mode"] = 'edit';
245  }
246 
247  }
248 
249  /**
250  * When the entry creator is changed, add a note to the entry
251  *
252  * @param array $form GF entry array
253  * @param int $entry_id Entry ID
254  * @return void
255  */
256  function update_entry_creator( $form, $entry_id ) {
257 
258  global $current_user;
259 
260  // Update the entry
261  $created_by = absint( \GV\Utils::_POST( 'created_by' ) );
262 
263  RGFormsModel::update_lead_property( $entry_id, 'created_by', $created_by );
264 
265  // If the creator has changed, let's add a note about who it used to be.
266  $originally_created_by = \GV\Utils::_POST( 'originally_created_by' );
267 
268  // If there's no owner and there didn't used to be, keep going
269  if ( empty( $originally_created_by ) && empty( $created_by ) ) {
270  return;
271  }
272 
273  // If the values have changed
274  if ( absint( $originally_created_by ) !== absint( $created_by ) ) {
275 
276  $user_data = get_userdata( $current_user->ID );
277 
278  $user_format = _x( '%s (ID #%d)', 'The name and the ID of users who initiated changes to entry ownership', 'gk-gravityview' );
279 
280  $original_name = $created_by_name = esc_attr_x( 'No User', 'To show that the entry was unassigned from an actual user to no user.', 'gk-gravityview' );
281 
282  if ( ! empty( $originally_created_by ) ) {
283  $originally_created_by_user_data = get_userdata( $originally_created_by );
284 
285  $original_name = ! empty( $originally_created_by_user_data ) ?
286  sprintf( $user_format, $originally_created_by_user_data->display_name, $originally_created_by_user_data->ID ) :
287  esc_attr_x( 'Deleted User', 'To show that the entry was created by a no longer existing user.', 'gk-gravityview' );
288  }
289 
290  if ( ! empty( $created_by ) ) {
291  $created_by_user_data = get_userdata( $created_by );
292 
293  $created_by_name = ! empty( $created_by_user_data ) ?
294  sprintf( $user_format, $created_by_user_data->display_name, $created_by_user_data->ID ) :
295  esc_attr_x( 'Deleted User', 'To show that the entry was created by a no longer existing user.', 'gk-gravityview' );
296  }
297 
298  GravityView_Entry_Notes::add_note( $entry_id, $current_user->ID, $user_data->display_name, sprintf( __( 'Changed entry creator from %s to %s', 'gk-gravityview' ), $original_name, $created_by_name ), 'note' );
299  }
300 
301  }
302 
303  /**
304  * Output select element used to change the entry creator
305  *
306  * @param int $form_id GF Form ID
307  * @param array $entry GF entry array
308  *
309  * @return void
310  */
311  function add_select( $form_id, $entry ) {
312 
313  if ( 'edit' !== \GV\Utils::_POST( 'screen_mode' ) ) {
314  return;
315  }
316 
317  $output = '<label for="change_created_by">';
318  $output .= esc_html__( 'Change Entry Creator:', 'gk-gravityview' );
319  $output .= '</label>';
320  $output .= '<select name="created_by" id="change_created_by" class="widefat">';
321 
322  $entry_creator_user_id = \GV\Utils::get( $entry, 'created_by' );
323 
324  $entry_creator_user = GVCommon::get_users( 'change_entry_creator', array( 'include' => $entry_creator_user_id ) );
325  $entry_creator_user = isset( $entry_creator_user[0] ) ? $entry_creator_user[0] : array();
326 
327  $output .= '<option value="0" ' . selected( true, empty( $entry_creator_user_id ), false ) . '> &mdash; ' . esc_attr_x( 'No User', 'No user assigned to the entry', 'gk-gravityview' ) . ' &mdash; </option>';
328 
329  // Always show the entry creator, even when the user isn't included within the pagination limits
330  if ( ! empty( $entry_creator_user_id ) && ! empty( $entry_creator_user ) ) {
331  $output .= '<option value="' . $entry_creator_user->ID . '" selected="selected">' . esc_attr( $entry_creator_user->display_name . ' (' . $entry_creator_user->user_nicename . ')' ) . '</option>';
332  }
333 
334  $all_users = GVCommon::get_users( 'change_entry_creator', array( 'number' => self::DEFAULT_NUMBER_OF_USERS ) );
335  foreach ( $all_users as $user ) {
336  if ( $entry_creator_user_id === $user->ID ) {
337  continue;
338  }
339 
340  $output .= '<option value="' . esc_attr( $user->ID ) . '">' . esc_attr( $user->display_name . ' (' . $user->user_nicename . ')' ) . '</option>';
341  }
342 
343  $user_count = count_users();
344  $user_count = $user_count['total_users'];
345  $users_displayed = self::DEFAULT_NUMBER_OF_USERS + ( ! empty( $entry_creator_user ) ? 1 : 0 );
346  if ( $user_count > $users_displayed ) {
347  $remaining_users = $user_count - $users_displayed;
348  $user_users = _n( esc_html__('user', 'gk-gravityview' ), esc_html__('users', 'gk-gravityview' ), $remaining_users );
349  $message = esc_html_x( 'Use the input above to search the remaining %d %s.', '%d is replaced with user count %s is replaced with "user" or "users"', 'gk-gravityview' );
350  $message = sprintf( $message, $remaining_users, $user_users );
351  $output .= '<option value="_user_count" disabled="disabled">' . esc_html( $message ) . '</option>';
352  }
353 
354  $output .= '</select>';
355  $output .= '<input name="originally_created_by" value="' . esc_attr( $entry['created_by'] ) . '" type="hidden" />';
356  $output .= wp_nonce_field( 'gv_entry_creator', 'gv_entry_creator_nonce', false, false );
357 
358  echo $output;
359  }
360 
361  /**
362  * Allow UI assets
363  *
364  * @param array $assets
365  *
366  * @return array
367  */
368  function register_gform_noconflict( $assets ) {
369  $assets[] = 'gravityview_selectwoo';
370  $assets[] = 'gravityview_entry_creator';
371 
372  return $assets;
373  }
374 }
375 
if(empty( $value)) $user
__construct()
const DEFAULT_NUMBER_OF_USERS
register_gform_noconflict( $assets)
Allow UI assets.
if(gravityview() ->plugin->is_GF_25()) $form
static $version
update_entry_creator( $form, $entry_id)
When the entry creator is changed, add a note to the entry.
set_screen_mode()
Allows for edit links to work with a link instead of a form (GET instead of POST) ...
static get_users( $context='change_entry_creator', $args=array())
Get WordPress users with reasonable limits set.
entry_creator_get_users()
Get users list for entry creator.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:40
assign_new_user_to_lead( $user_id, $config, $entry=array(), $password='')
When an user is created using the User Registration add-on, assign the entry to them.
$created_by
load()
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
static add_note( $lead_id, $user_id, $user_name, $note='', $note_type='gravityview')
Alias for GFFormsModel::add_note() with default note_type of &#39;gravityview&#39;.
new GravityView_Change_Entry_Creator
gravityview()
The main GravityView wrapper function.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
$entry
Definition: notes.php:27
add_select( $form_id, $entry)
Output select element used to change the entry creator.
prevent_conflicts()
Disable previous functionality; use this one as the canonical.
enqueue_selectwoo_assets()
Enqueue selectWoo script and style.
static _POST( $name, $default=null)
Grab a value from the _POST superglobal or default.