| 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 |
add_action( 'init', array( $this, 'register_scripts' ) ); |
| 45 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_js_api' ) ); |
| 46 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_js_tracker' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register JavaScripts, 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 |
* @return void |
| 56 |
*/ |
| 57 |
public function register_scripts(): void { |
| 58 |
$parsely_options = $this->parsely->get_options(); |
| 59 |
|
| 60 |
if ( $this->parsely->api_key_is_missing() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$tracker_url = 'https://cdn.parsely.com/keys/' . $parsely_options['apikey'] . '/p.js'; |
| 65 |
$tracker_url = esc_url( $tracker_url ); |
| 66 |
|
| 67 |
wp_register_script( |
| 68 |
'wp-parsely-tracker', |
| 69 |
$tracker_url, |
| 70 |
array(), |
| 71 |
Parsely::get_asset_cache_buster(), |
| 72 |
true |
| 73 |
); |
| 74 |
|
| 75 |
$api_script_asset = require plugin_dir_path( PARSELY_FILE ) . 'build/init-api.asset.php'; |
| 76 |
wp_register_script( |
| 77 |
'wp-parsely-api', |
| 78 |
plugin_dir_url( PARSELY_FILE ) . 'build/init-api.js', |
| 79 |
$api_script_asset['dependencies'], |
| 80 |
Parsely::get_asset_cache_buster(), |
| 81 |
true |
| 82 |
); |
| 83 |
|
| 84 |
wp_localize_script( |
| 85 |
'wp-parsely-api', |
| 86 |
'wpParsely', // This globally-scoped object holds variables used to interact with the API. |
| 87 |
array( |
| 88 |
'apikey' => $parsely_options['apikey'], |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Enqueues the JavaScript code required to send off beacon requests. |
| 95 |
* |
| 96 |
* @since 2.5.0 Rename from insert_parsely_javascript |
| 97 |
* @since 3.0.0 Rename from load_js_tracker |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function enqueue_js_tracker(): void { |
| 102 |
$parsely_options = $this->parsely->get_options(); |
| 103 |
if ( $this->parsely->api_key_is_missing() || $parsely_options['disable_javascript'] ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
global $post; |
| 108 |
$display = true; |
| 109 |
if ( in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! Parsely::post_has_trackable_status( $post ) ) { |
| 110 |
$display = false; |
| 111 |
} |
| 112 |
if ( ! $parsely_options['track_authenticated_users'] && $this->parsely->parsely_is_user_logged_in() ) { |
| 113 |
$display = false; |
| 114 |
} |
| 115 |
if ( ! in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! in_array( get_post_type(), $parsely_options['track_page_types'], true ) ) { |
| 116 |
$display = false; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Filters whether to enqueue the Parsely JavaScript tracking script from the CDN. |
| 121 |
* |
| 122 |
* If true, the script is enqueued. |
| 123 |
* |
| 124 |
* @since 2.5.0 |
| 125 |
* |
| 126 |
* @param bool $display True if the JavaScript file should be included. False if not. |
| 127 |
*/ |
| 128 |
if ( ! apply_filters( 'wp_parsely_load_js_tracker', $display ) ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! has_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ) ) ) { |
| 133 |
add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 3 ); |
| 134 |
} |
| 135 |
|
| 136 |
wp_enqueue_script( 'wp-parsely-tracker' ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Load JavaScript for Parse.ly API. |
| 141 |
* |
| 142 |
* @since 2.5.0 |
| 143 |
* @since 3.0.0 Rename from load_js_api |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function enqueue_js_api(): void { |
| 148 |
$parsely_options = $this->parsely->get_options(); |
| 149 |
|
| 150 |
// If we don't have an API secret, there's no need to proceed. |
| 151 |
if ( empty( $parsely_options['api_secret'] ) ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
if ( ! has_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ) ) ) { |
| 156 |
add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 3 ); |
| 157 |
} |
| 158 |
|
| 159 |
wp_enqueue_script( 'wp-parsely-api' ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Filter the script tag for certain scripts to add needed attributes. |
| 164 |
* |
| 165 |
* @since 2.5.0 |
| 166 |
* |
| 167 |
* @param string $tag The `script` tag for the enqueued script. |
| 168 |
* @param string $handle The script's registered handle. |
| 169 |
* @param string $src Unused? The script's source URL. |
| 170 |
* @return string Amended `script` tag. |
| 171 |
*/ |
| 172 |
public function script_loader_tag( string $tag, string $handle, string $src ): string { |
| 173 |
$parsely_options = $this->parsely->get_options(); |
| 174 |
if ( in_array( |
| 175 |
$handle, |
| 176 |
array( |
| 177 |
'wp-parsely-api', |
| 178 |
'wp-parsely-tracker', |
| 179 |
'wp-parsely-recommended-widget', |
| 180 |
), |
| 181 |
true |
| 182 |
) ) { |
| 183 |
/** |
| 184 |
* Filter whether to include the CloudFlare Rocket Loader attribute (`data-cfasync=false`) in the script. |
| 185 |
* Only needed if the site being served is behind Cloudflare. Should return false otherwise. |
| 186 |
* https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts |
| 187 |
* |
| 188 |
* @since 3.0.0 |
| 189 |
* |
| 190 |
* @param bool $enabled True if enabled, false if not. |
| 191 |
* @param string $handle The script's registered handle. |
| 192 |
*/ |
| 193 |
if ( apply_filters( 'wp_parsely_enable_cfasync_attribute', false, $handle ) ) { |
| 194 |
$tag = preg_replace( '/^<script /', '<script data-cfasync="false" ', $tag ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if ( 'wp-parsely-tracker' === $handle ) { |
| 199 |
$tag = preg_replace( '/ id=(["\'])wp-parsely-tracker-js\1/', ' id="parsely-cfg"', $tag ); |
| 200 |
$tag = preg_replace( |
| 201 |
'/ src=/', |
| 202 |
' data-parsely-site="' . esc_attr( $parsely_options['apikey'] ) . '" src=', |
| 203 |
$tag |
| 204 |
); |
| 205 |
} |
| 206 |
return $tag; |
| 207 |
} |
| 208 |
} |
| 209 |
|