| 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', '0' ) == '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 |
'animationShow' => $this->animation_string_to_array( WPUltimatePostGrid::option( 'grid_animation_show', 'opacity: 1' ) ), |
| 78 |
'animationHide' => $this->animation_string_to_array( WPUltimatePostGrid::option( 'grid_animation_hide', 'opacity: 0' ) ), |
| 79 |
'nonce' => wp_create_nonce( 'wpupg_grid' ), |
| 80 |
'rtl' => is_rtl(), |
| 81 |
'dropdown_hide_search' => WPUltimatePostGrid::option( 'filters_dropdown_hide_search', '0' ) == '1' ? true : false, |
| 82 |
'link_class' => WPUltimatePostGrid::option( 'grid_links_class', '' ), |
| 83 |
), |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private function animation_string_to_array( $string ) |
| 90 |
{ |
| 91 |
$array = array(); |
| 92 |
|
| 93 |
$options = explode( ', ', $string ); |
| 94 |
foreach( $options as $option ) { |
| 95 |
list( $k, $v ) = explode( ': ', $option ); |
| 96 |
$array[$k] = $v; |
| 97 |
} |
| 98 |
|
| 99 |
return $array; |
| 100 |
} |
| 101 |
|
| 102 |
public function default_admin_assets() |
| 103 |
{ |
| 104 |
$this->add( |
| 105 |
array( |
| 106 |
'file' => '/css/admin.css', |
| 107 |
'admin' => true, |
| 108 |
), |
| 109 |
array( |
| 110 |
'file' => '/css/admin_form.css', |
| 111 |
'admin' => true, |
| 112 |
), |
| 113 |
array( |
| 114 |
'file' => 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css', |
| 115 |
'direct' => true, |
| 116 |
'admin' => true, |
| 117 |
'page' => 'grid_form', |
| 118 |
), |
| 119 |
array( |
| 120 |
'file' => WPUltimatePostGrid::get()->coreUrl . '/vendor/select2/css/select2.css', |
| 121 |
'direct' => true, |
| 122 |
'admin' => true, |
| 123 |
'page' => 'grid_form', |
| 124 |
), |
| 125 |
array( |
| 126 |
'name' => 'select2wpupg', |
| 127 |
'file' => '/vendor/select2/js/select2.js', |
| 128 |
'admin' => true, |
| 129 |
'page' => 'grid_form', |
| 130 |
'deps' => array( |
| 131 |
'jquery', |
| 132 |
), |
| 133 |
), |
| 134 |
array( |
| 135 |
'file' => '/vendor/nouislider/jquery.nouislider.min.css', |
| 136 |
'public' => true, |
| 137 |
'admin' => true, |
| 138 |
'page' => 'grid_form', |
| 139 |
), |
| 140 |
array( |
| 141 |
'name' => 'nouislider', |
| 142 |
'file' => '/vendor/nouislider/jquery.nouislider.all.min.js', |
| 143 |
'public' => true, |
| 144 |
'admin' => true, |
| 145 |
'page' => 'grid_form', |
| 146 |
'deps' => array( |
| 147 |
'jquery', |
| 148 |
) |
| 149 |
), |
| 150 |
array( |
| 151 |
'file' => '/js/admin_post.js', |
| 152 |
'admin' => true, |
| 153 |
'deps' => array( |
| 154 |
'jquery', |
| 155 |
), |
| 156 |
'data' => array( |
| 157 |
'name' => 'wpupg_admin_post', |
| 158 |
'text_add_grid_image' => __( 'Add Grid Image', 'wp-ultimate-post-grid' ), |
| 159 |
'text_remove_grid_image' => __( 'Remove Grid Image', 'wp-ultimate-post-grid' ), |
| 160 |
), |
| 161 |
), |
| 162 |
array( |
| 163 |
'file' => '/js/admin_form.js', |
| 164 |
'admin' => true, |
| 165 |
'page' => 'grid_form', |
| 166 |
'deps' => array( |
| 167 |
'jquery', |
| 168 |
'jquery-ui-datepicker', |
| 169 |
'select2wpupg', |
| 170 |
'nouislider', |
| 171 |
'wp-color-picker', |
| 172 |
), |
| 173 |
'data' => array( |
| 174 |
'name' => 'wpupg_admin', |
| 175 |
'ajax_url' => WPUltimatePostGrid::get()->helper('ajax')->url(), |
| 176 |
), |
| 177 |
) |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
public function color_picker() |
| 182 |
{ |
| 183 |
wp_enqueue_style( 'wp-color-picker' ); |
| 184 |
} |
| 185 |
|
| 186 |
public function add() |
| 187 |
{ |
| 188 |
$assets = func_get_args(); |
| 189 |
|
| 190 |
foreach( $assets as $asset ) |
| 191 |
{ |
| 192 |
if( isset( $asset['file'] ) ) { |
| 193 |
|
| 194 |
if( !isset( $asset['type'] ) ) { |
| 195 |
$asset['type'] = pathinfo( $asset['file'], PATHINFO_EXTENSION ); |
| 196 |
} |
| 197 |
|
| 198 |
if( !isset( $asset['priority'] ) ) { |
| 199 |
$asset['priority'] = 10; |
| 200 |
} |
| 201 |
|
| 202 |
// Set a URL and DIR variable |
| 203 |
if( isset( $asset['direct'] ) && $asset['direct'] ) { |
| 204 |
$asset['url'] = $asset['file']; |
| 205 |
$asset['dir'] = $asset['file']; |
| 206 |
} else { |
| 207 |
$base_url = WPUltimatePostGrid::get()->coreUrl; |
| 208 |
$base_dir = WPUltimatePostGrid::get()->coreDir; |
| 209 |
|
| 210 |
if( isset( $asset['premium'] ) && $asset['premium'] ) { |
| 211 |
$base_url = WPUltimatePostGridPremium::get()->premiumUrl; |
| 212 |
$base_dir = WPUltimatePostGridPremium::get()->premiumDir; |
| 213 |
} |
| 214 |
|
| 215 |
$asset['url'] = $base_url . $asset['file']; |
| 216 |
$asset['dir'] = $base_dir . $asset['file']; |
| 217 |
} |
| 218 |
|
| 219 |
$this->assets[] = $asset; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public function enqueue( $hook = '' ) |
| 225 |
{ |
| 226 |
$assets = $this->assets; |
| 227 |
|
| 228 |
// Check which assets to enqueue |
| 229 |
$css_to_enqueue = array(); |
| 230 |
$js_to_enqueue = array(); |
| 231 |
|
| 232 |
foreach( $assets as $asset ) |
| 233 |
{ |
| 234 |
// Check if asset is intended for admin or public side |
| 235 |
if( !is_admin() && ( !isset( $asset['public'] ) || !$asset['public'] ) ) continue; |
| 236 |
if( is_admin() && ( !isset( $asset['admin'] ) || !$asset['admin'] ) ) continue; |
| 237 |
|
| 238 |
// Check if we're on a certain page |
| 239 |
if( isset( $asset['page'] ) ) { |
| 240 |
$screen = get_current_screen(); |
| 241 |
|
| 242 |
switch ( strtolower( $asset['page'] ) ) { |
| 243 |
|
| 244 |
case 'grid_posts': |
| 245 |
if( $hook != 'edit.php' || $screen->post_type != WPUPG_POST_TYPE ) continue 2; // Switch is consider a loop statement for continue |
| 246 |
break; |
| 247 |
|
| 248 |
case 'grid_form': |
| 249 |
if( !in_array( $hook, array( 'post.php', 'post-new.php' ) ) || $screen->post_type != WPUPG_POST_TYPE ) continue 2; |
| 250 |
break; |
| 251 |
|
| 252 |
default: |
| 253 |
if( $hook != strtolower( $asset['page'] ) ) continue 2; |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// Check for shortcode |
| 259 |
if( isset( $asset['shortcode'] ) ) { |
| 260 |
if( !$this->check_for_shortcode( $asset['shortcode'] ) ) continue; |
| 261 |
} |
| 262 |
|
| 263 |
// Check if setting equals value |
| 264 |
if( isset( $asset['setting'] ) && count( $asset['setting'] ) == 2 ) { |
| 265 |
if( WPUltimatePostGrid::option( $asset['setting'][0], $asset['setting'][1] ) != $asset['setting'][1] ) continue; |
| 266 |
} |
| 267 |
|
| 268 |
// Check if setting does not equal value |
| 269 |
if( isset( $asset['setting_inverse'] ) && count( $asset['setting_inverse'] ) == 2 ) { |
| 270 |
if( WPUltimatePostGrid::option( $asset['setting_inverse'][0], $asset['setting_inverse'][1] ) == $asset['setting_inverse'][1] ) continue; |
| 271 |
} |
| 272 |
|
| 273 |
// If we've made it here, this asset should be included |
| 274 |
switch( strtolower( $asset['type'] ) ) { |
| 275 |
|
| 276 |
case 'css': |
| 277 |
$css_to_enqueue[] = $asset; |
| 278 |
break; |
| 279 |
case 'js': |
| 280 |
$js_to_enqueue[] = $asset; |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
// Hooks for assets |
| 286 |
$css_to_enqueue = apply_filters( 'wpupg_assets_css', $css_to_enqueue ); |
| 287 |
$js_to_enqueue = apply_filters( 'wpupg_assets_js', $js_to_enqueue ); |
| 288 |
|
| 289 |
// We've got the assets we need, enqueue them |
| 290 |
if( count( $css_to_enqueue ) > 0 ) $this->enqueue_css( $css_to_enqueue ); |
| 291 |
if( count( $js_to_enqueue ) > 0 ) $this->enqueue_js( $js_to_enqueue ); |
| 292 |
} |
| 293 |
|
| 294 |
private function enqueue_css( $assets ) |
| 295 |
{ |
| 296 |
$i = 1; |
| 297 |
foreach( $assets as $asset ) { |
| 298 |
$version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time(); |
| 299 |
|
| 300 |
wp_enqueue_style( 'wpupg_style' . $i, $asset['url'], false, $version, 'all' ); |
| 301 |
$i++; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
private function enqueue_js( $assets ) |
| 306 |
{ |
| 307 |
$i = 1; |
| 308 |
foreach( $assets as $asset ) { |
| 309 |
$name = isset( $asset['name'] ) ? $asset['name'] : 'wpupg_script' . $i; |
| 310 |
$deps = isset( $asset['deps'] ) ? $asset['deps'] : ''; |
| 311 |
|
| 312 |
$version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time(); |
| 313 |
|
| 314 |
wp_enqueue_script( $name, $asset['url'], $deps, $version, true ); |
| 315 |
|
| 316 |
if( isset( $asset['data'] ) && isset( $asset['data']['name'] ) ) { |
| 317 |
$data_name = $asset['data']['name']; |
| 318 |
unset( $asset['data']['name'] ); |
| 319 |
|
| 320 |
wp_localize_script( $name, $data_name, $asset['data'] ); |
| 321 |
} |
| 322 |
|
| 323 |
$i++; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Check if any of the shortcodes is used in post |
| 329 |
*/ |
| 330 |
public function check_for_shortcode( $shortcodes ) { |
| 331 |
if( !is_single() ) return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); // TODO Needs better solution |
| 332 |
|
| 333 |
global $post; |
| 334 |
|
| 335 |
if( function_exists( 'has_shortcode' ) ) { |
| 336 |
|
| 337 |
// Multiple shortcodes passed, if one shortcode is in the post, return true |
| 338 |
if( is_array( $shortcodes ) ) { |
| 339 |
$shortcode_used = false; |
| 340 |
|
| 341 |
foreach( $shortcodes as $shortcode ) { |
| 342 |
if( isset( $post->post_content ) && has_shortcode( $post->post_content, $shortcode ) ) { |
| 343 |
$shortcode_used = true; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
return apply_filters( 'wpupg_check_for_shortcode', $shortcode_used, $shortcodes ); |
| 348 |
} |
| 349 |
|
| 350 |
// Only one shortcode passed, true if that one is in the post |
| 351 |
if( isset( $post->post_content ) && has_shortcode( $post->post_content, $shortcodes ) ) { |
| 352 |
return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); |
| 353 |
} |
| 354 |
|
| 355 |
return apply_filters( 'wpupg_check_for_shortcode', false, $shortcodes ); |
| 356 |
} |
| 357 |
|
| 358 |
return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); // In older versions of WP just enqueue everything |
| 359 |
} |
| 360 |
} |