PluginProbe
Parse.ly / 3.16.2
Parse.ly v3.16.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 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Integrations / class-amp.php

class-amp.php in Parse.ly 3.16.2, at src/Integrations/class-amp.php

202 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Integrations: AMP integration class
4 *
5 * @package Parsely
6 * @since 2.6.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Integrations;
12
13 /**
14 * Integrates Parse.ly tracking with the AMP plugin.
15 *
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 * }
45 */
46 class Amp extends Integration {
47 /**
48 * Applies the hooks that integrate the plugin or theme with the Parse.ly
49 * plugin.
50 *
51 * @since 2.6.0
52 */
53 public function integrate(): void {
54 if ( defined( 'AMP__VERSION' ) ) {
55 add_action( 'template_redirect', array( $this, 'add_actions' ) );
56 }
57 }
58
59 /**
60 * Verifies that request is an AMP request.
61 *
62 * This is needed to make it easier to mock whether the function exists or
63 * not during tests.
64 *
65 * @since 2.6.0
66 *
67 * @return bool True is an AMP request, false otherwise.
68 */
69 public function is_amp_request(): bool {
70 return function_exists( 'amp_is_request' ) && amp_is_request();
71 }
72
73 /**
74 * Verifies that request is an AMP request, and that AMP support is not
75 * disabled.
76 *
77 * @since 2.6.0
78 *
79 * @return bool True is an AMP request and not disabled, false otherwise.
80 */
81 public function can_handle_amp_request(): bool {
82 $options = self::$parsely->get_options();
83
84 return $this->is_amp_request() && ! $options['disable_amp'];
85 }
86
87 /**
88 * Adds AMP actions.
89 *
90 * @since 2.6.0
91 */
92 public function add_actions(): void {
93 if ( $this->can_handle_amp_request() ) {
94 add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) );
95 add_filter( 'amp_analytics_entries', array( $this, 'register_parsely_for_amp_native_analytics' ) );
96 }
97 }
98
99 /**
100 * Registers Parse.ly for AMP analytics.
101 *
102 * @since 2.6.0
103 *
104 * @param array<string, mixed>|null $analytics The analytics registry.
105 * @return array<string, mixed> The analytics registry.
106 */
107 public function register_parsely_for_amp_analytics( ?array $analytics ): array {
108 if ( null === $analytics ) {
109 $analytics = array();
110 }
111
112 $config = self::construct_amp_config();
113 if ( array() === $config ) {
114 return $analytics;
115 }
116
117 $analytics['parsely'] = array(
118 'type' => 'parsely',
119 'attributes' => array(),
120 'config_data' => $config,
121 );
122
123 return $analytics;
124 }
125
126 /**
127 * Registers Parse.ly for AMP native analytics.
128 *
129 * @since 2.6.0
130 *
131 * @param array<string, mixed>|null $analytics The analytics registry.
132 * @return Amp_Analytics|array<string, mixed> The analytics registry.
133 */
134 public function register_parsely_for_amp_native_analytics( ?array $analytics ): array {
135 if ( null === $analytics ) {
136 $analytics = array();
137 }
138
139 $options = self::$parsely->get_options();
140
141 if ( true === $options['disable_amp'] ) {
142 return $analytics;
143 }
144
145 $config = self::construct_amp_json();
146 if ( '' === $config ) {
147 return $analytics;
148 }
149
150 $analytics['parsely'] = array(
151 'type' => 'parsely',
152 'attributes' => array(),
153 'config' => $config,
154 );
155
156 return $analytics;
157 }
158
159 /**
160 * Returns a string containing the JSON-encoded configuration required for
161 * AMP. It consists of the site's Site ID if that's defined, an empty string
162 * otherwise.
163 *
164 * @since 3.2.0
165 *
166 * @return string
167 */
168 public static function construct_amp_json(): string {
169 $config = self::construct_amp_config();
170 if ( array() === $config ) {
171 return '';
172 }
173
174 $encoded = wp_json_encode( $config );
175 return is_string( $encoded ) ? $encoded : '';
176 }
177
178 /**
179 * Returns an array containing the configuration required for AMP. It
180 * consists of the site's Site ID if that's defined, or an empty array
181 * otherwise.
182 *
183 * @since 3.2.0
184 *
185 * @link https://docs.parse.ly/google-amp/
186 *
187 * @return array<string, array<string, string>>
188 */
189 public static function construct_amp_config(): array {
190 if ( self::$parsely->site_id_is_set() ) {
191 return array(
192 'vars' => array(
193 // This field will be rendered in a JS context.
194 'apikey' => esc_js( self::$parsely->get_site_id() ),
195 ),
196 );
197 }
198
199 return array();
200 }
201 }
202