| @@ -5,53 +5,86 @@ | ||
| 5 | 5 | private $assets = array(); |
| 6 | 6 | |
| 7 | 7 | public function __construct() |
| 8 | 8 | { |
| 9 | - add_action( 'wp_enqueue_scripts', array( $this, 'public_assets' ) ); | |
| 10 | - add_action( 'admin_enqueue_scripts', array( $this, 'admin_assets' ) ); | |
| 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' ) ); | |
| 11 | 16 | } |
| 12 | 17 | |
| 13 | - public function public_assets() | |
| 18 | + public function default_assets() | |
| 14 | 19 | { |
| 15 | - $version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time(); | |
| 16 | - $base_layout = WPUltimatePostGrid::option( 'grid_template_force_style', '0' ) == '1' ? 'public-forced.css' : 'public.css'; | |
| 17 | - | |
| 18 | - wp_enqueue_style( 'wpupg_public', WPUltimatePostGrid::get()->coreUrl . '/dist/' . $base_layout, array(), $version, 'all' ); | |
| 19 | - wp_register_script( 'wpupg_public', WPUltimatePostGrid::get()->coreUrl . '/dist/public.js', array( 'jquery' ), $version, true ); | |
| 20 | - | |
| 21 | - wp_localize_script( 'wpupg_public', 'wpupg_public', array( | |
| 22 | - 'ajax_url' => WPUltimatePostGrid::get()->helper('ajax')->url(), | |
| 23 | - 'animationSpeed' => WPUltimatePostGrid::option( 'grid_animation_speed', '0.8' ) . 's', | |
| 24 | - 'animationShow' => $this->animation_string_to_array( WPUltimatePostGrid::option( 'grid_animation_show', 'opacity: 1' ) ), | |
| 25 | - 'animationHide' => $this->animation_string_to_array( WPUltimatePostGrid::option( 'grid_animation_hide', 'opacity: 0' ) ), | |
| 26 | - 'nonce' => wp_create_nonce( 'wpupg_grid' ), | |
| 27 | - 'rtl' => is_rtl(), | |
| 28 | - 'dropdown_hide_search' => WPUltimatePostGrid::option( 'filters_dropdown_hide_search', '0' ) == '1' ? true : false, | |
| 29 | - 'link_class' => WPUltimatePostGrid::option( 'grid_links_class', '' ), | |
| 30 | - )); | |
| 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 | + ); | |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | - public function admin_assets( $hook ) | |
| 34 | + public function default_public_assets() | |
| 34 | 35 | { |
| 35 | - $version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time(); | |
| 36 | + if( !is_admin() ) { | |
| 37 | + $base_layout = WPUltimatePostGrid::option( 'grid_template_force_style', '0' ) == '1' ? 'layout_base_forced.css' : 'layout_base.css'; | |
| 36 | 38 | |
| 37 | - $screen = get_current_screen(); | |
| 38 | - if ( in_array( $hook, array( 'post.php', 'post-new.php' ) ) && WPUPG_POST_TYPE === $screen->post_type ) { | |
| 39 | - wp_enqueue_style( 'wp-color-picker' ); | |
| 40 | - wp_enqueue_style( 'wpupg_admin_form', WPUltimatePostGrid::get()->coreUrl . '/dist/admin-form.css', array(), $version, 'all' ); | |
| 41 | - wp_enqueue_script( 'wpupg_admin_form', WPUltimatePostGrid::get()->coreUrl . '/dist/admin-form.js', array( 'jquery', 'jquery-ui-datepicker', 'wp-color-picker' ), $version, true ); | |
| 42 | - | |
| 43 | - wp_localize_script( 'wpupg_admin_form', 'wpupg_admin', array( | |
| 44 | - 'ajax_url' => WPUltimatePostGrid::get()->helper('ajax')->url(), | |
| 45 | - )); | |
| 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 | + ); | |
| 46 | 86 | } |
| 47 | - | |
| 48 | - wp_enqueue_style( 'wpupg_admin_post', WPUltimatePostGrid::get()->coreUrl . '/dist/admin.css', array(), $version, 'all' ); | |
| 49 | - wp_enqueue_script( 'wpupg_admin_post', WPUltimatePostGrid::get()->coreUrl . '/dist/admin.js', array( 'jquery' ), $version, true ); | |
| 50 | - wp_localize_script( 'wpupg_admin_post', 'wpupg_admin_post', array( | |
| 51 | - 'text_add_grid_image' => __( 'Add Grid Image', 'wp-ultimate-post-grid' ), | |
| 52 | - 'text_remove_grid_image' => __( 'Remove Grid Image', 'wp-ultimate-post-grid' ), | |
| 53 | - )); | |
| 54 | 87 | } |
| 55 | 88 | |
| 56 | 89 | private function animation_string_to_array( $string ) |
| 57 | 90 | { |
| @@ -65,8 +98,92 @@ | ||
| 65 | 98 | |
| 66 | 99 | return $array; |
| 67 | 100 | } |
| 68 | 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 | + | |
| 69 | 186 | public function add() |
| 70 | 187 | { |
| 71 | 188 | $assets = func_get_args(); |
| 72 | 189 | |
| @@ -101,6 +218,139 @@ | ||
| 101 | 218 | |
| 102 | 219 | $this->assets[] = $asset; |
| 103 | 220 | } |
| 104 | 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 | + // We've got the assets we need, enqueue them | |
| 286 | + if( count( $css_to_enqueue ) > 0 ) $this->enqueue_css( $css_to_enqueue ); | |
| 287 | + if( count( $js_to_enqueue ) > 0 ) $this->enqueue_js( $js_to_enqueue ); | |
| 288 | + } | |
| 289 | + | |
| 290 | + private function enqueue_css( $assets ) | |
| 291 | + { | |
| 292 | + $i = 1; | |
| 293 | + foreach( $assets as $asset ) { | |
| 294 | + $version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time(); | |
| 295 | + | |
| 296 | + wp_enqueue_style( 'wpupg_style' . $i, $asset['url'], false, $version, 'all' ); | |
| 297 | + $i++; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + private function enqueue_js( $assets ) | |
| 302 | + { | |
| 303 | + $i = 1; | |
| 304 | + foreach( $assets as $asset ) { | |
| 305 | + $name = isset( $asset['name'] ) ? $asset['name'] : 'wpupg_script' . $i; | |
| 306 | + $deps = isset( $asset['deps'] ) ? $asset['deps'] : ''; | |
| 307 | + | |
| 308 | + $version = WPUltimatePostGrid::option( 'assets_use_cache', '1' ) == '1' ? WPUPG_VERSION : time(); | |
| 309 | + | |
| 310 | + wp_enqueue_script( $name, $asset['url'], $deps, $version, true ); | |
| 311 | + | |
| 312 | + if( isset( $asset['data'] ) && isset( $asset['data']['name'] ) ) { | |
| 313 | + $data_name = $asset['data']['name']; | |
| 314 | + unset( $asset['data']['name'] ); | |
| 315 | + | |
| 316 | + wp_localize_script( $name, $data_name, $asset['data'] ); | |
| 317 | + } | |
| 318 | + | |
| 319 | + $i++; | |
| 320 | + } | |
| 321 | + } | |
| 322 | + | |
| 323 | + /** | |
| 324 | + * Check if any of the shortcodes is used in post | |
| 325 | + */ | |
| 326 | + public function check_for_shortcode( $shortcodes ) { | |
| 327 | + if( !is_single() ) return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); // TODO Needs better solution | |
| 328 | + | |
| 329 | + global $post; | |
| 330 | + | |
| 331 | + if( function_exists( 'has_shortcode' ) ) { | |
| 332 | + | |
| 333 | + // Multiple shortcodes passed, if one shortcode is in the post, return true | |
| 334 | + if( is_array( $shortcodes ) ) { | |
| 335 | + $shortcode_used = false; | |
| 336 | + | |
| 337 | + foreach( $shortcodes as $shortcode ) { | |
| 338 | + if( isset( $post->post_content ) && has_shortcode( $post->post_content, $shortcode ) ) { | |
| 339 | + $shortcode_used = true; | |
| 340 | + } | |
| 341 | + } | |
| 342 | + | |
| 343 | + return apply_filters( 'wpupg_check_for_shortcode', $shortcode_used, $shortcodes ); | |
| 344 | + } | |
| 345 | + | |
| 346 | + // Only one shortcode passed, true if that one is in the post | |
| 347 | + if( isset( $post->post_content ) && has_shortcode( $post->post_content, $shortcodes ) ) { | |
| 348 | + return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); | |
| 349 | + } | |
| 350 | + | |
| 351 | + return apply_filters( 'wpupg_check_for_shortcode', false, $shortcodes ); | |
| 352 | + } | |
| 353 | + | |
| 354 | + return apply_filters( 'wpupg_check_for_shortcode', true, $shortcodes ); // In older versions of WP just enqueue everything | |
| 105 | 355 | } |
| 106 | 356 | } |