| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_Builders_Cascade extends Meow_MGL_Builders_Core { |
| 4 |
|
| 5 |
public $layouts = array(); |
| 6 |
|
| 7 |
public function __construct( $atts, $infinite, $isPreview = false ) { |
| 8 |
parent::__construct( $atts, $infinite, $isPreview ); |
| 9 |
$this->layout = 'cascade'; |
| 10 |
} |
| 11 |
|
| 12 |
function prepare_layouts() { |
| 13 |
$this->layouts = [ |
| 14 |
'o', 'i', 'ii' |
| 15 |
]; |
| 16 |
} |
| 17 |
|
| 18 |
function inline_css() { |
| 19 |
$class_id = '#' . $this->class_id; |
| 20 |
$gutter = isset( $this->atts['gutter'] ) ? |
| 21 |
$this->atts['gutter'] : Meow_MGL_Core::get_plugin_option( 'cascade_gutter', 10 ); |
| 22 |
$isPreview = $this->isPreview; |
| 23 |
ob_start(); |
| 24 |
include dirname( __FILE__ ) . '/cascade.css.php'; |
| 25 |
$html = ob_get_clean(); |
| 26 |
return $html; |
| 27 |
} |
| 28 |
|
| 29 |
function get_layout( $ids ) { |
| 30 |
$ids = array_reverse( $ids ); |
| 31 |
$layout = ""; |
| 32 |
foreach ( $ids as $id ) |
| 33 |
$layout .= $this->data[$id]['meta']['width'] >= $this->data[$id]['meta']['height'] ? 'o' : 'i'; |
| 34 |
return $layout; |
| 35 |
} |
| 36 |
|
| 37 |
function build_next_cell( $id, $data ) { |
| 38 |
$html = parent::build_next_cell( $id, $data ); |
| 39 |
$html = str_replace( '100vw', 100 / 3 . 'vw', $html ); |
| 40 |
return $html; |
| 41 |
} |
| 42 |
|
| 43 |
function build( $idsStr ) { |
| 44 |
|
| 45 |
// Generate gallery |
| 46 |
$classes = $this->build_classes(); |
| 47 |
$styles = $this->build_styles(); |
| 48 |
$out = "<div id='{$this->class_id}' class='{$classes}' style='{$styles}'>"; |
| 49 |
$this->prepare_data( $idsStr ); |
| 50 |
$this->prepare_layouts(); |
| 51 |
$ooo_v = 0; |
| 52 |
|
| 53 |
while ( count( $this->ids ) > 0 ) { |
| 54 |
|
| 55 |
$size = 2; |
| 56 |
$layout = null; |
| 57 |
$ideal = "N/A"; |
| 58 |
while ( !$layout && $size > 0 ) { |
| 59 |
// Look for exact layout $size-cols |
| 60 |
$currentIds = array_slice( $this->ids, $size * -1, $size, true ); |
| 61 |
$ideal = $this->get_layout( $currentIds ); |
| 62 |
if ( in_array( $ideal, $this->layouts ) ) { |
| 63 |
$layout = $ideal; |
| 64 |
break; |
| 65 |
} |
| 66 |
$size--; |
| 67 |
} |
| 68 |
|
| 69 |
// Display an error if the layout does not exist |
| 70 |
if ( !$layout ) { |
| 71 |
echo( '<div style="padding: 20px; background: darkred; color: white;"> |
| 72 |
MEOW GALLERY ERROR. No layout for '. $ideal . '.</div>' |
| 73 |
); |
| 74 |
$layout = $ideal; |
| 75 |
} |
| 76 |
|
| 77 |
// Create row with cells inside it (using the order in currentIds) |
| 78 |
$count = 0; |
| 79 |
$out .= '<div class="mgl-row mgl-layout-' . strlen( $layout ) . '-' . $layout . '" data-cascade-layout="'. $layout .'">'; |
| 80 |
while ( count( $currentIds ) > 0 ) { |
| 81 |
$out .= '<div class="mgl-box ' . chr( 97 + $count++ ) . '">'; |
| 82 |
$id = array_pop( $this->ids ); |
| 83 |
$id = array_pop( $currentIds ); |
| 84 |
$out .= $this->build_next_cell( $id, $this->data[$id] ); |
| 85 |
$out .= '</div>'; |
| 86 |
} |
| 87 |
$out .= '</div>'; |
| 88 |
} |
| 89 |
$out .= '</div>'; |
| 90 |
$out = apply_filters( 'mgl_gallery_written', $out, $this->layout ); |
| 91 |
|
| 92 |
// Generate gallery container |
| 93 |
$container_classes = $this->build_container_classes(); |
| 94 |
$inline_css = $this->inline_css(); |
| 95 |
$container = "<div class='{$container_classes}'>{$inline_css}{$out}</div>"; |
| 96 |
|
| 97 |
return $container; |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
?> |
| 103 |
|