Skip to content

Commit ccf4da1

Browse files
committed
move over sitegen service from data module
1 parent c22d2fa commit ccf4da1

File tree

8 files changed

+1718
-6
lines changed

8 files changed

+1718
-6
lines changed

includes/RestApi/FlowController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use NewfoldLabs\WP\Module\Onboarding\Permissions;
66
use NewfoldLabs\WP\Module\Onboarding\Data\Services\FlowService;
77
use NewfoldLabs\WP\Module\Onboarding\Services\PluginService;
8-
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService;
9-
use NewfoldLabs\WP\Module\Onboarding\Data\Services\GlobalStylesService;
8+
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenService;
9+
use NewfoldLabs\WP\Module\Onboarding\Services\GlobalStylesService;
1010
use NewfoldLabs\WP\Module\Onboarding\Services\StatusService;
11-
use NewfoldLabs\WP\Module\Onboarding\Data\Services\TemplatePartsService;
11+
use NewfoldLabs\WP\Module\Onboarding\Services\TemplatePartsService;
1212

1313
/**
1414
* Class FlowController

includes/RestApi/Themes/ThemeVariationsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use NewfoldLabs\WP\Module\Onboarding\Permissions;
55
use NewfoldLabs\WP\Module\Onboarding\Data\Themes;
6-
use NewfoldLabs\WP\Module\Onboarding\Data\Services\GlobalStylesService;
6+
use NewfoldLabs\WP\Module\Onboarding\Services\GlobalStylesService;
77

88
/**
99
* Class ThemeVariationsController

includes/Services/FontService.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Onboarding\Services;
4+
5+
use NewfoldLabs\WP\Module\Onboarding\Data\Themes\Fonts;
6+
use NewfoldLabs\WP\Module\Onboarding\Data\Services\FlowService;
7+
8+
/**
9+
* Class FontService
10+
*
11+
* Provides functionality to manage fonts in theme JSON settings, particularly
12+
* for deselecting inactive or unused fonts.
13+
*/
14+
class FontService {
15+
/**
16+
* Deselects fonts that are not active from the theme JSON settings.
17+
*
18+
* This function filters out fonts from the given theme JSON settings that
19+
* are not in the list of active font slugs, ensuring only the selected fonts
20+
* remain active.
21+
*
22+
* @param array $theme_json_settings The theme JSON settings array.
23+
* @param array $active_slugs An array of active font slugs to be retained.
24+
*
25+
* @return array The modified theme JSON settings with inactive fonts removed.
26+
*/
27+
public static function deselect_fonts_from_theme_json_settings( $theme_json_settings, $active_slugs ) {
28+
if ( ! isset( $theme_json_settings['typography']['fontFamilies'] ) ) {
29+
return $theme_json_settings;
30+
}
31+
32+
// Get the currently selected fonts
33+
$initial_selected_fonts = $theme_json_settings['typography']['fontFamilies'];
34+
$final_selected_fonts = array();
35+
// Iterate through each font and keep only those that are active
36+
foreach ( $initial_selected_fonts as $index => $font ) {
37+
if ( isset( $font['slug'] ) && in_array( $font['slug'], $active_slugs, true ) ) {
38+
array_push( $final_selected_fonts, $font );
39+
}
40+
}
41+
// Update the theme settings with the filtered list of fonts
42+
$theme_json_settings['typography']['fontFamilies'] = $final_selected_fonts;
43+
44+
return $theme_json_settings;
45+
}
46+
47+
/**
48+
* Deselects inactive DIY flow fonts from the theme JSON settings.
49+
*
50+
* This function retrieves the currently selected font style from the flow
51+
* data, checks which fonts are used, and then calls another function to
52+
* remove unused fonts from the theme JSON settings.
53+
*
54+
* @param array $theme_json_settings The theme JSON settings array.
55+
*
56+
* @return array The modified theme JSON settings with inactive DIY fonts removed.
57+
*/
58+
public static function deselect_inactive_diy_fonts( $theme_json_settings ) {
59+
// Get the selected font style from the flow data
60+
$selected_font_style = FlowService::get_selected_font_style();
61+
if ( empty( $selected_font_style ) ) {
62+
return $theme_json_settings;
63+
}
64+
65+
// Retrieve the fonts available in the flow
66+
$theme_fonts = Fonts::get_fonts_from_theme();
67+
if ( ! isset( $theme_fonts[ $selected_font_style ] ) ) {
68+
return $theme_json_settings;
69+
}
70+
71+
// Get the data for the selected font style
72+
$selected_font_data = $theme_fonts[ $selected_font_style ];
73+
$selected_slugs = $selected_font_data['slugs'];
74+
75+
// Deselect fonts that are not part of the selected slugs
76+
return self::deselect_fonts_from_theme_json_settings( $theme_json_settings, $selected_slugs );
77+
}
78+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Onboarding\Services;
4+
5+
use NewfoldLabs\WP\Module\Onboarding\Data\Themes;
6+
7+
/**
8+
* Class GlobalStylesService
9+
*
10+
* Handles operations related to global styles and settings, such as updating
11+
* global style variations and retrieving theme settings.
12+
*/
13+
class GlobalStylesService {
14+
/**
15+
* Updates the DIY flow global style variation.
16+
*
17+
* This function updates the global style variation using the provided ID, styles, and settings.
18+
* If styles or settings are empty, it attempts to fetch user-selected theme settings from the flow.
19+
*
20+
* @param string $id The ID of the global style variation to update.
21+
* @param array $styles The styles to apply. Defaults to empty.
22+
* @param array $settings The settings to apply. Defaults to empty.
23+
* @return true|\WP_Error Returns true on success, or a WP_Error on failure.
24+
*/
25+
public static function update_diy_global_style_variation( $id, $styles = array(), $settings = array() ) {
26+
// If both styles and settings are not empty, update directly.
27+
if ( ! ( empty( $styles ) && empty( $settings ) ) ) {
28+
29+
// Remove inactive DIY flow fonts from the theme JSON settings, retaining only the fonts that are currently in use or selected
30+
$settings = FontService::deselect_inactive_diy_fonts( $settings );
31+
32+
return self::update_global_style_variation(
33+
$id,
34+
$styles,
35+
$settings
36+
);
37+
}
38+
39+
// Retrieve user-selected theme settings.
40+
$user_selected_theme_settings = Themes::get_selected_diy_theme_settings();
41+
if ( ! $user_selected_theme_settings ) {
42+
return new \WP_Error(
43+
'nfd_onboarding_error',
44+
__( 'Selected theme settings were not found.', 'wp-module-onboarding' ),
45+
array( 'status' => 404 )
46+
);
47+
}
48+
49+
// If styles are empty, use styles from user-selected theme settings.
50+
if ( empty( $styles ) ) {
51+
if ( empty( $user_selected_theme_settings['styles'] ) ) {
52+
return new \WP_Error(
53+
'nfd_onboarding_error',
54+
__( 'Styles does not exist under the selected theme settings.', 'wp-module-onboarding' ),
55+
array( 'status' => 400 )
56+
);
57+
}
58+
59+
$styles = $user_selected_theme_settings['styles'];
60+
}
61+
62+
// If settings are empty, use settings from user-selected theme settings.
63+
if ( empty( $settings ) ) {
64+
if ( empty( $user_selected_theme_settings['settings'] ) ) {
65+
return new \WP_Error(
66+
'nfd_onboarding_error',
67+
__( 'Settings does not exist under the selected theme settings.', 'wp-module-onboarding' ),
68+
array( 'status' => 400 )
69+
);
70+
}
71+
72+
// Remove specific keys from the settings before using them as these are large and unnecessary.
73+
unset( $user_selected_theme_settings['settings']['styles'] );
74+
unset( $user_selected_theme_settings['settings']['__unstableResolvedAssets'] );
75+
unset( $user_selected_theme_settings['settings']['__experimentalFeatures'] );
76+
$settings = $user_selected_theme_settings['settings'];
77+
}
78+
79+
// Remove inactive DIY flow fonts from the theme JSON settings, retaining only the fonts that are currently in use or selected
80+
$settings = FontService::deselect_inactive_diy_fonts( $settings );
81+
82+
return self::update_global_style_variation(
83+
$id,
84+
$styles,
85+
$settings
86+
);
87+
}
88+
89+
/**
90+
* Updates the global style variation given the id.
91+
*
92+
* This private function sends a POST request to update the global style using the provided
93+
* styles and settings.
94+
*
95+
* @param string $id The ID of the global style variation to update.
96+
* @param array $styles The styles to apply.
97+
* @param array $settings The settings to apply.
98+
* @return true|\WP_Error Returns true on success, or a WP_Error on failure.
99+
*/
100+
private static function update_global_style_variation( $id, $styles, $settings ) {
101+
// Create a REST request to update global styles.
102+
$update_active_global_style_request = new \WP_REST_Request(
103+
'POST',
104+
"/wp/v2/global-styles/$id"
105+
);
106+
$update_active_global_style_request->set_header( 'Content-Type', 'application/json' );
107+
// Generate custom theme.json data.
108+
$custom_theme_json = self::create_custom_theme_json( $styles, $settings );
109+
if ( ! isset( $custom_theme_json['styles'] ) || ! isset( $custom_theme_json['settings'] ) ) {
110+
return new \WP_Error(
111+
'nfd_onboarding_error',
112+
__( 'There is an error with your styles or settings.', 'wp-module-onboarding' ),
113+
array( 'status' => 400 )
114+
);
115+
}
116+
// Set the request body parameters.
117+
$update_active_global_style_request->set_body_params(
118+
array(
119+
'id' => $id,
120+
'styles' => $custom_theme_json['styles'],
121+
'settings' => $custom_theme_json['settings'],
122+
)
123+
);
124+
125+
// Execute the REST request.
126+
$update_active_global_style_response = rest_do_request( $update_active_global_style_request );
127+
if ( $update_active_global_style_response->is_error() ) {
128+
return $update_active_global_style_response->as_error();
129+
}
130+
131+
return true;
132+
}
133+
134+
/**
135+
* Retrieves the post ID of the active/parent custom global styles.
136+
*
137+
* @return int The post ID of the active custom global styles.
138+
*/
139+
public static function get_active_custom_global_styles_post_id() {
140+
return \WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
141+
}
142+
143+
/**
144+
* Creates a custom theme.json array.
145+
*
146+
* This function generates a theme.json structure based on the provided styles and settings.
147+
*
148+
* @param array $styles The styles to include in the theme.json.
149+
* @param array $settings The settings to include in the theme.json.
150+
* @param int $version The version of the theme.json schema. Defaults to the latest schema.
151+
* @return array The raw theme.json data.
152+
*/
153+
public static function create_custom_theme_json( $styles, $settings, $version = \WP_Theme_JSON::LATEST_SCHEMA ) {
154+
$theme_json = new \WP_Theme_JSON(
155+
array(
156+
'version' => $version,
157+
'styles' => $styles,
158+
'settings' => $settings,
159+
)
160+
);
161+
162+
return $theme_json->get_raw_data();
163+
}
164+
}

includes/Services/PluginService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use NewfoldLabs\WP\Module\Installer\Tasks\PluginDeactivationTask;
1212
use NewfoldLabs\WP\Module\Installer\Tasks\PluginInstallTask;
1313
use NewfoldLabs\WP\Module\Onboarding\Data\Data;
14-
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService;
14+
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenService;
1515
use NewfoldLabs\WP\Module\Onboarding\Data\Plugins;
1616
use NewfoldLabs\WP\Module\Onboarding\Data\SiteFeatures;
1717

0 commit comments

Comments
 (0)