Skip to content

Commit

Permalink
Merge pull request #366 from newfold-labs/release/2.5.1
Browse files Browse the repository at this point in the history
Release/2.5.1
  • Loading branch information
wpscholar authored Dec 1, 2023
2 parents ca0efee + 9b66258 commit a7ef06e
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 421 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"newfold-labs/wp-module-loader": "^1.0.10",
"newfold-labs/wp-module-marketplace": "^2.1.0",
"newfold-labs/wp-module-notifications": "^1.1.6",
"newfold-labs/wp-module-onboarding": "^1.11.10",
"newfold-labs/wp-module-onboarding": "1.11.8",
"newfold-labs/wp-module-performance": "^1.2.2",
"newfold-labs/wp-module-runtime": "^1.0.7",
"newfold-labs/wp-module-secure-passwords": "^1.1",
Expand Down
82 changes: 21 additions & 61 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 39 additions & 29 deletions inc/LoginRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,36 @@
*/
class LoginRedirect {

/**
* @var \NewfoldLabs\WP\ModuleLoader\Container
*/
public static $container;

/**
* Initialize the login redirect functionality.
*/
public static function init() {

global $nfd_module_container;
self::$container = $nfd_module_container;

add_action( 'login_redirect', array( __CLASS__, 'on_login_redirect' ), 10, 3 );
add_action( 'login_init', array( __CLASS__, 'on_login_init' ), 10, 3 );
add_action( 'admin_init', array( __CLASS__, 'disable_yoast_onboarding_redirect' ), 2 );
add_action( 'admin_init', array( __CLASS__, 'disable_yoast_onboarding_redirect' ), 2 );

add_filter( 'login_form_defaults', array( __CLASS__, 'filter_login_form_defaults' ) );
add_filter( 'newfold_sso_success_url_default', array( __CLASS__, 'get_default_redirect_url' ) );
}

/**
* Check if we should redirect.
* Redirect only if abTestPluginHome capability is true
*
* @return boolean
*/
public static function should_redirect() {
global $nfd_module_container;
return $nfd_module_container->get( 'capabilities' )->get('abTestPluginHome');
}
/**
* Check if we should redirect.
* Redirect only if abTestPluginHome capability is true
*
* @return boolean
*/
public static function should_redirect() {
return current_user_can( 'manage_options' ) && self::$container->get( 'capabilities' )->get( 'abTestPluginHome' );
}

/**
* Get default redirect URL.
Expand All @@ -39,7 +48,7 @@ public static function should_redirect() {
* @return string
*/
public static function get_default_redirect_url( $url ) {
return current_user_can( 'manage_options' ) ? self::get_plugin_dashboard_url() : $url;
return self::should_redirect() ? self::get_plugin_dashboard_url() : $url;
}

/**
Expand All @@ -60,27 +69,27 @@ public static function on_login_init() {
* @return array
*/
public static function filter_login_form_defaults( array $defaults ) {
if ( self::should_redirect() ) {
$defaults['redirect'] = self::get_plugin_dashboard_url();
}
if ( self::should_redirect() ) {
$defaults['redirect'] = self::get_plugin_dashboard_url();
}

return $defaults;
}

/**
* Customize the login redirect URL if one hasn't already been set.
*
* @param string $redirect_to Current redirect URL.
* @param string $requested_redirect_to Requested redirect URL.
* @param \WP_User $user WordPress user.
* @param string $redirect_to Current redirect URL.
* @param string $requested_redirect_to Requested redirect URL.
* @param \WP_User $user WordPress user.
*
* @return string
*/
public static function on_login_redirect( $redirect_to, $requested_redirect_to, $user ) {

if ( self::is_user( $user ) && self::should_redirect() ) {
// If no redirect is defined and the user is an administrator, redirect to the Plugin dashboard.
if ( (empty( $requested_redirect_to ) || admin_url( '/' ) === $requested_redirect_to ) && self::is_administrator( $user ) ) {
if ( ( empty( $requested_redirect_to ) || admin_url( '/' ) === $requested_redirect_to ) && self::is_administrator( $user ) ) {
return self::get_plugin_dashboard_url();
}

Expand All @@ -93,14 +102,14 @@ public static function on_login_redirect( $redirect_to, $requested_redirect_to,
return $redirect_to;
}

/**
* Disable Yoast onboarding redirect.
*/
public static function disable_yoast_onboarding_redirect() {
if ( class_exists( 'WPSEO_Options' ) && self::should_redirect() ) {
/**
* Disable Yoast onboarding redirect.
*/
public static function disable_yoast_onboarding_redirect() {
if ( class_exists( 'WPSEO_Options' ) && self::should_redirect() ) {
\WPSEO_Options::set( 'should_redirect_after_install_free', false );
}
}
}

/**
* Check if we have a valid user.
Expand Down Expand Up @@ -132,7 +141,8 @@ public static function is_administrator( $user ) {
* @return bool
*/
public static function is_plugin_redirect( $redirect ) {
$plugin_id = self::get_plugin_id();
$plugin_id = self::get_plugin_id();

return false !== strpos( $redirect, admin_url( 'admin.php?page=' . $plugin_id ) );
}

Expand All @@ -143,6 +153,7 @@ public static function is_plugin_redirect( $redirect ) {
*/
public static function get_plugin_dashboard_url() {
$plugin_id = self::get_plugin_id();

return admin_url( 'admin.php?page=' . $plugin_id . '#/home' );
}

Expand All @@ -152,8 +163,7 @@ public static function get_plugin_dashboard_url() {
* @return string
*/
public static function get_plugin_id() {
global $nfd_module_container;
return $nfd_module_container->plugin()->id;
return self::$container->plugin()->id;
}

}
Expand Down
Loading

0 comments on commit a7ef06e

Please sign in to comment.