Glue for Yoast SEO & AMP - Version 0.3.3

Version Description

  • Bugfixes:
    • Fixes bug where AMP was only activated for the first post type in the list
    • Made sure that the function is not declared multiple times
Download this release

Release Info

Developer jipmoors
Plugin Icon 128x128 Glue for Yoast SEO & AMP
Version 0.3.3
Comparing to
See all releases

Code changes from version 0.3.2 to 0.3.3

classes/class-backend.php CHANGED
@@ -107,6 +107,8 @@ if ( ! class_exists( 'YoastSEO_AMP_Backend', false ) ) {
107
  *
108
  * @param string $var
109
  * @param string $label
 
 
110
  */
111
  private function color_picker( $var, $label ) {
112
  echo '<label class="checkbox" for="', esc_attr( $var ), '">', esc_html( $label ), '</label>';
107
  *
108
  * @param string $var
109
  * @param string $label
110
+ *
111
+ * @SuppressWarnings("PMD.UnusedPrivateMethod") // As this is used in a view
112
  */
113
  private function color_picker( $var, $label ) {
114
  echo '<label class="checkbox" for="', esc_attr( $var ), '">', esc_html( $label ), '</label>';
classes/class-frontend.php CHANGED
@@ -108,21 +108,25 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
108
  public function post_types() {
109
  $post_types = get_post_types( array( 'public' => true ), 'objects' );
110
  if ( is_array( $post_types ) && $post_types !== array() ) {
111
- foreach ( $post_types as $pt ) {
112
- if ( ! isset( $this->options[ 'post_types-' . $pt->name . '-amp' ] ) ) {
 
 
 
113
  continue;
114
  }
115
- if ( $this->options[ 'post_types-' . $pt->name . '-amp' ] === 'on' ) {
116
- add_post_type_support( $pt->name, AMP_QUERY_VAR );
 
 
117
  }
118
- else {
119
- if ( 'post' === $pt->name ) {
120
- add_action( 'wp', array( $this, 'disable_amp_for_posts' ) );
121
- }
122
- else {
123
- remove_post_type_support( $pt->name, AMP_QUERY_VAR );
124
- }
125
  }
 
 
126
  }
127
  }
128
  }
@@ -315,12 +319,10 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
315
  * @return string
316
  */
317
  private function get_post_schema_type( $post ) {
 
318
  if ( 'post' === $post->post_type ) {
319
  $type = 'Article';
320
  }
321
- else {
322
- $type = 'WebPage';
323
- }
324
 
325
  /**
326
  * Filter: 'yoastseo_amp_schema_type' - Allow changing the Schema.org type for the post
108
  public function post_types() {
109
  $post_types = get_post_types( array( 'public' => true ), 'objects' );
110
  if ( is_array( $post_types ) && $post_types !== array() ) {
111
+ foreach ( $post_types as $post_type ) {
112
+
113
+ $post_type_name = $post_type->name;
114
+
115
+ if ( ! isset( $this->options[ 'post_types-' . $post_type_name . '-amp' ] ) ) {
116
  continue;
117
  }
118
+
119
+ if ( $this->options[ 'post_types-' . $post_type_name . '-amp' ] === 'on' ) {
120
+ add_post_type_support( $post_type_name, AMP_QUERY_VAR );
121
+ continue;
122
  }
123
+
124
+ if ( 'post' === $post_type_name ) {
125
+ add_action( 'wp', array( $this, 'disable_amp_for_posts' ) );
126
+ continue;
 
 
 
127
  }
128
+
129
+ remove_post_type_support( $post_type_name, AMP_QUERY_VAR );
130
  }
131
  }
132
  }
319
  * @return string
320
  */
321
  private function get_post_schema_type( $post ) {
322
+ $type = 'WebPage';
323
  if ( 'post' === $post->post_type ) {
324
  $type = 'Article';
325
  }
 
 
 
326
 
327
  /**
328
  * Filter: 'yoastseo_amp_schema_type' - Allow changing the Schema.org type for the post
classes/class-options.php CHANGED
@@ -61,9 +61,106 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
61
  public function sanitize_options( $options ) {
62
  $options['version'] = 1;
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  return $options;
65
  }
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  /**
68
  * Get the options
69
  *
@@ -90,13 +187,12 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
90
 
91
  /**
92
  * Collect options
 
 
93
  */
94
  private function fetch_options() {
95
-
96
- if ( isset( $this->options ) ) {
97
- $saved_options = $this->options;
98
- }
99
- else {
100
  $saved_options = get_option( 'wpseo_amp' );
101
 
102
  // Apply defaults.
@@ -117,17 +213,16 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
117
  */
118
  private function update_post_type_settings() {
119
  $post_type_names = array();
120
- $post_types = get_post_types( array( 'public' => true ), 'objects' );
 
121
 
122
  if ( is_array( $post_types ) && $post_types !== array() ) {
123
  foreach ( $post_types as $post_type ) {
124
  if ( ! isset( $this->options[ 'post_types-' . $post_type->name . '-amp' ] ) ) {
 
125
  if ( 'post' === $post_type->name ) {
126
  $this->options[ 'post_types-' . $post_type->name . '-amp' ] = 'on';
127
  }
128
- else {
129
- $this->options[ 'post_types-' . $post_type->name . '-amp' ] = 'off';
130
- }
131
  }
132
 
133
  $post_type_names[] = $post_type->name;
61
  public function sanitize_options( $options ) {
62
  $options['version'] = 1;
63
 
64
+ // Sanitize extra CSS field.
65
+ $extra_css = strip_tags( $options['extra-css'] );
66
+ $extra_css = wp_check_invalid_utf8( $extra_css );
67
+ $extra_css = _wp_specialchars( $extra_css, ENT_NOQUOTES );
68
+ $options['extra-css'] = $extra_css;
69
+
70
+ // Only allow meta and link tags in head.
71
+ $options['extra-head'] = strip_tags( $options['extra-head'], '<link><meta>' );
72
+
73
+ $colors = array(
74
+ 'header-color',
75
+ 'headings-color',
76
+ 'text-color',
77
+ 'meta-color',
78
+ 'link-color',
79
+ 'blockquote-text-color',
80
+ 'blockquote-bg-color',
81
+ 'blockquote-border-color',
82
+ );
83
+
84
+ foreach ( $colors as $color ) {
85
+ $options[ $color ] = $this->sanitize_color( $options[ $color ], '' );
86
+ }
87
+
88
+ // Only allow 'on' or 'off'
89
+ foreach ( $options as $key => $value ) {
90
+ if ( 'post_types-' === substr( $key, 0, 11 ) ) {
91
+ $options[ $key ] = ( $value === 'on' ) ? 'on' : 'off';
92
+ }
93
+ }
94
+
95
+ $options['analytics-extra'] = $this->sanitize_analytics_code( $options['analytics-extra'] );
96
+
97
  return $options;
98
  }
99
 
100
+ /**
101
+ * Sanitize hexadecimal color
102
+ *
103
+ * @param string $color String to test for valid color.
104
+ * @param string $default Value the string will get when no color is found.
105
+ *
106
+ * @return string Color or $default
107
+ */
108
+ private function sanitize_color( $color, $default ) {
109
+ if ( preg_match( '~^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$~', $color, $matches ) ) {
110
+ return $matches[0];
111
+ }
112
+
113
+ return $default;
114
+ }
115
+
116
+ /**
117
+ * @param string $source Raw input.
118
+ *
119
+ * @return string Sanitized code.
120
+ */
121
+ private function sanitize_analytics_code( $source ) {
122
+
123
+ $source = trim( $source );
124
+
125
+ if ( empty( $source ) ) {
126
+ return $source;
127
+ }
128
+
129
+ $code = $source;
130
+
131
+ // Strip all tags, to verify JSON input.
132
+ $json = strip_tags( $code );
133
+ $parsed_json = json_decode( $json, true );
134
+
135
+ // Non-parsable JSON is always bad.
136
+ if ( is_null( $parsed_json ) ) {
137
+ return '';
138
+ }
139
+
140
+ $allowed_tags = strip_tags( $code, '<amp-analytics>' );
141
+
142
+ // Strip JSON content so we can apply verified script tag.
143
+ $tag = str_replace( $json, '', $allowed_tags );
144
+
145
+ // If the tag doesn't occur in the code, the code is invalid.
146
+ if ( false === strpos( $allowed_tags, '<amp-analytics' ) ) {
147
+ return '';
148
+ }
149
+
150
+ $parts = explode( '><', $tag );
151
+ $parts[0] .= '>';
152
+ $parts[1] = '<' . $parts[1];
153
+
154
+ // Rebuild with script tag and json content.
155
+ array_splice( $parts, 1, null, array(
156
+ '<script type="application/json">',
157
+ trim( $json ),
158
+ '</script>'
159
+ ) );
160
+
161
+ return implode( "\n", $parts );
162
+ }
163
+
164
  /**
165
  * Get the options
166
  *
187
 
188
  /**
189
  * Collect options
190
+ *
191
+ * @SuppressWarnings("PMD.UnusedPrivateMethod")
192
  */
193
  private function fetch_options() {
194
+ $saved_options = $this->options;
195
+ if ( ! is_array( $this->options ) ) {
 
 
 
196
  $saved_options = get_option( 'wpseo_amp' );
197
 
198
  // Apply defaults.
213
  */
214
  private function update_post_type_settings() {
215
  $post_type_names = array();
216
+
217
+ $post_types = get_post_types( array( 'public' => true ), 'objects' );
218
 
219
  if ( is_array( $post_types ) && $post_types !== array() ) {
220
  foreach ( $post_types as $post_type ) {
221
  if ( ! isset( $this->options[ 'post_types-' . $post_type->name . '-amp' ] ) ) {
222
+ $this->options[ 'post_types-' . $post_type->name . '-amp' ] = 'off';
223
  if ( 'post' === $post_type->name ) {
224
  $this->options[ 'post_types-' . $post_type->name . '-amp' ] = 'on';
225
  }
 
 
 
226
  }
227
 
228
  $post_type_names[] = $post_type->name;
classes/class-sanitizer.php CHANGED
@@ -103,10 +103,8 @@ class Yoast_AMP_Blacklist_Sanitizer extends AMP_Base_Sanitizer {
103
  private function sanitize_a_attribute( $node, $attribute ) {
104
  $attribute_name = strtolower( $attribute->name );
105
 
106
- if ( 'rel' === $attribute_name ) {
107
- if ( 'nofollow' !== $attribute->value ) {
108
- $node->removeAttribute( $attribute_name );
109
- }
110
  }
111
  }
112
 
103
  private function sanitize_a_attribute( $node, $attribute ) {
104
  $attribute_name = strtolower( $attribute->name );
105
 
106
+ if ( 'rel' === $attribute_name && 'nofollow' !== $attribute->value ) {
107
+ $node->removeAttribute( $attribute_name );
 
 
108
  }
109
  }
110
 
classes/views/admin-page.php CHANGED
@@ -38,8 +38,13 @@ $yform->admin_header( true, 'wpseo_amp', false, 'wpseo_amp_settings' );
38
  );
39
  }
40
  }
41
-
42
  ?>
 
 
 
 
 
 
43
  </div>
44
 
45
  <div id="design" class="wpseotab">
38
  );
39
  }
40
  }
 
41
  ?>
42
+
43
+ <br>
44
+ <strong><?php echo esc_html( __( 'Please note:', 'wordpress-seo' ) ); ?></strong>
45
+ <?php echo esc_html( __( 'Currently pages are not supported by the AMP plugin.', 'wordpress-seo' ) ); ?>
46
+ <?php echo esc_html( __( 'We still show this option to be able to provide functionality when the AMP plugin implements support for them.', 'wordpress-seo' ) ); ?>
47
+ </p>
48
  </div>
49
 
50
  <div id="design" class="wpseotab">
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: joostdevalk
3
  Tags: AMP, SEO
4
  Requires at least: 4.2
5
- Tested up to: 4.4
6
- Stable tag: 0.3.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -11,8 +11,12 @@ This plugin makes sure the default WordPress AMP plugin uses the proper Yoast SE
11
 
12
  == Description ==
13
 
14
- This plugin makes sure the default [WordPress AMP plugin](https://wordpress.org/plugins/amp/) uses the proper [Yoast SEO](https://wordpress.org/plugins/wordpress-seo/) metadata. Without this glue plugin things like canonical might go wrong.
15
 
 
 
 
 
16
  To change your AMP page design, go to SEO -> AMP, and look at the design tab.
17
 
18
  == Installation ==
@@ -30,6 +34,11 @@ To change your AMP page design, go to SEO -> AMP, and look at the design tab.
30
 
31
  == Changelog ==
32
 
 
 
 
 
 
33
  = 0.3.2 =
34
  * Bugfixes:
35
  * Fixed underline setting that wasn't working.
2
  Contributors: joostdevalk
3
  Tags: AMP, SEO
4
  Requires at least: 4.2
5
+ Tested up to: 4.6
6
+ Stable tag: 0.3.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
11
 
12
  == Description ==
13
 
14
+ Google is currently working on the "Accelerated Mobile Pages" or AMP project. The [WordPress AMP plugin](https://wordpress.org/plugins/amp/) provides the technical implementation of the AMP specifications. This makes sure that your pages contain valid AMP code.
15
 
16
+ The Yoast SEO: AMP Glue plugin integrates [Yoast SEO](https://wordpress.org/plugins/wordpress-seo/) into your AMP pages. This makes sure your meta-data is implemented correctly.
17
+
18
+ Next to the technical SEO improvements, the Yoast SEO: AMP Glue plugin also provides a simple way to customize your AMP pages.
19
+ It adds rudimental styling in the form of colors and link styles, so your AMP pages can maintain the feeling your main pages have.
20
  To change your AMP page design, go to SEO -> AMP, and look at the design tab.
21
 
22
  == Installation ==
34
 
35
  == Changelog ==
36
 
37
+ = 0.3.3 =
38
+ * Bugfixes:
39
+ * Fixes bug where AMP was only activated for the first post type in the list
40
+ * Made sure that the function is not declared multiple times
41
+
42
  = 0.3.2 =
43
  * Bugfixes:
44
  * Fixed underline setting that wasn't working.
yoastseo-amp.php CHANGED
@@ -7,11 +7,11 @@
7
  *
8
  * @wordpress-plugin
9
  * Plugin Name: Glue for Yoast SEO & AMP
10
- * Plugin URI: https://wordpress.org/plugins/glue-for-yoast-seo-amp/
11
  * Description: Makes sure the default WordPress AMP plugin uses the proper Yoast SEO metadata
12
- * Version: 0.3.2
13
- * Author: Joost de Valk
14
- * Author URI: https://yoast.com
15
  */
16
 
17
  if ( ! class_exists( 'YoastSEO_AMP', false ) ) {
@@ -30,24 +30,26 @@ if ( ! class_exists( 'YoastSEO_AMP', false ) ) {
30
  if ( is_admin() ) {
31
  require 'classes/class-backend.php';
32
  new YoastSEO_AMP_Backend();
 
33
  }
34
- else {
35
- require 'classes/class-build-css.php';
36
- require 'classes/class-frontend.php';
37
- new YoastSEO_AMP_Frontend();
38
- }
39
  }
40
 
41
  }
42
  }
43
 
44
- /**
45
- * Initialize the Yoast SEO AMP Glue plugin
46
- */
47
- function yoast_seo_amp_glue_init() {
48
- if ( defined( 'WPSEO_FILE' ) && defined( 'AMP__FILE__' ) ) {
49
- new YoastSEO_AMP();
 
 
50
  }
51
- }
52
 
53
- add_action( 'init', 'yoast_seo_amp_glue_init', 9 );
 
7
  *
8
  * @wordpress-plugin
9
  * Plugin Name: Glue for Yoast SEO & AMP
10
+ * Plugin URI: https://wordpress.org/plugins/glue-for-yoast-seo-amp/
11
  * Description: Makes sure the default WordPress AMP plugin uses the proper Yoast SEO metadata
12
+ * Version: 0.3.3
13
+ * Author: Joost de Valk
14
+ * Author URI: https://yoast.com
15
  */
16
 
17
  if ( ! class_exists( 'YoastSEO_AMP', false ) ) {
30
  if ( is_admin() ) {
31
  require 'classes/class-backend.php';
32
  new YoastSEO_AMP_Backend();
33
+ return;
34
  }
35
+
36
+ require 'classes/class-build-css.php';
37
+ require 'classes/class-frontend.php';
38
+ new YoastSEO_AMP_Frontend();
 
39
  }
40
 
41
  }
42
  }
43
 
44
+ if ( ! function_exists('yoast_seo_amp_glue_init' ) ) {
45
+ /**
46
+ * Initialize the Yoast SEO AMP Glue plugin
47
+ */
48
+ function yoast_seo_amp_glue_init() {
49
+ if ( defined( 'WPSEO_FILE' ) && defined( 'AMP__FILE__' ) ) {
50
+ new YoastSEO_AMP();
51
+ }
52
  }
 
53
 
54
+ add_action( 'init', 'yoast_seo_amp_glue_init', 9 );
55
+ }