GravityView  2.10.1
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 whitelist 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.', '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', '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', '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', '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', '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.', 'gravityview' );
281 
282  if ( ! empty( $originally_created_by ) ) {
283  $originally_created_by_user_data = get_userdata( $originally_created_by );
284  $original_name = sprintf( $user_format, $originally_created_by_user_data->display_name, $originally_created_by_user_data->ID );
285  }
286 
287  if ( ! empty( $created_by ) ) {
288  $created_by_user_data = get_userdata( $created_by );
289  $created_by_name = sprintf( $user_format, $created_by_user_data->display_name, $created_by_user_data->ID );
290  }
291 
292  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' );
293  }
294 
295  }
296 
297  /**
298  * Output select element used to change the entry creator
299  *
300  * @param int $form_id GF Form ID
301  * @param array $entry GF entry array
302  *
303  * @return void
304  */
305  function add_select( $form_id, $entry ) {
306 
307  if ( 'edit' !== \GV\Utils::_POST( 'screen_mode' ) ) {
308  return;
309  }
310 
311  $output = '<label for="change_created_by">';
312  $output .= esc_html__( 'Change Entry Creator:', 'gravityview' );
313  $output .= '</label>';
314  $output .= '<select name="created_by" id="change_created_by" class="widefat">';
315 
316  $entry_creator_user_id = \GV\Utils::get( $entry, 'created_by' );
317 
318  $entry_creator_user = GVCommon::get_users( 'change_entry_creator', array( 'include' => $entry_creator_user_id ) );
319  $entry_creator_user = isset( $entry_creator_user[0] ) ? $entry_creator_user[0] : array();
320 
321  if ( empty( $entry_creator_user ) ) {
322  $output .= '<option value="0"> &mdash; ' . esc_attr_x( 'No User', 'No user assigned to the entry', 'gravityview' ) . ' &mdash; </option>';
323  } else {
324  $output .= '<option value="' . $entry_creator_user->ID . '" "selected">' . esc_attr( $entry_creator_user->display_name . ' (' . $entry_creator_user->user_nicename . ')' ) . '</option>';
325  }
326 
327  $all_users = GVCommon::get_users( 'change_entry_creator', array( 'number' => self::DEFAULT_NUMBER_OF_USERS ) );
328  foreach ( $all_users as $user ) {
329  if ( $entry_creator_user_id === $user->ID ) {
330  continue;
331  }
332 
333  $output .= '<option value="' . $user->ID . '">' . esc_attr( $user->display_name . ' (' . $user->user_nicename . ')' ) . '</option>';
334  }
335 
336  $user_count = count_users();
337  $user_count = $user_count['total_users'];
338  $users_displayed = self::DEFAULT_NUMBER_OF_USERS + ( ! empty( $entry_creator_user ) ? 1 : 0 );
339  if ( $user_count > $users_displayed ) {
340  $remaining_users = $user_count - $users_displayed;
341  $user_users = _n( esc_html__('user', 'gravityview' ), esc_html__('users', 'gravityview' ), $remaining_users );
342  $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"', 'gravityview' );
343  $message = sprintf( $message, $remaining_users, $user_users );
344  $output .= '<option value="_user_count" disabled="disabled">' . esc_html( $message ) . '</option>';
345  }
346 
347  $output .= '</select>';
348  $output .= '<input name="originally_created_by" value="' . esc_attr( $entry['created_by'] ) . '" type="hidden" />';
349  $output .= wp_nonce_field( 'gv_entry_creator', 'gv_entry_creator_nonce', false, false );
350 
351  echo $output;
352  }
353 
354  /**
355  * Whitelist UI assets
356  *
357  * @param array $assets
358  *
359  * @return array
360  */
361  function register_gform_noconflict( $assets ) {
362  $assets[] = 'gravityview_selectwoo';
363  $assets[] = 'gravityview_entry_creator';
364 
365  return $assets;
366  }
367 }
368 
if(empty( $value)) $user
__construct()
const DEFAULT_NUMBER_OF_USERS
register_gform_noconflict( $assets)
Whitelist UI assets.
gravityview()
Definition: _stubs.php:26
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:31
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
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.