Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 59 additions & 53 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,55 @@ private function init() {
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_search_queries_script' ], 11 );
}

/**
* Enqueue cloaked affiliate links assets if the option is enabled.
*
* @return void
*/
public function maybe_enqueue_cloaked_affiliate_links_assets() {
if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::CLOAKED_AFFILIATE_LINKS ) && Helpers::main_script_is_registered() ) {
wp_enqueue_script(
'plausible-affiliate-links',
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-affiliate-links.js',
[ 'plausible-analytics' ],
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-affiliate-links.js' ),
);

$affiliate_links = Helpers::get_settings()['affiliate_links'] ?? [];

wp_add_inline_script( 'plausible-affiliate-links', 'const plausibleAffiliateLinks = ' . wp_json_encode( $affiliate_links ) . ';', 'before' );
}
}

/**
* Enqueue 404 script if the option is enabled.
*
* @return void
*/
public function maybe_enqueue_four_o_four_script() {
$is_404 = apply_filters( 'plausible_analytics_is_404', is_404() );

if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::FOUR_O_FOUR ) && $is_404 && Helpers::main_script_is_registered() ) {
$data = wp_json_encode(
[
'props' => [
'path' => 'document.location.pathname',
],
]
);

/**
* document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
*/
$data = str_replace( '"document.location.pathname"', 'document.location.pathname', $data );
$event_name = EnhancedMeasurements::FOUR_O_FOUR;

wp_add_inline_script(
'plausible-analytics',
"document.addEventListener('DOMContentLoaded', () => { plausible( $event_name, $data ); });"
);
}
}

/**
* Register main JS if this user should be tracked.
Expand All @@ -40,11 +89,18 @@ private function init() {
public function maybe_enqueue_main_script() {
$settings = Helpers::get_settings();
$user_role = Helpers::get_user_role();
$url = $this->get_js_url( true );

if ( ! $url ) {
echo '<!-- ' . __( 'Please enter your plugin token to start using Plausible Analytics.', 'plausible-analytics' ) . " -->\n";

return;
}

/**
* This is a dummy script that will allow us to attach inline scripts further down the line.
*/
wp_register_script( 'plausible-analytics', $this->get_js_url( true ), [], null, apply_filters( 'plausible_load_js_in_footer', false ) );
wp_register_script( 'plausible-analytics', $url, [], null, apply_filters( 'plausible_load_js_in_footer', false ) );

/**
* Bail if tracked_user_roles is empty (which means no roles should be tracked) or if the current role should not be tracked.
Expand Down Expand Up @@ -85,63 +141,13 @@ protected function get_js_url( bool $local = false ) {
return Helpers::get_js_url( $local );
}

/**
* Enqueue cloaked affiliate links assets if the option is enabled.
*
* @return void
*/
public function maybe_enqueue_cloaked_affiliate_links_assets() {
if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::CLOAKED_AFFILIATE_LINKS ) ) {
wp_enqueue_script(
'plausible-affiliate-links',
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-affiliate-links.js',
[ 'plausible-analytics' ],
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-affiliate-links.js' ),
);

$affiliate_links = Helpers::get_settings()['affiliate_links'] ?? [];

wp_add_inline_script( 'plausible-affiliate-links', 'const plausibleAffiliateLinks = ' . wp_json_encode( $affiliate_links ) . ';', 'before' );
}
}

/**
* Enqueue 404 script if the option is enabled.
*
* @return void
*/
public function maybe_enqueue_four_o_four_script() {
$is_404 = apply_filters( 'plausible_analytics_is_404', is_404() );

if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::FOUR_O_FOUR ) && $is_404 ) {
$data = wp_json_encode(
[
'props' => [
'path' => 'document.location.pathname',
],
]
);

/**
* document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
*/
$data = str_replace( '"document.location.pathname"', 'document.location.pathname', $data );
$event_name = EnhancedMeasurements::FOUR_O_FOUR;

wp_add_inline_script(
'plausible-analytics',
"document.addEventListener('DOMContentLoaded', () => { plausible( $event_name, $data ); });"
);
}
}

/**
* Enqueue Query Params script if the option is enabled.
*
* @return void
*/
public function maybe_enqueue_query_params_script() {
if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::QUERY_PARAMS ) ) {
if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::QUERY_PARAMS ) && Helpers::main_script_is_registered() ) {
$query_params = Helpers::get_settings()['query_params'] ?? [];
$props = [];

Expand Down Expand Up @@ -176,7 +182,7 @@ public function maybe_enqueue_query_params_script() {
public function maybe_enqueue_search_queries_script() {
$is_search = apply_filters( 'plausible_analytics_is_search', is_search() );

if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::SEARCH_QUERIES ) && $is_search ) {
if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::SEARCH_QUERIES ) && $is_search && Helpers::main_script_is_registered() ) {
global $wp_query;

$search_source = isset( $_REQUEST['search_source'] ) ? sanitize_text_field( $_REQUEST['search_source'] ) : wp_get_referer();
Expand Down
12 changes: 9 additions & 3 deletions src/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,26 @@ private function maybe_download() {
$remote = Helpers::get_js_url();
$local = Helpers::get_js_path();

if ( ! $remote || ! $local ) {
return false;
}

return $this->download_file( $remote, $local );
}

/**
* Downloads a remote file to this server.
*
* @param string $local_file Absolute path to where to store the $remote_file.
* @since 1.3.0
*
* @param string $remote_file Full URL to file to download.
*
* @param string $local_file Absolute path to where to store the $remote_file.
*
* @return bool True when successful. False if it fails.
* @throws Exception
* @throws InvalidArgument
*
* @since 1.3.0
* @throws Exception
*/
private function download_file( $remote_file, $local_file ) {
$file_contents = wp_remote_get( $remote_file );
Expand Down
Loading
Loading