| 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 and deregisters any scripts that create AMP validation |
| 89 |
* issues. |
| 90 |
* |
| 91 |
* @since 2.6.0 |
| 92 |
*/ |
| 93 |
public function add_actions(): void { |
| 94 |
if ( $this->can_handle_amp_request() ) { |
| 95 |
add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) ); |
| 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' ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Registers Parse.ly for AMP analytics. |
| 112 |
* |
| 113 |
* @since 2.6.0 |
| 114 |
* |
| 115 |
* @param array<string, mixed>|null $analytics The analytics registry. |
| 116 |
* @return array<string, mixed> The analytics registry. |
| 117 |
*/ |
| 118 |
public function register_parsely_for_amp_analytics( ?array $analytics ): array { |
| 119 |
if ( null === $analytics ) { |
| 120 |
$analytics = array(); |
| 121 |
} |
| 122 |
|
| 123 |
$config = self::construct_amp_config(); |
| 124 |
if ( array() === $config ) { |
| 125 |
return $analytics; |
| 126 |
} |
| 127 |
|
| 128 |
$analytics['parsely'] = array( |
| 129 |
'type' => 'parsely', |
| 130 |
'attributes' => array(), |
| 131 |
'config_data' => $config, |
| 132 |
); |
| 133 |
|
| 134 |
return $analytics; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Registers Parse.ly for AMP native analytics. |
| 139 |
* |
| 140 |
* @since 2.6.0 |
| 141 |
* |
| 142 |
* @param array<string, mixed>|null $analytics The analytics registry. |
| 143 |
* @return Amp_Analytics|array<string, mixed> The analytics registry. |
| 144 |
*/ |
| 145 |
public function register_parsely_for_amp_native_analytics( ?array $analytics ): array { |
| 146 |
if ( null === $analytics ) { |
| 147 |
$analytics = array(); |
| 148 |
} |
| 149 |
|
| 150 |
$options = self::$parsely->get_options(); |
| 151 |
|
| 152 |
if ( true === $options['disable_amp'] ) { |
| 153 |
return $analytics; |
| 154 |
} |
| 155 |
|
| 156 |
$config = self::construct_amp_json(); |
| 157 |
if ( '' === $config ) { |
| 158 |
return $analytics; |
| 159 |
} |
| 160 |
|
| 161 |
$analytics['parsely'] = array( |
| 162 |
'type' => 'parsely', |
| 163 |
'attributes' => array(), |
| 164 |
'config' => $config, |
| 165 |
); |
| 166 |
|
| 167 |
return $analytics; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 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. |
| 174 |
* |
| 175 |
* @since 3.2.0 |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public static function construct_amp_json(): string { |
| 180 |
$config = self::construct_amp_config(); |
| 181 |
if ( array() === $config ) { |
| 182 |
return ''; |
| 183 |
} |
| 184 |
|
| 185 |
$encoded = wp_json_encode( $config ); |
| 186 |
return is_string( $encoded ) ? $encoded : ''; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 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. |
| 193 |
* |
| 194 |
* @since 3.2.0 |
| 195 |
* |
| 196 |
* @link https://docs.parse.ly/google-amp/ |
| 197 |
* |
| 198 |
* @return array<string, array<string, string>> |
| 199 |
*/ |
| 200 |
public static function construct_amp_config(): array { |
| 201 |
if ( self::$parsely->site_id_is_set() ) { |
| 202 |
return array( |
| 203 |
'vars' => array( |
| 204 |
// This field will be rendered in a JS context. |
| 205 |
'apikey' => esc_js( self::$parsely->get_site_id() ), |
| 206 |
), |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
return array(); |
| 211 |
} |
| 212 |
} |
| 213 |
|