Skip to content

Commit

Permalink
Add plugin recommendations helper
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Dec 20, 2023
1 parent 568db6d commit 143e1e3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
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
55 changes: 55 additions & 0 deletions includes/Services/SiteGenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}

0 comments on commit 143e1e3

Please sign in to comment.