PluginProbe
Parse.ly / 3.2.1
Parse.ly v3.2.1
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.2.1, at src/Integrations/class-amp.php

175 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AMP integration class
4 *
5 * @package Parsely\Integrations
6 * @since 2.6.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Integrations;
12
13 use Parsely\Parsely;
14
15 /**
16 * Integrates Parse.ly tracking with the AMP plugin.
17 *
18 * @since 2.6.0 Moved from Parsely class to this file.
19 */
20 class Amp implements Integration {
21 /**
22 * Apply the hooks that integrate the plugin or theme with the Parse.ly plugin.
23 *
24 * @since 2.6.0
25 *
26 * @return void
27 */
28 public function integrate(): void {
29 if ( defined( 'AMP__VERSION' ) ) {
30 add_action( 'template_redirect', array( $this, 'add_actions' ) );
31 }
32 }
33
34 /**
35 * Verify if request is an AMP request.
36 *
37 * This is needed to make it easier to mock whether the function exists ot not during tests.
38 *
39 * @since 2.6.0
40 *
41 * @return bool True is an AMP request, false otherwise.
42 */
43 public function is_amp_request(): bool {
44 return function_exists( 'amp_is_request' ) && amp_is_request();
45 }
46
47 /**
48 * Verify if request is an AMP request, and that AMP support is not disabled.
49 *
50 * @since 2.6.0
51 *
52 * @return bool True is an AMP request and not disabled, false otherwise.
53 */
54 public function can_handle_amp_request(): bool {
55 $options = get_option( Parsely::OPTIONS_KEY );
56
57 return $this->is_amp_request() && is_array( $options ) && ! $options['disable_amp'];
58 }
59
60 /**
61 * Add AMP actions.
62 *
63 * @since 2.6.0
64 *
65 * @return void
66 */
67 public function add_actions(): void {
68 if ( $this->can_handle_amp_request() ) {
69 add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) );
70 add_filter( 'amp_analytics_entries', array( $this, 'register_parsely_for_amp_native_analytics' ) );
71 }
72 }
73
74 /**
75 * Register Parse.ly for AMP analytics.
76 *
77 * @since 2.6.0
78 *
79 * @param array|null $analytics The analytics registry.
80 * @return array The analytics registry.
81 */
82 public function register_parsely_for_amp_analytics( ?array $analytics ): array {
83 if ( null === $analytics ) {
84 $analytics = array();
85 }
86
87 $config = self::construct_amp_config();
88 if ( array() === $config ) {
89 return $analytics;
90 }
91
92 $analytics['parsely'] = array(
93 'type' => 'parsely',
94 'attributes' => array(),
95 'config_data' => $config,
96 );
97
98 return $analytics;
99 }
100
101 /**
102 * Register Parse.ly for AMP native analytics.
103 *
104 * @since 2.6.0
105 *
106 * @param array|null $analytics The analytics registry.
107 * @return array The analytics registry.
108 */
109 public function register_parsely_for_amp_native_analytics( ?array $analytics ): array {
110 if ( null === $analytics ) {
111 $analytics = array();
112 }
113
114 $options = get_option( Parsely::OPTIONS_KEY );
115
116 if ( isset( $options['disable_amp'] ) && true === $options['disable_amp'] ) {
117 return $analytics;
118 }
119
120 $config = self::construct_amp_json();
121 if ( '' === $config ) {
122 return $analytics;
123 }
124
125 $analytics['parsely'] = array(
126 'type' => 'parsely',
127 'attributes' => array(),
128 'config' => $config,
129 );
130
131 return $analytics;
132 }
133
134 /**
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.
137 *
138 * @since 3.2.0
139 *
140 * @return string
141 */
142 public static function construct_amp_json(): string {
143 $config = self::construct_amp_config();
144 if ( array() === $config ) {
145 return '';
146 }
147
148 $encoded = wp_json_encode( $config );
149 return is_string( $encoded ) ? $encoded : '';
150 }
151
152 /**
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.
155 *
156 * @since 3.2.0
157 *
158 * @return array<string, array<string, string>>
159 */
160 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'] ) {
164 return array(
165 'vars' => array(
166 // This field will be rendered in a JS context.
167 'apikey' => esc_js( $options['apikey'] ),
168 ),
169 );
170 }
171
172 return array();
173 }
174 }
175