Skip to content

Commit

Permalink
Add extensions loading to settings, no longer require YODA conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevalk committed Jun 29, 2022
1 parent 0ef9bd3 commit 8e21bd0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 33 deletions.
1 change: 1 addition & 0 deletions phpcs.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment" />
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
<exclude name="WordPress.PHP.YodaConditions" />
</rule>

<rule ref="Generic.Arrays.DisallowLongArraySyntax.Found">
Expand Down
29 changes: 16 additions & 13 deletions src/Admin/Settings/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ class="plausible-analytics-btn plausible-analytics-save-btn"
* @since 1.3.0
* @access public
*
* @return mixed
* @return string
*/
public function render_text_field( array $field ) {
ob_start();
$value = ! empty( $field['value'] ) ? $field['value'] : '';
?>
<label for="">
<label for="<?php echo $field['slug']; ?>">
<?php echo esc_attr( $field['label'] ); ?>
</label>
<input type="text" name="plausible_analytics_settings[<?php echo $field['slug']; ?>]" value="<?php echo $value; ?>" />
<input id="<?php echo $field['slug']; ?>" type="text" name="plausible_analytics_settings[<?php echo $field['slug']; ?>]" value="<?php echo $value; ?>" />
<?php
return ob_get_clean();
}
Expand All @@ -96,38 +96,37 @@ public function render_text_field( array $field ) {
* @since 1.3.0
* @access public
*
* @return mixed
* @return string
*/
public function render_group_field( array $group ) {
$settings = Helpers::get_settings();
$toggle = ! ! $group['toggle'];
$toggle = $group['toggle'];
$fields = $group['fields'];
$field_value = ! empty( $settings[ $group['slug'] ] ) ? $settings[ $group['slug'] ] : false;
$is_checked = checked( $field_value, true, false );
$domain_name = ! empty( $settings['domain_name'] ) ? $settings['domain_name'] : '';
ob_start();
?>
<div class="plausible-analytics-admin-field">
<div class="plausible-analytics-admin-field-header">
<label for="">
<?php echo esc_attr( $group['label'] ); ?>
</label>
<?php if ( $toggle ) { ?>
<?php if ( $toggle === true ) { ?>
<label class="plausible-analytics-switch">
<input <?php echo $is_checked; ?> class="plausible-analytics-switch-checkbox" name="plausible_analytics_settings[<?php echo $group['slug']; ?>]" value="1" type="checkbox">
<span class="plausible-analytics-switch-slider"></span>
</label>
<?php } else { ?>
<a target="_blank" class="plausible-analytics-link" href="https://plausible.io/<?php echo $domain_name; ?>">
<?php esc_html_e( 'Open Analytics', 'plausible-analytics' ); ?>
<?php } elseif ( is_array( $toggle ) ) { ?>
<a target="_blank" class="plausible-analytics-link" href="<?php echo $toggle['anchor']; ?>">
<?php echo $toggle['label']; ?>
</a>
<?php } ?>
</div>
<div class="plausible-analytics-admin-field-body">
<?php
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
echo call_user_func( [ $this, "render_{$field['type']}_field" ], $field );
echo call_user_func( [ $this, "render_{$field['type']}_field" ], $field ) . '<br/>';
}
}
?>
Expand All @@ -146,7 +145,7 @@ public function render_group_field( array $group ) {
* @since 1.3.0
* @access public
*
* @return mixed
* @return string
*/
public function render_checkbox_field( array $field ) {
ob_start();
Expand All @@ -155,6 +154,7 @@ public function render_checkbox_field( array $field ) {
?>
<span class="plausible-checkbox-list">
<input
id="<?php echo $field['slug']; ?>"
type="checkbox"
name="plausible_analytics_settings[<?php echo $field['slug']; ?>][]"
value="<?php echo $value; ?>"
Expand All @@ -164,7 +164,10 @@ public function render_checkbox_field( array $field ) {
'';
?>
/>
<?php echo esc_attr( $field['label'] ); ?>
<label for="<?php echo $field['slug']; ?>"><?php echo $field['label']; ?></label>
<?php if ( ! empty( $field['docs'] ) ) { ?>
- <a href="<?php echo $field['docs']; ?>"><?php echo $field['docs_label']; ?></a>
<?php } ?>
</span>
<?php
return ob_get_clean();
Expand Down
62 changes: 57 additions & 5 deletions src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plausible Analytics | Settings API.
*
* @since 1.3.0
* @since 1.3.0
*
* @package WordPress
* @subpackage Plausible Analytics
Expand Down Expand Up @@ -47,7 +47,10 @@ public function __construct() {
esc_html__( 'Follow these instructions', 'plausible-analytics' ),
esc_html__( 'to add your site to Plausible.', 'plausible-analytics' )
),
'toggle' => false,
'toggle' => [
'anchor' => 'https://plausible.io/' . $domain,
'label' => esc_html__( 'Open Analytics', 'plausible-analytics' ),
],
'fields' => [
[
'label' => esc_html__( 'Domain Name', 'plausible-analytics' ),
Expand Down Expand Up @@ -82,6 +85,52 @@ public function __construct() {
],
],
],
[
'label' => esc_html__( 'Enhanced measurements', 'plausible-analytics' ),
'slug' => 'enhanced_measurements',
'type' => 'group',
// translators: %1$s replaced with <code>outbound-links</code>.
'desc' => '<strong>' . esc_html__( 'Note:', 'plausible-analytics' ) . '</strong> ' . esc_html__( 'for each of these extensions, you have to set the goals manually!', 'plausible-analytics' ) . '<br><br>'
. sprintf( esc_html__( 'By default, we load just the %1$s extension, you can enable other extensions here.', 'plausible-analytics' ), '<a href="https://plausible.io/docs/outbound-link-click-tracking"></a><code>outbound-links</code></a>' ),
'toggle' => [
'anchor' => 'https://plausible.io/docs/script-extensions',
'label' => __( 'Documentation', 'plausible-analytics' ),
],
'fields' => [
'outbound-links' => [
'label' => esc_html__( 'Outbound links', 'plausible-analytics' ),
'docs' => 'https://plausible.io/docs/outbound-link-click-tracking#step-2-create-a-custom-event-goal-in-your-plausible-analytics-account',
'docs_label' => esc_html__( 'Goal setup', 'plausible-analytics' ),
'slug' => 'file-downloads',
'type' => 'checkbox',
'value' => '1',
],
'file-downloads' => [
'label' => esc_html__( 'File downloads', 'plausible-analytics' ),
'docs' => 'https://plausible.io/docs/file-downloads-tracking#step-2-create-a-custom-event-goal-in-your-plausible-analytics-account',
'docs_label' => esc_html__( 'Goal setup', 'plausible-analytics' ),
'slug' => 'file-downloads',
'type' => 'checkbox',
'value' => '1',
],
'hash' => [
'label' => esc_html__( 'Hash-based routing', 'plausible-analytics' ),
'docs' => 'https://plausible.io/docs/hash-based-routing',
'docs_label' => esc_html__( 'Documentation', 'plausible-analytics' ),
'slug' => 'hash',
'type' => 'checkbox',
'value' => '1',
],
'compat' => [
'label' => esc_html__( 'IE compatibility', 'plausible-analytics' ),
'docs' => 'https://plausible.io/docs/script-extensions#scriptcompatjs',
'docs_label' => esc_html__( 'Documentation', 'plausible-analytics' ),
'slug' => 'compat',
'type' => 'checkbox',
'value' => '1',
],
],
],
[
'label' => esc_html__( 'View your stats in your WordPress dashboard', 'plausible-analytics' ),
'slug' => 'is_shared_link',
Expand Down Expand Up @@ -229,7 +278,7 @@ public function register_menu() {
* @since 1.3.0
* @access public
*
* @return void|mixed
* @return void
*/
public function render_page_header() {
$screen = get_current_screen();
Expand All @@ -250,7 +299,7 @@ public function render_page_header() {
?>
<div class="plausible-analytics-header">
<div class="plausible-analytics-logo">
<img src="<?php echo PLAUSIBLE_ANALYTICS_PLUGIN_URL . '/assets/dist/images/icon.png'; ?>" alt="<?php esc_html_e( 'Plausible Analytics', 'plausible-analytics' ); ?>" />
<img src="<?php echo PLAUSIBLE_ANALYTICS_PLUGIN_URL . '/assets/dist/images/icon.png'; ?>" alt="<?php esc_html_e( 'Plausible Analytics', 'plausible-analytics' ); ?>"/>
</div>
<div class="plausible-analytics-header-content">
<div class="plausible-analytics-title">
Expand Down Expand Up @@ -360,6 +409,7 @@ public function statistics_page() {
'%1$s',
esc_html__( 'You don\'t have sufficient privileges to access the analytics dashboard. Please contact administrator of the website to grant you the access.', 'plausible-analytics' )
);

return;
?>
</div>
Expand All @@ -373,7 +423,9 @@ public function statistics_page() {
$shared_link .= "&page={$_GET[ 'page-url' ]}";
}
?>
<iframe plausible-embed="" src="<?php echo "{$shared_link}&embed=true&theme=light&background=transparent"; ?>" scrolling="no" frameborder="0" loading="lazy" style="width: 100%; height: 1750px; "></iframe>
<iframe plausible-embed=""
src="<?php echo "{$shared_link}&embed=true&theme=light&background=transparent"; ?>" scrolling="no"
frameborder="0" loading="lazy" style="width: 100%; height: 1750px; "></iframe>
<script async="" src="https://plausible.io/js/embed.host.js"></script>
<?php
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plausible Analytics | Upgrades
*
* @since 1.3.0
* @since 1.3.0
*
* @package WordPress
* @subpackage Plausible Analytics
Expand Down
2 changes: 1 addition & 1 deletion src/Includes/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plausible Analytics | Actions.
*
* @since 1.0.0
* @since 1.0.0
*
* @package WordPress
* @subpackage Plausible Analytics
Expand Down
16 changes: 12 additions & 4 deletions src/Includes/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plausible Analytics | Filters.
*
* @since 1.0.0
* @since 1.0.0
*
* @package WordPress
* @subpackage Plausible Analytics
Expand Down Expand Up @@ -45,9 +45,17 @@ public function add_plausible_attributes( $tag, $handle ) {
return $tag;
}

$settings = Helpers::get_settings();
$api_url = Helpers::get_data_api_url();
$domain_name = $settings['domain_name'];
$settings = Helpers::get_settings();
$api_url = Helpers::get_data_api_url();
$domain_name = $settings['domain_name'];
$id_replacement = '';

// If we're loading the compat script, we need the correct id attribute. If not, we can just remove it.
if ( isset( $settings['compat'] ) && $settings['compat'][0] === '1' ) {
$id_replacement = " id='plausible'";
}

$tag = str_replace( " id='plausible-analytics-js'", $id_replacement, $tag );

return str_replace( ' src', " async defer data-domain='{$domain_name}' data-api='{$api_url}' src", $tag );
}
Expand Down
31 changes: 22 additions & 9 deletions src/Includes/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plausible Analytics | Helpers
*
* @since 1.0.0
* @since 1.0.0
*
* @package WordPress
* @subpackage Plausible Analytics
Expand Down Expand Up @@ -40,11 +40,16 @@ public static function get_domain() {
* @return string
*/
public static function get_analytics_url() {
$settings = self::get_settings();
$domain = $settings['domain_name'];
$default_domain = 'plausible.io';
$is_outbound_link = apply_filters( 'plausible_analytics_enable_outbound_links', true );
$file_name = $is_outbound_link ? 'plausible.outbound-links' : 'plausible';
$settings = self::get_settings();
$domain = $settings['domain_name'];
$default_domain = 'plausible.io';
$file_name = 'plausible';

foreach ( [ 'outbound-links', 'file-downloads', 'compat', 'hash' ] as $extension ) {
if ( ! empty( $settings[ $extension ] ) && $settings[ $extension ][0] === '1' ) {
$file_name .= '.' . $extension;
}
}

// Triggered when self-hosted analytics is enabled.
if (
Expand Down Expand Up @@ -98,7 +103,7 @@ public static function display_toggle_switch( $name ) {
$individual_settings = ! empty( $settings[ $name ] ) ? $settings[ $name ] : '';
?>
<label class="plausible-analytics-switch">
<input <?php checked( $individual_settings, 'true' ); ?> class="plausible-analytics-switch-checkbox" name="plausible_analytics_settings[<?php echo $name; ?>]" value="1" type="checkbox" />
<input <?php checked( $individual_settings, 'true' ); ?> class="plausible-analytics-switch-checkbox" name="plausible_analytics_settings[<?php echo $name; ?>]" value="1" type="checkbox"/>
<span class="plausible-analytics-switch-slider"></span>
</label>
<?php
Expand All @@ -113,7 +118,15 @@ public static function display_toggle_switch( $name ) {
* @return array
*/
public static function get_settings() {
return get_option( 'plausible_analytics_settings', [] );
$settings = get_option( 'plausible_analytics_settings', [] );

// Keep around for backwards compatibility reasons.
$track_outbound_links = apply_filters( 'plausible_analytics_enable_outbound_links', isset( $settings['outbound-links'][0] ) ? $settings['outbound-links'][0] : true );
if ( $track_outbound_links ) {
$settings['outbound-links'][0] = 1;
}

return $settings;
}

/**
Expand Down Expand Up @@ -235,7 +248,7 @@ public static function clean( $var ) {
}

/**
* Get user role for the loggedin user.
* Get user role for the logged-in user.
*
* @since 1.3.0
* @access public
Expand Down

0 comments on commit 8e21bd0

Please sign in to comment.