From 8ab9254a37325ba4a082678aa9d7f412991cb21a Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:39:01 +0200 Subject: [PATCH] Required API token for self-hosters. --- src/Admin/Actions.php | 7 ++++-- src/Admin/SelfHosted.php | 45 ------------------------------------- src/Admin/Settings/API.php | 17 +++++++++++++- src/Admin/Settings/Page.php | 29 +++++++++++++++--------- src/Ajax.php | 9 +++++++- src/Plugin.php | 5 ++++- 6 files changed, 52 insertions(+), 60 deletions(-) delete mode 100644 src/Admin/SelfHosted.php diff --git a/src/Admin/Actions.php b/src/Admin/Actions.php index f8702e17..fef8b74b 100644 --- a/src/Admin/Actions.php +++ b/src/Admin/Actions.php @@ -1,6 +1,7 @@ init(); - } - - /** - * Action and filter hooks. - * @return void - */ - private function init() { - add_filter( 'pre_update_option_plausible_analytics_settings', [ $this, 'maybe_remove_api_token' ], 10 ); - } - - /** - * Removing the API token will effectively disable all auto provisioning, which Self Hosters can't use either way. - * - * @param $settings - * - * @return void - */ - public function maybe_remove_api_token( $settings ) { - if ( empty( $settings[ 'self_hosted_domain' ] ) ) { - return $settings; - } - - $settings[ 'api_token' ] = ''; - - return $settings; - } -} diff --git a/src/Admin/Settings/API.php b/src/Admin/Settings/API.php index 91b685e5..92fc7343 100644 --- a/src/Admin/Settings/API.php +++ b/src/Admin/Settings/API.php @@ -2,6 +2,7 @@ /** * Plausible Analytics | Settings API. + * * @since 1.3.0 * @package WordPress * @subpackage Plausible Analytics @@ -16,6 +17,7 @@ class API { /** * Admin Setting Fields. + * * @since 1.3.0 * @access public * @var array @@ -24,6 +26,7 @@ class API { /** * Slide IDs and Titles + * * @since v2.0.0 * @var string[] $slides */ @@ -31,6 +34,7 @@ class API { /** * Slide IDs and Descriptions + * * @since v2.0.0 * @var array $slides_description */ @@ -38,6 +42,7 @@ class API { /** * Render Fields. + * * @since 1.3.0 * @access public * @return void @@ -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. @@ -195,6 +200,7 @@ public function settings_page() { /** * Renders the configuration wizard on the Settings page. + * * @return void */ private function show_wizard() { @@ -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() { @@ -442,6 +449,7 @@ private function get_wizard_option_properties( $slug ) { /** * Render Header Navigation. + * * @since 1.3.0 * @access public * @return void @@ -486,6 +494,7 @@ public function render_navigation() { /** * Render Quick Actions + * * @since 1.3.0 * @return string */ @@ -508,6 +517,7 @@ private function render_quick_actions() { /** * Get Quick Actions. + * * @since 1.3.0 * @return array */ @@ -539,6 +549,7 @@ private function get_quick_actions() { /** * Render Group Field. + * * @since 1.3.0 * @access public * @return string @@ -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 @@ -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 @@ -684,6 +697,7 @@ class="plausible-analytics-toggle $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' ] ) ), ], ], ], @@ -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', @@ -421,6 +420,7 @@ public function __construct() { /** * Action hooks. + * * @return void */ private function init() { @@ -477,6 +477,7 @@ private function build_user_roles_array( $slug, $disable_elements = [] ) { /** * Register Menu. + * * @since 1.0.0 * @access public * @return void @@ -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() { @@ -556,6 +558,7 @@ public function add_background_color() { /** * Statistics Page via Embed feature. + * * @since 1.2.0 * @access public * @return void @@ -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. @@ -682,6 +686,7 @@ public function render_analytics_dashboard() { /** * Display connect button. + * * @return void */ public function connect_button() { @@ -702,6 +707,7 @@ public function connect_button() { /** * Renders the warning for the Enable Proxy option. + * * @since 1.3.0 * @return void */ @@ -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 Domain Name setting is enabled under Self-Hosted settings.', + 'This option is disabled, because a Self-Hosted Domain Name is entered.', 'plausible-analytics' ), 'post' @@ -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 */ @@ -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 */ diff --git a/src/Ajax.php b/src/Ajax.php index e0f08acc..776fcb66 100644 --- a/src/Ajax.php +++ b/src/Ajax.php @@ -1,6 +1,7 @@ name ] = trim( $option->value ); // Validate API token, if this is the API token field. - if ( $option->name === 'api_token' && empty( $settings[ 'self_hosted_domain' ] ) ) { + if ( $option->name === 'api_token' ) { $this->validate_api_token( $option->value ); } } diff --git a/src/Plugin.php b/src/Plugin.php index b2e25eec..3b771e81 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -6,11 +6,13 @@ /** * Loads and registers plugin functionality through WordPress hooks. + * * @since 1.0.0 */ final class Plugin { /** * Registers functionality with WordPress hooks. + * * @since 1.0.0 * @access public * @return void @@ -25,6 +27,7 @@ public function register() { /** * Registers the individual services of the plugin. + * * @since 1.0.0 * @access public * @return void @@ -37,7 +40,6 @@ public function register_services() { new Admin\Actions(); new Admin\Module(); new Admin\Provisioning(); - new Admin\SelfHosted(); } new Actions(); @@ -50,6 +52,7 @@ public function register_services() { /** * Loads the plugin's translated strings. + * * @since 1.0.0 * @access public * @return void