';
wp_cache_flush();
$upgrader = new Plugin_Upgrader();
echo 'Check if WP 301 Redirects is already installed ... ';
if (self::is_plugin_installed($plugin_slug)) {
echo 'WP 301 Redirects is already installed!
Making sure it\'s the latest version. ';
$upgrader->upgrade($plugin_slug);
$installed = true;
} else {
echo 'Installing WP 301 Redirects. ';
$installed = $upgrader->install($plugin_zip);
}
wp_cache_flush();
if (!is_wp_error($installed) && $installed) {
echo 'Activating WP 301 Redirects. ';
$activate = activate_plugin($plugin_slug);
if (is_null($activate)) {
echo 'WP 301 Redirects Activated. ';
echo '';
echo ' If you are not redirected in a few seconds - click here.';
}
} else {
echo 'Could not install WP 301 Redirects. You\'ll have to download and install manually.';
}
echo '
';
} // install_wp301
/**
* Check if given plugin is installed
*
* @param [string] $slug Plugin slug
* @return boolean
*/
static function is_plugin_installed($slug)
{
if (!function_exists('get_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
if (!empty($all_plugins[$slug])) {
return true;
} else {
return false;
}
} // is_plugin_installed
static function countFails($username = "")
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
$ip = WPCaptcha_Utility::getUserIP();
// phpcs:ignore db call warning as we are using a custom table
$numFails = $wpdb->get_var( // phpcs:ignore
$wpdb->prepare(
"SELECT COUNT(login_attempt_ID) FROM " . $wpdb->wpcatcha_login_fails . " WHERE login_attempt_date + INTERVAL %d MINUTE > %s AND login_attempt_IP = %s",
array($options['retries_within'], current_time('mysql'), $ip)
)
);
return $numFails;
}
static function incrementFails($username = "", $reason = "")
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
$ip = WPCaptcha_Utility::getUserIP();
$identifier = sanitize_text_field($username);
$user = get_user_by('login', $identifier);
if (false === $user && is_email($identifier)) {
$user = get_user_by('email', $identifier);
}
if ($user || 1 == $options['lockout_invalid_usernames']) {
if (false === $user) {
$user_id = -1;
$failed_user = sanitize_text_field($identifier);
} else {
$user_id = $user->ID;
// Normalize existing users to their actual login name.
$failed_user = $user->user_login;
}
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->wpcatcha_login_fails,
array(
'user_id' => $user_id,
'login_attempt_date' => current_time('mysql'),
'login_attempt_IP' => $ip,
'failed_user' => $failed_user,
'reason' => $reason,
)
);
}
}
static function lockDown($username = "", $reason = "")
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
$ip = WPCaptcha_Utility::getUserIP();
$identifier = sanitize_text_field($username);
$user = get_user_by('login', $identifier);
if (false === $user && is_email($identifier)) {
$user = get_user_by('email', $identifier);
}
if ($user || 1 == $options['lockout_invalid_usernames']) {
if ($user === false) {
$user_id = -1;
} else {
$user_id = $user->ID;
}
// phpcs:ignore db call warning as we are using a custom table
$wpdb->insert( // phpcs:ignore
$wpdb->wpcatcha_accesslocks,
array(
'user_id' => $user_id,
'accesslock_date' => current_time('mysql'),
'release_date' => gmdate('Y-m-d H:i:s', strtotime(current_time('mysql')) + $options['lockout_length'] * 60),
'accesslock_IP' => $ip,
'reason' => $reason
)
);
}
}
static function isLockedDown()
{
global $wpdb;
$ip = WPCaptcha_Utility::getUserIP();
// phpcs:ignore db call warning as we are using a custom table
$stillLocked = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM " . $wpdb->wpcatcha_accesslocks . " WHERE release_date > %s AND accesslock_IP = %s AND unlocked = 0", array(current_time('mysql'), $ip))); // phpcs:ignore
if($stillLocked > 0){
return true;
} else {
return false;
}
return $stillLocked;
}
static function is_rest_request()
{
if (function_exists('wp_is_serving_rest_request')) {
return wp_is_serving_rest_request();
}
// Compatibility with WordPress < 6.5.
return defined('REST_REQUEST') && REST_REQUEST;
}
static function wp_authenticate_lock($user, $username, $password)
{
$options = WPCaptcha_Setup::get_options();
if (self::isLockedDown()) {
self::accesslock_screen($options['block_message']);
return new WP_Error('lockdown_fail_count', __("ERROR: We're sorry, but this IP has been blocked due to too many recent failed login attempts.
Please try again later.", 'login-lockdown'));
}
return $user;
}
static function wp_authenticate_user($user, $password)
{
if (is_wp_error($user)) {
return $user;
}
if (!is_a($user, 'WP_User')) {
return $user;
}
$options = WPCaptcha_Setup::get_options();
if ($options['login_protection'] && self::isLockedDown()) {
self::accesslock_screen($options['block_message']);
return new WP_Error('wpcaptcha_fail_count', __("ERROR: We're sorry, but this IP has been blocked due to too many recent failed login attempts.
Please try again later.", 'advanced-google-recaptcha'));
}
if (0 !== intval($user->user_status)) {
return new WP_Error('incorrect_password', __('ERROR: Inactive account', 'advanced-google-recaptcha'));
}
if (self::is_rest_request()) {
return $user;
}
if ($options['captcha_show_login']) {
$captcha = self::handle_captcha();
if (is_wp_error($captcha)) {
return $captcha;
}
}
return $user;
}
static function handle_captcha()
{
// no need for nonce check here, added phpcs:ignore to captcha $_POST variables
$options = WPCaptcha_Setup::get_options();
if ($options['captcha'] == 'recaptchav2') {
if (!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) { // phpcs:ignore
return new WP_Error('wpcaptcha_recaptchav2_not_submitted', __("ERROR: reCAPTCHA verification failed.
";
esc_html_e('Login form protected by', 'advanced-google-recaptcha');
echo " WP Captcha
";
echo '';
}
}
static function captcha_fields_print()
{
//phpcs:ignore this just prints the recaptcha HTML inline and all variables are already escaped
echo self::captcha_fields(false); //phpcs:ignore
}
static function captcha_fields($output = false)
{
$options = WPCaptcha_Setup::get_options();
if (false === $output) {
$output = '';
}
if ($options['captcha'] == 'recaptchav2') {
$output .= '';
if (class_exists('woocommerce')) {
$output .= '';
}
} else if ($options['captcha'] == 'recaptchav3') {
$output .= '';
$output .= '';
if (class_exists('woocommerce')) {
$output .= '';
}
} else if ($options['captcha'] == 'builtin') {
$output .= ' ';
}
return $output;
}
static function login_enqueue_scripts()
{
$options = WPCaptcha_Setup::get_options();
if ($options['captcha'] == 'recaptchav2') {
wp_enqueue_script('wpcaptcha-recaptcha', 'https://www.google.com/recaptcha/api.js', array('jquery'), self::$version, true);
} else if ($options['captcha'] == 'recaptchav3') {
wp_enqueue_script('wpcaptcha-recaptcha', 'https://www.google.com/recaptcha/api.js?onload=wpcaptchaRefreshToken&render=' . esc_html($options['captcha_site_key']), array('jquery'), self::$version, true);
}
}
static function login_scripts_print()
{
//phpcs:ignore this just prints the recaptcha scripts inline as they can be used in various forms where enqueing normally is not always working
echo self::login_scripts(false); //phpcs:ignore
}
static function login_scripts($output = false)
{
$options = WPCaptcha_Setup::get_options();
if (false === $output) {
$output = '';
}
// scripts might need to be printed in odd contexts so wp_enqueue_script is not always ideal
if ($options['captcha'] == 'recaptchav2') {
$output .= ""; // phpcs:ignore
} else if ($options['captcha'] == 'recaptchav3') {
$output .= ""; // phpcs:ignore
}
return $output;
}
static function accesslock_screen($block_message = false)
{
$main_color = '#4285f4';
$secondary_color = '#8eb8ff';
echo '
';
echo '