| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Scripts class |
| 4 | 4 | * |
| 5 | 5 | * @package Parsely |
| 6 | - * @since 3.0.0 | |
| 6 | + * @since 3.0.0 | |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | declare(strict_types=1); |
| 10 | 10 | |
| @@ -9,10 +9,12 @@ | ||
| 9 | 9 | declare(strict_types=1); |
| 10 | 10 | |
| 11 | 11 | namespace Parsely; |
| 12 | 12 | |
| 13 | +use function Parsely\Utils\get_asset_info; | |
| 14 | + | |
| 13 | 15 | /** |
| 14 | - * Inserts the scripts and tracking code into the site's front-end | |
| 16 | + * Inserts the scripts and tracking code into the site's front-end. | |
| 15 | 17 | * |
| 16 | 18 | * @since 1.0.0 |
| 17 | 19 | * @since 3.0.0 Moved from class-parsely to separate file |
| 18 | 20 | */ |
| @@ -33,17 +35,15 @@ | ||
| 33 | 35 | $this->parsely = $parsely; |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | /** |
| 37 | - * Register js scripts. | |
| 39 | + * Registers scripts. | |
| 38 | 40 | * |
| 39 | 41 | * @since 3.0.0 |
| 40 | - * | |
| 41 | - * @return void | |
| 42 | 42 | */ |
| 43 | 43 | public function run(): void { |
| 44 | 44 | $parsely_options = $this->parsely->get_options(); |
| 45 | - if ( $this->parsely->api_key_is_set() && true !== $parsely_options['disable_javascript'] ) { | |
| 45 | + if ( $this->parsely->site_id_is_set() && true !== $parsely_options['disable_javascript'] ) { | |
| 46 | 46 | add_action( 'init', array( $this, 'register_scripts' ) ); |
| 47 | 47 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_js_tracker' ) ); |
| 48 | 48 | } |
| 49 | 49 | } |
| @@ -48,14 +48,12 @@ | ||
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | - * Register JavaScripts, if there's an API key value saved. | |
| 52 | + * Registers scripts, if there's a Site ID value saved. | |
| 53 | 53 | * |
| 54 | 54 | * @since 2.5.0 |
| 55 | 55 | * @since 3.0.0 Rename from register_js |
| 56 | - * | |
| 57 | - * @return void | |
| 58 | 56 | */ |
| 59 | 57 | public function register_scripts(): void { |
| 60 | 58 | wp_register_script( |
| 61 | 59 | 'wp-parsely-tracker', |
| @@ -64,9 +62,10 @@ | ||
| 64 | 62 | PARSELY_VERSION, |
| 65 | 63 | true |
| 66 | 64 | ); |
| 67 | 65 | |
| 68 | - $loader_asset = require plugin_dir_path( PARSELY_FILE ) . 'build/loader.asset.php'; | |
| 66 | + $loader_asset = get_asset_info( 'build/loader.asset.php' ); | |
| 67 | + | |
| 69 | 68 | wp_register_script( |
| 70 | 69 | 'wp-parsely-loader', |
| 71 | 70 | plugin_dir_url( PARSELY_FILE ) . 'build/loader.js', |
| 72 | 71 | $loader_asset['dependencies'], |
| @@ -79,12 +78,14 @@ | ||
| 79 | 78 | * Enqueues the JavaScript code required to send off beacon requests. |
| 80 | 79 | * |
| 81 | 80 | * @since 2.5.0 Rename from insert_parsely_javascript |
| 82 | 81 | * @since 3.0.0 Rename from load_js_tracker |
| 83 | - * | |
| 84 | - * @return void | |
| 85 | 82 | */ |
| 86 | 83 | public function enqueue_js_tracker(): void { |
| 84 | + if ( is_preview() ) { | |
| 85 | + return; | |
| 86 | + } | |
| 87 | + | |
| 87 | 88 | $parsely_options = $this->parsely->get_options(); |
| 88 | 89 | |
| 89 | 90 | global $post; |
| 90 | 91 | $display = true; |
| @@ -90,9 +91,9 @@ | ||
| 90 | 91 | $display = true; |
| 91 | 92 | if ( in_array( get_post_type(), $parsely_options['track_post_types'], true ) && ! Parsely::post_has_trackable_status( $post ) ) { |
| 92 | 93 | $display = false; |
| 93 | 94 | } |
| 94 | - if ( ! $parsely_options['track_authenticated_users'] && $this->parsely->parsely_is_user_logged_in() ) { | |
| 95 | + if ( ! $parsely_options['track_authenticated_users'] && $this->parsely->is_blog_member_logged_in() ) { | |
| 95 | 96 | $display = false; |
| 96 | 97 | } |
| 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 ) ) { |
| 98 | 99 | $display = false; |
| @@ -117,18 +118,23 @@ | ||
| 117 | 118 | |
| 118 | 119 | wp_enqueue_script( 'wp-parsely-loader' ); |
| 119 | 120 | wp_enqueue_script( 'wp-parsely-tracker' ); |
| 120 | 121 | |
| 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' ); | |
| 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' ); | |
| 126 | 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 | + } | |
| 127 | 133 | } |
| 128 | 134 | |
| 129 | 135 | /** |
| 130 | - * Filter the script tag for certain scripts to add needed attributes. | |
| 136 | + * Filters the script tag for certain scripts to add needed attributes. | |
| 131 | 137 | * |
| 132 | 138 | * @since 2.5.0 |
| 133 | 139 | * |
| 134 | 140 | * @param string $tag The `script` tag for the enqueued script. |
| @@ -136,10 +142,9 @@ | ||
| 136 | 142 | * @param string $src Unused? The script's source URL. |
| 137 | 143 | * @return string Amended `script` tag. |
| 138 | 144 | */ |
| 139 | 145 | public function script_loader_tag( string $tag, string $handle, string $src ): string { |
| 140 | - $parsely_options = $this->parsely->get_options(); | |
| 141 | - if ( in_array( | |
| 146 | + if ( \in_array( | |
| 142 | 147 | $handle, |
| 143 | 148 | array( |
| 144 | 149 | 'wp-parsely-loader', |
| 145 | 150 | 'wp-parsely-tracker', |
| @@ -163,13 +168,16 @@ | ||
| 163 | 168 | } |
| 164 | 169 | |
| 165 | 170 | if ( null !== $tag && 'wp-parsely-tracker' === $handle ) { |
| 166 | 171 | $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 | + if ( null !== $tag ) { | |
| 174 | + $tag = str_replace( | |
| 175 | + ' src=', | |
| 176 | + ' data-parsely-site="' . esc_attr( $this->parsely->get_site_id() ) . '" src=', | |
| 177 | + $tag | |
| 178 | + ); | |
| 179 | + } | |
| 172 | 180 | } |
| 173 | 181 | |
| 174 | 182 | return $tag ?? ''; |
| 175 | 183 | } |