Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enhanced measurement toggles #203

Merged
merged 7 commits into from
May 1, 2024
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
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.

2 changes: 2 additions & 0 deletions 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_url() . '";' );
}

/**
Expand Down
29 changes: 14 additions & 15 deletions src/Admin/Settings/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Plausible\Analytics\WP\Admin\Settings;

use Plausible\Analytics\WP\Ajax;
use Plausible\Analytics\WP\Helpers;

/**
Expand Down Expand Up @@ -581,23 +582,21 @@ public function render_group_field( array $group, $hide_header = false ) {
</header>
<?php endif; ?>
<?php if ( ! empty( $fields ) ): ?>
<?php $is_list = count( $fields ) > 1; ?>
<?php if ( $is_list ) {
foreach ( $fields as $field ) {
if ( $field[ 'type' ] === 'hook' ) {
continue;
}

if ( $field[ 'type' ] !== 'checkbox' ) {
$is_list = false;

break;
}
<?php
/**
* if $fields contains more than one checkbox field type, this is a list, which is treated different in @see Ajax::toggle_option()
*/
$is_list = array_filter(
$fields,
function ( $field ) {
return $field[ 'type' ] === 'checkbox';
}
);

foreach ( $fields as $field ) {
echo call_user_func( [ $this, "render_{$field['type']}_field" ], $field, count( $is_list ) > 1 );
}
foreach ( $fields as $field ): ?>
<?php echo call_user_func( [ $this, "render_{$field['type']}_field" ], $field, $is_list ); ?>
<?php endforeach; ?>
?>
<?php endif; ?>
</div>
<?php
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_url(), 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
22 changes: 12 additions & 10 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_url() . '/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_url(),
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 All @@ -630,12 +630,14 @@ public function render_analytics_dashboard() {
if ( $shared_link && $page_url ) {
$shared_link .= "&page={$page_url}";
}

$hosted_domain = Helpers::get_hosted_domain_url();
?>
<div id="plausible-analytics-stats">
<iframe plausible-embed=""
src="<?php echo "{$shared_link}&embed=true&theme=light&background=transparent"; ?>"
scrolling="no" loading="lazy" style="border: 0; width: 100%; height: 1750px; "></iframe>
<script async src="https://plausible.io/js/embed.host.js"></script>
<script async src="<?php echo $hosted_domain; ?>/js/embed.host.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
let iframe = '';
Expand Down
25 changes: 23 additions & 2 deletions src/Admin/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public function __construct() {
* @return void
*
* @throws Exception
*
* @codeCoverageIgnore
*/
public function run() {
// @codeCoverageIgnoreStart
$plausible_analytics_version = get_option( 'plausible_analytics_version' );

// If version doesn't exist, then consider it `1.0.0`.
Expand Down Expand Up @@ -71,7 +72,10 @@ public function run() {
if ( version_compare( $plausible_analytics_version, '2.0.3', '<' ) ) {
$this->upgrade_to_203();
}
// @codeCoverageIgnoreEnd

if ( version_compare( $plausible_analytics_version, '2.1.0', '<' ) ) {
$this->upgrade_to_210();
}

// Add required upgrade routines for future versions here.
}
Expand Down Expand Up @@ -248,4 +252,21 @@ private function upgrade_to_203() {

update_option( 'plausible_analytics_version', '2.0.3' );
}

/**
* v2.0.8 and older contained a bug that
*
* @return void
*/
public function upgrade_to_210() {
$settings = Helpers::get_settings();

if ( ! is_array( $settings[ 'enhanced_measurements' ] ) ) {
$settings[ 'enhanced_measurements' ] = [];
}

update_option( 'plausible_analytics_settings', $settings );

update_option( 'plausible_analytics_version', '2.1.0' );
}
}
Loading
Loading