| @@ -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,19 +78,17 @@ | ||
| 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. | |
| 62 | 89 | * |
| 63 | 90 | * @since 2.6.0 |
| 64 | - * | |
| 65 | - * @return void | |
| 66 | 91 | */ |
| 67 | 92 | public function add_actions(): void { |
| 68 | 93 | if ( $this->can_handle_amp_request() ) { |
| 69 | 94 | add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) ); |
| @@ -71,14 +96,14 @@ | ||
| 71 | 96 | } |
| 72 | 97 | } |
| 73 | 98 | |
| 74 | 99 | /** |
| 75 | - * Register Parse.ly for AMP analytics. | |
| 100 | + * Registers Parse.ly for AMP analytics. | |
| 76 | 101 | * |
| 77 | 102 | * @since 2.6.0 |
| 78 | 103 | * |
| 79 | - * @param array|null $analytics The analytics registry. | |
| 80 | - * @return array The analytics registry. | |
| 104 | + * @param array<string, mixed>|null $analytics The analytics registry. | |
| 105 | + * @return array<string, mixed> The analytics registry. | |
| 81 | 106 | */ |
| 82 | 107 | public function register_parsely_for_amp_analytics( ?array $analytics ): array { |
| 83 | 108 | if ( null === $analytics ) { |
| 84 | 109 | $analytics = array(); |
| @@ -98,14 +123,15 @@ | ||
| 98 | 123 | return $analytics; |
| 99 | 124 | } |
| 100 | 125 | |
| 101 | 126 | /** |
| 102 | - * Register Parse.ly for AMP native analytics. | |
| 127 | + * Registers Parse.ly for AMP native analytics. | |
| 103 | 128 | * |
| 104 | 129 | * @since 2.6.0 |
| 105 | 130 | * |
| 106 | - * @param array|null $analytics The analytics registry. | |
| 107 | - * @return array The analytics registry. | |
| 131 | + * @param array<string, mixed>|null $analytics The analytics registry. | |
| 132 | + * | |
| 133 | + * @return Amp_Analytics|array<string, mixed> The analytics registry. | |
| 108 | 134 | */ |
| 109 | 135 | public function register_parsely_for_amp_native_analytics( ?array $analytics ): array { |
| 110 | 136 | if ( null === $analytics ) { |
| 111 | 137 | $analytics = array(); |
| @@ -110,11 +136,11 @@ | ||
| 110 | 136 | if ( null === $analytics ) { |
| 111 | 137 | $analytics = array(); |
| 112 | 138 | } |
| 113 | 139 | |
| 114 | - $options = get_option( Parsely::OPTIONS_KEY ); | |
| 140 | + $options = self::$parsely->get_options(); | |
| 115 | 141 | |
| 116 | - if ( isset( $options['disable_amp'] ) && true === $options['disable_amp'] ) { | |
| 142 | + if ( true === $options['disable_amp'] ) { | |
| 117 | 143 | return $analytics; |
| 118 | 144 | } |
| 119 | 145 | |
| 120 | 146 | $config = self::construct_amp_json(); |
| @@ -131,10 +157,11 @@ | ||
| 131 | 157 | return $analytics; |
| 132 | 158 | } |
| 133 | 159 | |
| 134 | 160 | /** |
| 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. | |
| 161 | + * Returns a string containing the JSON-encoded configuration required for | |
| 162 | + * AMP. It consists of the site's Site ID if that's defined, an empty string | |
| 163 | + * otherwise. | |
| 137 | 164 | * |
| 138 | 165 | * @since 3.2.0 |
| 139 | 166 | * |
| 140 | 167 | * @return string |
| @@ -149,23 +176,23 @@ | ||
| 149 | 176 | return is_string( $encoded ) ? $encoded : ''; |
| 150 | 177 | } |
| 151 | 178 | |
| 152 | 179 | /** |
| 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. | |
| 180 | + * Returns an array containing the configuration required for AMP. It | |
| 181 | + * consists of the site's Site ID if that's defined, or an empty array | |
| 182 | + * otherwise. | |
| 155 | 183 | * |
| 184 | + * @link https://docs.parse.ly/google-amp/ | |
| 156 | 185 | * @since 3.2.0 |
| 157 | 186 | * |
| 158 | 187 | * @return array<string, array<string, string>> |
| 159 | 188 | */ |
| 160 | 189 | 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'] ) { | |
| 190 | + if ( self::$parsely->site_id_is_set() ) { | |
| 164 | 191 | return array( |
| 165 | 192 | 'vars' => array( |
| 166 | 193 | // This field will be rendered in a JS context. |
| 167 | - 'apikey' => esc_js( $options['apikey'] ), | |
| 194 | + 'apikey' => esc_js( self::$parsely->get_site_id() ), | |
| 168 | 195 | ), |
| 169 | 196 | ); |
| 170 | 197 | } |
| 171 | 198 | |