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