PluginProbe
Parse.ly / 3.4.2
Parse.ly v3.4.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.4.2, at src/class-scripts.php

201 lines 6.2 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 /**
14 * Inserts the scripts and tracking code into the site's front-end.
15 *
16 * @since 1.0.0
17 * @since 3.0.0 Moved from class-parsely to separate file
18 */
19 class Scripts {
20 /**
21 * Instance of Parsely class.
22 *
23 * @var Parsely
24 */
25 private $parsely;
26
27 /**
28 * Constructor.
29 *
30 * @param Parsely $parsely Instance of Parsely class.
31 */
32 public function __construct( Parsely $parsely ) {
33 $this->parsely = $parsely;
34 }
35
36 /**
37 * Registers scripts.
38 *
39 * @since 3.0.0
40 */
41 public function run(): void {
42 $parsely_options = $this->parsely->get_options();
43 if ( $this->parsely->api_key_is_set() && true !== $parsely_options['disable_javascript'] ) {
44 add_action( 'init', array( $this, 'register_scripts' ) );
45 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_js_tracker' ) );
46 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
47 }
48 }
49
50 /**
51 * Registers scripts, if there's an API key value saved.
52 *
53 * @since 2.5.0
54 * @since 3.0.0 Rename from register_js
55 */
56 public function register_scripts(): void {
57 wp_register_script(
58 'wp-parsely-tracker',
59 $this->parsely->get_tracker_url(),
60 array(),
61 PARSELY_VERSION,
62 true
63 );
64
65 $loader_asset = require plugin_dir_path( PARSELY_FILE ) . 'build/loader.asset.php';
66 wp_register_script(
67 'wp-parsely-loader',
68 plugin_dir_url( PARSELY_FILE ) . 'build/loader.js',
69 $loader_asset['dependencies'],
70 $loader_asset['version'],
71 true
72 );
73
74 $content_helper_asset = require plugin_dir_path( PARSELY_FILE ) . 'build/content-helper.asset.php';
75 wp_register_script(
76 'wp-parsely-block-content-helper',
77 plugin_dir_url( PARSELY_FILE ) . 'build/content-helper.js',
78 $content_helper_asset['dependencies'],
79 $loader_asset['version'],
80 true
81 );
82 }
83
84 /**
85 * Enqueues the JavaScript code required to send off beacon requests.
86 *
87 * @since 2.5.0 Rename from insert_parsely_javascript
88 * @since 3.0.0 Rename from load_js_tracker
89 */
90 public function enqueue_js_tracker(): void {
91 $parsely_options = $this->parsely->get_options();
92
93 global $post;
94 $display = true;
95 if ( in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! Parsely::post_has_trackable_status( $post ) ) {
96 $display = false;
97 }
98 if ( ! $parsely_options['track_authenticated_users'] && $this->parsely->parsely_is_user_logged_in() ) {
99 $display = false;
100 }
101 if ( ! in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! in_array( get_post_type(), $parsely_options['track_page_types'], true ) ) {
102 $display = false;
103 }
104
105 /**
106 * Filters whether to enqueue the Parsely JavaScript tracking script from the CDN.
107 *
108 * If true, the script is enqueued.
109 *
110 * @since 2.5.0
111 *
112 * @param bool $display True if the JavaScript file should be included. False if not.
113 */
114 if ( ! apply_filters( 'wp_parsely_load_js_tracker', $display ) ) {
115 return;
116 }
117
118 if ( false === has_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ) ) ) {
119 add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 3 );
120 }
121
122 wp_enqueue_script( 'wp-parsely-loader' );
123 wp_enqueue_script( 'wp-parsely-tracker' );
124
125 // If we don't have an API secret, there's no need to set the API key.
126 // Setting the API key triggers the UUID Profile Call function.
127 if ( isset( $parsely_options['api_secret'] ) && is_string( $parsely_options['api_secret'] ) && '' !== $parsely_options['api_secret'] ) {
128 $js_api_key = "window.wpParselyApiKey = '" . esc_js( $this->parsely->get_api_key() ) . "';";
129 wp_add_inline_script( 'wp-parsely-loader', $js_api_key, 'before' );
130 }
131
132 if ( isset( $parsely_options['disable_autotrack'] ) && true === $parsely_options['disable_autotrack'] ) {
133 $disable_autotrack = 'window.wpParselyDisableAutotrack = true;';
134 wp_add_inline_script( 'wp-parsely-loader', $disable_autotrack, 'before' );
135 }
136 }
137
138 /**
139 * Loads the Block Editor / Gutenberg experience enhancements.
140 * Hooked into the `enqueue_block_editor_assets` action in `$this->run()`
141 *
142 * @see: https://developer.wordpress.org/block-editor/how-to-guides/plugin-sidebar-0/
143 * @return void
144 */
145 public function enqueue_block_editor_assets(): void {
146 wp_enqueue_script( 'wp-parsely-block-content-helper' );
147
148 $prefix = trailingslashit( 'https://dash.parsely.com/' . esc_js( $this->parsely->get_api_key() ) ) . 'find';
149 $analytics_link_prefix = 'window.wpParselyContentHelperPrefix = "' . $prefix . '";';
150 wp_add_inline_script( 'wp-parsely-block-content-helper', $analytics_link_prefix, 'before' );
151 }
152
153 /**
154 * Filters the script tag for certain scripts to add needed attributes.
155 *
156 * @since 2.5.0
157 *
158 * @param string $tag The `script` tag for the enqueued script.
159 * @param string $handle The script's registered handle.
160 * @param string $src Unused? The script's source URL.
161 * @return string Amended `script` tag.
162 */
163 public function script_loader_tag( string $tag, string $handle, string $src ): string {
164 $parsely_options = $this->parsely->get_options();
165 if ( \in_array(
166 $handle,
167 array(
168 'wp-parsely-loader',
169 'wp-parsely-tracker',
170 'wp-parsely-recommended-widget',
171 ),
172 true
173 ) ) {
174 /**
175 * Filter whether to include the CloudFlare Rocket Loader attribute (`data-cfasync=false`) in the script.
176 * Only needed if the site being served is behind Cloudflare. Should return false otherwise.
177 * https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts
178 *
179 * @since 3.0.0
180 *
181 * @param bool $enabled True if enabled, false if not.
182 * @param string $handle The script's registered handle.
183 */
184 if ( apply_filters( 'wp_parsely_enable_cfasync_attribute', false, $handle ) ) {
185 $tag = preg_replace( '/^<script /', '<script data-cfasync="false" ', $tag );
186 }
187 }
188
189 if ( null !== $tag && 'wp-parsely-tracker' === $handle ) {
190 $tag = preg_replace( '/ id=(["\'])wp-parsely-tracker-js\1/', ' id="parsely-cfg"', $tag );
191 $tag = str_replace(
192 ' src=',
193 ' data-parsely-site="' . esc_attr( $parsely_options['apikey'] ) . '" src=',
194 $tag
195 );
196 }
197
198 return $tag ?? '';
199 }
200 }
201