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