KWS_GF_Change_Lead_Creator

The Gravity Forms Directory & Addons KWS GF Change Lead Creator class.

Defined (1)

The class is defined in the following location(s).

/gravity-forms-lead-creator.php  
  1. class KWS_GF_Change_Lead_Creator { 
  2.  
  3. function __construct() { 
  4.  
  5. add_action('plugins_loaded', array($this, 'load')); 
  6.  
  7. /** 
  8. * @since 3.6.3 
  9. * @return void 
  10. */ 
  11. function load() { 
  12.  
  13. // Does GF exist? Can the user edit entries? 
  14. if( !class_exists('GFCommon') ) { 
  15. return; 
  16.  
  17. if( !GFCommon::current_user_can_any("gravityforms_edit_entries") ) { 
  18. return; 
  19.  
  20. // If screen mode isn't set, then we're in the wrong place. 
  21. if( empty( $_REQUEST['screen_mode'] ) ) { 
  22. return; 
  23.  
  24. // Now, no validation is required in the methods; let's hook in. 
  25. add_action('admin_init', array( &$this, 'set_screen_mode' ) ); 
  26.  
  27. add_action("gform_entry_info", array( &$this, 'add_select' ), 10, 2); 
  28.  
  29. add_action("gform_after_update_entry", array( &$this, 'update_entry_creator' ), 10, 2); 
  30.  
  31.  
  32. /** 
  33. * Allows for edit links to work with a link instead of a form (GET instead of POST) 
  34. * @return [type] [description] 
  35. */ 
  36. function set_screen_mode() { 
  37.  
  38. if( !empty( $_REQUEST["screen_mode"] ) ) { 
  39. $_POST["screen_mode"] = esc_attr( $_REQUEST["screen_mode"] ); 
  40.  
  41.  
  42. /** 
  43. * When the entry creator is changed, add a note to the entry 
  44. * @param array $form GF entry array 
  45. * @param int $leadid Lead ID 
  46. * @return void 
  47. */ 
  48. function update_entry_creator($form, $leadid) { 
  49. global $current_user; 
  50.  
  51. // Update the entry 
  52. $created_by = intval( rgpost('created_by') ); 
  53.  
  54. RGFormsModel::update_lead_property($leadid, 'created_by', $created_by); 
  55.  
  56. // If the creator has changed, let's add a note about who it used to be. 
  57. $originally_created_by = rgpost('originally_created_by'); 
  58.  
  59. if($originally_created_by !== $created_by) { 
  60.  
  61. $user_data = get_userdata($current_user->ID); 
  62.  
  63. $user_format = __('%s (ID #%d)', 'gravity-view'); 
  64.  
  65. $original_name = $created_by_name = esc_attr__( 'No User', 'gravity-view'); 
  66.  
  67. if( !empty( $originally_created_by ) ) { 
  68. $originally_created_by_user_data = get_userdata($originally_created_by); 
  69. $original_name = sprintf( $user_format, $originally_created_by_user_data->display_name, $originally_created_by_user_data->ID ); 
  70.  
  71. if( !empty( $created_by ) ) { 
  72. $created_by_user_data = get_userdata($created_by); 
  73. $created_by_name = sprintf( $user_format, $created_by_user_data->display_name, $created_by_user_data->ID ); 
  74.  
  75. RGFormsModel::add_note($leadid, $current_user->ID, $user_data->display_name, sprintf(__('Changed lead creator from %s to %s', 'gravity-forms-addons'), $original_name, $created_by_name ) ); 
  76.  
  77.  
  78. /** 
  79. * Output the select to change the entry creator 
  80. * @param int $form_id GF Form ID 
  81. * @param array $lead GF lead array 
  82. * @return void 
  83. */ 
  84. function add_select($form_id, $lead) { 
  85.  
  86. if( rgpost('screen_mode') !== 'edit' ) { 
  87. return; 
  88.  
  89. /** 
  90. * There are issues with too many users where it breaks the select. We try to keep it at a reasonable number. 
  91. * @link texthttp://codex.wordpress.org/Function_Reference/get_users 
  92. * @var array Settings array 
  93. */ 
  94. $get_users_settings = apply_filters( 'gravityview_change_entry_creator_user_parameters', array( 'number' => 300 ) ); 
  95.  
  96. $users = get_users( $get_users_settings ); 
  97.  
  98. $output = '<label for="change_created_by">'; 
  99. $output .= esc_html__('Change Entry Creator:', 'gravity-forms-addons'); 
  100. $output .= '</label> 
  101. <select name="created_by" id="change_created_by" class="widefat">'; 
  102. $output .= '<option value=""> — '.esc_attr__( 'No User', 'gravity-view').' — </option>'; 
  103. foreach($users as $user) { 
  104. $output .= '<option value="'. $user->ID .'"'. selected( $lead['created_by'], $user->ID, false ).'>'.esc_attr( $user->display_name.' ('.$user->user_nicename.')' ).'</option>'; 
  105. $output .= '</select>'; 
  106. $output .= '<input name="originally_created_by" value="'.$lead['created_by'].'" type="hidden" />'; 
  107. echo $output; 
  108.  
  109.