Last active
September 29, 2022 20:40
-
-
Save wpmudev-sls/6fff9198a6825c76764d742c726537b8 to your computer and use it in GitHub Desktop.
[Defender Pro] - 2FA setup page integration for WooCommerce (and others)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Defender Pro] - 2FA setup page integration for WooCommerce (and others) | |
* Description: Moves the 2FA setup page to the 'My Account' WC page, and restricts the shop navigation util it's completed | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-3822 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
// | |
// Note: this snippet also registers the [defender_2fa_settings_form] shortcode for more flexibility, | |
// even if WooCommerce is not installed. | |
// | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( 'plugins_loaded', function() { | |
if ( ! function_exists( 'wd_di' ) ) { | |
return; | |
} | |
// 1) General | |
// ------------------------------------------------------------------------------------- | |
add_shortcode( 'defender_2fa_settings_form', function() { | |
if ( ! function_exists( 'wd_di' ) ) { | |
return; | |
} | |
call_user_func_array( | |
array( wd_di()->get( '\WP_Defender\Controller\Two_Factor' ), 'show_user_profile' ), | |
array( wp_get_current_user() ) | |
); | |
}); | |
// 2) WooCommerce-related stuff | |
// ------------------------------------------------------------------------------------- | |
add_filter( 'woocommerce_login_redirect', function( $redirect, $user ) { | |
if ( false !== strpos( $redirect, '#defender-security' ) ) { | |
return wc_get_page_permalink( 'myaccount' ) . 'edit-account/#defender-security'; | |
} | |
return $redirect; | |
}, 9999, 2 ); | |
add_filter( 'template_redirect', function() { | |
global $wp; | |
if ( ! is_user_logged_in() || wp_doing_ajax() || ! function_exists( 'is_shop' ) ) { | |
return; | |
} | |
$is_wc_page = is_shop() // Is any page of the shop... | |
|| is_page( 'cart' ) || is_cart() // ...or the cart | |
|| is_checkout() // ...or the checkout page | |
|| is_checkout_pay_page() // ...or the checkout payment page | |
|| is_account_page() // ...or any of WC 'My account' pages | |
; | |
if ( $is_wc_page ) { | |
$two_factor = wd_di()->get('\WP_Defender\Controller\Two_Factor'); | |
$should_redirect = false !== $two_factor->handle_woocommerce_login_redirect( false, wp_get_current_user() ); | |
if ( $should_redirect ) { | |
// Hide other menu items from WC My Account page. | |
add_filter ( 'woocommerce_account_menu_items', function( $menu ) { | |
unset( $menu['edit-address'] ); // Addresses | |
unset( $menu['dashboard'] ); // Dashboard | |
unset( $menu['payment-methods'] ); // Payment Methods | |
unset( $menu['orders'] ); // Orders | |
unset( $menu['downloads'] ); // Downloads | |
return $menu; | |
}); | |
} | |
if ( ! is_wc_endpoint_url( 'edit-account' ) ) { | |
if ( ! empty( $wp->request ) && false !== strpos( $wp->request, 'customer-logout' ) ) { | |
return; | |
} | |
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) . 'edit-account/#defender-security' ); | |
die; | |
} | |
} | |
}); | |
add_action( 'woocommerce_edit_account_form', function() { | |
echo do_shortcode( '[defender_2fa_settings_form]' ); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment