|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + Plugin Name: Ultimate Member - Maybe load assets |
| 5 | + Description: Remove CSS and JS on non UM pages |
| 6 | + Version: 1.0.2 |
| 7 | + Author: Ultimate Member |
| 8 | + Author URI: http://ultimatemember.com/ |
| 9 | + */ |
| 10 | + |
| 11 | +add_action( 'wp_print_footer_scripts', 'um_remove_scripts_and_styles', 9 ); |
| 12 | +add_action( 'wp_print_scripts', 'um_remove_scripts_and_styles', 9 ); |
| 13 | +add_action( 'wp_print_styles', 'um_remove_scripts_and_styles', 9 ); |
| 14 | +add_action( 'dynamic_sidebar', 'um_remove_scripts_and_styles_widget' ); |
| 15 | + |
| 16 | +/** |
| 17 | + * Maybe remove Ultimate Member CSS and JS |
| 18 | + * @global WP_Post $post |
| 19 | + * @global bool $um_load_assets |
| 20 | + * @global WP_Scripts $wp_scripts |
| 21 | + * @global WP_Styles $wp_styles |
| 22 | + * @return NULL |
| 23 | + */ |
| 24 | +function um_remove_scripts_and_styles() { |
| 25 | + global $post, $um_load_assets, $wp_scripts, $wp_styles; |
| 26 | + |
| 27 | + // Set here IDs of the pages, that use Ultimate Member scripts and styles |
| 28 | + $um_posts = array(0); |
| 29 | + |
| 30 | + // Set here URLs of the pages, that use Ultimate Member scripts and styles |
| 31 | + $um_urls = array( |
| 32 | + '/account/', |
| 33 | + '/activity/', |
| 34 | + '/groups/', |
| 35 | + '/login/', |
| 36 | + '/logout/', |
| 37 | + '/members/', |
| 38 | + '/my-groups/', |
| 39 | + '/password-reset/', |
| 40 | + '/register/', |
| 41 | + '/user/', |
| 42 | + ); |
| 43 | + |
| 44 | + if ( is_admin() || is_ultimatemember() ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $REQUEST_URI = $_SERVER['REQUEST_URI']; |
| 49 | + if ( in_array( $REQUEST_URI, $um_urls ) ) { |
| 50 | + return; |
| 51 | + } |
| 52 | + foreach ( $um_urls as $key => $um_url ) { |
| 53 | + if ( strpos( $REQUEST_URI, $um_url ) !== FALSE ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if ( !empty( $um_load_assets ) ) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if ( isset( $post ) && is_a( $post, 'WP_Post' ) ) { |
| 63 | + if ( in_array( $post->ID, $um_posts ) ) { |
| 64 | + return; |
| 65 | + } |
| 66 | + if ( strpos( $post->post_content, '[ultimatemember_' ) !== FALSE ) { |
| 67 | + return; |
| 68 | + } |
| 69 | + if ( strpos( $post->post_content, '[ultimatemember form_id' ) !== FALSE ) { |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if ( empty( $wp_scripts->queue ) || empty( $wp_styles->queue ) ) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + foreach ( $wp_scripts->queue as $key => $script ) { |
| 79 | + if ( strpos( $script, 'um_' ) === 0 || strpos( $script, 'um-' ) === 0 || strpos( $wp_scripts->registered[$script]->src, '/ultimate-member/assets/' ) !== FALSE ) { |
| 80 | + unset( $wp_scripts->queue[$key] ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + foreach ( $wp_styles->queue as $key => $style ) { |
| 85 | + if ( strpos( $style, 'um_' ) === 0 || strpos( $style, 'um-' ) === 0 || strpos( $wp_styles->registered[$style]->src, '/ultimate-member/assets/' ) !== FALSE ) { |
| 86 | + unset( $wp_styles->queue[$key] ); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Check whether Ultimate Member widget was used |
| 93 | + * @param array $widget |
| 94 | + */ |
| 95 | +function um_remove_scripts_and_styles_widget( $widget ) { |
| 96 | + if ( strpos( $widget['id'], 'um_' ) === 0 || strpos( $widget['id'], 'um-' ) === 0 ) { |
| 97 | + $GLOBALS['um_load_assets'] = TRUE; |
| 98 | + } |
| 99 | +} |
0 commit comments