AMP for WordPress - Version 0.4.2

Version Description

(2016-10-13) =

  • Fix: Prevent validation errors for html tag (h/t Maxime2 and everyone else that reported this error)
  • Fix: Handle variable name conflict that was causing content_max_width to be ignored (h/t mimancillas)
  • Fix: Prevent errors when nodes don't have attributes (h/t stephenmax)
  • Fix: Back-compat for 4.5 (add sanitize_hex_color function, h/t xotihcan)
  • Fix: Handle gif featured images (h/t protocolil)
  • Documentation updates (props troyxmccall)
Download this release

Release Info

Developer philipjohn
Plugin Icon 128x128 AMP for WordPress
Version 0.4.2
Comparing to
See all releases

Code changes from version 0.4.1 to 0.4.2

amp.php CHANGED
@@ -5,7 +5,7 @@
5
  * Plugin URI: https://github.com/automattic/amp-wp
6
  * Author: Automattic
7
  * Author URI: https://automattic.com
8
- * Version: 0.4.1
9
  * Text Domain: amp
10
  * Domain Path: /languages/
11
  * License: GPLv2 or later
@@ -13,7 +13,7 @@
13
 
14
  define( 'AMP__FILE__', __FILE__ );
15
  define( 'AMP__DIR__', dirname( __FILE__ ) );
16
- define( 'AMP__VERSION', '0.4.1' );
17
 
18
  require_once( AMP__DIR__ . '/back-compat/back-compat.php' );
19
  require_once( AMP__DIR__ . '/includes/amp-helper-functions.php' );
@@ -113,6 +113,7 @@ function amp_add_frontend_actions() {
113
 
114
  function amp_add_post_template_actions() {
115
  require_once( AMP__DIR__ . '/includes/amp-post-template-actions.php' );
 
116
  }
117
 
118
  function amp_prepare_render() {
5
  * Plugin URI: https://github.com/automattic/amp-wp
6
  * Author: Automattic
7
  * Author URI: https://automattic.com
8
+ * Version: 0.4.2
9
  * Text Domain: amp
10
  * Domain Path: /languages/
11
  * License: GPLv2 or later
13
 
14
  define( 'AMP__FILE__', __FILE__ );
15
  define( 'AMP__DIR__', dirname( __FILE__ ) );
16
+ define( 'AMP__VERSION', '0.4.2' );
17
 
18
  require_once( AMP__DIR__ . '/back-compat/back-compat.php' );
19
  require_once( AMP__DIR__ . '/includes/amp-helper-functions.php' );
113
 
114
  function amp_add_post_template_actions() {
115
  require_once( AMP__DIR__ . '/includes/amp-post-template-actions.php' );
116
+ require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' );
117
  }
118
 
119
  function amp_prepare_render() {
includes/amp-post-template-functions.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Was only available in Customizer > 4.6
4
+ if ( ! function_exists( 'sanitize_hex_color' ) ) {
5
+ function sanitize_hex_color( $color ) {
6
+ if ( '' === $color ) {
7
+ return '';
8
+ }
9
+
10
+ // 3 or 6 hex digits, or the empty string.
11
+ if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
12
+ return $color;
13
+ }
14
+ }
15
+ }
includes/class-amp-content.php CHANGED
@@ -92,7 +92,7 @@ class AMP_Content {
92
  }
93
 
94
  class AMP_Content_Sanitizer {
95
- public static function sanitize( $content, $sanitizer_classes, $args = array() ) {
96
  $scripts = array();
97
  $styles = array();
98
  $dom = AMP_DOM_Utils::get_dom_from_content( $content );
@@ -103,7 +103,7 @@ class AMP_Content_Sanitizer {
103
  continue;
104
  }
105
 
106
- $sanitizer = new $sanitizer_class( $dom, array_merge( $args, $args ) );
107
 
108
  if ( ! is_subclass_of( $sanitizer, 'AMP_Base_Sanitizer' ) ) {
109
  _doing_it_wrong( __METHOD__, sprintf( __( 'Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp' ), esc_html( $sanitizer_class ) ), '0.1' );
92
  }
93
 
94
  class AMP_Content_Sanitizer {
95
+ public static function sanitize( $content, $sanitizer_classes, $global_args = array() ) {
96
  $scripts = array();
97
  $styles = array();
98
  $dom = AMP_DOM_Utils::get_dom_from_content( $content );
103
  continue;
104
  }
105
 
106
+ $sanitizer = new $sanitizer_class( $dom, array_merge( $global_args, $args ) );
107
 
108
  if ( ! is_subclass_of( $sanitizer, 'AMP_Base_Sanitizer' ) ) {
109
  _doing_it_wrong( __METHOD__, sprintf( __( 'Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp' ), esc_html( $sanitizer_class ) ), '0.1' );
includes/class-amp-post-template.php CHANGED
@@ -50,6 +50,8 @@ class AMP_Post_Template {
50
  'canonical_url' => get_permalink( $post_id ),
51
  'home_url' => home_url(),
52
  'blog_name' => get_bloginfo( 'name' ),
 
 
53
  'body_class' => '',
54
 
55
  'site_icon_url' => apply_filters( 'amp_site_icon_url', function_exists( 'get_site_icon_url' ) ? get_site_icon_url( self::SITE_ICON_SIZE ) : '' ),
@@ -84,6 +86,7 @@ class AMP_Post_Template {
84
  $this->build_post_content();
85
  $this->build_post_data();
86
  $this->build_customizer_settings();
 
87
 
88
  $this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
89
  }
@@ -198,7 +201,7 @@ class AMP_Post_Template {
198
 
199
  $comments_open = comments_open( $this->ID );
200
 
201
- // Don't show link if close and no comments
202
  if ( ! $comments_open
203
  && ! $this->post->comment_count ) {
204
  return;
@@ -267,7 +270,7 @@ class AMP_Post_Template {
267
 
268
  $featured_image = get_post( $featured_id );
269
 
270
- list( $sanitized_html ) = AMP_Content_Sanitizer::sanitize(
271
  $featured_html,
272
  array( 'AMP_Img_Sanitizer' => array() ),
273
  array(
@@ -279,6 +282,14 @@ class AMP_Post_Template {
279
  'amp_html' => $sanitized_html,
280
  'caption' => $featured_image->post_excerpt,
281
  ) );
 
 
 
 
 
 
 
 
282
  }
283
 
284
  private function build_customizer_settings() {
@@ -347,6 +358,21 @@ class AMP_Post_Template {
347
  return $post_image_meta;
348
  }
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
  private function verify_and_include( $file, $template_type ) {
351
  $located_file = $this->locate_template( $file );
352
  if ( $located_file ) {
50
  'canonical_url' => get_permalink( $post_id ),
51
  'home_url' => home_url(),
52
  'blog_name' => get_bloginfo( 'name' ),
53
+
54
+ 'html_tag_attributes' => array(),
55
  'body_class' => '',
56
 
57
  'site_icon_url' => apply_filters( 'amp_site_icon_url', function_exists( 'get_site_icon_url' ) ? get_site_icon_url( self::SITE_ICON_SIZE ) : '' ),
86
  $this->build_post_content();
87
  $this->build_post_data();
88
  $this->build_customizer_settings();
89
+ $this->build_html_tag_attributes();
90
 
91
  $this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
92
  }
201
 
202
  $comments_open = comments_open( $this->ID );
203
 
204
+ // Don't show link if close and no comments
205
  if ( ! $comments_open
206
  && ! $this->post->comment_count ) {
207
  return;
270
 
271
  $featured_image = get_post( $featured_id );
272
 
273
+ list( $sanitized_html, $featured_scripts, $featured_styles ) = AMP_Content_Sanitizer::sanitize(
274
  $featured_html,
275
  array( 'AMP_Img_Sanitizer' => array() ),
276
  array(
282
  'amp_html' => $sanitized_html,
283
  'caption' => $featured_image->post_excerpt,
284
  ) );
285
+
286
+ if ( $featured_scripts ) {
287
+ $this->merge_data_for_key( 'amp_component_scripts', $featured_scripts );
288
+ }
289
+
290
+ if ( $featured_styles ) {
291
+ $this->add_data_by_key( 'post_amp_styles', $featured_styles );
292
+ }
293
  }
294
 
295
  private function build_customizer_settings() {
358
  return $post_image_meta;
359
  }
360
 
361
+ private function build_html_tag_attributes() {
362
+ $attributes = array();
363
+
364
+ if ( function_exists( 'is_rtl' ) && is_rtl() ) {
365
+ $attributes['dir'] = 'rtl';
366
+ }
367
+
368
+ $lang = get_bloginfo( 'language' );
369
+ if ( $lang ) {
370
+ $attributes['lang'] = $lang;
371
+ }
372
+
373
+ $this->add_data_by_key( 'html_tag_attributes', $attributes );
374
+ }
375
+
376
  private function verify_and_include( $file, $template_type ) {
377
  $located_file = $this->locate_template( $file );
378
  if ( $located_file ) {
includes/utils/class-amp-dom-utils.php CHANGED
@@ -45,6 +45,10 @@ class AMP_DOM_Utils {
45
 
46
  public static function get_node_attributes_as_assoc_array( $node ) {
47
  $attributes = array();
 
 
 
 
48
  foreach ( $node->attributes as $attribute ) {
49
  $attributes[ $attribute->nodeName ] = $attribute->nodeValue;
50
  }
45
 
46
  public static function get_node_attributes_as_assoc_array( $node ) {
47
  $attributes = array();
48
+ if ( ! $node->hasAttributes() ) {
49
+ return $attributes;
50
+ }
51
+
52
  foreach ( $node->attributes as $attribute ) {
53
  $attributes[ $attribute->nodeName ] = $attribute->nodeValue;
54
  }
readme.md CHANGED
@@ -192,12 +192,12 @@ In `t/meta-custom-tax.php`, you can add something like the following to replace
192
  </li>
193
  ```
194
 
195
- ##### Example: Remove Author from `meta`
196
 
197
  This will completely remove the author section:
198
 
199
  ```php
200
- add_filter( 'amp_post_template_meta_parts', 'xyz_amp_remove_author_meta' );
201
 
202
  function xyz_amp_remove_author_meta( $meta_parts ) {
203
  foreach ( array_keys( $meta_parts, 'meta-author', true ) as $key ) {
@@ -207,12 +207,12 @@ function xyz_amp_remove_author_meta( $meta_parts ) {
207
  }
208
  ```
209
 
210
- ##### Example: Add Comment Count to `meta`
211
 
212
  This adds a new section to display the comment count:
213
 
214
  ```php
215
- add_filter( 'amp_post_template_meta_parts', 'xyz_amp_add_comment_count_meta' );
216
 
217
  function xyz_amp_add_comment_count_meta( $meta_parts ) {
218
  $meta_parts[] = 'xyz-meta-comment-count';
192
  </li>
193
  ```
194
 
195
+ ##### Example: Remove Author from `header_meta`
196
 
197
  This will completely remove the author section:
198
 
199
  ```php
200
+ add_filter( 'amp_post_article_header_meta', 'xyz_amp_remove_author_meta' );
201
 
202
  function xyz_amp_remove_author_meta( $meta_parts ) {
203
  foreach ( array_keys( $meta_parts, 'meta-author', true ) as $key ) {
207
  }
208
  ```
209
 
210
+ ##### Example: Add Comment Count to `footer_meta`
211
 
212
  This adds a new section to display the comment count:
213
 
214
  ```php
215
+ add_filter( 'amp_post_article_footer_meta', 'xyz_amp_add_comment_count_meta' );
216
 
217
  function xyz_amp_add_comment_count_meta( $meta_parts ) {
218
  $meta_parts[] = 'xyz-meta-comment-count';
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: batmoo, joen, automattic, potatomaster
3
  Tags: amp, mobile
4
  Requires at least: 4.4
5
- Tested up to: 4.6
6
- Stable tag: 0.4.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -25,7 +25,7 @@ Follow along with or contribute to the development of this plugin at https://git
25
 
26
  1. Upload the folder to the `/wp-content/plugins/` directory
27
  1. Activate the plugin through the 'Plugins' menu in WordPress
28
- 1. You may need to refresh your permalinks by going to `Settings > Permalinks` and tapping the `Save` button.
29
 
30
  == Frequently Asked Questions ==
31
 
@@ -53,7 +53,16 @@ A wise green Yoda once said, "Patience you must have, my young padawan." We're w
53
 
54
  == Changelog ==
55
 
56
- = 0.4.1 (2016-10-10 =
 
 
 
 
 
 
 
 
 
57
 
58
  - Fix: Don't fire the_content for featured image output
59
  - Fix: Don't show comment link when disabled and no comments on post (h/t neotrope)
2
  Contributors: batmoo, joen, automattic, potatomaster
3
  Tags: amp, mobile
4
  Requires at least: 4.4
5
+ Tested up to: 4.8
6
+ Stable tag: 0.4.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
25
 
26
  1. Upload the folder to the `/wp-content/plugins/` directory
27
  1. Activate the plugin through the 'Plugins' menu in WordPress
28
+ 1. You may need to refresh your permalinks by going to `Settings > Permalinks` and tapping the `Save` button.
29
 
30
  == Frequently Asked Questions ==
31
 
53
 
54
  == Changelog ==
55
 
56
+ = 0.4.2 (2016-10-13) =
57
+
58
+ - Fix: Prevent validation errors for `html` tag (h/t Maxime2 and everyone else that reported this error)
59
+ - Fix: Handle variable name conflict that was causing content_max_width to be ignored (h/t mimancillas)
60
+ - Fix: Prevent errors when nodes don't have attributes (h/t stephenmax)
61
+ - Fix: Back-compat for 4.5 (add sanitize_hex_color function, h/t xotihcan)
62
+ - Fix: Handle gif featured images (h/t protocolil)
63
+ - Documentation updates (props troyxmccall)
64
+
65
+ = 0.4.1 (2016-10-10) =
66
 
67
  - Fix: Don't fire the_content for featured image output
68
  - Fix: Don't show comment link when disabled and no comments on post (h/t neotrope)
templates/single.php CHANGED
@@ -1,5 +1,5 @@
1
  <!doctype html>
2
- <html amp <?php language_attributes(); ?>>
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">
1
  <!doctype html>
2
+ <html amp <?php echo AMP_HTML_Utils::build_attributes_string( $this->get( 'html_tag_attributes' ) ); ?>>
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">