Completed
Pull Request — master (#690)
by
unknown
04:31
created

after_submission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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 705.

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
	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...
21
22
		$this->add_hooks();
23
24
	}
25
26
	private function add_hooks() {
27
		/** Edit Gravity Form page */
28
29
		// Add button to left menu
30
		add_filter( 'gform_add_field_buttons', array( $this, 'add_field_buttons' ) );
31
		// Set defaults
32
		add_action( 'gform_editor_js_set_default_values', array( $this, 'set_defaults' ) );
33
34
		/** gf_entries page - entries table screen */
35
36
		// capture bulk actions
37
		add_action( 'init', array( $this, 'process_bulk_action') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
38
		// add hidden field with approve status
39
		add_action( 'gform_entries_first_column', array( $this, 'add_entry_approved_hidden_input' ), 1, 5 );
40
		// process ajax approve entry requests
41
		add_action('wp_ajax_gv_update_approved', array( $this, 'ajax_update_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...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
42
43
		// when using the User opt-in field, check on entry submission
44
		add_action( 'gform_after_submission', array( $this, 'after_submission' ), 10, 2 );
45
46
		// in case entry is edited (on admin or frontend)
47
		add_action( 'gform_after_update_entry', array( $this, 'after_update_entry_update_approved_meta' ), 10, 2);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
48
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
49
50
		add_filter( 'gravityview_tooltips', array( $this, 'tooltips' ) );
51
52
		// adding styles and scripts
53
		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...
54
		// bypass Gravity Forms no-conflict mode
55
		add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict_script' ) );
56
		add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict_style' ) );
57
	}
58
59
	/**
60
	 * Add the GravityView Fields group tooltip
61
	 *
62
	 * @param $tooltips
63
	 *
64
	 * @return array Tooltips array with GravityView fields tooltip
65
	 */
66
	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...
67
68
		$tooltips['form_gravityview_fields'] = array(
69
			'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...
70
			'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...
71
		);
72
73
		return $tooltips;
74
	}
75
76
77
	/**
78
	 * Inject new add field buttons in the gravity form editor page
79
	 *
80
	 * @access public
81
	 * @param mixed $field_groups
82
	 * @return array Array of fields
83
	 */
84
	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...
85
86
		$gravityview_fields = array(
87
			'name' => 'gravityview_fields',
88
			'label' => 'GravityView',
89
			'fields' => array(
90
				array(
91
					'class' => 'button',
92
					'value' => __( 'Approve/Reject', 'gravityview' ),
93
					'onclick' => "StartAddField('gravityviewapproved_admin');",
94
					'data-type' => 'gravityviewapproved_admin'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
95
				),
96
				array(
97
					'class' => 'button',
98
					'value' => __( 'User Opt-In', 'gravityview' ),
99
					'onclick' => "StartAddField('gravityviewapproved');",
100
					'data-type' => 'gravityviewapproved'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
101
				),
102
			)
103
		);
104
105
		array_push( $field_groups, $gravityview_fields );
106
107
		return $field_groups;
108
	}
109
110
111
112
	/**
113
	 * At edit form page, set the field Approve defaults
114
	 *
115
	 * @todo Convert to a partial include file
116
	 * @access public
117
	 * @return void
118
	 */
119
	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...
120
		?>
121
		case 'gravityviewapproved_admin':
122
			field.label = "<?php _e( 'Approved? (Admin-only)', 'gravityview' ); ?>";
123
124
			field.adminLabel = "<?php _e( 'Approved?', 'gravityview' ); ?>";
125
			field.adminOnly = true;
126
127
			field.choices = null;
128
			field.inputs = null;
129
130
			if( !field.choices ) {
131
				field.choices = new Array( new Choice("<?php _e( 'Approved', 'gravityview' ); ?>") );
132
			}
133
134
			field.inputs = new Array();
135
			for( var i=1; i<=field.choices.length; i++ ) {
136
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
137
			}
138
139
			field.type = 'checkbox';
140
			field.gravityview_approved = 1;
141
142
			break;
143
		case 'gravityviewapproved':
144
			field.label = "<?php _e( 'Show Entry on Website', 'gravityview' ); ?>";
145
146
			field.adminLabel = "<?php _e( 'Opt-In', 'gravityview' ); ?>";
147
			field.adminOnly = false;
148
149
			field.choices = null;
150
			field.inputs = null;
151
152
			if( !field.choices ) {
153
				field.choices = new Array(
154
					new Choice("<?php _e( 'Yes, display my entry on the website', 'gravityview' ); ?>")
155
				);
156
			}
157
158
			field.inputs = new Array();
159
			for( var i=1; i<=field.choices.length; i++ ) {
160
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
161
			}
162
163
			field.type = 'checkbox';
164
			field.gravityview_approved = 1;
165
166
			break;
167
		<?php
168
	}
169
170
171
172
	/**
173
	 * Capture bulk actions - gf_entries table
174
	 *
175
	 * @uses  GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
176
	 * @access public
177
	 * @return void|boolean
178
	 */
179
	public function process_bulk_action() {
180
		if ( ! class_exists( 'RGForms' ) ) {
181
			return;
182
		}
183
184
		// gforms_update_note is sent when bulk editing entry notes. We don't want to process then.
185
		if ( 'bulk' === RGForms::post( 'action' ) && empty( $_POST['gforms_update_note'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
186
187
			check_admin_referer( 'gforms_entry_list', 'gforms_entry_list' );
188
189
			// The action is formatted like: approve-16 or disapprove-16, where the first word is the name of the action and the second is the ID of the form. Bulk action 2 is the bottom bulk action select form.
190
			$bulk_action = ! empty( $_POST['bulk_action'] ) ? $_POST['bulk_action'] : $_POST['bulk_action2'];
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...
191
192
			/**
193
			 * The extra '-' is to make sure that there are at *least* two items in array.
194
			 * @see https://github.com/katzwebservices/GravityView/issues/370
195
			 */
196
			$bulk_action .= '-';
197
198
			list( $approved_status, $form_id ) = explode( '-', $bulk_action );
199
200
			if ( empty( $form_id ) ) {
201
				do_action( 'gravityview_log_error', '[process_bulk_action] Form ID is empty from parsing bulk action.', $bulk_action );
202
				return false;
203
			}
204
205
			// All entries are set to be updated, not just the visible ones
206
			if ( ! empty( $_POST['all_entries'] ) ) {
207
208
				// Convert the current entry search into GF-formatted search criteria
209
				$search = array(
210
					'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...
211
					'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...
212
					'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...
213
				);
214
215
				$search_criteria = GravityView_frontend::get_search_criteria( $search, $form_id );
216
217
				// Get all the entry IDs for the form
218
				$entries = gravityview_get_entry_ids( $form_id, $search_criteria );
219
220
			} else {
221
222
				$entries = $_POST['lead'];
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...
223
224
			}
225
226
			if ( empty( $entries ) ) {
227
				do_action( 'gravityview_log_error', '[process_bulk_action] Entries are empty' );
228
				return false;
229
			}
230
231
			$entry_count = count( $entries ) > 1 ? sprintf( __( '%d entries', 'gravityview' ), count( $entries ) ) : __( '1 entry', 'gravityview' );
232
233
			switch ( $approved_status ) {
234
				case 'approve':
235
					self::update_bulk( $entries, 1, $form_id );
236
					$this->bulk_update_message = sprintf( __( '%s approved.', 'gravityview' ), $entry_count );
237
					break;
238
239
				case 'unapprove':
240
					self::update_bulk( $entries, 0, $form_id );
241
					$this->bulk_update_message = sprintf( __( '%s disapproved.', 'gravityview' ), $entry_count );
242
					break;
243
			}
244
		}
245
	}
246
247
248
249
250
251
	/**
252
	 * Process a bulk of entries to update the approve field/property
253
	 *
254
	 * @access private
255
	 * @static
256
	 * @param array|boolean $entries If array, array of entry IDs that are to be updated. If true: update all entries.
257
	 * @param int $approved Approved status. If `0`: unapproved, if not empty, `Approved`
258
	 * @param int $form_id The Gravity Forms Form ID
259
	 * @return boolean|void
260
	 */
261
	private static function update_bulk( $entries, $approved, $form_id ) {
262
263
		if( empty($entries) || ( $entries !== true && !is_array($entries) ) ) {
0 ignored issues
show
introduced by
Found "!== true". Use Yoda Condition checks, you must
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
Expected 1 space after "!"; 0 found
Loading history...
264
			do_action( 'gravityview_log_error', __METHOD__ . ' Entries were empty or malformed.', $entries );
265
			return false;
266
		}
267
268
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries' ) ) {
269
			do_action( 'gravityview_log_error', __METHOD__ . ' User does not have the `gravityview_moderate_entries` capability.' );
270
			return false;
271
		}
272
273
		$approved = empty( $approved ) ? 0 : 'Approved';
274
275
		// calculate approved field id
276
		$approved_column_id = self::get_approved_column( $form_id );
277
278
		foreach( $entries as $entry_id ) {
0 ignored issues
show
Bug introduced by
The expression $entries of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
279
			self::update_approved( (int)$entry_id, $approved, $form_id, $approved_column_id );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
280
		}
281
	}
282
283
284
285
286
	/**
287
	 * update_approved function.
288
	 *
289
	 * @access public
290
	 * @static
291
	 * @param int $entry_id (default: 0)
292
	 * @param int $approved (default: 0)
293
	 * @param int $form_id (default: 0)
294
	 * @param int $approvedcolumn (default: 0)
295
	 * @return boolean True: It worked; False: it failed
296
	 */
297
	public static function update_approved( $entry_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) {
298
299
		if( !class_exists( 'GFAPI' ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
300
			do_action( 'gravityview_log_error', __METHOD__ . 'GFAPI does not exist' );
301
			return false;
302
		}
303
304
		if( empty( $approvedcolumn ) ) {
305
			$approvedcolumn = self::get_approved_column( $form_id );
306
		}
307
308
		//get the entry
309
		$entry = GFAPI::get_entry( $entry_id );
310
311
		//update entry
312
		$entry[ (string)$approvedcolumn ] = $approved;
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
313
314
		/** @var bool|WP_Error $result */
315
		$result = GFAPI::update_entry( $entry );
316
317
		/**
318
		 * GFAPI::update_entry() doesn't trigger `gform_after_update_entry`, so we trigger updating the meta ourselves.
319
		 */
320
		self::update_approved_meta( $entry_id, $approved );
321
322
		// add note to entry
323
		if( $result === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
324
325
			$note = empty( $approved ) ? __( 'Disapproved the Entry for GravityView', 'gravityview' ) : __( 'Approved the Entry for GravityView', 'gravityview' );
326
327
			/**
328
			 * @filter `gravityview/approve_entries/add-note` Add a note when the entry has been approved or disapproved?
329
			 * @since todo
330
			 * @param bool $add_note True: Yep, add that note! False: Do not, under any circumstances, add that note!
331
			 */
332
			$add_note = apply_filters( 'gravityview/approve_entries/add-note', true );
333
334
			if( $add_note && class_exists( 'GravityView_Entry_Notes' ) ) {
335
				$current_user = wp_get_current_user();
336
				GravityView_Entry_Notes::add_note( $entry_id, $current_user->ID, $current_user->display_name, $note );
337
			}
338
339
			/**
340
			 * Destroy the cache for this form
341
			 * @see class-cache.php
342
			 * @since 1.5.1
343
			 */
344
			do_action( 'gravityview_clear_form_cache', $form_id );
345
346
		} else if( is_wp_error( $result ) ) {
347
348
			do_action( 'gravityview_log_error', __METHOD__ . sprintf( ' - Entry approval not updated: %s', $result->get_error_message() ) );
349
350
			$result = false;
351
		}
352
353
		return $result;
354
355
	}
356
357
358
	/**
359
	 * Update the is_approved meta whenever the entry is submitted (and it contains a User Opt-in field)
360
	 *
361
	 * @since 1.16.6
362
	 *
363
	 * @param $entry array Gravity Forms entry object
364
	 * @param $form array Gravity Forms form object
365
	 */
366
	public function after_submission( $entry, $form ) {
367
		$this->after_update_entry_update_approved_meta( $form , $entry['id'] );
368
	}
369
370
371
372
	/**
373
	 * Update the is_approved meta whenever the entry is updated
374
	 *
375
	 * @since 1.7.6.1 Was previously named `update_approved_meta`
376
	 *
377
	 * @param  array $form     Gravity Forms form array
378
	 * @param  int $entry_id ID of the Gravity Forms entry
379
	 * @return void
380
	 */
381
	public function after_update_entry_update_approved_meta( $form, $entry_id = NULL ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
382
383
		$approvedcolumn = self::get_approved_column( $form['id'] );
384
385
        /**
386
         * If the form doesn't contain the approve field, don't assume anything.
387
         */
388
        if( empty( $approvedcolumn ) ) {
389
            return;
390
        }
391
392
		$entry = GFAPI::get_entry( $entry_id );
393
394
		self::update_approved_meta( $entry_id, $entry[ (string)$approvedcolumn ] );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
395
396
	}
397
398
	/**
399
	 * Update the `is_approved` entry meta value
400
	 * @param  int $entry_id ID of the Gravity Forms entry
401
	 * @param  string $is_approved String whether entry is approved or not. `0` for not approved, `Approved` for approved.
402
	 *
403
	 * @since 1.7.6.1 `after_update_entry_update_approved_meta` was previously to be named `update_approved_meta`
404
	 *
405
	 * @return void
406
	 */
407
	private static function update_approved_meta( $entry_id, $is_approved ) {
408
409
		/**
410
		 * Make sure that the "User Opt-in" and the Admin Approve/Reject entry set the same meta value
411
		 * @since 1.16.6
412
		 */
413
		$is_approved = empty( $is_approved ) ? 0 : 'Approved';
414
415
		// update entry meta
416
		if( function_exists('gform_update_meta') ) {
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...
417
418
			gform_update_meta( $entry_id, 'is_approved', $is_approved );
419
420
			/**
421
			 * @action `gravityview/approve_entries/updated` Triggered when an entry approval is updated
422
			 * @since 1.7.6.1
423
			 * @param  int $entry_id ID of the Gravity Forms entry
424
			 * @param  string $is_approved String whether entry is approved or not. `0` for not approved, `Approved` for approved.
425
			 */
426
			do_action( 'gravityview/approve_entries/updated', $entry_id, $is_approved );
427
428
			if( empty( $is_approved ) ) {
429
430
				/**
431
				 * @action `gravityview/approve_entries/disapproved` Triggered when an entry is rejected
432
				 * @since 1.7.6.1
433
				 * @param  int $entry_id ID of the Gravity Forms entry
434
				 */
435
				do_action( 'gravityview/approve_entries/disapproved', $entry_id );
436
437
			} else {
438
439
				/**
440
				 * @action `gravityview/approve_entries/approved` Triggered when an entry is approved
441
				 * @since 1.7.6.1
442
				 * @param  int $entry_id ID of the Gravity Forms entry
443
				 */
444
				do_action( 'gravityview/approve_entries/approved', $entry_id );
445
446
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
447
448
		} else {
449
450
			do_action('gravityview_log_error', __METHOD__ . ' - `gform_update_meta` does not exist.' );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
451
452
		}
453
	}
454
455
456
	/**
457
	 * Approve/Disapprove entries using the × or ✓ icons in the GF Entries screen
458
	 * @return void
459
	 */
460
	public function ajax_update_approved() {
461
462
		if( empty( $_POST['entry_id'] ) || empty( $_POST['form_id'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
463
464
			do_action( 'gravityview_log_error', __METHOD__ . ' entry_id or form_id are empty.', $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
465
466
			$result = false;
467
		}
468
469
		else if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajaxgfentries' ) ) {
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...
470
471
			do_action( 'gravityview_log_error', __METHOD__ . ' Security check failed.', $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
472
473
			$result = false;
474
		}
475
476
		else if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $_POST['entry_id'] ) ) {
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...
477
478
			do_action( 'gravityview_log_error', __METHOD__ . ' User does not have the `gravityview_moderate_entries` capability.' );
479
480
			$result = false;
481
		}
482
483
		else {
484
485
			$result = self::update_approved( $_POST['entry_id'], $_POST['approved'], $_POST['form_id'] );
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...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
486
487
			if( is_wp_error( $result ) ) {
488
				/** @var WP_Error $result */
489
				do_action( 'gravityview_log_error', __METHOD__ .' Error updating approval: ' . $result->get_error_message() );
490
				$result = false;
491
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
492
493
		}
494
495
		exit( $result );
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_update_approved() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
496
	}
497
498
499
	/**
500
	 * Calculate the approve field.input id
501
	 *
502
	 * @access public
503
	 * @static
504
	 * @param mixed $form GF Form or Form ID
505
	 * @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.
506
	 */
507
	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...
508
509
        if( empty( $form ) ) {
510
            return null;
511
        }
512
513
        if( !is_array( $form ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
514
            $form = GVCommon::get_form( $form );
515
        }
516
517
		foreach( $form['fields'] as $key => $field ) {
518
519
            $field = (array) $field;
520
521
			if( !empty( $field['gravityview_approved'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
522
				if( !empty($field['inputs'][0]['id']) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
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...
523
					return $field['inputs'][0]['id'];
524
				}
525
			}
526
527
            // Note: This is just for backward compatibility from GF Directory plugin and old GV versions - when using i18n it may not work..
528
            if( 'checkbox' == $field['type'] && isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
529
                foreach ( $field['inputs'] as $key2 => $input ) {
530
                    if ( strtolower( $input['label'] ) == 'approved' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
531
                        return $input['id'];
532
                    }
533
                }
534
            }
535
		}
536
537
		return null;
538
	}
539
540
541
542
	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...
543
544
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $entry['id'] ) ) {
545
			return;
546
		}
547
548
		if( empty( $entry['id'] ) ) {
549
			return;
550
		}
551
552
		if( gform_get_meta( $entry['id'], 'is_approved' ) ) {
553
			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...
554
		}
555
	}
556
557
558
559
560
	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...
561
562
		if( !class_exists( 'RGForms' ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
563
564
			do_action( 'gravityview_log_error', 'GravityView_Admin_ApproveEntries[add_scripts_and_styles] RGForms does not exist.' );
565
566
			return;
567
		}
568
569
		// enqueue styles & scripts gf_entries
570
		// But only if we're on the main Entries page, not on reports pages
571
		if( RGForms::get_page() === 'entry_list' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
572
573
			$form_id = RGForms::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...
574
575
			// If there are no forms identified, use the first form. That's how GF does it.
576
			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...
577
				$forms = gravityview_get_forms();
578
				if( !empty( $forms ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
579
					$form_id = $forms[0]['id'];
580
				}
581
			}
582
583
			$approvedcolumn = self::get_approved_column( $form_id );
584
585
			wp_register_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...
586
			wp_enqueue_style( 'gravityview_entries_list' );
587
588
			$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...
589
590
			wp_register_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...
591
			wp_enqueue_script( 'gravityview_gf_entries_scripts' );
592
593
			wp_localize_script( 'gravityview_gf_entries_scripts', 'gvGlobals', array(
594
				'nonce' => wp_create_nonce( 'gravityview_ajaxgfentries'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
595
				'form_id' => $form_id,
596
				'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...
597
				'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...
598
				'bulk_actions' => $this->get_bulk_actions( $form_id ),
599
				'bulk_message' => $this->bulk_update_message,
600
				'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...
601
				'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...
602
				'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...
603
				'column_link' => esc_url( add_query_arg( array('sort' => $approvedcolumn) ) ),
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
604
			) );
605
606
		}
607
608
	}
609
610
	/**
611
	 * Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" option group
612
	 *
613
	 * @since 1.16.3
614
	 *
615
	 * @param int $form_id  ID of the form currently being displayed
616
	 *
617
	 * @return array Array of actions to be added to the GravityView option group
618
	 */
619
	private function get_bulk_actions( $form_id ) {
620
621
		$bulk_actions = array(
622
			'GravityView' => array(
623
				array(
624
					'label' => __( 'Approve', 'gravityview' ),
625
					'value' => sprintf( 'approve-%d', $form_id ),
626
				),
627
				array(
628
					'label' => __( 'Disapprove', 'gravityview' ),
629
					'value' => sprintf( 'unapprove-%d', $form_id ),
630
				),
631
			),
632
		);
633
634
		/**
635
		 * @filter `gravityview/approve_entries/bulk_actions` Modify the GravityView "Bulk action" dropdown list. Return an empty array to hide.
636
		 * @see https://gist.github.com/zackkatz/82785402c996b51b4dc9 for an example of how to use this filter
637
		 * @since 1.16.3
638
		 * @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
639
		 * @param int $form_id ID of the form currently being displayed
640
		 */
641
		$bulk_actions = apply_filters( 'gravityview/approve_entries/bulk_actions', $bulk_actions, $form_id );
642
643
		// Sanitize the values, just to be sure.
644
		foreach ( $bulk_actions as $key => $group ) {
645
			foreach ( $group as $i => $action ) {
646
				$bulk_actions[ $key ][ $i ]['label'] = esc_html( $bulk_actions[ $key ][ $i ]['label'] );
647
				$bulk_actions[ $key ][ $i ]['value'] = esc_attr( $bulk_actions[ $key ][ $i ]['value'] );
648
			}
649
		}
650
651
		return $bulk_actions;
652
	}
653
654
	/**
655
	 * Should the Approve/Reject Entry column be shown in the GF Entries page?
656
	 *
657
	 * @since 1.7.2
658
	 *
659
	 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
660
	 *
661
	 * @return bool True: Show column; False: hide column
662
	 */
663
	private function show_approve_entry_column( $form_id ) {
664
665
		$show_approve_column = GVCommon::has_cap( 'gravityview_moderate_entries' );
666
667
		/**
668
		 * @filter `gravityview/approve_entries/hide-if-no-connections` Return true to hide reject/approve if there are no connected Views
669
		 * @since 1.7.2
670
		 * @param boolean $hide_if_no_connections
671
		 */
672
		$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...
673
674
		if( $hide_if_no_connections ) {
675
676
			$connected_views = gravityview_get_connected_views( $form_id );
677
678
			if( empty( $connected_views ) ) {
679
				$show_approve_column = false;
680
			}
681
		}
682
683
		/**
684
		 * @filter `gravityview/approve_entries/show-column` Override whether the column is shown
685
		 * @param boolean $show_approve_column Whether the column will be shown
686
		 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
687
		 */
688
		$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...
689
690
		return $show_approve_column;
691
	}
692
693
	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...
694
		$scripts[] = 'gravityview_gf_entries_scripts';
695
		return $scripts;
696
	}
697
698
	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...
699
		$styles[] = 'gravityview_entries_list';
700
		return $styles;
701
	}
702
703
}
704
705
new GravityView_Admin_ApproveEntries;
706