GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-admin-approve-entries.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-admin-approve-entries.php
4  * @package GravityView
5  * @license GPL2+
6  * @author GravityView <[email protected]>
7  * @link http://gravityview.co
8  * @copyright Copyright 2014, Katz Web Services, Inc.
9  *
10  * @since 1.0.0
11  */
12 
13 
14 
16 
17  // hold notification messages
18  public $bulk_update_message = '';
19 
20  /**
21  * @var array Set the prefixes here instead of spread across the class
22  * @since 1.17
23  */
24  private $bulk_action_prefixes = array(
25  'approve' => 'gvapprove',
26  'disapprove' => 'gvdisapprove',
27  'unapprove' => 'gvunapprove',
28  );
29 
30  function __construct() {
31 
32  $this->add_hooks();
33 
34  }
35 
36  private function add_hooks() {
37  /** Edit Gravity Form page */
38 
39  // Add button to left menu
40  add_filter( 'gform_add_field_buttons', array( $this, 'add_field_buttons' ) );
41  // Set defaults
42  add_action( 'gform_editor_js_set_default_values', array( $this, 'set_defaults' ) );
43 
44  /** gf_entries page - entries table screen */
45 
46  // add hidden field with approve status
47  add_action( 'gform_entries_first_column_actions', array( $this, 'add_entry_approved_hidden_input' ), 1, 5 );
48 
49  add_filter( 'gravityview/metaboxes/tooltips', array( $this, 'tooltips' ) );
50 
51  // adding styles and scripts
52  add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles') );
53  // bypass Gravity Forms no-conflict mode
54  add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict_script' ) );
55  add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict_style' ) );
56 
57  add_filter( 'gform_filter_links_entry_list', array( $this, 'filter_links_entry_list' ), 10, 3 );
58  }
59 
60  /**
61  * Add filter links to the Entries page
62  *
63  * Can be disabled by returning false on the `gravityview/approve_entries/show_filter_links_entry_list` filter
64  *
65  * @since 1.17.1
66  *
67  * @param array $filter_links Array of links to include in the subsubsub filter list. Includes `id`, `field_filters`, `count`, and `label` keys
68  * @param array $form GF Form object of current form
69  * @param bool $include_counts Whether to include counts in the output
70  *
71  * @return array Filter links, with GravityView approved/disapproved links added
72  */
73  public function filter_links_entry_list( $filter_links = array(), $form = array(), $include_counts = true ) {
74 
75  /**
76  * @filter `gravityview/approve_entries/show_filter_links_entry_list` Disable filter links
77  * @since 1.17.1
78  * @param bool $show_filter_links True: show the "approved"/"disapproved" filter links. False: hide them.
79  * @param array $form GF Form object of current form
80  */
81  if( false === apply_filters( 'gravityview/approve_entries/show_filter_links_entry_list', true, $form ) ) {
82  return $filter_links;
83  }
84 
85  $field_filters_approved = array(
86  array(
89  ),
90  );
91 
92  $field_filters_disapproved = array(
93  array(
96  ),
97  );
98 
99  $field_filters_unapproved = array(
100  array(
103  ),
104  );
105 
106  $approved_count = $disapproved_count = $unapproved_count = 0;
107 
108  // Only count if necessary
109  if( $include_counts ) {
110  $approved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_approved ) ) );
111  $disapproved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_disapproved ) ) );
112  $unapproved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_unapproved ) ) );
113  }
114 
115  $filter_links[] = array(
116  'id' => 'gv_approved',
117  'field_filters' => $field_filters_approved,
118  'count' => $approved_count,
120  );
121 
122  $filter_links[] = array(
123  'id' => 'gv_disapproved',
124  'field_filters' => $field_filters_disapproved,
125  'count' => $disapproved_count,
127  );
128 
129  $filter_links[] = array(
130  'id' => 'gv_unapproved',
131  'field_filters' => $field_filters_unapproved,
132  'count' => $unapproved_count,
134  );
135 
136  return $filter_links;
137  }
138 
139  /**
140  * Add the GravityView Fields group tooltip
141  *
142  * @param $tooltips
143  *
144  * @return array Tooltips array with GravityView fields tooltip
145  */
146  function tooltips( $tooltips ) {
147 
148  $tooltips['form_gravityview_fields'] = array(
149  'title' => __('GravityView Fields', 'gk-gravityview'),
150  'value' => __( 'Allow administrators to approve or reject entries and users to opt-in or opt-out of their entries being displayed.', 'gk-gravityview'),
151  );
152 
153  return $tooltips;
154  }
155 
156 
157  /**
158  * Inject new add field buttons in the gravity form editor page
159  *
160  * @param mixed $field_groups
161  * @return array Array of fields
162  */
163  function add_field_buttons( $field_groups ) {
164 
165  $gravityview_fields = array(
166  'name' => 'gravityview_fields',
167  'label' => 'GravityView',
168  'fields' => array(
169  array(
170  'class' => 'button',
171  'value' => __( 'Approve/Reject', 'gk-gravityview' ),
172  'onclick' => "StartAddField('gravityviewapproved_admin');",
173  'data-type' => 'gravityviewapproved_admin',
174  'data-icon' => 'dashicons-yes-alt'
175  ),
176  array(
177  'class' => 'button',
178  'value' => __( 'User Opt-In', 'gk-gravityview' ),
179  'onclick' => "StartAddField('gravityviewapproved');",
180  'data-type' => 'gravityviewapproved',
181  'data-icon' => 'dashicons-media-text',
182  ),
183  )
184  );
185 
186  array_push( $field_groups, $gravityview_fields );
187 
188  return $field_groups;
189  }
190 
191 
192 
193  /**
194  * At edit form page, set the field Approve defaults
195  *
196  * @todo Convert to a partial include file
197  * @return void
198  */
199  function set_defaults() {
200  ?>
201  case 'gravityviewapproved_admin':
202  field.label = "<?php echo esc_js( __( 'Approved? (Admin-only)', 'gk-gravityview' ) ); ?>";
203 
204  field.adminLabel = "<?php echo esc_js( __( 'Approved?', 'gk-gravityview' ) ); ?>";
205  field.adminOnly = true;
206 
207  field.choices = null;
208  field.inputs = null;
209 
210  if( !field.choices ) {
211  field.choices = new Array( new Choice("<?php echo esc_js( GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::APPROVED ) ); ?>") );
212  }
213 
214  field.inputs = new Array();
215  for( var i=1; i<=field.choices.length; i++ ) {
216  field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
217  }
218 
219  field.type = 'checkbox';
220  field.gravityview_approved = 1;
221 
222  break;
223  case 'gravityviewapproved':
224  field.label = "<?php echo esc_js( __( 'Show Entry on Website', 'gk-gravityview' ) ); ?>";
225 
226  field.adminLabel = "<?php echo esc_js( __( 'Opt-In', 'gk-gravityview' ) ); ?>";
227  field.adminOnly = false;
228 
229  field.choices = null;
230  field.inputs = null;
231 
232  if( !field.choices ) {
233  field.choices = new Array(
234  new Choice("<?php echo esc_js( __( 'Yes, display my entry on the website', 'gk-gravityview' ) ); ?>")
235  );
236  }
237 
238  field.inputs = new Array();
239  for( var i=1; i<=field.choices.length; i++ ) {
240  field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
241  }
242 
243  field.type = 'checkbox';
244  field.gravityview_approved = 1;
245 
246  break;
247  <?php
248  }
249 
250  /**
251  * update_approved function.
252  *
253  * @since 1.18 Moved to GravityView_Entry_Approval::update_approved
254  * @see GravityView_Entry_Approval::update_approved
255  *
256  * @param int $entry_id (default: 0)
257  * @param int $approved (default: 0)
258  * @param int $form_id (default: 0)
259  * @param int $approvedcolumn (default: 0)
260  *
261  * @return boolean True: It worked; False: it failed
262  */
263  public static function update_approved( $entry_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) {
264  return GravityView_Entry_Approval::update_approved( $entry_id, $approved, $form_id, $approvedcolumn );
265  }
266 
267  /**
268  * Calculate the approve field.input id
269  *
270  * @since 1.18 Moved to GravityView_Entry_Approval::get_approved_column
271  * @see GravityView_Entry_Approval::get_approved_column
272  *
273  * @param mixed $form GF Form or Form ID
274  * @return false|null|string Returns the input ID of the approved field. Returns NULL if no approved fields were found. Returns false if $form_id wasn't set.
275  */
276  static public function get_approved_column( $form ) {
277  return GravityView_Entry_Approval::get_approved_column( $form );
278  }
279 
280  /**
281  * Add a hidden input that is used in the Javascript to show approved/disapproved entries checkbox
282  *
283  * See the /assets/js/admin-entries-list.js setInitialApprovedEntries method
284  *
285  * @param $form_id
286  * @param $field_id
287  * @param $value
288  * @param $entry
289  * @param $query_string
290  *
291  * @return void
292  */
293  static public function add_entry_approved_hidden_input( $form_id, $field_id, $value, $entry, $query_string ) {
294 
295  if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $entry['id'] ) ) {
296  return;
297  }
298 
299  if( empty( $entry['id'] ) ) {
300  return;
301  }
302 
303  $status_value = GravityView_Entry_Approval::get_entry_status( $entry, 'value' );
304 
305  if( $status_value ) {
306  echo '<input type="hidden" class="entry_approval" id="entry_approved_'. $entry['id'] .'" value="' . esc_attr( $status_value ) . '" />';
307  }
308  }
309 
310  /**
311  * Get the form ID of the form currently being displayed
312  *
313  * @since 1.17.1
314  *
315  * @return int ID of the current form being displayed. `0` is returned if no forms are found.
316  */
317  private function get_form_id() {
318 
319  $form_id = GFForms::get('id');
320 
321  // If there are no forms identified, use the first form. That's how GF does it.
322  if( empty( $form_id ) && class_exists('RGFormsModel') ) {
323  $form_id = $this->get_first_form_id();
324  }
325 
326  return absint( $form_id );
327  }
328 
329  /**
330  * Get the first form ID from Gravity Forms, sorted in the same order as in the All Forms page
331  *
332  * @see GFEntryList::all_entries_page() This method is based on the form-selecting code here
333  *
334  * @since 1.17.2
335  *
336  * @return int ID of the first form, sorted by title. `0` if no forms were found.
337  */
338  private function get_first_form_id() {
339 
340  $forms = RGFormsModel::get_forms( null, 'title' );
341 
342  if( ! isset( $forms[0] ) ) {
343  gravityview()->log->error( 'No forms were found' );
344  return 0;
345  }
346 
347  $first_form = $forms[0];
348 
349  $form_id = is_object( $forms[0] ) ? $first_form->id : $first_form['id'];
350 
351  return intval( $form_id );
352  }
353 
354 
355  function add_scripts_and_styles( $hook ) {
356 
357  if( ! class_exists( 'GFForms' ) ) {
358  gravityview()->log->error( 'GFForms does not exist.' );
359  return;
360  }
361 
362  // enqueue styles & scripts gf_entries
363  // But only if we're on the main Entries page, not on reports pages
364  if( GFForms::get_page() !== 'entry_list' ) {
365  return;
366  }
367 
368  $form_id = $this->get_form_id();
369 
370  // Things are broken; no forms were found
371  if( empty( $form_id ) ) {
372  return;
373  }
374 
375  wp_enqueue_style( 'gravityview_entries_list', plugins_url('assets/css/admin-entries-list.css', GRAVITYVIEW_FILE), array(), GV_PLUGIN_VERSION );
376 
377  $script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
378 
379  wp_enqueue_script( 'gravityview_gf_entries_scripts', plugins_url('assets/js/admin-entries-list'.$script_debug.'.js', GRAVITYVIEW_FILE), array( 'jquery' ), GV_PLUGIN_VERSION );
380 
381  wp_enqueue_script( 'gravityview_entries_list-popper', plugins_url( 'assets/lib/tippy/popper.min.js', GRAVITYVIEW_FILE ), array(), GV_PLUGIN_VERSION );
382  wp_enqueue_script( 'gravityview_entries_list-tippy', plugins_url( 'assets/lib/tippy/tippy.min.js', GRAVITYVIEW_FILE ), array(), GV_PLUGIN_VERSION );
383  wp_enqueue_style( 'gravityview_entries_list-tippy', plugins_url( 'assets/lib/tippy/tippy.css', GRAVITYVIEW_FILE ), array(), GV_PLUGIN_VERSION );
384 
385  wp_localize_script( 'gravityview_gf_entries_scripts', 'gvGlobals', array(
386  'nonce' => wp_create_nonce( 'gravityview_entry_approval'),
387  'admin_nonce' => wp_create_nonce( 'gravityview_admin_entry_approval'),
388  'form_id' => $form_id,
389  'show_column' => (int)$this->show_approve_entry_column( $form_id ),
390  'add_bulk_action' => (int)GVCommon::has_cap( 'gravityview_moderate_entries' ),
391  'status_approved' => GravityView_Entry_Approval_Status::APPROVED,
392  'status_disapproved' => GravityView_Entry_Approval_Status::DISAPPROVED,
393  'status_unapproved' => GravityView_Entry_Approval_Status::UNAPPROVED,
395  'bulk_message' => $this->bulk_update_message,
396  'unapprove_title' => GravityView_Entry_Approval_Status::get_title_attr('unapproved'),
397  'approve_title' => GravityView_Entry_Approval_Status::get_title_attr('disapproved'),
398  'disapprove_title' => GravityView_Entry_Approval_Status::get_title_attr('approved'),
399  'column_title' => __( 'Show entry in directory view?', 'gk-gravityview'),
400  'column_link' => esc_url( $this->get_sort_link() ),
401  'status_popover_template' => GravityView_Entry_Approval::get_popover_template(),
402  'status_popover_placement' => GravityView_Entry_Approval::get_popover_placement(),
403  ) );
404 
405  }
406 
407  /**
408  * Generate a link to sort by approval status
409  *
410  * Note: Sorting by approval will never be great because it's not possible currently to declare the sorting as
411  * numeric, but it does group the approved entries together.
412  *
413  * @since 2.0.14 Remove need for approval field for sorting by approval status
414  *
415  * @param int $form_id [NO LONGER USED]
416  *
417  * @return string Sorting link
418  */
419  private function get_sort_link( $form_id = 0 ) {
420 
421  $args = array(
422  'orderby' => 'is_approved',
423  'order' => ( 'desc' === \GV\Utils::_GET( 'order' ) ) ? 'asc' : 'desc',
424  );
425 
426  return add_query_arg( $args );
427  }
428 
429  /**
430  * Should the Approve/Reject Entry column be shown in the GF Entries page?
431  *
432  * @since 1.7.2
433  *
434  * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
435  *
436  * @return bool True: Show column; False: hide column
437  */
438  private function show_approve_entry_column( $form_id ) {
439 
440  $show_approve_column = GVCommon::has_cap( 'gravityview_moderate_entries' );
441 
442  /**
443  * @filter `gravityview/approve_entries/hide-if-no-connections` Return true to hide reject/approve if there are no connected Views
444  * @since 1.7.2
445  * @param boolean $hide_if_no_connections
446  */
447  $hide_if_no_connections = apply_filters('gravityview/approve_entries/hide-if-no-connections', false );
448 
449  if( $hide_if_no_connections ) {
450 
451  $connected_views = gravityview_get_connected_views( $form_id );
452 
453  if( empty( $connected_views ) ) {
454  $show_approve_column = false;
455  }
456  }
457 
458  /**
459  * @filter `gravityview/approve_entries/show-column` Override whether the column is shown
460  * @param boolean $show_approve_column Whether the column will be shown
461  * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
462  */
463  $show_approve_column = apply_filters('gravityview/approve_entries/show-column', $show_approve_column, $form_id );
464 
465  return $show_approve_column;
466  }
467 
468  function register_gform_noconflict_script( $scripts ) {
469  $scripts[] = 'gravityview_gf_entries_scripts';
470  $scripts[] = 'gravityview_entries_list-popper';
471  $scripts[] = 'gravityview_entries_list-tippy';
472  return $scripts;
473  }
474 
475  function register_gform_noconflict_style( $styles ) {
476  $styles[] = 'gravityview_entries_list';
477  $styles[] = 'gravityview_entries_list-tippy';
478  return $styles;
479  }
480 
481 }
482 
static get_bulk_actions( $form_id)
Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" op...
const GV_PLUGIN_VERSION(! GravityKit\GravityView\Foundation\meets_min_php_version_requirement(__FILE__, '7.2.0'))
Constants.
Definition: gravityview.php:34
add_field_buttons( $field_groups)
Inject new add field buttons in the gravity form editor page.
get_first_form_id()
Get the first form ID from Gravity Forms, sorted in the same order as in the All Forms page...
static update_approved( $entry_id=0, $approved=0, $form_id=0, $approvedcolumn=0)
update_approved function.
$forms
Definition: data-source.php:19
static get_popover_placement()
Where should the popover be placed?
static get_approved_column( $form)
Calculate the approve field.input id.
if(gravityview() ->plugin->is_GF_25()) $form
static get_title_attr( $value_or_key)
Get the label for a specific approval value.
filter_links_entry_list( $filter_links=array(), $form=array(), $include_counts=true)
Add filter links to the Entries page.
get_sort_link( $form_id=0)
Generate a link to sort by approval status.
const APPROVED
gravityview_get_connected_views( $form_id, $args=array())
Get the views for a particular form.
static update_approved( $entry_id=0, $approved=2, $form_id=0, $approvedcolumn=0)
update_approved function.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:40
static get_entry_status( $entry, $value_or_label='label')
Get the approval status for an entry.
const DISAPPROVED
tooltips( $tooltips)
Add the GravityView Fields group tooltip.
get_form_id()
Get the form ID of the form currently being displayed.
if(empty( $created_by)) $form_id
const UNAPPROVED
static add_entry_approved_hidden_input( $form_id, $field_id, $value, $entry, $query_string)
Add a hidden input that is used in the Javascript to show approved/disapproved entries checkbox...
gravityview()
The main GravityView wrapper function.
gravityview_get_entry_ids( $form_id, $search_criteria=array())
Wrapper for the Gravity Forms GFFormsModel::search_lead_ids() method.
static get_popover_template()
Get HTML template for a popover used to display approval statuses.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
$entry
Definition: notes.php:27
set_defaults()
At edit form page, set the field Approve defaults.
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
show_approve_entry_column( $form_id)
Should the Approve/Reject Entry column be shown in the GF Entries page?
const meta_key
static get_label( $value_or_key)
Get the label for a specific approval value.
new GravityView_Admin_ApproveEntries