Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created October 18, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/bb4fa1858c37ae1fc6f4ae60d8f30aaf to your computer and use it in GitHub Desktop.
Save wpmudev-sls/bb4fa1858c37ae1fc6f4ae60d8f30aaf to your computer and use it in GitHub Desktop.
[Defender] - Exclude site ids from using 2FA in a WordPress MultiSite installation
<?php
/**
* Plugin Name: [Defender] - Exclude Site ids from 2FA
* Plugin URI: https://premium.wpmudev.org/
* Description: Exclude site ids from using 2FA in a WordPress MultiSite installation
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Defender_Exclude_2FA' ) ) {
class WPMUDEV_Defender_Exclude_2FA {
public $site_ids = array( 2, 3 );
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Defender_Exclude_2FA();
}
return self::$_instance;
}
private function __construct() {
add_action( 'wp_login', [ $this, 'rm_passcode' ], 1, 2 );
add_action( 'admin_init', [ $this, 'release_profile' ] );
add_action( 'admin_init', [ $this, 'rm_user_box' ] );
}
protected function unhook( $tag, $hook_method, $hook_class ) {
global $wp_filter;
if ( ! isset( $wp_filter[$tag] ) ) {
return;
}
foreach ( $wp_filter[$tag]->callbacks as $key => $callback_array ) {
foreach ( $callback_array as $c_key => $callback ) {
if ( substr_compare( $c_key, $hook_method, strlen( $c_key )-strlen( $hook_method ), strlen( $hook_method ) ) === 0 ) {
if ( $callback['function'][0] instanceof $hook_class ){
unset( $wp_filter[$tag]->callbacks[$key][$c_key] );
}
}
}
}
}
public function rm_user_box() {
if ( ! in_array( get_current_blog_id(), $this->site_ids )) {
return;
}
$tag = 'show_user_profile';
$hook_method = 'showUsers2FactorActivation';
$hook_class = 'WP_Defender\\Module\\Advanced_Tools\\Component\\Auth_Listener';
$this->unhook( $tag, $hook_method, $hook_class );
}
public function release_profile() {
if ( ! in_array( get_current_blog_id(), $this->site_ids )) {
return;
}
$tag = 'current_screen';
$hook_method = 'forceProfilePage';
$hook_class = 'WP_Defender\\Module\\Advanced_Tools\\Component\\Auth_Listener';
$this->unhook( $tag, $hook_method, $hook_class );
}
public function rm_passcode( $userLogin, $user ) {
if ( ! in_array( get_current_blog_id(), $this->site_ids )) {
return;
}
//update_user_meta( $user->ID, 'defenderAuthOn', 1 );
//update_user_meta( $user->ID, 'defenderForceAuth', 1 );
$tag = 'wp_login';
$hook_method = 'maybeShowOTPLogin';
//$hook_class = 'WP_Defender\\Module\\Advanced_Tools\\Controller\\Main';
$hook_class = 'WP_Defender\\Module\\Advanced_Tools\\Component\\Auth_Listener';
$this->unhook( $tag, $hook_method, $hook_class );
}
}
if ( ! function_exists( 'wpmudev_defender_exclude_2fa' ) ) {
function wpmudev_defender_exclude_2fa() {
return WPMUDEV_Defender_Exclude_2FA::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_defender_exclude_2fa', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment