|
| 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 | +} |
0 commit comments