|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once( AMP__ROOT__ . '/includes/embeds/class-amp-base-embed-handler.php' ); |
|
4
|
|
|
|
|
5
|
|
|
class AMP_Gallery_Embed_Handler extends AMP_Base_Embed_Handler { |
|
6
|
|
|
private static $script_slug = 'amp-carousel'; |
|
7
|
|
|
private static $script_src = 'https://cdn.ampproject.org/v0/amp-carousel-0.1.js'; |
|
8
|
|
|
|
|
9
|
|
|
public function register_embed() { |
|
10
|
|
|
add_shortcode( 'gallery', array( $this, 'shortcode' ) ); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function unregister_embed() { |
|
14
|
|
|
remove_shortcode( 'gallery' ); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function get_scripts() { |
|
18
|
|
|
if ( ! $this->did_convert_elements ) { |
|
19
|
|
|
return array(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return array( self::$script_slug => self::$script_src ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function shortcode( $attr ) { |
|
26
|
|
|
$post = get_post(); |
|
27
|
|
|
|
|
28
|
|
|
if ( ! empty( $attr['ids'] ) ) { |
|
29
|
|
|
// 'ids' is explicitly ordered, unless you specify otherwise. |
|
30
|
|
|
if ( empty( $attr['orderby'] ) ) { |
|
31
|
|
|
$attr['orderby'] = 'post__in'; |
|
32
|
|
|
} |
|
33
|
|
|
$attr['include'] = $attr['ids']; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$atts = shortcode_atts( array( |
|
37
|
|
|
'order' => 'ASC', |
|
38
|
|
|
'orderby' => 'menu_order ID', |
|
39
|
|
|
'id' => $post ? $post->ID : 0, |
|
40
|
|
|
'include' => '', |
|
41
|
|
|
'exclude' => '', |
|
42
|
|
|
'size' => array( $this->args['width'], $this->args['height'] ) |
|
43
|
|
|
), $attr, 'gallery' ); |
|
44
|
|
|
|
|
45
|
|
|
$id = intval( $atts['id'] ); |
|
46
|
|
|
|
|
47
|
|
|
if ( ! empty( $atts['include'] ) ) { |
|
48
|
|
|
$attachments = get_posts( array( |
|
49
|
|
|
'include' => $atts['include'], |
|
50
|
|
|
'post_status' => 'inherit', |
|
51
|
|
|
'post_type' => 'attachment', |
|
52
|
|
|
'post_mime_type' => 'image', |
|
53
|
|
|
'order' => $atts['order'], |
|
54
|
|
|
'orderby' => $atts['orderby'], |
|
55
|
|
|
'fields' => 'ids', |
|
56
|
|
|
) ); |
|
57
|
|
|
} elseif ( ! empty( $atts['exclude'] ) ) { |
|
58
|
|
|
$attachments = get_children( array( |
|
59
|
|
|
'post_parent' => $id, |
|
60
|
|
|
'exclude' => $atts['exclude'], |
|
61
|
|
|
'post_status' => 'inherit', |
|
62
|
|
|
'post_type' => 'attachment', |
|
63
|
|
|
'post_mime_type' => 'image', |
|
64
|
|
|
'order' => $atts['order'], |
|
65
|
|
|
'orderby' => $atts['orderby'], |
|
66
|
|
|
'fields' => 'ids', |
|
67
|
|
|
) ); |
|
68
|
|
|
} else { |
|
69
|
|
|
$attachments = get_children( array( |
|
70
|
|
|
'post_parent' => $id, |
|
71
|
|
|
'post_status' => 'inherit', |
|
72
|
|
|
'post_type' => 'attachment', |
|
73
|
|
|
'post_mime_type' => 'image', |
|
74
|
|
|
'order' => $atts['order'], |
|
75
|
|
|
'orderby' => $atts['orderby'], |
|
76
|
|
|
'fields' => 'ids', |
|
77
|
|
|
) ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ( empty( $attachments ) ) { |
|
81
|
|
|
return ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$urls = array(); |
|
85
|
|
|
foreach ( $attachments as $attachment_id ) { |
|
86
|
|
|
list( $url, $width, $height ) = wp_get_attachment_image_src( $attachment_id, $atts['size'], true ); |
|
87
|
|
|
|
|
88
|
|
|
if ( ! $url ) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$urls[] = array( |
|
93
|
|
|
'url' => $url, |
|
94
|
|
|
'width' => $width, |
|
95
|
|
|
'height' => $height, |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->render( array( |
|
100
|
|
|
'images' => $urls, |
|
101
|
|
|
) ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function render( $args ) { |
|
105
|
|
|
$this->did_convert_elements = true; |
|
106
|
|
|
|
|
107
|
|
|
$args = wp_parse_args( $args, array( |
|
108
|
|
|
'images' => false, |
|
109
|
|
|
) ); |
|
110
|
|
|
|
|
111
|
|
|
if ( empty( $args['images'] ) ) { |
|
112
|
|
|
return ''; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$images = array(); |
|
116
|
|
|
foreach ( $args['images'] as $image ) { |
|
117
|
|
|
$images[] = AMP_HTML_Utils::build_tag( |
|
118
|
|
|
'amp-img', |
|
119
|
|
|
array( |
|
120
|
|
|
'src' => $image['url'], |
|
121
|
|
|
'width' => $image['width'], |
|
122
|
|
|
'height' => $image['height'], |
|
123
|
|
|
'layout' => 'responsive', |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return AMP_HTML_Utils::build_tag( |
|
129
|
|
|
'amp-carousel', |
|
130
|
|
|
array( |
|
131
|
|
|
'width' => $this->args['width'], |
|
132
|
|
|
'height' => $this->args['height'], |
|
133
|
|
|
'type' => 'slides', |
|
134
|
|
|
'layout' => 'responsive', |
|
135
|
|
|
), |
|
136
|
|
|
implode( PHP_EOL, $images ) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|