| @@ -1,10 +1,35 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +/* | |
| 4 | + Meow_MGL_Run is the single place where the assets of the plugin (everything under app/) are | |
| 5 | + registered and enqueued. Nothing else should call wp_enqueue_* / wp_register_* for them. | |
| 6 | + | |
| 7 | + What exists, and when it gets loaded: | |
| 8 | + | |
| 9 | + app/style.min.css 'mgl-css' front-end + admin, always | |
| 10 | + app/style-pro.min.css 'mgl-pro-css' same, but Pro only | |
| 11 | + app/galleries.js 'mgl-js' front-end: in the footer, once a gallery or | |
| 12 | + a collection has been built | |
| 13 | + admin: in the header, as a dependency of | |
| 14 | + the admin bundle | |
| 15 | + app/admin.js 'mgl-admin-js' admin only (settings, dashboard, block) | |
| 16 | + Lato (Google Fonts) 'meow-neko-ui-lato-font' admin only | |
| 17 | + | |
| 18 | + To add data to the gallery bundle from somewhere else (the Pro class does it for the map | |
| 19 | + settings), hook 'mgl_scripts_registered' and localize on the handle it passes. | |
| 20 | +*/ | |
| 21 | + | |
| 3 | 22 | class Meow_MGL_Run { |
| 4 | - private $isEnqueued = false; | |
| 23 | + | |
| 24 | + const SCRIPT_HANDLE = 'mgl-js'; | |
| 25 | + const ADMIN_SCRIPT_HANDLE = 'mgl-admin-js'; | |
| 26 | + const STYLE_HANDLE = 'mgl-css'; | |
| 27 | + const PRO_STYLE_HANDLE = 'mgl-pro-css'; | |
| 28 | + const FONT_STYLE_HANDLE = 'meow-neko-ui-lato-font'; | |
| 29 | + | |
| 5 | 30 | private $core; |
| 6 | - private $atts; | |
| 31 | + private $isRegistered = false; | |
| 7 | 32 | |
| 8 | 33 | public function __construct( $core ) { |
| 9 | 34 | $this->core = $core; |
| 10 | 35 | |
| @@ -12,15 +37,17 @@ | ||
| 12 | 37 | if ( !$override_disabled ) { add_shortcode( 'gallery', array( $core, 'gallery' ) ); } |
| 13 | 38 | add_shortcode( 'meow-gallery', array( $core, 'gallery' ) ); |
| 14 | 39 | |
| 15 | 40 | if ( is_admin() ) { |
| 16 | - add_action( 'init', array( $this, 'enqueue_scripts' ) ); | |
| 17 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); | |
| 41 | + // The gallery bundle is only registered: the admin bundle depends on it, so WordPress | |
| 42 | + // loads it for us, once, and in the right order. | |
| 43 | + add_action( 'init', array( $this, 'register_gallery_script' ) ); | |
| 44 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); | |
| 18 | 45 | } else { |
| 19 | 46 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ), 10 ); |
| 20 | - | |
| 21 | - add_action( 'mgl_gallery_created', array( $this, 'enqueue_scripts' ), 10, 0 ); | |
| 22 | - add_action( 'mgl_collection_created', array( $this, 'enqueue_scripts' ), 10, 0 ); | |
| 47 | + | |
| 48 | + add_action( 'mgl_gallery_created', array( $this, 'enqueue_gallery_script' ), 10, 0 ); | |
| 49 | + add_action( 'mgl_collection_created', array( $this, 'enqueue_gallery_script' ), 10, 0 ); | |
| 23 | 50 | } |
| 24 | 51 | |
| 25 | 52 | // Yoast: Some people really want this, but it needs to be reviewed as Yoast changed its API |
| 26 | 53 | //add_filter( 'wpseo_sitemap_urlimages', array( $this, 'wpseo_siteimap' ), 10, 2 ); |
| @@ -25,51 +52,100 @@ | ||
| 25 | 52 | // Yoast: Some people really want this, but it needs to be reviewed as Yoast changed its API |
| 26 | 53 | //add_filter( 'wpseo_sitemap_urlimages', array( $this, 'wpseo_siteimap' ), 10, 2 ); |
| 27 | 54 | } |
| 28 | 55 | |
| 56 | + /* | |
| 57 | + Assets | |
| 58 | + */ | |
| 59 | + | |
| 60 | + private function asset_url( $file ) { | |
| 61 | + return MGL_URL . 'app/' . $file; | |
| 62 | + } | |
| 63 | + | |
| 64 | + // The file modification time is used as a cache buster, so a rebuild is picked up right away. | |
| 65 | + private function asset_version( $file ) { | |
| 66 | + $physical_file = MGL_PATH . '/app/' . $file; | |
| 67 | + return file_exists( $physical_file ) ? filemtime( $physical_file ) : MGL_VERSION; | |
| 68 | + } | |
| 69 | + | |
| 70 | + // Styles, on the front-end as well as in the admin. | |
| 29 | 71 | function enqueue_styles() { |
| 30 | - // Cache buster for CSS | |
| 31 | - $css_file = MGL_PATH . '/app/style.min.css'; | |
| 32 | - $css_cache_buster = file_exists( $css_file ) ? filemtime( $css_file ) : MGL_VERSION; | |
| 33 | - wp_enqueue_style( 'mgl-css', plugins_url( '/app/style.min.css', __DIR__ ), null, $css_cache_buster ); | |
| 72 | + wp_enqueue_style( self::STYLE_HANDLE, $this->asset_url( 'style.min.css' ), null, | |
| 73 | + $this->asset_version( 'style.min.css' ) ); | |
| 74 | + | |
| 75 | + if ( class_exists( 'MeowPro_MGL_Core' ) ) { | |
| 76 | + wp_enqueue_style( self::PRO_STYLE_HANDLE, $this->asset_url( 'style-pro.min.css' ), null, | |
| 77 | + $this->asset_version( 'style-pro.min.css' ) ); | |
| 78 | + } | |
| 34 | 79 | } |
| 35 | 80 | |
| 36 | - function enqueue_scripts() { | |
| 37 | - // Only need to load the scripts once. | |
| 38 | - if ( $this->isEnqueued ) { | |
| 81 | + // Declares app/galleries.js and its settings. Safe to call more than once, and required before | |
| 82 | + // anything can enqueue or localize that script. | |
| 83 | + function register_gallery_script() { | |
| 84 | + if ( $this->isRegistered ) { | |
| 39 | 85 | return; |
| 40 | 86 | } |
| 41 | - $this->isEnqueued = true; | |
| 87 | + $this->isRegistered = true; | |
| 42 | 88 | |
| 43 | - // Load the JS for Meow Gallery | |
| 44 | - $physical_file = MGL_PATH . '/app/galleries.js'; | |
| 45 | - $cache_buster = file_exists( $physical_file ) ? filemtime( $physical_file ) : MGL_VERSION; | |
| 46 | - wp_enqueue_script( 'mgl-js', plugins_url( '/app/galleries.js', __DIR__ ), array(), $cache_buster, true ); | |
| 89 | + // In the admin, the block editor bundle runs in the header, so this one cannot be deferred. | |
| 90 | + $in_footer = !is_admin(); | |
| 47 | 91 | |
| 92 | + wp_register_script( self::SCRIPT_HANDLE, $this->asset_url( 'galleries.js' ), array(), | |
| 93 | + $this->asset_version( 'galleries.js' ), $in_footer ); | |
| 48 | 94 | |
| 49 | - // TODO: This should be moved in a getter (since it is also used by tiles.php) | |
| 50 | - $density = []; | |
| 51 | - if ( isset( $this->atts['density'] ) ) { | |
| 52 | - $density['desktop'] = $this->atts['density']; | |
| 53 | - $density['tablet'] = $this->atts['density']; | |
| 54 | - $density['mobile'] = $this->atts['density']; | |
| 55 | - } | |
| 56 | - else { | |
| 57 | - $density['desktop'] = $this->core->get_option( 'tiles_density', 'high' ); | |
| 58 | - $density['tablet'] = $this->core->get_option( 'tiles_density_tablet', 'medium' ); | |
| 59 | - $density['mobile'] = $this->core->get_option( 'tiles_density_mobile', 'low' ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - wp_localize_script('mgl-js', 'mgl_settings', | |
| 95 | + wp_localize_script( self::SCRIPT_HANDLE, 'mgl_settings', | |
| 63 | 96 | array( |
| 64 | 97 | 'infinite_buffer' => $this->core->get_option( 'infinite_buffer', 0 ), |
| 65 | 98 | 'disable_right_click' => !$this->core->get_option( 'right_click', false ), |
| 66 | - 'tiles' => array( 'density' => $density ), | |
| 99 | + 'tiles' => array( 'density' => Meow_MGL_Core::get_tiles_density() ), | |
| 67 | 100 | 'api_url' => get_rest_url( null, '/meow-gallery/v1/' ), |
| 68 | 101 | 'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
| 69 | 102 | 'options' => $this->core->get_all_options(), |
| 70 | 103 | ) |
| 71 | 104 | ); |
| 105 | + | |
| 106 | + do_action( 'mgl_scripts_registered', self::SCRIPT_HANDLE ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + // Front-end: called once a gallery or a collection has been built. | |
| 110 | + function enqueue_gallery_script() { | |
| 111 | + $this->register_gallery_script(); | |
| 112 | + wp_enqueue_script( self::SCRIPT_HANDLE ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + // Admin: the styles, the fonts and app/admin.js (settings, Meow dashboard, Gutenberg block). | |
| 116 | + // The gallery bundle comes along as a dependency, since the block editor renders galleries. | |
| 117 | + function enqueue_admin_assets() { | |
| 118 | + $this->enqueue_styles(); | |
| 119 | + | |
| 120 | + wp_enqueue_style( self::FONT_STYLE_HANDLE, | |
| 121 | + '//fonts.googleapis.com/css2?family=Lato:wght@100;300;400;700;900&display=swap' ); | |
| 122 | + | |
| 123 | + wp_register_script( self::ADMIN_SCRIPT_HANDLE, $this->asset_url( 'admin.js' ), | |
| 124 | + array( self::SCRIPT_HANDLE, 'wp-editor', 'wp-i18n', 'wp-element' ), | |
| 125 | + $this->asset_version( 'admin.js' ) ); | |
| 126 | + | |
| 127 | + global $wplr; | |
| 128 | + wp_localize_script( self::ADMIN_SCRIPT_HANDLE, 'mgl_meow_gallery', | |
| 129 | + array( | |
| 130 | + 'api_url' => get_rest_url( null, '/meow-gallery/v1/' ), | |
| 131 | + 'rest_url' => get_rest_url(), | |
| 132 | + 'plugin_url' => MGL_URL, | |
| 133 | + 'prefix' => MGL_PREFIX, | |
| 134 | + 'domain' => MGL_DOMAIN, | |
| 135 | + 'is_pro' => class_exists( 'MeowPro_MGL_Core' ), | |
| 136 | + // Same check as MeowKit_MGL_Admin::is_registered(), which lives in another class. | |
| 137 | + 'is_registered' => !!apply_filters( MGL_PREFIX . '_meowapps_is_registered', false, MGL_PREFIX ), | |
| 138 | + 'rest_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 139 | + 'wplr_collections' => $wplr ? $wplr->read_collections_recursively() : [], | |
| 140 | + 'options' => $this->core->get_all_options(), | |
| 141 | + // Names only: the selectors don't need the medias (see get_gallery_names). | |
| 142 | + 'galleries' => $this->core->get_gallery_names(), | |
| 143 | + 'collections' => $this->core->get_collection_names(), | |
| 144 | + ) | |
| 145 | + ); | |
| 146 | + | |
| 147 | + wp_enqueue_script( self::ADMIN_SCRIPT_HANDLE ); | |
| 72 | 148 | } |
| 73 | 149 | |
| 74 | 150 | /* |
| 75 | 151 | For Yoast SEO |