From 10576e26cf948e918bfcc33c2022673c42d00497 Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Tue, 3 Oct 2023 08:50:26 +0200 Subject: [PATCH] Move REST API to its own class --- inc/main.php | 5 +- src/Admin/AdminAPI.php | 150 ++++++++++++++++++++++++++ src/Admin/SetupWizard.php | 142 ++---------------------- vendor/composer/autoload_classmap.php | 5 +- vendor/composer/autoload_static.php | 5 +- vendor/composer/installed.php | 4 +- 6 files changed, 163 insertions(+), 148 deletions(-) create mode 100644 src/Admin/AdminAPI.php diff --git a/inc/main.php b/inc/main.php index 9240e00..99bdaac 100644 --- a/inc/main.php +++ b/inc/main.php @@ -8,6 +8,7 @@ namespace TLA_Media\GTM_Kit; use Automattic\WooCommerce\Utilities\FeaturesUtil; +use TLA_Media\GTM_Kit\Admin\AdminAPI; use TLA_Media\GTM_Kit\Admin\Analytics; use TLA_Media\GTM_Kit\Admin\HelpOptionsPage; use TLA_Media\GTM_Kit\Admin\IntegrationsOptionsPage; @@ -91,7 +92,7 @@ function gtmkit_frontend_init(): void { $rest_api_server = new RestAPIServer(); $util = new Util( $rest_api_server ); - ( new SetupWizard( $options, $util ) )->rest_init(); + ( new AdminAPI( $options, $util ) )->rest_init(); if ( ! $options->get( 'general', 'just_the_container' ) ) { BasicDatalayerData::register( $options ); @@ -133,7 +134,7 @@ function gtmkit_admin_init(): void { $rest_api_server = new RestAPIServer(); $util = new Util( $rest_api_server ); - ( new SetupWizard( $options, $util ) )->hooks(); + SetupWizard::register(); Analytics::register( $options, $util ); GeneralOptionsPage::register( $options, $util ); IntegrationsOptionsPage::register( $options, $util ); diff --git a/src/Admin/AdminAPI.php b/src/Admin/AdminAPI.php new file mode 100644 index 0000000..62beb69 --- /dev/null +++ b/src/Admin/AdminAPI.php @@ -0,0 +1,150 @@ +options = $options; + $this->util = $util; + } + + /** + * Initialize REST + * + * @return void + */ + public function rest_init() { + add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); + } + + + /** + * Register REST routes + * + * @return void + */ + public function register_rest_routes(): void { + $this->util->rest_api_server->register_rest_route( + '/get-install-data', + [ + 'methods' => 'POST', + 'callback' => [ $this, 'get_install_data' ], + ] + ); + + $this->util->rest_api_server->register_rest_route( + '/get-options', + [ + 'methods' => 'POST', + 'callback' => [ $this, 'get_options' ], + ] + ); + + $this->util->rest_api_server->register_rest_route( + '/set-options', + [ + 'methods' => 'POST', + 'callback' => [ $this, 'set_options' ], + ] + ); + + $this->util->rest_api_server->register_rest_route( + '/get-site-data', + [ + 'methods' => 'POST', + 'callback' => [ $this, 'get_site_data' ], + ] + ); + } + + /** + * Permission callback + * + * @return true|WP_Error + */ + public function permission_callback() { + $capability = is_multisite() ? 'manage_network_options' : 'manage_options'; + $capability = apply_filters( 'gtmkit_admin_capability', $capability ); + + if ( ! current_user_can( $capability ) ) { + return new WP_Error( 'rest_forbidden', esc_html__( 'Only authenticated users can access endpoint.', 'gtm-kit' ), [ 'status' => 401 ] ); + } + + return true; + } + + /** + * Get install data + * + * @return void + */ + public function get_install_data(): void { + $other_plugin_settings = ( new PluginDataImport() )->get_all(); + wp_send_json_success( $other_plugin_settings ); + } + + /** + * Get options + * + * @return void + */ + public function get_options(): void { + wp_send_json_success( $this->options->get_all_raw() ); + } + + /** + * Set options + * + * @return void + */ + public function set_options(): void { + $new_options = json_decode( file_get_contents( 'php://input' ), true ); + $this->options->set( $new_options ); + + wp_send_json_success( $this->options->get_all_raw() ); + } + + /** + * Get site data + * + * @return void + */ + public function get_site_data(): void { + $site_data = $this->util->get_site_data( $this->options->get_all_raw() ); + wp_send_json_success( $site_data ); + } +} diff --git a/src/Admin/SetupWizard.php b/src/Admin/SetupWizard.php index d226739..67dcc0f 100644 --- a/src/Admin/SetupWizard.php +++ b/src/Admin/SetupWizard.php @@ -25,48 +25,14 @@ final class SetupWizard { const SLUG = 'gtmkit_setup_wizard'; /** - * An instance of Options. - * - * @var Options - */ - private $options; - - /** - * An instance of Util. - * - * @var Util + * Register the setup wizard. */ - private $util; - - /** - * Constructor - * - * @param Options $options An instance of Options. - * @param Util $util An instance of Util. - */ - public function __construct( Options $options, Util $util ) { - $this->options = $options; - $this->util = $util; - } + public static function register(): void { + $page = new SetupWizard(); - /** - * Run all the hooks needed for the Setup Wizard. - */ - public function hooks() { - add_action( 'admin_init', [ $this, 'maybe_redirect_after_activation' ], 9999 ); - add_action( 'admin_menu', [ $this, 'add_dashboard_page' ], 20 ); - - add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); - add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); - } - - /** - * Initialize REST - * - * @return void - */ - public function rest_init() { - add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); + add_action( 'admin_init', [ $page, 'maybe_redirect_after_activation' ], 9999 ); + add_action( 'admin_menu', [ $page, 'add_dashboard_page' ], 20 ); + add_action( 'admin_enqueue_scripts', [ $page, 'enqueue_assets' ] ); } /** @@ -231,61 +197,6 @@ class="gtmkit-bg-color-primary gtmkit-text-white gtmkit-rounded-md gtmkit-py-4 g util->rest_api_server->register_rest_route( - '/get-install-data', - [ - 'methods' => 'POST', - 'callback' => [ $this, 'get_install_data' ], - ] - ); - - $this->util->rest_api_server->register_rest_route( - '/get-options', - [ - 'methods' => 'POST', - 'callback' => [ $this, 'get_options' ], - ] - ); - - $this->util->rest_api_server->register_rest_route( - '/set-options', - [ - 'methods' => 'POST', - 'callback' => [ $this, 'set_options' ], - ] - ); - - $this->util->rest_api_server->register_rest_route( - '/get-site-data', - [ - 'methods' => 'POST', - 'callback' => [ $this, 'get_site_data' ], - ] - ); - } - - /** - * Permission callback - * - * @return true|WP_Error - */ - public function permission_callback() { - $capability = is_multisite() ? 'manage_network_options' : 'manage_options'; - $capability = apply_filters( 'gtmkit_admin_capability', $capability ); - - if ( ! current_user_can( $capability ) ) { - return new WP_Error( 'rest_forbidden', esc_html__( 'Only authenticated users can access endpoint.', 'gtm-kit' ), [ 'status' => 401 ] ); - } - - return true; - } - /** * Render page * @@ -294,45 +205,4 @@ public function permission_callback() { public function render_page() { $this->setup_wizard_content(); } - - /** - * Get install data - * - * @return void - */ - public function get_install_data(): void { - $other_plugin_settings = ( new PluginDataImport() )->get_all(); - wp_send_json_success( $other_plugin_settings ); - } - - /** - * Get options - * - * @return void - */ - public function get_options(): void { - wp_send_json_success( $this->options->get_all_raw() ); - } - - /** - * Set options - * - * @return void - */ - public function set_options(): void { - $new_options = json_decode( file_get_contents( 'php://input' ), true ); - $this->options->set( $new_options ); - - wp_send_json_success( $this->options->get_all_raw() ); - } - - /** - * Get site data - * - * @return void - */ - public function get_site_data(): void { - $site_data = $this->util->get_site_data( $this->options->get_all_raw() ); - wp_send_json_success( $site_data ); - } } diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 66e1b78..4f47845 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -8,14 +8,11 @@ return array( 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', 'TLA_Media\\GTM_Kit\\Admin\\AbstractOptionsPage' => $baseDir . '/src/Admin/AbstractOptionsPage.php', + 'TLA_Media\\GTM_Kit\\Admin\\AdminAPI' => $baseDir . '/src/Admin/AdminAPI.php', 'TLA_Media\\GTM_Kit\\Admin\\Analytics' => $baseDir . '/src/Admin/Analytics.php', 'TLA_Media\\GTM_Kit\\Admin\\GeneralOptionsPage' => $baseDir . '/src/Admin/GeneralOptionsPage.php', 'TLA_Media\\GTM_Kit\\Admin\\HelpOptionsPage' => $baseDir . '/src/Admin/HelpOptionsPage.php', 'TLA_Media\\GTM_Kit\\Admin\\IntegrationsOptionsPage' => $baseDir . '/src/Admin/IntegrationsOptionsPage.php', - 'TLA_Media\\GTM_Kit\\Admin\\MetaBox' => $baseDir . '/src/Admin/MetaBox.php', - 'TLA_Media\\GTM_Kit\\Admin\\OptionTab' => $baseDir . '/src/Admin/OptionTab.php', - 'TLA_Media\\GTM_Kit\\Admin\\OptionTabs' => $baseDir . '/src/Admin/OptionTabs.php', - 'TLA_Media\\GTM_Kit\\Admin\\OptionsForm' => $baseDir . '/src/Admin/OptionsForm.php', 'TLA_Media\\GTM_Kit\\Admin\\SetupWizard' => $baseDir . '/src/Admin/SetupWizard.php', 'TLA_Media\\GTM_Kit\\Common\\RestAPIServer' => $baseDir . '/src/Common/RestAPIServer.php', 'TLA_Media\\GTM_Kit\\Common\\Util' => $baseDir . '/src/Common/Util.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 159c861..d548fcf 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -23,14 +23,11 @@ class ComposerStaticInit3b585b55966a016a3d1b071eb261592a public static $classMap = array ( 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', 'TLA_Media\\GTM_Kit\\Admin\\AbstractOptionsPage' => __DIR__ . '/../..' . '/src/Admin/AbstractOptionsPage.php', + 'TLA_Media\\GTM_Kit\\Admin\\AdminAPI' => __DIR__ . '/../..' . '/src/Admin/AdminAPI.php', 'TLA_Media\\GTM_Kit\\Admin\\Analytics' => __DIR__ . '/../..' . '/src/Admin/Analytics.php', 'TLA_Media\\GTM_Kit\\Admin\\GeneralOptionsPage' => __DIR__ . '/../..' . '/src/Admin/GeneralOptionsPage.php', 'TLA_Media\\GTM_Kit\\Admin\\HelpOptionsPage' => __DIR__ . '/../..' . '/src/Admin/HelpOptionsPage.php', 'TLA_Media\\GTM_Kit\\Admin\\IntegrationsOptionsPage' => __DIR__ . '/../..' . '/src/Admin/IntegrationsOptionsPage.php', - 'TLA_Media\\GTM_Kit\\Admin\\MetaBox' => __DIR__ . '/../..' . '/src/Admin/MetaBox.php', - 'TLA_Media\\GTM_Kit\\Admin\\OptionTab' => __DIR__ . '/../..' . '/src/Admin/OptionTab.php', - 'TLA_Media\\GTM_Kit\\Admin\\OptionTabs' => __DIR__ . '/../..' . '/src/Admin/OptionTabs.php', - 'TLA_Media\\GTM_Kit\\Admin\\OptionsForm' => __DIR__ . '/../..' . '/src/Admin/OptionsForm.php', 'TLA_Media\\GTM_Kit\\Admin\\SetupWizard' => __DIR__ . '/../..' . '/src/Admin/SetupWizard.php', 'TLA_Media\\GTM_Kit\\Common\\RestAPIServer' => __DIR__ . '/../..' . '/src/Common/RestAPIServer.php', 'TLA_Media\\GTM_Kit\\Common\\Util' => __DIR__ . '/../..' . '/src/Common/Util.php', diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index a93121a..45fa7c9 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'tlamedia/gtm-kit', 'pretty_version' => 'dev-main', 'version' => 'dev-main', - 'reference' => 'abedcf11d848052e7d1eef575faadc888d6a1806', + 'reference' => '959422ed7da824e003b15aeff2398a86a2b25972', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -13,7 +13,7 @@ 'tlamedia/gtm-kit' => array( 'pretty_version' => 'dev-main', 'version' => 'dev-main', - 'reference' => 'abedcf11d848052e7d1eef575faadc888d6a1806', + 'reference' => '959422ed7da824e003b15aeff2398a86a2b25972', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(),