Completed
Push — master ( 2adc18...d10a7f )
by Zack
30:36 queued 26:56
created

GravityView_Admin_ApproveEntries::tooltips()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
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 673.

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
		// in case entry is edited (on admin or frontend)
44
		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...
45
46
		add_filter( 'gravityview_tooltips', array( $this, 'tooltips' ) );
47
48
		// adding styles and scripts
49
		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...
50
		// bypass Gravity Forms no-conflict mode
51
		add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict_script' ) );
52
		add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict_style' ) );
53
	}
54
55
	/**
56
	 * Add the GravityView Fields group tooltip
57
	 *
58
	 * @param $tooltips
59
	 *
60
	 * @return array Tooltips array with GravityView fields tooltip
61
	 */
62
	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...
63
64
		$tooltips['form_gravityview_fields'] = array(
65
			'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...
66
			'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...
67
		);
68
69
		return $tooltips;
70
	}
71
72
73
	/**
74
	 * Inject new add field buttons in the gravity form editor page
75
	 *
76
	 * @access public
77
	 * @param mixed $field_groups
78
	 * @return array Array of fields
79
	 */
80
	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...
81
82
		$gravityview_fields = array(
83
			'name' => 'gravityview_fields',
84
			'label' => 'GravityView',
85
			'fields' => array(
86
				array(
87
					'class' => 'button',
88
					'value' => __( 'Approve/Reject', 'gravityview' ),
89
					'onclick' => "StartAddField('gravityviewapproved_admin');",
90
					'data-type' => 'gravityviewapproved_admin'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
91
				),
92
				array(
93
					'class' => 'button',
94
					'value' => __( 'User Opt-In', 'gravityview' ),
95
					'onclick' => "StartAddField('gravityviewapproved');",
96
					'data-type' => 'gravityviewapproved'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
97
				),
98
			)
99
		);
100
101
		array_push( $field_groups, $gravityview_fields );
102
103
		return $field_groups;
104
	}
105
106
107
108
	/**
109
	 * At edit form page, set the field Approve defaults
110
	 *
111
	 * @todo Convert to a partial include file
112
	 * @access public
113
	 * @return void
114
	 */
115
	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...
116
		?>
117
		case 'gravityviewapproved_admin':
118
			field.label = "<?php _e( 'Approved? (Admin-only)', 'gravityview' ); ?>";
119
120
			field.adminLabel = "<?php _e( 'Approved?', 'gravityview' ); ?>";
121
			field.adminOnly = true;
122
123
			field.choices = null;
124
			field.inputs = null;
125
126
			if( !field.choices ) {
127
				field.choices = new Array( new Choice("<?php _e( 'Approved', 'gravityview' ); ?>") );
128
			}
129
130
			field.inputs = new Array();
131
			for( var i=1; i<=field.choices.length; i++ ) {
132
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
133
			}
134
135
			field.type = 'checkbox';
136
			field.gravityview_approved = 1;
137
138
			break;
139
		case 'gravityviewapproved':
140
			field.label = "<?php _e( 'Show Entry on Website', 'gravityview' ); ?>";
141
142
			field.adminLabel = "<?php _e( 'Opt-In', 'gravityview' ); ?>";
143
			field.adminOnly = false;
144
145
			field.choices = null;
146
			field.inputs = null;
147
148
			if( !field.choices ) {
149
				field.choices = new Array(
150
					new Choice("<?php _e( 'Yes, display my entry on the website', 'gravityview' ); ?>")
151
				);
152
			}
153
154
			field.inputs = new Array();
155
			for( var i=1; i<=field.choices.length; i++ ) {
156
				field.inputs.push(new Input(field.id + (i/10), field.choices[i-1].text));
157
			}
158
159
			field.type = 'checkbox';
160
			field.gravityview_approved = 1;
161
162
			break;
163
		<?php
164
	}
165
166
167
168
	/**
169
	 * Capture bulk actions - gf_entries table
170
	 *
171
	 * @uses  GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
172
	 * @access public
173
	 * @return void|boolean
174
	 */
175
	public function process_bulk_action() {
176
		if ( ! class_exists( 'RGForms' ) ) {
177
			return;
178
		}
179
180
		// gforms_update_note is sent when bulk editing entry notes. We don't want to process then.
181
		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...
182
183
			check_admin_referer( 'gforms_entry_list', 'gforms_entry_list' );
184
185
			// 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.
186
			$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...
187
188
			/**
189
			 * The extra '-' is to make sure that there are at *least* two items in array.
190
			 * @see https://github.com/katzwebservices/GravityView/issues/370
191
			 */
192
			$bulk_action .= '-';
193
194
			list( $approved_status, $form_id ) = explode( '-', $bulk_action );
195
196
			if ( empty( $form_id ) ) {
197
				do_action( 'gravityview_log_error', '[process_bulk_action] Form ID is empty from parsing bulk action.', $bulk_action );
198
				return false;
199
			}
200
201
			// All entries are set to be updated, not just the visible ones
202
			if ( ! empty( $_POST['all_entries'] ) ) {
203
204
				// Convert the current entry search into GF-formatted search criteria
205
				$search = array(
206
					'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...
207
					'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...
208
					'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...
209
				);
210
211
				$search_criteria = GravityView_frontend::get_search_criteria( $search, $form_id );
212
213
				// Get all the entry IDs for the form
214
				$entries = gravityview_get_entry_ids( $form_id, $search_criteria );
215
216
			} else {
217
218
				$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...
219
220
			}
221
222
			if ( empty( $entries ) ) {
223
				do_action( 'gravityview_log_error', '[process_bulk_action] Entries are empty' );
224
				return false;
225
			}
226
227
			$entry_count = count( $entries ) > 1 ? sprintf( __( '%d entries', 'gravityview' ), count( $entries ) ) : __( '1 entry', 'gravityview' );
228
229
			switch ( $approved_status ) {
230
				case 'approve':
231
					self::update_bulk( $entries, 1, $form_id );
232
					$this->bulk_update_message = sprintf( __( '%s approved.', 'gravityview' ), $entry_count );
233
					break;
234
235
				case 'unapprove':
236
					self::update_bulk( $entries, 0, $form_id );
237
					$this->bulk_update_message = sprintf( __( '%s disapproved.', 'gravityview' ), $entry_count );
238
					break;
239
			}
240
		}
241
	}
242
243
244
245
246
247
	/**
248
	 * Process a bulk of entries to update the approve field/property
249
	 *
250
	 * @access private
251
	 * @static
252
	 * @param array|boolean $entries If array, array of entry IDs that are to be updated. If true: update all entries.
253
	 * @param int $approved Approved status. If `0`: unapproved, if not empty, `Approved`
254
	 * @param int $form_id The Gravity Forms Form ID
255
	 * @return boolean|void
256
	 */
257
	private static function update_bulk( $entries, $approved, $form_id ) {
258
259
		if( empty($entries) || ( $entries !== true && !is_array($entries) ) ) {
0 ignored issues
show
introduced by
Found "!== true". Use Yoda Condition checks, you must
Loading history...
introduced by
Space after opening control structure is required
Loading history...
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...
260
			do_action( 'gravityview_log_error', __METHOD__ . ' Entries were empty or malformed.', $entries );
261
			return false;
262
		}
263
264
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries' ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
265
			do_action( 'gravityview_log_error', __METHOD__ . ' User does not have the `gravityview_moderate_entries` capability.' );
266
			return false;
267
		}
268
269
		$approved = empty( $approved ) ? 0 : 'Approved';
270
271
		// calculate approved field id
272
		$approved_column_id = self::get_approved_column( $form_id );
273
274
		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...
introduced by
Space after opening control structure is required
Loading history...
275
			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...
276
		}
277
	}
278
279
280
281
282
	/**
283
	 * update_approved function.
284
	 *
285
	 * @access public
286
	 * @static
287
	 * @param int $entry_id (default: 0)
288
	 * @param int $approved (default: 0)
289
	 * @param int $form_id (default: 0)
290
	 * @param int $approvedcolumn (default: 0)
291
	 * @return boolean True: It worked; False: it failed
292
	 */
293
	public static function update_approved( $entry_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) {
294
295
		if( !class_exists( 'GFAPI' ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
296
			do_action( 'gravityview_log_error', __METHOD__ . 'GFAPI does not exist' );
297
			return false;
298
		}
299
300
		if( empty( $approvedcolumn ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
301
			$approvedcolumn = self::get_approved_column( $form_id );
302
		}
303
304
		//get the entry
305
		$entry = GFAPI::get_entry( $entry_id );
306
307
		//update entry
308
		$entry[ (string)$approvedcolumn ] = $approved;
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
309
310
		/** @var bool|WP_Error $result */
311
		$result = GFAPI::update_entry( $entry );
312
313
		/**
314
		 * GFAPI::update_entry() doesn't trigger `gform_after_update_entry`, so we trigger updating the meta ourselves.
315
		 */
316
		self::update_approved_meta( $entry_id, $approved );
317
318
		// add note to entry
319
		if( $result === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
introduced by
Space after opening control structure is required
Loading history...
320
			$note = empty( $approved ) ? __( 'Disapproved the Entry for GravityView', 'gravityview' ) : __( 'Approved the Entry for GravityView', 'gravityview' );
321
322
			if( class_exists( 'GravityView_Entry_Notes' ) ){
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
323
				global $current_user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
324
      			get_currentuserinfo();
325
				GravityView_Entry_Notes::add_note( $entry_id, $current_user->ID, $current_user->display_name, $note );
326
			}
327
328
			/**
329
			 * Destroy the cache for this form
330
			 * @see class-cache.php
331
			 * @since 1.5.1
332
			 */
333
			do_action( 'gravityview_clear_form_cache', $form_id );
334
335
		} else if( is_wp_error( $result ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
336
337
			do_action( 'gravityview_log_error', __METHOD__ . sprintf( ' - Entry approval not updated: %s', $result->get_error_message() ) );
338
339
			$result = false;
340
		}
341
342
		return $result;
343
344
	}
345
346
	/**
347
	 * Update the is_approved meta whenever the entry is updated
348
	 *
349
	 * @since 1.7.6.1 Was previously named `update_approved_meta`
350
	 *
351
	 * @param  array $form     Gravity Forms form array
352
	 * @param  int $entry_id ID of the Gravity Forms entry
353
	 * @return void
354
	 */
355
	public static 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...
356
357
		$approvedcolumn = self::get_approved_column( $form['id'] );
358
359
        /**
360
         * If the form doesn't contain the approve field, don't assume anything.
361
         */
362
        if( empty( $approvedcolumn ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
363
            return;
364
        }
365
366
		$entry = GFAPI::get_entry( $entry_id );
367
368
		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...
369
370
	}
371
372
	/**
373
	 * Update the `is_approved` entry meta value
374
	 * @param  int $entry_id ID of the Gravity Forms entry
375
	 * @param  string $is_approved String whether entry is approved or not. `0` for not approved, `Approved` for approved.
376
	 *
377
	 * @since 1.7.6.1 `after_update_entry_update_approved_meta` was previously to be named `update_approved_meta`
378
	 *
379
	 * @return void
380
	 */
381
	private static function update_approved_meta( $entry_id, $is_approved ) {
382
383
		// update entry meta
384
		if( function_exists('gform_update_meta') ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
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...
385
386
			gform_update_meta( $entry_id, 'is_approved', $is_approved );
387
388
			/**
389
			 * @action `gravityview/approve_entries/updated` Triggered when an entry approval is updated
390
			 * @since 1.7.6.1
391
			 * @param  int $entry_id ID of the Gravity Forms entry
392
			 * @param  string $is_approved String whether entry is approved or not. `0` for not approved, `Approved` for approved.
393
			 */
394
			do_action( 'gravityview/approve_entries/updated', $entry_id, $is_approved );
395
396
			if( empty( $is_approved ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
397
398
				/**
399
				 * @action `gravityview/approve_entries/disapproved` Triggered when an entry is rejected
400
				 * @since 1.7.6.1
401
				 * @param  int $entry_id ID of the Gravity Forms entry
402
				 */
403
				do_action( 'gravityview/approve_entries/disapproved', $entry_id );
404
405
			} else {
406
407
				/**
408
				 * @action `gravityview/approve_entries/approved` Triggered when an entry is approved
409
				 * @since 1.7.6.1
410
				 * @param  int $entry_id ID of the Gravity Forms entry
411
				 */
412
				do_action( 'gravityview/approve_entries/approved', $entry_id );
413
414
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
415
416
		} else {
417
418
			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...
419
420
		}
421
	}
422
423
424
	/**
425
	 * Approve/Disapprove entries using the × or ✓ icons in the GF Entries screen
426
	 * @return void
427
	 */
428
	public function ajax_update_approved() {
429
430
		if( empty( $_POST['entry_id'] ) || empty( $_POST['form_id'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
431
432
			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...
433
434
			$result = false;
435
		}
436
437
		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...
438
439
			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...
440
441
			$result = false;
442
		}
443
444
		else if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $_POST['entry_id'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
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...
445
446
			do_action( 'gravityview_log_error', __METHOD__ . ' User does not have the `gravityview_moderate_entries` capability.' );
447
448
			$result = false;
449
		}
450
451
		else {
452
453
			$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...
454
455
			if( is_wp_error( $result ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
456
				/** @var WP_Error $result */
457
				do_action( 'gravityview_log_error', __METHOD__ .' Error updating approval: ' . $result->get_error_message() );
458
				$result = false;
459
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
460
461
		}
462
463
		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...
464
	}
465
466
467
	/**
468
	 * Calculate the approve field.input id
469
	 *
470
	 * @access public
471
	 * @static
472
	 * @param mixed $form GF Form or Form ID
473
	 * @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.
474
	 */
475
	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...
476
477
        if( empty( $form ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
478
            return null;
479
        }
480
481
        if( !is_array( $form ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
482
            $form = GVCommon::get_form( $form );
483
        }
484
485
		foreach( $form['fields'] as $key => $field ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
486
487
            $field = (array) $field;
488
489
			if( !empty( $field['gravityview_approved'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
490
				if( !empty($field['inputs'][0]['id']) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
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...
491
					return $field['inputs'][0]['id'];
492
				}
493
			}
494
495
            // Note: This is just for backward compatibility from GF Directory plugin and old GV versions - when using i18n it may not work..
496
            if( 'checkbox' == $field['type'] && isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
497
                foreach ( $field['inputs'] as $key2 => $input ) {
498
                    if ( strtolower( $input['label'] ) == 'approved' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
499
                        return $input['id'];
500
                    }
501
                }
502
            }
503
		}
504
505
		return null;
506
	}
507
508
509
510
	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...
511
512
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries', $entry['id'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
513
			return;
514
		}
515
516
		if( empty( $entry['id'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
517
			return;
518
		}
519
520
		if( gform_get_meta( $entry['id'], 'is_approved' ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
521
			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...
522
		}
523
	}
524
525
526
527
528
	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...
529
530
		if( !class_exists( 'RGForms' ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
531
532
			do_action( 'gravityview_log_error', 'GravityView_Admin_ApproveEntries[add_scripts_and_styles] RGForms does not exist.' );
533
534
			return;
535
		}
536
537
		// enqueue styles & scripts gf_entries
538
		// But only if we're on the main Entries page, not on reports pages
539
		if( RGForms::get_page() === 'entry_list' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
introduced by
Space after opening control structure is required
Loading history...
540
541
			$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...
542
543
			// If there are no forms identified, use the first form. That's how GF does it.
544
			if( empty( $form_id ) && class_exists('RGFormsModel') ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
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...
545
				$forms = gravityview_get_forms();
546
				if( !empty( $forms ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
547
					$form_id = $forms[0]['id'];
548
				}
549
			}
550
551
			$approvedcolumn = self::get_approved_column( $form_id );
552
553
			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...
554
			wp_enqueue_style( 'gravityview_entries_list' );
555
556
			$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...
557
558
			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...
559
			wp_enqueue_script( 'gravityview_gf_entries_scripts' );
560
561
			wp_localize_script( 'gravityview_gf_entries_scripts', 'gvGlobals', array(
562
				'nonce' => wp_create_nonce( 'gravityview_ajaxgfentries'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
563
				'form_id' => $form_id,
564
				'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...
565
				'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...
566
				'bulk_actions' => $this->get_bulk_actions( $form_id ),
567
				'bulk_message' => $this->bulk_update_message,
568
				'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...
569
				'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...
570
				'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...
571
				'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...
572
			) );
573
574
		}
575
576
	}
577
578
	/**
579
	 * Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" option group
580
	 *
581
	 * @since 1.16.3
582
	 *
583
	 * @param int $form_id  ID of the form currently being displayed
584
	 *
585
	 * @return array Array of actions to be added to the GravityView option group
586
	 */
587
	private function get_bulk_actions( $form_id ) {
588
589
		$bulk_actions = array(
590
			'GravityView' => array(
591
				array(
592
					'label' => __( 'Approve', 'gravityview' ),
593
					'value' => sprintf( 'approve-%d', $form_id ),
594
				),
595
				array(
596
					'label' => __( 'Disapprove', 'gravityview' ),
597
					'value' => sprintf( 'unapprove-%d', $form_id ),
598
				),
599
			),
600
		);
601
602
		/**
603
		 * @filter `gravityview/approve_entries/bulk_actions` Modify the GravityView "Bulk action" dropdown list. Return an empty array to hide.
604
		 * @see https://gist.github.com/zackkatz/82785402c996b51b4dc9 for an example of how to use this filter
605
		 * @since 1.16.3
606
		 * @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
607
		 * @param int $form_id ID of the form currently being displayed
608
		 */
609
		$bulk_actions = apply_filters( 'gravityview/approve_entries/bulk_actions', $bulk_actions, $form_id );
610
611
		// Sanitize the values, just to be sure.
612
		foreach ( $bulk_actions as $key => $group ) {
613
			foreach ( $group as $i => $action ) {
614
				$bulk_actions[ $key ][ $i ]['label'] = esc_html( $bulk_actions[ $key ][ $i ]['label'] );
615
				$bulk_actions[ $key ][ $i ]['value'] = esc_attr( $bulk_actions[ $key ][ $i ]['value'] );
616
			}
617
		}
618
619
		return $bulk_actions;
620
	}
621
622
	/**
623
	 * Should the Approve/Reject Entry column be shown in the GF Entries page?
624
	 *
625
	 * @since 1.7.2
626
	 *
627
	 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
628
	 *
629
	 * @return bool True: Show column; False: hide column
630
	 */
631
	private function show_approve_entry_column( $form_id ) {
632
633
		$show_approve_column = GVCommon::has_cap( 'gravityview_moderate_entries' );
634
635
		/**
636
		 * @filter `gravityview/approve_entries/hide-if-no-connections` Return true to hide reject/approve if there are no connected Views
637
		 * @since 1.7.2
638
		 * @param boolean $hide_if_no_connections
639
		 */
640
		$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...
641
642
		if( $hide_if_no_connections ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
643
644
			$connected_views = gravityview_get_connected_views( $form_id );
645
646
			if( empty( $connected_views ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
647
				$show_approve_column = false;
648
			}
649
		}
650
651
		/**
652
		 * @filter `gravityview/approve_entries/show-column` Override whether the column is shown
653
		 * @param boolean $show_approve_column Whether the column will be shown
654
		 * @param int $form_id The ID of the Gravity Forms form for which entries are being shown
655
		 */
656
		$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...
657
658
		return $show_approve_column;
659
	}
660
661
	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...
662
		$scripts[] = 'gravityview_gf_entries_scripts';
663
		return $scripts;
664
	}
665
666
	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...
667
		$styles[] = 'gravityview_entries_list';
668
		return $styles;
669
	}
670
671
}
672
673
new GravityView_Admin_ApproveEntries;
674