Skip to content

Commit

Permalink
Move REST API to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Oct 3, 2023
1 parent eff6f8a commit 10576e2
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 148 deletions.
5 changes: 3 additions & 2 deletions inc/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
150 changes: 150 additions & 0 deletions src/Admin/AdminAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* GTM Kit plugin file.
*
* @package GTM Kit
*/

namespace TLA_Media\GTM_Kit\Admin;

use TLA_Media\GTM_Kit\Common\Util;
use TLA_Media\GTM_Kit\Installation\PluginDataImport;
use TLA_Media\GTM_Kit\Options;
use WP_Error;

/**
* Class for the admin REST API.
*/
final class AdminAPI {

/**
* An instance of Options.
*
* @var Options
*/
private $options;

/**
* An instance of Util.
*
* @var Util
*/
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;
}

/**
* 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 );
}
}
142 changes: 6 additions & 136 deletions src/Admin/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}

/**
Expand Down Expand Up @@ -231,61 +197,6 @@ class="gtmkit-bg-color-primary gtmkit-text-white gtmkit-rounded-md gtmkit-py-4 g
<?php
}

/**
* 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;
}

/**
* Render page
*
Expand All @@ -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 );
}
}
5 changes: 1 addition & 4 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 1 addition & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit 10576e2

Please sign in to comment.