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

SiteGen: Add Plugin Recommendations #23

Merged
merged 3 commits into from
Dec 26, 2023
Merged
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
6 changes: 3 additions & 3 deletions includes/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}

Expand Down Expand Up @@ -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'];
}

/**
Expand Down
13 changes: 10 additions & 3 deletions includes/Flows/Flows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),

Expand Down Expand Up @@ -213,6 +215,7 @@ public static function get_flows() {
? $current_brand['config']['enabled_flows'] : array(
'wp-setup' => false,
'ecommerce' => false,
'sitegen' => false,
);
}

Expand Down Expand Up @@ -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'];

}

Expand Down
119 changes: 101 additions & 18 deletions includes/Services/SiteGenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand All @@ -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 ] );
}

/**
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -225,4 +253,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,
),
'plugin_recommendation'
);

if ( isset( $plugin_recommendations['error'] ) || is_wp_error( $plugin_recommendations ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Cannot retrieve plugin recommendations.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}

$priority = 0;
$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( $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 );
}

}
10 changes: 10 additions & 0 deletions includes/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class Themes {
protected static $flow_default_theme_slugs = array(
'wp-setup' => 'yith-wonder',
'ecommerce' => 'yith-wonder',
'sitegen' => 'yith-wonder',
);

/**
Expand Down Expand Up @@ -48,6 +49,15 @@ final class Themes {
),
),
),
'sitegen' => array(
'default' => array(
array(
'slug' => 'nfd_slug_yith_wonder',
'activate' => true,
'priority' => 20,
),
),
),
);

/**
Expand Down