Completed
Push — master ( cf8268...397f5b )
by Zack
24:22 queued 11:06
created

merge_meta_keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles, as well as
4
 * add post meta keys for GravityView to parse when checking for the existence of shortcodes in content.
5
 *
6
 * @file      abstract-gravityview-plugin-and-theme-hooks.php
7
 * @package   GravityView
8
 * @license   GPL2+
9
 * @author    Katz Web Services, Inc.
10
 * @link      http://gravityview.co
11
 * @copyright Copyright 2015, Katz Web Services, Inc.
12
 *
13
 * @since 1.15.2
14
 */
15
16
/**
17
 * Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles, as well as
18
 * add post meta keys for GravityView to parse when checking for the existence of shortcodes in content.
19
 *
20
 * @since 1.15.2
21
 */
22
abstract class GravityView_Plugin_and_Theme_Hooks {
23
24
	/**
25
	 * @type string Optional. Class that should be exist in a plugin or theme. Used to check whether plugin is active.
26
	 * @since 1.15.2
27
	 */
28
	protected $class_name = false;
29
30
	/**
31
	 * @type string Optional. Function that should be exist in a plugin or theme. Used to check whether plugin is active.
32
	 * @since 1.15.2
33
	 */
34
	protected $function_name = false;
35
36
	/**
37
	 * @type string Optional. Constant that should be defined by plugin or theme. Used to check whether plugin is active.
38
	 * @since 1.15.2
39
	 */
40
	protected $constant_name = false;
41
42
	/**
43
	 * Define the keys to be parsed by the `gravityview/view_collection/from_post/meta_keys` hook
44
	 * @see View_Collection::from_post
45
	 * @since 2.0
46
	 * @type array
47
	 */
48
	protected $content_meta_keys = array();
49
50
	/**
51
	 * Define the keys to be parsed by the `gravityview/data/parse/meta_keys` hook
52
	 * @see GravityView_View_Data::parse_post_meta
53
	 * @deprecated 2.0
54
	 * @since 1.15.2
55
	 * @type array
56
	 */
57
	protected $meta_keys = array();
58
59
	/**
60
	 * Define script handles used by the theme or plugin to be added to allowed no-conflict scripts
61
	 * @see GravityView_Admin::remove_conflicts
62
	 * @since 1.15.2
63
	 * @type array
64
	 */
65
	protected $script_handles = array();
66
67
	/**
68
	 * Define style handles used by the theme or plugin to be added to allowed no-conflict styles
69
	 * @see GravityView_Admin::remove_conflicts
70
	 * @since 1.15.2
71
	 * @type array
72
	 */
73
	protected $style_handles = array();
74
75
	/**
76
	 * Define features in the admin editor used by the theme or plugin to be used when registering the GravityView post type
77
	 * @see \GV\Entry::get_endpoint_name
78
	 * @since 1.15.2
79
	 * @type array
80
	 */
81
	protected $post_type_support = array();
82
83
	/**
84
	 * GravityView_Theme_Support constructor.
85
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
86
	 */
87
	public function __construct() {
88
		add_action( 'wp_loaded', array( $this, '_wp_loaded' ) );
89
	}
90
91
	/**
92
	 * Fired when all themes and plugins have been loaded.
93
	 * This makes sure we can reliably detect functions and classes.
94
	 * @internal
95
	 * @return void
96
	 */
97
	public function _wp_loaded() {
98
		$this->maybe_add_hooks();
99
	}
100
101
	/**
102
	 * Check whether plugin or theme exists. If so, add hooks.
103
	 * This is to reduce load time, since `apply_filters()` isn't free.
104
	 * If the class name or function name or constant exists for a plugin or theme, add hooks
105
	 * If the class/function/definition aren't speicifed add the hooks
106
	 *
107
	 * @since 1.15.2
108
	 * @return void
109
	 */
110
	private function maybe_add_hooks() {
111
		$class_exists = $this->class_name && class_exists( $this->class_name );
112
		$function_exists = $this->function_name && function_exists( $this->function_name );
113
		$constant_defined = $this->constant_name && defined("{$this->constant_name}");
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...
114
115
		if( $class_exists || $function_exists || $constant_defined ) {
116
			$this->add_hooks();
117
		}
118
	}
119
120
	/**
121
	 * Add filters for meta key and script/style handles, if defined.
122
	 * @since 1.15.2
123
	 * @return void
124
	 */
125
	protected function add_hooks() {
126
127
		if( $this->meta_keys ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->meta_keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Deprecated Code introduced by
The property GravityView_Plugin_and_Theme_Hooks::$meta_keys has been deprecated with message: 2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
128
			add_filter( 'gravityview/data/parse/meta_keys', array( $this, 'merge_meta_keys' ), 10, 2 );
129
		}
130
131
		if( $this->content_meta_keys ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->content_meta_keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132
			add_filter( 'gravityview/view_collection/from_post/meta_keys', array( $this, 'merge_content_meta_keys' ), 10, 3 );
133
		}
134
135
		if( $this->script_handles ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->script_handles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
136
			add_filter( 'gravityview_noconflict_scripts', array( $this, 'merge_noconflict_scripts' ) );
137
		}
138
139
		if( $this->style_handles ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->style_handles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
140
			add_filter( 'gravityview_noconflict_styles', array( $this, 'merge_noconflict_styles' ) );
141
		}
142
143
		if( $this->post_type_support ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->post_type_support of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
			add_filter( 'gravityview_post_type_support', array( $this, 'merge_post_type_support' ), 10, 2 );
145
		}
146
	}
147
148
	/**
149
	 * Merge plugin or theme post type support definitions with existing support values
150
	 *
151
	 * @since 1.15.2
152
	 *
153
	 * @param array $supports Array of features associated with a functional area of the edit screen.
154
	 * @param boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
155
	 *
156
	 * @return array Array of features associated with a functional area of the edit screen, merged with existing values
157
	 */
158
	public function merge_post_type_support( $supports = array(), $is_hierarchical = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $is_hierarchical 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...
159
		$supports = array_merge( $this->post_type_support, $supports );
160
		return $supports;
161
	}
162
163
	/**
164
	 * Merge plugin or theme styles with existing no-conflict styles
165
	 *
166
	 * @since 1.15.2
167
	 *
168
	 * @param array $handles Array of style handles, as registered with WordPress
169
	 *
170
	 * @return array Handles, merged with existing styles
171
	 */
172
	public function merge_noconflict_styles( $handles ) {
173
		$handles = array_merge( $this->style_handles, $handles );
174
		return $handles;
175
	}
176
177
	/**
178
	 * Merge plugin or theme scripts with existing no-conflict scripts
179
	 *
180
	 * @since 1.15.2
181
	 *
182
	 * @param array $handles Array of script handles, as registered with WordPress
183
	 *
184
	 * @return array Handles, merged with existing scripts
185
	 */
186
	public function merge_noconflict_scripts( $handles ) {
187
		$handles = array_merge( $this->script_handles, $handles );
188
		return $handles;
189
	}
190
191
	/**
192
	 * Merge plugin or theme meta keys that store shortcode data with existing keys to check
193
	 *
194
	 * @since 1.15.2
195
	 *
196
	 * @deprecated 2.0.7
197
	 *
198
	 * @param array $handles Array of meta keys to check for existence of shortcodes
0 ignored issues
show
Bug introduced by
There is no parameter named $handles. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
199
	 * @param int $post_id The ID being checked by GravityView
200
	 *
201
	 * @return array Meta key array, merged with existing meta keys
202
	 */
203
	public function merge_meta_keys( $meta_keys = array(), $post_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id 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...
204
		return array_merge( $this->meta_keys, $meta_keys );
0 ignored issues
show
Deprecated Code introduced by
The property GravityView_Plugin_and_Theme_Hooks::$meta_keys has been deprecated with message: 2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
205
	}
206
207
	/**
208
	 * Merge plugin or theme meta keys that store shortcode data with existing keys to check
209
	 *
210
	 * @since 2.0.7
211
	 *
212
	 * @param array $handles Array of meta keys to check for existence of shortcodes
0 ignored issues
show
Bug introduced by
There is no parameter named $handles. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
213
	 * @param \WP_Post $post The ID being checked by GravityView
214
	 *
215
	 * @return array Meta key array, merged with existing meta keys
216
	 */
217
	public function merge_content_meta_keys( $meta_keys = array(), $post = null, & $views = null ) {
218
		return array_merge( $this->content_meta_keys, $meta_keys );
219
	}
220
221
}