id = uniqid(); $this->size = isset( $atts['size'] ) ? $atts['size'] : $this->size; $this->size = apply_filters( 'mgl_media_size', $this->size ); $this->infinite = $infinite; $this->atts = $atts; $this->customClass = isset( $atts['custom-class'] ) ? $atts['custom-class'] : null; $this->link = isset( $atts['link'] ) ? $atts['link'] : null; $this->align = isset( $atts['align'] ) ? $atts['align'] : $this->align; $this->isPreview = $isPreview; $this->class_id = 'mgl-gallery-' . $this->id; $this->updir = trailingslashit( $wpUploadDir['baseurl'] ); $this->captions = isset( $atts['captions'] ) ? $atts['captions'] : get_option( 'mgl_captions', 'none' ); $this->animation = null; if ( isset( $atts['animation'] ) && $atts['animation'] != 'default' ) $this->animation = $atts['animation']; else $this->animation = get_option( 'mgl_animation', null ); } function build_inline_attributes( $id, $data ) { return ''; } function prepare_data( $idsStr ) { global $wpdb; // Escape the array of IDs for SQL $ids = array_map( 'intval', explode(',', $idsStr ) ); $idsStr = implode( ',', $ids ); $query = "SELECT p.ID id, p.post_excerpt caption, m.meta_value meta FROM $wpdb->posts p, $wpdb->postmeta m WHERE m.meta_key = '_wp_attachment_metadata' AND p.ID = m.post_id AND p.ID IN (" . $idsStr . ")"; $res = $wpdb->get_results( $query ); $this->ids = explode( ',', $idsStr ); foreach ( $res as $r ) { $this->data[$r->id] = array( 'caption' => $r->caption,'meta' => unserialize( $r->meta ) ); } $this->ids = array_reverse( $this->ids ); $cleanIds = array(); foreach ( $this->ids as $id ) { if ( isset( $this->data[$id] ) ) array_push( $cleanIds, $id ); } $this->ids = apply_filters( 'mgl_sort', $cleanIds, $this->data, $this->layout, $this->atts ); } function build_next_cell( $id, $data ) { $src = $this->updir . $data['meta']['file']; $data['caption'] = apply_filters( 'mgl_caption', $data['caption'], $id ); $caption = $this->captions ? $data['caption'] : ''; $image_size = get_option( 'mgl_image_size', 'srcset' ); $imgSrc = null; if ( empty( $image_size ) || $image_size === 'srcset' ) { $imgSrc = wp_get_attachment_image( $id, $this->size, false, $this->layout === 'carousel' ? array( 'class' => 'skip-lazy' ) : array( 'class' => 'wp-image-' . $id ) ); } else { $info = wp_get_attachment_image_src( $id, $image_size ); $imgSrc = ''; } $attributes = $this->build_inline_attributes( $id, $data ); if ( !empty( $attributes ) ) { $attributes = ' ' . $attributes; } $linkUrl = null; if ( $this->link === 'attachment' ) $linkUrl = get_permalink( (int)$id ); else if ( $this->link === 'media' || $this->link === 'file' ) $linkUrl = $src; $linkUrl = apply_filters( 'mgl_link', $linkUrl, (int)$id, $data ); $isPreview = $this->isPreview; ob_start(); include dirname( __FILE__ ) . '/cell.tpl.php'; $html = ob_get_clean(); return $html; } function build_container_classes() { $classes = 'mgl-' . $this->layout . '-container'; // Align $classes .= $this->align ? (' align' . $this->align) : ''; return $classes; } function build_classes() { $classes = 'mgl-gallery'; // Layout $classes .= ' mgl-' . $this->layout; // Custom Class $classes .= $this->customClass ? ( ' ' . $this->customClass ) : ''; // Animation if ( $this->animation ) $classes .= ' is-animated ' . $this->animation; // Captions if ( $this->captions ) $classes .= ' captions-' . $this->captions; return $classes; } function build_styles() { return ""; } function build( $idsStr ) { // Generate gallery $classes = $this->build_classes(); $styles = $this->build_styles(); $out = "
"; $this->prepare_data( $idsStr ); while ( count( $this->ids ) > 0 ) { $id = array_pop( $this->ids ); $out .= $this->build_next_cell( $id, $this->data[$id] ); } $out .= '
'; $out = apply_filters( 'mgl_gallery_written', $out, $this->layout ); // Generate gallery container $container_classes = $this->build_container_classes(); $inline_css = $this->inline_css(); if ( !empty( $inline_css ) ) { $inline_css = preg_replace( "/\r|\n/", "", $inline_css ); $container = "
{$inline_css}{$out}
"; } else { $container = "
{$out}
"; } return $container; } } ?>