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

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

177 lines 4.9 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 * Register js scripts.
38 *
39 * @since 3.0.0
40 *
41 * @return void
42 */
43 public function run(): void {
44 $parsely_options = $this->parsely->get_options();
45 if ( $this->parsely->api_key_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 * Register JavaScripts, if there's an API key value saved.
53 *
54 * @since 2.5.0
55 * @since 3.0.0 Rename from register_js
56 *
57 * @return void
58 */
59 public function register_scripts(): void {
60 wp_register_script(
61 'wp-parsely-tracker',
62 $this->parsely->get_tracker_url(),
63 array(),
64 PARSELY_VERSION,
65 true
66 );
67
68 $loader_asset = require plugin_dir_path( PARSELY_FILE ) . 'build/loader.asset.php';
69 wp_register_script(
70 'wp-parsely-loader',
71 plugin_dir_url( PARSELY_FILE ) . 'build/loader.js',
72 $loader_asset['dependencies'],
73 $loader_asset['version'],
74 true
75 );
76 }
77
78 /**
79 * Enqueues the JavaScript code required to send off beacon requests.
80 *
81 * @since 2.5.0 Rename from insert_parsely_javascript
82 * @since 3.0.0 Rename from load_js_tracker
83 *
84 * @return void
85 */
86 public function enqueue_js_tracker(): void {
87 $parsely_options = $this->parsely->get_options();
88
89 global $post;
90 $display = true;
91 if ( in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! Parsely::post_has_trackable_status( $post ) ) {
92 $display = false;
93 }
94 if ( ! $parsely_options['track_authenticated_users'] && $this->parsely->parsely_is_user_logged_in() ) {
95 $display = false;
96 }
97 if ( ! in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! in_array( get_post_type(), $parsely_options['track_page_types'], true ) ) {
98 $display = false;
99 }
100
101 /**
102 * Filters whether to enqueue the Parsely JavaScript tracking script from the CDN.
103 *
104 * If true, the script is enqueued.
105 *
106 * @since 2.5.0
107 *
108 * @param bool $display True if the JavaScript file should be included. False if not.
109 */
110 if ( ! apply_filters( 'wp_parsely_load_js_tracker', $display ) ) {
111 return;
112 }
113
114 if ( false === has_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ) ) ) {
115 add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 3 );
116 }
117
118 wp_enqueue_script( 'wp-parsely-loader' );
119 wp_enqueue_script( 'wp-parsely-tracker' );
120
121 // If we don't have an API secret, there's no need to set the API key.
122 // Setting the API key triggers the UUID Profile Call function.
123 if ( isset( $parsely_options['api_secret'] ) && is_string( $parsely_options['api_secret'] ) && '' !== $parsely_options['api_secret'] ) {
124 $js_api_key = "window.wpParselyApiKey = '" . esc_js( $this->parsely->get_api_key() ) . "';";
125 wp_add_inline_script( 'wp-parsely-loader', $js_api_key, 'before' );
126 }
127 }
128
129 /**
130 * Filter the script tag for certain scripts to add needed attributes.
131 *
132 * @since 2.5.0
133 *
134 * @param string $tag The `script` tag for the enqueued script.
135 * @param string $handle The script's registered handle.
136 * @param string $src Unused? The script's source URL.
137 * @return string Amended `script` tag.
138 */
139 public function script_loader_tag( string $tag, string $handle, string $src ): string {
140 $parsely_options = $this->parsely->get_options();
141 if ( in_array(
142 $handle,
143 array(
144 'wp-parsely-loader',
145 'wp-parsely-tracker',
146 'wp-parsely-recommended-widget',
147 ),
148 true
149 ) ) {
150 /**
151 * Filter whether to include the CloudFlare Rocket Loader attribute (`data-cfasync=false`) in the script.
152 * Only needed if the site being served is behind Cloudflare. Should return false otherwise.
153 * https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts
154 *
155 * @since 3.0.0
156 *
157 * @param bool $enabled True if enabled, false if not.
158 * @param string $handle The script's registered handle.
159 */
160 if ( apply_filters( 'wp_parsely_enable_cfasync_attribute', false, $handle ) ) {
161 $tag = preg_replace( '/^<script /', '<script data-cfasync="false" ', $tag );
162 }
163 }
164
165 if ( null !== $tag && 'wp-parsely-tracker' === $handle ) {
166 $tag = preg_replace( '/ id=(["\'])wp-parsely-tracker-js\1/', ' id="parsely-cfg"', $tag );
167 $tag = preg_replace(
168 '/ src=/',
169 ' data-parsely-site="' . esc_attr( $parsely_options['apikey'] ) . '" src=',
170 $tag
171 );
172 }
173
174 return $tag ?? '';
175 }
176 }
177