Completed
Push — master ( 1b7620...a4da27 )
by Zack
12s
created

GravityView_Plugin_Hooks_ACF::fix_posted_fields()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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

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
 * Add Advanced Custom Fields customizations
4
 *
5
 * @file      class-gravityview-plugin-hooks-acf.php
6
 * @package   GravityView
7
 * @license   GPL2+
8
 * @author    Katz Web Services, Inc.
9
 * @link      http://gravityview.co
10
 * @copyright Copyright 2015, Katz Web Services, Inc.
11
 *
12
 * @since 1.16.5
13
 */
14
15
/**
16
 * @inheritDoc
17
 * @since 1.16.5
18
 */
19
class GravityView_Plugin_Hooks_ACF extends GravityView_Plugin_and_Theme_Hooks {
20
21
	/**
22
	 * @inheritDoc
23
	 * @since 1.16.5
24
	 */
25
	protected $function_name = 'acf';
26
27
	/**
28
	 * @since 1.16.5
29
	 */
30
	protected function add_hooks() {
31
		parent::add_hooks();
32
33
		add_filter( 'gravityview/data/parse/meta_keys', array( $this, 'add_meta_keys_from_post' ), 10, 2 );
34
35
		$this->fix_posted_fields();
36
	}
37
38
	/**
39
	 * @param array $meta_keys Existing meta keys to parse for [gravityview] shortcode
40
	 * @param int $post_id Current post ID
41
	 *
42
	 * @return array
43
	 */
44
	function add_meta_keys_from_post( $meta_keys = array(), $post_id = 0 ) {
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...
45
		global $wp_filter;
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...
46
47
		// Can never be too careful: double-check that ACF is active and the function exists
48
		if ( ! function_exists( 'get_field_objects' ) ) {
49
			return $meta_keys;
50
		}
51
52
		if( isset( $wp_filter['acf/format_value/type=wysiwyg'] ) && is_object( $wp_filter['acf/format_value/type=wysiwyg'] ) ) {
53
			$backup_filters = clone( $wp_filter['acf/format_value/type=wysiwyg'] );
54
		}
55
56
		// Prevent infinite loop by removing filters
57
		remove_all_filters( 'acf/format_value/type=wysiwyg' );
58
59
		$acf_keys = get_field_objects( $post_id, array( 'load_value' => false ) );
60
61
		// Restore existing filters
62
		if( ! empty( $backup_filters ) ) {
63
			$wp_filter['acf/format_value/type=wysiwyg'] = $backup_filters;
64
		}
65
66
		if( $acf_keys ) {
67
			return array_merge( array_keys( $acf_keys ), $meta_keys );
68
		}
69
70
		return $meta_keys;
71
	}
72
73
	/**
74
	 * ACF needs $_POST['fields'] to be an array. GV supports both serialized array and array, so we just process earlier.
75
	 *
76
	 * @since 1.16.5
77
	 *
78
	 * @return void
79
	 */
80
	private function fix_posted_fields() {
81
		if( is_admin() && isset( $_POST['action'] ) && isset( $_POST['post_type'] ) ) {
82
			if( 'editpost' === $_POST['action'] && 'gravityview' === $_POST['post_type'] ) {
83
				$_POST['fields'] = _gravityview_process_posted_fields();
84
			}
85
		}
86
	}
87
}
88
89
new GravityView_Plugin_Hooks_ACF;