AMP for WordPress - Version 0.2

Version Description

(Jan 28, 2016) =

  • Lots and lots and lots of compatibility and validation fixes
  • Lots and lots and lots of improvements for customization
Download this release

Release Info

Developer batmoo
Plugin Icon 128x128 AMP for WordPress
Version 0.2
Comparing to
See all releases

Version 0.2

LICENSE ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Lesser General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ {description}
294
+ Copyright (C) {year} {fullname}
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License along
307
+ with this program; if not, write to the Free Software Foundation, Inc.,
308
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
+
310
+ Also add information on how to contact you by electronic and paper mail.
311
+
312
+ If the program is interactive, make it output a short notice like this
313
+ when it starts in an interactive mode:
314
+
315
+ Gnomovision version 69, Copyright (C) year name of author
316
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
+ This is free software, and you are welcome to redistribute it
318
+ under certain conditions; type `show c' for details.
319
+
320
+ The hypothetical commands `show w' and `show c' should show the appropriate
321
+ parts of the General Public License. Of course, the commands you use may
322
+ be called something other than `show w' and `show c'; they could even be
323
+ mouse-clicks or menu items--whatever suits your program.
324
+
325
+ You should also get your employer (if you work as a programmer) or your
326
+ school, if any, to sign a "copyright disclaimer" for the program, if
327
+ necessary. Here is a sample; alter the names:
328
+
329
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
+
332
+ {signature of Ty Coon}, 1 April 1989
333
+ Ty Coon, President of Vice
334
+
335
+ This General Public License does not permit incorporating your program into
336
+ proprietary programs. If your program is a subroutine library, you may
337
+ consider it more useful to permit linking proprietary applications with the
338
+ library. If this is what you want to do, use the GNU Lesser General
339
+ Public License instead of this License.
340
+
amp.php ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Plugin Name: AMP
4
+ * Description: Add AMP support to your WordPress site.
5
+ * Plugin URI: https://github.com/automattic/amp-wp
6
+ * Author: Automattic
7
+ * Author URI: https://automattic.com
8
+ * Version: 0.2
9
+ * Text Domain: amp
10
+ * Domain Path: /languages/
11
+ * License: GPLv2 or later
12
+ */
13
+
14
+ define( 'AMP_QUERY_VAR', 'amp' );
15
+ if ( ! defined( 'AMP_DEV_MODE' ) ) {
16
+ define( 'AMP_DEV_MODE', defined( 'WP_DEBUG' ) && WP_DEBUG );
17
+ }
18
+
19
+ define( 'AMP__FILE__', __FILE__ );
20
+ define( 'AMP__DIR__', dirname( __FILE__ ) );
21
+
22
+ require_once( AMP__DIR__ . '/includes/amp-helper-functions.php' );
23
+
24
+ register_activation_hook( __FILE__, 'amp_activate' );
25
+ function amp_activate(){
26
+ amp_init();
27
+ flush_rewrite_rules();
28
+ }
29
+
30
+ register_deactivation_hook( __FILE__, 'amp_deactivate' );
31
+ function amp_deactivate(){
32
+ flush_rewrite_rules();
33
+ }
34
+
35
+ add_action( 'init', 'amp_init' );
36
+ function amp_init() {
37
+ if ( false === apply_filters( 'amp_is_enabled', true ) ) {
38
+ return;
39
+ }
40
+
41
+ load_plugin_textdomain( 'amp', false, plugin_basename( AMP__DIR__ ) . '/languages' );
42
+
43
+ add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );
44
+ add_post_type_support( 'post', AMP_QUERY_VAR );
45
+
46
+ add_action( 'wp', 'amp_maybe_add_actions' );
47
+
48
+ if ( class_exists( 'Jetpack' ) ) {
49
+ require_once( AMP__DIR__ . '/jetpack-helper.php' );
50
+ }
51
+ }
52
+
53
+ function amp_maybe_add_actions() {
54
+ if ( ! is_singular() ) {
55
+ return;
56
+ }
57
+
58
+ $is_amp_endpoint = is_amp_endpoint();
59
+
60
+ $post = get_queried_object();
61
+ $supports = post_supports_amp( $post );
62
+
63
+ if ( ! $supports ) {
64
+ if ( $is_amp_endpoint ) {
65
+ wp_safe_redirect( get_permalink( $post->ID ) );
66
+ exit;
67
+ }
68
+ return;
69
+ }
70
+
71
+ if ( $is_amp_endpoint ) {
72
+ amp_prepare_render();
73
+ } else {
74
+ amp_add_frontend_actions();
75
+ }
76
+ }
77
+
78
+ function amp_load_classes() {
79
+ require_once( AMP__DIR__ . '/includes/class-amp-post-template.php' ); // this loads everything else
80
+ }
81
+
82
+ function amp_add_frontend_actions() {
83
+ require_once( AMP__DIR__ . '/includes/amp-frontend-actions.php' );
84
+ }
85
+
86
+ function amp_add_post_template_actions() {
87
+ require_once( AMP__DIR__ . '/includes/amp-post-template-actions.php' );
88
+ }
89
+
90
+ function amp_prepare_render() {
91
+ add_action( 'template_redirect', 'amp_render' );
92
+ }
93
+
94
+ function amp_render() {
95
+ amp_load_classes();
96
+
97
+ $post_id = get_queried_object_id();
98
+ do_action( 'pre_amp_render_post', $post_id );
99
+
100
+ amp_add_post_template_actions();
101
+ $template = new AMP_Post_Template( $post_id );
102
+ $template->load();
103
+ exit;
104
+ }
assets/images/placeholder-icon.png ADDED
Binary file
includes/amp-frontend-actions.php ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Callbacks for adding AMP-related things to the main theme
3
+
4
+ add_action( 'wp_head', 'amp_frontend_add_canonical' );
5
+
6
+ function amp_frontend_add_canonical() {
7
+ if ( false === apply_filters( 'amp_frontend_show_canonical', true ) ) {
8
+ return;
9
+ }
10
+
11
+ $amp_url = amp_get_permalink( get_queried_object_id() );
12
+ printf( '<link rel="amphtml" href="%s" />', esc_url( $amp_url ) );
13
+ }
includes/amp-helper-functions.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ function amp_get_permalink( $post_id ) {
4
+ if ( '' != get_option( 'permalink_structure' ) ) {
5
+ $amp_url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( AMP_QUERY_VAR, 'single_amp' );
6
+ } else {
7
+ $amp_url = add_query_arg( AMP_QUERY_VAR, absint( $post_id ), home_url() );
8
+ }
9
+
10
+ return apply_filters( 'amp_get_permalink', $amp_url, $post_id );
11
+ }
12
+
13
+ function post_supports_amp( $post ) {
14
+ // Because `add_rewrite_endpoint` doesn't let us target specific post_types :(
15
+ if ( ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) {
16
+ return false;
17
+ }
18
+
19
+ if ( true === apply_filters( 'amp_skip_post', false, $post->ID ) ) {
20
+ return false;
21
+ }
22
+
23
+ return true;
24
+ }
25
+
26
+ /**
27
+ * Are we currently on an AMP URL?
28
+ *
29
+ * Note: will always return `false` if called before the `parse_query` hook.
30
+ */
31
+ function is_amp_endpoint() {
32
+ return false !== get_query_var( AMP_QUERY_VAR, false );
33
+ }
34
+
35
+ function amp_get_asset_url( $file ) {
36
+ return plugins_url( sprintf( 'assets/%s', $file ), AMP__FILE__ );
37
+ }
includes/amp-post-template-actions.php ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Callbacks for adding content to an AMP template
3
+
4
+ add_action( 'amp_post_template_head', 'amp_post_template_add_title' );
5
+ function amp_post_template_add_title( $amp_template ) {
6
+ ?>
7
+ <title><?php echo esc_html( $amp_template->get( 'document_title' ) ); ?></title>
8
+ <?php
9
+ }
10
+
11
+ add_action( 'amp_post_template_head', 'amp_post_template_add_canonical' );
12
+ function amp_post_template_add_canonical( $amp_template ) {
13
+ ?>
14
+ <link rel="canonical" href="<?php echo esc_url( $amp_template->get( 'canonical_url' ) ); ?>" />
15
+ <?php
16
+ }
17
+
18
+ add_action( 'amp_post_template_head', 'amp_post_template_add_scripts' );
19
+ function amp_post_template_add_scripts( $amp_template ) {
20
+ $scripts = $amp_template->get( 'amp_component_scripts', array() );
21
+ foreach ( $scripts as $element => $script ) : ?>
22
+ <script custom-element="<?php echo esc_attr( $element ); ?>" src="<?php echo esc_url( $script ); ?>" async></script>
23
+ <?php endforeach; ?>
24
+ <script src="<?php echo esc_url( $amp_template->get( 'amp_runtime_script' ) ); ?>" async></script>
25
+ <?php
26
+ }
27
+
28
+ add_action( 'amp_post_template_head', 'amp_post_template_add_schemaorg_metadata' );
29
+ function amp_post_template_add_schemaorg_metadata( $amp_template ) {
30
+ ?>
31
+ <script type="application/ld+json"><?php echo json_encode( $amp_template->get( 'metadata' ) ); ?></script>
32
+ <?php
33
+ }
includes/class-amp-content.php ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/utils/class-amp-dom-utils.php' );
4
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
5
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );
6
+
7
+ class AMP_Content {
8
+ private $content;
9
+ private $amp_content = '';
10
+ private $amp_scripts = array();
11
+ private $args = array();
12
+ private $embed_handler_classes = array();
13
+ private $sanitizer_classes = array();
14
+
15
+ public function __construct( $content, $embed_handler_classes, $sanitizer_classes, $args = array() ) {
16
+ $this->content = $content;
17
+ $this->args = $args;
18
+ $this->embed_handler_classes = $embed_handler_classes;
19
+ $this->sanitizer_classes = $sanitizer_classes;
20
+
21
+ $this->transform();
22
+ }
23
+
24
+ public function get_amp_content() {
25
+ return $this->amp_content;
26
+ }
27
+
28
+ public function get_amp_scripts() {
29
+ return $this->amp_scripts;
30
+ }
31
+
32
+ private function transform() {
33
+ $content = $this->content;
34
+
35
+ // First, embeds + the_content filter
36
+ $embed_handlers = $this->register_embed_handlers();
37
+ $content = apply_filters( 'the_content', $content );
38
+ $this->unregister_embed_handlers( $embed_handlers );
39
+
40
+ // Then, sanitize to strip and/or convert non-amp content
41
+ $content = $this->sanitize( $content );
42
+
43
+ $this->amp_content = $content;
44
+ }
45
+
46
+ private function add_scripts( $scripts ) {
47
+ $this->amp_scripts = array_merge( $this->amp_scripts, $scripts );
48
+ }
49
+
50
+ private function register_embed_handlers() {
51
+ $embed_handlers = array();
52
+
53
+ foreach ( $this->embed_handler_classes as $embed_handler_class => $args ) {
54
+ $embed_handler = new $embed_handler_class( array_merge( $this->args, $args ) );
55
+
56
+ if ( ! is_subclass_of( $embed_handler, 'AMP_Base_Embed_Handler' ) ) {
57
+ _doing_it_wrong( __METHOD__, sprintf( __( 'Embed Handler (%s) must extend `AMP_Embed_Handler`', 'amp' ), $embed_handler_class ), '0.1' );
58
+ continue;
59
+ }
60
+
61
+ $embed_handler->register_embed();
62
+ $embed_handlers[] = $embed_handler;
63
+ }
64
+
65
+ return $embed_handlers;
66
+ }
67
+
68
+ private function unregister_embed_handlers( $embed_handlers ) {
69
+ foreach ( $embed_handlers as $embed_handler ) {
70
+ $this->add_scripts( $embed_handler->get_scripts() );
71
+ $embed_handler->unregister_embed();
72
+ }
73
+ }
74
+
75
+ private function sanitize( $content ) {
76
+ $dom = AMP_DOM_Utils::get_dom_from_content( $content );
77
+
78
+ foreach ( $this->sanitizer_classes as $sanitizer_class => $args ) {
79
+ $sanitizer = new $sanitizer_class( $dom, array_merge( $this->args, $args ) );
80
+
81
+ if ( ! is_subclass_of( $sanitizer, 'AMP_Base_Sanitizer' ) ) {
82
+ _doing_it_wrong( __METHOD__, sprintf( __( 'Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp' ), esc_html( $sanitizer_class ) ), '0.1' );
83
+ continue;
84
+ }
85
+
86
+ $sanitizer->sanitize();
87
+ $this->add_scripts( $sanitizer->get_scripts() );
88
+ }
89
+
90
+ return AMP_DOM_Utils::get_content_from_dom( $dom );
91
+ }
92
+ }
includes/class-amp-post-template.php ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/class-amp-content.php' );
4
+
5
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-blacklist-sanitizer.php' );
6
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-img-sanitizer.php' );
7
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-video-sanitizer.php' );
8
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-iframe-sanitizer.php' );
9
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-audio-sanitizer.php' );
10
+
11
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php' );
12
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php' );
13
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php' );
14
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php' );
15
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php' );
16
+
17
+ class AMP_Post_Template {
18
+ const SITE_ICON_SIZE = 32;
19
+
20
+ private $template_dir;
21
+ private $data;
22
+
23
+ public function __construct( $post_id ) {
24
+ $this->template_dir = AMP__DIR__ . '/templates';
25
+
26
+ $this->ID = $post_id;
27
+ $this->post = get_post( $post_id );
28
+
29
+ $content_max_width = isset( $GLOBALS['content_width'] ) ? absint( $GLOBALS['content_width'] ) : 600;
30
+ $content_max_width = apply_filters( 'amp_content_max_width', $content_max_width );
31
+
32
+ $this->data = array(
33
+ 'content_max_width' => $content_max_width,
34
+
35
+ 'document_title' => function_exists( 'wp_get_document_title' ) ? wp_get_document_title() : wp_title( '', false ), // back-compat with 4.3
36
+ 'canonical_url' => get_permalink( $post_id ),
37
+ 'home_url' => home_url(),
38
+ 'blog_name' => get_bloginfo( 'name' ),
39
+
40
+ 'site_icon_url' => get_site_icon_url( self::SITE_ICON_SIZE ),
41
+ 'placeholder_image_url' => amp_get_asset_url( 'images/placeholder-icon.png' ),
42
+
43
+ 'amp_runtime_script' => 'https://cdn.ampproject.org/v0.js',
44
+ 'amp_component_scripts' => array(),
45
+ );
46
+
47
+ $this->build_post_content();
48
+ $this->build_post_data();
49
+
50
+ $this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
51
+ }
52
+
53
+ public function get( $property, $default = null ) {
54
+ if ( isset( $this->data[ $property ] ) ) {
55
+ return $this->data[ $property ];
56
+ } else {
57
+ _doing_it_wrong( __METHOD__, sprintf( __( 'Called for non-existant key ("%s").', 'amp' ), esc_html( $property ) ), '0.1' );
58
+ }
59
+
60
+ return $default;
61
+ }
62
+
63
+ public function load() {
64
+ $this->load_parts( array( 'single' ) );
65
+ }
66
+
67
+ public function load_parts( $templates ) {
68
+ foreach ( $templates as $template ) {
69
+ $file = $this->get_template_path( $template );
70
+ $this->verify_and_include( $file, $template );
71
+ }
72
+ }
73
+
74
+ private function get_template_path( $template ) {
75
+ return sprintf( '%s/%s.php', $this->template_dir, $template );
76
+ }
77
+
78
+ private function add_data( $data ) {
79
+ $this->data = array_merge( $this->data, $data );
80
+ }
81
+
82
+ private function add_data_by_key( $key, $value ) {
83
+ $this->data[ $key ] = $value;
84
+ }
85
+
86
+ private function merge_data_for_key( $key, $value ) {
87
+ if ( is_array( $this->data[ $key ] ) ) {
88
+ $this->data[ $key ] = array_merge( $this->data[ $key ], $value );
89
+ } else {
90
+ $this->add_data_by_key( $key, $value );
91
+ }
92
+ }
93
+
94
+ private function build_post_data() {
95
+ $post_title = get_the_title( $this->ID );
96
+ $post_publish_timestamp = get_the_date( 'U', $this->ID );
97
+ $post_modified_timestamp = get_post_modified_time( 'U', false, $this->post );
98
+ $post_author = get_userdata( $this->post->post_author );
99
+
100
+ $this->add_data( array(
101
+ 'post' => $this->post,
102
+ 'post_id' => $this->ID,
103
+ 'post_title' => $post_title,
104
+ 'post_publish_timestamp' => $post_publish_timestamp,
105
+ 'post_modified_timestamp' => $post_modified_timestamp,
106
+ 'post_author' => $post_author,
107
+ ) );
108
+
109
+ $metadata = array(
110
+ '@context' => 'http://schema.org',
111
+ '@type' => 'BlogPosting',
112
+ 'mainEntityOfPage' => $this->get( 'canonical_url' ),
113
+ 'publisher' => array(
114
+ '@type' => 'Organization',
115
+ 'name' => $this->get( 'blog_name' ),
116
+ ),
117
+ 'headline' => $post_title,
118
+ 'datePublished' => date( 'c', $post_publish_timestamp ),
119
+ 'dateModified' => date( 'c', $post_modified_timestamp ),
120
+ 'author' => array(
121
+ '@type' => 'Person',
122
+ 'name' => $post_author->display_name,
123
+ ),
124
+ 'image' => $this->get_post_image_metadata(),
125
+ );
126
+
127
+ $site_icon_url = $this->get( 'site_icon_url' );
128
+ if ( $site_icon_url ) {
129
+ $metadata['publisher']['logo'] = array(
130
+ '@type' => 'ImageObject',
131
+ 'url' => $site_icon_url,
132
+ 'height' => self::SITE_ICON_SIZE,
133
+ 'width' => self::SITE_ICON_SIZE,
134
+ );
135
+ }
136
+
137
+ $this->add_data_by_key( 'metadata', $metadata );
138
+ }
139
+
140
+ private function build_post_content() {
141
+ $amp_content = new AMP_Content( $this->post->post_content,
142
+ apply_filters( 'amp_content_embed_handlers', array(
143
+ 'AMP_Twitter_Embed_Handler' => array(),
144
+ 'AMP_YouTube_Embed_Handler' => array(),
145
+ 'AMP_Instagram_Embed_Handler' => array(),
146
+ 'AMP_Vine_Embed_Handler' => array(),
147
+ 'AMP_Gallery_Embed_Handler' => array(),
148
+ ), $this->post ),
149
+ apply_filters( 'amp_content_sanitizers', array(
150
+ 'AMP_Blacklist_Sanitizer' => array(),
151
+ 'AMP_Img_Sanitizer' => array(),
152
+ 'AMP_Video_Sanitizer' => array(),
153
+ 'AMP_Audio_Sanitizer' => array(),
154
+ 'AMP_Iframe_Sanitizer' => array(
155
+ 'add_placeholder' => true,
156
+ ),
157
+ ), $this->post ),
158
+ array(
159
+ 'content_max_width' => $this->get( 'content_max_width' ),
160
+ )
161
+ );
162
+
163
+ $this->add_data_by_key( 'post_amp_content', $amp_content->get_amp_content() );
164
+ $this->merge_data_for_key( 'amp_component_scripts', $amp_content->get_amp_scripts() );
165
+ }
166
+
167
+ /**
168
+ * Grabs featured image or the first attached image for the post
169
+ *
170
+ * TODO: move to a utils class?
171
+ */
172
+ private function get_post_image_metadata() {
173
+ $post_image_meta = null;
174
+ $post_image_id = false;
175
+
176
+ if ( has_post_thumbnail( $this->ID ) ) {
177
+ $post_image_id = get_post_thumbnail_id( $this->ID );
178
+ } else {
179
+ $attached_image_ids = get_posts( array(
180
+ 'post_parent' => $this->ID,
181
+ 'post_type' => 'attachment',
182
+ 'post_mime_type' => 'image',
183
+ 'posts_per_page' => 1,
184
+ 'orderby' => 'menu_order',
185
+ 'order' => 'ASC',
186
+ 'fields' => 'ids',
187
+ 'suppress_filters' => false,
188
+ ) );
189
+
190
+ if ( ! empty( $attached_image_ids ) ) {
191
+ $post_image_id = array_shift( $attached_image_ids );
192
+ }
193
+ }
194
+
195
+ if ( ! $post_image_id ) {
196
+ return false;
197
+ }
198
+
199
+ $post_image_src = wp_get_attachment_image_src( $post_image_id, 'full' );
200
+
201
+ if ( $post_image_src ) {
202
+ $post_image_meta = array(
203
+ '@type' => 'ImageObject',
204
+ 'url' => $post_image_src[0],
205
+ 'width' => $post_image_src[1],
206
+ 'height' => $post_image_src[2],
207
+ );
208
+ }
209
+
210
+ return $post_image_meta;
211
+ }
212
+
213
+ private function verify_and_include( $file, $template_type ) {
214
+ $located_file = $this->locate_template( $file );
215
+ if ( $located_file ) {
216
+ $file = $located_file;
217
+ }
218
+
219
+ $file = apply_filters( 'amp_post_template_file', $file, $template_type, $this->post );
220
+ if ( ! $this->is_valid_template( $file ) ) {
221
+ _doing_it_wrong( __METHOD__, sprintf( __( 'Path validation for template (%s) failed. Path cannot traverse and must be located in `%s`.', 'amp' ), esc_html( $file ), 'WP_CONTENT_DIR' ), '0.1' );
222
+ return;
223
+ }
224
+
225
+ include( $file );
226
+ }
227
+
228
+
229
+ private function locate_template( $file ) {
230
+ $search_file = sprintf( 'amp/%s', basename( $file ) );
231
+ return locate_template( array( $search_file ), false );
232
+ }
233
+
234
+ private function is_valid_template( $template ) {
235
+ if ( 0 !== strpos( $template, WP_CONTENT_DIR ) ) {
236
+ return false;
237
+ }
238
+
239
+ if ( 0 !== validate_file( $template ) ) {
240
+ return false;
241
+ }
242
+
243
+ return true;
244
+ }
245
+ }
includes/embeds/class-amp-base-embed-handler.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Used by some children
4
+ require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php' );
5
+
6
+ abstract class AMP_Base_Embed_Handler {
7
+ protected $DEFAULT_WIDTH = 600;
8
+ protected $DEFAULT_HEIGHT = 480;
9
+
10
+ protected $args = array();
11
+ protected $did_convert_elements = false;
12
+
13
+ abstract function register_embed();
14
+ abstract function unregister_embed();
15
+
16
+ function __construct( $args = array() ) {
17
+ $this->args = wp_parse_args( $args, array(
18
+ 'width' => $this->DEFAULT_WIDTH,
19
+ 'height' => $this->DEFAULT_HEIGHT,
20
+ ) );
21
+ }
22
+
23
+ public function get_scripts() {
24
+ return array();
25
+ }
26
+ }
includes/embeds/class-amp-gallery-embed.php ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/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
+ )
124
+ );
125
+ }
126
+
127
+ return AMP_HTML_Utils::build_tag(
128
+ 'amp-carousel',
129
+ wp_parse_args( array(), $this->args ),
130
+ implode( PHP_EOL, $images )
131
+ );
132
+ }
133
+ }
includes/embeds/class-amp-instagram-embed.php ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
+
5
+ // Much of this class is borrowed from Jetpack embeds
6
+ class AMP_Instagram_Embed_Handler extends AMP_Base_Embed_Handler {
7
+ const SHORT_URL_HOST = 'instagr.am';
8
+ const URL_PATTERN = '#http(s?)://(www\.)?instagr(\.am|am\.com)/p/([^/?]+)#i';
9
+
10
+ protected $DEFAULT_WIDTH = 600;
11
+ protected $DEFAULT_HEIGHT = 600;
12
+
13
+ private static $script_slug = 'amp-instagram';
14
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-instagram-0.1.js';
15
+
16
+ public function register_embed() {
17
+ wp_embed_register_handler( 'amp-instagram', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
18
+ add_shortcode( 'instagram', array( $this, 'shortcode' ) );
19
+ }
20
+
21
+ public function unregister_embed() {
22
+ wp_embed_unregister_handler( 'amp-instagram', -1 );
23
+ remove_shortcode( 'instagram' );
24
+ }
25
+
26
+ public function get_scripts() {
27
+ if ( ! $this->did_convert_elements ) {
28
+ return array();
29
+ }
30
+
31
+ return array( self::$script_slug => self::$script_src );
32
+ }
33
+
34
+ public function shortcode( $attr ) {
35
+ $url = false;
36
+
37
+ $instagram_id = false;
38
+ if ( isset( $attr['url'] ) ) {
39
+ $url = trim( $attr['url'] );
40
+ }
41
+
42
+ if ( empty( $url ) ) {
43
+ return '';
44
+ }
45
+
46
+ $instagram_id = $this->get_instagram_id_from_url( $url );
47
+
48
+ return $this->render( array(
49
+ 'url' => $url,
50
+ 'instagram_id' => $instagram_id,
51
+ ) );
52
+ }
53
+
54
+ public function oembed( $matches, $attr, $url, $rawattr ) {
55
+ return $this->render( array( 'url' => $url, 'instagram_id' => end( $matches ) ) );
56
+ }
57
+
58
+ public function render( $args ) {
59
+ $args = wp_parse_args( $args, array(
60
+ 'url' => false,
61
+ 'instagram_id' => false,
62
+ ) );
63
+
64
+ if ( empty( $args['instagram_id'] ) ) {
65
+ return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
66
+ }
67
+
68
+ $this->did_convert_elements = true;
69
+
70
+ return AMP_HTML_Utils::build_tag(
71
+ 'amp-instagram',
72
+ array(
73
+ 'data-shortcode' => $args['instagram_id'],
74
+ 'layout' => 'responsive',
75
+ 'width' => $this->args['width'],
76
+ 'height' => $this->args['height'],
77
+ )
78
+ );
79
+ }
80
+
81
+ private function get_instagram_id_from_url( $url ) {
82
+ $url_path = parse_url( $url, PHP_URL_PATH );
83
+
84
+ // /p/{id} on both, short url and normal urls
85
+ $instagram_id = mb_substr( $url_path, 3 );
86
+
87
+ if( ! empty( $instagram_id ) ) {
88
+ return $instagram_id;
89
+ }
90
+
91
+ return false;
92
+ }
93
+ }
includes/embeds/class-amp-twitter-embed.php ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
+
5
+ // Much of this class is borrowed from Jetpack embeds
6
+ class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {
7
+ const URL_PATTERN = '#http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)#i';
8
+
9
+ private static $script_slug = 'amp-twitter';
10
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-twitter-0.1.js';
11
+
12
+ public function register_embed() {
13
+ add_shortcode( 'tweet', array( $this, 'shortcode' ) );
14
+ wp_embed_register_handler( 'amp-twitter', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
15
+ }
16
+
17
+ public function unregister_embed() {
18
+ remove_shortcode( 'tweet' );
19
+ wp_embed_unregister_handler( 'amp-twitter', -1 );
20
+ }
21
+
22
+ public function get_scripts() {
23
+ if ( ! $this->did_convert_elements ) {
24
+ return array();
25
+ }
26
+
27
+ return array( self::$script_slug => self::$script_src );
28
+ }
29
+
30
+ function shortcode( $attr ) {
31
+ $attr = wp_parse_args( $attr, array(
32
+ 'tweet' => false,
33
+ ) );
34
+
35
+ $id = false;
36
+ if ( intval( $attr['tweet'] ) ) {
37
+ $id = intval( $attr['tweet'] );
38
+ } else {
39
+ preg_match( self::URL_PATTERN, $attr['tweet'], $matches );
40
+ if ( isset( $matches[5] ) && intval( $matches[5] ) ) {
41
+ $id = intval( $matches[5] );
42
+ }
43
+
44
+ if ( empty( $id ) ) {
45
+ return '';
46
+ }
47
+ }
48
+
49
+ $this->did_convert_elements = true;
50
+
51
+ return AMP_HTML_Utils::build_tag(
52
+ 'amp-twitter',
53
+ array(
54
+ 'data-tweetid' => $id,
55
+ 'layout' => 'responsive',
56
+ 'width' => $this->args['width'],
57
+ 'height' => $this->args['height'],
58
+ )
59
+ );
60
+ }
61
+
62
+ function oembed( $matches, $attr, $url, $rawattr ) {
63
+ $id = false;
64
+
65
+ if ( isset( $matches[5] ) && intval( $matches[5] ) ) {
66
+ $id = intval( $matches[5] );
67
+ }
68
+
69
+ if ( ! $id ) {
70
+ return '';
71
+ }
72
+
73
+ return $this->shortcode( array( 'tweet' => $id ) );
74
+ }
75
+ }
includes/embeds/class-amp-vine-embed.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
+
5
+ class AMP_Vine_Embed_Handler extends AMP_Base_Embed_Handler {
6
+ const URL_PATTERN = '#https?://vine\.co/v/([^/?]+)#i';
7
+
8
+ protected $DEFAULT_WIDTH = 400;
9
+ protected $DEFAULT_HEIGHT = 400;
10
+
11
+ private static $script_slug = 'amp-vine';
12
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-vine-0.1.js';
13
+
14
+ public function register_embed() {
15
+ wp_embed_register_handler( 'amp-vine', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
16
+ }
17
+
18
+ public function unregister_embed() {
19
+ wp_embed_unregister_handler( 'amp-vine', -1 );
20
+ }
21
+
22
+ public function get_scripts() {
23
+ if ( ! $this->did_convert_elements ) {
24
+ return array();
25
+ }
26
+
27
+ return array( self::$script_slug => self::$script_src );
28
+ }
29
+
30
+ public function oembed( $matches, $attr, $url, $rawattr ) {
31
+ return $this->render( array( 'url' => $url, 'vine_id' => end( $matches ) ) );
32
+ }
33
+
34
+ public function render( $args ) {
35
+ $args = wp_parse_args( $args, array(
36
+ 'url' => false,
37
+ 'vine_id' => false,
38
+ ) );
39
+
40
+ if ( empty( $args['vine_id'] ) ) {
41
+ return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
42
+ }
43
+
44
+ $this->did_convert_elements = true;
45
+
46
+ return AMP_HTML_Utils::build_tag(
47
+ 'amp-vine',
48
+ array(
49
+ 'data-vineid' => $args['vine_id'],
50
+ 'layout' => 'responsive',
51
+ 'width' => $this->args['width'],
52
+ 'height' => $this->args['height'],
53
+ )
54
+ );
55
+ }
56
+ }
includes/embeds/class-amp-youtube-embed.php ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
+
5
+ // Much of this class is borrowed from Jetpack embeds
6
+ class AMP_YouTube_Embed_Handler extends AMP_Base_Embed_Handler {
7
+ const SHORT_URL_HOST = 'youtu.be';
8
+ const URL_PATTERN = '#https?://(?:www\.)?(?:youtube.com/(?:v/|e/|embed/|playlist|watch[/\#?])|youtu\.be/).*#i';
9
+
10
+ private static $script_slug = 'amp-youtube';
11
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-youtube-0.1.js';
12
+
13
+ function register_embed() {
14
+ wp_embed_register_handler( 'amp-youtube', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
15
+ add_shortcode( 'youtube', array( $this, 'shortcode' ) );
16
+ }
17
+
18
+ public function unregister_embed() {
19
+ wp_embed_unregister_handler( 'amp-youtube', -1 );
20
+ remove_shortcode( 'youtube' );
21
+ }
22
+
23
+ public function get_scripts() {
24
+ if ( ! $this->did_convert_elements ) {
25
+ return array();
26
+ }
27
+
28
+ return array( self::$script_slug => self::$script_src );
29
+ }
30
+
31
+ public function shortcode( $attr ) {
32
+ $url = false;
33
+ $video_id = false;
34
+ if ( isset( $attr[0] ) ) {
35
+ $url = ltrim( $attr[0] , '=' );
36
+ } elseif ( function_exists ( 'shortcode_new_to_old_params' ) ) {
37
+ $url = shortcode_new_to_old_params( $attr );
38
+ }
39
+
40
+ if ( empty( $url ) ) {
41
+ return '';
42
+ }
43
+
44
+ $video_id = $this->get_video_id_from_url( $url );
45
+
46
+ return $this->render( array(
47
+ 'url' => $url,
48
+ 'video_id' => $video_id,
49
+ ) );
50
+ }
51
+
52
+ public function oembed( $matches, $attr, $url, $rawattr ) {
53
+ return $this->shortcode( array( $url ) );
54
+ }
55
+
56
+ public function render( $args ) {
57
+ $args = wp_parse_args( $args, array(
58
+ 'video_id' => false,
59
+ ) );
60
+
61
+ if ( empty( $args['video_id'] ) ) {
62
+ return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
63
+ }
64
+
65
+ $this->did_convert_elements = true;
66
+
67
+ return AMP_HTML_Utils::build_tag(
68
+ 'amp-youtube',
69
+ array(
70
+ 'data-videoid' => $args['video_id'],
71
+ 'layout' => 'responsive',
72
+ 'width' => $this->args['width'],
73
+ 'height' => $this->args['height'],
74
+ )
75
+ );
76
+ }
77
+
78
+ private function get_video_id_from_url( $url ) {
79
+ $video_id = false;
80
+ $parsed_url = parse_url( $url );
81
+
82
+ if ( self::SHORT_URL_HOST === substr( $parsed_url['host'], -strlen( self::SHORT_URL_HOST ) ) ) {
83
+ // youtu.be/{id}
84
+ $parts = explode( '/', $parsed_url['path'] );
85
+ if ( ! empty( $parts ) ) {
86
+ $video_id = $parts[1];
87
+ }
88
+ } else {
89
+ // ?v={id} or ?list={id}
90
+ parse_str( $parsed_url['query'], $query_args );
91
+
92
+ if ( isset( $query_args['v'] ) ) {
93
+ $video_id = $query_args['v'];
94
+ }
95
+ }
96
+
97
+ if ( empty( $video_id ) ) {
98
+ // /(v|e|embed)/{id}
99
+ $parts = explode( '/', $parsed_url['path'] );
100
+
101
+ if ( in_array( $parts[1], array( 'v', 'e', 'embed' ) ) ) {
102
+ $video_id = $parts[2];
103
+ }
104
+ }
105
+
106
+ return $video_id;
107
+ }
108
+ }
includes/sanitizers/class-amp-audio-sanitizer.php ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
4
+
5
+ class AMP_Audio_Sanitizer extends AMP_Base_Sanitizer {
6
+ public static $tag = 'audio';
7
+
8
+ private static $script_slug = 'amp-audio';
9
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-audio-0.1.js';
10
+
11
+ public function get_scripts() {
12
+ if ( ! $this->did_convert_elements ) {
13
+ return array();
14
+ }
15
+
16
+ return array( self::$script_slug => self::$script_src );
17
+ }
18
+
19
+ public function sanitize() {
20
+ $nodes = $this->dom->getElementsByTagName( self::$tag );
21
+ $num_nodes = $nodes->length;
22
+ if ( 0 === $num_nodes ) {
23
+ return;
24
+ }
25
+
26
+ for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
27
+ $node = $nodes->item( $i );
28
+ $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
29
+
30
+ $new_attributes = $this->filter_attributes( $old_attributes );
31
+
32
+ $new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-audio', $new_attributes );
33
+
34
+ // TODO: limit child nodes too (only allowed: `source`; move rest to div+fallback)
35
+ // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well.
36
+ foreach ( $node->childNodes as $child_node ) {
37
+ $new_child_node = $child_node->cloneNode( true );
38
+ $new_node->appendChild( $new_child_node );
39
+ }
40
+
41
+ $node->parentNode->replaceChild( $new_node, $node );
42
+
43
+ $this->did_convert_elements = true;
44
+ }
45
+ }
46
+
47
+ private function filter_attributes( $attributes ) {
48
+ $out = array();
49
+
50
+ foreach ( $attributes as $name => $value ) {
51
+ switch ( $name ) {
52
+ case 'src':
53
+ case 'width':
54
+ case 'height':
55
+ case 'class':
56
+ $out[ $name ] = $value;
57
+ break;
58
+ case 'loop':
59
+ case 'muted':
60
+ if ( 'false' !== $value ) {
61
+ $out[ $name ] = 'true';
62
+ }
63
+ break;
64
+ case 'autoplay':
65
+ $out[ $name ] = 'desktop tablet mobile';
66
+ break;
67
+ default;
68
+ break;
69
+ }
70
+ }
71
+
72
+ return $out;
73
+ }
74
+ }
includes/sanitizers/class-amp-base-sanitizer.php ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ abstract class AMP_Base_Sanitizer {
4
+ protected $DEFAULT_ARGS = array();
5
+
6
+ protected $dom;
7
+ protected $args;
8
+ protected $did_convert_elements = false;
9
+
10
+ public function __construct( $dom, $args = array() ) {
11
+ $this->dom = $dom;
12
+ $this->args = array_merge( $this->DEFAULT_ARGS, $args );
13
+ }
14
+
15
+ abstract public function sanitize();
16
+
17
+ public function get_scripts() {
18
+ return array();
19
+ }
20
+
21
+ protected function get_body_node() {
22
+ return $this->dom->getElementsByTagName( 'body' )->item( 0 );
23
+ }
24
+
25
+ /**
26
+ * This is our workaround to enforce max sizing with layout=responsive.
27
+ *
28
+ * We want elements to not grow beyond their width and shrink to fill the screen on viewports smaller than their width.
29
+ *
30
+ * See https://github.com/ampproject/amphtml/issues/1280#issuecomment-171533526
31
+ * See https://github.com/Automattic/amp-wp/issues/101
32
+ */
33
+ public function enforce_sizes_attribute( $attributes ) {
34
+ if ( isset( $attributes['sizes'] ) ) {
35
+ return $attributes;
36
+ }
37
+
38
+ if ( ! isset( $attributes['width'], $attributes['height'] ) ) {
39
+ return $attributes;
40
+ }
41
+
42
+ $max_width = $attributes['width'];
43
+ if ( isset( $this->args['content_max_width'] ) && $max_width >= $this->args['content_max_width'] ) {
44
+ $max_width = $this->args['content_max_width'];
45
+ }
46
+
47
+ $attributes['sizes'] = sprintf( '(min-width: %1$dpx) %1$dpx, 100vw', absint( $max_width ) );
48
+
49
+ $class = 'amp-wp-enforced-sizes';
50
+ if ( isset( $attributes['class'] ) ) {
51
+ $attributes['class'] .= ' ' . $class;
52
+ } else {
53
+ $attributes['class'] = $class;
54
+ }
55
+
56
+ return $attributes;
57
+ }
58
+ }
includes/sanitizers/class-amp-blacklist-sanitizer.php ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
4
+
5
+ /**
6
+ * Strips blacklisted tags and attributes from content.
7
+ *
8
+ * See following for blacklist:
9
+ * https://github.com/ampproject/amphtml/blob/master/spec/amp-html-format.md#html-tags
10
+ */
11
+ class AMP_Blacklist_Sanitizer extends AMP_Base_Sanitizer {
12
+ public function sanitize() {
13
+ $blacklisted_tags = $this->get_blacklisted_tags();
14
+ $blacklisted_attributes = $this->get_blacklisted_attributes();
15
+ $blacklisted_protocols = $this->get_blacklisted_protocols();
16
+
17
+ $body = $this->get_body_node();
18
+ $this->strip_tags( $body, $blacklisted_tags );
19
+ $this->strip_attributes_recursive( $body, $blacklisted_attributes, $blacklisted_protocols );
20
+ }
21
+
22
+ private function strip_attributes_recursive( $node, $bad_attributes, $bad_protocols ) {
23
+ if ( $node->nodeType !== XML_ELEMENT_NODE ) {
24
+ return;
25
+ }
26
+
27
+ if ( $node->hasAttributes() ) {
28
+ $length = $node->attributes->length;
29
+ for ( $i = $length - 1; $i >= 0; $i-- ) {
30
+ $attribute = $node->attributes->item( $i );
31
+ $attribute_name = strtolower( $attribute->name );
32
+ if ( in_array( $attribute_name, $bad_attributes ) ) {
33
+ $node->removeAttribute( $attribute_name );
34
+ continue;
35
+ }
36
+
37
+ // on* attributes (like onclick) are a special case
38
+ if ( 0 === stripos( $attribute_name, 'on' ) ) {
39
+ $node->removeAttribute( $attribute_name );
40
+ continue;
41
+ }
42
+
43
+ if ( 'href' === $attribute_name ) {
44
+ $protocol = strtok( $attribute->value, ':' );
45
+ if ( in_array( $protocol, $bad_protocols ) ) {
46
+ $node->removeAttribute( $attribute_name );
47
+ continue;
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ foreach ( $node->childNodes as $child_node ) {
54
+ $this->strip_attributes_recursive( $child_node, $bad_attributes, $bad_protocols );
55
+ }
56
+ }
57
+
58
+ private function strip_tags( $node, $tag_names ) {
59
+ foreach ( $tag_names as $tag_name ) {
60
+ $elements = $node->getElementsByTagName( $tag_name );
61
+ $length = $elements->length;
62
+ if ( 0 === $length ) {
63
+ continue;
64
+ }
65
+
66
+ for ( $i = $length - 1; $i >= 0; $i-- ) {
67
+ $element = $elements->item( $i );
68
+ $parent_node = $element->parentNode;
69
+ $parent_node->removeChild( $element );
70
+
71
+ if ( 'body' !== $parent_node->nodeName && AMP_DOM_Utils::is_node_empty( $parent_node ) ) {
72
+ $parent_node->parentNode->removeChild( $parent_node );
73
+ }
74
+ }
75
+ }
76
+ }
77
+
78
+ private function get_blacklisted_protocols() {
79
+ return array(
80
+ 'javascript',
81
+ );
82
+ }
83
+
84
+ private function get_blacklisted_tags() {
85
+ return array(
86
+ 'script',
87
+ 'noscript',
88
+ 'style',
89
+ 'frame',
90
+ 'frameset',
91
+ 'object',
92
+ 'param',
93
+ 'applet',
94
+ 'form',
95
+ 'input',
96
+ 'button',
97
+ 'textarea',
98
+ 'select',
99
+ 'option',
100
+ 'link',
101
+ 'meta',
102
+
103
+ // These are converted into amp-* versions
104
+ //'img',
105
+ //'video',
106
+ //'audio',
107
+ //'iframe',
108
+ );
109
+ }
110
+
111
+ private function get_blacklisted_attributes() {
112
+ return array(
113
+ 'style',
114
+ );
115
+ }
116
+ }
includes/sanitizers/class-amp-iframe-sanitizer.php ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
4
+
5
+ /**
6
+ * Converts <iframe> tags to <amp-iframe>
7
+ */
8
+ class AMP_Iframe_Sanitizer extends AMP_Base_Sanitizer {
9
+ const FALLBACK_HEIGHT = 400;
10
+ const SANDBOX_DEFAULTS = 'allow-scripts allow-same-origin';
11
+
12
+ public static $tag = 'iframe';
13
+
14
+ private static $script_slug = 'amp-iframe';
15
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-iframe-0.1.js';
16
+
17
+ protected $DEFAULT_ARGS = array(
18
+ 'add_placeholder' => false,
19
+ );
20
+
21
+ public function get_scripts() {
22
+ if ( ! $this->did_convert_elements ) {
23
+ return array();
24
+ }
25
+
26
+ return array( self::$script_slug => self::$script_src );
27
+ }
28
+
29
+ public function sanitize() {
30
+ $nodes = $this->dom->getElementsByTagName( self::$tag );
31
+ $num_nodes = $nodes->length;
32
+ if ( 0 === $num_nodes ) {
33
+ return;
34
+ }
35
+
36
+ for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
37
+ $node = $nodes->item( $i );
38
+ $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
39
+
40
+ if ( ! array_key_exists( 'src', $old_attributes ) ) {
41
+ $node->parentNode->removeChild( $node );
42
+ continue;
43
+ }
44
+
45
+ $this->did_convert_elements = true;
46
+
47
+ $new_attributes = $this->filter_attributes( $old_attributes );
48
+ if ( ! isset( $new_attributes['width'], $new_attributes['height'] ) ) {
49
+ $new_attributes['height'] = self::FALLBACK_HEIGHT;
50
+ $new_attributes['layout'] = 'fixed-height';
51
+ }
52
+ $new_attributes = $this->enforce_sizes_attribute( $new_attributes );
53
+
54
+ $new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-iframe', $new_attributes );
55
+
56
+ if ( true === $this->args['add_placeholder'] ) {
57
+ $placeholder_node = $this->build_placeholder( $new_attributes );
58
+ $new_node->appendChild( $placeholder_node );
59
+ }
60
+
61
+ $parent_node = $node->parentNode;
62
+ if ( 'p' === strtolower( $parent_node->tagName ) ) {
63
+ // AMP does not like iframes in p tags
64
+ $parent_node->removeChild( $node );
65
+ $parent_node->parentNode->insertBefore( $new_node, $parent_node->nextSibling );
66
+
67
+ if ( AMP_DOM_Utils::is_node_empty( $parent_node ) ) {
68
+ $parent_node->parentNode->removeChild( $parent_node );
69
+ }
70
+ } else {
71
+ $parent_node->replaceChild( $new_node, $node );
72
+ }
73
+ }
74
+ }
75
+
76
+ private function filter_attributes( $attributes ) {
77
+ $out = array();
78
+
79
+ foreach ( $attributes as $name => $value ) {
80
+ switch ( $name ) {
81
+ case 'src':
82
+ case 'sandbox':
83
+ case 'width':
84
+ case 'height':
85
+ case 'frameborder':
86
+ case 'class':
87
+ case 'sizes':
88
+ $out[ $name ] = $value;
89
+ break;
90
+
91
+ case 'allowfullscreen':
92
+ case 'allowtransparency':
93
+ if ( 'false' !== $value ) {
94
+ $out[ $name ] = '';
95
+ }
96
+ break;
97
+
98
+ default;
99
+ break;
100
+ }
101
+ }
102
+
103
+ if ( ! isset( $out[ 'sandbox' ] ) ) {
104
+ $out[ 'sandbox' ] = self::SANDBOX_DEFAULTS;
105
+ }
106
+
107
+ return $out;
108
+ }
109
+
110
+ private function build_placeholder( $parent_attributes ) {
111
+ $placeholder_node = AMP_DOM_Utils::create_node( $this->dom, 'div', array(
112
+ 'placeholder' => '',
113
+ 'class' => 'amp-wp-iframe-placeholder',
114
+ ) );
115
+
116
+ return $placeholder_node;
117
+ }
118
+ }
includes/sanitizers/class-amp-img-sanitizer.php ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
4
+ require_once( AMP__DIR__ . '/includes/utils/class-amp-image-dimension-extractor.php' );
5
+
6
+ /**
7
+ * Converts <img> tags to <amp-img> or <amp-anim>
8
+ */
9
+ class AMP_Img_Sanitizer extends AMP_Base_Sanitizer {
10
+ public static $tag = 'img';
11
+
12
+ private static $anim_extension = '.gif';
13
+
14
+ private static $script_slug = 'amp-anim';
15
+ private static $script_src = 'https://cdn.ampproject.org/v0/amp-anim-0.1.js';
16
+
17
+ public function sanitize() {
18
+ $nodes = $this->dom->getElementsByTagName( self::$tag );
19
+ $num_nodes = $nodes->length;
20
+ if ( 0 === $num_nodes ) {
21
+ return;
22
+ }
23
+
24
+ for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
25
+ $node = $nodes->item( $i );
26
+ $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
27
+
28
+ if ( ! array_key_exists( 'src', $old_attributes ) ) {
29
+ $node->parentNode->removeChild( $node );
30
+ continue;
31
+ }
32
+
33
+ $new_attributes = $this->filter_attributes( $old_attributes );
34
+ if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) {
35
+ $dimensions = AMP_Image_Dimension_Extractor::extract( $new_attributes['src'] );
36
+ if ( $dimensions ) {
37
+ $new_attributes['width'] = $dimensions[0];
38
+ $new_attributes['height'] = $dimensions[1];
39
+ }
40
+ }
41
+
42
+ $new_attributes = $this->enforce_sizes_attribute( $new_attributes );
43
+
44
+ if ( $this->is_gif_url( $new_attributes['src'] ) ) {
45
+ $this->did_convert_elements = true;
46
+ $new_tag = 'amp-anim';
47
+ } else {
48
+ $new_tag = 'amp-img';
49
+ }
50
+
51
+ $new_node = AMP_DOM_Utils::create_node( $this->dom, $new_tag, $new_attributes );
52
+ $node->parentNode->replaceChild( $new_node, $node );
53
+ }
54
+ }
55
+
56
+ public function get_scripts() {
57
+ if ( ! $this->did_convert_elements ) {
58
+ return array();
59
+ }
60
+
61
+ return array( self::$script_slug => self::$script_src );
62
+ }
63
+
64
+ private function filter_attributes( $attributes ) {
65
+ $out = array();
66
+
67
+ foreach ( $attributes as $name => $value ) {
68
+ switch ( $name ) {
69
+ case 'src':
70
+ case 'alt':
71
+ case 'width':
72
+ case 'height':
73
+ case 'class':
74
+ case 'srcset':
75
+ case 'sizes':
76
+ $out[ $name ] = $value;
77
+ break;
78
+ default;
79
+ break;
80
+ }
81
+ }
82
+
83
+ return $out;
84
+ }
85
+
86
+ private function is_gif_url( $url ) {
87
+ $ext = self::$anim_extension;
88
+ $path = parse_url( $url, PHP_URL_PATH );
89
+ return $ext === substr( $path, -strlen( $ext ) );
90
+ }
91
+ }
includes/sanitizers/class-amp-video-sanitizer.php ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
4
+
5
+ /**
6
+ * Converts <video> tags to <amp-video>
7
+ */
8
+ class AMP_Video_Sanitizer extends AMP_Base_Sanitizer {
9
+ const FALLBACK_HEIGHT = 400;
10
+
11
+ public static $tag = 'video';
12
+
13
+ public function sanitize() {
14
+ $nodes = $this->dom->getElementsByTagName( self::$tag );
15
+ $num_nodes = $nodes->length;
16
+ if ( 0 === $num_nodes ) {
17
+ return;
18
+ }
19
+
20
+ for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
21
+ $node = $nodes->item( $i );
22
+ $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
23
+
24
+ $new_attributes = $this->filter_attributes( $old_attributes );
25
+ if ( ! isset( $new_attributes['width'], $new_attributes['height'] ) ) {
26
+ $new_attributes['height'] = self::FALLBACK_HEIGHT;
27
+ $new_attributes['layout'] = 'fixed-height';
28
+ }
29
+ $new_attributes = $this->enforce_sizes_attribute( $new_attributes );
30
+
31
+ $new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-video', $new_attributes );
32
+
33
+ // TODO: limit child nodes too (only allowed: `source`; move rest to div+fallback)
34
+ // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well.
35
+ foreach ( $node->childNodes as $child_node ) {
36
+ $new_child_node = $child_node->cloneNode( true );
37
+ $new_node->appendChild( $new_child_node );
38
+ }
39
+
40
+ $node->parentNode->replaceChild( $new_node, $node );
41
+ }
42
+ }
43
+
44
+ private function filter_attributes( $attributes ) {
45
+ $out = array();
46
+
47
+ foreach ( $attributes as $name => $value ) {
48
+ switch ( $name ) {
49
+ case 'src':
50
+ case 'poster':
51
+ case 'width':
52
+ case 'height':
53
+ case 'class':
54
+ case 'sizes':
55
+ $out[ $name ] = $value;
56
+ break;
57
+ case 'controls':
58
+ case 'loop':
59
+ case 'muted':
60
+ if ( 'false' !== $value ) {
61
+ $out[ $name ] = 'true';
62
+ }
63
+ break;
64
+ case 'autoplay':
65
+ $out[ $name ] = 'desktop tablet mobile';
66
+ break;
67
+ default;
68
+ break;
69
+ }
70
+ }
71
+
72
+ return $out;
73
+ }
74
+ }
includes/utils/class-amp-dom-utils.php ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class AMP_DOM_Utils {
4
+ public static function get_dom_from_content( $content ) {
5
+ $libxml_previous_state = libxml_use_internal_errors( true );
6
+
7
+ $dom = new DOMDocument;
8
+ // Wrap in dummy tags, since XML needs one parent node.
9
+ // It also makes it easier to loop through nodes.
10
+ // We can later use this to extract our nodes.
11
+ // Add utf-8 charset so loadHTML does not have problems parsing it. See: http://php.net/manual/en/domdocument.loadhtml.php#78243
12
+ $result = $dom->loadHTML( '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $content . '</body></html>' );
13
+
14
+ libxml_clear_errors();
15
+ libxml_use_internal_errors( $libxml_previous_state );
16
+
17
+ if ( ! $result ) {
18
+ return false;
19
+ }
20
+
21
+ return $dom;
22
+ }
23
+
24
+ public static function get_content_from_dom( $dom ) {
25
+ // Only want children of the body tag, since we have a subset of HTML.
26
+ $out = '';
27
+ $body = $dom->getElementsByTagName( 'body' )->item( 0 );
28
+ foreach ( $body->childNodes as $node ) {
29
+ $out .= $dom->saveXML( $node, LIBXML_NOEMPTYTAG );
30
+ }
31
+ return $out;
32
+ }
33
+
34
+ public static function create_node( $dom, $tag, $attributes ) {
35
+ $node = $dom->createElement( $tag );
36
+ self::add_attributes_to_node( $node, $attributes );
37
+ return $node;
38
+ }
39
+
40
+ public static function get_node_attributes_as_assoc_array( $node ) {
41
+ $attributes = array();
42
+ foreach ( $node->attributes as $attribute ) {
43
+ $attributes[ $attribute->nodeName ] = $attribute->nodeValue;
44
+ }
45
+ return $attributes;
46
+ }
47
+
48
+ public static function add_attributes_to_node( $node, $attributes ) {
49
+ foreach ( $attributes as $name => $value ) {
50
+ $node->setAttribute( $name, $value );
51
+ }
52
+ }
53
+
54
+ public static function is_node_empty( $node ) {
55
+ return 0 === $node->childNodes->length && empty( $node->textContent );
56
+ }
57
+ }
includes/utils/class-amp-html-utils.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class AMP_HTML_Utils {
4
+ public static function build_tag( $tag_name, $attributes = array(), $content = '' ) {
5
+ $attr_string = self::build_attributes_string( $attributes );
6
+ return sprintf( '<%1$s %2$s>%3$s</%1$s>', sanitize_key( $tag_name ), $attr_string, $content );
7
+ }
8
+
9
+ public static function build_attributes_string( $attributes ) {
10
+ $string = array();
11
+ foreach ( $attributes as $name => $value ) {
12
+ if ( '' === $value ) {
13
+ $string[] = sprintf( '%s', sanitize_key( $name ) );
14
+ } else {
15
+ $string[] = sprintf( '%s="%s"', sanitize_key( $name ), esc_attr( $value ) );
16
+ }
17
+ }
18
+ return implode( ' ', $string );
19
+ }
20
+ }
includes/utils/class-amp-image-dimension-extractor.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class AMP_Image_Dimension_Extractor {
4
+ static $callbacks_registered = false;
5
+
6
+ static public function extract( $url ) {
7
+ if ( ! self::$callbacks_registered ) {
8
+ self::register_callbacks();
9
+ }
10
+
11
+ return apply_filters( 'amp_extract_image_dimensions', false, $url );
12
+ }
13
+
14
+ private static function register_callbacks() {
15
+ self::$callbacks_registered = true;
16
+
17
+ add_filter( 'amp_extract_image_dimensions', array( __CLASS__, 'extract_from_attachment_metadata' ), 10, 2 );
18
+ }
19
+
20
+ public static function extract_from_attachment_metadata( $dimensions, $url ) {
21
+ if ( $dimensions ) {
22
+ return $dimensions;
23
+ }
24
+
25
+ $url = strtok( $url, '?' );
26
+ $attachment_id = attachment_url_to_postid( $url );
27
+ if ( empty( $attachment_id ) ) {
28
+ return false;
29
+ }
30
+
31
+ $metadata = wp_get_attachment_metadata( $attachment_id );
32
+ if ( ! $metadata ) {
33
+ return false;
34
+ }
35
+
36
+ return array( $metadata['width'], $metadata['height'] );
37
+ }
38
+ }
jetpack-helper.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Jetpack bits.
4
+
5
+ add_action( 'pre_amp_render', 'amp_jetpack_mods' );
6
+
7
+ /**
8
+ * Disable Jetpack features that are not compatible with AMP.
9
+ *
10
+ **/
11
+ function amp_jetpack_mods() {
12
+ amp_jetpack_disable_related_posts();
13
+ }
14
+
15
+ /**
16
+ * Remove the Related Posts placeholder and headline that gets hooked into the_content
17
+ *
18
+ * That placeholder is useless since we can't ouput, and don't want to output Related Posts in AMP.
19
+ *
20
+ **/
21
+ function amp_jetpack_disable_related_posts() {
22
+ if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
23
+ $jprp = Jetpack_RelatedPosts::init();
24
+ remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ), 40 );
25
+ }
26
+ }
readme.md ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AMP for WordPress
2
+
3
+ ## Overview
4
+
5
+ This plugin adds support for the Accelerated Mobile Pages (AMP) Project, which is an an open source initiative that aims to provide mobile optimized content that can load instantly everywhere.
6
+
7
+ With the plugin active, all content on your site will have dynamically generated AMP-compatible versions, accessible by appending `/amp/` to the end your permalinks. (If you do not have pretty permalinks enabled, you can do the same thing by appending `?amp=1`.)
8
+
9
+ Developers: please note that this plugin is still in early stages and the underlying APIs (like filters, classes, etc.) may change.
10
+
11
+ ## Customization / Templating
12
+
13
+ The plugin ships with a default template that looks nice and clean and we tried to find a good balance between ease and extensibility when it comes to customization.
14
+
15
+ You can tweak small pieces of the template or the entire thing depending on your needs.
16
+
17
+ ### Theme Mods
18
+
19
+ (This still needs to be implemented.)
20
+
21
+ The default template will attempt to draw from various theme mods, such as site icon and background and header color/image, if supported by the active theme.
22
+
23
+ ### Template Tweaks
24
+
25
+ You can tweak various parts of the template via code.
26
+
27
+ #### Content Width
28
+
29
+ By default, your theme's `$content_width` value will be used to determine the size of the `amp` content well. You can change this:
30
+
31
+ ```php
32
+ add_filter( 'amp_content_max_width', 'xyz_amp_change_content_width' );
33
+
34
+ function xyz_amp_change_content_width( $content_max_width ) {
35
+ return 1200;
36
+ }
37
+ ```
38
+
39
+ #### Template Data
40
+
41
+ Use the `amp_post_template_data` filter to override default template data. The following changes the placeholder image used for iframes to a file located in the current theme:
42
+
43
+ ```php
44
+ add_filter( 'amp_post_template_data', 'xyz_amp_set_custom_placeholder_image' );
45
+
46
+ function xyz_set_custom_placeholder_image( $data ) {
47
+ $data[ 'placeholder_image_url' ] = get_stylesheet_directory_uri() . '/images/amp-iframe-placeholder.png';
48
+ return $data;
49
+ }
50
+ ```
51
+
52
+ Note: The path must pass the default criteria set out by [`validate_file`](https://developer.wordpress.org/reference/functions/validate_file/) and must be somewhere in a subfolder of `WP_CONTENT_DIR`.
53
+
54
+ #### Meta
55
+
56
+ For the meta section of the template (i.e. author, date, taxonomies, etc.), you can override templates for the existing sections, remove them, add new ones.
57
+
58
+ ##### Example: Override Author Template from Theme
59
+
60
+ Create a folder in your theme called `amp` and add a file called `meta-author.php` with the following:
61
+
62
+ ```php
63
+ <li class="byline">
64
+ <span>Anonymous</span>
65
+ </li>
66
+ ```
67
+
68
+ Replace the contents, as needed.
69
+
70
+ ##### Example: Override Taxonomy Template via filter
71
+
72
+ This will load the file `t/meta-custom-tax.php` for the `taxonomy` section:
73
+
74
+ ```php
75
+ add_filter( 'amp_post_template_file', 'xyz_amp_set_custom_tax_meta_template', 10, 3 );
76
+
77
+ function xyz_amp_set_custom_tax_meta_template( $file, $type, $post ) {
78
+ if ( 'meta-taxonomy' === $type ) {
79
+ $file = dirname( __FILE__ ) . '/t/meta-custom-tax.php';
80
+ }
81
+ return $file;
82
+ }
83
+ ```
84
+
85
+ In `t/meta-custom-tax.php`, you can add something like the following to replace the default category and tags with your custom `author` taxonomy:
86
+
87
+ ```php
88
+ <li class="tax-authors">
89
+ <?php echo get_the_term_list( $this->get( 'post_id' ), 'xyz-author', '', ', ' ); ?>
90
+ </li>
91
+ ```
92
+
93
+ ##### Example: Remove Author from `meta`
94
+
95
+ This will completely remove the author section:
96
+
97
+ ```php
98
+ add_filter( 'amp_post_template_meta_parts', 'xyz_amp_remove_author_meta' );
99
+
100
+ function xyz_amp_remove_author_meta( $meta_parts ) {
101
+ foreach ( array_keys( $meta_parts, 'meta-author', true ) as $key ) {
102
+ unset( $meta_parts[ $key ] );
103
+ }
104
+ return $meta_parts;
105
+ }
106
+ ```
107
+
108
+ ##### Example: Add Comment Count to `meta`
109
+
110
+ This adds a new section to display the comment count:
111
+
112
+ ```php
113
+ add_filter( 'amp_post_template_meta_parts', 'xyz_amp_add_comment_count_meta' );
114
+
115
+ function xyz_amp_add_comment_count_meta( $meta_parts ) {
116
+ $meta_parts[] = 'xyz-meta-comment-count';
117
+ return $meta_parts;
118
+ }
119
+
120
+ add_filter( 'amp_post_template_file', 'xyz_amp_set_comment_count_meta_path', 10, 3 );
121
+
122
+ function xyz_amp_set_comment_count_meta_path( $file, $type, $post ) {
123
+ if ( 'xyz-meta-comment-count' === $type ) {
124
+ $file = dirname( __FILE__ ) . '/templates/xyz-meta-comment-count.php';
125
+ }
126
+ return $file;
127
+ }
128
+ ```
129
+
130
+ Then, in `templates/xyz-meta-comment-count.php`:
131
+
132
+ ```php
133
+ <li>
134
+ <?php printf( _n( '%d comment', '%d comments', $this->get( 'post' )->comment_count, 'xyz-text-domain' ) ); ?>
135
+ </li>
136
+ ```
137
+
138
+ #### Custom CSS
139
+
140
+ If you'd prefer to use your own styles, you can either:
141
+
142
+ - Create a folder in your theme called `amp` and add a file called `style.php` with your custom CSS.
143
+ - Specify a custom template using the `amp_post_template_file` filter for `'style' === $type`.
144
+
145
+ See the "Override" examples in the "Meta" section for examples.
146
+
147
+ #### Head and Footer
148
+
149
+ If you want to add stuff to the head or footer of the default AMP template, use the `amp_post_template_head` and `amp_post_template_footer` actions.
150
+
151
+ ```php
152
+ add_action( 'amp_post_template_footer', 'xyz_amp_add_analytics' );
153
+
154
+ function xyz_amp_add_analytics( $amp_template ) {
155
+ $post_id = $amp_template->get( 'post_id' );
156
+ // see https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/amp-analytics.md for more on amp-analytics
157
+ ?>
158
+ <amp-analytics>
159
+ <script type="application/json">
160
+ {
161
+ "requests": {
162
+ "pageview": "https://example.com/analytics?url=${canonicalUrl}&title=${title}&acct=${account}",
163
+ "event": "https://example.com/analytics?eid=${eventId}&elab=${eventLabel}&acct=${account}"
164
+ }
165
+ // ...
166
+ }
167
+ </script>
168
+ </amp-analytics>
169
+ <?php
170
+ }
171
+ ```
172
+
173
+ ### Custom Template
174
+
175
+ If you want complete control over the look and feel of your AMP content, you can override the default template using the `amp_post_template_file` filter and pass it the path to a custom template:
176
+
177
+ ```php
178
+ add_filter( 'amp_post_template_file', 'xyz_amp_set_custom_template', 10, 3 );
179
+
180
+ function xyz_amp_set_custom_template( $file, $type, $post ) {
181
+ if ( 'single' === $type ) {
182
+ $file = dirname( __FILE__ ) . '/templates/my-amp-template.php';
183
+ }
184
+ return $file;
185
+ }
186
+ ```
187
+
188
+ Note: there are some requirements for a custom template:
189
+
190
+ * You must trigger the `amp_post_template_head` action in the `<head>` section:
191
+
192
+ ```
193
+ do_action( 'amp_post_template_head', $this );
194
+ ```
195
+
196
+ * You must trigger the `amp_post_template_footer` action right before the `</body>` tag:
197
+
198
+ ```
199
+ do_action( 'amp_post_template_footer', $this );
200
+ ```
201
+
202
+ * You must include [all required mark-up](https://www.ampproject.org/docs/get_started/create/basic_markup.html) that isn't already output via the `amp_post_template_head` action.
203
+
204
+ ## Handling Media
205
+
206
+ By default, the plugin attempts to gracefully handle the following media elements in your content:
207
+
208
+ - images (converted from `img` => `amp-img` or `amp-anim`)
209
+ - videos (converted from `video` => `amp-video`; Note: Flash is not supported)
210
+ - audio (converted from `audio` => `amp-audio`)
211
+ - iframes (converted from `iframes` => `amp-iframes`)
212
+ - YouTube, Instagram, Twitter, and Vine oEmbeds and shortcodes (converted from the embed to the matching `amp-` component)
213
+
214
+ For additional media content such as custom shortcodes, oEmbeds or manually inserted embeds, ads, etc. there are several customization options available and outlined below.
215
+
216
+ ### Do Nothing
217
+
218
+ If your embeds/media use standard iframes, you can choose to do nothing and let the plugin handle things. They should "just work" in most cases.
219
+
220
+ ### `the_content` filter
221
+
222
+ All existing hooks on `the_content` will continue to work. This can be a good or bad thing. Good, because existing plugin integrations will continue to work. Bad, because not all added content may make sense in an AMP context.
223
+
224
+ You can add additional callbacks to `the_content` filter to output additional content as needed. Use the `is_amp_endpoint()` function to check if an AMP version of a post is being viewed. However, we recommend using an Embed Handler instead.
225
+
226
+ Caveat: with this method, if you add a custom component that requires inclusion of a script, you will need to add that script manually to the template using the `amp_post_template_head` action.
227
+
228
+ ### Update Existing Shortcodes
229
+
230
+ In your existing shortcode or oEmbed callbacks, you can branch using the `is_amp_endpoint()` and output customized content for AMP content.
231
+
232
+ The same caveat about scripts for custom AMP components applies.
233
+
234
+ ### Custom Embed Handler
235
+
236
+ Embed Handlers are helper classes to inject AMP-specific content for your oEmbeds and shortcodes.
237
+
238
+ Embed Handlers register the embeds they handle using standard WordPress functions such as `add_shortcode`. For working examples, check out the existing implementations for Instagram, Twitter, etc. as guides to build your own.
239
+
240
+ While the primary purpose of Embed Handlers is for use with embeds, you can use them for adding AMP-specific `the_content` callbacks as well.
241
+
242
+ #### Step 1: Build the Embed Handler
243
+
244
+ Your Embed Handler class needs to extend the `AMP_Base_Embed_Handler` class.
245
+
246
+ Note: make sure to set proper priorities or remove existing callbacks for your regular content.
247
+
248
+ In `classes/class-amp-related-posts-embed.php`:
249
+
250
+ ```php
251
+ class XYZ_AMP_Related_Posts_Embed extends AMP_Base_Embed_Handler {
252
+ public function register_embed() {
253
+ // If we have an existing callback we are overriding, remove it.
254
+ remove_filter( 'the_content', 'xyz_add_related_posts' );
255
+
256
+ // Add our new callback
257
+ add_filter( 'the_content', array( $this, 'add_related_posts' ) );
258
+ }
259
+
260
+ public function unregister_embed() {
261
+ // Let's clean up after ourselves, just in case.
262
+ add_filter( 'the_content', 'xyz_add_related_posts' );
263
+ remove_filter( 'the_content', array( $this, 'add_related_posts' ) );
264
+ }
265
+
266
+ public function get_scripts() {
267
+ return array( 'amp-mustache' => 'https://cdn.ampproject.org/v0/amp-mustache-0.1.js' );
268
+ }
269
+
270
+ public function add_related_posts( $content ) {
271
+ // See https://github.com/ampproject/amphtml/blob/master/extensions/amp-list/amp-list.md for details on amp-list
272
+ $related_posts_list = '
273
+ <amp-list src="https://data.com/articles.json?ref=CANONICAL_URL" width=300 height=200 layout=responsive>
274
+ <template type="amp-mustache">
275
+ <div>
276
+ <amp-img src="{{imageUrl}}" width=50 height=50></amp-img>
277
+ {{title}}
278
+ </div>
279
+ </template>
280
+ <div overflow role=button aria-label="Show more" class="list-overflow">
281
+ Show more
282
+ </div>
283
+ </amp-list>';
284
+
285
+ $content .= $related_posts_list;
286
+
287
+ return $content;
288
+ }
289
+ }
290
+ ```
291
+
292
+ #### Step 2: Load the Embed Handler
293
+
294
+ ```php
295
+ add_filter( 'amp_content_embed_handlers', 'xyz_amp_add_related_embed', 10, 2 );
296
+
297
+ function xyz_amp_add_related_embed( $embed_handler_classes, $post ) {
298
+ require_once( dirname( __FILE__ ) . '/classes/class-amp-related-posts-embed.php' );
299
+ $embed_handler_classes[ 'XYZ_AMP_Related_Posts_Embed' ] = array();
300
+ return $embed_handler_classes;
301
+ }
302
+ ```
303
+
304
+ ### Custom Sanitizer
305
+
306
+ The name "sanitizer" is a bit of a misnomer. These are primarily used internally in the plugin to make your site's content compatible with the amp spec. This involves stripping unsupported tags and attributes and transforming media elements to their matching amp version (e.g. `img` => `amp-img`).
307
+
308
+ Sanitizers are pretty versatile and, unlike Embed Handlers -- which work with HTML content as a string -- they can be used to manipulate your post's AMP content using [PHP's `DOM` library](http://php.net/manual/en/book.dom.php). We've included an example that shows you how to use a custom sanitizer to inject ads into your content. You can, of course, do many other things such as add related content.
309
+
310
+ #### Step 1: Build the Sanitizer
311
+
312
+ Your sanitizer needs to extend the `AMP_Base_Sanitizer`. In `classes/class-ad-inject-sanitizer.php`:
313
+
314
+ ```php
315
+ class XYZ_AMP_Ad_Injection_Sanitizer extends AMP_Base_Sanitizer {
316
+ public function sanitize() {
317
+ $body = $this->get_body_node();
318
+
319
+ // Build our amp-ad tag
320
+ $ad_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-ad', array(
321
+ // Taken from example at https://github.com/ampproject/amphtml/blob/master/builtins/amp-ad.md
322
+ 'width' => 300,
323
+ 'height' => 250,
324
+ 'type' => 'a9',
325
+ 'data-aax_size' => '300x250',
326
+ 'data-aax_pubname' => 'test123',
327
+ 'data-aax_src' => '302',
328
+ ) );
329
+
330
+ // Add a placeholder to show while loading
331
+ $fallback_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-img', array(
332
+ 'placeholder' => '',
333
+ 'layout' => 'fill',
334
+ 'src' => 'https://placehold.it/300X250',
335
+ ) );
336
+ $ad_node->appendChild( $fallback_node );
337
+
338
+ // If we have a lot of paragraphs, insert before the 4th one.
339
+ // Otherwise, add it to the end.
340
+ $p_nodes = $body->getElementsByTagName( 'p' );
341
+ if ( $p_nodes->length > 6 ) {
342
+ $p_nodes->item( 4 )->insertBefore( $ad_node );
343
+ } else {
344
+ $body->appendChild( $ad_node );
345
+ }
346
+ }
347
+ }
348
+ ```
349
+
350
+ #### Step 2: Load the Sanitizer
351
+
352
+ ```php
353
+ add_filter( 'amp_content_sanitizers', 'xyz_amp_add_ad_sanitizer', 10, 2 );
354
+
355
+ function xyz_amp_add_ad_sanitizer( $sanitizer_classes, $post ) {
356
+ require_once( dirname( __FILE__ ) . '/classes/class-ad-inject-sanitizer.php' );
357
+ $sanitizer_classes[ 'XYZ_AMP_Ad_Injection_Sanitizer' ] = array(); // the array can be used to pass args to your sanitizer and accessed within the class via `$this->args`
358
+ return $sanitizer_classes;
359
+ }
360
+ ```
readme.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === AMP ===
2
+ Contributors: batmoo, joen, automattic
3
+ Tags: amp, mobile
4
+ Requires at least: 4.4
5
+ Tested up to: 4.4
6
+ Stable tag: 0.2
7
+ License: GPLv2 or later
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Enable Accelerated Mobile Pages (AMP) on your WordPress site.
11
+
12
+ == Description ==
13
+
14
+ This plugin adds support for the [Accelerated Mobile Pages](https://www.ampproject.org) (AMP) Project, which is an an open source initiative that aims to provide mobile optimized content that can load instantly everywhere.
15
+
16
+ With the plugin active, all posts on your site will have dynamically generated AMP-compatible versions, accessible by appending `/amp/` to the end your permalinks. (If you do not have pretty permalinks enabled, you can do the same thing by appending `?amp=1`.)
17
+
18
+ Note that Pages and archives are not currently supported.
19
+
20
+ Follow along with or contribute to the development of this plugin at https://github.com/Automattic/amp-wp
21
+
22
+ == Installation ==
23
+
24
+ 1. Upload the folder to the `/wp-content/plugins/` directory
25
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
26
+
27
+ == Frequently Asked Questions ==
28
+
29
+ = How do I customize the AMP output for my site? =
30
+
31
+ You can find details about customization options at https://github.com/Automattic/amp-wp/blob/master/readme.md
32
+
33
+ == Changelog ==
34
+
35
+ = 0.2 (Jan 28, 2016) =
36
+
37
+ * Lots and lots and lots of compatibility and validation fixes
38
+ * Lots and lots and lots of improvements for customization
39
+
40
+ = 0.1 =
41
+ * Initial version
templates/meta-author.php ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <?php $post_author = $this->get( 'post_author' ); ?>
2
+ <li class="byline">
3
+ <amp-img src="<?php echo esc_url( get_avatar_url( $post_author->user_email, array(
4
+ 'size' => 24,
5
+ ) ) ); ?>" width="24" height="24" layout="fixed"></amp-img>
6
+ <span class="author"><?php echo esc_html( $post_author->display_name ); ?></span>
7
+ </li>
templates/meta-taxonomy.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php $categories = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'amp' ) ); ?>
2
+ <?php if ( $categories ) : ?>
3
+ <li class="tax-category">
4
+ <span class="screen-reader-text">Categories:</span>
5
+ <?php echo $categories; ?>
6
+ </li>
7
+ <?php endif; ?>
8
+
9
+ <?php $tags = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'amp' ) ); ?>
10
+ <?php if ( $tags ) : ?>
11
+ <li class="tax-tag">
12
+ <span class="screen-reader-text">Tags:</span>
13
+ <?php echo $tags; ?>
14
+ </li>
15
+ <?php endif; ?>
templates/meta-time.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <li class="posted-on">
2
+ <time datetime="<?php echo esc_attr( date( 'c', $this->get( 'post_publish_timestamp' ) ) ); ?>">
3
+ <?php
4
+ echo esc_html(
5
+ sprintf(
6
+ _x( '%s ago', '%s = human-readable time difference', 'amp' ),
7
+ human_time_diff( $this->get( 'post_publish_timestamp' ) )
8
+ )
9
+ );
10
+ ?>
11
+ </time>
12
+ </li>
templates/single.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html amp>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
6
+ <link href="https://fonts.googleapis.com/css?family=Merriweather:400,400italic,700,700italic|Open+Sans:400,700,400italic,700italic" rel="stylesheet" type="text/css">
7
+ <?php do_action( 'amp_post_template_head', $this ); ?>
8
+ <style>body {opacity: 0}</style><noscript><style>body {opacity: 1}</style></noscript>
9
+ <?php $this->load_parts( array( 'style' ) ); ?>
10
+ </head>
11
+ <body>
12
+ <nav class="title-bar">
13
+ <div>
14
+ <a href="<?php echo esc_url( $this->get( 'home_url' ) ); ?>">
15
+ <?php $site_icon_url = $this->get( 'site_icon_url' ); ?>
16
+ <?php if ( $site_icon_url ) : ?>
17
+ <amp-img src="<?php echo esc_url( $site_icon_url ); ?>" width="32" height="32" class="site-icon"></amp-img>
18
+ <?php else : ?>
19
+ <svg x="0px" y="0px" width="24" height="24" viewBox="0 0 24 24">
20
+ <path class="st0" d="M12,0C5.4,0,0,5.4,0,12c0,6.6,5.4,12,12,12c6.6,0,12-5.4,12-12C24,5.4,18.6,0,12,0z M1.2,12c0-1.6,0.3-3,0.9-4.4l5.1,14.1C3.7,20,1.2,16.3,1.2,12z M12,22.8c-1.1,0-2.1-0.2-3-0.4l3.2-9.4l3.3,9.1c0,0.1,0,0.1,0.1,0.1C14.5,22.6,13.3,22.8,12,22.8z M13.5,6.9c0.6,0,1.2-0.1,1.2-0.1c0.6-0.1,0.5-0.9-0.1-0.9c0,0-1.7,0.1-2.9,0.1c-1.1,0-2.8-0.1-2.8-0.1c-0.6,0-0.7,0.9-0.1,0.9c0,0,0.6,0.1,1.1,0.1l1.7,4.6l-2.4,7.1L5.4,6.9c0.7,0,1.2-0.1,1.2-0.1c0.6-0.1,0.5-0.9-0.1-0.9c0,0-1.7,0.1-2.9,0.1c-0.2,0-0.4,0-0.7,0c1.9-2.9,5.2-4.9,9-4.9c2.8,0,5.4,1.1,7.3,2.8c0,0-0.1,0-0.1,0c-1.1,0-1.8,0.9-1.8,1.9c0,0.9,0.5,1.6,1.1,2.5c0.4,0.7,0.9,1.6,0.9,3c0,0.9-0.3,2.1-0.8,3.5l-1.1,3.6L13.5,6.9z M17.4,21.3l3.3-9.5c0.6-1.5,0.8-2.8,0.8-3.9c0-0.4,0-0.8-0.1-1.1c0.8,1.5,1.3,3.3,1.3,5.2C22.8,16,20.6,19.5,17.4,21.3z"/>
21
+ </svg>
22
+ <?php endif; ?>
23
+ <?php echo esc_html( $this->get( 'blog_name' ) ); ?>
24
+ </a>
25
+ </div>
26
+ </nav>
27
+ <div class="content">
28
+ <h1 class="title"><?php echo esc_html( $this->get( 'post_title' ) ); ?></h1>
29
+ <ul class="meta">
30
+ <?php $this->load_parts( apply_filters( 'amp_post_template_meta_parts', array( 'meta-author', 'meta-time', 'meta-taxonomy' ) ) ); ?>
31
+ </ul>
32
+ <?php echo $this->get( 'post_amp_content' ); // amphtml content; no kses ?>
33
+ </div>
34
+ <?php do_action( 'amp_post_template_footer', $this ); ?>
35
+ </body>
36
+ </html>
templates/style.php ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <style amp-custom>
2
+ /* Generic WP styling */
3
+ amp-img.alignright { float: right; margin: 0 0 1em 1em; }
4
+ amp-img.alignleft { float: left; margin: 0 1em 1em 0; }
5
+ amp-img.aligncenter { display: block; margin-left: auto; margin-right: auto; }
6
+ .alignright { float: right; }
7
+ .alignleft { float: left; }
8
+ .aligncenter { display: block; margin-left: auto; margin-right: auto; }
9
+
10
+ .wp-caption.alignleft { margin-right: 1em; }
11
+ .wp-caption.alignright { margin-left: 1em; }
12
+
13
+ .amp-wp-enforced-sizes {
14
+ /** Our sizes fallback is 100vw, and we have a padding on the container; the max-width here prevents the element from overflowing. **/
15
+ max-width: 100%;
16
+ }
17
+
18
+ /* Generic WP.com reader style */
19
+ .content, .title-bar div {
20
+ max-width: <?php echo sprintf( '%dpx', absint( $this->get( 'content_max_width' ) ) ); ?>;
21
+ margin: 0 auto;
22
+ }
23
+
24
+ body {
25
+ font-family: 'Merriweather', Serif;
26
+ font-size: 16px;
27
+ line-height: 1.8;
28
+ background: #fff;
29
+ color: #3d596d;
30
+ padding-bottom: 100px;
31
+ }
32
+
33
+ .content {
34
+ padding: 16px;
35
+ overflow-wrap: break-word;
36
+ word-wrap: break-word;
37
+ font-weight: 400;
38
+ color: #3d596d;
39
+ }
40
+
41
+ .title {
42
+ margin: 36px 0 0 0;
43
+ font-size: 36px;
44
+ line-height: 1.258;
45
+ font-weight: 700;
46
+ color: #2e4453;
47
+ }
48
+
49
+ .meta {
50
+ margin-bottom: 16px;
51
+ }
52
+
53
+ p,
54
+ ol,
55
+ ul,
56
+ figure {
57
+ margin: 0 0 24px 0;
58
+ }
59
+
60
+ a,
61
+ a:visited {
62
+ color: #0087be;
63
+ }
64
+
65
+ a:hover,
66
+ a:active,
67
+ a:focus {
68
+ color: #33bbe3;
69
+ }
70
+
71
+
72
+ /* Open Sans */
73
+ .meta,
74
+ nav.title-bar,
75
+ .wp-caption-text {
76
+ font-family: "Open Sans", sans-serif;
77
+ font-size: 15px;
78
+ }
79
+
80
+
81
+ /* Meta */
82
+ ul.meta {
83
+ padding: 24px 0 0 0;
84
+ margin: 0 0 24px 0;
85
+ }
86
+
87
+ ul.meta li {
88
+ list-style: none;
89
+ display: inline-block;
90
+ margin: 0;
91
+ line-height: 24px;
92
+ white-space: nowrap;
93
+ overflow: hidden;
94
+ text-overflow: ellipsis;
95
+ max-width: 300px;
96
+ }
97
+
98
+ ul.meta li:before {
99
+ content: "\2022";
100
+ margin: 0 8px;
101
+ }
102
+
103
+ ul.meta li:first-child:before {
104
+ display: none;
105
+ }
106
+
107
+ .meta,
108
+ .meta a {
109
+ color: #4f748e;
110
+ }
111
+
112
+ .meta .screen-reader-text {
113
+ /* from twentyfifteen */
114
+ clip: rect(1px, 1px, 1px, 1px);
115
+ height: 1px;
116
+ overflow: hidden;
117
+ position: absolute;
118
+ width: 1px;
119
+ }
120
+
121
+ .byline amp-img {
122
+ border-radius: 50%;
123
+ border: 0;
124
+ background: #f3f6f8;
125
+ position: relative;
126
+ top: 6px;
127
+ margin-right: 6px;
128
+ }
129
+
130
+
131
+ /* Titlebar */
132
+ nav.title-bar {
133
+ background: #0a89c0;
134
+ padding: 0 16px;
135
+ }
136
+
137
+ nav.title-bar div {
138
+ line-height: 54px;
139
+ color: #fff;
140
+ }
141
+
142
+ nav.title-bar a {
143
+ color: #fff;
144
+ text-decoration: none;
145
+ }
146
+
147
+ nav.title-bar .site-icon {
148
+ /** site icon is 32px **/
149
+ float: left;
150
+ margin: 11px 8px 0 0;
151
+ border-radius: 50%;
152
+ }
153
+
154
+ nav.title-bar svg {
155
+ /** svg is 24px **/
156
+ fill: #fff;
157
+ float: left;
158
+ margin: 15px 8px 0 0;
159
+ }
160
+
161
+
162
+ /* Captions */
163
+ .wp-caption-text {
164
+ padding: 8px 16px;
165
+ font-style: italic;
166
+ }
167
+
168
+
169
+ /* Quotes */
170
+ blockquote {
171
+ padding: 16px;
172
+ margin: 8px 0 24px 0;
173
+ border-left: 2px solid #87a6bc;
174
+ color: #4f748e;
175
+ background: #e9eff3;
176
+ }
177
+
178
+ blockquote p:last-child {
179
+ margin-bottom: 0;
180
+ }
181
+
182
+ /* Other Elements */
183
+ amp-carousel {
184
+ background: #000;
185
+ }
186
+
187
+ amp-iframe,
188
+ amp-youtube,
189
+ amp-instagram,
190
+ amp-vine {
191
+ background: #f3f6f8;
192
+ }
193
+
194
+ .amp-wp-iframe-placeholder {
195
+ background: #f3f6f8 url( <?php echo esc_url( $this->get( 'placeholder_image_url' ) ); ?> ) no-repeat center 40%;
196
+ background-size: 48px 48px;
197
+ min-height: 48px;
198
+ }
199
+ </style>
wpcom-helper.php ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // WPCOM-specific things
4
+
5
+ define( 'AMP_DEV_MODE', defined( 'WPCOM_SANDBOXED' ) && WPCOM_SANDBOXED );
6
+
7
+ // Add stats pixel
8
+ add_filter( 'amp_post_content', function( $content, $post ) {
9
+ $urls = array(
10
+ wpcom_amp_get_pageview_url(),
11
+ wpcom_amp_get_mc_url(),
12
+ wpcom_amp_get_stats_extras_url(),
13
+ );
14
+
15
+ foreach ( $urls as $url ) {
16
+ if ( ! $url ) {
17
+ continue;
18
+ }
19
+ $content .= sprintf( '<amp-pixel src="%s">', esc_url( $url ) );
20
+ }
21
+
22
+ return $content;
23
+ }, 10, 2 );
24
+
25
+ function wpcom_amp_get_pageview_url() {
26
+ $stats_info = stats_collect_info();
27
+ $a = $stats_info['st_go_args'];
28
+
29
+ $url = add_query_arg( array(
30
+ 'rand' => '$RANDOM', // special amp placeholder
31
+ 'host' => rawurlencode( $_SERVER['HTTP_HOST'] ),
32
+ // TODO: ref; not reliably accessible server-side; flag as amp?
33
+ ), 'https://pixel.wp.com/b.gif' );
34
+ $url .= '&' . stats_array_string( $a );
35
+ return $url;
36
+ }
37
+
38
+ function wpcom_amp_get_mc_url() {
39
+ return add_query_arg( array(
40
+ 'rand' => '$RANDOM', // special amp placeholder
41
+ 'v' => 'wpcom-no-pv',
42
+ 'x_amp-views' => 'view',
43
+ ), 'https://pixel.wp.com/b.gif' );
44
+ }
45
+
46
+ function wpcom_amp_get_stats_extras_url() {
47
+ $stats_extras = stats_extras();
48
+ if ( ! $stats_extras ) {
49
+ return false;
50
+ }
51
+
52
+ $url = add_query_arg( array(
53
+ 'rand' => '$RANDOM', // special amp placeholder
54
+ 'v' => 'wpcom-no-pv',
55
+ ), 'https://pixel.wp.com/b.gif' );
56
+
57
+ $url .= '&' . stats_array_string( array(
58
+ 'crypt' => base64_encode(
59
+ wp_encrypt_plus(
60
+ ltrim(
61
+ add_query_arg( $stats_extras, ''),
62
+ '?'),
63
+ 8, 'url')
64
+ )
65
+ ) );
66
+
67
+ return $url;
68
+ }
69
+
70
+ add_action( 'pre_amp_render', function() {
71
+ add_filter( 'post_flair_disable', '__return_true', 99 );
72
+ remove_filter( 'the_title', 'widont' );
73
+
74
+ remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
75
+ remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
76
+ } );
77
+
78
+ add_action( 'post_amp_render', function() {
79
+ add_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
80
+ add_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
81
+ } );
82
+
83
+ add_action( 'amp_head', function( $amp_post ) {
84
+ if ( function_exists( 'jetpack_og_tags' ) ) {
85
+ jetpack_og_tags();
86
+ }
87
+ } );
88
+
89
+ add_filter( 'amp_post_metadata', function( $metadata, $post ) {
90
+ $metadata = wpcom_amp_add_blavatar( $metadata, $post );
91
+ return $metadata;
92
+ }, 10, 2 );
93
+
94
+ function wpcom_amp_add_blavatar( $metadata, $post ) {
95
+ if ( ! function_exists( 'blavatar_domain' ) ) {
96
+ return $metadata;
97
+ }
98
+
99
+ if ( ! isset( $metadata['publisher'] ) ) {
100
+ return $metadata;
101
+ }
102
+
103
+ if ( isset( $metadata['publisher']['logo'] ) ) {
104
+ return $metadata;
105
+ }
106
+
107
+ $size = 60;
108
+ $blavatar_domain = blavatar_domain( site_url() );
109
+ if ( blavatar_exists( $blavatar_domain ) ) {
110
+ $metadata['publisher']['logo'] = array(
111
+ '@type' => 'ImageObject',
112
+ 'url' => blavatar_url( $blavatar_domain, 'img', $size, false, true ),
113
+ 'width' => $size,
114
+ 'height' => $size,
115
+ );
116
+ }
117
+
118
+ return $metadata;
119
+ }
120
+
121
+ add_action( 'amp_extract_image_dimensions', 'wpcom_amp_extract_image_dimensions', 9, 2 ); // Hook in before the default extractors
122
+ function wpcom_amp_extract_image_dimensions( $dimensions, $url ) {
123
+ if ( $dimensions ) {
124
+ return $dimensions;
125
+ }
126
+
127
+ $host = parse_url( $url, PHP_URL_HOST );
128
+ if ( ! wp_endswith( $host, '.wp.com' ) || ! wp_endswith( $host, '.files.wordpress.com' ) ) {
129
+ return false;
130
+ }
131
+
132
+ $query = parse_url( $url, PHP_URL_QUERY );
133
+ $w = isset( $query['w'] ) ? absint( $query['w'] ) : false;
134
+ $h = isset( $query['h'] ) ? absint( $query['h'] ) : false;
135
+
136
+ if ( false !== $w && false !== $h ) {
137
+ return array( $w, $h );
138
+ }
139
+
140
+ return false;
141
+ }
142
+
143
+
144
+ add_action( 'amp_extract_image_dimensions', 'wpcom_amp_extract_image_dimensions_fallback', 100, 2 ); // Our last resort
145
+ function wpcom_amp_extract_image_dimensions_fallback( $dimensions, $url ) {
146
+ if ( $dimensions ) {
147
+ return $dimensions;
148
+ }
149
+
150
+ if ( ! function_exists( 'require_lib' ) ) {
151
+ return false;
152
+ }
153
+
154
+ require_lib( 'wpcom/imagesize' );
155
+ $size = wpcom_getimagesize( $url );
156
+ if ( $size ) {
157
+ return array( $size[0], $size[1] );
158
+ }
159
+
160
+ return false;
161
+ }