-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-output.php
299 lines (249 loc) · 7.95 KB
/
class-output.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Class to output all files and HTML markup required for Slick Slider on frontend.
*/
class Slick_Slider_Output {
/**
* Number of Slick Sliders on same WordPress page.
*
* @since 0.1
*
* @var integer
*/
private static $slick_instance = 0;
/**
* Initiate registering of assets and replacing default WordPress gallery HTML with Slick Slider markup.
*
* @since 0.1
*
* @return If PHP version is too low.
*/
public static function init_slider() {
if ( ! Slick_Slider_Main::is_min_php( SLICK_SLIDER_MIN_PHP ) ) {
return;
}
add_action(
'wp_enqueue_scripts',
array(
__CLASS__,
'register_slick_assets'
)
);
add_filter(
'post_gallery',
array(
__CLASS__,
'slick_markup'
),
10,
3
);
}
/**
* Register assets (JS and CSS files).
* Also initiate Slick Slider and add helper CSS
* which moves Slick Slider arrows inside the gallery because it is invisible on white backgrounds.
* To prevent output use
* add_filter( 'slick_slider_init', '__return_false' ); and
* add_filter( 'slick_slider_helper_css', '__return_false' ); respectively.
*
* @since 0.1
*/
public static function register_slick_assets() {
$asset_suffix = Slick_Slider_Main::get_asset_suffix();
wp_register_script(
'slick-slider-core',
Slick_Slider_Main::plugin_url( "bower_components/slick-carousel/slick/slick{$asset_suffix}.js" ),
array( 'jquery' ),
'1.8.0',
true
);
wp_register_style(
'slick-slider-core',
Slick_Slider_Main::plugin_url( 'bower_components/slick-carousel/slick/slick.css' ),
array(),
'1.8.0'
);
wp_register_style(
'slick-slider-core-theme',
Slick_Slider_Main::plugin_url( 'bower_components/slick-carousel/slick/slick-theme.css' ),
array( 'slick-slider-core' ),
'1.8.0'
);
if ( apply_filters( 'slick_slider_init_slider', true ) ) {
wp_add_inline_script( 'slick-slider-core', file_get_contents( SLICK_SLIDER_DIR . '/js/slick-slider-init.js' ) );
}
if ( apply_filters( 'slick_slider_load_helper_css', true ) ) {
wp_add_inline_style( 'slick-slider-core-theme', file_get_contents( SLICK_SLIDER_DIR . "/css/slick-slider-helper{$asset_suffix}.css" ) );
}
if ( class_exists( 'WP_Featherlight' ) ) {
wp_register_script(
'slick-slider-featherlight-helper',
Slick_Slider_Main::plugin_url( "js/slick-slider-featherlight-helper{$asset_suffix}.js" ),
array( 'wp-featherlight' ),
Slick_Slider_Main::get_plugin_data( 'Version' ),
true
);
}
}
/**
* Enqueue required assets, build HTML markup for Slick Slider and return it.
*
* @since 0.1
*
* @param string $output HTML markup.
* @param array $atts Attributes from the gallery shortcode.
* @param integer $instance Unique numeric ID of this gallery shortcode instance.
* @return string Complete Slick Slider markup which can be modified by multiple filters.
*/
public static function slick_markup( $output = '', $atts, $instance ) {
if ( isset( $atts['slick_active'] ) && 'true' === $atts['slick_active'] ) {
global $post;
self::$slick_instance++;
$atts = wp_parse_args( $atts, array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'link' => '',
'slick_instance' => self::$slick_instance,
) );
$atts = apply_filters( 'shortcode_atts_gallery', $atts, [], 'gallery' );
$id = intval( $atts['id'] );
if ( ! empty( $atts['include'] ) ) {
$_attachments = get_posts(
array(
'include' => $atts['include'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby']
)
);
$attachments = [];
foreach ( $_attachments as $key => $val ) {
$attachments[ $val->ID ] = $_attachments[ $key ];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children(
array(
'post_parent' => $id,
'exclude' => $atts['exclude'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby']
)
);
} else {
$attachments = get_children(
array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby']
)
);
}
if ( empty( $attachments ) ) {
return '';
}
wp_enqueue_script( 'slick-slider-featherlight-helper' );
wp_enqueue_script( 'slick-slider-core' );
wp_enqueue_style( 'slick-slider-core-theme' );
$options = Slick_Slider_Options::prepare_options_for_output( $atts );
if ( apply_filters( 'slick_slider_show_caption', false ) ) {
/* translators: Replacement string ("Use %3$s instead."), see https://developer.wordpress.org/reference/functions/_deprecated_function/ */
_deprecated_function( 'add_filter( \'slick_slider_show_caption\' )', 0.5, __( 'the new option “Show caption”', 'slick-slider' ) );
$options['showCaption'] = true;
}
add_filter(
'wp_get_attachment_image_attributes',
array(
'Slick_Slider_Main',
'switch_attachment_attr'
),
10,
3
);
do_action( 'slick_slider_before_slider', $atts, $post->ID, self::$slick_instance );
$output = [];
$output[] = '<div class="slick-slider-wrapper">';
$output[] = sprintf(
'<div class="slick-slider slick-slider--size-%s" id="slick-slider-%s" %s>',
sanitize_html_class( $atts['size'] ),
$atts['slick_instance'],
! empty( $options )
? sprintf(
"data-slick='%s'",
json_encode( $options )
)
: ''
);
foreach ( $attachments as $id => $attachment ) {
do_action( 'slick_slider_before_slide', $id, $post->ID, self::$slick_instance );
$slide = [];
$slide[] = sprintf( '<div class="slide" data-attachment-id="%s">', $id );
$slide[] = '<div class="slide__inner">';
$image_tag = wp_get_attachment_image( $id, $atts['size'] );
if ( class_exists( 'WPGalleryCustomLinks' ) && $link = get_post_meta( $id, '_gallery_link_url', true ) ) {
$slide[] = sprintf(
'<a href="%s" target="%s">%s</a>',
esc_html( apply_filters( 'wpgcl_filter_raw_gallery_link_url', $link, $id, $post->ID ) ),
esc_html( get_post_meta( $id, '_gallery_link_target', true ) ),
$image_tag
);
} elseif ( 'none' !== $atts['link'] ) {
$slide[] = wp_get_attachment_link(
$id,
$atts['size'],
'file' === $atts['link'] ? false : true,
false,
$image_tag
);
} else {
$slide[] = $image_tag;
}
if ( isset( $options['showCaption'] ) && $options['showCaption'] ) {
$meta = wp_prepare_attachment_for_js( $id );
$caption_text = ! empty( $meta['caption'] )
? $meta['caption']
: ( ! empty( $meta['title'] )
? $meta['title']
: $meta['alt'] );
if ( ! empty( $caption_text ) ) {
$caption = [];
$caption[] = '<div class="slide__caption">';
$caption[] = apply_filters( 'slick_slider_caption_html', $caption_text, $id, $post->ID, self::$slick_instance );
$caption[] = '</div>';
$slide[] = implode( "\n", $caption );
}
}
$slide[] = '</div>';
$slide[] = '</div>';
$output[] = apply_filters( 'slick_slider_slide_html', implode( "\n", $slide ), $id, $post->ID, self::$slick_instance );
do_action( 'slick_slider_after_slide', $id, $post->ID, self::$slick_instance );
}
$output[] = '</div>';
$output[] = '</div>';
$output = implode( "\n", $output );
do_action( 'slick_slider_after_slider', $atts, $post->ID, self::$slick_instance );
remove_filter(
'wp_get_attachment_image_attributes',
array(
'Slick_Slider_Main',
'switch_attachment_attr'
),
10
);
return apply_filters( 'slick_slider_html', $output, $post->ID, self::$slick_instance );
}
}
}