namespace WPForms\Admin\Settings\Captcha;
* Base captcha settings class.
* Saved CAPTCHA settings.
* List of required static properties.
private $required_static_properties = [
$this->settings = wp_parse_args( wpforms_get_captcha_settings(), [ 'provider' => 'none' ] );
foreach ( $this->required_static_properties as $property ) {
if ( empty( static::${$property} ) ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
'The $%s static property is required for a %s class',
* Array of captcha settings fields.
abstract public function get_settings_fields();
* Get API request url for the captcha preview.
public function get_api_url() {
$url = add_query_arg( $this->get_api_url_query_arg(), $url );
* @param string $url API URL.
* @param array $settings Captcha settings array.
return apply_filters( 'wpforms_admin_settings_captcha_get_api_url', $url, $this->settings );
* Enqueue assets for the CAPTCHA settings page.
public function enqueues() {
* Allow/disallow to enquire captcha settings.
* @param boolean $allow True/false. Default: false.
$disable_enqueues = apply_filters( 'wpforms_admin_settings_captcha_enqueues_disable', false );
if ( $disable_enqueues || ! $this->is_captcha_preview_ready() ) {
$api_url = $this->get_api_url();
$provider_name = $this->settings['provider'];
$handle = "wpforms-settings-{$provider_name}";
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_script( $handle, $api_url, [ 'jquery' ], null, true );
wp_add_inline_script( $handle, $this->get_inline_script() );
* Inline script for initialize captcha JS code.
protected function get_inline_script() {
return /** @lang JavaScript */
'var wpformsSettingsCaptchaLoad = function() {
jQuery( ".wpforms-captcha" ).each( function( index, el ) {
var widgetID = ' . static::$api_var . '.render( el );
jQuery( el ).attr( "data-captcha-id", widgetID );
jQuery( document ).trigger( "wpformsSettingsCaptchaLoaded" );
* Check if CAPTCHA config is ready to display a preview.
public function is_captcha_preview_ready() {
( $this->settings['provider'] === static::$slug || ( $this->settings['provider'] === 'recaptcha' && $this->settings['recaptcha_type'] === 'v2' ) ) &&
! empty( $this->settings['site_key'] ) &&
! empty( $this->settings['secret_key'] )
* Retrieve query arguments for the CAPTCHA API URL.
protected function get_api_url_query_arg() {
* Modify captcha api url parameters.
* @param array $params Array of parameters.
* @param array $params Saved CAPTCHA settings.
return (array) apply_filters(
'wpforms_admin_settings_captcha_get_api_url_query_arg',
'onload' => 'wpformsSettingsCaptchaLoad',
public function get_field_desc() {
$content = wpforms_render( 'admin/settings/' . static::$slug . '-description' );
return wpforms_render( 'admin/settings/specific-note', [ 'content' => $content ], true );