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

136 lines 3.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 $analytics The analytics registry.
80 * @return array The analytics registry.
81 */
82 public function register_parsely_for_amp_analytics( array $analytics ): array {
83 $options = get_option( Parsely::OPTIONS_KEY );
84
85 if ( empty( $options['apikey'] ) ) {
86 return $analytics;
87 }
88
89 $analytics['parsely'] = array(
90 'type' => 'parsely',
91 'attributes' => array(),
92 'config_data' => array(
93 'vars' => array(
94 'apikey' => $options['apikey'],
95 ),
96 ),
97 );
98
99 return $analytics;
100 }
101
102 /**
103 * Register Parse.ly for AMP native analytics.
104 *
105 * @since 2.6.0
106 *
107 * @param array $analytics The analytics registry.
108 * @return array The analytics registry.
109 */
110 public function register_parsely_for_amp_native_analytics( array $analytics ): array {
111 $options = get_option( Parsely::OPTIONS_KEY );
112
113 if ( ! empty( $options['disable_amp'] ) && true === $options['disable_amp'] ) {
114 return $analytics;
115 }
116
117 if ( empty( $options['apikey'] ) ) {
118 return $analytics;
119 }
120
121 $analytics['parsely'] = array(
122 'type' => 'parsely',
123 'attributes' => array(),
124 'config' => wp_json_encode(
125 array(
126 'vars' => array(
127 'apikey' => $options['apikey'],
128 ),
129 )
130 ),
131 );
132
133 return $analytics;
134 }
135 }
136