| 1 |
<?php |
| 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 |
|
| 22 |
class Meow_MGL_Run { |
| 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 |
|
| 33 |
public function __construct( $core ) { |
| 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' ) ); } |
| 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 |
|
| 52 |
// Yoast: Some people really want this, but it needs to be reviewed as Yoast changed its API |
| 53 |
//add_filter( 'wpseo_sitemap_urlimages', array( $this, 'wpseo_siteimap' ), 10, 2 ); |
| 54 |
} |
| 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. |
| 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' ) ); |
| 78 |
} |
| 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; |
| 86 |
} |
| 87 |
$this->isRegistered = true; |
| 88 |
|
| 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', |
| 96 |
array( |
| 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(), |
| 103 |
) |
| 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 ); |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
For Yoast SEO |
| 152 |
*/ |
| 153 |
|
| 154 |
function wpseo_siteimap( $images, $post_id ) { |
| 155 |
$galleries = get_post_galleries( $post_id ); |
| 156 |
$images_ids = array(); |
| 157 |
foreach ( $galleries as $gallery ) { |
| 158 |
preg_match_all( '/wp\-image\-([0-9]{1,16})/', $gallery, $matches ); |
| 159 |
if ( !empty( $matches ) ) { |
| 160 |
foreach ( $matches[1] as $id ) |
| 161 |
array_push( $images_ids, $id ); |
| 162 |
} |
| 163 |
} |
| 164 |
$images_ids = array_unique( $images_ids ); |
| 165 |
foreach ( $images_ids as $id ) { |
| 166 |
array_push( $images, array( |
| 167 |
'src' => wp_get_attachment_url( $id ), |
| 168 |
'title' => get_the_title( $id ), |
| 169 |
'alt' => get_post_meta( $id, '_wp_attachment_image_alt', true ) |
| 170 |
) ); |
| 171 |
} |
| 172 |
return $images; |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
?> |
| 178 |
|