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

move over the required services from onboarding #127

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions includes/Services/FontService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\Data\Services;

use NewfoldLabs\WP\Module\Onboarding\Data\Themes\Fonts;
use NewfoldLabs\WP\Module\Onboarding\Data\Services\FlowService;

/**
* Class FontService
*
* Provides functionality to manage fonts in theme JSON settings, particularly
* for deselecting inactive or unused fonts.
*/
class FontService {
/**
* Deselects fonts that are not active from the theme JSON settings.
*
* This function filters out fonts from the given theme JSON settings that
* are not in the list of active font slugs, ensuring only the selected fonts
* remain active.
*
* @param array $theme_json_settings The theme JSON settings array.
* @param array $active_slugs An array of active font slugs to be retained.
*
* @return array The modified theme JSON settings with inactive fonts removed.
*/
public static function deselect_fonts_from_theme_json_settings( $theme_json_settings, $active_slugs ) {
if ( ! isset( $theme_json_settings['typography']['fontFamilies'] ) ) {
return $theme_json_settings;
}

// Get the currently selected fonts
$initial_selected_fonts = $theme_json_settings['typography']['fontFamilies'];
$final_selected_fonts = array();
// Iterate through each font and keep only those that are active
foreach ( $initial_selected_fonts as $index => $font ) {
if ( isset( $font['slug'] ) && in_array( $font['slug'], $active_slugs, true ) ) {
array_push( $final_selected_fonts, $font );
}
}
// Update the theme settings with the filtered list of fonts
$theme_json_settings['typography']['fontFamilies'] = $final_selected_fonts;

return $theme_json_settings;
}

/**
* Deselects inactive DIY flow fonts from the theme JSON settings.
*
* This function retrieves the currently selected font style from the flow
* data, checks which fonts are used, and then calls another function to
* remove unused fonts from the theme JSON settings.
*
* @param array $theme_json_settings The theme JSON settings array.
*
* @return array The modified theme JSON settings with inactive DIY fonts removed.
*/
public static function deselect_inactive_diy_fonts( $theme_json_settings ) {
// Get the selected font style from the flow data
$selected_font_style = FlowService::get_selected_font_style();
if ( empty( $selected_font_style ) ) {
return $theme_json_settings;
}

// Retrieve the fonts available in the flow
$theme_fonts = Fonts::get_fonts_from_theme();
if ( ! isset( $theme_fonts[ $selected_font_style ] ) ) {
return $theme_json_settings;
}

// Get the data for the selected font style
$selected_font_data = $theme_fonts[ $selected_font_style ];
$selected_slugs = $selected_font_data['slugs'];

// Deselect fonts that are not part of the selected slugs
return self::deselect_fonts_from_theme_json_settings( $theme_json_settings, $selected_slugs );
}
}
164 changes: 164 additions & 0 deletions includes/Services/GlobalStylesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\Data\Services;

use NewfoldLabs\WP\Module\Onboarding\Data\Themes;

/**
* Class GlobalStylesService
*
* Handles operations related to global styles and settings, such as updating
* global style variations and retrieving theme settings.
*/
class GlobalStylesService {
/**
* Updates the DIY flow global style variation.
*
* This function updates the global style variation using the provided ID, styles, and settings.
* If styles or settings are empty, it attempts to fetch user-selected theme settings from the flow.
*
* @param string $id The ID of the global style variation to update.
* @param array $styles The styles to apply. Defaults to empty.
* @param array $settings The settings to apply. Defaults to empty.
* @return true|\WP_Error Returns true on success, or a WP_Error on failure.
*/
public static function update_diy_global_style_variation( $id, $styles = array(), $settings = array() ) {
// If both styles and settings are not empty, update directly.
if ( ! ( empty( $styles ) && empty( $settings ) ) ) {

// Remove inactive DIY flow fonts from the theme JSON settings, retaining only the fonts that are currently in use or selected
$settings = FontService::deselect_inactive_diy_fonts( $settings );

return self::update_global_style_variation(
$id,
$styles,
$settings
);
}

// Retrieve user-selected theme settings.
$user_selected_theme_settings = Themes::get_selected_diy_theme_settings();
if ( ! $user_selected_theme_settings ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Selected theme settings were not found.', 'wp-module-onboarding' ),
array( 'status' => 404 )
);
}

// If styles are empty, use styles from user-selected theme settings.
if ( empty( $styles ) ) {
if ( empty( $user_selected_theme_settings['styles'] ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Styles does not exist under the selected theme settings.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}

$styles = $user_selected_theme_settings['styles'];
}

// If settings are empty, use settings from user-selected theme settings.
if ( empty( $settings ) ) {
if ( empty( $user_selected_theme_settings['settings'] ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Settings does not exist under the selected theme settings.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}

// Remove specific keys from the settings before using them as these are large and unnecessary.
unset( $user_selected_theme_settings['settings']['styles'] );
unset( $user_selected_theme_settings['settings']['__unstableResolvedAssets'] );
unset( $user_selected_theme_settings['settings']['__experimentalFeatures'] );
$settings = $user_selected_theme_settings['settings'];
}

// Remove inactive DIY flow fonts from the theme JSON settings, retaining only the fonts that are currently in use or selected
$settings = FontService::deselect_inactive_diy_fonts( $settings );

return self::update_global_style_variation(
$id,
$styles,
$settings
);
}

/**
* Updates the global style variation given the id.
*
* This private function sends a POST request to update the global style using the provided
* styles and settings.
*
* @param string $id The ID of the global style variation to update.
* @param array $styles The styles to apply.
* @param array $settings The settings to apply.
* @return true|\WP_Error Returns true on success, or a WP_Error on failure.
*/
private static function update_global_style_variation( $id, $styles, $settings ) {
// Create a REST request to update global styles.
$update_active_global_style_request = new \WP_REST_Request(
'POST',
"/wp/v2/global-styles/$id"
);
$update_active_global_style_request->set_header( 'Content-Type', 'application/json' );
// Generate custom theme.json data.
$custom_theme_json = self::create_custom_theme_json( $styles, $settings );
if ( ! isset( $custom_theme_json['styles'] ) || ! isset( $custom_theme_json['settings'] ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'There is an error with your styles or settings.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}
// Set the request body parameters.
$update_active_global_style_request->set_body_params(
array(
'id' => $id,
'styles' => $custom_theme_json['styles'],
'settings' => $custom_theme_json['settings'],
)
);

// Execute the REST request.
$update_active_global_style_response = rest_do_request( $update_active_global_style_request );
if ( $update_active_global_style_response->is_error() ) {
return $update_active_global_style_response->as_error();
}

return true;
}

/**
* Retrieves the post ID of the active/parent custom global styles.
*
* @return int The post ID of the active custom global styles.
*/
public static function get_active_custom_global_styles_post_id() {
return \WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
}

/**
* Creates a custom theme.json array.
*
* This function generates a theme.json structure based on the provided styles and settings.
*
* @param array $styles The styles to include in the theme.json.
* @param array $settings The settings to include in the theme.json.
* @param int $version The version of the theme.json schema. Defaults to the latest schema.
* @return array The raw theme.json data.
*/
public static function create_custom_theme_json( $styles, $settings, $version = \WP_Theme_JSON::LATEST_SCHEMA ) {
$theme_json = new \WP_Theme_JSON(
array(
'version' => $version,
'styles' => $styles,
'settings' => $settings,
)
);

return $theme_json->get_raw_data();
}
}
102 changes: 102 additions & 0 deletions includes/Services/TemplatePartsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\Data\Services;

use NewfoldLabs\WP\Module\Onboarding\Data\Themes;
use NewfoldLabs\WP\Module\Onboarding\Data\Patterns;
use NewfoldLabs\WP\Module\Onboarding\Data\Services\FlowService;

/**
* Class TemplatePartsService
*
* Service class for handling template parts within the onboarding module.
*/
class TemplatePartsService {
/**
* Retrieves a template part by its ID using the WordPress REST API.
*
* @param string $id The ID of the template part to retrieve.
* @return array|WP_Error The data of the template part or a WP_Error object on failure.
*/
public static function get_template_part( $id ) {
// Create a GET request for the specified template part ID.
$get_template_part_request = new \WP_REST_Request(
'GET',
"/wp/v2/template-parts/$id"
);
// Execute the request and get the response.
$get_template_part_response = rest_do_request( $get_template_part_request );
if ( $get_template_part_response->is_error() ) {
return $get_template_part_response->as_error();
}

return $get_template_part_response->get_data();
}

/**
* Updates a template part with new content using the WordPress REST API.
*
* @param string $id The ID of the template part to update.
* @param string $content The new content for the template part.
* @return bool|WP_Error True on success, or a WP_Error object on failure.
*/
public static function update_template_part( $id, $content ) {
// Create a POST request for the specified template part ID.
$update_template_part_request = new \WP_REST_Request(
'POST',
"/wp/v2/template-parts/$id"
);
// Set the body parameters for the request with the new content.
$update_template_part_request->set_body_params(
array(
'content' => $content,
)
);

// Execute the request and get the response.
$update_template_part_response = rest_do_request( $update_template_part_request );
if ( $update_template_part_response->is_error() ) {
return $update_template_part_response->as_error();
}

return true;
}

/**
* Updates the default template parts with the user-selected header template part for the DIY flow.
*
* @return bool|WP_Error True on success, or a WP_Error object on failure.
*/
public static function update_diy_selected_template_parts() {
// Retrieve the selected header template part ID from the flow data.
$selected_template_part_id = FlowService::get_selected_header_template_part();
if ( ! $selected_template_part_id ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Selected header template part not stored.', 'wp-module-onboarding' ),
array( 'status' => 404 )
);
}

// Retrieve the template part content from Wonder Theme or Wonder Blocks.
$selected_template_part = Patterns::get_pattern_from_slug( $selected_template_part_id );
if ( ! $selected_template_part ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Selected header template part not found.', 'wp-module-onboarding' ),
array( 'status' => 404 )
);
}
$selected_template_part_content = $selected_template_part['content'];
// Get the active theme and construct the default header ID.
$active_theme = Themes::get_active_theme();
$default_header_id = "$active_theme/header";
// Update the default header template part with the selected content.
$update_status = self::update_template_part( $default_header_id, $selected_template_part_content );
if ( is_wp_error( $update_status ) ) {
return $update_status;
}

return true;
}
}
Loading