From 3aef4b79ece752ebe92828f4e6463be6fee9aef9 Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Thu, 12 Sep 2024 09:04:36 +0200 Subject: [PATCH] Use single admin page base URL --- src/Admin/GeneralOptionsPage.php | 23 +++++++++++------------ src/Admin/HelpOptionsPage.php | 19 +++++++++---------- src/Admin/IntegrationsOptionsPage.php | 6 ++---- src/Admin/SetupWizard.php | 1 + src/Admin/TemplatesOptionsPage.php | 16 +++++++--------- src/Admin/UpgradesOptionsPage.php | 15 +++++++-------- src/Common/Util.php | 27 +++++++++++++++++++++++++++ 7 files changed, 64 insertions(+), 43 deletions(-) diff --git a/src/Admin/GeneralOptionsPage.php b/src/Admin/GeneralOptionsPage.php index d075a74..4ea6474 100644 --- a/src/Admin/GeneralOptionsPage.php +++ b/src/Admin/GeneralOptionsPage.php @@ -123,18 +123,17 @@ public function localize_script( string $page_slug, string $script_handle ): voi 'gtmkit-' . $script_handle . '-script', 'gtmkitSettings', [ - 'rootId' => 'gtmkit-settings', - 'currentPage' => $page_slug, - 'root' => \esc_url_raw( rest_url() ), - 'nonce' => \wp_create_nonce( 'wp_rest' ), - 'tutorials' => $this->get_tutorials(), - 'integrations' => Integrations::get_integrations(), - 'dashboardUrl' => \menu_page_url( 'gtmkit_general', false ), - 'integrationsUrl' => \menu_page_url( 'gtmkit_integrations', false ), - 'settings' => $this->options->get_all_raw(), - 'site_data' => $this->util->get_site_data( $this->options->get_all_raw() ), - 'user_roles' => $this->get_user_roles(), - 'notifications' => $this->get_notifications(), + 'rootId' => 'gtmkit-settings', + 'currentPage' => $page_slug, + 'root' => \esc_url_raw( rest_url() ), + 'nonce' => \wp_create_nonce( 'wp_rest' ), + 'tutorials' => $this->get_tutorials(), + 'integrations' => Integrations::get_integrations(), + 'adminURL' => $this->util->get_admin_page_url(), + 'settings' => $this->options->get_all_raw(), + 'site_data' => $this->util->get_site_data( $this->options->get_all_raw() ), + 'user_roles' => $this->get_user_roles(), + 'notifications' => $this->get_notifications(), ] ); } diff --git a/src/Admin/HelpOptionsPage.php b/src/Admin/HelpOptionsPage.php index 36ca718..3f5b602 100644 --- a/src/Admin/HelpOptionsPage.php +++ b/src/Admin/HelpOptionsPage.php @@ -87,16 +87,15 @@ public function localize_script( string $page_slug, string $script_handle ): voi 'gtmkit-' . $script_handle . '-script', 'gtmkitSettings', [ - 'rootId' => 'gtmkit-settings', - 'currentPage' => $page_slug, - 'root' => \esc_url_raw( rest_url() ), - 'nonce' => \wp_create_nonce( 'wp_rest' ), - 'tutorials' => $this->get_tutorials(), - 'dashboardUrl' => \menu_page_url( 'gtmkit_general', false ), - 'integrationsUrl' => \menu_page_url( 'gtmkit_integrations', false ), - 'settings' => $this->options->get_all_raw(), - 'site_data' => [ 'gtmkit_version' => GTMKIT_VERSION ], - 'isPremium' => ( new PremiumConditional() )->is_met(), + 'rootId' => 'gtmkit-settings', + 'currentPage' => $page_slug, + 'root' => \esc_url_raw( rest_url() ), + 'nonce' => \wp_create_nonce( 'wp_rest' ), + 'tutorials' => $this->get_tutorials(), + 'adminURL' => $this->util->get_admin_page_url(), + 'settings' => $this->options->get_all_raw(), + 'site_data' => [ 'gtmkit_version' => GTMKIT_VERSION ], + 'isPremium' => ( new PremiumConditional() )->is_met(), ] ); } diff --git a/src/Admin/IntegrationsOptionsPage.php b/src/Admin/IntegrationsOptionsPage.php index eb40874..7941ba4 100644 --- a/src/Admin/IntegrationsOptionsPage.php +++ b/src/Admin/IntegrationsOptionsPage.php @@ -114,10 +114,8 @@ public function localize_script( string $page_slug, string $script_handle ): voi 'nonce' => \wp_create_nonce( 'wp_rest' ), 'isPremium' => ( new PremiumConditional() )->is_met(), 'integrations' => Integrations::get_integrations(), - 'dashboardUrl' => \menu_page_url( 'gtmkit_general', false ), - 'integrationsUrl' => \menu_page_url( 'gtmkit_integrations', false ), - 'templatesUrl' => \menu_page_url( 'gtmkit_templates', false ), - 'pluginInstallUrl' => $admin_url . 'plugin-install.php?tab=search&type=term&s=', + 'adminPageUrl' => $this->util->get_admin_page_url(), + 'pluginInstallUrl' => $this->util->get_plugin_install_url(), 'plugins' => $this->get_plugins(), 'taxonomyOptions' => $taxonomy_options, 'settings' => $this->options->get_all_raw(), diff --git a/src/Admin/SetupWizard.php b/src/Admin/SetupWizard.php index 2e7e99c..ef068c0 100644 --- a/src/Admin/SetupWizard.php +++ b/src/Admin/SetupWizard.php @@ -146,6 +146,7 @@ public function enqueue_assets( string $hook ): void { 'currentPage' => 'wizard', 'root' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), + 'adminURL' => $this->util->get_admin_page_url(), 'dashboardUrl' => menu_page_url( 'gtmkit_general', false ), 'templatesUrl' => menu_page_url( 'gtmkit_templates', false ), 'helpUrl' => menu_page_url( 'gtmkit_help', false ), diff --git a/src/Admin/TemplatesOptionsPage.php b/src/Admin/TemplatesOptionsPage.php index bf303e0..da0d6c4 100644 --- a/src/Admin/TemplatesOptionsPage.php +++ b/src/Admin/TemplatesOptionsPage.php @@ -85,15 +85,13 @@ public function localize_script( string $page_slug, string $script_handle ): voi 'gtmkit-' . $script_handle . '-script', 'gtmkitSettings', [ - 'rootId' => 'gtmkit-settings', - 'currentPage' => $page_slug, - 'root' => \esc_url_raw( rest_url() ), - 'nonce' => \wp_create_nonce( 'wp_rest' ), - 'templates' => $this->get_templates(), - 'dashboardUrl' => \menu_page_url( 'gtmkit_general', false ), - 'integrationsUrl' => \menu_page_url( 'gtmkit_integrations', false ), - 'templatesUrl' => \menu_page_url( 'gtmkit_templates', false ), - 'settings' => $this->options->get_all_raw(), + 'rootId' => 'gtmkit-settings', + 'currentPage' => $page_slug, + 'root' => \esc_url_raw( rest_url() ), + 'nonce' => \wp_create_nonce( 'wp_rest' ), + 'templates' => $this->get_templates(), + 'adminURL' => $this->util->get_admin_page_url(), + 'settings' => $this->options->get_all_raw(), ] ); } diff --git a/src/Admin/UpgradesOptionsPage.php b/src/Admin/UpgradesOptionsPage.php index cf0e18c..74ad09f 100644 --- a/src/Admin/UpgradesOptionsPage.php +++ b/src/Admin/UpgradesOptionsPage.php @@ -87,14 +87,13 @@ public function localize_script( string $page_slug, string $script_handle ): voi 'gtmkit-' . $script_handle . '-script', 'gtmkitSettings', [ - 'rootId' => 'gtmkit-settings', - 'currentPage' => $page_slug, - 'root' => \esc_url_raw( rest_url() ), - 'nonce' => \wp_create_nonce( 'wp_rest' ), - 'opportunities' => $this->get_upgrade_opportunities(), - 'dashboardUrl' => \menu_page_url( 'gtmkit_general', false ), - 'integrationsUrl' => \menu_page_url( 'gtmkit_integrations', false ), - 'settings' => $this->options->get_all_raw(), + 'rootId' => 'gtmkit-settings', + 'currentPage' => $page_slug, + 'root' => \esc_url_raw( rest_url() ), + 'nonce' => \wp_create_nonce( 'wp_rest' ), + 'opportunities' => $this->get_upgrade_opportunities(), + 'adminURL' => $this->util->get_admin_page_url(), + 'settings' => $this->options->get_all_raw(), ] ); } diff --git a/src/Common/Util.php b/src/Common/Util.php index 0bace87..2153b13 100644 --- a/src/Common/Util.php +++ b/src/Common/Util.php @@ -452,4 +452,31 @@ public function normalize_and_hash_email_address( } return $this->normalize_and_hash( $hash_algorithm, $normalized_email, true ); } + + /** + * Get admin page base URL + * + * @return string + */ + public function get_admin_page_url(): string { + return $this->get_admin_url() . 'admin.php?page=gtmkit_'; + } + + /** + * Get the plugin install URL + * + * @return string + */ + public function get_plugin_install_url(): string { + return $this->get_admin_url() . 'plugin-install.php?tab=search&type=term&s='; + } + + /** + * Get admin url + * + * @return string + */ + private function get_admin_url(): string { + return is_network_admin() ? network_admin_url() : admin_url(); + } }