| 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 |
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 extends Integration { |
| 21 |
/** |
| 22 |
* Applies the hooks that integrate the plugin or theme with the Parse.ly |
| 23 |
* plugin. |
| 24 |
* |
| 25 |
* @since 2.6.0 |
| 26 |
*/ |
| 27 |
public function integrate(): void { |
| 28 |
if ( defined( 'AMP__VERSION' ) ) { |
| 29 |
add_action( 'template_redirect', array( $this, 'add_actions' ) ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Verifies that request is an AMP request. |
| 35 |
* |
| 36 |
* This is needed to make it easier to mock whether the function exists or |
| 37 |
* 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 |
* Verifies that request is an AMP request, and that AMP support is not |
| 49 |
* disabled. |
| 50 |
* |
| 51 |
* @since 2.6.0 |
| 52 |
* |
| 53 |
* @return bool True is an AMP request and not disabled, false otherwise. |
| 54 |
*/ |
| 55 |
public function can_handle_amp_request(): bool { |
| 56 |
$options = self::$parsely->get_options(); |
| 57 |
|
| 58 |
return $this->is_amp_request() && is_array( $options ) && ! $options['disable_amp']; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Adds AMP actions. |
| 63 |
* |
| 64 |
* @since 2.6.0 |
| 65 |
*/ |
| 66 |
public function add_actions(): void { |
| 67 |
if ( $this->can_handle_amp_request() ) { |
| 68 |
add_filter( 'amp_post_template_analytics', array( $this, 'register_parsely_for_amp_analytics' ) ); |
| 69 |
add_filter( 'amp_analytics_entries', array( $this, 'register_parsely_for_amp_native_analytics' ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Registers Parse.ly for AMP analytics. |
| 75 |
* |
| 76 |
* @since 2.6.0 |
| 77 |
* |
| 78 |
* @param array|null $analytics The analytics registry. |
| 79 |
* @return array The analytics registry. |
| 80 |
*/ |
| 81 |
public function register_parsely_for_amp_analytics( ?array $analytics ): array { |
| 82 |
if ( null === $analytics ) { |
| 83 |
$analytics = array(); |
| 84 |
} |
| 85 |
|
| 86 |
$config = self::construct_amp_config(); |
| 87 |
if ( array() === $config ) { |
| 88 |
return $analytics; |
| 89 |
} |
| 90 |
|
| 91 |
$analytics['parsely'] = array( |
| 92 |
'type' => 'parsely', |
| 93 |
'attributes' => array(), |
| 94 |
'config_data' => $config, |
| 95 |
); |
| 96 |
|
| 97 |
return $analytics; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Registers Parse.ly for AMP native analytics. |
| 102 |
* |
| 103 |
* @since 2.6.0 |
| 104 |
* |
| 105 |
* @param array|null $analytics The analytics registry. |
| 106 |
* @return array The analytics registry. |
| 107 |
*/ |
| 108 |
public function register_parsely_for_amp_native_analytics( ?array $analytics ): array { |
| 109 |
if ( null === $analytics ) { |
| 110 |
$analytics = array(); |
| 111 |
} |
| 112 |
|
| 113 |
$options = self::$parsely->get_options(); |
| 114 |
|
| 115 |
if ( isset( $options['disable_amp'] ) && true === $options['disable_amp'] ) { |
| 116 |
return $analytics; |
| 117 |
} |
| 118 |
|
| 119 |
$config = self::construct_amp_json(); |
| 120 |
if ( '' === $config ) { |
| 121 |
return $analytics; |
| 122 |
} |
| 123 |
|
| 124 |
$analytics['parsely'] = array( |
| 125 |
'type' => 'parsely', |
| 126 |
'attributes' => array(), |
| 127 |
'config' => $config, |
| 128 |
); |
| 129 |
|
| 130 |
return $analytics; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns a string containing the JSON-encoded configuration required for |
| 135 |
* AMP. It consists of the site's Site ID if that's defined, an empty string |
| 136 |
* 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 |
| 154 |
* consists of the site's Site ID if that's defined, or an empty array |
| 155 |
* otherwise. |
| 156 |
* |
| 157 |
* @since 3.2.0 |
| 158 |
* |
| 159 |
* @return array<string, array<string, string>> |
| 160 |
*/ |
| 161 |
public static function construct_amp_config(): array { |
| 162 |
$options = self::$parsely->get_options(); |
| 163 |
|
| 164 |
if ( isset( $options['apikey'] ) && is_string( $options['apikey'] ) && '' !== $options['apikey'] ) { |
| 165 |
return array( |
| 166 |
'vars' => array( |
| 167 |
// This field will be rendered in a JS context. |
| 168 |
'apikey' => esc_js( $options['apikey'] ), |
| 169 |
), |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
return array(); |
| 174 |
} |
| 175 |
} |
| 176 |
|