'alphagf_fields', 'text' => $this->get_form_editor_field_title(), ); } public function get_form_editor_field_icon() { return 'gform-icon alphagf_icon icon-math-captcha'; } public function get_field_input($form, $value = array(), $entry = null) { if ($this->is_form_editor()) { return $this->get_editor_field_content(); } $form_id = absint($form['id']); $is_entry_detail = $this->is_entry_detail(); $is_form_editor = $this->is_form_editor(); $id = (int) $this->id; $field_id = ($is_entry_detail || $is_form_editor || $form_id === 0) ? 'input_' . $id : 'input_' . $form_id . '_' . $id; $tab_index = GFCommon::get_tabindex(); $placeholder_attribute = $this->get_field_placeholder_attribute(); $invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"'; $captcha = ALPHAGF_Math_Captcha::get_instance(); $posted_token = (string) rgpost('input_' . $id . '_hash'); $posted_data = $captcha->parse_token($posted_token); // On multi-page forms the field is re-rendered after it has been solved: keep the same question, // otherwise the answer carried over from the earlier page would no longer match on the final page. if (! $this->failed_validation && $posted_data && $captcha->verify_answer($posted_token, $value)) { $token = $posted_token; $question = $posted_data['q']; } else { $captcha_problem = $captcha->generate_math_problem('easy', array('addition', 'subtraction')); $token = $captcha_problem['token']; $question = $captcha_problem['question']; $value = ''; } $captcha_image = $captcha->generate_svg_captcha($question); $html = '
'; $html .= '
'; $html .= sprintf( '%s', esc_attr($captcha_image), esc_html__('Math Captcha', 'alpha-addons-for-gravity-forms') ); $html .= ''; $html .= sprintf( '', esc_attr('input_' . $id . '_hash'), esc_attr('input_' . $form_id . '_' . $id . '_hash'), esc_attr($token) ); $html .= '
'; $html .= sprintf( '', esc_attr('input_' . $id), esc_attr($field_id), esc_attr($value), $invalid_attribute, $placeholder_attribute, $tab_index ); $html .= '
'; return $html; } public function validate($value, $form) { $token = (string) rgpost("input_{$this->id}_hash"); if (! ALPHAGF_Math_Captcha::get_instance()->verify_answer($token, is_scalar($value) ? $value : '')) { $this->failed_validation = true; $this->validation_message = $this->errorMessage ? esc_html($this->errorMessage) : esc_html__('Math Captcha verification failed. Please try again.', 'alpha-addons-for-gravity-forms'); } } public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead) { return ''; } private function get_editor_field_content() { $preview = 'math-captcha.svg'; return sprintf("", ALPHAGF_ASSETS . "/images/{$preview}"); } /** * 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; } public function allow_populate_parameter() { return false; } public function allow_html() { return false; } } GF_Fields::register(new ALPHAGF_Field_Math_Captcha());