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