PluginProbe
Powerkit – Supercharge your WordPress Site / 2.3.2
Powerkit – Supercharge your WordPress Site v2.3.2
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / extensions / gallery / class-powerkit-gallery.php

class-powerkit-gallery.php in Powerkit – Supercharge your WordPress Site 2.3.2, at extensions/gallery/class-powerkit-gallery.php

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