Completed
Push — develop ( 8db1a5...e63648 )
by Zack
17:27
created

GravityView_Entry_Approval::add_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
ccs 0
cts 6
cp 0
crap 2
rs 9.7666
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 23 and the first side effect is on line 15.

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-gravityview-entry-approval.php
4
 * @package   GravityView
5
 * @license   GPL2+
6
 * @author    Katz Web Services, Inc.
7
 * @link      https://gravityview.co
8
 * @copyright Copyright 2016, Katz Web Services, Inc.
9
 *
10
 * @since 1.18
11
 */
12
13
/** If this file is called directly, abort. */
14
if ( ! defined( 'ABSPATH' ) ) {
15
	die;
16
}
17
18
/**
19
 * Generate linked list output for a list of entries.
20
 *
21
 * @since 1.18
22
 */
23
class GravityView_Entry_Approval {
24
25
	/**
26
	 * @var string Key used to store approval status in the Gravity Forms entry meta table
27
	 */
28
	const meta_key = 'is_approved';
29
30
	public function __construct() {
31
		$this->add_hooks();
32
	}
33
34
	/**
35
	 * Add actions and filters related to entry approval
36
	 *
37
	 * @return void
38
	 */
39
	private function add_hooks() {
40
41
		// in case entry is edited (on admin or frontend)
42
		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...
43
44
		// when using the User opt-in field, check on entry submission
45
		add_action( 'gform_after_submission', array( $this, 'after_submission' ), 10, 2 );
46
47
		// process ajax approve entry requests
48
		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...
49
50
		// autounapprove
51
		add_action( 'gravityview/edit_entry/after_update', array( __CLASS__, 'autounapprove' ), 10, 4 );
52
53
	}
54
55
	/**
56
	 * Get the approval status for an entry
57
	 *
58
	 * @since 1.18
59
	 * @uses GVCommon::get_entry_id() Accepts entry slug or entry ID
60
	 *
61
	 * @param array|int|string $entry Entry array, entry slug, or entry ID
62
	 * @param string $value_or_label "value" or "label" (default: "label")
63
	 *
64
	 * @return bool|string Return the label or value of entry approval
65
	 */
66 1
	public static function get_entry_status( $entry, $value_or_label = 'label' ) {
67
68 1
		$entry_id = is_array( $entry ) ? $entry['id'] : GVCommon::get_entry_id( $entry, true );
69
70 1
		$status = gform_get_meta( $entry_id, self::meta_key );
71
72 1
		$status = GravityView_Entry_Approval_Status::maybe_convert_status( $status );
73
74 1
		if( 'value' === $value_or_label ) {
75 1
			return $status;
76
		}
77
78
		return GravityView_Entry_Approval_Status::get_label( $status );
79
	}
80
81
	/**
82
	 * Approve/Disapprove entries using the × or ✓ icons in the GF Entries screen
83
	 *
84
	 * @uses wp_send_json_error()
85
	 * @uses wp_send_json_success()
86
	 *
87
	 * Expects a $_POST request with the following $_POST keys and values:
88
	 *
89
	 * @global array $_POST {
90
	 * @type int $form_id ID of the form connected to the entry being updated
91
	 * @type string|int $entry_slug The ID or slug of the entry being updated
92
	 * @type string $approved The value of the entry approval status {@see GravityView_Entry_Approval_Status::is_valid() }
93
	 * }
94
	 *
95
	 * @return void Prints result using wp_send_json_success() and wp_send_json_error()
96
	 */
97
	public function ajax_update_approved() {
98
		
99
		$form_id = intval( \GV\Utils::_POST( 'form_id' ) );
100
101
		// We always want requests from the admin to allow entry IDs, but not from the frontend
102
		// There's another nonce sent when approving entries in the admin that we check
103
		$force_entry_ids = \GV\Utils::_POST( 'admin_nonce' ) && wp_verify_nonce( \GV\Utils::_POST( 'admin_nonce' ), 'gravityview_admin_entry_approval' );
104
		
105
		$entry_id = GVCommon::get_entry_id( \GV\Utils::_POST( 'entry_slug' ), $force_entry_ids );
106
107
		$approval_status = \GV\Utils::_POST( 'approved' );
108
109
		$nonce = \GV\Utils::_POST( 'nonce' );
110
111
		// Valid status
112
		if( ! GravityView_Entry_Approval_Status::is_valid( $approval_status ) ) {
113
114
			gravityview()->log->error( 'Invalid approval status', array( 'data' => $_POST ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
115
116
			$result = new WP_Error( 'invalid_status', __( 'The request was invalid. Refresh the page and try again.', 'gravityview' ) );
117
118
		}
119
120
		// Valid values
121
		elseif ( empty( $entry_id ) || empty( $form_id ) ) {
122
123
			gravityview()->log->error( 'entry_id or form_id are empty.', array( 'data' => $_POST ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
124
125
			$result = new WP_Error( 'empty_details', __( 'The request was invalid. Refresh the page and try again.', 'gravityview' ) );
126
127
		}
128
129
		// Valid nonce
130
		else if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'gravityview_entry_approval' ) ) {
131
132
			gravityview()->log->error( 'Security check failed.', array( 'data' => $_POST ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
133
134
			$result = new WP_Error( 'invalid_nonce', __( 'The request was invalid. Refresh the page and try again.', 'gravityview' ) );
135
136
		}
137
138
		// Has capability
139
		elseif ( ! GVCommon::has_cap( 'gravityview_moderate_entries', $entry_id ) ) {
140
141
			gravityview()->log->error( 'User does not have the `gravityview_moderate_entries` capability.' );
142
143
			$result = new WP_Error( 'Missing Cap: gravityview_moderate_entries', __( 'You do not have permission to edit this entry.', 'gravityview') );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
144
145
		}
146
147
		// All checks passed
148
		else {
149
150
			$result = self::update_approved( $entry_id, $approval_status, $form_id );
151
152
		}
153
154
		if ( is_wp_error( $result ) ) {
155
			gravityview()->log->error( 'Error updating approval: {error}', array( 'error' =>  $result->get_error_message() ) );
0 ignored issues
show
introduced by
Expected 1 space between double arrow and "$result"; 2 found
Loading history...
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
156
157
			wp_send_json_error( $result );
158
		}
159
160
		$current_status = self::get_entry_status( $entry_id, 'value' );
161
162
		wp_send_json_success( array(
163
			'status' => $current_status
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
164
		) );
165
	}
166
167
	/**
168
	 * Update the is_approved meta whenever the entry is submitted (and it contains a User Opt-in field)
169
	 *
170
	 * @since 1.16.6
171
	 * @since 2.0.14 Set the default approval `is_approved` value upon submission
172
	 *
173
	 * @param $entry array Gravity Forms entry object
174
	 * @param $form array Gravity Forms form object
175
	 */
176 1
	public function after_submission( $entry, $form ) {
177
178 1
		$default_status = GravityView_Entry_Approval_Status::UNAPPROVED;
179
180
		/**
181
		 * @filter `gravityview/approve_entries/after_submission/default_status` Modify the default approval status for newly submitted entries
182
		 * @since 2.0.14
183
		 * @param int $default_status See GravityView_Entry_Approval_Status() for valid statuses.
184
		 */
185 1
		$filtered_status = apply_filters( 'gravityview/approve_entries/after_submission/default_status', $default_status );
186
187 1
		if ( GravityView_Entry_Approval_Status::is_valid( $filtered_status ) ) {
188 1
			$default_status = $filtered_status;
189
		} else {
190 1
			gravityview()->log->error( 'Invalid approval status returned by `gravityview/approve_entries/after_submission/default_status` filter: {status}', array( 'status' => $filtered_status ) );
191
		}
192
193
		// Set default
194 1
		self::update_approved_meta( $entry['id'], $default_status, $entry['form_id'] );
195
196
		// Then check for if there is an approval column, and use that value instead
197 1
		$this->after_update_entry_update_approved_meta( $form , $entry['id'] );
198 1
	}
199
200
	/**
201
	 * Update the is_approved meta whenever the entry is updated
202
	 *
203
	 * @since 1.7.6.1 Was previously named `update_approved_meta`
204
	 *
205
	 * @param  array $form     Gravity Forms form array
206
	 * @param  int $entry_id ID of the Gravity Forms entry
207
	 * @return void
208
	 */
209 3
	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...
210
211 3
		$approved_column = self::get_approved_column( $form['id'] );
212
213
		/**
214
		 * If the form doesn't contain the approve field, don't assume anything.
215
		 */
216 3
		if( empty( $approved_column ) ) {
217 3
			return;
218
		}
219
220
		$entry = GFAPI::get_entry( $entry_id );
221
222
		// If the checkbox is blank, it's disapproved, regardless of the label
223
		if ( '' === \GV\Utils::get( $entry, $approved_column ) ) {
224
			$value = GravityView_Entry_Approval_Status::DISAPPROVED;
225
		} else {
226
			// If the checkbox is not blank, it's approved
227
			$value = GravityView_Entry_Approval_Status::APPROVED;
228
		}
229
230
		self::update_approved_meta( $entry_id, $value, $form['id'] );
231
	}
232
233
	/**
234
	 * Process a bulk of entries to update the approve field/property
235
	 *
236
	 * @since 1.18 Moved to GravityView_Entry_Approval
237
	 * @since 1.18 Made public
238
	 *
239
	 * @access public
240
	 * @static
241
	 * @param array|boolean $entries If array, array of entry IDs that are to be updated. If true: update all entries.
242
	 * @param int $approved Approved status. If `0`: unapproved, if not empty, `Approved`
243
	 * @param int $form_id The Gravity Forms Form ID
244
	 * @return boolean|null True: successfully updated all entries. False: there was an error updating at least one entry. NULL: an error occurred (see log)
245
	 */
246 1
	public static function update_bulk( $entries = array(), $approved, $form_id ) {
247
248 1
		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...
249
			gravityview()->log->error( 'Entries were empty or malformed.', array( 'data' => $entries ) );
250
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
251
		}
252
253 1
		if( ! GVCommon::has_cap( 'gravityview_moderate_entries' ) ) {
254 1
			gravityview()->log->error( 'User does not have the `gravityview_moderate_entries` capability.' );
255 1
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
256
		}
257
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
258
259 1
		if ( ! GravityView_Entry_Approval_Status::is_valid( $approved ) ) {
260
			gravityview()->log->error( 'Invalid approval status', array( 'data' => $approved ) );
261
			return NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
262
		}
263
264
		// calculate approved field id once instead of looping through in the update_approved() method
265 1
		$approved_column_id = self::get_approved_column( $form_id );
266
267 1
		$success = true;
268 1
		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...
269 1
			$update_success = 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...
270
271 1
			if( ! $update_success ) {
272 1
				$success = false;
273
			}
274
		}
275
276 1
		return $success;
277
	}
278
279
	/**
280
	 * update_approved function.
281
	 *
282
	 * @since 1.18 Moved to GravityView_Entry_Approval class
283
	 *
284
	 * @access public
285
	 * @static
286
	 * @param int $entry_id (default: 0)
287
	 * @param int $approved (default: 2)
288
	 * @param int $form_id (default: 0)
289
	 * @param int $approvedcolumn (default: 0)
290
	 *
291
	 * @return boolean True: It worked; False: it failed
292
	 */
293 1
	public static function update_approved( $entry_id = 0, $approved = 2, $form_id = 0, $approvedcolumn = 0 ) {
294
295 1
		if( !class_exists( 'GFAPI' ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
296
			gravityview()->log->error( 'GFAPI does not exist' );
297
			return false;
298
		}
299
300 1
		if( ! GravityView_Entry_Approval_Status::is_valid( $approved ) ) {
301
			gravityview()->log->error( 'Not a valid approval value.' );
302
			return false;
303
		}
304
305 1
		$approved = GravityView_Entry_Approval_Status::maybe_convert_status( $approved );
306
307 1
		$entry = GFAPI::get_entry( $entry_id );
308
309 1
		if ( is_wp_error( $entry ) ) {
310 1
			gravityview()->log->error( 'Entry does not exist' );
311 1
			return false;
312
		}
313
314
		// If the form has an Approve/Reject field, update that value
315 1
		$result = self::update_approved_column( $entry_id, $approved, $form_id, $approvedcolumn );
316
317 1
		if( is_wp_error( $result ) ) {
318
			gravityview()->log->error( 'Entry approval not updated: {error}', array( 'error' => $result->get_error_message() ) );
319
			return false;
320
		}
321
322 1
		$form_id = intval( $form_id );
323
324
		// Update the entry meta
325 1
		self::update_approved_meta( $entry_id, $approved, $form_id );
326
327
		// add note to entry if approval field updating worked or there was no approved field
328
		// There's no validation for the meta
329 1
		if( true === $result ) {
330
331
			// Add an entry note
332 1
			self::add_approval_status_updated_note( $entry_id, $approved );
333
334
			/**
335
			 * Destroy the cache for this form
336
			 * @see class-cache.php
337
			 * @since 1.5.1
338
			 */
339 1
			do_action( 'gravityview_clear_form_cache', $form_id );
340
341
		}
342
343 1
		return $result;
344
	}
345
346
	/**
347
	 * Add a note when an entry is approved
348
	 *
349
	 * @see GravityView_Entry_Approval::update_approved
350
	 *
351
	 * @since 1.18
352
	 *
353
	 * @param int $entry_id Gravity Forms entry ID
354
	 * @param int $approved Approval status
355
	 *
356
	 * @return false|int|WP_Error Note ID if successful; WP_Error if error when adding note, FALSE if note not updated because of `gravityview/approve_entries/add-note` filter or `GravityView_Entry_Notes` class not existing
357
	 */
358 1
	private static function add_approval_status_updated_note( $entry_id, $approved = 0 ) {
359 1
		$note = '';
360
361 1
		switch ( $approved ) {
362
			case GravityView_Entry_Approval_Status::APPROVED:
363 1
				$note = __( 'Approved the Entry for GravityView', 'gravityview' );
364 1
				break;
365
			case GravityView_Entry_Approval_Status::UNAPPROVED:
366
				$note = __( 'Reset Entry approval for GravityView', 'gravityview' );
367
				break;
368
			case GravityView_Entry_Approval_Status::DISAPPROVED:
369
				$note = __( 'Disapproved the Entry for GravityView', 'gravityview' );
370
				break;
371
		}
372
373
		/**
374
		 * @filter `gravityview/approve_entries/add-note` Add a note when the entry has been approved or disapproved?
375
		 * @since 1.16.3
376
		 * @param bool $add_note True: Yep, add that note! False: Do not, under any circumstances, add that note!
377
		 */
378 1
		$add_note = apply_filters( 'gravityview/approve_entries/add-note', true );
379
380 1
		$note_id = false;
381
382 1
		if( $add_note && class_exists( 'GravityView_Entry_Notes' ) ) {
383
384 1
			$current_user = wp_get_current_user();
385
386 1
			$note_id = GravityView_Entry_Notes::add_note( $entry_id, $current_user->ID, $current_user->display_name, $note );
387
		}
388
389 1
		return $note_id;
390
	}
391
392
	/**
393
	 * Update the Approve/Disapproved field value
394
	 *
395
	 * @param  int $entry_id ID of the Gravity Forms entry
396
	 * @param  string $status String whether entry is approved or not. `0` for not approved, `Approved` for approved.
397
	 * @param int $form_id ID of the form of the entry being updated. Improves query performance.
398
	 * @param string $approvedcolumn Gravity Forms Field ID
399
	 *
400
	 * @return true|WP_Error
401
	 */
402
	private static function update_approved_column( $entry_id = 0, $status = '0', $form_id = 0, $approvedcolumn = 0 ) {
403
404
		if( empty( $approvedcolumn ) ) {
405
			$approvedcolumn = self::get_approved_column( $form_id );
406
		}
407
408
		if ( empty( $approvedcolumn ) ) {
409
			return true;
410
		}
411
412
		if ( ! GravityView_Entry_Approval_Status::is_valid( $status ) ) {
413
			return new WP_Error( 'invalid_status', 'Invalid entry approval status', $status );
414
		}
415
416
		//get the entry
417
		$entry = GFAPI::get_entry( $entry_id );
418
419
		// Entry doesn't exist
420
		if ( is_wp_error( $entry ) ) {
421
			return $entry;
422
		}
423
424
		$status = GravityView_Entry_Approval_Status::maybe_convert_status( $status );
425
426
		$new_value = '';
427
		if( GravityView_Entry_Approval_Status::APPROVED === $status ) {
428
			$new_value = self::get_approved_column_input_label( $form_id, $approvedcolumn );
429
		}
430
431
		//update entry
432
		$entry["{$approvedcolumn}"] = $new_value;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
433
434
		/**
435
		 * Note: GFAPI::update_entry() doesn't trigger `gform_after_update_entry`, so we trigger updating the meta ourselves
436
		 * @see GravityView_Entry_Approval::after_update_entry_update_approved_meta
437
		 * @var true|WP_Error $result
438
		 */
439
		$result = GFAPI::update_entry( $entry );
440
		
441
		return $result;
442
	}
443
444
	/**
445
	 * Get the value for the approved field checkbox
446
	 *
447
	 * When approving a field via the entry meta, use the correct value for the new approved column input
448
	 *
449
	 * @since 1.19
450
	 *
451
	 * @param array|int $form Form ID or form array
452
	 * @param string $approved_column Approved column field ID
453
	 *
454
	 * @return string|null
455
	 */
456
	private static function get_approved_column_input_label( $form, $approved_column ) {
457
458
		$field = gravityview_get_field( $form, $approved_column );
459
460
		// If the user has enabled a different value than the label (for some reason), use it.
461
		// This is highly unlikely
462
		if ( is_array( $field->choices ) && ! empty( $field->choices ) ) {
463
			return isset( $field->choices[0]['value'] ) ? $field->choices[0]['value'] : $field->choices[0]['text'];
464
		}
465
466
		// Otherwise, fall back on the inputs array
467
		if ( is_array( $field->inputs ) && ! empty( $field->inputs ) ) {
468
			return $field->inputs[0]['label'];
469
		}
470
471
		return null;
472
	}
473
474
	/**
475
	 * Update the `is_approved` entry meta value
476
	 *
477
	 * @since 1.7.6.1 `after_update_entry_update_approved_meta` was previously to be named `update_approved_meta`
478
	 * @since 1.17.1 Added $form_id parameter
479
	 *
480
	 * @param  int $entry_id ID of the Gravity Forms entry
481
	 * @param  string $status String whether entry is approved or not. `0` for not approved, `Approved` for approved.
482
	 * @param int $form_id ID of the form of the entry being updated. Improves query performance.
483
	 *
484
	 * @return void
485
	 */
486
	private static function update_approved_meta( $entry_id, $status, $form_id = 0 ) {
487
488
		if ( ! GravityView_Entry_Approval_Status::is_valid( $status ) ) {
489
			gravityview()->log->error( '$is_approved not valid value', array( 'data' => $status ) );
490
			return;
491
		}
492
493
		if ( ! function_exists( 'gform_update_meta' ) ) {
494
			gravityview()->log->error( '`gform_update_meta` does not exist.' );
495
			return;
496
		}
497
498
		$status = GravityView_Entry_Approval_Status::maybe_convert_status( $status );
499
500
		// update entry meta
501
		gform_update_meta( $entry_id, self::meta_key, $status, $form_id );
502
503
		/**
504
		 * @action `gravityview/approve_entries/updated` Triggered when an entry approval is updated
505
		 * @since 1.7.6.1
506
		 * @param  int $entry_id ID of the Gravity Forms entry
507
		 * @param  string|int $status String whether entry is approved or not. See GravityView_Entry_Approval_Status for valid statuses.
508
		 */
509
		do_action( 'gravityview/approve_entries/updated', $entry_id, $status );
510
511
		$action = GravityView_Entry_Approval_Status::get_key( $status );
512
513
		/**
514
		 * @action `gravityview/approve_entries/{$action}` Triggered when an entry approval is reset.
515
		 * $action can be 'approved', 'unapproved', or 'disapproved'
516
		 * Note: If you want this to work with Bulk Actions, run in a plugin rather than a theme; the bulk updates hook runs before themes are loaded.
517
		 * @since 1.7.6.1
518
		 * @since 1.18 Added "unapproved"
519
		 * @param  int $entry_id ID of the Gravity Forms entry
520
		 */
521
		do_action( 'gravityview/approve_entries/' . $action , $entry_id );
522
	}
523
524
	/**
525
	 * Calculate the approve field.input id
526
	 *
527
	 * @access public
528
	 * @static
529
	 * @param mixed $form GF Form or Form ID
530
	 * @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.
531
	 */
532 3
	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...
533
534 3
		if( empty( $form ) ) {
535
			return null;
536
		}
537
538 3
		if( !is_array( $form ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
539 3
			$form = GVCommon::get_form( $form );
540
		}
541
542 3
		$approved_column_id = null;
543
544
		/**
545
		 * @var string $key
546
		 * @var GF_Field $field
547
		 */
548 3
		foreach( $form['fields'] as $key => $field ) {
549
550 3
			$inputs = $field->get_entry_inputs();
551
552 3
			if( !empty( $field->gravityview_approved ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
553
				if ( ! empty( $inputs ) && !empty( $inputs[0]['id'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
554
					$approved_column_id = $inputs[0]['id'];
555
					break;
556
				}
557
			}
558
559
			// Note: This is just for backward compatibility from GF Directory plugin and old GV versions - when using i18n it may not work..
560 3
			if( 'checkbox' === $field->type && ! empty( $inputs ) ) {
561
				foreach ( $inputs as $input ) {
562
					if ( 'approved' === strtolower( $input['label'] ) ) {
563
						$approved_column_id = $input['id'];
564 3
						break;
565
					}
566
				}
567
			}
568
		}
569
570 3
		return $approved_column_id;
571
	}
572
573
	/**
574
	 * Maybe unapprove entry on edit.
575
	 *
576
	 * Called from gravityview/edit_entry/after_update
577
	 *
578
	 * @param array $form Gravity Forms form array
579
	 * @param string $entry_id Numeric ID of the entry that was updated
580
	 * @param GravityView_Edit_Entry_Render $edit This object
581
	 * @param GravityView_View_Data $gv_data The View data
582
	 *
583
	 * @return void
584
	 */
585 4
	public static function autounapprove( $form, $entry_id, $edit, $gv_data ) {
586
587 4
		$view_keys = array_keys( $gv_data->get_views() );
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View_Data::get_views() has been deprecated.

This method has been deprecated.

Loading history...
588
589 4
		$view = \GV\View::by_id( $view_keys[0] );
590
591 4
		if ( ! $view->settings->get( 'unapprove_edit' ) ) {
592 4
			return;
593
		}
594
595 1
		if ( GVCommon::has_cap( 'gravityview_moderate_entries' ) ) {
596 1
			return;
597
		}
598
599 1
		self::update_approved_meta( $entry_id, GravityView_Entry_Approval_Status::UNAPPROVED, $form['id'] );
600 1
	}
601
602
}
603
604
new GravityView_Entry_Approval;