/
class-amp-gallery-embed.php
176 lines (155 loc) · 3.77 KB
/
class-amp-gallery-embed.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
<?php
/**
* Class AMP_Gallery_Embed_Handler
*
* @package AMP
*/
/**
* Class AMP_Gallery_Embed_Handler
*
* @since 0.2
*/
class AMP_Gallery_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* Register embed.
*/
public function register_embed() {
add_shortcode( 'gallery', array( $this, 'shortcode' ) );
}
/**
* Unregister embed.
*/
public function unregister_embed() {
remove_shortcode( 'gallery' );
}
/**
* Shortcode handler.
*
* @param array $attr Shortcode attributes.
* @return string Rendered gallery.
*/
public function shortcode( $attr ) {
$post = get_post();
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) ) {
$attr['orderby'] = 'post__in';
}
$attr['include'] = $attr['ids'];
}
$atts = shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'include' => '',
'exclude' => '',
'size' => array( $this->args['width'], $this->args['height'] ),
'link' => 'none',
), $attr, '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'],
'fields' => 'ids',
) );
} 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'],
'fields' => 'ids',
) );
} else {
$attachments = get_children( array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'fields' => 'ids',
) );
}
if ( empty( $attachments ) ) {
return '';
}
$urls = array();
foreach ( $attachments as $attachment_id ) {
list( $url, $width, $height ) = wp_get_attachment_image_src( $attachment_id, $atts['size'], true );
if ( ! $url ) {
continue;
}
$href = null;
if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
$href = $url;
} elseif ( ! empty( $atts['link'] ) && 'post' === $atts['link'] ) {
$href = get_attachment_link( $attachment_id );
}
$urls[] = array(
'href' => $href,
'url' => $url,
'width' => $width,
'height' => $height,
);
}
return $this->render( array(
'images' => $urls,
) );
}
/**
* Render.
*
* @param array $args Args.
* @return string Rendered.
*/
public function render( $args ) {
$this->did_convert_elements = true;
$args = wp_parse_args( $args, array(
'images' => false,
) );
if ( empty( $args['images'] ) ) {
return '';
}
$images = array();
foreach ( $args['images'] as $props ) {
$image = AMP_HTML_Utils::build_tag(
'amp-img',
array(
'src' => $props['url'],
'width' => $props['width'],
'height' => $props['height'],
'layout' => 'responsive',
)
);
if ( ! empty( $props['href'] ) ) {
$image = AMP_HTML_Utils::build_tag(
'a',
array(
'href' => $props['href'],
),
$image
);
}
$images[] = $image;
}
return AMP_HTML_Utils::build_tag(
'amp-carousel',
array(
'width' => $this->args['width'],
'height' => $this->args['height'],
'type' => 'slides',
'layout' => 'responsive',
),
implode( PHP_EOL, $images )
);
}
}