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

Require API token for self-hosters #195

Merged
merged 16 commits into from
Apr 2, 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
16 changes: 10 additions & 6 deletions src/Actions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Plausible Analytics | Actions.
*
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
Expand All @@ -13,6 +14,7 @@
class Actions {
/**
* Constructor.
*
* @since 1.0.0
* @access public
* @return void
Expand All @@ -24,6 +26,7 @@ public function __construct() {

/**
* Register Assets.
*
* @since 1.0.0
* @access public
* @return void
Expand All @@ -38,7 +41,7 @@ public function maybe_register_assets() {
*/
if ( ( ! empty( $user_role ) && ! isset( $settings[ 'tracked_user_roles' ] ) ) ||
( ! empty( $user_role ) && ! in_array( $user_role, $settings[ 'tracked_user_roles' ], true ) ) ) {
return;
return; // @codeCoverageIgnore
}

$version =
Expand All @@ -65,6 +68,7 @@ public function maybe_register_assets() {

/**
* Create admin bar nodes.
*
* @since 1.3.0
* @access public
*
Expand All @@ -76,12 +80,12 @@ public function admin_bar_node( $admin_bar ) {
$disable = ! empty( Helpers::get_settings()[ 'disable_toolbar_menu' ] );

if ( $disable ) {
return;
return; // @codeCoverageIgnore
}

// Add main admin bar node.
$args[] = [
'id' => 'plausible-admin-bar',
'id' => 'plausible-analytics',
'title' => 'Plausible Analytics',
];

Expand All @@ -93,7 +97,7 @@ public function admin_bar_node( $admin_bar ) {
'id' => 'view-analytics',
'title' => esc_html__( 'View Analytics', 'plausible-analytics' ),
'href' => admin_url( 'index.php?page=plausible_analytics_statistics' ),
'parent' => 'plausible-admin-bar',
'parent' => 'plausible-analytics',
];

// Add link to individual page stats.
Expand All @@ -109,7 +113,7 @@ public function admin_bar_node( $admin_bar ) {
is_home() ? '' : $uri,
admin_url( 'index.php?page=plausible_analytics_statistics' )
),
'parent' => 'plausible-admin-bar',
'parent' => 'plausible-analytics',
];
}
}
Expand All @@ -119,7 +123,7 @@ public function admin_bar_node( $admin_bar ) {
'id' => 'settings',
'title' => esc_html__( 'Settings', 'plausible-analytics' ),
'href' => admin_url( 'options-general.php?page=plausible_analytics' ),
'parent' => 'plausible-admin-bar',
'parent' => 'plausible-analytics',
];

foreach ( $args as $arg ) {
Expand Down
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
12 changes: 11 additions & 1 deletion src/Admin/Module.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Plausible Analytics | Module.
*
* @since 1.3.0
* @package WordPress
* @subpackage Plausible Analytics
Expand All @@ -16,6 +17,7 @@
class Module {
/**
* Build properties.
*
* @return void
*/
public function __construct() {
Expand All @@ -24,6 +26,7 @@ public function __construct() {

/**
* Filters & Actions.
*
* @return void
*/
private function init() {
Expand All @@ -33,6 +36,7 @@ private function init() {

/**
* Decide whether we should install the module, or not.
*
* @since 1.3.0
*
* @param array $settings Current settings, already written to the DB.
Expand All @@ -49,6 +53,7 @@ public function maybe_install_module( $old_settings, $settings ) {

/**
* Takes care of installing the M(ust)U(se) plugin when the Proxy is enabled.
*
* @since 1.3.0
* @return void.
*/
Expand Down Expand Up @@ -102,6 +107,7 @@ private function show_module_not_installed_error() {

/**
* Uninstall the Speed Module, generates JS files and all related settings when the proxy is disabled.
*
* @since 1.3.0
* @return void.
*/
Expand Down Expand Up @@ -149,6 +155,7 @@ public function uninstall() {
* Check if a directory is empty.
* This works because a new FilesystemIterator will initially point to the first file in the folder -
* if there are no files in the folder, valid() will return false.
*
* @see https://www.php.net/manual/en/directoryiterator.valid.php
* @since 1.3.0
*
Expand All @@ -164,6 +171,7 @@ private function dir_is_empty( $dir ) {

/**
* Test the proxy before enabling the option.
*
* @since 1.3.0
*
* @param mixed $settings
Expand Down Expand Up @@ -221,6 +229,7 @@ public function maybe_enable_proxy( $settings, $old_settings ) {
* is_ssl() only checks the current scheme that is used, which fails in a Nginx Reverse Proxy configuration (where the scheme is HTTP behind the
* proxy), this function is a custom wrapper which also checks the WordPress configuration for the presence of "https" in the configured Home
* URL.
*
* @return bool
*/
private function is_ssl() {
Expand All @@ -229,13 +238,14 @@ private function is_ssl() {

/**
* Runs a quick internal call to the WordPress API to make sure it's accessable.
*
* @since 1.3.0
* @return bool
* @throws Exception
*/
private function test_proxy( $run = true ) {
// Should we run the test?
if ( ! $run ) {
if ( ! apply_filters( 'plausible_analytics_module_run_test_proxy', $run ) ) {
return false;
}

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
Loading
Loading