Skip to content

Commit

Permalink
Required API token for self-hosters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan0sz committed Apr 2, 2024
1 parent 26bba85 commit 8ab9254
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 60 deletions.
7 changes: 5 additions & 2 deletions src/Admin/Actions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Plausible Analytics | Admin Actions.
*
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
Expand All @@ -15,6 +16,7 @@
class Actions {
/**
* Constructor.
*
* @since 1.0.0
* @access public
* @return void
Expand All @@ -26,6 +28,7 @@ public function __construct() {

/**
* Register Assets.
*
* @since 1.0.0
* @since 1.3.0 Don't load CSS admin-wide. JS needs to load admin-wide, since we're throwing admin-wide, dismissable notices.
* @access public
Expand Down Expand Up @@ -53,6 +56,7 @@ public function register_assets( $current_page ) {

/**
* Redirect to Configuration Wizard on first boot.
*
* @return void
*/
public function maybe_redirect_to_wizard() {
Expand All @@ -67,9 +71,8 @@ public function maybe_redirect_to_wizard() {
}

$wizard_done = get_option( 'plausible_analytics_wizard_done', false );
$self_hosted = Helpers::get_settings()[ 'self_hosted_domain' ];

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

wp_redirect( $url );
Expand Down
45 changes: 0 additions & 45 deletions src/Admin/SelfHosted.php

This file was deleted.

17 changes: 16 additions & 1 deletion src/Admin/Settings/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Plausible Analytics | Settings API.
*
* @since 1.3.0
* @package WordPress
* @subpackage Plausible Analytics
Expand All @@ -16,6 +17,7 @@
class API {
/**
* Admin Setting Fields.
*
* @since 1.3.0
* @access public
* @var array
Expand All @@ -24,20 +26,23 @@ class API {

/**
* Slide IDs and Titles
*
* @since v2.0.0
* @var string[] $slides
*/
private $slides = [];

/**
* Slide IDs and Descriptions
*
* @since v2.0.0
* @var array $slides_description
*/
private $slides_description = [];

/**
* Render Fields.
*
* @since 1.3.0
* @access public
* @return void
Expand All @@ -46,7 +51,7 @@ public function settings_page() {
wp_nonce_field( 'plausible_analytics_toggle_option' );

$settings = Helpers::get_settings();
$followed_wizard = get_option( 'plausible_analytics_wizard_done' ) || ! empty( $settings[ 'self_hosted_domain' ] );
$followed_wizard = get_option( 'plausible_analytics_wizard_done' );

/**
* On-boarding wizard.
Expand Down Expand Up @@ -195,6 +200,7 @@ public function settings_page() {

/**
* Renders the configuration wizard on the Settings page.
*
* @return void
*/
private function show_wizard() {
Expand Down Expand Up @@ -361,6 +367,7 @@ class="plausible-analytics-wizard-completed-step flex hidden items-start mb-6">

/**
* Renders the notice "bubble", which is further handled by JS.
*
* @return void
*/
private function render_notices_field() {
Expand Down Expand Up @@ -442,6 +449,7 @@ private function get_wizard_option_properties( $slug ) {

/**
* Render Header Navigation.
*
* @since 1.3.0
* @access public
* @return void
Expand Down Expand Up @@ -486,6 +494,7 @@ public function render_navigation() {

/**
* Render Quick Actions
*
* @since 1.3.0
* @return string
*/
Expand All @@ -508,6 +517,7 @@ private function render_quick_actions() {

/**
* Get Quick Actions.
*
* @since 1.3.0
* @return array
*/
Expand Down Expand Up @@ -539,6 +549,7 @@ private function get_quick_actions() {

/**
* Render Group Field.
*
* @since 1.3.0
* @access public
* @return string
Expand Down Expand Up @@ -585,6 +596,7 @@ public function render_group_field( array $group, $hide_header = false ) {

/**
* Render Text Field.
*
* @since 1.3.0
* @access public
* @return string
Expand Down Expand Up @@ -640,6 +652,7 @@ class="plausible-analytics-button border-0 hover:cursor-pointer inline-flex item

/**
* Render Checkbox Field.
*
* @since 1.3.0
* @access public
* @return string
Expand Down Expand Up @@ -684,6 +697,7 @@ class="plausible-analytics-toggle <?php echo $checked && ! $disabled ? 'bg-indig

/**
* Render textarea field.
*
* @since 1.2.5
* @access public
*
Expand Down Expand Up @@ -714,6 +728,7 @@ class="block w-full max-w-xl border-gray-300 dark:border-gray-700 resize-none sh

/**
* Render just the label, and allow insertion of anything using the hook beside it.
*
* @since 1.3.0
*
* @param array $field
Expand Down
29 changes: 19 additions & 10 deletions src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Plausible Analytics | Settings API.
*
* @since 1.3.0
* @package WordPress
* @subpackage Plausible Analytics
Expand All @@ -21,6 +22,7 @@ class Page extends API {

/**
* Constructor.
*
* @since 1.3.0
* @access public
* @return void
Expand Down Expand Up @@ -56,20 +58,17 @@ public function __construct() {
'value' => $domain,
],
[
'label' => esc_html__( 'API token', 'plausible-analytics' ),
'slug' => 'api_token',
'type' => 'text',
'value' => $settings[ 'api_token' ],
'disabled' => ! empty( $settings[ 'self_hosted_domain' ] ),
'label' => esc_html__( 'API token', 'plausible-analytics' ),
'slug' => 'api_token',
'type' => 'text',
'value' => $settings[ 'api_token' ],
],
[
'label' => empty( $settings[ 'domain_name' ] ) || empty( $settings[ 'api_token' ] ) ?
esc_html__( 'Connect', 'plausible-analytics' ) : esc_html__( 'Connected', 'plausible-analytics' ),
'slug' => 'connect_plausible_analytics',
'type' => 'button',
'disabled' => ( empty( $settings[ 'self_hosted_domain' ] ) ) &&
( empty( $settings[ 'domain_name' ] ) || empty( $settings[ 'api_token' ] ) ) ||
( ! empty( $settings[ 'domain_name' ] ) && ! empty( $settings[ 'api_token' ] ) ),
'disabled' => ( empty( $settings[ 'domain_name' ] ) || empty( $settings[ 'api_token' ] ) ),
],
],
],
Expand Down Expand Up @@ -392,7 +391,7 @@ public function __construct() {
];
}

if ( empty( $settings[ 'api_token' ] ) && empty( $settings[ 'self_hosted_domain' ] ) ) {
if ( empty( $settings[ 'api_token' ] ) ) {
$this->fields[ 'general' ][ 0 ][ 'fields' ][] = [
'label' => '',
'slug' => 'api_token_missing',
Expand Down Expand Up @@ -421,6 +420,7 @@ public function __construct() {

/**
* Action hooks.
*
* @return void
*/
private function init() {
Expand Down Expand Up @@ -477,6 +477,7 @@ private function build_user_roles_array( $slug, $disable_elements = [] ) {

/**
* Register Menu.
*
* @since 1.0.0
* @access public
* @return void
Expand Down Expand Up @@ -546,6 +547,7 @@ public function register_menu() {

/**
* A little hack to add some classes to the core #wpcontent div.
*
* @return void
*/
public function add_background_color() {
Expand All @@ -556,6 +558,7 @@ public function add_background_color() {

/**
* Statistics Page via Embed feature.
*
* @since 1.2.0
* @access public
* @return void
Expand Down Expand Up @@ -608,6 +611,7 @@ public function render_analytics_dashboard() {
* 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
* those who haven't properly set it up.
*
* @since v1.2.5
* For self-hosters the View Stats option doesn't need to be enabled, if a Shared Link is entered, we can assume they want to View Stats.
* For regular users, the shared link is provisioned by the API, so it shouldn't be empty.
Expand Down Expand Up @@ -682,6 +686,7 @@ public function render_analytics_dashboard() {

/**
* Display connect button.
*
* @return void
*/
public function connect_button() {
Expand All @@ -702,6 +707,7 @@ public function connect_button() {

/**
* Renders the warning for the Enable Proxy option.
*
* @since 1.3.0
* @return void
*/
Expand All @@ -724,12 +730,13 @@ public function proxy_warning() {

/**
* Show notice when API token notice is disabled.
*
* @return void
*/
public function option_disabled_by_self_hosted_domain() {
echo wp_kses(
__(
'This option is disabled, because the <strong>Domain Name</strong> setting is enabled under <em>Self-Hosted</em> settings.',
'This option is disabled, because a Self-Hosted <strong>Domain Name</strong> is entered.',
'plausible-analytics'
),
'post'
Expand All @@ -738,6 +745,7 @@ public function option_disabled_by_self_hosted_domain() {

/**
* Renders the analytics dashboard link if the option is enabled.
*
* @since 2.0.0
* @return void
*/
Expand All @@ -758,6 +766,7 @@ public function enable_analytics_dashboard_notice() {

/**
* Renders the Self-hosted warning if the Proxy is enabled.
*
* @since 1.3.3
* @return void
*/
Expand Down
Loading

0 comments on commit 8ab9254

Please sign in to comment.