'alphagf_fields',
'text' => $this->get_form_editor_field_title(),
);
}
public function get_field_input($form, $value = array(), $entry = null) {
$response = $this->field_input_markup();
if ($this->failed_validation) {
$response .= sprintf('
%1$s
', wp_kses_post($this->validation_message));
}
return $response;
}
public function field_input_markup() {
$key = ALPHAGF_hCaptcha::get_instance()->get_plugin_setting('site_key');
$theme = $this->alphagf_hcaptcha_theme;
if (empty($theme)) {
$theme = ALPHAGF_hCaptcha::get_instance()->get_plugin_setting('theme');
}
$theme = in_array($theme, array('light', 'dark', 'auto'), true) ? $theme : 'light';
$previous_response = (string) $this->get_value_submission(array());
$has_previous_response = ! $this->failed_validation && '' !== $previous_response;
$form_id = absint($this->formId);
// Always output the widget container, rendering the actual widget is controlled by the JS side.
$div = '
';
// If the challenge was already solved add the token so in multipage forms it could be validated after submitting the last page.
if ($has_previous_response) {
$div .= ' ';
}
return sprintf("%s
", $div);
}
public function get_field_content($value, $force_frontend_label, $form) {
$form_id = $form['id'];
$admin_buttons = $this->get_admin_buttons();
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$is_admin = $is_entry_detail || $is_form_editor || (rgget('context') === 'edit' && ! empty(rgget('post_id')));
$field_label = $this->get_field_label($force_frontend_label, $value);
$field_id = $is_admin || $form_id == 0 ? "input_{$this->id}" : 'input_' . $form_id . "_{$this->id}";
$admin_hidden_markup = ($this->visibility == 'hidden') ? $this->get_hidden_admin_markup() : '';
$field_content = ! $is_admin ? '{FIELD}' : sprintf("%s%s%s %s
", $admin_buttons, $admin_hidden_markup, $field_id, esc_html($field_label), $this->get_editor_field_content());
if (! $is_admin && ! ALPHAGF_hCaptcha::get_instance()->has_valid_credentials()) {
return '';
}
return $field_content;
}
private function get_editor_field_content() {
$site_key = ALPHAGF_hCaptcha::get_instance()->get_plugin_setting('site_key');
$site_secret = ALPHAGF_hCaptcha::get_instance()->get_plugin_setting('site_secret');
$theme = ALPHAGF_hCaptcha::get_instance()->get_plugin_setting('theme');
if (empty($site_key) || empty($site_secret)) {
return '';
}
$preview = $theme == 'light' ? 'hcaptcha-light.svg' : 'hcaptcha-dark.svg';
return sprintf(" ", ALPHAGF_ASSETS . "/images/{$preview}");
}
public function validate($value, $form) {
if (! ALPHAGF_hCaptcha::get_instance()->has_valid_credentials()) {
return;
}
$has_pages = \GFCommon::has_pages($form);
$target_page = rgpost('gform_target_page_number_' . $form['id']);
if (ALPHAGF_hCaptcha::get_instance()->form_has_errors($form)) {
return;
}
if ($has_pages && (int) $target_page !== 0 && ! empty($value)) {
return;
}
if (empty($value)) {
$this->failed_validation = true;
$this->validation_message = $this->errorMessage ? esc_html($this->errorMessage) : __('Please complete the hCaptcha verification.', 'alpha-addons-for-gravity-forms');
return;
}
$challenge_result = $this->make_hcaptcha_challenge($value);
if ($challenge_result) {
return;
}
$this->failed_validation = true;
$this->validation_message = $this->errorMessage ? esc_html($this->errorMessage) : __('hCaptcha verification failed. Please try again.', 'alpha-addons-for-gravity-forms');
}
/**
* Make a POST request to perform the hcaptcha challenge.
*
* @since 1.0
*
* @param string $value The submitted field value to verify.
*
* @return bool
*/
private function make_hcaptcha_challenge($value) {
return ALPHAGF_hCaptcha::get_instance()->verify_token($value);
}
public function get_value_submission($field_values, $get_from_post_global_var = true) {
$value = rgpost('h-captcha-response');
if (empty($value)) {
return rgpost('alphagf_hcaptcha_response_' . $this->formId);
}
return $value;
}
/**
* The captcha token is single-use and meaningless after validation, so don't store it in the entry.
*/
public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead) {
return '';
}
/**
* Set some defaults for the field via JS hooks.
*
* @since 1.0
*
* @return string
*/
public function get_form_editor_inline_script_on_page_render() {
// set the default field label for the field
$script = sprintf("function SetDefaultValues_%s(field) {field.label = '%s';}", $this->type, $this->get_form_editor_field_title()) . PHP_EOL;
return $script;
}
}
GF_Fields::register(new ALPHAGF_Field_hCaptcha());