Glue for Yoast SEO & AMP - Version 0.3.1

Version Description

  • Bugfixes:
    • Fixed bug where featured image wouldn't be used properly anymore.
    • Fixed bug where CSS in Extra CSS field could be wrongly escaped.
    • Fixed bug where wrong hook was used to add_post_type_support, causing integration issues.
    • Fixed bug where post type settings wouldn't save properly.
  • Enhancement:
    • Added some more escaping to color picker functionality.
    • Made sure no notice is thrown on frontend when post type setting isn't available.
Download this release

Release Info

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

Code changes from version 0.3 to 0.3.1

classes/class-backend.php CHANGED
@@ -103,16 +103,18 @@ if ( ! class_exists( 'YoastSEO_AMP_Backend', false ) ) {
103
  }
104
 
105
  /**
 
 
106
  * @param string $var
107
  * @param string $label
108
  */
109
  private function color_picker( $var, $label ) {
110
- echo '<label class="checkbox" for="', $var, '">', $label, '</label>';
111
- echo '<input type="text" name="wpseo_amp[', $var, ']"';
112
  if ( isset( $this->options[ $var ] ) ) {
113
- echo ' value="' . $this->options[ $var ] . '"';
114
  }
115
- echo ' class="yst_colorpicker" id="', $var, '"/>';
116
  echo '<br/>';
117
  }
118
  }
103
  }
104
 
105
  /**
106
+ * Render a color picker
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>';
113
+ echo '<input type="text" name="wpseo_amp[', esc_attr( $var ), ']"';
114
  if ( isset( $this->options[ $var ] ) ) {
115
+ echo ' value="' . esc_attr( $this->options[ $var ] ) . '"';
116
  }
117
+ echo ' class="yst_colorpicker" id="', esc_attr( $var ), '"/>';
118
  echo '<br/>';
119
  }
120
  }
classes/class-frontend.php CHANGED
@@ -33,7 +33,7 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
33
  public function __construct() {
34
  $this->set_options();
35
 
36
- add_action( 'wp', array( $this, 'post_types' ) );
37
 
38
  add_action( 'amp_post_template_css', array( $this, 'additional_css' ) );
39
  add_action( 'amp_post_template_head', array( $this, 'extra_head' ) );
@@ -85,8 +85,7 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
85
 
86
  $analytics['yst-googleanalytics'] = array(
87
  'type' => 'googleanalytics',
88
- 'attributes' => array(
89
- ),
90
  'config_data' => array(
91
  'vars' => array(
92
  'account' => $UA
@@ -110,16 +109,31 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
110
  $post_types = get_post_types( array( 'public' => true ), 'objects' );
111
  if ( is_array( $post_types ) && $post_types !== array() ) {
112
  foreach ( $post_types as $pt ) {
 
 
 
113
  if ( $this->options[ 'post_types-' . $pt->name . '-amp' ] === 'on' ) {
114
  add_post_type_support( $pt->name, AMP_QUERY_VAR );
115
  }
116
  else {
117
- remove_post_type_support( $pt->name, AMP_QUERY_VAR );
 
 
 
 
 
118
  }
119
  }
120
  }
121
  }
122
 
 
 
 
 
 
 
 
123
  /**
124
  * Fix the basic AMP post data
125
  *
@@ -159,7 +173,9 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
159
  $metadata['description'] = $desc;
160
  }
161
 
162
- $metadata['image'] = $this->get_image( $post );
 
 
163
  $metadata['@type'] = $this->get_post_schema_type( $post );
164
 
165
  return $metadata;
@@ -188,7 +204,12 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
188
 
189
  echo $css_builder->build();
190
 
191
- echo esc_html( $this->options['extra-css'] );
 
 
 
 
 
192
  }
193
 
194
  /**
@@ -207,7 +228,7 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
207
 
208
  do_action( 'wpseo_opengraph' );
209
 
210
- echo strip_tags($this->options['extra-head'], '<link><meta>');
211
  }
212
 
213
  /**
@@ -267,16 +288,20 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
267
  /**
268
  * Retrieve the Schema.org image for the post
269
  *
270
- * @param WP_Post $post
 
271
  *
272
- * @return array|false
273
  */
274
- private function get_image( $post ) {
275
- $image = $this->get_image_object( WPSEO_Meta::get_value( 'opengraph-image', $post->ID ) );
 
 
 
276
 
277
  // Posts without an image fail validation in Google, leading to Search Console errors
278
  if ( ! is_array( $image ) && isset( $this->options['default_image'] ) ) {
279
- $image = $this->get_image_object( $this->options['default_image'] );
280
  }
281
 
282
  return $image;
@@ -301,6 +326,7 @@ if ( ! class_exists( 'YoastSEO_AMP_Frontend' ) ) {
301
  * Filter: 'yoastseo_amp_schema_type' - Allow changing the Schema.org type for the post
302
  *
303
  * @api string $type The Schema.org type for the $post
 
304
  * @param WP_Post $post
305
  */
306
  $type = apply_filters( 'yoastseo_amp_schema_type', $type, $post );
33
  public function __construct() {
34
  $this->set_options();
35
 
36
+ add_action( 'amp_init', array( $this, 'post_types' ) );
37
 
38
  add_action( 'amp_post_template_css', array( $this, 'additional_css' ) );
39
  add_action( 'amp_post_template_head', array( $this, 'extra_head' ) );
85
 
86
  $analytics['yst-googleanalytics'] = array(
87
  'type' => 'googleanalytics',
88
+ 'attributes' => array(),
 
89
  'config_data' => array(
90
  'vars' => array(
91
  'account' => $UA
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
  }
129
 
130
+ /**
131
+ * Disables AMP for posts specifically, run later because of AMP plugin internals
132
+ */
133
+ public function disable_amp_for_posts() {
134
+ remove_post_type_support( 'post', AMP_QUERY_VAR );
135
+ }
136
+
137
  /**
138
  * Fix the basic AMP post data
139
  *
173
  $metadata['description'] = $desc;
174
  }
175
 
176
+ $image = isset( $metadata['image'] ) ? $metadata['image'] : null;
177
+
178
+ $metadata['image'] = $this->get_image( $post, $image );
179
  $metadata['@type'] = $this->get_post_schema_type( $post );
180
 
181
  return $metadata;
204
 
205
  echo $css_builder->build();
206
 
207
+ if ( ! empty( $this->options['extra-css'] ) ) {
208
+ $safe_text = strip_tags($this->options['extra-css']);
209
+ $safe_text = wp_check_invalid_utf8( $safe_text );
210
+ $safe_text = _wp_specialchars( $safe_text, ENT_NOQUOTES );
211
+ echo $safe_text;
212
+ }
213
  }
214
 
215
  /**
228
 
229
  do_action( 'wpseo_opengraph' );
230
 
231
+ echo strip_tags( $this->options['extra-head'], '<link><meta>' );
232
  }
233
 
234
  /**
288
  /**
289
  * Retrieve the Schema.org image for the post
290
  *
291
+ * @param WP_Post $post
292
+ * @param array|null $image The currently set post image
293
  *
294
+ * @return array
295
  */
296
+ private function get_image( $post, $image ) {
297
+ $og_image = $this->get_image_object( WPSEO_Meta::get_value( 'opengraph-image', $post->ID ) );
298
+ if ( is_array( $og_image ) ) {
299
+ return $og_image;
300
+ }
301
 
302
  // Posts without an image fail validation in Google, leading to Search Console errors
303
  if ( ! is_array( $image ) && isset( $this->options['default_image'] ) ) {
304
+ return $this->get_image_object( $this->options['default_image'] );
305
  }
306
 
307
  return $image;
326
  * Filter: 'yoastseo_amp_schema_type' - Allow changing the Schema.org type for the post
327
  *
328
  * @api string $type The Schema.org type for the $post
329
+ *
330
  * @param WP_Post $post
331
  */
332
  $type = apply_filters( 'yoastseo_amp_schema_type', $type, $post );
classes/class-options.php CHANGED
@@ -18,27 +18,27 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
18
 
19
  /** @var array Option defaults */
20
  private $defaults = array(
21
- 'version' => 1,
22
- 'amp_site_icon' => '',
23
- 'default_image' => '',
24
- 'header-color' => '',
25
- 'headings-color' => '',
26
- 'text-color' => '',
27
- 'meta-color' => '',
28
- 'link-color' => '',
29
- 'link-color-hover' => '',
30
- 'underline' => 'underline',
31
- 'blockquote-text-color' => '',
32
- 'blockquote-bg-color' => '',
33
  'blockquote-border-color' => '',
34
- 'extra-css' => '',
35
- 'extra-head' => '',
36
- 'analytics-extra' => '',
37
  );
38
 
39
  /** @var self Class instance */
40
  private static $instance;
41
-
42
  private function __construct() {
43
  // Register settings
44
  add_action( 'admin_init', array( $this, 'register_settings' ) );
@@ -66,17 +66,14 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
66
 
67
  /**
68
  * Get the options
69
- *
70
  * @return array
71
  */
72
  public static function get() {
73
-
74
  $me = self::get_instance();
75
-
76
- if ( ! isset( $me->options ) ) {
77
- $me->fetch_options();
78
- }
79
-
80
  return $me->options;
81
  }
82
 
@@ -87,7 +84,7 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
87
  if ( ! isset( self::$instance ) ) {
88
  self::$instance = new self();
89
  }
90
-
91
  return self::$instance;
92
  }
93
 
@@ -95,11 +92,16 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
95
  * Collect options
96
  */
97
  private function fetch_options() {
98
-
99
- $saved_options = get_option( 'wpseo_amp' );
100
 
101
- // Apply defaults.
102
- $this->options = wp_parse_args( $saved_options, $this->defaults );
 
 
 
 
 
 
 
103
 
104
  // Make sure all post types are present.
105
  $this->update_post_type_settings();
@@ -115,14 +117,15 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
115
  */
116
  private function update_post_type_settings() {
117
  $post_type_names = array();
118
- $post_types = get_post_types( array( 'public' => true ), 'objects' );
119
 
120
  if ( is_array( $post_types ) && $post_types !== array() ) {
121
  foreach ( $post_types as $post_type ) {
122
  if ( ! isset( $this->options[ 'post_types-' . $post_type->name . '-amp' ] ) ) {
123
  if ( 'post' === $post_type->name ) {
124
  $this->options[ 'post_types-' . $post_type->name . '-amp' ] = 'on';
125
- } else {
 
126
  $this->options[ 'post_types-' . $post_type->name . '-amp' ] = 'off';
127
  }
128
  }
@@ -130,17 +133,6 @@ if ( ! class_exists( 'YoastSEO_AMP_Options' ) ) {
130
  $post_type_names[] = $post_type->name;
131
  }
132
  }
133
-
134
- // Remove missing post types.
135
- foreach ( $this->options as $key => $value ) {
136
- if ( 0 === strpos( $key, 'post_types' ) ) {
137
- $post_type = substr( substr( $key, 11 ), 0, -4 );
138
- if ( ! in_array( $post_type, $post_type_names ) ) {
139
- unset ( $this->options[ $key ] );
140
- }
141
- }
142
- }
143
  }
144
  }
145
-
146
  }
18
 
19
  /** @var array Option defaults */
20
  private $defaults = array(
21
+ 'version' => 1,
22
+ 'amp_site_icon' => '',
23
+ 'default_image' => '',
24
+ 'header-color' => '',
25
+ 'headings-color' => '',
26
+ 'text-color' => '',
27
+ 'meta-color' => '',
28
+ 'link-color' => '',
29
+ 'link-color-hover' => '',
30
+ 'underline' => 'underline',
31
+ 'blockquote-text-color' => '',
32
+ 'blockquote-bg-color' => '',
33
  'blockquote-border-color' => '',
34
+ 'extra-css' => '',
35
+ 'extra-head' => '',
36
+ 'analytics-extra' => '',
37
  );
38
 
39
  /** @var self Class instance */
40
  private static $instance;
41
+
42
  private function __construct() {
43
  // Register settings
44
  add_action( 'admin_init', array( $this, 'register_settings' ) );
66
 
67
  /**
68
  * Get the options
69
+ *
70
  * @return array
71
  */
72
  public static function get() {
73
+
74
  $me = self::get_instance();
75
+ $me->fetch_options();
76
+
 
 
 
77
  return $me->options;
78
  }
79
 
84
  if ( ! isset( self::$instance ) ) {
85
  self::$instance = new self();
86
  }
87
+
88
  return self::$instance;
89
  }
90
 
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.
103
+ $this->options = wp_parse_args( $saved_options, $this->defaults );
104
+ }
105
 
106
  // Make sure all post types are present.
107
  $this->update_post_type_settings();
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
  }
133
  $post_type_names[] = $post_type->name;
134
  }
135
  }
 
 
 
 
 
 
 
 
 
 
136
  }
137
  }
 
138
  }
classes/views/admin-page.php CHANGED
@@ -9,7 +9,6 @@ if ( ! defined( 'WPSEO_VERSION' ) ) {
9
  $yform = Yoast_Form::get_instance();
10
  $yform->admin_header( true, 'wpseo_amp', false, 'wpseo_amp_settings' );
11
 
12
- // data-default-color="#0a89c0"
13
  ?>
14
 
15
  <h2 class="nav-tab-wrapper" id="wpseo-tabs">
9
  $yform = Yoast_Form::get_instance();
10
  $yform->admin_header( true, 'wpseo_amp', false, 'wpseo_amp_settings' );
11
 
 
12
  ?>
13
 
14
  <h2 class="nav-tab-wrapper" id="wpseo-tabs">
readme.txt CHANGED
@@ -3,26 +3,37 @@ Contributors: joostdevalk
3
  Tags: AMP, SEO
4
  Requires at least: 4.2
5
  Tested up to: 4.4
6
- Stable tag: 0.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
- This plugin makes sure the default WordPress AMP plugin uses the proper Yoast SEO metadata.
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 company name or logo, you now have to go to the SEO settings, General tab.
17
 
18
  == Installation ==
19
 
20
  1. Upload the plugin files to the `/wp-content/plugins/yoast-seo-amp` directory, or install the plugin through the WordPress plugins screen directly.
21
  1. Activate the plugin through the 'Plugins' screen in WordPress
 
22
  1. You're done.
23
 
24
  == Changelog ==
25
 
 
 
 
 
 
 
 
 
 
 
26
  = 0.3 =
27
  * Split the plugin into several classes.
28
  * Added a settings page, found under SEO -> AMP
3
  Tags: AMP, SEO
4
  Requires at least: 4.2
5
  Tested up to: 4.4
6
+ Stable tag: 0.3.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
+ This plugin makes sure the default WordPress AMP plugin uses the proper Yoast SEO metadata and allows modification of the AMP page design.
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 ==
19
 
20
  1. Upload the plugin files to the `/wp-content/plugins/yoast-seo-amp` directory, or install the plugin through the WordPress plugins screen directly.
21
  1. Activate the plugin through the 'Plugins' screen in WordPress
22
+ 1. Go to SEO -> AMP to change your design and enable custom post types.
23
  1. You're done.
24
 
25
  == Changelog ==
26
 
27
+ = 0.3.1 =
28
+ * Bugfixes:
29
+ * Fixed bug where featured image wouldn't be used properly anymore.
30
+ * Fixed bug where CSS in Extra CSS field could be wrongly escaped.
31
+ * Fixed bug where wrong hook was used to `add_post_type_support`, causing integration issues.
32
+ * Fixed bug where post type settings wouldn't save properly.
33
+ * Enhancement:
34
+ * Added some more escaping to color picker functionality.
35
+ * Made sure no notice is thrown on frontend when post type setting isn't available.
36
+
37
  = 0.3 =
38
  * Split the plugin into several classes.
39
  * Added a settings page, found under SEO -> AMP
yoastseo-amp.php CHANGED
@@ -7,9 +7,9 @@
7
  *
8
  * @wordpress-plugin
9
  * Plugin Name: Glue for Yoast SEO & AMP
10
- * Plugin URI: https://yoast.com/yoast-seo-amp-glue/
11
  * Description: Makes sure the default WordPress AMP plugin uses the proper Yoast SEO metadata
12
- * Version: 0.3
13
  * Author: Joost de Valk
14
  * Author URI: https://yoast.com
15
  */
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.1
13
  * Author: Joost de Valk
14
  * Author URI: https://yoast.com
15
  */