| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_Builders_Tiles_PHP extends Meow_MGL_Builders_Core { |
| 4 |
|
| 5 |
public $min_columns = 3; |
| 6 |
public $max_columns = 4; |
| 7 |
public $layouts = array(); |
| 8 |
|
| 9 |
public function __construct( $atts, $infinite, $isPreview = false ) { |
| 10 |
parent::__construct( $atts, $infinite, $isPreview ); |
| 11 |
$this->layout = 'tiles'; |
| 12 |
} |
| 13 |
|
| 14 |
function prepare_layouts() { |
| 15 |
$this->layouts = [ |
| 16 |
'o', 'i', |
| 17 |
'oo', 'ii', 'oi', 'io', |
| 18 |
'ooo', 'oii', 'ooi', 'ioo', 'oio', 'ioi', 'iio', 'iii', |
| 19 |
'iooo', 'oioo', 'ooio', 'oooi', 'iiii', 'oooo', |
| 20 |
'ioooo', 'ooioo', 'ooooi', 'iiooo', 'iooio', 'ooiio', 'ooioi', 'oooii', 'oiioo', 'oiooi', 'iiioo', 'iiooi', 'iooii', 'ooiii' |
| 21 |
]; |
| 22 |
} |
| 23 |
|
| 24 |
function inline_css() { |
| 25 |
$class_id = '#' . $this->class_id; |
| 26 |
$gutter = isset( $this->atts['gutter'] ) ? |
| 27 |
$this->atts['gutter'] : Meow_MGL_Core::get_plugin_option( 'tiles_gutter', 10 ); |
| 28 |
$isPreview = $this->isPreview; |
| 29 |
ob_start(); |
| 30 |
include dirname( __FILE__ ) . '/tiles.css.php'; |
| 31 |
$html = ob_get_clean(); |
| 32 |
return $html; |
| 33 |
} |
| 34 |
|
| 35 |
function permutations( $items, $perms = array() ) { |
| 36 |
$back = array(); |
| 37 |
if ( empty( $items ) ) { |
| 38 |
array_push( $back, join( ' ', $perms ) ); |
| 39 |
return $back; |
| 40 |
} |
| 41 |
for ( $i = count( $items ) - 1; $i >= 0; --$i ) { |
| 42 |
$new = $items; |
| 43 |
$newp = $perms; |
| 44 |
list( $foo ) = array_splice( $new, $i, 1 ); |
| 45 |
array_unshift( $newp, $foo ); |
| 46 |
$back = array_merge( $back, permutations( $new, $newp ) ); |
| 47 |
} |
| 48 |
return $back; |
| 49 |
} |
| 50 |
|
| 51 |
function get_layout( $ids ) { |
| 52 |
$ids = array_reverse( $ids ); |
| 53 |
$layout = ""; |
| 54 |
foreach ( $ids as $id ) |
| 55 |
$layout .= $this->data[$id]['meta']['width'] >= $this->data[$id]['meta']['height'] ? 'o' : 'i'; |
| 56 |
return $layout; |
| 57 |
} |
| 58 |
|
| 59 |
function build_next_cell( $id, $data ) { |
| 60 |
$html = parent::build_next_cell( $id, $data ); |
| 61 |
$html = str_replace( '100vw', 100 / 3 . 'vw', $html ); |
| 62 |
return $html; |
| 63 |
} |
| 64 |
|
| 65 |
function build( $idsStr ) { |
| 66 |
|
| 67 |
// Generate gallery |
| 68 |
$classes = $this->build_classes(); |
| 69 |
$styles = $this->build_styles(); |
| 70 |
$out = "<div id='{$this->class_id}' class='{$classes}' style='{$styles}'>"; |
| 71 |
$this->prepare_data( $idsStr ); |
| 72 |
$this->prepare_layouts(); |
| 73 |
$ooo_v = 0; |
| 74 |
|
| 75 |
while ( count( $this->ids ) > 0 ) { |
| 76 |
$size = 5; |
| 77 |
|
| 78 |
// This seems to handle all issues with the solitary photo |
| 79 |
$howManyLeft = count( $this->ids ); |
| 80 |
if ( $howManyLeft >= 4 && $howManyLeft <= 6) { |
| 81 |
$size = $howManyLeft - 2; |
| 82 |
$currentIds = array_slice( $this->ids, $size * -1, $size, true ); |
| 83 |
$ideal = $this->get_layout( $currentIds ); |
| 84 |
if ( in_array( $ideal, $this->layouts ) ) |
| 85 |
$size = $howManyLeft - 2; |
| 86 |
else |
| 87 |
$size = $howManyLeft - 3; |
| 88 |
} |
| 89 |
|
| 90 |
$layout = null; |
| 91 |
$ideal = "N/A"; |
| 92 |
while ( !$layout && $size > 0 ) { |
| 93 |
// Look for exact layout $size-cols |
| 94 |
$currentIds = array_slice( $this->ids, $size * -1, $size, true ); |
| 95 |
$ideal = $this->get_layout( $currentIds ); |
| 96 |
if ( in_array( $ideal, $this->layouts ) ) { |
| 97 |
$layout = $ideal; |
| 98 |
break; |
| 99 |
} |
| 100 |
$size--; |
| 101 |
} |
| 102 |
|
| 103 |
// Display an error if the layout does not exist |
| 104 |
if ( !$layout ) { |
| 105 |
echo( '<div style="padding: 20px; background: darkred; color: white;"> |
| 106 |
MEOW GALLERY ERROR. No layout for '. $ideal . '.</div>' |
| 107 |
); |
| 108 |
$layout = $ideal; |
| 109 |
} |
| 110 |
|
| 111 |
// Variations in order to avoid the same layout to be repeated |
| 112 |
$variation = ''; |
| 113 |
if ( $layout === 'oooo' ) { |
| 114 |
$variation = '-v' . $ooo_v; |
| 115 |
$ooo_v = $ooo_v > 1 ? 0 : $ooo_v + 1; |
| 116 |
} |
| 117 |
|
| 118 |
// Create row with cells inside it (using the order in currentIds) |
| 119 |
$count = 0; |
| 120 |
$out .= '<div class="mgl-row mgl-layout-' . strlen( $layout ) . '-' . $layout . $variation . '" data-tiles-layout="'. $layout . $variation .'">'; |
| 121 |
while ( count( $currentIds ) > 0 ) { |
| 122 |
$out .= '<div class="mgl-box ' . chr( 97 + $count++ ) . '">'; |
| 123 |
$id = array_pop( $this->ids ); |
| 124 |
$id = array_pop( $currentIds ); |
| 125 |
$out .= $this->build_next_cell( $id, $this->data[$id] ); |
| 126 |
$out .= '</div>'; |
| 127 |
} |
| 128 |
$out .= '</div>'; |
| 129 |
} |
| 130 |
$out .= '</div>'; |
| 131 |
$out = apply_filters( 'mgl_gallery_written', $out, $this->layout ); |
| 132 |
|
| 133 |
// Generate gallery container |
| 134 |
$container_classes = $this->build_container_classes(); |
| 135 |
$inline_css = $this->inline_css(); |
| 136 |
$container = "<div class='{$container_classes}'>{$inline_css}{$out}</div>"; |
| 137 |
return $container; |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
?> |
| 143 |
|