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

class-gravityview-plugin-hooks-acf.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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 ) {
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;