| 1 |
<?php |
| 2 |
|
| 3 |
abstract class Meow_MGL_Builders_Core { |
| 4 |
|
| 5 |
public $id = null; |
| 6 |
public $layout = 'none'; |
| 7 |
public $class_id = 'mgl-gallery-none'; |
| 8 |
public $img_class = ''; |
| 9 |
public $size = 'large'; |
| 10 |
public $customClass = ''; |
| 11 |
public $align = null; |
| 12 |
public $ids = array(); |
| 13 |
public $link = null; |
| 14 |
public $atts = array(); |
| 15 |
public $data = array(); |
| 16 |
public $isPreview = false; |
| 17 |
public $updir = null; |
| 18 |
public $captions = false; |
| 19 |
public $animation = null; |
| 20 |
|
| 21 |
abstract function inline_css(); |
| 22 |
|
| 23 |
public function __construct( $atts, $infinite, $isPreview = false ) { |
| 24 |
$wpUploadDir = wp_upload_dir(); |
| 25 |
$this->id = uniqid(); |
| 26 |
$this->size = isset( $atts['size'] ) ? $atts['size'] : $this->size; |
| 27 |
$this->size = apply_filters( 'mgl_media_size', $this->size ); |
| 28 |
$this->infinite = $infinite; |
| 29 |
$this->atts = $atts; |
| 30 |
$this->customClass = isset( $atts['custom-class'] ) ? $atts['custom-class'] : null; |
| 31 |
$this->link = isset( $atts['link'] ) ? $atts['link'] : null; |
| 32 |
$this->align = isset( $atts['align'] ) ? $atts['align'] : $this->align; |
| 33 |
$this->isPreview = $isPreview; |
| 34 |
$this->class_id = 'mgl-gallery-' . $this->id; |
| 35 |
$this->updir = trailingslashit( $wpUploadDir['baseurl'] ); |
| 36 |
$this->captions = isset( $atts['captions'] ) ? $atts['captions'] : get_option( 'mgl_captions', 'none' ); |
| 37 |
$this->animation = null; |
| 38 |
if ( isset( $atts['animation'] ) && $atts['animation'] != 'default' ) |
| 39 |
$this->animation = $atts['animation']; |
| 40 |
else |
| 41 |
$this->animation = get_option( 'mgl_animation', null ); |
| 42 |
} |
| 43 |
|
| 44 |
function build_inline_attributes( $id, $data ) { |
| 45 |
return ''; |
| 46 |
} |
| 47 |
|
| 48 |
function prepare_data( $idsStr ) { |
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
// Escape the array of IDs for SQL |
| 52 |
$ids = array_map( 'intval', explode(',', $idsStr ) ); |
| 53 |
$idsStr = implode( ',', $ids ); |
| 54 |
|
| 55 |
$query = "SELECT p.ID id, p.post_excerpt caption, m.meta_value meta |
| 56 |
FROM $wpdb->posts p, $wpdb->postmeta m |
| 57 |
WHERE m.meta_key = '_wp_attachment_metadata' |
| 58 |
AND p.ID = m.post_id |
| 59 |
AND p.ID IN (" . $idsStr . ")"; |
| 60 |
$res = $wpdb->get_results( $query ); |
| 61 |
$this->ids = explode( ',', $idsStr ); |
| 62 |
foreach ( $res as $r ) { |
| 63 |
$this->data[$r->id] = array( 'caption' => $r->caption,'meta' => unserialize( $r->meta ) ); |
| 64 |
} |
| 65 |
$this->ids = array_reverse( $this->ids ); |
| 66 |
$cleanIds = array(); |
| 67 |
foreach ( $this->ids as $id ) { |
| 68 |
if ( isset( $this->data[$id] ) ) |
| 69 |
array_push( $cleanIds, $id ); |
| 70 |
} |
| 71 |
$this->ids = apply_filters( 'mgl_sort', $cleanIds, $this->data, $this->layout, $this->atts ); |
| 72 |
} |
| 73 |
|
| 74 |
function build_next_cell( $id, $data ) { |
| 75 |
$src = $this->updir . $data['meta']['file']; |
| 76 |
$data['caption'] = apply_filters( 'mgl_caption', $data['caption'], $id ); |
| 77 |
$caption = $this->captions ? $data['caption'] : ''; |
| 78 |
|
| 79 |
$image_size = get_option( 'mgl_image_size', 'srcset' ); |
| 80 |
$imgSrc = null; |
| 81 |
if ( empty( $image_size ) || $image_size === 'srcset' ) { |
| 82 |
$imgSrc = wp_get_attachment_image( $id, $this->size, false, |
| 83 |
$this->layout === 'carousel' ? array( 'class' => 'skip-lazy' ) : array( 'class' => 'wp-image-' . $id ) ); |
| 84 |
} |
| 85 |
else { |
| 86 |
$info = wp_get_attachment_image_src( $id, $image_size ); |
| 87 |
$imgSrc = '<img src="' . $info[0] . '" class="' . |
| 88 |
( $this->layout === 'carousel' ? 'skip-lazy' : ( 'wp-image-' . $id ) ) . '" />'; |
| 89 |
} |
| 90 |
$attributes = $this->build_inline_attributes( $id, $data ); |
| 91 |
if ( !empty( $attributes ) ) { |
| 92 |
$attributes = ' ' . $attributes; |
| 93 |
} |
| 94 |
|
| 95 |
$linkUrl = null; |
| 96 |
if ( $this->link === 'attachment' ) |
| 97 |
$linkUrl = get_permalink( (int)$id ); |
| 98 |
else if ( $this->link === 'media' || $this->link === 'file' ) |
| 99 |
$linkUrl = $src; |
| 100 |
$linkUrl = apply_filters( 'mgl_link', $linkUrl, (int)$id, $data ); |
| 101 |
$isPreview = $this->isPreview; |
| 102 |
ob_start(); |
| 103 |
include dirname( __FILE__ ) . '/cell.tpl.php'; |
| 104 |
$html = ob_get_clean(); |
| 105 |
return $html; |
| 106 |
} |
| 107 |
|
| 108 |
function build_container_classes() { |
| 109 |
$classes = 'mgl-' . $this->layout . '-container'; |
| 110 |
|
| 111 |
// Align |
| 112 |
$classes .= $this->align ? (' align' . $this->align) : ''; |
| 113 |
|
| 114 |
return $classes; |
| 115 |
} |
| 116 |
|
| 117 |
function build_classes() { |
| 118 |
$classes = 'mgl-gallery'; |
| 119 |
|
| 120 |
// Layout |
| 121 |
$classes .= ' mgl-' . $this->layout; |
| 122 |
|
| 123 |
// Custom Class |
| 124 |
$classes .= $this->customClass ? ( ' ' . $this->customClass ) : ''; |
| 125 |
|
| 126 |
// Animation |
| 127 |
if ( $this->animation ) |
| 128 |
$classes .= ' is-animated ' . $this->animation; |
| 129 |
|
| 130 |
// Captions |
| 131 |
if ( $this->captions ) |
| 132 |
$classes .= ' captions-' . $this->captions; |
| 133 |
|
| 134 |
return $classes; |
| 135 |
} |
| 136 |
|
| 137 |
function build_styles() { |
| 138 |
return ""; |
| 139 |
} |
| 140 |
|
| 141 |
function build( $idsStr ) { |
| 142 |
|
| 143 |
// Generate gallery |
| 144 |
$classes = $this->build_classes(); |
| 145 |
$styles = $this->build_styles(); |
| 146 |
$out = "<div id='{$this->class_id}' class='{$classes}' style='{$styles}'>"; |
| 147 |
$this->prepare_data( $idsStr ); |
| 148 |
while ( count( $this->ids ) > 0 ) { |
| 149 |
$id = array_pop( $this->ids ); |
| 150 |
$out .= $this->build_next_cell( $id, $this->data[$id] ); |
| 151 |
} |
| 152 |
$out .= '</div>'; |
| 153 |
$out = apply_filters( 'mgl_gallery_written', $out, $this->layout ); |
| 154 |
|
| 155 |
// Generate gallery container |
| 156 |
$container_classes = $this->build_container_classes(); |
| 157 |
$inline_css = $this->inline_css(); |
| 158 |
if ( !empty( $inline_css ) ) { |
| 159 |
$inline_css = preg_replace( "/\r|\n/", "", $inline_css ); |
| 160 |
$container = "<div class='{$container_classes}'>{$inline_css}{$out}</div>"; |
| 161 |
} |
| 162 |
else { |
| 163 |
$container = "<div class='{$container_classes}'>{$out}</div>"; |
| 164 |
} |
| 165 |
|
| 166 |
return $container; |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
?> |
| 172 |
|