PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
3.24.3 3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 All 107 releases
← All changes | src/Integrations/class-amp.php +71 -33 3.2.0 → 3.24.2 View file →
@@ -1,10 +1,10 @@
1 1 <?php
2 2 /**
3 - * AMP integration class
3 + * Integrations: AMP integration class
4 4 *
5 - * @package Parsely\Integrations
6 - * @since 2.6.0
5 + * @package Parsely
6 + * @since 2.6.0
7 7 */
8 8
9 9 declare(strict_types=1);
10 10
@@ -9,22 +9,47 @@
9 9 declare(strict_types=1);
10 10
11 11 namespace Parsely\Integrations;
12 12
13 -use Parsely\Parsely;
14 -
15 13 /**
16 14 * Integrates Parse.ly tracking with the AMP plugin.
17 15 *
18 16 * @since 2.6.0 Moved from Parsely class to this file.
17 + *
18 + * @phpstan-type Amp_Analytics array{
19 + * parsely: Parsely_Amp_Analytics,
20 + * }
21 + *
22 + * @phpstan-type Amp_Native_Analytics array{
23 + * parsely: Parsely_Amp_Native_Analytics,
24 + * }
25 + *
26 + * @phpstan-type Parsely_Amp_Analytics array{
27 + * type: string,
28 + * attributes: array<string, mixed>,
29 + * config_data: Parsely_Amp_Config
30 + * }
31 + *
32 + * @phpstan-type Parsely_Amp_Native_Analytics array{
33 + * type: string,
34 + * attributes: array<string, mixed>,
35 + * config: string
36 + * }
37 + *
38 + * @phpstan-type Parsely_Amp_Config array{
39 + * vars: Parsely_Amp_Config_Vars,
40 + * }
41 + *
42 + * @phpstan-type Parsely_Amp_Config_Vars array{
43 + * apikey: string,
44 + * }
19 45 */
20 -class Amp implements Integration {
46 +class Amp extends Integration {
21 47 /**
22 - * Apply the hooks that integrate the plugin or theme with the Parse.ly plugin.
48 + * Applies the hooks that integrate the plugin or theme with the Parse.ly
49 + * plugin.
23 50 *
24 51 * @since 2.6.0
25 - *
26 - * @return void
27 52 */
28 53 public function integrate(): void {
29 54 if ( defined( 'AMP__VERSION' ) ) {
30 55 add_action( 'template_redirect', array( $this, 'add_actions' ) );
@@ -31,11 +56,12 @@
31 56 }
32 57 }
33 58
34 59 /**
35 - * Verify if request is an AMP request.
60 + * Verifies that request is an AMP request.
36 61 *
37 - * This is needed to make it easier to mock whether the function exists ot not during tests.
62 + * This is needed to make it easier to mock whether the function exists or
63 + * not during tests.
38 64 *
39 65 * @since 2.6.0
40 66 *
41 67 * @return bool True is an AMP request, false otherwise.
@@ -44,9 +70,10 @@
44 70 return function_exists( 'amp_is_request' ) && amp_is_request();
45 71 }
46 72
47 73 /**
48 - * Verify if request is an AMP request, and that AMP support is not disabled.
74 + * Verifies that request is an AMP request, and that AMP support is not
75 + * disabled.
49 76 *
50 77 * @since 2.6.0
51 78 *
52 79 * @return bool True is an AMP request and not disabled, false otherwise.
@@ -51,34 +78,43 @@
51 78 *
52 79 * @return bool True is an AMP request and not disabled, false otherwise.
53 80 */
54 81 public function can_handle_amp_request(): bool {
55 - $options = get_option( Parsely::OPTIONS_KEY );
82 + $options = self::$parsely->get_options();
56 83
57 - return $this->is_amp_request() && is_array( $options ) && ! $options['disable_amp'];
84 + return $this->is_amp_request() && ! $options['disable_amp'];
58 85 }
59 86
60 87 /**
61 - * Add AMP actions.
88 + * Adds AMP actions and deregisters any scripts that create AMP validation
89 + * issues.
62 90 *
63 91 * @since 2.6.0
64 - *
65 - * @return void
66 92 */
67 93 public function add_actions(): void {
68 94 if ( $this->can_handle_amp_request() ) {
69 95 add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) );
70 96 add_filter( 'amp_analytics_entries', array( $this, 'register_parsely_for_amp_native_analytics' ) );
97 +
98 + // The tracker script isn't needed here, since tracking is done via
99 + // the amp-analytics tag. Including the tracker creates an AMP
100 + // validation issue.
101 + wp_deregister_script( 'wp-parsely-tracker' );
102 +
103 + // Our loader script creates an AMP validation issue, and it might
104 + // not be useful under the AMP context. If we get feedback that it's
105 + // needed, we can consider adding it under AMP.
106 + wp_deregister_script( 'wp-parsely-loader' );
71 107 }
72 108 }
73 109
74 110 /**
75 - * Register Parse.ly for AMP analytics.
111 + * Registers Parse.ly for AMP analytics.
76 112 *
77 113 * @since 2.6.0
78 114 *
79 - * @param array|null $analytics The analytics registry.
80 - * @return array The analytics registry.
115 + * @param array<string, mixed>|null $analytics The analytics registry.
116 + * @return array<string, mixed> The analytics registry.
81 117 */
82 118 public function register_parsely_for_amp_analytics( ?array $analytics ): array {
83 119 if ( null === $analytics ) {
84 120 $analytics = array();
@@ -98,14 +134,14 @@
98 134 return $analytics;
99 135 }
100 136
101 137 /**
102 - * Register Parse.ly for AMP native analytics.
138 + * Registers Parse.ly for AMP native analytics.
103 139 *
104 140 * @since 2.6.0
105 141 *
106 - * @param array|null $analytics The analytics registry.
107 - * @return array The analytics registry.
142 + * @param array<string, mixed>|null $analytics The analytics registry.
143 + * @return Amp_Analytics|array<string, mixed> The analytics registry.
108 144 */
109 145 public function register_parsely_for_amp_native_analytics( ?array $analytics ): array {
110 146 if ( null === $analytics ) {
111 147 $analytics = array();
@@ -110,11 +146,11 @@
110 146 if ( null === $analytics ) {
111 147 $analytics = array();
112 148 }
113 149
114 - $options = get_option( Parsely::OPTIONS_KEY );
150 + $options = self::$parsely->get_options();
115 151
116 - if ( isset( $options['disable_amp'] ) && true === $options['disable_amp'] ) {
152 + if ( true === $options['disable_amp'] ) {
117 153 return $analytics;
118 154 }
119 155
120 156 $config = self::construct_amp_json();
@@ -131,10 +167,11 @@
131 167 return $analytics;
132 168 }
133 169
134 170 /**
135 - * Returns a string containing the JSON-encoded configuration required for AMP. It consists of the site's API
136 - * key if that's defined, an empty string otherwise.
171 + * Returns a string containing the JSON-encoded configuration required for
172 + * AMP. It consists of the site's Site ID if that's defined, an empty string
173 + * otherwise.
137 174 *
138 175 * @since 3.2.0
139 176 *
140 177 * @return string
@@ -149,23 +186,24 @@
149 186 return is_string( $encoded ) ? $encoded : '';
150 187 }
151 188
152 189 /**
153 - * Returns an array containing the configuration required for AMP. It consists of the site's API key if that's
154 - * defined, or an empty array otherwise.
190 + * Returns an array containing the configuration required for AMP. It
191 + * consists of the site's Site ID if that's defined, or an empty array
192 + * otherwise.
155 193 *
156 194 * @since 3.2.0
157 195 *
196 + * @link https://docs.parse.ly/google-amp/
197 + *
158 198 * @return array<string, array<string, string>>
159 199 */
160 200 public static function construct_amp_config(): array {
161 - $options = get_option( Parsely::OPTIONS_KEY );
162 -
163 - if ( isset( $options['apikey'] ) && is_string( $options['apikey'] ) && '' !== $options['apikey'] ) {
201 + if ( self::$parsely->site_id_is_set() ) {
164 202 return array(
165 203 'vars' => array(
166 204 // This field will be rendered in a JS context.
167 - 'apikey' => esc_js( $options['apikey'] ),
205 + 'apikey' => esc_js( self::$parsely->get_site_id() ),
168 206 ),
169 207 );
170 208 }
171 209