| 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 |
private static $plugin_option_name = 'mgl_options'; |
| 8 |
private $option_name = 'mgl_options'; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
load_plugin_textdomain( MGL_DOMAIN, false, MGL_PATH . '/languages' ); |
| 12 |
|
| 13 |
// Initializes the classes needed |
| 14 |
MeowCommon_Helpers::is_rest() && new Meow_MGL_Rest( $this ); |
| 15 |
is_admin() && new Meow_MGL_Admin( $this ); |
| 16 |
class_exists( 'MeowPro_MGL_Core' ) && new MeowPro_MGL_Core(); |
| 17 |
|
| 18 |
// The gallery build process should only be enabled if the request is non-asynchronous |
| 19 |
if ( !MeowCommon_Helpers::is_asynchronous_request() ) { |
| 20 |
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'wp_get_attachment_image_attributes' ), 25, 3 ); |
| 21 |
if ( is_admin() || $this->is_gallery_used ) { |
| 22 |
new Meow_MGL_Run( $this ); |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public function can_access_settings() { |
| 28 |
return apply_filters( 'mgl_allow_setup', current_user_can( 'manage_options' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
public function can_access_features() { |
| 32 |
return apply_filters( 'mgl_allow_usage', current_user_can( 'upload_files' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
// Use by the Gutenberg block |
| 36 |
|
| 37 |
// Rewrite the sizes attributes of the src-set for each image |
| 38 |
function wp_get_attachment_image_attributes( $attr, $attachment, $size ) { |
| 39 |
if (!$this->gallery_process) |
| 40 |
return $attr; |
| 41 |
$sizes = null; |
| 42 |
if ( $this->gallery_layout === 'tiles' ) |
| 43 |
$sizes = '50vw'; |
| 44 |
else if ( $this->gallery_layout === 'masonry' ) |
| 45 |
$sizes = '50vw'; |
| 46 |
else if ( $this->gallery_layout === 'square' ) |
| 47 |
$sizes = '33vw'; |
| 48 |
else if ( $this->gallery_layout === 'cascade' ) |
| 49 |
$sizes = '80vw'; |
| 50 |
else if ( $this->gallery_layout === 'justified' ) |
| 51 |
$sizes = '(max-width: 800px) 80vw, 50vw'; |
| 52 |
$sizes = apply_filters( 'mgl_sizes', $sizes, $this->gallery_layout, $attachment, $attr ); |
| 53 |
if ( !empty( $sizes ) ) |
| 54 |
$attr['sizes'] = $sizes; |
| 55 |
return $attr; |
| 56 |
} |
| 57 |
|
| 58 |
function gallery( $atts, $isPreview = false ) { |
| 59 |
$atts = apply_filters( 'shortcode_atts_gallery', $atts, null, $atts ); |
| 60 |
|
| 61 |
// Get the IDs |
| 62 |
$images = array(); |
| 63 |
if ( isset( $atts['ids'] ) ) { |
| 64 |
$images = $atts['ids']; |
| 65 |
} |
| 66 |
if ( isset( $atts['include'] ) ) { |
| 67 |
$images = is_array( $atts['include'] ) ? implode( ',', $atts['include'] ) : $atts['include']; |
| 68 |
$atts['include'] = $images; |
| 69 |
} |
| 70 |
|
| 71 |
// Filter the IDs |
| 72 |
$ids = is_array( $images ) ? $images : explode( ',', $images ); |
| 73 |
$ids = apply_filters( 'mgl_ids', $ids, $atts ); |
| 74 |
$images = implode( ',', $ids ); |
| 75 |
|
| 76 |
// Use attached images if still empty |
| 77 |
if ( empty( $images ) ) { |
| 78 |
$attachments = get_attached_media( 'image' ); |
| 79 |
$attachmentIds = array_map( function($x) { return $x->ID; }, $attachments ); |
| 80 |
if ( !empty( $attachmentIds ) ) { |
| 81 |
$images = implode( ',', $attachmentIds ); |
| 82 |
} |
| 83 |
else { |
| 84 |
return "<p class='meow-error'><b>Meow Gallery:</b> The gallery is empty.</p>"; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ( $isPreview ) { |
| 89 |
$check = explode( ',', $images ); |
| 90 |
$check = array_slice( $check, 0, 40 ); |
| 91 |
$images = implode( ',', $check ); |
| 92 |
} |
| 93 |
|
| 94 |
// Ordering |
| 95 |
if ( isset( $atts['orderby'] ) ) { |
| 96 |
$images = explode( ',', $images ); |
| 97 |
$images = Meow_MGL_OrderBy::run( $images, $atts['orderby'], isset( $atts['order'] ) ? $atts['order'] : 'asc' ); |
| 98 |
$images = implode( ',', $images ); |
| 99 |
} |
| 100 |
|
| 101 |
// Layout |
| 102 |
$layout = 'none'; |
| 103 |
if ( isset( $atts['layout'] ) && $atts['layout'] != 'default' ) |
| 104 |
$layout = $atts['layout']; |
| 105 |
else if ( isset( $atts['mgl-layout'] ) && $atts['mgl-layout'] != 'default' ) |
| 106 |
$layout = $atts['mgl-layout']; |
| 107 |
else |
| 108 |
$layout = $this->get_option( 'layout', 'tiles' ); |
| 109 |
|
| 110 |
// Check the settings |
| 111 |
if ( $layout === 'none' ) |
| 112 |
return gallery_shortcode( $atts ); |
| 113 |
$layoutClass = 'Meow_MGL_Builders_' . ucfirst( $layout ); |
| 114 |
if ( !class_exists( $layoutClass ) ) { |
| 115 |
error_log( "Meow Gallery: Class $layoutClass does not exist." ); |
| 116 |
return "<p class='meow-error'><b>Meow Gallery:</b> The layout $layout is not available in this version.</p>"; |
| 117 |
} |
| 118 |
|
| 119 |
// Captions |
| 120 |
if ( isset( $atts['captions'] ) && ( $atts['captions'] === false || $atts['captions'] === 'false' ) ) { |
| 121 |
// This is to avoid issues linked to the old block editor for the Meow Gallery |
| 122 |
$atts['captions'] = 'none'; |
| 123 |
} |
| 124 |
|
| 125 |
//DEBUG: Display $atts |
| 126 |
//error_log( print_r( $atts, 1 ) ); |
| 127 |
|
| 128 |
// Start the process of building the gallery |
| 129 |
$this->gallery_process = true; |
| 130 |
$this->gallery_layout = $layout; |
| 131 |
|
| 132 |
// This should be probably removed. |
| 133 |
// wp_enqueue_style( 'mgl-css' ); |
| 134 |
|
| 135 |
$infinite = $this->get_option( 'infinite', false ) && class_exists( 'MeowPro_MGL_Core' ); |
| 136 |
$gen = new $layoutClass( $atts, !$isPreview && $infinite, $isPreview ); |
| 137 |
$result = $gen->build( $images ); |
| 138 |
$this->gallery_process = false; |
| 139 |
do_action( 'mgl_' . $layout . '_gallery_created', $layout ); |
| 140 |
//$result = apply_filters( 'post_gallery', $result, $atts, null ); |
| 141 |
|
| 142 |
do_action( 'mgl_gallery_created' ); |
| 143 |
|
| 144 |
return $result; |
| 145 |
} |
| 146 |
|
| 147 |
// #region Options |
| 148 |
|
| 149 |
static function get_plugin_option_name() { |
| 150 |
return self::$plugin_option_name; |
| 151 |
} |
| 152 |
|
| 153 |
static function get_plugin_option( $option_name, $default = null ) { |
| 154 |
$options = get_option( self::$plugin_option_name, null ); |
| 155 |
return $options[ $option_name ] ?? $default; |
| 156 |
} |
| 157 |
|
| 158 |
function get_option( $option_name, $default = null ) { |
| 159 |
$options = $this->get_all_options(); |
| 160 |
return $options[ $option_name ] ?? $default; |
| 161 |
} |
| 162 |
|
| 163 |
function list_options() { |
| 164 |
return array( |
| 165 |
'layout' => 'tiles', |
| 166 |
'captions' => 'none', |
| 167 |
'animation' => false, |
| 168 |
'image_size' => 'srcset', |
| 169 |
'infinite' => false, |
| 170 |
'infinite_buffer' => 0, |
| 171 |
'tiles_gutter' => 5, |
| 172 |
'tiles_gutter_tablet' => 5, |
| 173 |
'tiles_gutter_mobile' => 5, |
| 174 |
'tiles_density' => 'high', |
| 175 |
'tiles_density_tablet' => 'medium', |
| 176 |
'tiles_density_mobile' => 'low', |
| 177 |
'masonry_gutter' => 5, |
| 178 |
'masonry_columns' => 3, |
| 179 |
'justified_gutter' => 5, |
| 180 |
'justified_row_height' => 200, |
| 181 |
'square_gutter' => 5, |
| 182 |
'square_columns' => 5, |
| 183 |
'cascade_gutter' => 5, |
| 184 |
'horizontal_gutter' => 5, |
| 185 |
'horizontal_image_height' => 500, |
| 186 |
'horizontal_hide_scrollbar' => false, |
| 187 |
'carousel_gutter' => 5, |
| 188 |
'carousel_image_height' => 500, |
| 189 |
'carousel_arrow_nav_enabled' => true, |
| 190 |
'carousel_dot_nav_enabled' => true, |
| 191 |
'map_engine' => '', |
| 192 |
'map_height' => 400, |
| 193 |
'googlemaps_token' => '', |
| 194 |
'googlemaps_style' => '[]', |
| 195 |
'mapbox_token' => '', |
| 196 |
'mapbox_style' => '{"username":"", "style_id":""}', |
| 197 |
'maptiler_token' => '', |
| 198 |
'right_click' => false |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
function get_all_options() { |
| 203 |
$options = get_option( $this->option_name, null ); |
| 204 |
$options = $this->check_options( $options ); |
| 205 |
return $options; |
| 206 |
} |
| 207 |
|
| 208 |
// Upgrade from the old way of storing options to the new way. |
| 209 |
function check_options( $options = [] ) { |
| 210 |
$plugin_options = $this->list_options(); |
| 211 |
$options = empty( $options ) ? [] : $options; |
| 212 |
$hasChanges = false; |
| 213 |
foreach ( $plugin_options as $option => $default ) { |
| 214 |
// The option already exists |
| 215 |
if ( isset( $options[$option] ) ) { |
| 216 |
continue; |
| 217 |
} |
| 218 |
// The option does not exist, so we need to add it. |
| 219 |
// Let's use the old value if any, or the default value. |
| 220 |
$options[$option] = get_option( 'mgl_' . $option, $default ); |
| 221 |
delete_option( 'mgl_' . $option ); |
| 222 |
$hasChanges = true; |
| 223 |
} |
| 224 |
if ( $hasChanges ) { |
| 225 |
update_option( $this->option_name , $options ); |
| 226 |
} |
| 227 |
return $options; |
| 228 |
} |
| 229 |
|
| 230 |
function update_options( $options ) { |
| 231 |
if ( !update_option( $this->option_name, $options, false ) ) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
$options = $this->sanitize_options(); |
| 235 |
return $options; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
// Validate and keep the options clean and logical. |
| 240 |
function sanitize_options() { |
| 241 |
$options = $this->get_all_options(); |
| 242 |
// something to do |
| 243 |
return $options; |
| 244 |
} |
| 245 |
|
| 246 |
# endregion |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
?> |
| 251 |
|