GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-admin-no-conflict.php
Go to the documentation of this file.
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  */
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() {
52  global $wp_scripts;
53 
54  if( ! gravityview()->request->is_admin( '', null ) ) {
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  // Common
86  'select2-js',
87  'qtip-js',
88 
89  // jQuery
90  'jquery',
91  'jquery-ui-core',
92  'jquery-ui-sortable',
93  'jquery-ui-datepicker',
94  'jquery-ui-dialog',
95  'jquery-ui-slider',
96  'jquery-ui-dialog',
97  'jquery-ui-tabs',
98  'jquery-ui-draggable',
99  'jquery-ui-droppable',
100  'jquery-ui-accordion',
101  );
102 
103  $this->remove_conflicts( $wp_scripts, $wp_allowed_scripts, 'scripts' );
104  }
105 
106  /**
107  * Callback to eliminate any non-registered style
108  *
109  * @since 1.17 Moved to GravityView_Admin_No_Conflict class
110  *
111  * @return void
112  */
113  function no_conflict_styles() {
114  global $wp_styles;
115 
116  if( ! gravityview()->request->is_admin( '', null ) ) {
117  return;
118  }
119 
120  // Dequeue other jQuery styles even if no-conflict is off.
121  // Terrible-looking tabs help no one.
122  if( !empty( $wp_styles->registered ) ) {
123  foreach ($wp_styles->registered as $key => $style) {
124  if( preg_match( '/^(?:wp\-)?jquery/ism', $key ) ) {
125  wp_dequeue_style( $key );
126  }
127  }
128  }
129 
130  $no_conflict_mode = gravityview()->plugin->settings->get( 'no_conflict_mode' );
131 
132  // If no conflict is off, jQuery will suffice.
133  if( empty( $no_conflict_mode ) ) {
134  return;
135  }
136 
137  $wp_allowed_styles = array(
138  'admin-bar',
139  'colors',
140  'ie',
141  'wp-auth-check',
142  'media-views',
143  'thickbox',
144  'dashicons',
145  'wp-jquery-ui-dialog',
146  'jquery-ui-sortable',
147  'code-editor',
148 
149  // Settings
150  'gravityview_settings',
151 
152  // @todo qTip styles not loading for some reason!
153  'jquery-qtip.js',
154  );
155 
156  $this->remove_conflicts( $wp_styles, $wp_allowed_styles, 'styles' );
157 
158  /**
159  * @action `gravityview_remove_conflicts_after` Runs after no-conflict styles are removed. You can re-add styles here.
160  */
161  do_action('gravityview_remove_conflicts_after');
162  }
163 
164  /**
165  * Remove any style or script non-registered in the no conflict mode
166  *
167  * @since 1.17 Moved to GravityView_Admin_No_Conflict class
168  *
169  * @param WP_Dependencies $wp_objects Object of WP_Styles or WP_Scripts
170  * @param string[] $required_objects List of registered script/style handles
171  * @param string $type Either 'styles' or 'scripts'
172  * @return void
173  */
174  private function remove_conflicts( &$wp_objects, $required_objects, $type = 'scripts' ) {
175 
176  /**
177  * @filter `gravityview_noconflict_{$type}` Modify the list of no conflict scripts or styles\n
178  * Filter is `gravityview_noconflict_scripts` or `gravityview_noconflict_styles`
179  * @param array $required_objects
180  */
181  $required_objects = apply_filters( "gravityview_noconflict_{$type}", $required_objects );
182 
183  $allow_prefixes = array(
184  'gravityview',
185  'gf_',
186  'gk_',
187  'gravityforms',
188  'gform_',
189  'jquery-ui-',
190  );
191 
192  $allow_regex = '/^' . implode( '|', $allow_prefixes ) . '/ism';
193 
194  //reset queue
195  $queue = array();
196  foreach( $wp_objects->queue as $object ) {
197  if( in_array( $object, $required_objects ) || preg_match( $allow_regex, $object ) ) {
198  $queue[] = $object;
199  }
200  }
201  $wp_objects->queue = $queue;
202 
203  $required_objects = $this->add_script_dependencies( $wp_objects->registered, $required_objects );
204 
205  //unregistering scripts
206  $registered = array();
207  foreach( $wp_objects->registered as $handle => $script_registration ){
208  if( in_array( $handle, $required_objects ) || preg_match( $allow_regex, $handle ) ){
209  $registered[ $handle ] = $script_registration;
210  }
211  }
212  $wp_objects->registered = $registered;
213  }
214 
215  /**
216  * Add dependencies
217  *
218  * @since 1.17 Moved to GravityView_Admin_No_Conflict class
219  *
220  * @param array $registered [description]
221  * @param array $scripts [description]
222  */
223  private function add_script_dependencies($registered, $scripts) {
224 
225  //gets all dependent scripts linked to the $scripts array passed
226  do {
227  $dependents = array();
228  foreach ( $scripts as $script ) {
229  $deps = isset( $registered[ $script ] ) && is_array( $registered[ $script ]->deps ) ? $registered[ $script ]->deps : array();
230  foreach ( $deps as $dep ) {
231  if ( ! in_array( $dep, $scripts ) && ! in_array( $dep, $dependents ) ) {
232  $dependents[] = $dep;
233  }
234  }
235  }
236  $scripts = array_merge( $scripts, $dependents );
237  } while ( ! empty( $dependents ) );
238 
239  return $scripts;
240  }
241 }
242 
remove_conflicts(&$wp_objects, $required_objects, $type='scripts')
Remove any style or script non-registered in the no conflict mode.
no_conflict_styles()
Callback to eliminate any non-registered style.
no_conflict_scripts()
Callback to eliminate any non-registered script.
add_script_dependencies($registered, $scripts)
Add dependencies.
gravityview()
The main GravityView wrapper function.
add_hooks()
Add the hooks to fix script and style conflicts.