From 143e1e3a43035a26874e930abd987a8675bebc09 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Wed, 20 Dec 2023 17:18:07 +0530 Subject: [PATCH 1/3] Add plugin recommendations helper --- includes/Data.php | 6 +-- includes/Services/SiteGenService.php | 55 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/includes/Data.php b/includes/Data.php index 5c9fcdf..3644084 100644 --- a/includes/Data.php +++ b/includes/Data.php @@ -26,11 +26,11 @@ public static function runtime() { 'settings' => Preview::get_settings(), 'stepPreviewData' => Themes::step_preview_data(), ), - 'aiPreviewSettings' => array( + 'aiPreviewSettings' => array( 'settings' => Preview::get_settings(), 'stepPreviewData' => Themes::step_preview_data(), ), - 'currentUserDetails' => self::wp_current_user_details(), + 'currentUserDetails' => self::wp_current_user_details(), ); } @@ -124,7 +124,7 @@ public static function current_plan() { */ public static function current_flow() { $current_plan = self::current_plan(); - return 'sitegen'; + return $current_plan['flow']; } /** diff --git a/includes/Services/SiteGenService.php b/includes/Services/SiteGenService.php index 7f3c2ed..43a43da 100644 --- a/includes/Services/SiteGenService.php +++ b/includes/Services/SiteGenService.php @@ -225,4 +225,59 @@ public static function generate_child_theme( $data ) { } + /** + * Get Plugin recommendations from the sitegen meta. + * + * @return array|\WP_Error + */ + public static function get_plugin_recommendations() { + $flow_data = get_option( Options::get_option_name( 'flow' ), false ); + if ( ! $flow_data || empty( $flow_data['sitegen']['siteDetails']['prompt'] ) ) { + return new \WP_Error( + 'nfd_onboarding_error', + __( 'Prompt not found.', 'wp-module-onboarding' ), + array( 'status' => 404 ) + ); + } + + $prompt = $flow_data['sitegen']['siteDetails']['prompt']; + $plugin_recommendations = self::instantiate_site_meta( + array( + 'site_description' => $prompt, + ), + 'pluginrecommendation' + ); + + if ( isset( $plugin_recommendations['error'] ) ) { + return new \WP_Error( + 'nfd_onboarding_error', + __( 'Cannot retrieve plugin recommendations.', 'wp-module-onboarding' ), + array( 'status' => 400 ) + ); + } + + $priority = 0; + $required_plugins = isset( $plugin_recommendations['requiredPlugins'] ) ? $plugin_recommendations['requiredPlugins'] : array(); + $final_required_plugins = array(); + foreach ( $required_plugins as $required_plugin ) { + $required_plugin['slug'] = explode( '/', $required_plugin['slug'] )[0]; + $required_plugin['priority'] = $priority; + $priority += 20; + $required_plugin['activate'] = true; + array_push( $new_required_plugins, $required_plugin ); + } + + $recommended_plugins = isset( $plugin_recommendations['recommendedPlugins'] ) ? $plugin_recommendations['recommendedPlugins'] : array(); + $final_recommended_plugins = array(); + foreach ( $recommended_plugins as $recommended_plugin ) { + $recommended_plugin['slug'] = explode( '/', $recommended_plugin['slug'] )[0]; + $recommended_plugin['priority'] = $priority; + $priority += 20; + $recommended_plugin['activate'] = false; + array_push( $new_recommended_plugins, $recommended_plugin ); + } + + return array_merge( $final_required_plugins, $final_recommended_plugins ); + } + } From 9e2015679a7d4438b69f149c83044f02cf3a1ce3 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Thu, 21 Dec 2023 09:58:59 +0530 Subject: [PATCH 2/3] Improve logic since API is not reliable --- includes/Flows/Flows.php | 13 +++- includes/Services/SiteGenService.php | 94 ++++++++++++++++++---------- 2 files changed, 71 insertions(+), 36 deletions(-) diff --git a/includes/Flows/Flows.php b/includes/Flows/Flows.php index 43e4acc..cd4e8f9 100644 --- a/includes/Flows/Flows.php +++ b/includes/Flows/Flows.php @@ -16,7 +16,7 @@ final class Flows { * @var array */ protected static $data = array( - 'version' => '1.0.6', + 'version' => '1.0.7', // Each time step is viewed, insert GMT timestamp to array. 'isViewed' => array(), @@ -112,6 +112,8 @@ final class Flows { 'comingSoon' => false, ), + 'activeFlow' => '', + // we will store active flows (abandoned wp-setup, abandoned wp-commerce) with their identifier and use as a reference to access currentStep and data 'currentFlows' => array(), @@ -213,6 +215,7 @@ public static function get_flows() { ? $current_brand['config']['enabled_flows'] : array( 'wp-setup' => false, 'ecommerce' => false, + 'sitegen' => false, ); } @@ -309,12 +312,16 @@ public static function get_flow_from_plan_subtype( $plan_subtype ) { * @return boolean */ public static function is_sitegen() { + if ( ! self::get_flows()['sitegen'] ) { + return false; + } + $flow_data = FlowService::read_data_from_wp_option(); - if ( ! $flow_data || empty( $flow_data['sitegen'] ) ) { + if ( ! $flow_data || empty( $flow_data['activeFlow'] ) ) { return false; } - return true === self::get_flows()['sitegen']; + return 'sitegen' === $flow_data['activeFlow']; } diff --git a/includes/Services/SiteGenService.php b/includes/Services/SiteGenService.php index 43a43da..a9a4940 100644 --- a/includes/Services/SiteGenService.php +++ b/includes/Services/SiteGenService.php @@ -15,21 +15,47 @@ */ class SiteGenService { + /** + * Onboarding to SiteGen identifier map. + * + * @var array + */ + private static $identifiers = array( + 'site_classification' => 'siteclassification', + 'target_audience' => 'targetaudience', + 'content_tones' => 'contenttones', + 'content_structure' => 'contentstructure', + 'color_palette' => 'colorpalette', + 'sitemap' => 'sitemap', + 'plugin_recommendation' => 'pluginrecommendation', + 'font_pair' => 'fontpair', + ); + + /** + * Get SiteGen identifier from an Onboarding identifier key. + * + * @param string $identifier_key Onboarding identifier key. + * @return string|false + */ + public static function get_identifier_name( $identifier_key ) { + return isset( self::$identifiers[ $identifier_key ] ) ? self::$identifiers[ $identifier_key ] : false; + } + /** * Gets Valid Identifiers. * * @return array */ - public static function get_identifiers() { + public static function enabled_identifiers() { return array( - 'siteclassification' => true, - 'targetaudience' => true, - 'contenttones' => true, - 'contentstructure' => true, - 'colorpalette' => true, - 'sitemap' => true, - 'pluginrecommendation' => true, - 'fontpair' => true, + 'site_classification' => true, + 'target_audience' => true, + 'content_tones' => true, + 'content_structure' => true, + 'color_palette' => true, + 'sitemap' => true, + 'plugin_recommendation' => true, + 'font_pair' => true, ); } @@ -40,7 +66,7 @@ public static function get_identifiers() { * @return boolean */ public static function is_identifier( $key ) { - return isset( self::get_identifiers()[ $key ] ); + return isset( self::enabled_identifiers()[ $key ] ); } /** @@ -66,16 +92,18 @@ public static function is_enabled() { * @return array */ public static function instantiate_site_meta( $site_info, $identifier, $skip_cache = false ) { - - if ( self::is_identifier( $identifier ) ) { - // sleep( 8 ); - return SiteGen::generate_site_meta( $site_info, $identifier, $skip_cache ); + if ( ! self::is_identifier( $identifier ) ) { + return new \WP_Error( + 'nfd_onboarding_error', + __( 'Not a valid identifier', 'wp-module-onboarding' ), + array( + 'status' => '400', + ) + ); } - // Imitates the error pattern returned by SiteGen Class - return array( - 'error' => __( 'The given identifier is not valid' ), - ); + $identifier = self::get_identifier_name( $identifier ); + return SiteGen::generate_site_meta( $site_info, $identifier, $skip_cache ); } /** @@ -226,7 +254,7 @@ public static function generate_child_theme( $data ) { } /** - * Get Plugin recommendations from the sitegen meta. + * Get Plugin recommendations from the SiteGen meta. * * @return array|\WP_Error */ @@ -245,10 +273,10 @@ public static function get_plugin_recommendations() { array( 'site_description' => $prompt, ), - 'pluginrecommendation' + 'plugin_recommendation' ); - if ( isset( $plugin_recommendations['error'] ) ) { + if ( isset( $plugin_recommendations['error'] ) || is_wp_error( $plugin_recommendations ) ) { return new \WP_Error( 'nfd_onboarding_error', __( 'Cannot retrieve plugin recommendations.', 'wp-module-onboarding' ), @@ -256,17 +284,7 @@ public static function get_plugin_recommendations() { ); } - $priority = 0; - $required_plugins = isset( $plugin_recommendations['requiredPlugins'] ) ? $plugin_recommendations['requiredPlugins'] : array(); - $final_required_plugins = array(); - foreach ( $required_plugins as $required_plugin ) { - $required_plugin['slug'] = explode( '/', $required_plugin['slug'] )[0]; - $required_plugin['priority'] = $priority; - $priority += 20; - $required_plugin['activate'] = true; - array_push( $new_required_plugins, $required_plugin ); - } - + $priority = 0; $recommended_plugins = isset( $plugin_recommendations['recommendedPlugins'] ) ? $plugin_recommendations['recommendedPlugins'] : array(); $final_recommended_plugins = array(); foreach ( $recommended_plugins as $recommended_plugin ) { @@ -274,7 +292,17 @@ public static function get_plugin_recommendations() { $recommended_plugin['priority'] = $priority; $priority += 20; $recommended_plugin['activate'] = false; - array_push( $new_recommended_plugins, $recommended_plugin ); + array_push( $final_recommended_plugins, $recommended_plugin ); + } + + $required_plugins = isset( $plugin_recommendations['requiredPlugins'] ) ? $plugin_recommendations['requiredPlugins'] : array(); + $final_required_plugins = array(); + foreach ( $required_plugins as $required_plugin ) { + $required_plugin['slug'] = explode( '/', $required_plugin['slug'] )[0]; + $required_plugin['priority'] = $priority; + $priority += 20; + $required_plugin['activate'] = true; + array_push( $final_required_plugins, $required_plugin ); } return array_merge( $final_required_plugins, $final_recommended_plugins ); From 1ecd5d2f8996c78f526557a1b02c4ddd058117c9 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Thu, 21 Dec 2023 10:09:14 +0530 Subject: [PATCH 3/3] Add Wonder to SiteGen --- includes/Themes.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/includes/Themes.php b/includes/Themes.php index e786b75..9a74050 100644 --- a/includes/Themes.php +++ b/includes/Themes.php @@ -14,6 +14,7 @@ final class Themes { protected static $flow_default_theme_slugs = array( 'wp-setup' => 'yith-wonder', 'ecommerce' => 'yith-wonder', + 'sitegen' => 'yith-wonder', ); /** @@ -48,6 +49,15 @@ final class Themes { ), ), ), + 'sitegen' => array( + 'default' => array( + array( + 'slug' => 'nfd_slug_yith_wonder', + 'activate' => true, + 'priority' => 20, + ), + ), + ), ); /**