Completed
Pull Request — develop (#1547)
by Zack
17:03
created

GravityView_Admin_No_Conflict   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 220
ccs 0
cts 118
cp 0
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A add_hooks() 0 10 1
B no_conflict_scripts() 0 57 3
B no_conflict_styles() 0 50 6
B remove_conflicts() 0 29 6
B add_script_dependencies() 0 18 8
1
<?php
2
/**
3
 * GravityView's No-Conflict mode: disable scripts that interfere with the plugin.
4
 *
5
 * @since 1.17
6
 * @file class-gravityview-admin-no-conflict.php
7
 * @package GravityView
8
 * @subpackage includes\admin
9
 */
10
11
/**
12
 * @since 1.17
13
 */
14
class GravityView_Admin_No_Conflict {
15
16
	/**
17
	 * @since 1.17
18
	 */
19
	public function __construct() {
20
21
		if( ! is_admin() ) { return; }
22
		
23
		$this->add_hooks();
24
	}
25
26
	/**
27
	 * Add the hooks to fix script and style conflicts
28
	 *
29
	 * @since 1.17
30
	 *
31
	 * @return void
32
	 */
33
	private function add_hooks() {
34
		//Hooks for no-conflict functionality
35
		add_action( 'wp_print_scripts', array( $this, 'no_conflict_scripts' ), 1000);
36
		add_action( 'admin_print_footer_scripts', array( $this, 'no_conflict_scripts' ), 9);
37
38
		add_action( 'wp_print_styles', array( $this, 'no_conflict_styles' ), 1000);
39
		add_action( 'admin_print_styles', array( $this, 'no_conflict_styles' ), 11);
40
		add_action( 'admin_print_footer_scripts', array( $this, 'no_conflict_styles' ), 1);
41
		add_action( 'admin_footer', array( $this, 'no_conflict_styles' ), 1);
42
	}
43
44
	/**
45
	 * Callback to eliminate any non-registered script
46
	 *
47
	 * @since 1.17 Moved to GravityView_Admin_No_Conflict class
48
	 *
49
	 * @return void
50
	 */
51
	function no_conflict_scripts() {
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...
52
		global $wp_scripts;
53
54
		if( ! gravityview()->request->is_admin( '', null ) ) {
0 ignored issues
show
Unused Code introduced by
The call to Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Frontend_Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
			return;
56
		}
57
58
		$no_conflict_mode = gravityview()->plugin->settings->get( 'no-conflict-mode' );
59
60
		if( empty( $no_conflict_mode ) ) {
61
			return;
62
		}
63
64
		$wp_allowed_scripts = array(
65
			'common',
66
			'admin-bar',
67
			'autosave',
68
			'post',
69
			'inline-edit-post',
70
			'utils',
71
			'svg-painter',
72
			'wp-auth-check',
73
			'heartbeat',
74
			'media-editor',
75
			'media-upload',
76
			'thickbox',
77
			'wp-color-picker',
78
			'code-editor',
79
			'htmlhint',
80
			'htmlhint-kses',
81
			'jshint',
82
			'csslint',
83
			'jsonlint',
84
85
			// Settings
86
			'gv-admin-edd-license',
87
88
			// Common
89
			'select2-js',
90
			'qtip-js',
91
92
			// jQuery
93
			'jquery',
94
			'jquery-ui-core',
95
			'jquery-ui-sortable',
96
			'jquery-ui-datepicker',
97
			'jquery-ui-dialog',
98
			'jquery-ui-slider',
99
			'jquery-ui-dialog',
100
			'jquery-ui-tabs',
101
			'jquery-ui-draggable',
102
			'jquery-ui-droppable',
103
			'jquery-ui-accordion',
104
		);
105
106
		$this->remove_conflicts( $wp_scripts, $wp_allowed_scripts, 'scripts' );
107
	}
108
109
	/**
110
	 * Callback to eliminate any non-registered style
111
	 *
112
	 * @since 1.17 Moved to GravityView_Admin_No_Conflict class
113
	 *
114
	 * @return void
115
	 */
116
	function no_conflict_styles() {
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...
117
		global $wp_styles;
118
119
		if( ! gravityview()->request->is_admin( '', null ) ) {
0 ignored issues
show
Unused Code introduced by
The call to Frontend_Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
			return;
121
		}
122
123
		// Dequeue other jQuery styles even if no-conflict is off.
124
		// Terrible-looking tabs help no one.
125
		if( !empty( $wp_styles->registered ) )  {
126
			foreach ($wp_styles->registered as $key => $style) {
127
				if( preg_match( '/^(?:wp\-)?jquery/ism', $key ) ) {
128
					wp_dequeue_style( $key );
129
				}
130
			}
131
		}
132
133
		$no_conflict_mode = gravityview()->plugin->settings->get( 'no-conflict-mode' );
134
135
		// If no conflict is off, jQuery will suffice.
136
		if( empty( $no_conflict_mode ) ) {
137
			return;
138
		}
139
140
		$wp_allowed_styles = array(
141
			'admin-bar',
142
			'colors',
143
			'ie',
144
			'wp-auth-check',
145
			'media-views',
146
			'thickbox',
147
			'dashicons',
148
			'wp-jquery-ui-dialog',
149
			'jquery-ui-sortable',
150
			'code-editor',
151
152
			// Settings
153
			'gravityview_settings',
154
155
			// @todo qTip styles not loading for some reason!
156
			'jquery-qtip.js',
157
		);
158
159
		$this->remove_conflicts( $wp_styles, $wp_allowed_styles, 'styles' );
160
161
		/**
162
		 * @action `gravityview_remove_conflicts_after` Runs after no-conflict styles are removed. You can re-add styles here.
163
		 */
164
		do_action('gravityview_remove_conflicts_after');
165
	}
166
167
	/**
168
	 * Remove any style or script non-registered in the no conflict mode
169
	 *
170
	 * @since 1.17 Moved to GravityView_Admin_No_Conflict class
171
	 *
172
	 * @param  WP_Dependencies $wp_objects        Object of WP_Styles or WP_Scripts
173
	 * @param  string[] $required_objects   List of registered script/style handles
174
	 * @param  string $type              Either 'styles' or 'scripts'
175
	 * @return void
176
	 */
177
	private function remove_conflicts( &$wp_objects, $required_objects, $type = 'scripts' ) {
178
179
		/**
180
		 * @filter `gravityview_noconflict_{$type}` Modify the list of no conflict scripts or styles\n
181
		 * Filter is `gravityview_noconflict_scripts` or `gravityview_noconflict_styles`
182
		 * @param array $required_objects
183
		 */
184
		$required_objects = apply_filters( "gravityview_noconflict_{$type}", $required_objects );
185
186
		//reset queue
187
		$queue = array();
188
		foreach( $wp_objects->queue as $object ) {
189
			if( in_array( $object, $required_objects ) || preg_match('/gravityview|gf_|gravityforms/ism', $object ) ) {
190
				$queue[] = $object;
191
			}
192
		}
193
		$wp_objects->queue = $queue;
194
195
		$required_objects = $this->add_script_dependencies( $wp_objects->registered, $required_objects );
196
197
		//unregistering scripts
198
		$registered = array();
199
		foreach( $wp_objects->registered as $handle => $script_registration ){
200
			if( in_array( $handle, $required_objects ) ){
201
				$registered[ $handle ] = $script_registration;
202
			}
203
		}
204
		$wp_objects->registered = $registered;
205
	}
206
207
	/**
208
	 * Add dependencies
209
	 *
210
	 * @since 1.17 Moved to GravityView_Admin_No_Conflict class
211
	 *
212
	 * @param array $registered [description]
213
	 * @param array $scripts    [description]
214
	 */
215
	private function add_script_dependencies($registered, $scripts) {
216
217
		//gets all dependent scripts linked to the $scripts array passed
218
		do {
219
			$dependents = array();
220
			foreach ( $scripts as $script ) {
221
				$deps = isset( $registered[ $script ] ) && is_array( $registered[ $script ]->deps ) ? $registered[ $script ]->deps : array();
222
				foreach ( $deps as $dep ) {
223
					if ( ! in_array( $dep, $scripts ) && ! in_array( $dep, $dependents ) ) {
224
						$dependents[] = $dep;
225
					}
226
				}
227
			}
228
			$scripts = array_merge( $scripts, $dependents );
229
		} while ( ! empty( $dependents ) );
230
231
		return $scripts;
232
	}
233
}
234
235
new GravityView_Admin_No_Conflict;