";
}
$image_ids = $shortcode['medias']['thumbnail_ids'];
unset( $shortcode['medias'] );
//* We merge $atts into $shortcode ( not $shortcode into $atts ) so we can override the gallery settings even when using the ID.
// Override specifics: "default" layout from atts should be the layout from shortcode
if( array_key_exists('layout', $atts) && $atts['layout'] == 'default' )
{
$atts['layout'] = $shortcode['layout'];
}
$atts = array_merge( $shortcode, $atts );
}
// Real Media Library folder path (e.g. rml="2025/France/Tours").
// Re-evaluated here (not only from the top) so it also works when the value
// comes from a saved gallery loaded via [gallery id="..."]. The logic lives
// in Meow_MGL_RML::get_image_ids() to keep this method clean.
$has_rml = isset( $atts['rml'] ) && !empty( $atts['rml'] );
if ( $has_rml ) {
$rml_ids = Meow_MGL_RML::get_image_ids( $atts['rml'] );
if ( is_wp_error( $rml_ids ) ) {
return "
Meow Gallery: The layout $layout is not available in this version.
";
}
// Captions
if ( isset( $atts['captions'] ) && ( $atts['captions'] === false || $atts['captions'] === 'false' ) ) {
// This is to avoid issues linked to the old block editor for the Meow Gallery
$atts['captions'] = 'none';
}
// apply filter 'mgl_sort_ahead' to sort the images before anything else
if ( !empty( $image_ids ) ){
$image_ids = implode( ',', apply_filters( 'mgl_sort_ahead', explode( ',', $image_ids ), $layout, $atts ) );
}
//DEBUG: Display $atts
//error_log( print_r( $atts, 1 ) );
// Start the process of building the gallery
$this->gallery_process = true;
$this->gallery_layout = $layout;
// This should be probably removed.
// wp_enqueue_style( 'mgl-css' );
$infinite = $this->get_option( 'infinite', false ) && class_exists( 'MeowPro_MGL_Core' );
// $gen = new $layoutClass( $atts, !$isPreview && $infinite, $isPreview );
// $result = $gen->build( $image_ids );
do_action( 'mgl_' . $layout . '_gallery_created', $layout );
//$result = apply_filters( 'post_gallery', $result, $atts, null );
$this->rewrittenMwlData = apply_filters('mgl_force_rewrite_mwl_data', explode( ',', $image_ids ) );
do_action( 'mgl_gallery_created', $atts, explode( ',', $image_ids ), $layout );
$gallery_options = $this->get_gallery_options( $image_ids, $atts, $infinite, $isPreview, $layout );
// If infinite scroll option was enabled, get the images up to 12 at first.
$loading_image_ids = explode(',', $image_ids);
$loading_image_ids = apply_filters( 'mgl_sort', $loading_image_ids, [], $layout, $atts );
// Only add the carousel to the infinite layouts if the option is enabled
if( $infinite && $this->get_option( 'carousel_infinite', false ) ) {
$this->infinite_layouts[] = 'carousel';
}
if (!$isPreview && $infinite && in_array( $layout, $this->infinite_layouts ) ) {
$loading_image_ids = array_slice( $loading_image_ids, 0, 12 );
}
$gallery_images = $this->get_gallery_images( $loading_image_ids, $atts, $layout, $gallery_options['size'], $posts_ids );
// Get the class and data attributes
$class = $this->get_mgl_root_class( $atts );
$data_atts = $this->get_data_as_json( $atts );
$data_gallery_options = $this->get_data_as_json( $gallery_options );
$data_gallery_images = $this->get_data_as_json( $gallery_images );
$html = sprintf(
'
',
esc_attr( $class ),
$data_gallery_options,
$data_gallery_images,
$data_atts
);
// Run at /wp-includes/formatting.php on line 6037
// WordPress returns early if the text is simple ASCII without emoji-like content,
// so we only need to check preg_split if WordPress would actually run it.
$needs_emoji_check = str_contains( $html, '' ) ||
!( ( function_exists( 'mb_check_encoding' ) && mb_check_encoding( $html, 'ASCII' ) ) || ! preg_match( '/[^\x00-\x7F]/', $html ) );
if ( $needs_emoji_check ) {
$textarr = preg_split( '/(<.*>)/U', $html , -1, PREG_SPLIT_DELIM_CAPTURE);
if ( $textarr === false ) {
$error = preg_last_error();
error_log( "[MEOW GALLERY] Regex: " . preg_last_error_msg() . " (Code $error)" );
return "
Meow Gallery: The gallery is too large for your Wordpress pcre.backtrack_limit, increase it in your server settings, or reduce your gallery size.";
}
}
//The Gallery Container is where the images in the right layout will be rendered.
$html .= '
';
// Add skeleton loading placeholder to prevent layout shift
$skeleton_loading = $this->get_option( 'skeleton_loading', true );
if ( $skeleton_loading ) {
$image_count = count( $gallery_images );
$html .= $this->skeleton_handler->get_skeleton_html( $layout, $gallery_options, $image_count );
}
// Use the DOM to generate the images (so that lightboxes can hook into them, and for better SEO)
// If there are no images, the JS will look for the img_html and build the gallery from there.
// TODO: We should check why it's not working with the carousel (for map, it's normal).
if ( $layout !== 'map' && $layout !== 'carousel' && $this->get_option( 'rendering_mode', 'dom' ) === 'dom' ) {
$html .= '
';
foreach ( $gallery_images as $image ) {
if ( !empty( $image['link_href'] ) ) {
// If there is a link, we will get the alt from the image id so we have a proper aria-label
$aria_label = __('Open image', MGL_DOMAIN);
$aria_value = esc_attr( get_post_meta( $image['id'], '_wp_attachment_image_alt', true ) );
$aria = !empty( $aria_value ) ? $aria_label . ': ' . $aria_value : $aria_label;
$custom_link_classes = apply_filters( 'mgl_custom_link_classes', '', $image );
$html .= '';
$html .= $image['img_html'];
$html .= '';
}
else {
$html .= $image['img_html'];
}
}
$html .= '
';
}
$html .= '
';
$this->gallery_process = false;
return $html;
}
public function get_gallery_options(string $image_ids, array $atts, bool $infinite, bool $is_preview, string $layout) {
$image_ids = explode(',', $image_ids);
$wp_upload_dir = wp_upload_dir();
$options = $this->get_all_options();
$id = uniqid();
$size = isset( $atts['size'] ) ? $atts['size'] : 'large';
$size = apply_filters( 'mgl_media_size', $size );
$custom_class = isset( $atts['custom-class'] ) ? $atts['custom-class'] : null;
$link = isset( $atts['link'] ) ? $atts['link'] : ( $options['link'] ?? null );
$updir = trailingslashit( $wp_upload_dir['baseurl'] );
$captions = isset( $atts['captions'] ) ? $atts['captions'] : ( $options['captions'] ?? 'none' );
$animation = null;
if ( isset( $atts['animation'] ) && $atts['animation'] != 'default' ) {
$animation = $atts['animation'];
} else {
$animation = $options['animation'] ?? null;
}
$class_id = 'mgl-gallery-' . $id;
$layouts = [];
// Justified
$justified_row_height = $options['justified_row_height'];
$justified_gutter = $options['justified_gutter'];
if ( $layout === 'justified' ) {
$justified_row_height = $atts['row-height'] ?? $options['justified_row_height'];
$justified_gutter = $atts['gutter'] ?? $options['justified_gutter'];
}
// Masonry
$masonry_gutter = $options['masonry_gutter'];
$masonry_columns = $options['masonry_columns'];
if ( $layout === 'masonry' ) {
$masonry_gutter = $atts['gutter'] ?? $options['masonry_gutter'];
$masonry_columns = $atts['columns'] ?? $options['masonry_columns'];
}
// Square
$square_gutter = $options['square_gutter'];
$square_columns = $options['square_columns'];
if ( $layout === 'square' ) {
$square_gutter = $atts['gutter'] ?? $options['square_gutter'];
$square_columns = $atts['columns'] ?? $options['square_columns'];
}
// Cascade
$cascade_gutter = $options['cascade_gutter'];
if ( $layout === 'cascade' ) {
$layouts = [ 'o', 'i', 'ii' ];
$cascade_gutter = $atts['gutter'] ?? $options['cascade_gutter'];
}
// Tiles
$tiles_gutter = $options['tiles_gutter'];
$tiles_gutter_tablet = $options['tiles_gutter_tablet'];
$tiles_gutter_mobile = $options['tiles_gutter_mobile'];
$tiles_density = $options['tiles_density'];
$tiles_density_tablet = $options['tiles_density_tablet'];
$tiles_density_mobile = $options['tiles_density_mobile'];
if ( $layout === 'tiles' ) {
$tiles_gutter = $atts['gutter'] ?? $options['tiles_gutter'];
$tiles_gutter_tablet = $atts['gutter'] ?? $options['tiles_gutter_tablet'];
$tiles_gutter_mobile = $atts['gutter'] ?? $options['tiles_gutter_mobile'];
$tiles_density = $atts['density'] ?? $options['tiles_density'];
$tiles_density_tablet = $atts['density'] ?? $options['tiles_density_tablet'];
$tiles_density_mobile = $atts['density'] ?? $options['tiles_density_mobile'];
}
// Horizontal
$horizontal_gutter = $options['horizontal_gutter'];
$horizontal_image_height = $options['horizontal_image_height'];
$horizontal_hide_scrollbar = $options['horizontal_hide_scrollbar'];
if ( $layout === 'horizontal' ) {
$horizontal_gutter = $atts['gutter'] ?? $options['horizontal_gutter'];
$horizontal_image_height = $atts['image_height'] ?? $options['horizontal_image_height'];
$horizontal_hide_scrollbar = $atts['hide_scrollbar'] ?? $options['horizontal_hide_scrollbar'];
}
// Carousel
$carousel_gutter = $options['carousel_gutter'];
$carousel_arrow_nav_enabled = $options['carousel_arrow_nav_enabled'];
$carousel_dot_nav_enabled = $options['carousel_dot_nav_enabled'];
$carousel_image_height = $options['carousel_image_height'];
$carousel_keep_aspect_ratio = $options['carousel_aspect_ratio'] ?? false;
if ( $layout === 'carousel' ) {
$carousel_gutter = $atts['gutter'] ?? $options['carousel_gutter'];
$carousel_arrow_nav_enabled = $atts['arrow_nav_enabled'] ?? $options['carousel_arrow_nav_enabled'];
$carousel_dot_nav_enabled = $atts['dot_nav_enabled'] ?? $options['carousel_dot_nav_enabled'];
$carousel_image_height = $atts['image_height'] ?? $options['carousel_image_height'];
$carousel_keep_aspect_ratio = array_key_exists( 'keep-aspect-ratio', $atts ) ? $atts['keep-aspect-ratio'] : 1;
}
// Map
$map_gutter = $options['map_gutter'];
$map_height = $options['map_height'];
if ( $layout === 'map' ) {
$map_gutter = $atts['gutter'] ?? $options['map_gutter'];
$map_height = $atts['map_height'] ?? $options['map_height'];
}
return compact(
'image_ids',
'id',
'size',
'infinite',
'custom_class',
'link',
'is_preview',
'updir',
'captions',
'animation',
'layout',
'justified_row_height',
'justified_gutter',
'masonry_gutter',
'masonry_columns',
'square_gutter',
'square_columns',
'cascade_gutter',
'class_id',
'layouts',
'tiles_gutter',
'tiles_gutter_tablet',
'tiles_gutter_mobile',
'tiles_density',
'tiles_density_tablet',
'tiles_density_mobile',
'horizontal_gutter',
'horizontal_image_height',
'horizontal_hide_scrollbar',
'carousel_gutter',
'carousel_arrow_nav_enabled',
'carousel_dot_nav_enabled',
'carousel_image_height',
'carousel_keep_aspect_ratio',
'map_gutter',
'map_height',
);
}
// #region Options
static function get_plugin_option_name() {
return self::$plugin_option_name;
}
// Tiles density, from the shortcode attributes when they set one, from the options otherwise.
// Shared by the front-end settings (Meow_MGL_Run) and the tiles CSS (Meow_MGL_Builders_Tiles).
static function get_tiles_density( $atts = [] ) {
if ( isset( $atts['density'] ) ) {
return array(
'desktop' => $atts['density'],
'tablet' => $atts['density'],
'mobile' => $atts['density'],
);
}
$options = get_option( self::$plugin_option_name, [] );
return array(
'desktop' => $options['tiles_density'] ?? 'high',
'tablet' => $options['tiles_density_tablet'] ?? 'medium',
'mobile' => $options['tiles_density_mobile'] ?? 'low',
);
}
static function get_plugin_option( $option_name, $default = null ) {
$options = get_option( self::$plugin_option_name, null );
if ( !empty( $options ) && array_key_exists( $option_name, $options ) ) {
return $options[$option_name];
}
return $default;
}
function get_option( $option_name, $default = null ) {
$options = $this->get_all_options();
if ( array_key_exists( $option_name, $options ) ) {
return $options[$option_name];
}
return $default;
}
function reset_options() {
delete_option( 'mgl_db_version');
delete_option( $this->option_name );
}
function list_options() {
return array(
'layout' => 'tiles',
'ç' => 'none',
'link' => null,
'caption_source' => 'caption',
'captions_alignment' => 'center',
'captions_background' => 'fade-black',
'animation' => false,
'image_size' => 'srcset',
'truncate_on_listing' => true,
'truncate_count' => 4,
'use_attachments_on_empty' => false,
'debug_logs' => false,
'rendering_mode' => 'dom', // Can be 'dom' or 'js'
'tiles_gutter' => 10,
'tiles_gutter_tablet' => 10,
'tiles_gutter_mobile' => 10,
'tiles_density' => 'high',
'tiles_density_tablet' => 'medium',
'tiles_density_mobile' => 'low',
'justified_density' => 'low',
'justified_density_tablet' => 'medium',
'justified_density_mobile' => 'low',
'masonry_gutter' => 5,
'masonry_columns' => 3,
'masonry_left_to_right' => false,
'justified_gutter' => 5,
'justified_row_height' => 200,
'square_gutter' => 5,
'square_columns' => 5,
'cascade_gutter' => 10,
'horizontal_gutter' => 10,
'horizontal_image_height' => 500,
'horizontal_hide_scrollbar' => false,
'carousel_gutter' => 5,
'carousel_image_height' => 500,
'carousel_arrow_nav_enabled' => true,
'carousel_dot_nav_enabled' => true,
'carousel_infinite' => false,
'map_engine' => 'googlemaps',
'map_height' => 500,
'map_zoom' => 10,
'map_gutter' => 10,
'googlemaps_token' => '',
'googlemaps_style' => '[]',
'mapbox_token' => '',
'mapbox_style' => '{"username":"", "style_id":""}',
'maptiler_token' => '',
// Stylish effect options
'stylish_enabled' => false,
'stylish_border_radius' => 6,
'stylish_border_width' => 0,
'stylish_border_color' => '#ffffff',
'stylish_shadow_opacity' => 0.08,
'stylish_shadow_opacity_hover' => 0.12,
'stylish_hover_lift' => 2,
'stylish_transition_speed' => 250,
//PRO OPTIONS
'infinite' => false,
'infinite_buffer' => 0,
'right_click' => false,
'gallery_shortcode_override_disabled' => false,
'skeleton_loading' => false,
);
}
function list_pro_options() {
return array(
'infinite' => false,
'infinite_buffer' => 0,
'right_click' => false,
'gallery_shortcode_override_disabled' => false,
'skeleton_loading' => false,
);
}
function get_all_options() {
$options = get_option( $this->option_name, null );
$options = $this->check_options( $options );
return $options;
}
// Upgrade from the old way of storing options to the new way.
function check_options( $options = [] ) {
$plugin_options = $this->list_options();
$pro_options = $this->list_pro_options();
$options = empty( $options ) ? [] : $options;
$hasChanges = false;
foreach ( $plugin_options as $option => $default ) {
// The option already exists
if ( isset( $options[$option] ) ) {
continue;
}
// The option does not exist, so we need to add it.
// Let's use the old value if any, or the default value.
$options[$option] = get_option( 'mgl_' . $option, $default );
delete_option( 'mgl_' . $option );
$hasChanges = true;
}
if( !$this->pro_module ) {
foreach ( $pro_options as $pro_option => $default ) {
if ( $options[$pro_option] !== $default ) {
$options[$pro_option] = $default;
$hasChanges = true;
}
}
}
if ( $hasChanges ) {
update_option( $this->option_name , $options );
}
return $options;
}
function update_options( $options ) {
if ( !update_option( $this->option_name, $options, false ) ) {
return false;
}
$options = $this->sanitize_options();
return $options;
}
// Validate and keep the options clean and logical.
function sanitize_options() {
$options = $this->get_all_options();
// something to do
return $options;
}
# endregion
function get_caption_from_source( $image ) {
$caption_source = $this->get_option( 'caption_source', 'caption' );
$caption = '';
switch ( $caption_source ) {
case 'title':
$caption = $image->title;
break;
case 'caption':
$caption = $image->caption;
break;
case 'description':
$caption = $image->description;
break;
case 'alt':
$caption = $image->alt;
break;
default:
$caption = $image->caption;
break;
}
return $caption;
}
function get_gallery_images( array $image_ids, array $atts, string $layout, string $size, array $posts_ids = []) {
global $wpdb;
// Enable the image-attribute rewriting (and the 'mgl_sizes' filter) for the duration of this method.
// This matters when get_gallery_images() is called directly (e.g. infinite scroll via REST) without
// going through gallery(), which is what normally sets these on the initial page load.
$previous_gallery_process = $this->gallery_process;
$previous_gallery_layout = $this->gallery_layout;
$this->gallery_process = true;
$this->gallery_layout = $layout;
// Escape the array of IDs for SQL
$ids = array_map( 'intval', $image_ids );
$ids_str = implode( ',', $ids );
$query = "SELECT
p.ID id,
p.post_title title,
p.post_content description,
p.post_excerpt caption,
pm.meta_value alt,
pm2.meta_value meta
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_wp_attachment_image_alt'
LEFT JOIN $wpdb->postmeta pm2 ON pm2.post_id = p.ID AND pm2.meta_key = '_wp_attachment_metadata'
WHERE p.post_type = 'attachment'
AND p.ID IN (" . $ids_str . ")
";
$res = $wpdb->get_results( $query );
$ids = explode( ',', $ids_str );
$images = [];
foreach ( $res as $r ) {
$images[$r->id] = [
'caption' => $this->get_caption_from_source( $r ),
'meta' => unserialize( $r->meta ),
];
}
$cleanIds = [];
foreach ( $ids as $id ) {
if ( isset( $images[$id] ) )
array_push( $cleanIds, $id );
}
$ids = apply_filters( 'mgl_sort', $cleanIds, $images, $layout, $atts );
if ($layout === 'map') {
$map_result = $this->get_map_images( $ids, $images, $atts );
$this->gallery_process = $previous_gallery_process;
$this->gallery_layout = $previous_gallery_layout;
return $map_result;
}
$result = [];
foreach ($ids as $index => $id) {
$image = $images[$id];
// Determine orientation if layout is 'tiles'
$orientation = [];
if ($layout === 'tiles') {
$orientation = [
'orientation' => ($image['meta']['width'] > $image['meta']['height'] ? 'o' : 'i')
];
}
$default_link = $this->get_option( 'link', null );
$link_attr = $this->get_link_attributes( $id, $atts['link'] ?? $default_link, $image );
$no_lightbox = $link_attr['type'] === 'link';
$mergedArray = [
'id' => $id,
'caption' => wp_kses(
html_entity_decode(apply_filters( 'mgl_caption', $image['caption'], $id ), ENT_QUOTES),
[
'strong' => [], // Bold
'b' => [], // Bold alternative
'em' => [], // Italic
'i' => [] // Italic alternative
]
),
'img_html' => apply_filters( 'mgl_gallery_written',
$this->get_img_html( $id, $size, $layout, $atts, $image, $no_lightbox ),
$layout
),
'link_href' => $link_attr['href'] ?? null,
'link_target' => $link_attr['target'] ?? null,
'link_rel' => $link_attr['rel'] ?? null,
'attributes' => $this->get_attributes( $id, $image, $layout ),
];
if( !empty( $posts_ids ) && isset( $atts['hero'] ) && $atts['hero'] ) {
$post_id = $posts_ids[$index];
$post = get_post( $post_id );
$mergedArray['featured_post_id'] = $post_id;
$mergedArray['featured_post_title'] = $post->post_title;
$mergedArray['featured_post_excerpt'] = $post->post_excerpt;
$mergedArray['featured_post_url'] = get_permalink( $post_id );
}
$result[] = array_merge( $image, $mergedArray, $orientation );
}
$this->gallery_process = $previous_gallery_process;
$this->gallery_layout = $previous_gallery_layout;
return $result;
}
private function get_image_class( $id, $layout, $noLightbox ) {
$base_class = 'wp-image-' . $id;
if( $layout === 'carousel' ) {
$base_class .= ' skip-lazy';
}
if ( $noLightbox ) {
$base_class .= ' no-lightbox';
}
return $base_class;
}
private function get_img_html( $id, $size, $layout, $atts, $data, $noLightbox ) {
//check if the media is a video
$media_type = get_post_mime_type( $id );
if ( strpos( $media_type, 'video' ) !== false ) {
$video = wp_get_attachment_url( $id );
$video = apply_filters( 'mgl_video', $video, $id, $data );
if ( !empty( $video ) ) {
return '';
}
}
$image_size = $this->get_option( 'image_size', 'srcset' );
$img_html = null;
if ( empty( $image_size ) || $image_size === 'srcset' ) {
$img_html = wp_get_attachment_image( $id, $size, false, [
'class' => $this->get_image_class( $id, $layout, $noLightbox ),
'draggable' => $layout === 'carousel' ? 'false' : null,
]);
}
else {
$info = wp_get_attachment_image_src( $id, $image_size );
$alt_text = get_post_meta( $id, '_wp_attachment_image_alt', true );
$img_html = '';
}
if ( $layout === 'masonry' ) {
$masonry_column = $this->get_option( 'masonry_column', 3 );
$columns = ( isset( $atts['columns'] ) ? $atts['columns'] : $masonry_column ) + 1;
$img_html = str_replace( '100vw', 100 / $columns . 'vw', $img_html );
}
else if ( $layout === 'square' ) {
$square_column = $this->get_option( 'square_columns', 5 );
$columns = ( isset( $atts['columns'] ) ? $atts['columns'] : $square_column ) + 1;
$img_html = str_replace( '100vw', 100 / $columns . 'vw', $img_html );
}
else if ( $layout === 'cascade' ) {
$img_html = str_replace( '100vw', 100 / 3 . 'vw', $img_html );
}
return wp_kses( $img_html, [
'img' => [
'src' => true,
'srcset' => true,
'loading' => true,
'tabindex' => true,
'sizes' => true,
'class' => true,
'id' => true,
'width' => true,
'height' => true,
'alt' => true,
'align' => true,
'draggable' => true,
]
]
);
}
private function get_link_attributes( $id, $link, $data ) {
$link_url = null;
$type = 'media';
$rel = null;
$target = '_self';
if ( $link === 'attachment' ) {
$link_url = get_permalink( (int)$id );
}
else if ( $link === 'media' || $link === 'file' ) {
$wpUploadDir = wp_upload_dir();
$updir = trailingslashit( $wpUploadDir['baseurl'] );
if ( isset( $data['meta']['file'] ) ) {
$link_url = $updir . $data['meta']['file'];
} else {
$link_url = get_permalink( (int)$id );
}
}
else if ( $link === null ){
$link_url = get_post_meta( $id, '_gallery_link_url', true );
if ( !empty( $link_url ) ) {
$type = 'link';
$target = get_post_meta( $id, '_gallery_link_target', true );
}
}
$link_attr = [
'href' => !empty( $link_url ) ? esc_url( $link_url ) : null,
'target' => $target,
'type' => $type,
'rel' => $rel,
];
return apply_filters( 'mgl_link_attributes', $link_attr, (int)$id, $data );
}
private function get_attributes( $id, $data, $layout ) {
$attributes = '';
if ( $layout === 'raw' ) {
if ( isset( $data['meta'] ) && isset( $data['meta']['width'] ) && isset( $data['meta']['height'] ) ) {
$attributes = 'data-mgl-id=' . $id . ' data-mgl-width=' . $data['meta']['width'] . ' data-mgl-height=' . $data['meta']['height'];
}
}
elseif ( $layout === 'tiles' ) {
if ( isset( $data['meta'] ) && isset( $data['meta']['width'] ) && isset( $data['meta']['height'] ) ) {
$attributes = 'data-mgl-id=' . $id . ' data-mgl-width=' . $data['meta']['width'] . ' data-mgl-height=' . $data['meta']['height'];
}
}
$attributes = apply_filters( 'mgl_attributes', $attributes, $id, $data );
if ( $attributes === '' ) {
return [];
}
$attribute_list = explode( ' ', $attributes );
$attributes = [];
foreach ( $attribute_list as $attribute ) {
list( $key, $value ) = explode( '=', $attribute );
$attributes[$key] = $value;
}
return $attributes;
}
private function get_map_images( $ids, $images, $atts = [] ) {
$map_images = array_map( function ( $id ) use ( $images, $atts ) {
$image = $images[$id];
$default_link = $this->get_option( 'link', null );
$link_attr = $this->get_link_attributes( $id, $atts['link'] ?? $default_link, $image );
$geo_coordinates = MeowPro_MGL_Exif::get_gps_data( $id, $image['meta'] );
if ( empty( $geo_coordinates ) ) {
return null;
}
$callback = function ( &$value, $key ) use ( $id ) {
$imgsrc = wp_get_attachment_image_src( $id, $key );
$value = $imgsrc[0];
};
array_walk( $image['meta']['sizes'], $callback );
return array_merge(
$image,
[
'id' => $id,
'file' => $image['meta']['file'],
'file_full' => wp_get_attachment_url( $id ),
'file_srcset' => wp_get_attachment_image_srcset( $id, 'full' ),
'file_sizes' => wp_get_attachment_image_sizes( $id, 'full' ),
'dimension' => [
'width' => $image['meta']['width'],
'height' => $image['meta']['height'],
],
'sizes' => $image['meta']['sizes'],
'data' => [
'caption' => $image['meta']['image_meta']['caption'],
'gps' => $geo_coordinates,
],
'link' => $link_attr,
]
);
}, $ids );
return array_values( array_filter( $map_images ) );
}
public function get_mgl_root_class( $atts, $classes = ['mgl-root'] ) {
$classes[] = isset( $atts['align'] ) ? 'align' . $atts['align'] : '';
return trim( implode( ' ', $classes ) );
}
public function get_data_as_json( $data ) {
return esc_attr( htmlspecialchars( wp_json_encode( $data ), ENT_QUOTES, 'UTF-8' ) );
}
public function generate_uniqid($length = 13) {
// Use WordPress function
if ( function_exists( 'wp_unique_id' ) ) {
$prefix = uniqid();
return wp_unique_id( $prefix );
}
// Fall back
else {
if ( function_exists( "random_bytes" ) ) {
$bytes = random_bytes( ceil( $length / 2 ) );
}
elseif ( function_exists( "openssl_random_pseudo_bytes" ) ) {
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
}
else {
throw new Exception( "No cryptographically secure random function available." );
}
return substr( bin2hex( $bytes ), 0, $length );
}
}
/**
* Reads the "medias" of a gallery. Only the ordered attachment IDs are stored (older versions
* also stored their URLs and mime types, which are ignored: they were a copy of the Media
* Library that went stale). This is also the shape that gets written back, and all the
* front-end needs. The Admin uses hydrate_medias() to get the URLs to display.
*/
public static function normalize_medias( $medias ) {
$ids = ( is_array( $medias ) && isset( $medias['thumbnail_ids'] ) && is_array( $medias['thumbnail_ids'] ) )
? array_values( $medias['thumbnail_ids'] ) : [];
return [ 'thumbnail_ids' => $ids ];
}
/**
* Adds the 'thumbnails' the Admin displays: one entry per ID, with its URLs and mime type
* resolved from the Media Library. Never stale, and a deleted attachment simply gets empty
* URLs (the Admin then shows a placeholder). Not used on the front-end, which only needs
* the IDs.
*/
public static function hydrate_medias( $medias ) {
$ids = self::normalize_medias( $medias )['thumbnail_ids'];
// One query for all the attachments instead of one per thumbnail. _prime_post_caches()
// only caches the attachments which exist, so anything still absent from the cache
// afterwards is gone: it's skipped instead of being queried on every request.
if ( !empty( $ids ) ) {
_prime_post_caches( array_values( array_unique( array_map( 'intval', $ids ) ) ), false, true );
}
$thumbnails = [];
foreach ( $ids as $id ) {
$thumbnail = [ 'id' => $id, 'url' => '', 'zoom_url' => '', 'mime' => '' ];
if ( !empty( $id ) && wp_cache_get( (int)$id, 'posts' ) ) {
$mime = get_post_mime_type( $id ) ?: '';
// Same rule as the latest_photos endpoint: videos have no image sizes, so their
// own URL is used for both the thumbnail and the zoom.
$is_video = strpos( $mime, 'video' ) !== false;
$url = $is_video ? wp_get_attachment_url( $id ) : wp_get_attachment_image_url( $id, 'thumbnail' );
$zoom = $is_video ? $url : wp_get_attachment_image_url( $id, 'large' );
$thumbnail['url'] = $url ?: '';
$thumbnail['zoom_url'] = $zoom ?: $thumbnail['url'];
$thumbnail['mime'] = $mime;
}
$thumbnails[] = $thumbnail;
}
return [ 'thumbnail_ids' => $ids, 'thumbnails' => $thumbnails ];
}
public function get_gallery_by_id( $id ) {
global $wpdb;
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
$gallery = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $shortcodes_table WHERE id = %s", $id ), ARRAY_A );
if ( !$gallery ) {
throw new Exception( __( 'Gallery not found.', MGL_DOMAIN ));
}
$gallery['medias'] = self::normalize_medias( maybe_unserialize( $gallery['medias'] ) );
$gallery['posts'] = $gallery['posts'] ? maybe_unserialize( $gallery['posts'] ) : null;
$gallery['tags'] = $gallery['tags'] ? unserialize( $gallery['tags'] ) : null;
return $gallery;
}
public function get_galleries_by_ids( $ids ){
global $wpdb;
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
$galleries = [];
$ids_str = "'" . implode( "','", array_map( 'esc_sql', $ids )) . "'";
$query = "SELECT * FROM $shortcodes_table WHERE id IN ( $ids_str )";
$results = $wpdb->get_results( $query, ARRAY_A );
foreach ( $results as $gallery ) {
$galleries[$gallery['id']] = [
'name' => $gallery['name'],
'description' => $gallery['description'],
'layout' => $gallery['layout'],
'medias' => self::normalize_medias( maybe_unserialize( $gallery['medias'] ) ),
'lead_image_id' => $gallery['lead_image_id'],
'order_by' => $gallery['order_by'],
'is_post_mode' => ( bool )$gallery['is_post_mode'],
'dynamic_source' => $gallery['dynamic_source'],
'hero' => ( bool )$gallery['is_hero_mode'],
'posts' => $gallery['posts'] ? maybe_unserialize( $gallery['posts'] ) : null,
'latest_posts' => $gallery['latest_posts'],
'tags' => $gallery['tags'] ? unserialize( $gallery['tags'] ) : null,
'dynamic_source' => $gallery['dynamic_source'],
'rml' => $gallery['rml'] ?? null,
'updated' => strtotime( $gallery['updated_at'] )
];
}
return $galleries;
}
public function get_galleries( $offset = 0, $limit = 10, $order = 'DESC', $sort = null, $page = 1, $search = '' ) {
global $wpdb;
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
Meow_MGL_Migrations::check_db();
// Get total count
$total = 0;
if ( empty( $search ) ) {
$total = $wpdb->get_var( "SELECT COUNT( * ) FROM $shortcodes_table" );
} else {
$total = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $shortcodes_table WHERE name LIKE %s", '%' . $wpdb->esc_like( $search ) . '%' ) );
}
// Calculate offset based on page if provided
if ($page > 1 && $offset === 0) {
$offset = ($page - 1) * $limit;
}
// Get shortcodes with pagination and sorting
$sort_accessor_to_column = [
'info' => 'name',
'updated' => 'updated_at',
'rank' => 'pref_rank',
];
$sort = $sort_accessor_to_column[$sort] ?? 'pref_rank';
$order = in_array( strtoupper( $order ), ['ASC', 'DESC'] ) ? strtoupper( $order ) : 'DESC';
// Sort by rank DESC first, then by updated_at for equal ranks
$query = $wpdb->prepare(
"SELECT * FROM $shortcodes_table WHERE name LIKE %s ORDER BY $sort $order, updated_at DESC LIMIT %d, %d",
'%' . $wpdb->esc_like( $search ) . '%', $offset, $limit
);
$results = $wpdb->get_results( $query, ARRAY_A );
$shortcodes = [];
foreach ( $results as $gallery ) {
// Transform database format to match expected format
$shortcodes[$gallery['id']] = [
'name' => $gallery['name'],
'description' => $gallery['description'],
'layout' => $gallery['layout'],
'medias' => self::hydrate_medias( maybe_unserialize( $gallery['medias'] ) ),
'lead_image_id' => $gallery['lead_image_id'],
'order_by' => $gallery['order_by'],
'is_post_mode' => ( bool )$gallery['is_post_mode'],
'hero' => ( bool )$gallery['is_hero_mode'],
'posts' => $gallery['posts'] ? maybe_unserialize( $gallery['posts'] ) : null,
'latest_posts' => $gallery['latest_posts'],
'tags' => $gallery['tags'] ? unserialize( $gallery['tags'] ) : null,
'dynamic_source' => $gallery['dynamic_source'],
'rml' => $gallery['rml'] ?? null,
'rank' => intval( $gallery['pref_rank'] ?? 0 ),
'updated' => strtotime( $gallery['updated_at'] )
];
}
return [
'total' => $total,
'galleries' => $shortcodes
];
}
/**
* Just the id => name pairs, for the selectors (the Gutenberg block). They only need the
* names, so this avoids shipping every gallery's medias in the page and, unlike
* get_galleries(), it isn't paginated: the selectors used to be capped at 10 entries.
*/
public function get_gallery_names() {
global $wpdb;
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
Meow_MGL_Migrations::check_db();
$names = [];
$results = $wpdb->get_results( "SELECT id, name FROM $shortcodes_table ORDER BY name ASC", ARRAY_A );
foreach ( $results as $gallery ) {
$names[$gallery['id']] = [ 'name' => $gallery['name'] ];
}
return [ 'galleries' => $names ];
}
public function get_collection_names() {
global $wpdb;
$collections_table = $wpdb->prefix . 'mgl_collections';
Meow_MGL_Migrations::check_db();
$names = [];
$results = $wpdb->get_results( "SELECT id, name FROM $collections_table ORDER BY name ASC", ARRAY_A );
foreach ( $results as $collection ) {
$names[$collection['id']] = [ 'name' => $collection['name'] ];
}
return [ 'collections' => $names ];
}
public function get_collection_by_id( $id ) {
global $wpdb;
$collections_table = $wpdb->prefix . 'mgl_collections';
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
// Get the collection
$collection = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $collections_table WHERE id = %d", $id ), ARRAY_A );
if ( !$collection ) {
throw new Exception( __( 'Collection not found.', MGL_DOMAIN ));
}
$collection['galleries_ids'] = unserialize( $collection['galleries_ids'] );
// Get the associated galleries
$galleries = [];
if ( !empty( $collection['galleries_ids'] )) {
$galleries_ids_str = "'" . implode( "','", array_map( 'esc_sql', $collection['galleries_ids'] )) . "'";
$galleries_query = "SELECT * FROM $shortcodes_table WHERE id IN ( $galleries_ids_str )";
$galleries_data = $wpdb->get_results( $galleries_query, ARRAY_A );
foreach ( $galleries_data as $gallery ) {
// Transform database format to match expected format
$galleries[] = [
'id' => $gallery['id'],
'name' => $gallery['name'],
'description' => $gallery['description'],
'layout' => $gallery['layout'],
'medias' => self::normalize_medias( maybe_unserialize( $gallery['medias'] ) ),
'lead_image_id' => $gallery['lead_image_id'],
'order_by' => $gallery['order_by'],
'is_post_mode' => ( bool )$gallery['is_post_mode'],
'hero' => ( bool )$gallery['is_hero_mode'],
'posts' => $gallery['posts'] ? unserialize( $gallery['posts'] ) : null,
'latest_posts' => $gallery['latest_posts'],
'updated' => strtotime( $gallery['updated_at'] )
];
}
// Format collection data
$collection['galleries'] = $galleries;
}
return $collection;
}
public function get_collections( $offset = 0, $limit = 10, $order = 'DESC', $page = 1, $search = '' ) {
global $wpdb;
$collections_table = $wpdb->prefix . 'mgl_collections';
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
Meow_MGL_Migrations::check_db();
// Get total count
if( empty( $search ) ) {
$total = $wpdb->get_var( "SELECT COUNT( * ) FROM $collections_table" );
} else {
$total = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $collections_table WHERE name LIKE %s", '%' . $wpdb->esc_like( $search ) . '%' ) );
}
// Calculate offset based on page if provided
if ($page > 1 && $offset === 0) {
$offset = ($page - 1) * $limit;
}
// Get collections with pagination and sorting
$query = $wpdb->prepare(
"SELECT * FROM $collections_table WHERE name LIKE %s ORDER BY updated_at $order LIMIT %d, %d",
'%' . $wpdb->esc_like( $search ) . '%', $offset, $limit
);
$collections = $wpdb->get_results( $query, ARRAY_A );
$result = [];
foreach ( $collections as $collection ) {
$collection_id = $collection['id'];
$galleries_ids = unserialize( $collection['galleries_ids'] );
// Get the associated galleries
$galleries = [];
if ( !empty( $galleries_ids )) {
$galleries_ids_str = "'" . implode( "','", array_map( 'esc_sql', $galleries_ids )) . "'";
$galleries_query = "SELECT * FROM $shortcodes_table WHERE id IN ( $galleries_ids_str )";
$galleries_data = $wpdb->get_results( $galleries_query, ARRAY_A );
foreach ( $galleries_data as $gallery ) {
// Transform database format to match expected format
$gallery_item = [
'id' => $gallery['id'],
'name' => $gallery['name'],
'description' => $gallery['description'],
'layout' => $gallery['layout'],
'medias' => self::hydrate_medias( maybe_unserialize( $gallery['medias'] ) ),
'lead_image_id' => $gallery['lead_image_id'],
'order_by' => $gallery['order_by'],
'is_post_mode' => ( bool )$gallery['is_post_mode'],
'hero' => ( bool )$gallery['is_hero_mode'],
'posts' => $gallery['posts'] ? unserialize( $gallery['posts'] ) : null,
'latest_posts' => $gallery['latest_posts'],
'tags' => $gallery['tags'] ? unserialize( $gallery['tags'] ) : null,
'dynamic_source' => $gallery['dynamic_source'],
'updated' => strtotime( $gallery['updated_at'] )
];
$galleries[] = $gallery_item;
}
}
// Format collection data
$result[$collection_id] = [
'name' => $collection['name'],
'description' => $collection['description'],
'layout' => $collection['layout'],
'galleries_ids' => $galleries_ids,
'galleries' => $galleries,
'updated' => strtotime( $collection['updated_at'] )
];
}
return [
'total' => $total,
'collections' => $result
];
}
}
?>