Skip to content

Commit

Permalink
Merge pull request #102 from newfold-labs/PRESS-2-331-Maintain-State-…
Browse files Browse the repository at this point in the history
…for-Colors-and-Typography-Steps

PRESS2 331 Maintain State for colors and typography steps
  • Loading branch information
officiallygod authored Nov 22, 2022
2 parents 2f46f1e + e99fedd commit 2563768
Show file tree
Hide file tree
Showing 13 changed files with 681 additions and 439 deletions.
1 change: 1 addition & 0 deletions includes/Data/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class Options {
'site_icon' => 'site_icon',
'show_on_front' => 'show_on_front',
'page_on_front' => 'page_on_front',
'theme_settings' => 'theme_settings',
);

protected static $initialization_options = array(
Expand Down
79 changes: 75 additions & 4 deletions includes/RestApi/Themes/ThemeVariationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace NewfoldLabs\WP\Module\Onboarding\RestApi\Themes;

use NewfoldLabs\WP\Module\Onboarding\Permissions;
use NewfoldLabs\WP\Module\Onboarding\Data\Options;

/**
* Class ThemeVariationsController
Expand Down Expand Up @@ -39,23 +40,66 @@ public function register_routes() {
$this->rest_base . $this->rest_extended_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_theme_variations' ),
'methods' => \WP_REST_Server::READABLE,
'args' => $this->get_pattern_args(),
'callback' => array( $this, 'get_theme_variations' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'args' => $this->set_pattern_args(),
'callback' => array( $this, 'set_theme_variation' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
),
)
);
}

public function get_pattern_args() {
// These variable return the orginal numerous variations if true
// Else sends the recently saved theme settings in db
return array(
'variations' => array(
'type' => 'boolean',
'default' => false,
),
);
}

public function set_pattern_args() {
// This is the latest modified Global Style to be saved in the db
return array(
'title' => array(
'type' => 'string',
'required' => true,
),
'settings' => array(
'type' => 'array',
'required' => true,
),
);
}

/**
* Retrieves the active themes variations.
*
* @return \WP_REST_Response|\WP_Error
* @return \array|\WP_Error
*/
public function get_theme_variations( \WP_REST_Request $request ) {

$default = $request->get_param( 'variations' );

// If there exists an old Custom Theme then return that
if ( 'false' === $default && false !== \get_option( Options::get_option_name( 'theme_settings' ) ) ) {
return array(
\get_option( Options::get_option_name( 'theme_settings' ) ),
);
}

$active_variation = \WP_Theme_JSON_Resolver::get_merged_data( 'theme' )->get_raw_data();
$active_variation_global_style = array(
'title' => 'Default',
'id' => 0,
'title' => 'Default',
'version' => $active_variation['version'],
'settings' => $active_variation['settings'],
'styles' => $active_variation['styles'],
Expand All @@ -67,4 +111,31 @@ public function get_theme_variations( \WP_REST_Request $request ) {
);
}

/**
* Saves the custom active theme variations.
*
* @return \WP_REST_Response|\WP_Error
*/
public function set_theme_variation( \WP_REST_Request $request ) {
// The theme data with the new Colors and Fonts
$theme_data = json_decode( $request->get_body(), true );

if ( $theme_data ) {

// Save the new Theme style into the db
\update_option( Options::get_option_name( 'theme_settings' ), $theme_data );

return new \WP_REST_Response(
$theme_data,
200
);
}

return new \WP_Error(
500,
'Missing important parameters',
'Settings parameter is found to be missing'
);
}

}
Loading

0 comments on commit 2563768

Please sign in to comment.