| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_Core { |
| 4 |
|
| 5 |
private $gallery_process = false; |
| 6 |
private $is_gallery_used = true; // TODO: Would be nice to detect if the gallery is actually used on the current page. |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
load_plugin_textdomain( MGL_DOMAIN, false, MGL_PATH . '/languages' ); |
| 10 |
|
| 11 |
// Initializes the classes needed |
| 12 |
MeowCommon_Helpers::is_rest() && new Meow_MGL_Rest( $this ); |
| 13 |
is_admin() && new Meow_MGL_Admin(); |
| 14 |
class_exists( 'MeowPro_MGL_Core' ) && new MeowPro_MGL_Core(); |
| 15 |
|
| 16 |
// The gallery build process should only be enabled if the request is non-asynchronous |
| 17 |
if ( !MeowCommon_Helpers::is_asynchronous_request() ) { |
| 18 |
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'wp_get_attachment_image_attributes' ), 25, 3 ); |
| 19 |
if ( is_admin() || $this->is_gallery_used ) { |
| 20 |
new Meow_MGL_Run( $this ); |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function can_access_settings() { |
| 26 |
return apply_filters( 'mgl_allow_setup', current_user_can( 'manage_options' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
public function can_access_features() { |
| 30 |
return apply_filters( 'mgl_allow_usage', current_user_can( 'upload_files' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
// Use by the Gutenberg block |
| 34 |
|
| 35 |
// Rewrite the sizes attributes of the src-set for each image |
| 36 |
function wp_get_attachment_image_attributes( $attr, $attachment, $size ) { |
| 37 |
if (!$this->gallery_process) |
| 38 |
return $attr; |
| 39 |
$sizes = null; |
| 40 |
if ( $this->gallery_layout === 'tiles' ) |
| 41 |
$sizes = '50vw'; |
| 42 |
else if ( $this->gallery_layout === 'masonry' ) |
| 43 |
$sizes = '50vw'; |
| 44 |
else if ( $this->gallery_layout === 'square' ) |
| 45 |
$sizes = '33vw'; |
| 46 |
else if ( $this->gallery_layout === 'cascade' ) |
| 47 |
$sizes = '80vw'; |
| 48 |
else if ( $this->gallery_layout === 'justified' ) |
| 49 |
$sizes = '(max-width: 800px) 80vw, 50vw'; |
| 50 |
$sizes = apply_filters( 'mgl_sizes', $sizes, $this->gallery_layout, $attachment, $attr ); |
| 51 |
if ( !empty( $sizes ) ) |
| 52 |
$attr['sizes'] = $sizes; |
| 53 |
return $attr; |
| 54 |
} |
| 55 |
|
| 56 |
function gallery( $atts, $isPreview = false ) { |
| 57 |
$atts = apply_filters( 'shortcode_atts_gallery', $atts, null, $atts ); |
| 58 |
|
| 59 |
// Get the IDs |
| 60 |
$images = array(); |
| 61 |
if ( isset( $atts['ids'] ) ) { |
| 62 |
$images = $atts['ids']; |
| 63 |
} |
| 64 |
if ( isset( $atts['include'] ) ) { |
| 65 |
$images = is_array( $atts['include'] ) ? implode( ',', $atts['include'] ) : $atts['include']; |
| 66 |
$atts['include'] = $images; |
| 67 |
} |
| 68 |
|
| 69 |
// Filter the IDs |
| 70 |
$ids = is_array( $images ) ? $images : explode( ',', $images ); |
| 71 |
$ids = apply_filters( 'mgl_ids', $ids, $atts ); |
| 72 |
$images = implode( ',', $ids ); |
| 73 |
|
| 74 |
// Use attached images if still empty |
| 75 |
if ( empty( $images ) ) { |
| 76 |
$attachments = get_attached_media( 'image' ); |
| 77 |
$attachmentIds = array_map( function($x) { return $x->ID; }, $attachments ); |
| 78 |
if ( !empty( $attachmentIds ) ) { |
| 79 |
$images = implode( ',', $attachmentIds ); |
| 80 |
} |
| 81 |
else { |
| 82 |
return "<p class='meow-error'><b>Meow Gallery:</b> The gallery is empty.</p>"; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( $isPreview ) { |
| 87 |
$check = explode( ',', $images ); |
| 88 |
$check = array_slice( $check, 0, 40 ); |
| 89 |
$images = implode( ',', $check ); |
| 90 |
} |
| 91 |
|
| 92 |
// Ordering |
| 93 |
if ( isset( $atts['orderby'] ) ) { |
| 94 |
$images = explode( ',', $images ); |
| 95 |
$images = Meow_MGL_OrderBy::run( $images, $atts['orderby'], isset( $atts['order'] ) ? $atts['order'] : 'asc' ); |
| 96 |
$images = implode( ',', $images ); |
| 97 |
} |
| 98 |
|
| 99 |
// Layout |
| 100 |
$layout = 'none'; |
| 101 |
if ( isset( $atts['layout'] ) && $atts['layout'] != 'default' ) |
| 102 |
$layout = $atts['layout']; |
| 103 |
else if ( isset( $atts['mgl-layout'] ) && $atts['mgl-layout'] != 'default' ) |
| 104 |
$layout = $atts['mgl-layout']; |
| 105 |
else |
| 106 |
$layout = get_option( 'mgl_layout', 'tiles' ); |
| 107 |
|
| 108 |
// Check the settings |
| 109 |
if ( $layout === 'none' ) |
| 110 |
return gallery_shortcode( $atts ); |
| 111 |
$layoutClass = 'Meow_MGL_Builders_' . ucfirst( $layout ); |
| 112 |
if ( !class_exists( $layoutClass ) ) { |
| 113 |
error_log( "Meow Gallery: Class $layoutClass does not exist." ); |
| 114 |
return "<p class='meow-error'><b>Meow Gallery:</b> The layout $layout is not available in this version.</p>"; |
| 115 |
} |
| 116 |
|
| 117 |
// Captions |
| 118 |
if ( isset( $atts['captions'] ) && ( $atts['captions'] === false || $atts['captions'] === 'false' ) ) { |
| 119 |
// This is to avoid issues linked to the old block editor for the Meow Gallery |
| 120 |
$atts['captions'] = 'none'; |
| 121 |
} |
| 122 |
|
| 123 |
//DEBUG: Display $atts |
| 124 |
//error_log( print_r( $atts, 1 ) ); |
| 125 |
|
| 126 |
// Start the process of building the gallery |
| 127 |
$this->gallery_process = true; |
| 128 |
$this->gallery_layout = $layout; |
| 129 |
|
| 130 |
// This should be probably removed. |
| 131 |
// wp_enqueue_style( 'mgl-css' ); |
| 132 |
|
| 133 |
$infinite = get_option( 'mgl_infinite', false ) && class_exists( 'MeowPro_MGL_Core' ); |
| 134 |
$gen = new $layoutClass( $atts, !$isPreview && $infinite, $isPreview ); |
| 135 |
$result = $gen->build( $images ); |
| 136 |
$this->gallery_process = false; |
| 137 |
do_action( 'mgl_' . $layout . '_gallery_created', $layout ); |
| 138 |
//$result = apply_filters( 'post_gallery', $result, $atts, null ); |
| 139 |
|
| 140 |
do_action( 'mgl_gallery_created' ); |
| 141 |
|
| 142 |
return $result; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
?> |
| 147 |
|