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

Loading States for Ecommerce and Design Steps #86

Merged
merged 11 commits into from
Oct 7, 2022
60 changes: 60 additions & 0 deletions includes/RestApi/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public function register_routes() {
),
)
);

\register_rest_route(
$this->namespace,
$this->rest_base . '/status',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_status' ),
'args' => $this->get_status_args(),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
)
);
}

/**
Expand Down Expand Up @@ -106,6 +119,19 @@ public function get_install_plugin_args() {
);
}

public function get_status_args() {
return array(
'plugin' => array(
'type' => 'string',
'required' => true,
),
'activated' => array(
'type' => 'boolean',
'default' => true,
),
);
}

/**
* Verify caller has permissions to install plugins.
*
Expand Down Expand Up @@ -182,4 +208,38 @@ public function install( \WP_REST_Request $request ) {

return $plugin_install_task->execute();
}

public function get_status( \WP_REST_Request $request ) {
$plugin = $request->get_param( 'plugin' );
$activated = $request->get_param( 'activated' );

if ( PluginInstaller::exists( $plugin, $activated ) ) {
return new \WP_REST_Response(
array(
'status' => $activated ? 'activated' : 'installed',
),
200
);
}

$position_in_queue = PluginInstallTaskManager::status( $plugin );

if ( $position_in_queue !== false ) {
return new \WP_REST_Response(
array(
'status' => 'installing',
'estimate' => ( ( $position_in_queue + 1 ) * 30 ),
),
200
);
}

return new \WP_REST_Response(
array(
'status' => 'inactive',
),
200
);

}
}
42 changes: 35 additions & 7 deletions includes/RestApi/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use NewfoldLabs\WP\Module\Onboarding\Data\Options;
use NewfoldLabs\WP\Module\Onboarding\Data\Config;
use NewfoldLabs\WP\Module\Onboarding\WP_Config;
use NewfoldLabs\WP\Module\Onboarding\Data\Data;
use NewfoldLabs\WP\Module\Onboarding\Services\Webfonts;

/**
* Class SettingsController
Expand Down Expand Up @@ -81,6 +83,18 @@ public function register_routes() {
),
)
);

\register_rest_route(
$this->namespace,
$this->rest_base . '/preview',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_preview_settings' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
)
);
}

/**
Expand Down Expand Up @@ -177,12 +191,12 @@ public function get_current_settings() {
*/
public function initialize() {

if ( \get_option( Options::get_option_name( 'settings_initialized' ), false ) ) {
return new \WP_REST_Response(
array(),
200
);
}
if ( \get_option( Options::get_option_name( 'settings_initialized' ), false ) ) {
return new \WP_REST_Response(
array(),
200
);
}

// Update wp_options
$init_options = Options::get_initialization_options();
Expand All @@ -206,11 +220,25 @@ public function initialize() {
$wp_config->add_constant( $constant_key, $constant_value );
}

\update_option( Options::get_option_name( 'settings_initialized' ), true );
\update_option( Options::get_option_name( 'settings_initialized' ), true );

return new \WP_REST_Response(
array(),
201
);
}

public function get_preview_settings() {
$preview_settings = Data::preview_settings();

$webfonts_css = Webfonts::get_wp_theme_json_webfonts_css();
if ( $webfonts_css !== false ) {
$preview_settings['settings']['__unstableResolvedAssets']['styles'] .= '<style>' . $webfonts_css . '</style>';
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
}

return new \WP_REST_Response(
$preview_settings,
200
);
}
}
64 changes: 62 additions & 2 deletions includes/RestApi/Themes/ThemeInstallerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function register_routes() {
$this->rest_base . '/initialize',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'initialize' ),
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'initialize' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
)
Expand All @@ -50,6 +50,19 @@ public function register_routes() {
),
)
);

\register_rest_route(
$this->namespace,
$this->rest_base . '/status',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_status' ),
'args' => $this->get_status_args(),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
)
);
}

/**
Expand Down Expand Up @@ -78,6 +91,19 @@ public function get_install_theme_args() {
);
}

public function get_status_args() {
return array(
'theme' => array(
'type' => 'string',
'required' => true,
),
'activated' => array(
'type' => 'boolean',
'default' => true,
),
);
}

/**
* Queue in the initial list of themes to be installed.
*
Expand Down Expand Up @@ -139,4 +165,38 @@ public static function install( \WP_REST_Request $request ) {

return $theme_install_task->execute();
}

public function get_status( \WP_REST_Request $request ) {
$theme = $request->get_param( 'theme' );
$activated = $request->get_param( 'activated' );

if ( ThemeInstaller::exists( $theme, $activated ) ) {
return new \WP_REST_Response(
array(
'status' => $activated ? 'activated' : 'installed',
),
200
);
}

$position_in_queue = ThemeInstallTaskManager::status( $theme );

if ( $position_in_queue !== false ) {
return new \WP_REST_Response(
array(
'status' => 'installing',
'estimate' => ( ( $position_in_queue + 1 ) * 10 ),
),
200
);
}

return new \WP_REST_Response(
array(
'status' => 'inactive',
),
200
);

}
}
2 changes: 1 addition & 1 deletion includes/Services/ThemeInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function is_nfd_slug( $theme ) {
*/
public static function get_theme_stylesheet( $theme, $theme_type ) {
$theme_list = Themes::get();
return $theme_list[ $theme_type ][ $theme ]['stylesheet'];
return isset( $theme_list[ $theme_type ][ $theme ]['stylesheet'] ) ? $theme_list[ $theme_type ][ $theme ]['stylesheet'] : false;
}

/**
Expand Down
Loading