upgrade_captcha_options();
add_action('login_enqueue_scripts', array($this, 'aiowps_login_enqueue'));
}
/**
* This function handles upgrading captcha options
*
* @return void
*/
private function upgrade_captcha_options() {
global $aio_wp_security;
if (!empty($aio_wp_security->configs->get_value('aiowps_default_captcha'))) return;
// Upgrade the default captcha option
if ($aio_wp_security->configs->get_value('aiowps_default_recaptcha')) {
$aio_wp_security->configs->set_value('aiowps_default_recaptcha', '');
$aio_wp_security->configs->set_value('aiowps_default_captcha', 'google-recaptcha-v2');
} elseif ('1' == $aio_wp_security->configs->get_value('aiowps_enable_login_captcha') || '1' == $aio_wp_security->configs->get_value('aiowps_enable_registration_page_captcha')) {
$aio_wp_security->configs->set_value('aiowps_default_captcha', 'simple-math');
} else {
$aio_wp_security->configs->set_value('aiowps_default_captcha', 'none');
}
}
/**
* This function will return an array of supported CAPTCHA options
*
* @return array - an array of supported CAPTCHA options
*/
public function get_supported_captchas() {
return array(
'none' => 'No CAPTCHA',
'cloudflare-turnstile' => 'Cloudflare Turnstile',
'google-recaptcha-v2' => 'Google reCAPTCHA V2',
'simple-math' => 'Simple math CAPTCHA'
);
}
/**
* This function will display warning CAPTCHA settings not set.
*
* @global $aio_wp_security;
*
* @return void
*/
public static function warning_captcha_settings_notset() {
global $aio_wp_security;
$aiowps_default_captcha = $aio_wp_security->configs->get_value('aiowps_default_captcha');
if ('' == $aiowps_default_captcha || 'none' == $aiowps_default_captcha) {
$captcha_settings_link = '' . __('CAPTCHA settings', 'all-in-one-wp-security-and-firewall') . '';
echo '
';
echo sprintf(__('You should set %s before activating this feature.', 'all-in-one-wp-security-and-firewall'), $captcha_settings_link);
echo '
';
}
}
/**
* Enqueues the CAPTCHA script for the default CAPTCHA on the standard WP login page
*
* @return void
*/
public function aiowps_login_enqueue() {
global $aio_wp_security;
if ($aio_wp_security->is_login_lockdown_by_const()) return;
if ('1' != $aio_wp_security->configs->get_value('aiowps_enable_login_captcha') && '1' != $aio_wp_security->configs->get_value('aiowps_enable_registration_page_captcha')) return;
$default_captcha = $aio_wp_security->configs->get_value('aiowps_default_captcha');
switch ($default_captcha) {
case 'cloudflare-turnstile':
case 'google-recaptcha-v2':
wp_enqueue_script($default_captcha, $this->get_captcha_script_url($default_captcha), array(), AIO_WP_SECURITY_VERSION);
// Below is needed to provide some space for the CAPTCHA form (otherwise it appears partially hidden on RHS)
wp_add_inline_style('login', "#login { width: 340px; }");
break;
default:
break;
}
}
/**
* If the user is not on the WooCommerce account page, enqueue the CAPTCHA script in the wp_head for general pages
* Caters for scenarios when CAPTCHA is used on wp comments or custom wp login form pages
*
* @return void
*/
public function add_captcha_script() {
global $aio_wp_security;
// Do NOT enqueue if this is the main WooCommerce account login page because for WooCommerce page we "explicitly" render the reCAPTCHA widget
$is_woo = false;
// We don't want to load for Woo account page because we have a special function for this
if (function_exists('is_account_page')) $is_woo = is_account_page();
if (!empty($is_woo)) return;
$default_captcha = $aio_wp_security->configs->get_value('aiowps_default_captcha');
switch ($default_captcha) {
case 'cloudflare-turnstile':
case 'google-recaptcha-v2':
wp_enqueue_script($default_captcha, $this->get_captcha_script_url($default_captcha), array(), AIO_WP_SECURITY_VERSION);
break;
default:
break;
}
}
/**
* Renders CAPTCHA on form produced by the wp_login_form() function, ie, custom wp login form
*
* @param string $cust_html_code
*
* @return string
*/
public function insert_captcha_custom_login($cust_html_code) {
global $aio_wp_security;
if ($aio_wp_security->is_login_lockdown_by_const()) return '';
$default_captcha = $aio_wp_security->configs->get_value('aiowps_default_captcha');
switch ($default_captcha) {
case 'cloudflare-turnstile':
case 'google-recaptcha-v2':
$cust_html_code .= $this->get_captcha_form($default_captcha, 0, true);
return $cust_html_code;
break;
case 'simple-math':
$cap_form = '
';
$cust_html_code .= $cap_form;
return $cust_html_code;
break;
default:
return '';
break;
}
}
/**
* Explicit render CAPTCHA on WooCommerce my account page forms or if not just render normally
*
* @return void
*/
public function insert_captcha_question_form() {
global $aio_wp_security;
$default_captcha = $aio_wp_security->configs->get_value('aiowps_default_captcha');
switch ($default_captcha) {
case 'cloudflare-turnstile':
case 'google-recaptcha-v2':
// WooCommerce "my account" page needs special consideration, ie,
// need to display two CAPTCHA forms on same page (for login and register forms)
// For this case we use the "explicit" CAPTCHA display
$calling_hook = current_filter();
if ('woocommerce_login_form' == $calling_hook || 'woocommerce_lostpassword_form' == $calling_hook) {
$this->get_captcha_form($default_captcha, 1);
return;
}
if ('woocommerce_register_form' == $calling_hook) {
$this->get_captcha_form($default_captcha, 2);
return;
}
// For all other forms simply display CAPTCHA as normal
$this->display_captcha_form($default_captcha);
break;
case 'simple-math':
// Display plain maths CAPTCHA form
$this->display_captcha_form($default_captcha);
break;
default:
break;
}
}
/**
* For WooCommerce my account page - display two separate CAPTCHA forms "explicitly"
*
* @return void
*/
public function print_captcha_api_woo() {
global $aio_wp_security;
// We don't want to load for woo account page because we have a special function for this
if (function_exists('is_account_page') && !is_account_page()) return;
$default_captcha = $aio_wp_security->configs->get_value('aiowps_default_captcha');
if ('cloudflare-turnstile' == $default_captcha) :
$site_key = esc_html($aio_wp_security->configs->get_value('aiowps_turnstile_site_key'));
?>
configs->get_value('aiowps_recaptcha_site_key'));
?>
configs->get_value('aiowps_enable_bp_register_captcha') == '1' && defined('BP_VERSION')) {
//if buddy press feature active add action hook so buddy press can display our errors properly on bp registration form
do_action('bp_aiowps-captcha-answer_errors');
}
switch ($default_captcha) {
case 'cloudflare-turnstile':
if ('1' == $aio_wp_security->configs->get_value('aios_cloudflare_turnstile_invalid_configuration')) return;
$this->get_captcha_form($default_captcha);
break;
case 'google-recaptcha-v2':
if ('1' == $aio_wp_security->configs->get_value('aios_google_recaptcha_invalid_configuration')) return;
$this->get_captcha_form($default_captcha);
break;
case 'simple-math':
$cap_form = '