PluginProbe
Meow Gallery / 5.5.5
Meow Gallery v5.5.5
5.5.5 5.5.4 5.5.3 5.5.2 5.5.1 5.5.0 5.4.9 5.4.8 5.4.7 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 All 157 releases
← All changes | classes/run.php +130 -21 4.1.75.5.5 View file →
@@ -1,42 +1,151 @@
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 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 +
30 + private $core;
31 + private $isRegistered = false;
32 +
5 33 public function __construct( $core ) {
6 - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
7 - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
8 - add_shortcode( 'gallery', array( $core, 'gallery' ) );
34 + $this->core = $core;
35 +
36 + $override_disabled = get_option( 'mgl_options', [] )[ 'gallery_shortcode_override_disabled' ] ?? false;
37 + if ( !$override_disabled ) { add_shortcode( 'gallery', array( $core, 'gallery' ) ); }
9 38 add_shortcode( 'meow-gallery', array( $core, 'gallery' ) );
39 +
40 + if ( is_admin() ) {
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' ) );
45 + } else {
46 + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ), 10 );
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 );
50 + }
51 +
10 52 // Yoast: Some people really want this, but it needs to be reviewed as Yoast changed its API
11 53 //add_filter( 'wpseo_sitemap_urlimages', array( $this, 'wpseo_siteimap' ), 10, 2 );
12 54 }
13 55
14 - function enqueue_scripts() {
15 - $physical_file = MGL_PATH . '/app/galleries.js';
16 - $cache_buster = file_exists( $physical_file ) ? filemtime( $physical_file ) : MGL_VERSION;
17 - wp_enqueue_script( 'mgl-js', plugins_url( '/app/galleries.js', __DIR__ ), array( 'jquery' ), $cache_buster, false );
56 + /*
57 + Assets
58 + */
18 59
19 - // TODO: This should be moved in a getter (since it is also used by tiles.php)
20 - $density = [];
21 - if ( isset( $this->atts['density'] ) ) {
22 - $density['desktop'] = $this->atts['density'];
23 - $density['tablet'] = $this->atts['density'];
24 - $density['mobile'] = $this->atts['density'];
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.
71 + function enqueue_styles() {
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' ) );
25 78 }
26 - else {
27 - $density['desktop'] = get_option( 'mgl_tiles_density', 'high' );
28 - $density['tablet'] = get_option( 'mgl_tiles_density_tablet', 'medium' );
29 - $density['mobile'] = get_option( 'mgl_tiles_density_mobile', 'low' );
79 + }
80 +
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 ) {
85 + return;
30 86 }
87 + $this->isRegistered = true;
31 88
32 - wp_localize_script('mgl-js', 'mgl_settings',
89 + // In the admin, the block editor bundle runs in the header, so this one cannot be deferred.
90 + $in_footer = !is_admin();
91 +
92 + wp_register_script( self::SCRIPT_HANDLE, $this->asset_url( 'galleries.js' ), array(),
93 + $this->asset_version( 'galleries.js' ), $in_footer );
94 +
95 + wp_localize_script( self::SCRIPT_HANDLE, 'mgl_settings',
33 96 array(
34 - 'disable_right_click' => !get_option( 'mgl_right_click', false ),
35 - 'tiles' => array( 'density' => $density )
97 + 'infinite_buffer' => $this->core->get_option( 'infinite_buffer', 0 ),
98 + 'disable_right_click' => !$this->core->get_option( 'right_click', false ),
99 + 'tiles' => array( 'density' => Meow_MGL_Core::get_tiles_density() ),
100 + 'api_url' => get_rest_url( null, '/meow-gallery/v1/' ),
101 + 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
102 + 'options' => $this->core->get_all_options(),
36 103 )
37 104 );
38 - wp_enqueue_style( 'mgl-css', plugins_url( '/app/style.min.css', __DIR__ ), null, $cache_buster );
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 );
39 148 }
40 149
41 150 /*
42 151 For Yoast SEO