PluginProbe
WP Ultimate Post Grid / 1.7.2
WP Ultimate Post Grid v1.7.2
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / helpers / assets.php

assets.php in WP Ultimate Post Grid 1.7.2, at helpers/assets.php

327 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WPUPG_Assets {
4
5 private $assets = array();
6
7 public function __construct()
8 {
9 add_action( 'init', array( $this, 'default_assets' ) );
10 add_action( 'init', array( $this, 'default_public_assets' ) );
11 add_action( 'admin_init', array( $this, 'default_admin_assets' ) );
12
13 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
14 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
15 add_action( 'admin_enqueue_scripts', array( $this, 'color_picker' ) );
16 }
17
18 public function default_assets()
19 {
20 $this->add(
21 array(
22 'file' => '/css/filter.css',
23 'public' => true,
24 'admin' => true,
25 ),
26 array(
27 'file' => '/css/pagination.css',
28 'public' => true,
29 'admin' => true,
30 )
31 );
32 }
33
34 public function default_public_assets()
35 {
36 if( !is_admin() ) {
37 $base_layout = WPUltimatePostGrid::option( 'grid_template_force_style', '1' ) == '1' ? 'layout_base_forced.css' : 'layout_base.css';
38
39 $this->add(
40 array(
41 'file' => '/css/grid.css',
42 'public' => true
43 ),
44 array(
45 'file' => '/css/' . $base_layout,
46 'public' => true
47 ),
48 array(
49 'name' => 'isotope',
50 'file' => '/vendor/isotope/isotope.pkgd.min.js',
51 'public' => true,
52 'deps' => array(
53 'jquery',
54 )
55 ),
56 array(
57 'name' => 'imagesloaded',
58 'file' => '/vendor/imagesloaded/imagesloaded.pkgd.min.js',
59 'public' => true,
60 'deps' => array(
61 'jquery',
62 )
63 ),
64 array(
65 'file' => '/js/grid.js',
66 'name' => 'wpupg_grid',
67 'public' => true,
68 'deps' => array(
69 'jquery',
70 'isotope',
71 'imagesloaded',
72 ),
73 'data' => array(
74 'name' => 'wpupg_public',
75 'ajax_url' => WPUltimatePostGrid::get()->helper('ajax')->url(),
76 'animationSpeed' => WPUltimatePostGrid::option( 'grid_animation_speed', '0.8' ) . 's',
77 'nonce' => wp_create_nonce( 'wpupg_grid' ),
78 'rtl' => is_rtl(),
79 ),
80 )
81 );
82 }
83 }
84
85 public function default_admin_assets()
86 {
87 $this->add(
88 array(
89 'file' => '/css/admin.css',
90 'admin' => true,
91 ),
92 array(
93 'file' => '/css/admin_form.css',
94 'admin' => true,
95 ),
96 array(
97 'file' => 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css',
98 'direct' => true,
99 'admin' => true,
100 'page' => 'grid_form',
101 ),
102 array(
103 'file' => WPUltimatePostGrid::get()->coreUrl . '/vendor/select2/css/select2.css',
104 'direct' => true,
105 'admin' => true,
106 'page' => 'grid_form',
107 ),
108 array(
109 'name' => 'select2wpupg',
110 'file' => '/vendor/select2/js/select2.js',
111 'admin' => true,
112 'page' => 'grid_form',
113 'deps' => array(
114 'jquery',
115 ),
116 ),
117 array(
118 'file' => '/vendor/nouislider/jquery.nouislider.min.css',
119 'public' => true,
120 'admin' => true,
121 'page' => 'grid_form',
122 ),
123 array(
124 'name' => 'nouislider',
125 'file' => '/vendor/nouislider/jquery.nouislider.all.min.js',
126 'public' => true,
127 'admin' => true,
128 'page' => 'grid_form',
129 'deps' => array(
130 'jquery',
131 )
132 ),
133 array(
134 'file' => '/js/admin_form.js',
135 'admin' => true,
136 'page' => 'grid_form',
137 'deps' => array(
138 'jquery',
139 'jquery-ui-datepicker',
140 'select2wpupg',
141 'nouislider',
142 'wp-color-picker',
143 ),
144 'data' => array(
145 'name' => 'wpupg_admin',
146 'ajax_url' => WPUltimatePostGrid::get()->helper('ajax')->url(),
147 ),
148 )
149 );
150 }
151
152 public function color_picker()
153 {
154 wp_enqueue_style( 'wp-color-picker' );
155 }
156
157 public function add()
158 {
159 $assets = func_get_args();
160
161 foreach( $assets as $asset )
162 {
163 if( isset( $asset['file'] ) ) {
164
165 if( !isset( $asset['type'] ) ) {
166 $asset['type'] = pathinfo( $asset['file'], PATHINFO_EXTENSION );
167 }
168
169 if( !isset( $asset['priority'] ) ) {
170 $asset['priority'] = 10;
171 }
172
173 // Set a URL and DIR variable
174 if( isset( $asset['direct'] ) && $asset['direct'] ) {
175 $asset['url'] = $asset['file'];
176 $asset['dir'] = $asset['file'];
177 } else {
178 $base_url = WPUltimatePostGrid::get()->coreUrl;
179 $base_dir = WPUltimatePostGrid::get()->coreDir;
180
181 if( isset( $asset['premium'] ) && $asset['premium'] ) {
182 $base_url = WPUltimatePostGridPremium::get()->premiumUrl;
183 $base_dir = WPUltimatePostGridPremium::get()->premiumDir;
184 }
185
186 $asset['url'] = $base_url . $asset['file'];
187 $asset['dir'] = $base_dir . $asset['file'];
188 }
189
190 $this->assets[] = $asset;
191 }
192 }
193 }
194
195 public function enqueue( $hook = '' )
196 {
197 $assets = $this->assets;
198
199 // Check which assets to enqueue
200 $css_to_enqueue = array();
201 $js_to_enqueue = array();
202
203 foreach( $assets as $asset )
204 {
205 // Check if asset is intended for admin or public side
206 if( !is_admin() && ( !isset( $asset['public'] ) || !$asset['public'] ) ) continue;
207 if( is_admin() && ( !isset( $asset['admin'] ) || !$asset['admin'] ) ) continue;
208
209 // Check if we're on a certain page
210 if( isset( $asset['page'] ) ) {
211 $screen = get_current_screen();
212
213 switch ( strtolower( $asset['page'] ) ) {
214
215 case 'grid_posts':
216 if( $hook != 'edit.php' || $screen->post_type != WPUPG_POST_TYPE ) continue 2; // Switch is consider a loop statement for continue
217 break;
218
219 case 'grid_form':
220 if( !in_array( $hook, array( 'post.php', 'post-new.php' ) ) || $screen->post_type != WPUPG_POST_TYPE ) continue 2;
221 break;
222
223 default:
224 if( $hook != strtolower( $asset['page'] ) ) continue 2;
225 break;
226 }
227 }
228
229 // Check for shortcode
230 if( isset( $asset['shortcode'] ) ) {
231 if( !$this->check_for_shortcode( $asset['shortcode'] ) ) continue;
232 }
233
234 // Check if setting equals value
235 if( isset( $asset['setting'] ) && count( $asset['setting'] ) == 2 ) {
236 if( WPUltimatePostGrid::option( $asset['setting'][0], $asset['setting'][1] ) != $asset['setting'][1] ) continue;
237 }
238
239 // Check if setting does not equal value
240 if( isset( $asset['setting_inverse'] ) && count( $asset['setting_inverse'] ) == 2 ) {
241 if( WPUltimatePostGrid::option( $asset['setting_inverse'][0], $asset['setting_inverse'][1] ) == $asset['setting_inverse'][1] ) continue;
242 }
243
244 // If we've made it here, this asset should be included
245 switch( strtolower( $asset['type'] ) ) {
246
247 case 'css':
248 $css_to_enqueue[] = $asset;
249 break;
250 case 'js':
251 $js_to_enqueue[] = $asset;
252 break;
253 }
254 }
255
256 // We've got the assets we need, enqueue them
257 if( count( $css_to_enqueue ) > 0 ) $this->enqueue_css( $css_to_enqueue );
258 if( count( $js_to_enqueue ) > 0 ) $this->enqueue_js( $js_to_enqueue );
259 }
260
261 private function enqueue_css( $assets )
262 {
263 $i = 1;
264 foreach( $assets as $asset ) {
265 $version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time();
266
267 wp_enqueue_style( 'wpupg_style' . $i, $asset['url'], false, $version, 'all' );
268 $i++;
269 }
270 }
271
272 private function enqueue_js( $assets )
273 {
274 $i = 1;
275 foreach( $assets as $asset ) {
276 $name = isset( $asset['name'] ) ? $asset['name'] : 'wpupg_script' . $i;
277 $deps = isset( $asset['deps'] ) ? $asset['deps'] : '';
278
279 $version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time();
280
281 wp_enqueue_script( $name, $asset['url'], $deps, $version, true );
282
283 if( isset( $asset['data'] ) && isset( $asset['data']['name'] ) ) {
284 $data_name = $asset['data']['name'];
285 unset( $asset['data']['name'] );
286
287 wp_localize_script( $name, $data_name, $asset['data'] );
288 }
289
290 $i++;
291 }
292 }
293
294 /**
295 * Check if any of the shortcodes is used in post
296 */
297 public function check_for_shortcode( $shortcodes ) {
298 if( !is_single() ) return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); // TODO Needs better solution
299
300 global $post;
301
302 if( function_exists( 'has_shortcode' ) ) {
303
304 // Multiple shortcodes passed, if one shortcode is in the post, return true
305 if( is_array( $shortcodes ) ) {
306 $shortcode_used = false;
307
308 foreach( $shortcodes as $shortcode ) {
309 if( isset( $post->post_content ) && has_shortcode( $post->post_content, $shortcode ) ) {
310 $shortcode_used = true;
311 }
312 }
313
314 return apply_filters( 'wpupg_check_for_shortcode', $shortcode_used, $shortcodes );
315 }
316
317 // Only one shortcode passed, true if that one is in the post
318 if( isset( $post->post_content ) && has_shortcode( $post->post_content, $shortcodes ) ) {
319 return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes );
320 }
321
322 return apply_filters( 'wpupg_check_for_shortcode', false, $shortcodes );
323 }
324
325 return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); // In older versions of WP just enqueue everything
326 }
327 }