Skip to content

Commit

Permalink
Merge pull request #1976 from Codeinwp/onboarding
Browse files Browse the repository at this point in the history
[WiP] Raft Integrations
  • Loading branch information
HardeepAsrani authored Dec 18, 2023
2 parents 38240d2 + 2a1a87e commit d3bb4c3
Show file tree
Hide file tree
Showing 36 changed files with 3,293 additions and 15 deletions.
23 changes: 15 additions & 8 deletions .wp-env.override.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"WP_DEFAULT_THEME": "twentytwentythree"
},
"env": {
"development": {
"themes": [ "./test/emptytheme" ],
"mappings": {
"wp-content/themes/raft": "https://github.com/Codeinwp/raft/archive/refs/heads/onboarding.zip"
}
},
"tests": {
"config": {
"WP_DEBUG": false,
Expand All @@ -18,14 +24,15 @@
"plugins": [
"."
],
"themes": [ "./test/emptytheme" ],
"mappings": {
"wp-content/mu-plugins": "./packages/e2e-tests/mu-plugins",
"wp-content/plugins/gutenberg-test-plugins": "./packages/e2e-tests/plugins",
"wp-content/themes/gutenberg-test-themes": "./test/gutenberg-test-themes",
"wp-content/themes/gutenberg-test-themes/twentytwentyone": "https://downloads.wordpress.org/theme/twentytwentyone.1.7.zip",
"wp-content/themes/gutenberg-test-themes/twentytwentythree": "https://downloads.wordpress.org/theme/twentytwentythree.1.0.zip"
}
"themes": [ "./test/emptytheme" ],
"mappings": {
"wp-content/mu-plugins": "./packages/e2e-tests/mu-plugins",
"wp-content/plugins/gutenberg-test-plugins": "./packages/e2e-tests/plugins",
"wp-content/themes/gutenberg-test-themes": "./test/gutenberg-test-themes",
"wp-content/themes/gutenberg-test-themes/twentytwentyone": "https://downloads.wordpress.org/theme/twentytwentyone.1.7.zip",
"wp-content/themes/gutenberg-test-themes/twentytwentythree": "https://downloads.wordpress.org/theme/twentytwentythree.1.0.zip",
"wp-content/themes/raft": "https://github.com/Codeinwp/raft/archive/refs/heads/onboarding.zip"
}
}
}
}
2 changes: 2 additions & 0 deletions inc/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ public function autoload_classes() {
'\ThemeIsle\GutenbergBlocks\Plugins\Block_Conditions',
'\ThemeIsle\GutenbergBlocks\Plugins\Dashboard',
'\ThemeIsle\GutenbergBlocks\Plugins\Dynamic_Content',
'\ThemeIsle\GutenbergBlocks\Plugins\FSE_Onboarding',
'\ThemeIsle\GutenbergBlocks\Plugins\Options_Settings',
'\ThemeIsle\GutenbergBlocks\Plugins\Stripe_API',
'\ThemeIsle\GutenbergBlocks\Render\Masonry_Variant',
'\ThemeIsle\GutenbergBlocks\Server\Dashboard_Server',
'\ThemeIsle\GutenbergBlocks\Server\Dynamic_Content_Server',
'\ThemeIsle\GutenbergBlocks\Server\Stripe_Server',
'\ThemeIsle\GutenbergBlocks\Server\FSE_Onboarding_Server',
'\ThemeIsle\GutenbergBlocks\Integration\Form_Providers',
'\ThemeIsle\GutenbergBlocks\Integration\Form_Email',
'\ThemeIsle\GutenbergBlocks\Server\Form_Server',
Expand Down
2 changes: 2 additions & 0 deletions inc/plugins/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace ThemeIsle\GutenbergBlocks\Plugins;

use ThemeIsle\GutenbergBlocks\Pro;
use ThemeIsle\GutenbergBlocks\Plugins\FSE_Onboarding;

/**
* Class Dashboard
Expand Down Expand Up @@ -195,6 +196,7 @@ public function enqueue_options_assets() {
'docsLink' => Pro::get_docs_url(),
'showFeedbackNotice' => $this->should_show_feedback_notice(),
'deal' => ! Pro::is_pro_installed() ? $offer->get_localized_data() : array(),
'hasOnboarding' => false !== get_theme_support( FSE_Onboarding::SUPPORT_KEY ),
'days_since_install' => round( ( time() - get_option( 'otter_blocks_install', 0 ) ) / DAY_IN_SECONDS ),
'rootUrl' => get_site_url(),
)
Expand Down
291 changes: 291 additions & 0 deletions inc/plugins/class-fse-onboarding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
<?php
/**
* Otter FSE_Onboarding.
*
* @package ThemeIsle\GutenbergBlocks\Plugins
*/

namespace ThemeIsle\GutenbergBlocks\Plugins;

use ThemeIsle\GutenbergBlocks\Pro;

/**
* Class FSE_Onboarding
*/
class FSE_Onboarding {

const OPTION_KEY = 'otter_onboarding_status';
const SUPPORT_KEY = 'otter-onboarding';

/**
* The main instance var.
*
* @var FSE_Onboarding|null
*/
protected static $instance = null;

/**
* Initialize the class
*/
public function init() {
add_action( 'after_switch_theme', array( $this, 'on_switch_theme' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_options_assets' ) );
add_action( 'admin_menu', array( $this, 'register_menu_page' ) );
}

/**
* Register menu page
*
* @access public
* @return void
*/
public function register_menu_page() {
$has_support = get_theme_support( self::SUPPORT_KEY );

if ( false === $has_support || ! current_user_can( 'manage_options' ) || true !== boolval( get_option( 'themeisle_blocks_settings_onboarding_wizard', true ) ) ) {
return;
}

add_submenu_page(
'themes.php',
esc_html__( 'Theme Setup', 'otter-blocks' ),
esc_html__( 'Theme Setup', 'otter-blocks' ),
'manage_options',
'otter-onboarding',
function() {
$redirect = add_query_arg(
array(
'onboarding' => 'true',
),
admin_url( 'site-editor.php' )
);

echo '<p>Redirecting...</p>
<script>document.location.href = "' . esc_url( $redirect ) . '";</script>';
}
);
}

/**
* On switch theme
*
* @access public
* @return void
*/
public function on_switch_theme() {
// Check if the theme has support for FSE.
$support = get_theme_support( self::SUPPORT_KEY );

if ( false === $support ) {
return;
}

$status = get_option( self::OPTION_KEY, array() );
$slug = get_stylesheet();

if ( ! empty( $status[ $slug ] ) ) {
return;
}

// Run the onboarding.
$redirect = add_query_arg(
array(
'onboarding' => 'true',
),
admin_url( 'site-editor.php' )
);

// Redirect to the onboarding.
wp_safe_redirect( $redirect );
exit;
}

/**
* Set Onboarding Status
*
* @access public
* @return void
*/
public static function set_onboarding_status() {
$status = get_option( self::OPTION_KEY, array() );
$slug = get_stylesheet();

if ( ! empty( $status[ $slug ] ) ) {
return;
}

$status[ $slug ] = true;
update_option( self::OPTION_KEY, $status );
}

/**
* Get Theme Templates
*
* @access public
* @return array|false
*/
public function get_templates() {
$support = get_theme_support( self::SUPPORT_KEY );

if ( false === $support || ! is_array( $support ) || ( ! isset( $support[0]['templates'] ) && ! isset( $support[0]['page_templates'] ) ) ) {
return false;
}

$templates = array();

if ( isset( $support[0]['templates'] ) ) {
$templates = $support[0]['templates'];
}

if ( isset( $support[0]['page_templates'] ) ) {
$templates['page_templates'] = $support[0]['page_templates'];
}

if ( ! $templates ) {
return false;
}

foreach ( $templates as $key => $categories ) {
foreach ( $categories as $i => $template ) {
if ( file_exists( $template['file'] ) ) {
if ( 'php' === pathinfo( $template['file'], PATHINFO_EXTENSION ) ) {
$content = include $template['file'];
$templates[ $key ][ $i ]['content']['raw'] = $content;
} else {
$templates[ $key ][ $i ]['content']['raw'] = file_get_contents( $template['file'] );
}

unset( $templates[ $key ][ $i ]['file'] );
} else {
unset( $templates[ $key ][ $i ] );
}
}
}

return apply_filters( 'otter_fse_onboarding_templates', $templates );

}

/**
* Get Templates Types
*
* @access public
* @return array
*/
public function get_templates_types() {
$templates = $this->get_templates();

if ( ! $templates ) {
return array();
}

return array_keys( $templates );
}

/**
* Enqueue options assets.
*
* @access public
* @return void
*/
public function enqueue_options_assets() {
$current_screen = get_current_screen();
$has_support = get_theme_support( self::SUPPORT_KEY );

if (
false === $has_support ||
! current_user_can( 'manage_options' ) ||
! isset( $current_screen->id ) ||
'site-editor' !== $current_screen->id
) {
return;
}

// Flag onboarding status in case being run from a theme.
self::set_onboarding_status();

$asset_file = include OTTER_BLOCKS_PATH . '/build/onboarding/index.asset.php';

wp_enqueue_media();

wp_enqueue_style(
'otter-onboarding-styles',
OTTER_BLOCKS_URL . 'build/onboarding/style-index.css',
array( 'wp-components' ),
$asset_file['version']
);

wp_enqueue_script(
'otter-onboarding-scripts',
OTTER_BLOCKS_URL . 'build/onboarding/index.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);

wp_set_script_translations( 'otter-onboarding-scripts', 'otter-blocks' );

wp_localize_script(
'otter-onboarding-scripts',
'otterOnboardingData',
apply_filters(
'otter_onboarding_data',
array(
'version' => OTTER_BLOCKS_VERSION,
'assetsPath' => OTTER_BLOCKS_URL . 'assets/',
'supportedSteps' => $this->get_templates_types(),
'license' => apply_filters( 'product_otter_license_key', 'free' ),
'rootUrl' => get_site_url(),
'dashboardUrl' => get_admin_url(),
'isDev' => defined( 'ENABLE_OTTER_PRO_DEV' ),
'userEmail' => wp_get_current_user()->user_email,
)
)
);
}

/**
* The instance method for the static class.
* Defines and returns the instance of the static class.
*
* @static
* @since 1.7.1
* @access public
* @return FSE_Onboarding
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->init();
}

return self::$instance;
}

/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @access public
* @since 1.7.1
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'otter-blocks' ), '1.0.0' );
}

/**
* Disable unserializing of the class
*
* @access public
* @since 1.7.1
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'otter-blocks' ), '1.0.0' );
}
}
11 changes: 11 additions & 0 deletions inc/plugins/class-options-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ function ( $item ) {
),
)
);

register_setting(
'themeisle_blocks_settings',
'themeisle_blocks_settings_onboarding_wizard',
array(
'type' => 'boolean',
'description' => __( 'Enable Onboarding Wizard.', 'otter-blocks' ),
'show_in_rest' => true,
'default' => true,
)
);
}

/**
Expand Down
Loading

0 comments on commit d3bb4c3

Please sign in to comment.