Skip to content

Commit

Permalink
Merge pull request #7581 from Automattic/change/setup-wizard-redirect
Browse files Browse the repository at this point in the history
Change Setup Wizard redirect to work on all relevant pages
  • Loading branch information
renatho authored Apr 9, 2024
2 parents 0132d5c + 693851e commit 9410aa2
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 61 deletions.
4 changes: 4 additions & 0 deletions changelog/change-setup-wizard-redirect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Open Setup Wizard when navigating through relevant pages on admin if it didn't open yet
76 changes: 61 additions & 15 deletions includes/admin/class-sensei-setup-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function __construct() {
add_action( 'current_screen', [ $this, 'remove_notices_from_setup_wizard' ] );
add_action( 'admin_notices', [ $this, 'setup_wizard_notice' ] );
add_action( 'admin_init', [ $this, 'skip_setup_wizard' ] );
add_action( 'admin_init', [ $this, 'activation_redirect' ] );
add_action( 'current_screen', [ $this, 'activation_redirect' ] );
add_action( 'current_screen', [ $this, 'add_setup_wizard_help_tab' ] );

// Maybe prevent WooCommerce help tab.
Expand Down Expand Up @@ -189,18 +189,20 @@ public function prepare_wizard_page() {
public function activation_redirect() {
if (
// Check if activation redirect is needed.
! get_transient( 'sensei_activation_redirect' )
! get_option( 'sensei_activation_redirect', false )
// Test whether the context of execution comes from async action scheduler.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Arguments used for comparison.
|| ( isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === $_REQUEST['action'] )
// On these pages, or during these events, postpone the redirect.
|| wp_doing_ajax() || wp_doing_cron() || is_network_admin() || ! current_user_can( 'manage_sensei' )
|| wp_doing_ajax() || wp_doing_cron() || is_network_admin()
// Only redirects for admin users.
|| ! current_user_can( 'manage_sensei' )
// Check if it's an admin screen that should redirect.
|| ! $this->should_current_page_redirect_to_wizard()
) {
return;
}

delete_transient( 'sensei_activation_redirect' );

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Arguments used for comparison.
if ( isset( $_GET['activate-multi'] ) ) {
return;
Expand All @@ -221,6 +223,8 @@ protected function redirect_to_setup_wizard() {
* Render app container for setup wizard.
*/
public function render_wizard_page() {
// Delete option when the Setup Wizard is loaded, so it doesn't redirect anymore.
delete_option( 'sensei_activation_redirect' );

?>
<div id="sensei-setup-wizard-page" class="sensei-setup-wizard">
Expand All @@ -234,16 +238,39 @@ public function render_wizard_page() {
*
* @return boolean
*/
private function should_current_page_display_wizard() {
$screen = get_current_screen();
private function should_current_page_display_wizard_notice() {
// Dashboard, plugins, and Sensei pages, except Sensei Home.
$screens_without_sensei_prefix = [
'dashboard',
'plugins',
'edit-sensei_message',
'edit-course',
'edit-course-category',
'admin_page_course-order',
'edit-module',
'admin_page_module-order',
'edit-lesson',
'edit-lesson-tag',
'admin_page_lesson-order',
'edit-question',
'question',
'edit-question-category',
];

if ( false !== strpos( $screen->id, 'sensei-lms_page_sensei' ) ) {
return true;
}
return $this->check_sensei_screen( $screens_without_sensei_prefix );
}

/**
* Check if current screen is selected to redirect to the wizard.
*
* @return boolean
*/
private function should_current_page_redirect_to_wizard() {
// Dashboard, plugins, and Sensei pages.
$screens_without_sensei_prefix = [
'dashboard',
'plugins',
'toplevel_page_sensei',
'edit-sensei_message',
'edit-course',
'edit-course-category',
Expand All @@ -258,7 +285,26 @@ private function should_current_page_display_wizard() {
'edit-question-category',
];

return in_array( $screen->id, $screens_without_sensei_prefix, true );
return $this->check_sensei_screen( $screens_without_sensei_prefix );
}

/**
* Check if current screen is a Sensei screen.
* The default check verifies if the screen ID contains 'sensei-lms_page_sensei'.
* For more screens to be checked, pass the IDs as an array.
*
* @param array $other_screens Other screens to check.
*
* @return boolean
*/
private function check_sensei_screen( $other_screens = [] ) {
$screen = get_current_screen();

if ( false !== strpos( $screen->id, 'sensei-lms_page_sensei' ) ) {
return true;
}

return in_array( $screen->id, $other_screens, true );
}

/**
Expand All @@ -268,7 +314,7 @@ private function should_current_page_display_wizard() {
*/
public function setup_wizard_notice() {
if (
! $this->should_current_page_display_wizard()
! $this->should_current_page_display_wizard_notice()
|| ! get_option( self::SUGGEST_SETUP_WIZARD_OPTION, 0 )
|| ! current_user_can( 'manage_sensei' )
) {
Expand Down Expand Up @@ -446,7 +492,7 @@ public function get_woocommerce_connect_data() {
*
* @return stdClass Extension with status.
*/
private function get_feature_with_status( $extension, $installing_plugins, $selected_plugins ) {
private function get_feature_with_status( $extension, $installing_plugins, $selected_plugins ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Called by a public deprecated method.
_deprecated_function( __METHOD__, '4.8.0' );

$installing_key = array_search( $extension->product_slug, wp_list_pluck( $installing_plugins, 'product_slug' ), true );
Expand Down Expand Up @@ -496,7 +542,7 @@ public function get_sensei_extensions( $clear_active_plugins_cache = false ) {
}

$extensions = array_map(
function( $extension ) use ( $installing_plugins, $selected_plugins ) {
function ( $extension ) use ( $installing_plugins, $selected_plugins ) {
// Decode price.
if ( isset( $extension->price ) && 0 !== $extension->price ) {
$extension->price = html_entity_decode( $extension->price );
Expand Down Expand Up @@ -545,7 +591,7 @@ public static function close_wccom_install() {

if (
isset( $_SERVER['HTTP_REFERER'] ) &&
0 === strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout' ) && // phpcs:ignore sanitization ok.
0 === strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout' ) && // phpcs:ignore -- sanitization ok.
false !== get_transient( $wccom_installing_transient )
) {
delete_transient( $wccom_installing_transient );
Expand Down
3 changes: 2 additions & 1 deletion includes/class-sensei-data-cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Sensei_Data_Cleaner {
'sensei_home_tasks_dismissed',
'sensei_home_tasks_list_is_completed',
'sensei-home-task-pro-upsell',
'sensei_activation_redirect',
);

/**
Expand Down Expand Up @@ -233,7 +234,7 @@ class Sensei_Data_Cleaner {
'sensei_answers_feedback_[0-9]+_[0-9]+',
'quiz_grades_[0-9]+_[0-9]+',
'sensei_comment_counts_[0-9]+',
'sensei_activation_redirect',
'sensei_activation_redirect', // @deprecated $$next-version$$ Changed to an option.
'sensei_woocommerce_plugin_information',
'sensei_extensions_.*',
'sensei_background_job_.*',
Expand Down
2 changes: 1 addition & 1 deletion includes/class-sensei.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ public function activate_sensei() {
// Do not enable the wizard for sites that are created with the onboarding flow.
if ( 'sensei' !== get_option( 'site_intent' ) ) {

set_transient( 'sensei_activation_redirect', 1, 30 );
update_option( 'sensei_activation_redirect', 1 );
update_option( Sensei_Setup_Wizard::SUGGEST_SETUP_WIZARD_OPTION, 1 );

} else {
Expand Down
9 changes: 9 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@
<exclude-pattern>**/views/*</exclude-pattern>
<exclude-pattern>tests/bootstrap.php</exclude-pattern>
</rule>

<rule ref="WordPress.WP.Capabilities">
<properties>
<property name="custom_capabilities" type="array">
<element value="manage_sensei"/>
</property>
</properties>
</rule>

</ruleset>
Loading

0 comments on commit 9410aa2

Please sign in to comment.