Skip to content

Commit

Permalink
Introduced get_hosted_domain() function, refactored logic to use it w…
Browse files Browse the repository at this point in the history
…here needed and make sure Create API token and other relevant links use it as well.
  • Loading branch information
Dan0sz committed May 1, 2024
1 parent fdfad95 commit a8ed1f9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion assets/src/js/admin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ document.addEventListener('DOMContentLoaded', () => {
let domain = document.getElementById('domain_name').value;
domain = domain.replace('/', '%2F');

window.open(`https://plausible.io/${domain}/settings/integrations?new_token=WordPress`, '_blank', 'location=yes,height=768,width=1024,scrollbars=yes,status=no');
window.open(`${plausible_analytics_hosted_domain}/${domain}/settings/integrations?new_token=WordPress`, '_blank', 'location=yes,height=768,width=1024,scrollbars=yes,status=no');
},

/**
Expand Down
1 change: 1 addition & 0 deletions assets/src/js/admin/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/Admin/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function register_assets( $current_page ) {
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-admin.js' ),
true
);

wp_add_inline_script( 'plausible-admin', 'var plausible_analytics_hosted_domain = "' . Helpers::get_hosted_domain() . '";' );
}

/**
Expand All @@ -71,7 +73,7 @@ public function maybe_redirect_to_wizard() {
$wizard_done = get_option( 'plausible_analytics_wizard_done', false );

if ( ! $wizard_done ) {
$url = admin_url( 'options-general.php?page=plausible_analytics#welcome_slide' );
$url = admin_url( 'options - general . php ? page = plausible_analytics#welcome_slide' );

wp_redirect( $url );

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Settings/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function connect_button() {

<?php else: ?>
<?php
$url = sprintf( 'https://plausible.io/%s/settings/integrations?new_token=Wordpress', Helpers::get_domain() );
$url = sprintf( '%s/%s/settings/integrations?new_token=Wordpress', Helpers::get_hosted_domain(), Helpers::get_domain() );
?>
<a href="<?php esc_attr_e( $url, 'plausible-analytics' ); ?>" target="_blank" class="plausible-analytics-btn">
<?php esc_html_e( 'Connect to Plausible', 'plausible-analytics' ); ?>
Expand Down
18 changes: 9 additions & 9 deletions src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ class Page extends API {
public function __construct() {
$this->init();

$settings = Helpers::get_settings();
$domain = Helpers::get_domain();
$self_hosted_domain = defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ? PLAUSIBLE_SELF_HOSTED_DOMAIN : $settings[ 'self_hosted_domain' ];
$settings = Helpers::get_settings();

$this->fields = [
'general' => [
Expand All @@ -87,14 +85,14 @@ public function __construct() {
),
'post'
),
'https://plausible.io/sites'
Helpers::get_hosted_domain() . '/sites'
),
'fields' => [
[
'label' => esc_html__( 'Domain name', 'plausible-analytics' ),
'slug' => 'domain_name',
'type' => 'text',
'value' => $domain,
'value' => Helpers::get_domain(),
],
[
'label' => esc_html__( 'API token', 'plausible-analytics' ),
Expand Down Expand Up @@ -333,7 +331,8 @@ public function __construct() {
'label' => esc_html__( 'Domain name', 'plausible-analytics' ),
'slug' => 'self_hosted_domain',
'type' => 'text',
'value' => $self_hosted_domain,
'value' => defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ? PLAUSIBLE_SELF_HOSTED_DOMAIN :
$settings[ 'self_hosted_domain' ],
'placeholder' => 'e.g. ' . Helpers::get_domain(),
'disabled' => Helpers::proxy_enabled(),
],
Expand Down Expand Up @@ -368,8 +367,9 @@ public function __construct() {
'type' => 'text',
'value' => $settings[ 'self_hosted_shared_link' ],
'placeholder' => sprintf(
wp_kses( __( 'E.g. https://plausible.io/share/%s?auth=XXXXXXXXXXXX', 'plausible-analytics' ), 'post' ),
$domain
wp_kses( __( 'E.g. %s/share/%s?auth=XXXXXXXXXXXX', 'plausible-analytics' ), 'post' ),
Helpers::get_hosted_domain(),
Helpers::get_domain()
),
'disabled' => Helpers::proxy_enabled(),
],
Expand Down Expand Up @@ -613,7 +613,7 @@ public function render_analytics_dashboard() {
/**
* Prior to this version, the default value would contain an example "auth" key, i.e. XXXXXXXXX.
* When this option was saved to the database, underlying code would fail, throwing a CORS related error in browsers.
* Now, we explicitly check for the existence of this example "auth" key, and display a human readable error message to
* Now, we explicitly check for the existence of this example "auth" key, and display a human-readable error message to
* those who haven't properly set it up.
*
* @since v1.2.5
Expand Down
101 changes: 49 additions & 52 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class Helpers {
* @throws Exception
*/
public static function get_js_url( $local = false ) {
$settings = self::get_settings();
$file_name = self::get_filename( $local );
$default_domain = 'plausible.io';
$domain = $default_domain;
$file_name = self::get_filename( $local );

/**
* If Avoid ad blockers is enabled, return URL to local file.
Expand All @@ -36,21 +33,36 @@ public static function get_js_url( $local = false ) {
return esc_url( self::get_proxy_resource( 'cache_url' ) . $file_name . '.js' );
}

// Allows for hard-coding the self-hosted domain.
if ( defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ) {
$domain = PLAUSIBLE_SELF_HOSTED_DOMAIN; // @codeCoverageIgnore
return esc_url( self::get_hosted_domain() . "/js/$file_name.js" );
}

/**
* Get filename (without file extension)
*
* @since 1.3.0
* @return string
* @throws Exception
*/
public static function get_filename( $local = false ) {
$settings = self::get_settings();
$file_name = 'plausible';

if ( $local && self::proxy_enabled() ) {
return self::get_proxy_resource( 'file_alias' );
}

/**
* Set $domain to self_hosted_domain if it exists.
*/
if ( ! empty( $settings[ 'self_hosted_domain' ] ) && $domain === $default_domain ) {
$domain = $settings[ 'self_hosted_domain' ];
foreach ( [ 'outbound-links', 'file-downloads', 'tagged-events', 'revenue', 'pageview-props', 'compat', 'hash' ] as $extension ) {
if ( is_array( $settings[ 'enhanced_measurements' ] ) && in_array( $extension, $settings[ 'enhanced_measurements' ], true ) ) {
$file_name .= '.' . $extension;
}
}

$url = "https://{$domain}/js/{$file_name}.js";
// Load exclusions.js if any excluded pages are set.
if ( ! empty( $settings[ 'excluded_pages' ] ) ) {
$file_name .= '.' . 'exclusions';
}

return esc_url( $url );
return $file_name;
}

/**
Expand Down Expand Up @@ -81,34 +93,6 @@ public static function get_settings() {
return apply_filters( 'plausible_analytics_settings', wp_parse_args( $settings, $defaults ) );
}

/**
* Get filename (without file extension)
*
* @since 1.3.0
* @return string
*/
public static function get_filename( $local = false ) {
$settings = self::get_settings();
$file_name = 'plausible';

if ( $local && self::proxy_enabled() ) {
return self::get_proxy_resource( 'file_alias' );
}

foreach ( [ 'outbound-links', 'file-downloads', 'tagged-events', 'revenue', 'pageview-props', 'compat', 'hash' ] as $extension ) {
if ( is_array( $settings[ 'enhanced_measurements' ] ) && in_array( $extension, $settings[ 'enhanced_measurements' ], true ) ) {
$file_name .= '.' . $extension;
}
}

// Load exclusions.js if any excluded pages are set.
if ( ! empty( $settings[ 'excluded_pages' ] ) ) {
$file_name .= '.' . 'exclusions';
}

return $file_name;
}

/**
* Is the proxy enabled?
*
Expand Down Expand Up @@ -185,6 +169,28 @@ public static function get_proxy_resources() {
return $resources;
}

/**
* Returns the domain where Plausible Analytics is hosted: self-hosted or cloud.
*
* @return string
*/
public static function get_hosted_domain() {
$settings = self::get_settings();

if ( defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ) {
return 'https://' . PLAUSIBLE_SELF_HOSTED_DOMAIN;
}

if ( ! empty( $settings[ 'self_hosted_domain' ] ) ) {
/**
* Until proven otherwise, let's just assume people are all on SSL.
*/
return 'https://' . $settings[ 'self_hosted_domain' ];
}

return 'https://plausible.io';
}

/**
* @param $option_name
* @param $option_value
Expand Down Expand Up @@ -267,23 +273,14 @@ public static function get_domain() {
* @return string
*/
public static function get_data_api_url() {
$settings = self::get_settings();
$url = 'https://plausible.io/api/event';

if ( self::proxy_enabled() ) {
// This'll make sure the API endpoint is properly registered when we're testing.
$append = isset( $_GET[ 'plausible_proxy' ] ) ? '?plausible_proxy=1' : '';

return self::get_rest_endpoint() . $append;
}

// Triggered when self-hosted analytics is enabled.
if ( ! empty( $settings[ 'self_hosted_domain' ] ) ) {
$default_domain = $settings[ 'self_hosted_domain' ];
$url = "https://{$default_domain}/api/event";
}

return esc_url( $url );
return esc_url( self::get_hosted_domain() . '/api/event' );
}

/**
Expand Down

0 comments on commit a8ed1f9

Please sign in to comment.