From 166c83f95f113b2d01602f6d946beed6ed022e17 Mon Sep 17 00:00:00 2001 From: itthinx Date: Fri, 29 Nov 2024 19:16:03 +0100 Subject: [PATCH] revised welcome sequence --- changelog.txt | 2 +- lib/admin/class-groups-admin-welcome.php | 61 ++++++++++++++++-------- lib/core/class-groups-controller.php | 46 +++++++++++++++++- 3 files changed, 87 insertions(+), 22 deletions(-) diff --git a/changelog.txt b/changelog.txt index 4bb0542..29c2b33 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,7 +4,7 @@ * WordPress 6.7 compatible. * Adds fixes for "Translation loading for the ... domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. ..." PHP notices raised as of WordPress 6.7.0 in certain scenarios when translations are requested before the init or the after_setup_theme action have fired. * Avoids a potential infinite recursion when evaluating post counts. -* Make sure to present welcome screen only when Groups is activated alone and not in bulk. +* Restrict the redirect to the welcome screen so it happens only when Groups is activated alone and not in bulk. Updates do not trigger a redirect. A legacy update does not trigger a redirect and now only causes an admin notice to be displayed temporarily. = 3.3.0 = * WordPress 6.7 compatible. diff --git a/lib/admin/class-groups-admin-welcome.php b/lib/admin/class-groups-admin-welcome.php index b607c74..909a85f 100644 --- a/lib/admin/class-groups-admin-welcome.php +++ b/lib/admin/class-groups-admin-welcome.php @@ -36,6 +36,7 @@ public static function init() { add_action( 'admin_head', array( __CLASS__, 'admin_head' ) ); add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 ); + add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); } /** @@ -58,6 +59,40 @@ public static function admin_head() { remove_submenu_page( 'index.php', 'groups-welcome' ); } + /** + * Output admin notices. + * + * @since 3.3.1 + */ + public static function admin_notices() { + $is_updated_legacy = get_transient( 'groups_plugin_updated_legacy' ); + if ( $is_updated_legacy ) { + $message = '

'; + $message .= ''; + $message .= esc_html__( 'Important', 'groups' ); + $message .= ''; + $message .= ' '; + $message .= esc_html__( 'It seems that you have updated from Groups 1.x where access restrictions were based on capabilities.', 'groups' ); + $message .= '

'; + $message .= '

'; + $message .= sprintf( + esc_html__( 'Please make sure to read the %1$sMigration Guide%2$s.', 'groups' ), + '', + '' + ); + $message .= '

'; + wp_admin_notice( + $message, + array( + 'type' => 'info', + 'dismissible' => true, + 'additional_classes' => array( 'inline', 'notice-alt' ), + 'attributes' => array( 'data-slug' => 'plugin-slug' ) + ) + ); + } + } + /** * Checks if the welcome screen should be shown and redirected to. */ @@ -74,7 +109,8 @@ public static function admin_init() { } $groups_welcome_dismiss = Groups_Options::get_user_option( 'groups-welcome-dismiss', '' ); if ( version_compare( $groups_version, $groups_welcome_dismiss ) > 0 ) { - if ( get_transient( 'groups_plugin_activated' ) || get_transient( 'groups_plugin_updated_legacy' ) ) { + // @see Groups_Controller::activate() + if ( get_transient( 'groups_plugin_activated' ) ) { $doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; $doing_cron = defined( 'DOING_CRON' ) && DOING_CRON; // we'll delete the transients in the welcome screen handler @@ -83,19 +119,14 @@ public static function admin_init() { !$doing_cron && ( empty( $_GET['page'] ) || $_GET['page'] !== 'groups-welcome' ) && !is_network_admin() && - isset( $_REQUEST['activate'] ) && // @since 3.3.1 must be single activation - !isset( $_REQUEST['activate-multi'] ) && - !isset( $_REQUEST['activate-selected'] ) && // @since 3.3.1 must not be multiple activation - !isset( $_REQUEST['update-selected'] ) && // @since 3.3.1 must not be multiple update Groups_User::current_user_can( GROUPS_ACCESS_GROUPS ) && apply_filters( 'groups_welcome_show', true ) ) { wp_safe_redirect( admin_url( 'index.php?page=groups-welcome' ) ); exit; } else { - // @since 3.3.1 remove transients as we don't want to trigger a redirect in any other case than direct activation of the Groups plugin, also see above + // @since 3.3.1 remove the transient as we don't want to trigger a redirect in any other case than direct activation of the Groups plugin delete_transient( 'groups_plugin_activated' ); - delete_transient( 'groups_plugin_updated_legacy' ); } } } @@ -133,7 +164,9 @@ public static function groups_welcome() { wp_enqueue_style( 'groups_admin' ); delete_transient( 'groups_plugin_activated' ); - $legacy_update = get_transient( 'groups_plugin_updated_legacy' ); + // As of Groups 3.3.1 this transient does not trigger a redirect to the welcome screen. + // Instead, an admin notice pointing to the migration guide is displayed for as long as the transient has not expired. + // We still delete the transient here as we are displaying the welcome screen, and the notice will be visible on top of it. delete_transient( 'groups_plugin_updated_legacy' ); echo '
'; @@ -158,18 +191,6 @@ public static function groups_welcome() { esc_html_e( 'Thanks for using Groups! We have made it even easier to protect your content and hope you like it :)', 'groups' ); echo '

'; - if ( $legacy_update ) { - echo '

'; - echo ''; - esc_html_e( 'Important', 'groups' ); - echo ''; - echo '

'; - esc_html_e( 'It seems that you have updated from Groups 1.x where access restrictions were based on capabilities.', 'groups' ); - echo '
'; - printf( wp_kses_post( __( 'Please make sure to read the notes on Switching to Groups %s below.', 'groups' ) ), esc_html( $groups_version ) ); - echo '

'; - } - echo '

'; esc_html_e( "What's New?", 'groups' ); echo '

'; diff --git a/lib/core/class-groups-controller.php b/lib/core/class-groups-controller.php index d840e10..9710238 100644 --- a/lib/core/class-groups-controller.php +++ b/lib/core/class-groups-controller.php @@ -261,7 +261,10 @@ public static function activate( $network_wide = false ) { } } else { self::setup(); - set_transient( 'groups_plugin_activated', true, 60 ); + // @since 3.3.1 Check here already if it is a single activation of Groups and only then set the transient + if ( self::is_single_activate() ) { + set_transient( 'groups_plugin_activated', true, 60 ); + } } if ( $sem_id !== false ) { self::sem_release( $sem_id ); @@ -523,6 +526,47 @@ private static function cleanup( $drop = false ) { } } + /** + * Whether this is an individual activation of the plugin. + * + * @since 3.3.1 + * + * @return boolean + */ + private static function is_single_activate() { + $is = false; + $groups_basename = plugin_basename( GROUPS_FILE ); + if ( isset( $_REQUEST['action'] ) ) { + switch ( $_REQUEST['action'] ) { + case 'activate': + // Single plugin activation of Groups: + if ( !empty( $_REQUEST['plugin'] ) ) { + $slug = wp_unslash( $_REQUEST['plugin'] ); + if ( $slug === $groups_basename ) { + $is = true; + } + } + break; + case 'activate-selected': + // Bulk plugin activation of Groups but it is the only plugin being activated: + if ( !empty( $_REQUEST['checked'] ) ) { + if ( is_array( $_REQUEST['checked'] ) ) { + if ( count( $_REQUEST['checked'] ) === 1 ) { + $slugs = wp_unslash( $_REQUEST['checked'] ); + $slug = array_pop( $slugs ); + if ( $slug === $groups_basename ) { + $is = true; + break; + } + } + } + } + break; + } + } + return $is; + } + /** * Determines the default capabilities for the administrator role. * In lack of an administrator role, these capabilities are assigned