PluginProbe
Parse.ly / 3.21.2
Parse.ly v3.21.2
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 / class-scripts.php

class-scripts.php in Parse.ly 3.21.2, at src/class-scripts.php

189 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Scripts class
4 *
5 * @package Parsely
6 * @since 3.0.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely;
12
13 use Parsely\Utils\Utils;
14
15 /**
16 * Inserts the scripts and tracking code into the site's front-end.
17 *
18 * @since 1.0.0
19 * @since 3.0.0 Moved from class-parsely to separate file.
20 */
21 class Scripts {
22 /**
23 * Instance of Parsely class.
24 *
25 * @var Parsely
26 */
27 private $parsely;
28
29 /**
30 * Constructor.
31 *
32 * @param Parsely $parsely Instance of Parsely class.
33 */
34 public function __construct( Parsely $parsely ) {
35 $this->parsely = $parsely;
36 }
37
38 /**
39 * Registers scripts.
40 *
41 * @since 3.0.0
42 */
43 public function run(): void {
44 $parsely_options = $this->parsely->get_options();
45 if ( $this->parsely->site_id_is_set() && true !== $parsely_options['disable_javascript'] ) {
46 add_action( 'init', array( $this, 'register_scripts' ) );
47 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_js_tracker' ) );
48 }
49 }
50
51 /**
52 * Registers scripts, if there's a Site ID value saved.
53 *
54 * @since 2.5.0
55 * @since 3.0.0 Rename from register_js.
56 */
57 public function register_scripts(): void {
58 wp_register_script(
59 'wp-parsely-tracker',
60 $this->parsely->get_tracker_url(),
61 array(),
62 PARSELY_VERSION,
63 true
64 );
65
66 $loader_asset = Utils::get_asset_info( 'build/loader.asset.php' );
67
68 wp_register_script(
69 'wp-parsely-loader',
70 plugin_dir_url( PARSELY_FILE ) . 'build/loader.js',
71 $loader_asset['dependencies'],
72 $loader_asset['version'],
73 true
74 );
75 }
76
77 /**
78 * Enqueues the JavaScript code required to send off beacon requests.
79 *
80 * @since 2.5.0 Rename from insert_parsely_javascript.
81 * @since 3.0.0 Rename from load_js_tracker.
82 */
83 public function enqueue_js_tracker(): void {
84 if ( is_preview() ) {
85 return;
86 }
87
88 $parsely_options = $this->parsely->get_options();
89
90 global $post;
91 $display = true;
92 if ( in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! Parsely::post_has_trackable_status( $post ) ) {
93 $display = false;
94 }
95 if ( ! $parsely_options['track_authenticated_users'] && $this->parsely->is_blog_member_logged_in() ) {
96 $display = false;
97 }
98 if ( ! in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! in_array( get_post_type(), $parsely_options['track_page_types'], true ) ) {
99 $display = false;
100 }
101
102 /**
103 * Filters whether to enqueue the Parsely JavaScript tracking script from the CDN.
104 *
105 * If true, the script is enqueued.
106 *
107 * @since 2.5.0
108 *
109 * @param bool $display True if the JavaScript file should be included. False if not.
110 */
111 if ( ! apply_filters( 'wp_parsely_load_js_tracker', $display ) ) {
112 return;
113 }
114
115 if ( false === has_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ) ) ) {
116 add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 3 );
117 }
118
119 wp_enqueue_script( 'wp-parsely-loader' );
120 wp_enqueue_script( 'wp-parsely-tracker' );
121
122 // If we don't have an API secret, there's no need to set the Site ID.
123 // Setting the Site ID triggers the UUID Profile Call function.
124 if ( $this->parsely->api_secret_is_set() ) {
125 $js_site_id = "window.wpParselySiteId = '" . esc_js( $this->parsely->get_site_id() ) . "';";
126 wp_add_inline_script( 'wp-parsely-loader', $js_site_id, 'before' );
127 }
128
129 if ( true === $parsely_options['disable_autotrack'] ) {
130 $disable_autotrack = 'window.wpParselyDisableAutotrack = true;';
131 wp_add_inline_script( 'wp-parsely-loader', $disable_autotrack, 'before' );
132 }
133 }
134
135 /**
136 * Filters the script tag for certain scripts to add needed attributes.
137 *
138 * @since 2.5.0
139 *
140 * @param string $tag The `script` tag for the enqueued script.
141 * @param string $handle The script's registered handle.
142 * @param string $src Unused? The script's source URL.
143 * @return string Amended `script` tag.
144 */
145 public function script_loader_tag( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
146 string $tag,
147 string $handle,
148 string $src
149 ): string {
150 if ( \in_array(
151 $handle,
152 array(
153 'wp-parsely-loader',
154 'wp-parsely-tracker',
155 'wp-parsely-recommended-widget',
156 ),
157 true
158 ) ) {
159 /**
160 * Filter whether to include the CloudFlare Rocket Loader attribute (`data-cfasync=false`) in the script.
161 * Only needed if the site being served is behind Cloudflare. Should return false otherwise.
162 * https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts
163 *
164 * @since 3.0.0
165 *
166 * @param bool $enabled True if enabled, false if not.
167 * @param string $handle The script's registered handle.
168 */
169 if ( apply_filters( 'wp_parsely_enable_cfasync_attribute', false, $handle ) ) {
170 $tag = preg_replace( '/^<script /', '<script data-cfasync="false" ', $tag );
171 }
172 }
173
174 if ( null !== $tag && 'wp-parsely-tracker' === $handle ) {
175 $tag = preg_replace( '/ id=(["\'])wp-parsely-tracker-js\1/', ' id="parsely-cfg"', $tag );
176
177 if ( null !== $tag ) {
178 $tag = str_replace(
179 ' src=',
180 ' data-parsely-site="' . esc_attr( $this->parsely->get_site_id() ) . '" src=',
181 $tag
182 );
183 }
184 }
185
186 return $tag ?? '';
187 }
188 }
189