Completed
Push — master ( 2cfa6f...8927a4 )
by Zack
10:00 queued 06:05
created

update_approved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 3
rs 10
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 627.

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
		'disapprove' => 'gvdisapprove',
27
		'unapprove' => 'gvunapprove',
28
	);
29
30
	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...
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
		// capture bulk actions
47
		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...
48
49
		// add hidden field with approve status
50
		add_action( 'gform_entries_first_column_actions', array( $this, 'add_entry_approved_hidden_input' ), 1, 5 );
51
52
		add_filter( 'gravityview_tooltips', array( $this, 'tooltips' ) );
53
54
		// adding styles and scripts
55
		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...
56
		// bypass Gravity Forms no-conflict mode
57
		add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict_script' ) );
58
		add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict_style' ) );
59
60
		add_filter( 'gform_filter_links_entry_list', array( $this, 'filter_links_entry_list' ), 10, 3 );
61
	}
62
63
	/**
64
	 * Add filter links to the Entries page
65
	 *
66
	 * Can be disabled by returning false on the `gravityview/approve_entries/show_filter_links_entry_list` filter
67
	 *
68
	 * @since 1.17.1
69
	 *
70
	 * @param array $filter_links Array of links to include in the subsubsub filter list. Includes `id`, `field_filters`, `count`, and `label` keys
71
	 * @param array $form GF Form object of current form
72
	 * @param bool $include_counts Whether to include counts in the output
73
	 *
74
	 * @return array Filter links, with GravityView approved/disapproved links added
75
	 */
76
	public function filter_links_entry_list( $filter_links = array(), $form = array(), $include_counts = true ) {
77
78
		/**
79
		 * @filter `gravityview/approve_entries/show_filter_links_entry_list` Disable filter links
80
		 * @since 1.17.1
81
		 * @param bool $show_filter_links True: show the "approved"/"disapproved" filter links. False: hide them.
82
		 * @param array $form GF Form object of current form
83
		 */
84
		if( false === apply_filters( 'gravityview/approve_entries/show_filter_links_entry_list', true, $form ) ) {
85
			return $filter_links;
86
		}
87
88
		$field_filters_approved = array(
89
			array(
90
				'key' => GravityView_Entry_Approval::meta_key,
91
				'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...
92
			),
93
		);
94
95
		$field_filters_disapproved = array(
96
			array(
97
				'key'      => GravityView_Entry_Approval::meta_key,
98
				'value'    => GravityView_Entry_Approval_Status::DISAPPROVED,
99
			),
100
		);
101
102
		$approved_count = $disapproved_count = 0;
103
104
		// Only count if necessary
105
		if( $include_counts ) {
106
			$approved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_approved ) ) );
107
			$disapproved_count = count( gravityview_get_entry_ids( $form['id'], array( 'status' => 'active', 'field_filters' => $field_filters_disapproved ) ) );
108
		}
109
110
		$filter_links[] = array(
111
			'id'            => 'gv_approved',
112
			'field_filters' => $field_filters_approved,
113
			'count'         => $approved_count,
114
			'label'         => GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::APPROVED ),
115
		);
116
117
		$filter_links[] = array(
118
			'id'            => 'gv_disapproved',
119
			'field_filters' => $field_filters_disapproved,
120
			'count'         => $disapproved_count,
121
			'label'         => GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::DISAPPROVED ),
122
		);
123
124
		return $filter_links;
125
	}
126
127
	/**
128
	 * Add the GravityView Fields group tooltip
129
	 *
130
	 * @param $tooltips
131
	 *
132
	 * @return array Tooltips array with GravityView fields tooltip
133
	 */
134
	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...
135
136
		$tooltips['form_gravityview_fields'] = array(
137
			'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...
138
			'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...
139
		);
140
141
		return $tooltips;
142
	}
143
144
145
	/**
146
	 * Inject new add field buttons in the gravity form editor page
147
	 *
148
	 * @access public
149
	 * @param mixed $field_groups
150
	 * @return array Array of fields
151
	 */
152
	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...
153
154
		$gravityview_fields = array(
155
			'name' => 'gravityview_fields',
156
			'label' => 'GravityView',
157
			'fields' => array(
158
				array(
159
					'class' => 'button',
160
					'value' => __( 'Approve/Reject', 'gravityview' ),
161
					'onclick' => "StartAddField('gravityviewapproved_admin');",
162
					'data-type' => 'gravityviewapproved_admin'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
163
				),
164
				array(
165
					'class' => 'button',
166
					'value' => __( 'User Opt-In', 'gravityview' ),
167
					'onclick' => "StartAddField('gravityviewapproved');",
168
					'data-type' => 'gravityviewapproved'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
169
				),
170
			)
171
		);
172
173
		array_push( $field_groups, $gravityview_fields );
174
175
		return $field_groups;
176
	}
177
178
179
180
	/**
181
	 * At edit form page, set the field Approve defaults
182
	 *
183
	 * @todo Convert to a partial include file
184
	 * @access public
185
	 * @return void
186
	 */
187
	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...
188
		?>
189
		case 'gravityviewapproved_admin':
190
			field.label = "<?php echo esc_js( __( 'Approved? (Admin-only)', 'gravityview' ) ); ?>";
191
192
			field.adminLabel = "<?php echo esc_js( __( 'Approved?', 'gravityview' ) ); ?>";
193
			field.adminOnly = true;
194
195
			field.choices = null;
196
			field.inputs = null;
197
198
			if( !field.choices ) {
199
				field.choices = new Array( new Choice("<?php echo esc_js( GravityView_Entry_Approval_Status::get_label( GravityView_Entry_Approval_Status::APPROVED ) ); ?>") );
200
			}
201
202
			field.inputs = new Array();
203
			for( var i=1; i<=field.choices.length; i++ ) {
204
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
205
			}
206
207
			field.type = 'checkbox';
208
			field.gravityview_approved = 1;
209
210
			break;
211
		case 'gravityviewapproved':
212
			field.label = "<?php echo esc_js( __( 'Show Entry on Website', 'gravityview' ) ); ?>";
213
214
			field.adminLabel = "<?php echo esc_js( __( 'Opt-In', 'gravityview' ) ); ?>";
215
			field.adminOnly = false;
216
217
			field.choices = null;
218
			field.inputs = null;
219
220
			if( !field.choices ) {
221
				field.choices = new Array(
222
					new Choice("<?php echo esc_js( __( 'Yes, display my entry on the website', 'gravityview' ) ); ?>")
223
				);
224
			}
225
226
			field.inputs = new Array();
227
			for( var i=1; i<=field.choices.length; i++ ) {
228
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
229
			}
230
231
			field.type = 'checkbox';
232
			field.gravityview_approved = 1;
233
234
			break;
235
		<?php
236
	}
237
238
	/**
239
	 * Get the Bulk Action submitted value if it is a GravityView Approve/Unapprove action
240
	 *
241
	 * @since 1.17.1
242
	 *
243
	 * @return string|false If the bulk action was GravityView Approve/Unapprove, return the full string (gvapprove-16, gvunapprove-16). Otherwise, return false.
244
	 */
245
	private function get_gv_bulk_action() {
246
247
		$gv_bulk_action = false;
248
249
		if( version_compare( GFForms::$version, '2.0', '>=' ) ) {
250
			$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...
251
		} else {
252
			// GF 1.9.x - Bulk action 2 is the bottom bulk action select form.
253
			$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...
254
		}
255
256
		// Check the $bulk_action value against GV actions, see if they're the same. I hate strpos().
257
		if( ! empty( $bulk_action ) && preg_match( '/^('. implode( '|', $this->bulk_action_prefixes ) .')/ism', $bulk_action ) ) {
258
			$gv_bulk_action = $bulk_action;
259
		}
260
261
		return $gv_bulk_action;
262
	}
263
264
	/**
265
	 * Capture bulk actions - gf_entries table
266
	 *
267
	 * @uses  GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
268
	 * @access public
269
	 * @return void|boolean
270
	 */
271
	public function process_bulk_action() {
272
273
		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...
274
			return false;
275
		}
276
277
		// 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.
278
		$bulk_action = $this->get_gv_bulk_action();
279
		
280
		// gforms_entry_list is the nonce that confirms we're on the right page
281
		// gforms_update_note is sent when bulk editing entry notes. We don't want to process then.
282
		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...
283
284
			check_admin_referer( 'gforms_entry_list', 'gforms_entry_list' );
285
286
			/**
287
			 * The extra '-' is to make sure that there are at *least* two items in array.
288
			 * @see https://github.com/katzwebservices/GravityView/issues/370
289
			 */
290
			$bulk_action .= '-';
291
292
			list( $approved_status, $form_id ) = explode( '-', $bulk_action );
293
294
			if ( empty( $form_id ) ) {
295
				do_action( 'gravityview_log_error', '[process_bulk_action] Form ID is empty from parsing bulk action.', $bulk_action );
296
				return false;
297
			}
298
299
			// All entries are set to be updated, not just the visible ones
300
			if ( ! empty( $_POST['all_entries'] ) ) {
301
302
				// Convert the current entry search into GF-formatted search criteria
303
				$search = array(
304
					'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...
305
					'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...
306
					'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...
307
				);
308
309
				$search_criteria = GravityView_frontend::get_search_criteria( $search, $form_id );
310
311
				// Get all the entry IDs for the form
312
				$entries = gravityview_get_entry_ids( $form_id, $search_criteria );
313
314
			} else {
315
316
				// Changed from 'lead' to 'entry' in 2.0
317
				$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...
318
319
			}
320
321
			if ( empty( $entries ) ) {
322
				do_action( 'gravityview_log_error', '[process_bulk_action] Entries are empty' );
323
				return false;
324
			}
325
326
			$entry_count = count( $entries ) > 1 ? sprintf( __( '%d entries', 'gravityview' ), count( $entries ) ) : __( '1 entry', 'gravityview' );
327
328
			switch ( $approved_status ) {
329
				case $this->bulk_action_prefixes['approve']:
330
					GravityView_Entry_Approval::update_bulk( $entries, GravityView_Entry_Approval_Status::APPROVED, $form_id );
331
					$this->bulk_update_message = sprintf( __( '%s approved.', 'gravityview' ), $entry_count );
332
					break;
333
				case $this->bulk_action_prefixes['unapprove']:
334
					GravityView_Entry_Approval::update_bulk( $entries, GravityView_Entry_Approval_Status::UNAPPROVED, $form_id );
335
					$this->bulk_update_message = sprintf( __( '%s unapproved.', 'gravityview' ), $entry_count );
336
					break;
337
				case $this->bulk_action_prefixes['disapprove']:
338
					GravityView_Entry_Approval::update_bulk( $entries, GravityView_Entry_Approval_Status::DISAPPROVED, $form_id );
339
					$this->bulk_update_message = sprintf( __( '%s disapproved.', 'gravityview' ), $entry_count );
340
					break;
341
			}
342
		}
343
	}
344
345
346
	/**
347
	 * update_approved function.
348
	 *
349
     * @since 1.18 Moved to GravityView_Entry_Approval::update_approved
350
	 * @see GravityView_Entry_Approval::update_approved
351
     *
352
	 * @param int $entry_id (default: 0)
353
	 * @param int $approved (default: 0)
354
	 * @param int $form_id (default: 0)
355
	 * @param int $approvedcolumn (default: 0)
356
     *
357
	 * @return boolean True: It worked; False: it failed
358
	 */
359
	public static function update_approved( $entry_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) {
360
		return GravityView_Entry_Approval::update_approved( $entry_id, $approved, $form_id, $approvedcolumn );
361
	}
362
363
	/**
364
	 * Calculate the approve field.input id
365
	 *
366
     * @since 1.18 Moved to GravityView_Entry_Approval::get_approved_column
367
     * @see GravityView_Entry_Approval::get_approved_column
368
     *
369
	 * @param mixed $form GF Form or Form ID
370
	 * @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.
371
	 */
372
	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...
373
		return GravityView_Entry_Approval::get_approved_column( $form );
374
	}
375
376
	/**
377
	 * Add a hidden input that is used in the Javascript to show approved/disapproved entries checkbox
378
	 *
379
	 * See the /assets/js/admin-entries-list.js setInitialApprovedEntries method
380
	 *
381
	 * @param $form_id
382
	 * @param $field_id
383
	 * @param $value
384
	 * @param $entry
385
	 * @param $query_string
386
	 *
387
	 * @return void
388
	 */
389
	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...
390
391
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $entry['id'] ) ) {
392
			return;
393
		}
394
395
		if( empty( $entry['id'] ) ) {
396
			return;
397
		}
398
399
		$status_value = GravityView_Entry_Approval::get_entry_status( $entry, 'value' );
400
401
		if( $status_value ) {
402
			echo '<input type="hidden" class="entry_approval" id="entry_approved_'. $entry['id'] .'" value="' . esc_attr( $status_value ) . '" />';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$entry'
Loading history...
403
		}
404
	}
405
406
	/**
407
	 * Get the form ID of the form currently being displayed
408
	 *
409
	 * @since 1.17.1
410
	 *
411
	 * @return int ID of the current form being displayed. `0` is returned if no forms are found.
412
	 */
413
	private function get_form_id() {
414
415
		$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...
416
417
		// If there are no forms identified, use the first form. That's how GF does it.
418
		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...
419
			$form_id = $this->get_first_form_id();
420
		}
421
422
		return absint( $form_id );
423
	}
424
425
	/**
426
	 * Get the first form ID from Gravity Forms, sorted in the same order as in the All Forms page
427
	 *
428
	 * @see GFEntryList::all_entries_page() This method is based on the form-selecting code here
429
	 *
430
	 * @since 1.17.2
431
	 *
432
	 * @return int ID of the first form, sorted by title. `0` if no forms were found.
433
	 */
434
	private function get_first_form_id() {
435
436
		$forms = RGFormsModel::get_forms( null, 'title' );
437
438
		if( ! isset( $forms[0] ) ) {
439
			do_action( 'gravityview_log_error', __METHOD__ . ': No forms were found' );
440
			return 0;
441
		}
442
443
		$first_form = $forms[0];
444
445
		$form_id = is_object( $forms[0] ) ? $first_form->id : $first_form['id'];
446
447
		return intval( $form_id );
448
	}
449
450
451
	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...
452
453
		if( ! class_exists( 'RGForms' ) ) {
454
455
			do_action( 'gravityview_log_error', 'GravityView_Admin_ApproveEntries[add_scripts_and_styles] RGForms does not exist.' );
456
457
			return;
458
		}
459
460
		// enqueue styles & scripts gf_entries
461
		// But only if we're on the main Entries page, not on reports pages
462
		if( GFForms::get_page() !== 'entry_list' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
463
			return;
464
		}
465
466
		$form_id = $this->get_form_id();
467
468
		// Things are broken; no forms were found
469
		if( empty( $form_id ) ) {
470
			return;
471
		}
472
473
		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...
474
475
		$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...
476
477
		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...
478
479
		wp_localize_script( 'gravityview_gf_entries_scripts', 'gvGlobals', array(
480
			'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...
481
			'form_id' => $form_id,
482
			'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...
483
			'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...
484
			'status_approved' => GravityView_Entry_Approval_Status::APPROVED,
485
			'status_disapproved' => GravityView_Entry_Approval_Status::DISAPPROVED,
486
			'status_unapproved' => GravityView_Entry_Approval_Status::UNAPPROVED,
487
			'bulk_actions' => $this->get_bulk_actions( $form_id ),
488
			'bulk_message' => $this->bulk_update_message,
489
			'unapprove_title' => GravityView_Entry_Approval_Status::get_title_attr('unapproved'),
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...
490
            'approve_title' => GravityView_Entry_Approval_Status::get_title_attr('disapproved'),
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...
491
			'disapprove_title' => GravityView_Entry_Approval_Status::get_title_attr('approved'),
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...
492
			'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...
493
			'column_link' => esc_url( $this->get_sort_link( $form_id ) ),
494
		) );
495
496
	}
497
498
	/**
499
     * Generate a link to sort by approval status (if there is an Approve/Disapprove field)
500
     *
501
     * Note: Sorting by approval will never be great because it's not possible currently to declare the sorting as
502
     * numeric, but it does group the approved entries together.
503
     *
504
	 * @param int $form_id
505
	 *
506
	 * @return string Sorting link
507
	 */
508
	private function get_sort_link( $form_id = 0 ) {
509
510
		$approved_column_id = self::get_approved_column( $form_id );
511
512
		if( ! $approved_column_id ) {
513
		    return '';
514
        }
515
516
	    $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...
517
518
	    $args = array(
519
		    'orderby' => $approved_column_id,
520
            'order' => $order,
521
        );
522
523
	    $link = add_query_arg( $args );
524
525
		return $link;
526
    }
527
528
	/**
529
	 * Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" option group
530
	 *
531
	 * @since 1.16.3
532
	 *
533
	 * @param int $form_id  ID of the form currently being displayed
534
	 *
535
	 * @return array Array of actions to be added to the GravityView option group
536
	 */
537
	private function get_bulk_actions( $form_id ) {
538
539
		$bulk_actions = array(
540
			'GravityView' => array(
541
				array(
542
					'label' => GravityView_Entry_Approval_Status::get_string('approved', 'action'),
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...
543
					'value' => sprintf( '%s-%d', $this->bulk_action_prefixes['approve'], $form_id ),
544
				),
545
				array(
546
					'label' => GravityView_Entry_Approval_Status::get_string('disapproved', 'action'),
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...
547
					'value' => sprintf( '%s-%d', $this->bulk_action_prefixes['disapprove'], $form_id ),
548
				),
549
				array(
550
					'label' => GravityView_Entry_Approval_Status::get_string('unapproved', 'action'),
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...
551
					'value' => sprintf( '%s-%d', $this->bulk_action_prefixes['unapprove'], $form_id ),
552
				),
553
			),
554
		);
555
556
		/**
557
		 * @filter `gravityview/approve_entries/bulk_actions` Modify the GravityView "Bulk action" dropdown list. Return an empty array to hide.
558
		 * @see https://gist.github.com/zackkatz/82785402c996b51b4dc9 for an example of how to use this filter
559
		 * @since 1.16.3
560
		 * @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
561
		 * @param int $form_id ID of the form currently being displayed
562
		 */
563
		$bulk_actions = apply_filters( 'gravityview/approve_entries/bulk_actions', $bulk_actions, $form_id );
564
565
		// Sanitize the values, just to be sure.
566
		foreach ( $bulk_actions as $key => $group ) {
567
			foreach ( $group as $i => $action ) {
568
				$bulk_actions[ $key ][ $i ]['label'] = esc_html( $bulk_actions[ $key ][ $i ]['label'] );
569
				$bulk_actions[ $key ][ $i ]['value'] = esc_attr( $bulk_actions[ $key ][ $i ]['value'] );
570
			}
571
		}
572
573
		return $bulk_actions;
574
	}
575
576
	/**
577
	 * Should the Approve/Reject Entry column be shown in the GF Entries page?
578
	 *
579
	 * @since 1.7.2
580
	 *
581
	 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
582
	 *
583
	 * @return bool True: Show column; False: hide column
584
	 */
585
	private function show_approve_entry_column( $form_id ) {
586
587
		$show_approve_column = GVCommon::has_cap( 'gravityview_moderate_entries' );
588
589
		/**
590
		 * @filter `gravityview/approve_entries/hide-if-no-connections` Return true to hide reject/approve if there are no connected Views
591
		 * @since 1.7.2
592
		 * @param boolean $hide_if_no_connections
593
		 */
594
		$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...
595
596
		if( $hide_if_no_connections ) {
597
598
			$connected_views = gravityview_get_connected_views( $form_id );
599
600
			if( empty( $connected_views ) ) {
601
				$show_approve_column = false;
602
			}
603
		}
604
605
		/**
606
		 * @filter `gravityview/approve_entries/show-column` Override whether the column is shown
607
		 * @param boolean $show_approve_column Whether the column will be shown
608
		 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
609
		 */
610
		$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...
611
612
		return $show_approve_column;
613
	}
614
615
	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...
616
		$scripts[] = 'gravityview_gf_entries_scripts';
617
		return $scripts;
618
	}
619
620
	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...
621
		$styles[] = 'gravityview_entries_list';
622
		return $styles;
623
	}
624
625
}
626
627
new GravityView_Admin_ApproveEntries;
628