| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Setting; |
| 4 |
|
| 5 |
use Calotes\Model\Setting; |
| 6 |
|
| 7 |
class Recaptcha extends Setting { |
| 8 |
/** |
| 9 |
* Option name. |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
public $table = 'wd_recaptcha_settings'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Main switch of this function. |
| 16 |
* @var bool |
| 17 |
* @defender_property |
| 18 |
*/ |
| 19 |
public $enabled = false; |
| 20 |
/** |
| 21 |
* @var string |
| 22 |
* @defender_property |
| 23 |
* @rule required |
| 24 |
* @rule in[v2_checkbox,v2_invisible,v3_recaptcha] |
| 25 |
*/ |
| 26 |
public $active_type = 'v2_checkbox'; |
| 27 |
/** |
| 28 |
* @var array |
| 29 |
* @defender_property |
| 30 |
*/ |
| 31 |
public $data_v2_checkbox; |
| 32 |
/** |
| 33 |
* @var array |
| 34 |
* @defender_property |
| 35 |
*/ |
| 36 |
public $data_v2_invisible; |
| 37 |
/** |
| 38 |
* @var array |
| 39 |
* @defender_property |
| 40 |
*/ |
| 41 |
public $data_v3_recaptcha; |
| 42 |
/** |
| 43 |
* @var string |
| 44 |
* @defender_property |
| 45 |
* @rule required |
| 46 |
*/ |
| 47 |
public $language = ''; |
| 48 |
/** |
| 49 |
* @var string |
| 50 |
* @defender_property |
| 51 |
*/ |
| 52 |
public $message = ''; |
| 53 |
/** |
| 54 |
* @var array |
| 55 |
* @defender_property |
| 56 |
* @rule required |
| 57 |
*/ |
| 58 |
public $locations = array(); |
| 59 |
/** |
| 60 |
* @var bool |
| 61 |
* @defender_property |
| 62 |
*/ |
| 63 |
public $detect_woo = false; |
| 64 |
/** |
| 65 |
* @var array |
| 66 |
* @defender_property |
| 67 |
* @rule required |
| 68 |
*/ |
| 69 |
public $woo_checked_locations = array(); |
| 70 |
|
| 71 |
protected $rules = array( |
| 72 |
array( array( 'enabled', 'detect_woo' ), 'boolean' ), |
| 73 |
array( array( 'active_type' ), 'in', array( 'v2_checkbox', 'v2_invisible', 'v3_recaptcha' ) ), |
| 74 |
); |
| 75 |
|
| 76 |
/** |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
public function get_default_values() { |
| 80 |
|
| 81 |
return array( |
| 82 |
'message' => __( 'reCAPTCHA verification failed. Please try again.', 'wpdef' ), |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
protected function before_load() { |
| 87 |
$default_values = $this->get_default_values(); |
| 88 |
$this->message = $default_values['message']; |
| 89 |
$this->language = 'automatic'; |
| 90 |
$this->data_v2_checkbox = array( |
| 91 |
'key' => '', |
| 92 |
'secret' => '', |
| 93 |
'size' => 'normal', |
| 94 |
'style' => 'light', |
| 95 |
); |
| 96 |
$this->data_v2_invisible = array( |
| 97 |
'key' => '', |
| 98 |
'secret' => '', |
| 99 |
); |
| 100 |
$this->data_v3_recaptcha = array( |
| 101 |
'key' => '', |
| 102 |
'secret' => '', |
| 103 |
'threshold' => '0.5', |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @param string $active_type |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
private function check_recaptcha_type( $active_type ) { |
| 113 |
if ( |
| 114 |
'v2_checkbox' === $active_type |
| 115 |
&& ! empty( $this->data_v2_checkbox['key'] ) |
| 116 |
&& ! empty( $this->data_v2_checkbox['secret'] ) |
| 117 |
) { |
| 118 |
|
| 119 |
return true; |
| 120 |
} elseif ( |
| 121 |
'v2_invisible' === $active_type |
| 122 |
&& ! empty( $this->data_v2_invisible['key'] ) |
| 123 |
&& ! empty( $this->data_v2_invisible['secret'] ) |
| 124 |
) { |
| 125 |
|
| 126 |
return true; |
| 127 |
} elseif ( |
| 128 |
'v3_recaptcha' === $active_type |
| 129 |
&& ! empty( $this->data_v3_recaptcha['key'] ) && ! empty( $this->data_v3_recaptcha['secret'] ) |
| 130 |
) { |
| 131 |
|
| 132 |
return true; |
| 133 |
} else { |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public function is_active() { |
| 143 |
|
| 144 |
return apply_filters( |
| 145 |
'wd_recaptcha_enable', |
| 146 |
$this->enabled |
| 147 |
&& '' !== $this->active_type |
| 148 |
&& '' !== $this->language |
| 149 |
// For each Recaptcha type. |
| 150 |
&& $this->check_recaptcha_type( $this->active_type ) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @return bool |
| 156 |
*/ |
| 157 |
public function enable_default_locations() { |
| 158 |
return ! empty( $this->locations ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @return bool |
| 163 |
*/ |
| 164 |
public function enable_woo_locations() { |
| 165 |
return $this->detect_woo && ! empty( $this->woo_checked_locations ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param bool $is_woo_activated |
| 170 |
* |
| 171 |
* @return bool |
| 172 |
*/ |
| 173 |
public function check_woo_locations( $is_woo_activated ) { |
| 174 |
if ( ! $is_woo_activated ) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
return $this->enable_woo_locations(); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Define labels for settings key. |
| 182 |
* |
| 183 |
* @param string|null $key |
| 184 |
* |
| 185 |
* @return string|array|null |
| 186 |
*/ |
| 187 |
public function labels( $key = null ) { |
| 188 |
$labels = array( |
| 189 |
'active_type' => __( 'Configure reCaptcha', 'wpdef' ), |
| 190 |
'language' => __( 'Language', 'wpdef' ), |
| 191 |
'message' => __( 'Error Message', 'wpdef' ), |
| 192 |
'locations' => __( 'CAPTCHA Locations', 'wpdef' ), |
| 193 |
'detect_woo' => __( 'WooCommerce', 'wpdef' ), |
| 194 |
); |
| 195 |
|
| 196 |
if ( ! is_null( $key ) ) { |
| 197 |
return $labels[ $key ] ?? null; |
| 198 |
} |
| 199 |
|
| 200 |
return $labels; |
| 201 |
} |
| 202 |
|
| 203 |
public function after_validate() { |
| 204 |
if ( true === $this->detect_woo && empty( $this->woo_checked_locations ) ) { |
| 205 |
$this->errors[] = __( 'reCAPTCHA for WooCommerce is enabled, but no WooCommerce forms are selected. Please select at least one WooCommerce form location and then click Save Changes again.', 'wpdef' ); |
| 206 |
|
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|