Completed
Push — develop ( 1b9b3e...5c91dc )
by Zack
04:18
created

get_bulk_actions()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 615.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @file class-admin-approve-entries.php
4
 * @package   GravityView
5
 * @license   GPL2+
6
 * @author    Katz Web Services, Inc.
7
 * @link      http://gravityview.co
8
 * @copyright Copyright 2014, Katz Web Services, Inc.
9
 *
10
 * @since 1.0.0
11
 */
12
13
14
15
class GravityView_Admin_ApproveEntries {
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
		'unapprove' => 'gvunapprove',
27
	);
28
29
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
31
		$this->add_hooks();
32
33
	}
34
35
	private function add_hooks() {
36
		/** Edit Gravity Form page */
37
38
		// Add button to left menu
39
		add_filter( 'gform_add_field_buttons', array( $this, 'add_field_buttons' ) );
40
		// Set defaults
41
		add_action( 'gform_editor_js_set_default_values', array( $this, 'set_defaults' ) );
42
43
		/** gf_entries page - entries table screen */
44
45
		// capture bulk actions
46
		add_action( 'gform_loaded', array( $this, 'process_bulk_action') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
47
48
		// add hidden field with approve status
49
		add_action( 'gform_entries_first_column_actions', array( $this, 'add_entry_approved_hidden_input' ), 1, 5 );
50
51
		add_filter( 'gravityview_tooltips', array( $this, 'tooltips' ) );
52
53
		// adding styles and scripts
54
		add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
55
		// bypass Gravity Forms no-conflict mode
56
		add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict_script' ) );
57
		add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict_style' ) );
58
59
		add_filter( 'gform_filter_links_entry_list', array( $this, 'filter_links_entry_list' ), 10, 3 );
60
	}
61
62
	/**
63
	 * Add filter links to the Entries page
64
	 *
65
	 * Can be disabled by returning false on the `gravityview/approve_entries/show_filter_links_entry_list` filter
66
	 *
67
	 * @since 1.17.1
68
	 *
69
	 * @param array $filter_links Array of links to include in the subsubsub filter list. Includes `id`, `field_filters`, `count`, and `label` keys
70
	 * @param array $form GF Form object of current form
71
	 * @param bool $include_counts Whether to include counts in the output
72
	 *
73
	 * @return array Filter links, with GravityView approved/disapproved links added
74
	 */
75
	public function filter_links_entry_list( $filter_links = array(), $form = array(), $include_counts = true ) {
76
77
		/**
78
		 * @filter `gravityview/approve_entries/show_filter_links_entry_list` Disable filter links
79
		 * @since 1.17.1
80
		 * @param bool $show_filter_links True: show the "approved"/"disapproved" filter links. False: hide them.
81
		 * @param array $form GF Form object of current form
82
		 */
83
		if( false === apply_filters( 'gravityview/approve_entries/show_filter_links_entry_list', true, $form ) ) {
84
			return $filter_links;
85
		}
86
87
		$field_filters_approved = array(
88
			array(
89
				'key' => GravityView_Entry_Approval::meta_key,
90
				'value' => GravityView_Entry_Approval_Status::Approved
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
91
			),
92
		);
93
94
		$field_filters_disapproved = array(
95
			array(
96
				'key'      => GravityView_Entry_Approval::meta_key,
97
				'value'    => GravityView_Entry_Approval_Status::Disapproved,
98
			),
99
		);
100
101
		$approved_count = $disapproved_count = 0;
102
103
		// Only count if necessary
104
		if( $include_counts ) {
105
			$approved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_approved ) ) );
106
			$disapproved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_disapproved ) ) );
107
		}
108
109
		$filter_links[] = array(
110
			'id'            => 'gv_approved',
111
			'field_filters' => $field_filters_approved,
112
			'count'         => $approved_count,
113
			'label'         => GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::Approved ),
114
		);
115
116
		$filter_links[] = array(
117
			'id'            => 'gv_disapproved',
118
			'field_filters' => $field_filters_disapproved,
119
			'count'         => $disapproved_count,
120
			'label'         => GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::Disapproved ),
121
		);
122
123
		return $filter_links;
124
	}
125
126
	/**
127
	 * Add the GravityView Fields group tooltip
128
	 *
129
	 * @param $tooltips
130
	 *
131
	 * @return array Tooltips array with GravityView fields tooltip
132
	 */
133
	function tooltips( $tooltips ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
135
		$tooltips['form_gravityview_fields'] = array(
136
			'title' => __('GravityView Fields', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
137
			'value' => __( 'Allow administrators to approve or reject entries and users to opt-in or opt-out of their entries being displayed.', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
138
		);
139
140
		return $tooltips;
141
	}
142
143
144
	/**
145
	 * Inject new add field buttons in the gravity form editor page
146
	 *
147
	 * @access public
148
	 * @param mixed $field_groups
149
	 * @return array Array of fields
150
	 */
151
	function add_field_buttons( $field_groups ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
153
		$gravityview_fields = array(
154
			'name' => 'gravityview_fields',
155
			'label' => 'GravityView',
156
			'fields' => array(
157
				array(
158
					'class' => 'button',
159
					'value' => __( 'Approve/Reject', 'gravityview' ),
160
					'onclick' => "StartAddField('gravityviewapproved_admin');",
161
					'data-type' => 'gravityviewapproved_admin'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
162
				),
163
				array(
164
					'class' => 'button',
165
					'value' => __( 'User Opt-In', 'gravityview' ),
166
					'onclick' => "StartAddField('gravityviewapproved');",
167
					'data-type' => 'gravityviewapproved'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
168
				),
169
			)
170
		);
171
172
		array_push( $field_groups, $gravityview_fields );
173
174
		return $field_groups;
175
	}
176
177
178
179
	/**
180
	 * At edit form page, set the field Approve defaults
181
	 *
182
	 * @todo Convert to a partial include file
183
	 * @access public
184
	 * @return void
185
	 */
186
	function set_defaults() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
187
		?>
188
		case 'gravityviewapproved_admin':
189
			field.label = "<?php echo esc_js( __( 'Approved? (Admin-only)', 'gravityview' ) ); ?>";
190
191
			field.adminLabel = "<?php echo esc_js( __( 'Approved?', 'gravityview' ) ); ?>";
192
			field.adminOnly = true;
193
194
			field.choices = null;
195
			field.inputs = null;
196
197
			if( !field.choices ) {
198
				field.choices = new Array( new Choice("<?php echo esc_js( GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::Approved ) ); ?>") );
199
			}
200
201
			field.inputs = new Array();
202
			for( var i=1; i<=field.choices.length; i++ ) {
203
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
204
			}
205
206
			field.type = 'checkbox';
207
			field.gravityview_approved = 1;
208
209
			break;
210
		case 'gravityviewapproved':
211
			field.label = "<?php echo esc_js( __( 'Show Entry on Website', 'gravityview' ) ); ?>";
212
213
			field.adminLabel = "<?php echo esc_js( __( 'Opt-In', 'gravityview' ) ); ?>";
214
			field.adminOnly = false;
215
216
			field.choices = null;
217
			field.inputs = null;
218
219
			if( !field.choices ) {
220
				field.choices = new Array(
221
					new Choice("<?php echo esc_js( __( 'Yes, display my entry on the website', 'gravityview' ) ); ?>")
222
				);
223
			}
224
225
			field.inputs = new Array();
226
			for( var i=1; i<=field.choices.length; i++ ) {
227
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
228
			}
229
230
			field.type = 'checkbox';
231
			field.gravityview_approved = 1;
232
233
			break;
234
		<?php
235
	}
236
237
	/**
238
	 * Get the Bulk Action submitted value if it is a GravityView Approve/Unapprove action
239
	 *
240
	 * @since 1.17.1
241
	 *
242
	 * @return string|false If the bulk action was GravityView Approve/Unapprove, return the full string (gvapprove-16, gvunapprove-16). Otherwise, return false.
243
	 */
244
	private function get_gv_bulk_action() {
245
246
		$gv_bulk_action = false;
247
248
		if( version_compare( GFForms::$version, '2.0', '>=' ) ) {
249
			$bulk_action = ( '-1' !== rgpost('action') ) ? rgpost('action') : rgpost('action2');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
250
		} else {
251
			// GF 1.9.x - Bulk action 2 is the bottom bulk action select form.
252
			$bulk_action = rgpost('bulk_action') ? rgpost('bulk_action') : rgpost('bulk_action2');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
253
		}
254
255
		// Check the $bulk_action value against GV actions, see if they're the same. I hate strpos().
256
		if( ! empty( $bulk_action ) && preg_match( '/^('. implode( '|', $this->bulk_action_prefixes ) .')/ism', $bulk_action ) ) {
257
			$gv_bulk_action = $bulk_action;
258
		}
259
260
		return $gv_bulk_action;
261
	}
262
263
	/**
264
	 * Capture bulk actions - gf_entries table
265
	 *
266
	 * @uses  GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
267
	 * @access public
268
	 * @return void|boolean
269
	 */
270
	public function process_bulk_action() {
271
272
		if ( ! is_admin() || ! class_exists( 'GFForms' ) || empty( $_POST ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
273
			return false;
274
		}
275
276
		// 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.
277
		$bulk_action = $this->get_gv_bulk_action();
278
		
279
		// gforms_entry_list is the nonce that confirms we're on the right page
280
		// gforms_update_note is sent when bulk editing entry notes. We don't want to process then.
281
		if ( $bulk_action && rgpost('gforms_entry_list') && empty( $_POST['gforms_update_note'] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bulk_action of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
282
283
			check_admin_referer( 'gforms_entry_list', 'gforms_entry_list' );
284
285
			/**
286
			 * The extra '-' is to make sure that there are at *least* two items in array.
287
			 * @see https://github.com/katzwebservices/GravityView/issues/370
288
			 */
289
			$bulk_action .= '-';
290
291
			list( $approved_status, $form_id ) = explode( '-', $bulk_action );
292
293
			if ( empty( $form_id ) ) {
294
				do_action( 'gravityview_log_error', '[process_bulk_action] Form ID is empty from parsing bulk action.', $bulk_action );
295
				return false;
296
			}
297
298
			// All entries are set to be updated, not just the visible ones
299
			if ( ! empty( $_POST['all_entries'] ) ) {
300
301
				// Convert the current entry search into GF-formatted search criteria
302
				$search = array(
303
					'search_field' => isset( $_POST['f'] ) ? $_POST['f'][0] : 0,
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
304
					'search_value' => isset( $_POST['v'][0] ) ? $_POST['v'][0] : '',
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
305
					'search_operator' => isset( $_POST['o'][0] ) ? $_POST['o'][0] : 'contains',
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
306
				);
307
308
				$search_criteria = GravityView_frontend::get_search_criteria( $search, $form_id );
309
310
				// Get all the entry IDs for the form
311
				$entries = gravityview_get_entry_ids( $form_id, $search_criteria );
312
313
			} else {
314
315
				// Changed from 'lead' to 'entry' in 2.0
316
				$entries = isset( $_POST['lead'] ) ? $_POST['lead'] : $_POST['entry'];
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
317
318
			}
319
320
			if ( empty( $entries ) ) {
321
				do_action( 'gravityview_log_error', '[process_bulk_action] Entries are empty' );
322
				return false;
323
			}
324
325
			$entry_count = count( $entries ) > 1 ? sprintf( __( '%d entries', 'gravityview' ), count( $entries ) ) : __( '1 entry', 'gravityview' );
326
327
			switch ( $approved_status ) {
328
				case $this->bulk_action_prefixes['approve']:
329
					GravityView_Entry_Approval::update_bulk( $entries, 1, $form_id );
330
					$this->bulk_update_message = sprintf( __( '%s approved.', 'gravityview' ), $entry_count );
331
					break;
332
333
				case $this->bulk_action_prefixes['unapprove']:
334
					GravityView_Entry_Approval::update_bulk( $entries, 0, $form_id );
335
					$this->bulk_update_message = sprintf( __( '%s disapproved.', 'gravityview' ), $entry_count );
336
					break;
337
			}
338
		}
339
	}
340
341
342
	/**
343
	 * update_approved function.
344
	 *
345
     * @since 1.18 Moved to GravityView_Entry_Approval::update_approved
346
	 * @see GravityView_Entry_Approval::update_approved
347
     *
348
	 * @param int $entry_id (default: 0)
349
	 * @param int $approved (default: 0)
350
	 * @param int $form_id (default: 0)
351
	 * @param int $approvedcolumn (default: 0)
352
     *
353
	 * @return boolean True: It worked; False: it failed
354
	 */
355
	public static function update_approved( $entry_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) {
356
		return GravityView_Entry_Approval::update_approved( $entry_id, $approved, $form_id, $approvedcolumn );
357
	}
358
359
	/**
360
	 * Calculate the approve field.input id
361
	 *
362
     * @since 1.18 Moved to GravityView_Entry_Approval::get_approved_column
363
     * @see GravityView_Entry_Approval::get_approved_column
364
     *
365
	 * @param mixed $form GF Form or Form ID
366
	 * @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.
367
	 */
368
	static public function get_approved_column( $form ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
369
		return GravityView_Entry_Approval::get_approved_column( $form );
370
	}
371
372
	/**
373
	 * Add a hidden input that is used in the Javascript to show approved/disapproved entries checkbox
374
	 *
375
	 * See the /assets/js/admin-entries-list.js setInitialApprovedEntries method
376
	 *
377
	 * @param $form_id
378
	 * @param $field_id
379
	 * @param $value
380
	 * @param $entry
381
	 * @param $query_string
382
	 *
383
	 * @return void
384
	 */
385
	static public function add_entry_approved_hidden_input(  $form_id, $field_id, $value, $entry, $query_string ) {
0 ignored issues
show
Unused Code introduced by
The parameter $query_string is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
386
387
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $entry['id'] ) ) {
388
			return;
389
		}
390
391
		if( empty( $entry['id'] ) ) {
392
			return;
393
		}
394
395
		if( gform_get_meta( $entry['id'], GravityView_Entry_Approval::meta_key ) ) {
396
			echo '<input type="hidden" class="entry_approved" id="entry_approved_'. $entry['id'] .'" value="true" />';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$entry'
Loading history...
397
		}
398
	}
399
400
	/**
401
	 * Get the form ID of the form currently being displayed
402
	 *
403
	 * @since 1.17.1
404
	 *
405
	 * @return int ID of the current form being displayed. `0` is returned if no forms are found.
406
	 */
407
	private function get_form_id() {
408
409
		$form_id = GFForms::get('id');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
410
411
		// If there are no forms identified, use the first form. That's how GF does it.
412
		if( empty( $form_id ) && class_exists('RGFormsModel') ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
413
			$form_id = $this->get_first_form_id();
414
		}
415
416
		return absint( $form_id );
417
	}
418
419
	/**
420
	 * Get the first form ID from Gravity Forms, sorted in the same order as in the All Forms page
421
	 *
422
	 * @see GFEntryList::all_entries_page() This method is based on the form-selecting code here
423
	 *
424
	 * @since 1.17.2
425
	 *
426
	 * @return int ID of the first form, sorted by title. `0` if no forms were found.
427
	 */
428
	private function get_first_form_id() {
429
430
		$forms = RGFormsModel::get_forms( null, 'title' );
431
432
		if( ! isset( $forms[0] ) ) {
433
			do_action( 'gravityview_log_error', __METHOD__ . ': No forms were found' );
434
			return 0;
435
		}
436
437
		$first_form = $forms[0];
438
439
		$form_id = is_object( $forms[0] ) ? $first_form->id : $first_form['id'];
440
441
		return intval( $form_id );
442
	}
443
444
445
	function add_scripts_and_styles( $hook ) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
446
447
		if( ! class_exists( 'RGForms' ) ) {
448
449
			do_action( 'gravityview_log_error', 'GravityView_Admin_ApproveEntries[add_scripts_and_styles] RGForms does not exist.' );
450
451
			return;
452
		}
453
454
		// enqueue styles & scripts gf_entries
455
		// But only if we're on the main Entries page, not on reports pages
456
		if( GFForms::get_page() !== 'entry_list' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
457
			return;
458
		}
459
460
		$form_id = $this->get_form_id();
461
462
		// Things are broken; no forms were found
463
		if( empty( $form_id ) ) {
464
			return;
465
		}
466
467
		wp_enqueue_style( 'gravityview_entries_list', plugins_url('assets/css/admin-entries-list.css', GRAVITYVIEW_FILE), array(), GravityView_Plugin::version );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
468
469
		$script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
470
471
		wp_enqueue_script( 'gravityview_gf_entries_scripts', plugins_url('assets/js/admin-entries-list'.$script_debug.'.js', GRAVITYVIEW_FILE), array( 'jquery' ), GravityView_Plugin::version );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
472
473
		wp_localize_script( 'gravityview_gf_entries_scripts', 'gvGlobals', array(
474
			'nonce' => wp_create_nonce( 'gravityview_entry_approval'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
475
			'form_id' => $form_id,
476
			'show_column' => (int)$this->show_approve_entry_column( $form_id ),
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
477
			'add_bulk_action' => (int)GVCommon::has_cap( 'gravityview_moderate_entries' ),
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
478
			'status_approved' => GravityView_Entry_Approval_Status::Approved,
479
			'status_disapproved' => GravityView_Entry_Approval_Status::Disapproved,
480
			'bulk_actions' => $this->get_bulk_actions( $form_id ),
481
			'bulk_message' => $this->bulk_update_message,
482
			'approve_title' => __( 'Entry not approved for directory viewing. Click to approve this entry.', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
483
			'unapprove_title' => __( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
484
			'column_title' => __( 'Show entry in directory view?', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
485
			'column_link' => esc_url( $this->get_sort_link( $form_id ) ),
486
		) );
487
488
	}
489
490
	/**
491
     * Generate a link to sort by approval status (if there is an Approve/Disapprove field)
492
     *
493
     * Note: Sorting by approval will never be great because it's not possible currently to declare the sorting as
494
     * numeric, but it does group the approved entries together.
495
     *
496
	 * @param int $form_id
497
	 *
498
	 * @return string Sorting link
499
	 */
500
	private function get_sort_link( $form_id = 0 ) {
501
502
		$approved_column_id = self::get_approved_column( $form_id );
503
504
		if( ! $approved_column_id ) {
505
		    return '';
506
        }
507
508
	    $order = ( 'desc' === rgget('order') ) ? 'asc' : 'desc';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
509
510
	    $args = array(
511
		    'orderby' => $approved_column_id,
512
            'order' => $order,
513
        );
514
515
	    $link = add_query_arg( $args );
516
517
		return $link;
518
    }
519
520
	/**
521
	 * Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" option group
522
	 *
523
	 * @since 1.16.3
524
	 *
525
	 * @param int $form_id  ID of the form currently being displayed
526
	 *
527
	 * @return array Array of actions to be added to the GravityView option group
528
	 */
529
	private function get_bulk_actions( $form_id ) {
530
531
		$bulk_actions = array(
532
			'GravityView' => array(
533
				array(
534
					'label' => __( 'Approve', 'gravityview' ),
535
					'value' => sprintf( '%s-%d', $this->bulk_action_prefixes['approve'], $form_id ),
536
				),
537
				array(
538
					'label' => __( 'Disapprove', 'gravityview' ),
539
					'value' => sprintf( '%s-%d', $this->bulk_action_prefixes['unapprove'], $form_id ),
540
				),
541
			),
542
		);
543
544
		/**
545
		 * @filter `gravityview/approve_entries/bulk_actions` Modify the GravityView "Bulk action" dropdown list. Return an empty array to hide.
546
		 * @see https://gist.github.com/zackkatz/82785402c996b51b4dc9 for an example of how to use this filter
547
		 * @since 1.16.3
548
		 * @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
549
		 * @param int $form_id ID of the form currently being displayed
550
		 */
551
		$bulk_actions = apply_filters( 'gravityview/approve_entries/bulk_actions', $bulk_actions, $form_id );
552
553
		// Sanitize the values, just to be sure.
554
		foreach ( $bulk_actions as $key => $group ) {
555
			foreach ( $group as $i => $action ) {
556
				$bulk_actions[ $key ][ $i ]['label'] = esc_html( $bulk_actions[ $key ][ $i ]['label'] );
557
				$bulk_actions[ $key ][ $i ]['value'] = esc_attr( $bulk_actions[ $key ][ $i ]['value'] );
558
			}
559
		}
560
561
		return $bulk_actions;
562
	}
563
564
	/**
565
	 * Should the Approve/Reject Entry column be shown in the GF Entries page?
566
	 *
567
	 * @since 1.7.2
568
	 *
569
	 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
570
	 *
571
	 * @return bool True: Show column; False: hide column
572
	 */
573
	private function show_approve_entry_column( $form_id ) {
574
575
		$show_approve_column = GVCommon::has_cap( 'gravityview_moderate_entries' );
576
577
		/**
578
		 * @filter `gravityview/approve_entries/hide-if-no-connections` Return true to hide reject/approve if there are no connected Views
579
		 * @since 1.7.2
580
		 * @param boolean $hide_if_no_connections
581
		 */
582
		$hide_if_no_connections = apply_filters('gravityview/approve_entries/hide-if-no-connections', false );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
583
584
		if( $hide_if_no_connections ) {
585
586
			$connected_views = gravityview_get_connected_views( $form_id );
587
588
			if( empty( $connected_views ) ) {
589
				$show_approve_column = false;
590
			}
591
		}
592
593
		/**
594
		 * @filter `gravityview/approve_entries/show-column` Override whether the column is shown
595
		 * @param boolean $show_approve_column Whether the column will be shown
596
		 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
597
		 */
598
		$show_approve_column = apply_filters('gravityview/approve_entries/show-column', $show_approve_column, $form_id );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
599
600
		return $show_approve_column;
601
	}
602
603
	function register_gform_noconflict_script( $scripts ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
604
		$scripts[] = 'gravityview_gf_entries_scripts';
605
		return $scripts;
606
	}
607
608
	function register_gform_noconflict_style( $styles ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
609
		$styles[] = 'gravityview_entries_list';
610
		return $styles;
611
	}
612
613
}
614
615
new GravityView_Admin_ApproveEntries;
616