| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gallery |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( class_exists( 'Powerkit_Module' ) ) { |
| 10 |
/** |
| 11 |
* Init module |
| 12 |
*/ |
| 13 |
class Powerkit_Gallery extends Powerkit_Module { |
| 14 |
|
| 15 |
/** |
| 16 |
* Register module |
| 17 |
*/ |
| 18 |
public function register() { |
| 19 |
$this->name = 'gallery'; |
| 20 |
$this->slug = 'gallery'; |
| 21 |
$this->type = 'extension'; |
| 22 |
$this->category = 'basic'; |
| 23 |
$this->public = false; |
| 24 |
$this->enabled = false; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Initialize module |
| 29 |
*/ |
| 30 |
public function initialize() { |
| 31 |
add_action( 'admin_init', array( $this, 'gallery_init' ) ); |
| 32 |
add_action( 'post_gallery', array( $this, 'change_post_gallery' ), 10, 3 ); |
| 33 |
add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ) ); |
| 34 |
|
| 35 |
// Force Jetpack compatibility with our galleries. |
| 36 |
add_filter( 'jp_carousel_force_enable', '__return_true' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Gallery Shortcode Function |
| 41 |
* |
| 42 |
* @param string $output The current output. |
| 43 |
* @param array $attr Attributes from the gallery shortcode. |
| 44 |
* @param int $instance Numeric ID of the gallery shortcode instance. |
| 45 |
*/ |
| 46 |
public function change_post_gallery( $output, $attr, $instance ) { |
| 47 |
|
| 48 |
// Make sure we're overwriting only galleries with either layout or type attributes. |
| 49 |
if ( ! ( isset( $attr['layout'] ) || isset( $attr['type'] ) ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Support for the deprecated layout attribute. |
| 54 |
if ( isset( $attr['layout'] ) ) { |
| 55 |
if ( 'grid' === $attr['layout'] ) { |
| 56 |
$attr['type'] = 'default'; |
| 57 |
} else { |
| 58 |
$attr['type'] = $attr['layout']; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! isset( $attr['type'] ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// Return if type is neither justified nor slider. |
| 67 |
$gallery_types = apply_filters( 'powerkit_gallery_types', array() ); |
| 68 |
|
| 69 |
if ( ! array_key_exists( $attr['type'], (array) $gallery_types ) ) { |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
global $post; |
| 74 |
|
| 75 |
$atts = shortcode_atts( |
| 76 |
array( |
| 77 |
'order' => 'ASC', |
| 78 |
'orderby' => 'menu_order ID', |
| 79 |
'id' => $post ? $post->ID : 0, |
| 80 |
'itemtag' => 'figure', |
| 81 |
'icontag' => 'div', |
| 82 |
'captiontag' => 'figcaption', |
| 83 |
'columns' => 3, |
| 84 |
'type' => 'default', |
| 85 |
'size' => 'thumbnail', |
| 86 |
'include' => '', |
| 87 |
'exclude' => '', |
| 88 |
'link' => '', |
| 89 |
), $attr, 'gallery' |
| 90 |
); |
| 91 |
|
| 92 |
$settings = array( |
| 93 |
'custom_class' => '', |
| 94 |
'custom_attrs' => '', |
| 95 |
); |
| 96 |
|
| 97 |
$settings = apply_filters( 'powerkit_gallery_settings', $settings, $attr ); |
| 98 |
|
| 99 |
$id = intval( $atts['id'] ); |
| 100 |
|
| 101 |
if ( ! empty( $atts['include'] ) ) { |
| 102 |
$_attachments = get_posts( |
| 103 |
array( |
| 104 |
'include' => $atts['include'], |
| 105 |
'post_status' => 'inherit', |
| 106 |
'post_type' => 'attachment', |
| 107 |
'post_mime_type' => 'image', |
| 108 |
'order' => $atts['order'], |
| 109 |
'orderby' => $atts['orderby'], |
| 110 |
) |
| 111 |
); |
| 112 |
|
| 113 |
$attachments = array(); |
| 114 |
foreach ( $_attachments as $key => $val ) { |
| 115 |
$attachments[ $val->ID ] = $_attachments[ $key ]; |
| 116 |
} |
| 117 |
} elseif ( ! empty( $atts['exclude'] ) ) { |
| 118 |
$attachments = get_children( |
| 119 |
array( |
| 120 |
'post_parent' => $id, |
| 121 |
'exclude' => $atts['exclude'], |
| 122 |
'post_status' => 'inherit', |
| 123 |
'post_type' => 'attachment', |
| 124 |
'post_mime_type' => 'image', |
| 125 |
'order' => $atts['order'], |
| 126 |
'orderby' => $atts['orderby'], |
| 127 |
) |
| 128 |
); |
| 129 |
} else { |
| 130 |
$attachments = get_children( |
| 131 |
array( |
| 132 |
'post_parent' => $id, |
| 133 |
'post_status' => 'inherit', |
| 134 |
'post_type' => 'attachment', |
| 135 |
'post_mime_type' => 'image', |
| 136 |
'order' => $atts['order'], |
| 137 |
'orderby' => $atts['orderby'], |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
if ( empty( $attachments ) ) { |
| 143 |
return ''; |
| 144 |
} |
| 145 |
|
| 146 |
if ( is_feed() ) { |
| 147 |
$output = "\n"; |
| 148 |
foreach ( $attachments as $att_id => $attachment ) { |
| 149 |
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n"; |
| 150 |
} |
| 151 |
return $output; |
| 152 |
} |
| 153 |
|
| 154 |
$itemtag = tag_escape( $atts['itemtag'] ); |
| 155 |
$captiontag = tag_escape( $atts['captiontag'] ); |
| 156 |
$custom_class = $settings['custom_class']; |
| 157 |
$custom_attrs = $settings['custom_attrs']; |
| 158 |
|
| 159 |
if ( 'justified' === $atts['type'] ) { |
| 160 |
$captiontag = 'div'; |
| 161 |
} |
| 162 |
|
| 163 |
$type = esc_attr( $atts['type'] ); |
| 164 |
$float = is_rtl() ? 'right' : 'left'; |
| 165 |
|
| 166 |
$selector = "gallery-{$instance}"; |
| 167 |
|
| 168 |
$size_class = sanitize_html_class( $atts['size'] ); |
| 169 |
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-type-{$type} gallery-size-{$size_class} {$custom_class}' {$custom_attrs}>"; |
| 170 |
|
| 171 |
$output = apply_filters( 'powerkit_gallery_style', $gallery_div ); |
| 172 |
|
| 173 |
$i = 0; |
| 174 |
foreach ( $attachments as $id => $attachment ) { |
| 175 |
|
| 176 |
$attr = ( trim( $attachment->post_excerpt ) ) ? array( |
| 177 |
'aria-describedby' => "$selector-$id", |
| 178 |
) : array(); |
| 179 |
|
| 180 |
// Add gallery type attribute. |
| 181 |
$attr['data-gallery'] = $atts['type']; |
| 182 |
|
| 183 |
if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { |
| 184 |
$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); |
| 185 |
} elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { |
| 186 |
$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); |
| 187 |
} else { |
| 188 |
$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); |
| 189 |
} |
| 190 |
$image_meta = wp_get_attachment_metadata( $id ); |
| 191 |
|
| 192 |
$output .= "<{$itemtag} class='gallery-item'>"; |
| 193 |
$output .= $image_output; |
| 194 |
if ( $captiontag && trim( $attachment->post_excerpt ) ) { |
| 195 |
$output .= " |
| 196 |
<{$captiontag} class='caption wp-caption-text gallery-caption' id='$selector-$id'> |
| 197 |
" . wptexturize( $attachment->post_excerpt ) . " |
| 198 |
</{$captiontag}>"; |
| 199 |
} |
| 200 |
$output .= "</{$itemtag}>"; |
| 201 |
} |
| 202 |
|
| 203 |
$output .= '</div>'; |
| 204 |
|
| 205 |
return $output; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Add new gallery types if Jetpack is not installed |
| 210 |
*/ |
| 211 |
public function gallery_init() { |
| 212 |
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Add new gallery types to Jetpack if it's installed |
| 217 |
* |
| 218 |
* @param array $types Existing Jetpack gallery types. |
| 219 |
*/ |
| 220 |
public function jetpack_gallery_types( $types ) { |
| 221 |
|
| 222 |
$gallery_types = apply_filters( |
| 223 |
'powerkit_gallery_types', array( |
| 224 |
'default' => esc_html__( 'Default', 'powerkit' ), |
| 225 |
) |
| 226 |
); |
| 227 |
|
| 228 |
$types = array_merge( $types, $gallery_types ); |
| 229 |
|
| 230 |
return $types; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Add Gallery Type Dropdown |
| 235 |
*/ |
| 236 |
public function print_media_templates() { |
| 237 |
|
| 238 |
// Return if Jetpack's Tiled Gallery. |
| 239 |
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'tiled-gallery' ) ) { |
| 240 |
return ''; |
| 241 |
} |
| 242 |
|
| 243 |
$gallery_types = apply_filters( |
| 244 |
'powerkit_gallery_types', array( |
| 245 |
'default' => esc_html__( 'Default', 'powerkit' ), |
| 246 |
) |
| 247 |
); |
| 248 |
|
| 249 |
$default_gallery_type = apply_filters( 'powerkit_default_gallery_type', 'default' ); |
| 250 |
?> |
| 251 |
|
| 252 |
<script type="text/html" id="tmpl-pk-gallery-settings"> |
| 253 |
<label class="setting"> |
| 254 |
<span><?php esc_html_e( 'Type', 'powerkit' ); ?></span> |
| 255 |
<select class="type" name="type" data-setting="type"> |
| 256 |
<?php foreach ( $gallery_types as $value => $caption ) : ?> |
| 257 |
<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $caption ); ?></option> |
| 258 |
<?php endforeach; ?> |
| 259 |
</select> |
| 260 |
</label> |
| 261 |
</script> |
| 262 |
|
| 263 |
<script> |
| 264 |
jQuery( document ).ready( function() { |
| 265 |
_.extend( wp.media.gallery.defaults, { |
| 266 |
type: '<?php echo esc_attr( $default_gallery_type ); ?>' |
| 267 |
} ); |
| 268 |
|
| 269 |
// join default gallery settings template with yours -- store in list |
| 270 |
if ( !wp.media.gallery.templates ) wp.media.gallery.templates = [ 'gallery-settings' ]; |
| 271 |
wp.media.gallery.templates.push( 'pk-gallery-settings' ); |
| 272 |
|
| 273 |
// loop through list -- allowing for other templates/settings |
| 274 |
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend( { |
| 275 |
template: function( view ) { |
| 276 |
var output = ''; |
| 277 |
for ( var i in wp.media.gallery.templates ) { |
| 278 |
output += wp.media.template( wp.media.gallery.templates[ i ] )( view ); |
| 279 |
} |
| 280 |
return output; |
| 281 |
}, |
| 282 |
render: function() { |
| 283 |
var $el = this.$el; |
| 284 |
|
| 285 |
wp.media.view.Settings.prototype.render.apply( this, arguments ); |
| 286 |
|
| 287 |
// Hide the Columns setting for all types except Default |
| 288 |
$el.find( 'select[name=type]' ).on( 'change', function () { |
| 289 |
var columnSetting = $el.find( 'select[name=columns]' ).closest( 'label.setting' ); |
| 290 |
|
| 291 |
if ( 'default' === jQuery( this ).val() || 'thumbnails' === jQuery( this ).val() ) { |
| 292 |
columnSetting.show(); |
| 293 |
} else { |
| 294 |
columnSetting.hide(); |
| 295 |
} |
| 296 |
} ).change(); |
| 297 |
|
| 298 |
return this; |
| 299 |
} |
| 300 |
} ); |
| 301 |
|
| 302 |
} ); |
| 303 |
</script> |
| 304 |
<?php |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
new Powerkit_Gallery(); |
| 309 |
} |
| 310 |
|