Completed
Push — master ( cf9740...0c1274 )
by Zack
19:03 queued 03:33
created

GravityView_Plugin_and_Theme_Hooks   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 170
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A _wp_loaded() 0 3 1
B maybe_add_hooks() 0 9 7
B add_hooks() 0 17 5
A merge_post_type_support() 0 4 1
A merge_noconflict_styles() 0 4 1
A merge_noconflict_scripts() 0 4 1
A merge_content_meta_keys() 0 3 1
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/data/parse/meta_keys` hook
44
	 * @see GravityView_View_Data::parse_post_meta
45
	 * @since 1.15.2
46
	 * @type array
47
	 */
48
	protected $content_meta_keys = array();
49
50
	/**
51
	 * Define script handles used by the theme or plugin to be parsed by the `gravityview/data/parse/meta_keys` hook
52
	 * @see GravityView_Admin::remove_conflicts
53
	 * @since 1.15.2
54
	 * @type array
55
	 */
56
	protected $script_handles = array();
57
58
	/**
59
	 * Define style handles used by the theme or plugin to be parsed by the `gravityview/data/parse/meta_keys` hook
60
	 * @see GravityView_Admin::remove_conflicts
61
	 * @since 1.15.2
62
	 * @type array
63
	 */
64
	protected $style_handles = array();
65
66
	/**
67
	 * Define features in the admin editor used by the theme or plugin to be used when registering the GravityView post type
68
	 * @see \GV\Entry::get_endpoint_name
69
	 * @since 1.15.2
70
	 * @type array
71
	 */
72
	protected $post_type_support = array();
73
74
	/**
75
	 * GravityView_Theme_Support constructor.
76
	 * @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...
77
	 */
78
	public function __construct() {
79
		add_action( 'wp_loaded', array( $this, '_wp_loaded' ) );
80
	}
81
82
	/**
83
	 * Fired when all themes and plugins have been loaded.
84
	 * This makes sure we can reliably detect functions and classes.
85
	 * @internal
86
	 * @return void
87
	 */
88
	public function _wp_loaded() {
89
		$this->maybe_add_hooks();
90
	}
91
92
	/**
93
	 * Check whether plugin or theme exists. If so, add hooks.
94
	 * This is to reduce load time, since `apply_filters()` isn't free.
95
	 * If the class name or function name or constant exists for a plugin or theme, add hooks
96
	 * If the class/function/definition aren't speicifed add the hooks
97
	 *
98
	 * @since 1.15.2
99
	 * @return void
100
	 */
101
	private function maybe_add_hooks() {
102
		$class_exists = $this->class_name && class_exists( $this->class_name );
103
		$function_exists = $this->function_name && function_exists( $this->function_name );
104
		$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...
105
106
		if( $class_exists || $function_exists || $constant_defined ) {
107
			$this->add_hooks();
108
		}
109
	}
110
111
	/**
112
	 * Add filters for meta key and script/style handles, if defined.
113
	 * @since 1.15.2
114
	 * @return void
115
	 */
116
	protected function add_hooks() {
117
		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...
118
			add_filter( 'gravityview/data/parse/meta_keys', array( $this, 'merge_content_meta_keys' ), 10, 2 );
119
		}
120
121
		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...
122
			add_filter( 'gravityview_noconflict_scripts', array( $this, 'merge_noconflict_scripts' ) );
123
		}
124
125
		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...
126
			add_filter( 'gravityview_noconflict_styles', array( $this, 'merge_noconflict_styles' ) );
127
		}
128
129
		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...
130
			add_filter( 'gravityview_post_type_support', array( $this, 'merge_post_type_support' ), 10, 2 );
131
		}
132
	}
133
134
	/**
135
	 * Merge plugin or theme post type support definitions with existing support values
136
	 *
137
	 * @since 1.15.2
138
	 *
139
	 * @param array $supports Array of features associated with a functional area of the edit screen.
140
	 * @param boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
141
	 *
142
	 * @return array Array of features associated with a functional area of the edit screen, merged with existing values
143
	 */
144
	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...
145
		$supports = array_merge( $this->post_type_support, $supports );
146
		return $supports;
147
	}
148
149
	/**
150
	 * Merge plugin or theme styles with existing no-conflict styles
151
	 *
152
	 * @since 1.15.2
153
	 *
154
	 * @param array $handles Array of style handles, as registered with WordPress
155
	 *
156
	 * @return array Handles, merged with existing styles
157
	 */
158
	public function merge_noconflict_styles( $handles ) {
159
		$handles = array_merge( $this->style_handles, $handles );
160
		return $handles;
161
	}
162
163
	/**
164
	 * Merge plugin or theme scripts with existing no-conflict scripts
165
	 *
166
	 * @since 1.15.2
167
	 *
168
	 * @param array $handles Array of script handles, as registered with WordPress
169
	 *
170
	 * @return array Handles, merged with existing scripts
171
	 */
172
	public function merge_noconflict_scripts( $handles ) {
173
		$handles = array_merge( $this->script_handles, $handles );
174
		return $handles;
175
	}
176
177
	/**
178
	 * Merge plugin or theme meta keys that store shortcode data with existing keys to check
179
	 *
180
	 * @since 1.15.2
181
	 *
182
	 * @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...
183
	 * @param int $post_id The ID being checked by GravityView
184
	 *
185
	 * @return array Meta key array, merged with existing meta keys
186
	 */
187
	public function merge_content_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...
188
		return array_merge( $this->content_meta_keys, $meta_keys );
189
	}
190
191
}