GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-bulk-actions.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-admin-bulk-actions.php
4  * @package GravityView
5  * @license GPL2+
6  * @author GravityView <[email protected]>
7  * @link https://gravityview.co
8  * @copyright Copyright 2021, Katz Web Services, Inc.
9  *
10  * @since 2.13.2
11  */
12 
13 /**
14  * Handles interactions between GravityView and Gravity Forms' Bulk Actions menu on the Entries screen.
15  *
16  * @since 2.13.2
17  */
19 
20  // hold notification messages
21  public static $bulk_update_message = '';
22 
23  /**
24  * @var array Set the prefixes here instead of spread across the class
25  */
26  private static $bulk_action_prefixes = array(
27  'approve' => 'gvapprove',
28  'disapprove' => 'gvdisapprove',
29  'unapprove' => 'gvunapprove',
30  );
31 
32  /**
33  * GravityView_Admin_Bulk_Actions constructor.
34  */
35  public function __construct() {
36 
37  if ( did_action( 'admin_init' ) ) {
38  $this->process_bulk_action();
39  } else {
40  // capture bulk actions
41  add_action( 'admin_init', array( $this, 'process_bulk_action' ) );
42  }
43  }
44 
45  /**
46  * Get the Bulk Action submitted value if it is a GravityView Approve/Unapprove action
47  *
48  * @since 1.17.1
49  * @since 2.13.2 Moved to GravityView_Bulk_Actions class.
50  *
51  * @return string|false If the bulk action was GravityView Approve/Unapprove, return the full string (gvapprove-16, gvunapprove-16). Otherwise, return false.
52  */
53  private function get_gv_bulk_action() {
54 
55  $gv_bulk_action = false;
56 
57  if( version_compare( GFForms::$version, '2.0', '>=' ) ) {
58  $bulk_action = ( '-1' !== \GV\Utils::_POST( 'action' ) ) ? \GV\Utils::_POST( 'action' ) : \GV\Utils::_POST( 'action2' );
59  } else {
60  // GF 1.9.x - Bulk action 2 is the bottom bulk action select form.
61  $bulk_action = \GV\Utils::_POST( 'bulk_action' ) ? \GV\Utils::_POST( 'bulk_action' ) : \GV\Utils::_POST( 'bulk_action2' );
62  }
63 
64  // Check the $bulk_action value against GV actions, see if they're the same. I hate strpos().
65  if( ! empty( $bulk_action ) && preg_match( '/^('. implode( '|', self::$bulk_action_prefixes ) .')/ism', $bulk_action ) ) {
66  $gv_bulk_action = $bulk_action;
67  }
68 
69  return $gv_bulk_action;
70  }
71 
72  /**
73  * Capture bulk actions - gf_entries table
74  *
75  * @uses GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
76  * @return void|boolean
77  */
78  public function process_bulk_action() {
79 
80  if ( ! is_admin() || ! class_exists( 'GFForms' ) || empty( $_POST ) ) {
81  return false;
82  }
83 
84  // The action is formatted like: gvapprove-16 or gvunapprove-16, where the first word is the name of the action and the second is the ID of the form.
85  $bulk_action = $this->get_gv_bulk_action();
86 
87  // gforms_entry_list is the nonce that confirms we're on the right page
88  // gforms_update_note is sent when bulk editing entry notes. We don't want to process then.
89  if ( ! ( $bulk_action && \GV\Utils::_POST( 'gforms_entry_list' ) && empty( $_POST['gforms_update_note'] ) ) ) {
90  return;
91  }
92 
93  check_admin_referer( 'gforms_entry_list', 'gforms_entry_list' );
94 
95  /**
96  * The extra '-' is to make sure that there are at *least* two items in array.
97  * @see https://github.com/katzwebservices/GravityView/issues/370
98  */
99  $bulk_action .= '-';
100 
101  list( $approved_status, $form_id ) = explode( '-', $bulk_action );
102 
103  if ( empty( $form_id ) ) {
104  gravityview()->log->error( 'Form ID is empty from parsing bulk action.', array( 'data' => $bulk_action ) );
105  return false;
106  }
107 
108  // All entries are set to be updated, not just the visible ones
109  if ( ! empty( $_POST['all_entries'] ) ) {
110 
111  // Convert the current entry search into GF-formatted search criteria
112  $search = array(
113  'search_field' => isset( $_POST['f'] ) ? $_POST['f'][0] : 0,
114  'search_value' => isset( $_POST['v'][0] ) ? $_POST['v'][0] : '',
115  'search_operator' => isset( $_POST['o'][0] ) ? $_POST['o'][0] : 'contains',
116  );
117 
118  $search_criteria = GravityView_frontend::get_search_criteria( $search, $form_id );
119 
120  // Get all the entry IDs for the form
121  $entries = gravityview_get_entry_ids( $form_id, $search_criteria );
122 
123  } else {
124 
125  // Changed from 'lead' to 'entry' in 2.0
126  $entries = isset( $_POST['lead'] ) ? $_POST['lead'] : $_POST['entry'];
127 
128  }
129 
130  if ( empty( $entries ) ) {
131  gravityview()->log->error( 'Entries are empty' );
132  return false;
133  }
134 
135  $entry_count = count( $entries ) > 1 ? sprintf( __( '%d entries', 'gk-gravityview' ), count( $entries ) ) : __( '1 entry', 'gk-gravityview' );
136 
137  switch ( $approved_status ) {
138  case self::$bulk_action_prefixes['approve']:
140  self::$bulk_update_message = sprintf( __( '%s approved.', 'gk-gravityview' ), $entry_count );
141  break;
142  case self::$bulk_action_prefixes['unapprove']:
144  self::$bulk_update_message = sprintf( __( '%s unapproved.', 'gk-gravityview' ), $entry_count );
145  break;
146  case self::$bulk_action_prefixes['disapprove']:
148  self::$bulk_update_message = sprintf( __( '%s disapproved.', 'gk-gravityview' ), $entry_count );
149  break;
150  }
151  }
152 
153 
154  /**
155  * Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" option group
156  *
157  * @since 1.16.3
158  *
159  * @param int $form_id ID of the form currently being displayed
160  *
161  * @return array Array of actions to be added to the GravityView option group
162  */
163  public static function get_bulk_actions( $form_id ) {
164 
165  $bulk_actions = array(
166  'GravityView' => array(
167  array(
168  'label' => GravityView_Entry_Approval_Status::get_string('approved', 'action'),
169  'value' => sprintf( '%s-%d', self::$bulk_action_prefixes['approve'], $form_id ),
170  ),
171  array(
172  'label' => GravityView_Entry_Approval_Status::get_string('disapproved', 'action'),
173  'value' => sprintf( '%s-%d', self::$bulk_action_prefixes['disapprove'], $form_id ),
174  ),
175  array(
176  'label' => GravityView_Entry_Approval_Status::get_string('unapproved', 'action'),
177  'value' => sprintf( '%s-%d', self::$bulk_action_prefixes['unapprove'], $form_id ),
178  ),
179  ),
180  );
181 
182  /**
183  * @filter `gravityview/approve_entries/bulk_actions` Modify the GravityView "Bulk action" dropdown list. Return an empty array to hide.
184  * @see https://gist.github.com/zackkatz/82785402c996b51b4dc9 for an example of how to use this filter
185  * @since 1.16.3
186  * @param array $bulk_actions Associative array of actions to be added to "Bulk action" dropdown inside GravityView `<optgroup>`. Parent array key is the `<optgroup>` label, then each child array must have `label` (displayed text) and `value` (input value) keys
187  * @param int $form_id ID of the form currently being displayed
188  */
189  $bulk_actions = apply_filters( 'gravityview/approve_entries/bulk_actions', $bulk_actions, $form_id );
190 
191  // Sanitize the values, just to be sure.
192  foreach ( $bulk_actions as $key => $group ) {
193 
194  if( empty( $group ) ) {
195  continue;
196  }
197 
198  foreach ( $group as $i => $action ) {
199  $bulk_actions[ $key ][ $i ]['label'] = esc_html( $bulk_actions[ $key ][ $i ]['label'] );
200  $bulk_actions[ $key ][ $i ]['value'] = esc_attr( $bulk_actions[ $key ][ $i ]['value'] );
201  }
202  }
203 
204  return $bulk_actions;
205  }
206 
207 }
208 
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...
process_bulk_action()
Capture bulk actions - gf_entries table.
$entries
static update_bulk( $entries=array(), $approved=0, $form_id=0)
Process a bulk of entries to update the approve field/property.
get_gv_bulk_action()
Get the Bulk Action submitted value if it is a GravityView Approve/Unapprove action.
static get_search_criteria( $args, $form_id)
Parse search criteria for a entries search.
const APPROVED
const DISAPPROVED
Handles interactions between GravityView and Gravity Forms&#39; Bulk Actions menu on the Entries screen...
static get_string( $value_or_key, $string_key='')
Get the label for a specific approval value.
if(empty( $created_by)) $form_id
const UNAPPROVED
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.
__construct()
GravityView_Admin_Bulk_Actions constructor.
static _POST( $name, $default=null)
Grab a value from the _POST superglobal or default.