From 49697f8e850908cd08e60a5eb74928509ce24dcb Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Mon, 20 Feb 2023 15:10:59 +0530 Subject: [PATCH] improve code, fix site features skeleton --- includes/Data/Patterns.php | 14 +- includes/Data/SiteFeatures.php | 328 ++++++++++++------------- includes/Data/Themes.php | 93 ++++--- includes/RestApi/PluginsController.php | 33 ++- 4 files changed, 250 insertions(+), 218 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index c21363776..6196a4cf5 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -1,9 +1,6 @@ true, ), ), - 'site-features' => SiteFeatures::get_site_features()[ Data::current_flow() ], ), ); } @@ -142,11 +138,11 @@ public static function get_pattern_from_slug( $pattern_slug ) { $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); if ( $block_patterns_registry->is_registered( $pattern_slug ) ) { $pattern = $block_patterns_registry->get_registered( $pattern_slug ); - return array( - 'title' => $pattern['title'], - 'content' => self::cleanup_wp_grammar( $pattern['content'] ), - 'name' => $pattern['name'], - ); + return array( + 'title' => $pattern['title'], + 'content' => self::cleanup_wp_grammar( $pattern['content'] ), + 'name' => $pattern['name'], + ); } return false; diff --git a/includes/Data/SiteFeatures.php b/includes/Data/SiteFeatures.php index 3ef0a7a34..d0f2c5092 100644 --- a/includes/Data/SiteFeatures.php +++ b/includes/Data/SiteFeatures.php @@ -6,194 +6,192 @@ * Class SiteFeatures */ final class SiteFeatures { - /** - * Retrieve Site Features. + * Contains the map of site features for a particular flow and plan. * - * @return array + * @var array */ - public static function get_site_features() { - return array( - 'wp-setup' => array(), - 'ecommerce' => array( - 'default' => array( - 'jetpack' => array( - 'slug' => 'jetpack', - 'icon' => '--site-features-security', - 'title' => 'Security, Speed & Growth', - 'subtitle' => 'Powered by Jetpack', - 'desc' => 'Jetpack includes dozens of powerful, unique capabilities for your WordPress sites from Automattic.', - 'selected' => false, - 'shown' => true, - ), - 'wpforms-lite' => array( - 'slug' => 'wpforms-lite', - 'icon' => '--site-features-form', - 'title' => 'Forms', - 'subtitle' => 'Powered by WP Forms', - 'desc' => 'Five million people build smarter forms and surveys with WPForms from Awesome Motive.', - 'selected' => false, - 'shown' => true, - ), - 'google-analytics-for-wordpress' => array( - 'slug' => 'google-analytics-for-wordpress', - 'icon' => '--site-features-analytics', - 'title' => 'Site Traffic', - 'subtitle' => 'Powered by MonsterInsights', - 'desc' => 'See the opportunities in your website analytics traffic data using MonsterInsights from Awesome Motive.', - 'selected' => false, - 'shown' => true, - ), - 'wordpress-seo' => array( - 'slug' => 'wordpress-seo', - 'icon' => '--site-features-share', - 'title' => 'Search Engine Optimization', - 'subtitle' => 'Powered by Yoast', - 'desc' => 'Get more traffic to your WordPress site with powerful analysis and tools from our colleagues at Yoast.', - 'selected' => false, - 'shown' => true, - ), - 'creative-mail-by-constant-contact' => array( - 'slug' => 'creative-mail-by-constant-contact', - 'icon' => '--site-features-email', - 'title' => 'Email Newsletters', - 'subtitle' => 'Powered by Creative Email', - 'desc' => 'A professional logo builder, marketing automations with WooCommerce and social management -- CreativeMail is a whole lot more than mail from Constant Contact.', - 'selected' => false, - 'shown' => true, - ), - 'optinmonster' => array( - 'slug' => 'optinmonster', - 'icon' => '--site-features-lead', - 'title' => 'Lead Generation', - 'subtitle' => 'Powered by Optin Monster', - 'desc' => 'Connect with website visitors using a proven kit of tools for growth using this offering from Awesome Motive.', - 'selected' => false, - 'shown' => true, - ), + protected static $site_features_flow_plan_map = array( + 'wp-setup' => array(), + 'ecommerce' => array( + 'default' => array( + 'jetpack' => array( + 'slug' => 'jetpack', + 'icon' => '--site-features-security', + 'title' => 'Security, Speed & Growth', + 'subtitle' => 'Powered by Jetpack', + 'desc' => 'Jetpack includes dozens of powerful, unique capabilities for your WordPress sites from Automattic.', + 'selected' => false, + 'shown' => true, + ), + 'wpforms-lite' => array( + 'slug' => 'wpforms-lite', + 'icon' => '--site-features-form', + 'title' => 'Forms', + 'subtitle' => 'Powered by WP Forms', + 'desc' => 'Five million people build smarter forms and surveys with WPForms from Awesome Motive.', + 'selected' => false, + 'shown' => true, + ), + 'google-analytics-for-wordpress' => array( + 'slug' => 'google-analytics-for-wordpress', + 'icon' => '--site-features-analytics', + 'title' => 'Site Traffic', + 'subtitle' => 'Powered by MonsterInsights', + 'desc' => 'See the opportunities in your website analytics traffic data using MonsterInsights from Awesome Motive.', + 'selected' => false, + 'shown' => true, ), - 'wc_standard' => array( - 'yith-woocommerce-ajax-search' => array( - 'slug' => 'yith-woocommerce-ajax-search', - 'icon' => '--site-features-search', - 'title' => 'Enhanced Product Search', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Give your visitors great search experiences with this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), - 'nfd_slug_yith_woocommerce_ajax_product_filter' => array( - 'slug' => 'nfd_slug_yith_woocommerce_ajax_product_filter', - 'icon' => '--site-features-filter', - 'title' => 'Enhanced Product Filters', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Give your visitors powerful tools to discover your great products with this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), - 'nfd_slug_yith_woocommerce_booking' => array( - 'slug' => 'nfd_slug_yith_woocommerce_booking', - 'icon' => '--site-features-bookingcalendar', - 'title' => 'Bookings & Appointments', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Have visitors book meetings and services with you, accepting payment and more using this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), - 'nfd_slug_yith_woocommerce_wishlist' => array( - 'slug' => 'nfd_slug_yith_woocommerce_wishlist', - 'icon' => '--site-features-wishlist', - 'title' => 'Product Wishlists', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Let discerning shoppers curate their selections with a system of favorites using this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), + 'wordpress-seo' => array( + 'slug' => 'wordpress-seo', + 'icon' => '--site-features-share', + 'title' => 'Search Engine Optimization', + 'subtitle' => 'Powered by Yoast', + 'desc' => 'Get more traffic to your WordPress site with powerful analysis and tools from our colleagues at Yoast.', + 'selected' => false, + 'shown' => true, ), - 'wc_premium' => array( - 'yith-woocommerce-ajax-search' => array( - 'slug' => 'yith-woocommerce-ajax-search', - 'icon' => '--site-features-search', - 'title' => 'Enhanced Product Search', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Give your visitors great search experiences with this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), - 'nfd_slug_yith_woocommerce_ajax_product_filter' => array( - 'slug' => 'nfd_slug_yith_woocommerce_ajax_product_filter', - 'icon' => '--site-features-filter', - 'title' => 'Enhanced Product Filters', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Give your visitors powerful tools to discover your great products with this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), - 'nfd_slug_yith_woocommerce_booking' => array( - 'slug' => 'nfd_slug_yith_woocommerce_booking', - 'icon' => '--site-features-bookingcalendar', - 'title' => 'Bookings & Appointments', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Have visitors book meetings and services with you, accepting payment and more using this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), - 'nfd_slug_yith_woocommerce_wishlist' => array( - 'slug' => 'nfd_slug_yith_woocommerce_wishlist', - 'icon' => '--site-features-wishlist', - 'title' => 'Product Wishlists', - 'subtitle' => 'Powered by YITH', - 'desc' => 'Let discerning shoppers curate their selections with a system of favorites using this exclusive offering from our colleagues at YITH.', - 'selected' => false, - 'shown' => true, - ), + 'creative-mail-by-constant-contact' => array( + 'slug' => 'creative-mail-by-constant-contact', + 'icon' => '--site-features-email', + 'title' => 'Email Newsletters', + 'subtitle' => 'Powered by Creative Email', + 'desc' => 'A professional logo builder, marketing automations with WooCommerce and social management -- CreativeMail is a whole lot more than mail from Constant Contact.', + 'selected' => false, + 'shown' => true, + ), + 'optinmonster' => array( + 'slug' => 'optinmonster', + 'icon' => '--site-features-lead', + 'title' => 'Lead Generation', + 'subtitle' => 'Powered by Optin Monster', + 'desc' => 'Connect with website visitors using a proven kit of tools for growth using this offering from Awesome Motive.', + 'selected' => false, + 'shown' => true, ), - 'wc_priority' => array(), ), - ); - } + 'wc_standard' => array( + 'yith-woocommerce-ajax-search' => array( + 'slug' => 'yith-woocommerce-ajax-search', + 'icon' => '--site-features-search', + 'title' => 'Enhanced Product Search', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Give your visitors great search experiences with this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + 'nfd_slug_yith_woocommerce_ajax_product_filter' => array( + 'slug' => 'nfd_slug_yith_woocommerce_ajax_product_filter', + 'icon' => '--site-features-filter', + 'title' => 'Enhanced Product Filters', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Give your visitors powerful tools to discover your great products with this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + 'nfd_slug_yith_woocommerce_booking' => array( + 'slug' => 'nfd_slug_yith_woocommerce_booking', + 'icon' => '--site-features-bookingcalendar', + 'title' => 'Bookings & Appointments', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Have visitors book meetings and services with you, accepting payment and more using this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + 'nfd_slug_yith_woocommerce_wishlist' => array( + 'slug' => 'nfd_slug_yith_woocommerce_wishlist', + 'icon' => '--site-features-wishlist', + 'title' => 'Product Wishlists', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Let discerning shoppers curate their selections with a system of favorites using this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + ), + 'wc_premium' => array( + 'yith-woocommerce-ajax-search' => array( + 'slug' => 'yith-woocommerce-ajax-search', + 'icon' => '--site-features-search', + 'title' => 'Enhanced Product Search', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Give your visitors great search experiences with this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + 'nfd_slug_yith_woocommerce_ajax_product_filter' => array( + 'slug' => 'nfd_slug_yith_woocommerce_ajax_product_filter', + 'icon' => '--site-features-filter', + 'title' => 'Enhanced Product Filters', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Give your visitors powerful tools to discover your great products with this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + 'nfd_slug_yith_woocommerce_booking' => array( + 'slug' => 'nfd_slug_yith_woocommerce_booking', + 'icon' => '--site-features-bookingcalendar', + 'title' => 'Bookings & Appointments', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Have visitors book meetings and services with you, accepting payment and more using this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + 'nfd_slug_yith_woocommerce_wishlist' => array( + 'slug' => 'nfd_slug_yith_woocommerce_wishlist', + 'icon' => '--site-features-wishlist', + 'title' => 'Product Wishlists', + 'subtitle' => 'Powered by YITH', + 'desc' => 'Let discerning shoppers curate their selections with a system of favorites using this exclusive offering from our colleagues at YITH.', + 'selected' => false, + 'shown' => true, + ), + ), + 'wc_priority' => array(), + ), + ); /** - * Based on the flow type initial site features are marked. + * Retrieves all the site features for a particular flow and plan. * * @return array */ - private static function mark_initial_plugins() { - $plan_data = Data::current_plan(); - $plan_flow = $plan_data['flow']; - $plan_subtype = $plan_data['subtype']; - $installed_plugins = Plugins::get_init(); + public static function get() { + $plan_data = Data::current_plan(); + $plan_flow = $plan_data['flow']; + $plan_subtype = $plan_data['subtype']; - $site_features_marked = array(); - if ( $plan_flow && isset( self::get_site_features()[ $plan_flow ] ) ) { - if ( isset( self::get_site_features()[ $plan_flow ]['default'] ) ) { - $site_features_marked = array_merge( $site_features_marked, self::get_site_features()[ $plan_flow ]['default'] ); + $site_features = array(); + if ( $plan_flow && isset( self::$site_features_flow_plan_map[ $plan_flow ] ) ) { + if ( isset( self::$site_features_flow_plan_map[ $plan_flow ]['default'] ) ) { + $site_features = array_merge( $site_features, self::$site_features_flow_plan_map[ $plan_flow ]['default'] ); } - if ( 'default' !== $plan_subtype && isset( self::get_site_features()[ $plan_flow ][ $plan_subtype ] ) ) { - $site_features_marked = array_merge( $site_features_marked, self::get_site_features()[ $plan_flow ][ $plan_subtype ] ); + if ( 'default' !== $plan_subtype && isset( self::$site_features_flow_plan_map[ $plan_flow ][ $plan_subtype ] ) ) { + $site_features = array_merge( $site_features, self::$site_features_flow_plan_map[ $plan_flow ][ $plan_subtype ] ); } } + return $site_features; + } - if ( empty( $site_features_marked ) ) { + /** + * Retrieves all the site features for a particular flow and plan, marks + * each one as selected based on whether it has already been installed + * or is in the install queue. + * + * @return array + */ + public static function get_with_selections() { + $site_features = self::get(); + if ( empty( $site_features ) ) { return array(); } + $installed_plugins = Plugins::get_init(); foreach ( $installed_plugins as $installed_plugin ) { - if ( isset( $site_features_marked[ $installed_plugin['slug'] ] ) ) { - $site_features_marked[ $installed_plugin['slug'] ]['selected'] = true; + if ( isset( $site_features[ $installed_plugin['slug'] ] ) ) { + $site_features[ $installed_plugin['slug'] ]['selected'] = true; } } - return $site_features_marked; + return $site_features; } - - /** - * Retrieve marked site features - * - * @return array - */ - public static function get() { - return self::mark_initial_plugins(); - } - } diff --git a/includes/Data/Themes.php b/includes/Data/Themes.php index 5a8638b28..cb9a0b7b8 100644 --- a/includes/Data/Themes.php +++ b/includes/Data/Themes.php @@ -1,18 +1,18 @@ array( 'approved' => true, @@ -21,24 +21,29 @@ final class Themes { ), ); - // This is temporary, as we implement theme selections we can remove this. + /** + * Flow to necessary theme map. + * This is temporary, as we implement theme selections we can remove this. + * + * @var array + */ protected static $flow_default_theme_slugs = array( - 'wp-setup' => 'twentytwentythree', - 'ecommerce' => 'yith-wonder', + 'wp-setup' => 'twentytwentythree', + 'ecommerce' => 'yith-wonder', ); - /** - * @var array Initial themes to be installed classified based on the hosting plan. - * - * Key 'default' contains a list of default themes to be installed irrespective of the plan. - * Key contains a Key 'default' and a list of Key 's. - * Key => 'default' contains a list of default theme installs for . - * Key => contains a list of themes to be installed for a particular . - * - * The final queue of themes to be installed makes use of a max heap and hence the greater the number the earlier - * a theme will be placed for install in the queue. This will also allow us to - * prevent entering negative numbers when queueing a theme for earlier installs. - */ + /** + * Key 'default' contains a list of default themes to be installed irrespective of the plan. + * Key contains a Key 'default' and a list of Key 's. + * Key => 'default' contains a list of default theme installs for . + * Key => contains a list of themes to be installed for a particular . + * + * The final queue of themes to be installed makes use of a max heap and hence the greater the number the earlier + * a theme will be placed for install in the queue. This will also allow us to + * prevent entering negative numbers when queueing a theme for earlier installs. + * + * @var array Initial themes to be installed classified based on the hosting plan. + */ protected static $init_list = array( 'default' => array(), 'ecommerce' => array( @@ -53,7 +58,7 @@ final class Themes { ); /** - * Use this return value for a faster search of slugs. + * Returns the list of themes in a data structure that is faster to search. * * @return array */ @@ -64,33 +69,41 @@ public static function get() { } /** - * Get approved theme slugs. + * Get all the approved theme slugs. * * @return array */ - public static function get_approved() { return array( 'nfd_slugs' => array_keys( array_filter( self::$nfd_slugs, array( __CLASS__, 'check_approved' ) ) ), ); } + /** + * Checks if a slug has been approved. + * + * @param string $value The slug to check for + * @return boolean + */ private static function check_approved( $value ) { - return $value['approved'] === true; + return true === $value['approved']; } /** - * Get the current theme data like the theme variations and previews per step + * Get the number of previews that will be fetched in each step. + * This helps us show the number of necessary skeletons in the front end. * * @return array */ public static function step_preview_data() { - $theme_step_data = Patterns::get_count_of_patterns(); + $theme_step_data = Patterns::get_count_of_patterns(); + $site_features = count( SiteFeatures::get() ); + $theme_step_data['site-features']['previewCount'] = $site_features; return $theme_step_data; } /** - * Get the list of initial themes to be installed for a particular hosting plan. + * Get a list of initial themes to be installed for a particular hosting plan. * * @return array */ @@ -101,17 +114,23 @@ public static function get_init() { $init_list = self::$init_list['default']; if ( $plan_flow && isset( self::$init_list[ $plan_flow ] ) ) { if ( isset( self::$init_list[ $plan_flow ]['default'] ) ) { - $init_list = array_merge( $init_list, self::$init_list[ $plan_flow ]['default'] ); + $init_list = array_merge( $init_list, self::$init_list[ $plan_flow ]['default'] ); } - if ( $plan_subtype !== 'default' && isset( self::$init_list[ $plan_flow ][ $plan_subtype ] ) ) { - $init_list = array_merge( $init_list, self::$init_list[ $plan_flow ][ $plan_subtype ] ); + if ( 'default' !== $plan_subtype && isset( self::$init_list[ $plan_flow ][ $plan_subtype ] ) ) { + $init_list = array_merge( $init_list, self::$init_list[ $plan_flow ][ $plan_subtype ] ); } } return $init_list; } - // This is temporary, as we implement theme selections we can remove this. + /** + * Get the default necessary theme for a particular flow. + * This is temporary, as we implement theme selections we can remove this. + * + * @param string $flow The flow to get the theme for. + * @return string|boolean + */ public static function get_flow_default_theme_slug( $flow ) { return isset( self::$flow_default_theme_slugs[ $flow ] ) ? self::$flow_default_theme_slugs[ $flow ] : false; } diff --git a/includes/RestApi/PluginsController.php b/includes/RestApi/PluginsController.php index 2af786df3..78e183b75 100644 --- a/includes/RestApi/PluginsController.php +++ b/includes/RestApi/PluginsController.php @@ -14,13 +14,16 @@ * Class PluginsController */ class PluginsController { - /** + * The namespace of this controller's route. + * * @var string */ protected $namespace = 'newfold-onboarding/v1'; /** + * The base of this controller's route. + * * @var string */ protected $rest_base = '/plugins'; @@ -139,6 +142,11 @@ public function get_install_plugin_args() { ); } + /** + * Get the plugin status check arguments. + * + * @return array + */ public function get_status_args() { return array( 'plugin' => array( @@ -152,6 +160,11 @@ public function get_status_args() { ); } + /** + * Get the set site features arguments. + * + * @return array + */ public function set_site_features_args() { return array( 'plugins' => array( @@ -164,7 +177,7 @@ public function set_site_features_args() { /** * Verify caller has permissions to install plugins. * - * @param \WP_REST_Request $request + * @param \WP_REST_Request $request the incoming request object. * * @return boolean */ @@ -197,7 +210,7 @@ public function initialize() { /** * Install the requested plugin via a zip url (or) slug. * - * @param \WP_REST_Request $request + * @param \WP_REST_Request $request the incoming request object. * * @return \WP_REST_Response|\WP_Error */ @@ -238,6 +251,12 @@ public function install( \WP_REST_Request $request ) { return $plugin_install_task->execute(); } + /** + * Returns the status of a given plugin slug. + * + * @param \WP_REST_Request $request the incoming request object. + * @return \WP_REST_Response + */ public function get_status( \WP_REST_Request $request ) { $plugin = $request->get_param( 'plugin' ); $activated = $request->get_param( 'activated' ); @@ -253,7 +272,7 @@ public function get_status( \WP_REST_Request $request ) { $position_in_queue = PluginInstallTaskManager::status( $plugin ); - if ( $position_in_queue !== false ) { + if ( false !== $position_in_queue ) { return new \WP_REST_Response( array( 'status' => 'installing', @@ -273,18 +292,18 @@ public function get_status( \WP_REST_Request $request ) { } /** - * Retrieves the Customized list of Plugins for the user. + * Retrieves all the site features. * * @return array|\WP_Error */ public function get_site_features() { - return SiteFeatures::get(); + return SiteFeatures::get_with_selections(); } /** * Installs/Uninstalls the requested plugins. * - * @param \WP_REST_Request $request + * @param \WP_REST_Request $request the incoming request object. * * @return \WP_REST_Response|\WP_Error */