|
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; |
|
|
|
|
|
|
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; |
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.