| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access.'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Purpose of this class: abstract out code handling integrations with login forms |
| 7 |
*/ |
| 8 |
|
| 9 |
class Simba_TFA_Login_Form_Integrations { |
| 10 |
|
| 11 |
// Main class |
| 12 |
private $tfa; |
| 13 |
|
| 14 |
/** |
| 15 |
* Plugin constructor |
| 16 |
* |
| 17 |
* @param Object $tfa |
| 18 |
*/ |
| 19 |
public function __construct($tfa) { |
| 20 |
|
| 21 |
$this->tfa = $tfa; |
| 22 |
|
| 23 |
$enqueue_upon_actions = array( |
| 24 |
// This is needed for the login form on the dedicated payment page (e.g. /checkout/order-pay/123456/?pay_for_order=true&key=wc_order_blahblahblah) |
| 25 |
'woocommerce_login_form_start', |
| 26 |
'woocommerce_before_customer_login_form', |
| 27 |
// The login form on the checkout doesn't call the woocommerce_before_customer_login_form action |
| 28 |
'woocommerce_before_checkout_form', |
| 29 |
'affwp_login_fields_before', |
| 30 |
); |
| 31 |
|
| 32 |
foreach ($enqueue_upon_actions as $action) { |
| 33 |
add_action($action, array($this->tfa, 'login_enqueue_scripts')); |
| 34 |
} |
| 35 |
|
| 36 |
if (!defined('TWO_FACTOR_DISABLE') || !TWO_FACTOR_DISABLE) { |
| 37 |
add_action('affwp_process_login_form', array($this, 'affwp_process_login_form')); |
| 38 |
} |
| 39 |
|
| 40 |
add_filter('tml_display', array($this, 'tml_display')); |
| 41 |
|
| 42 |
// We want to run first if possible, so that we're not aborted by JavaScript exceptions in other components (our code is critical to the login process for TFA users) |
| 43 |
// Unfortunately, though, people start enqueuing from init onwards (before that is buggy - https://core.trac.wordpress.org/ticket/11526), so, we try to detect the login page and go earlier there. |
| 44 |
if (isset($GLOBALS['pagenow']) && 'wp-login.php' === $GLOBALS['pagenow']) { |
| 45 |
add_action('init', array($this->tfa, 'login_enqueue_scripts'), -99999999999); |
| 46 |
} else { |
| 47 |
add_action('login_enqueue_scripts', array($this->tfa, 'login_enqueue_scripts'), -99999999999); |
| 48 |
} |
| 49 |
|
| 50 |
add_filter('do_shortcode_tag', array($this, 'do_shortcode_tag'), 10, 2); |
| 51 |
|
| 52 |
add_filter('simba_tfa_login_enqueue_localize', array($this, 'simba_tfa_login_enqueue_localize'), 9); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Catch TML login widgets (other TML login forms already trigger) |
| 58 |
* |
| 59 |
* @param Mixed $whatever |
| 60 |
* |
| 61 |
* @return Mixed |
| 62 |
*/ |
| 63 |
public function tml_display($whatever) { |
| 64 |
$this->tfa->login_enqueue_scripts(); |
| 65 |
return $whatever; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Runs upon the WP filter simba_tfa_login_enqueue_localize. |
| 70 |
* |
| 71 |
* @param Array $localize |
| 72 |
* |
| 73 |
* @return Array |
| 74 |
*/ |
| 75 |
public function simba_tfa_login_enqueue_localize($localize) { |
| 76 |
// WP login form is #loginform |
| 77 |
// Ultimate Membership Pro - April 2018 |
| 78 |
// Theme My Login 6.x - .tml-login form[name="loginform"] |
| 79 |
// Theme My Login 7.x - .tml-login form[name="login"] (July 2018) |
| 80 |
// WP Members - March 2018 |
| 81 |
// bbPress - June 2021 |
| 82 |
// WooCommerce - ported over from the separate wooextend.js code, June 2021 |
| 83 |
// Affiliates WP - ported over from the separate wooextend.js code, June 2021 |
| 84 |
$localize['login_form_selectors'] .= '.tml-login form[name="loginform"], .tml-login form[name="login"], #loginform, #wpmem_login form, form#ihc_login_form, .bbp-login-form, .woocommerce form.login, #affwp-login-form'; |
| 85 |
$localize['login_form_off_selectors'] .= '#ihc_login_form'; |
| 86 |
return $localize; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Runs upon the WP action affwp_process_login_form |
| 91 |
*/ |
| 92 |
public function affwp_process_login_form() { |
| 93 |
|
| 94 |
if (!function_exists('affiliate_wp')) return; |
| 95 |
|
| 96 |
$affiliate_wp = affiliate_wp(); |
| 97 |
$login = $affiliate_wp->login; |
| 98 |
|
| 99 |
$params = array( |
| 100 |
'log' => stripslashes($_POST['affwp_user_login']), |
| 101 |
'caller'=> $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['REQUEST_URI'], |
| 102 |
'two_factor_code' => stripslashes((string) $_POST['two_factor_code']) |
| 103 |
); |
| 104 |
$code_ok = $this->tfa->authorise_user_from_login($params, true); |
| 105 |
|
| 106 |
$code_ok = apply_filters('simbatfa_affwp_process_login_form_auth_result', $code_ok, $params); |
| 107 |
|
| 108 |
if (is_wp_error($code_ok)) { |
| 109 |
$login->add_error($code_ok->get_error_code, $code_ok->get_error_message()); |
| 110 |
} elseif (!$code_ok) { |
| 111 |
$login->add_error('authentication_failed', __('Error:', 'two-factor-authentication').' '.__('The one-time password (TFA code) you entered was incorrect.', 'two-factor-authentication')); |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Ultimate Membership Pro support |
| 118 |
* |
| 119 |
* @param String $output |
| 120 |
* @param String $tag |
| 121 |
* |
| 122 |
* @return String |
| 123 |
*/ |
| 124 |
public function do_shortcode_tag($output, $tag) { |
| 125 |
if ('ihc-login-form' == $tag) $this->tfa->login_enqueue_scripts(); |
| 126 |
return $output; |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|