Skip to content

Commit

Permalink
#20 Formidable (#121)
Browse files Browse the repository at this point in the history
* #20 Formidable: Add original code from @slimco into modules subfolder

Source: slimco/formidable-field-frcaptcha@ebd0971

Permission was granted here: slimco/formidable-field-frcaptcha#2 (comment)

* #20 Formidable: Integrate

* Remove custom style
* Add own configuration option for Formidable
* Remove outdated readme
  • Loading branch information
amenk committed Apr 19, 2024
1 parent 2de92c1 commit fcbb19a
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 7 deletions.
23 changes: 16 additions & 7 deletions friendly-captcha/includes/core.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

/* Main entry point */
/* Main entry point */

// TODO: is this necessary? It breaks intellisense..
// if ( !class_exists( 'FriendlyCaptcha_Plugin' ) ) {
// if ( !class_exists( 'FriendlyCaptcha_Plugin' ) ) {
class FriendlyCaptcha_Plugin {

/**
Expand Down Expand Up @@ -32,6 +32,7 @@ class FriendlyCaptcha_Plugin {
public static $option_elementor_forms_integration_active_name = "frcaptcha_elementor_integration_active";
public static $option_html_forms_integration_active_name = "frcaptcha_html_forms_integration_active";
public static $option_forminator_integration_active_name = "frcaptcha_forminator_integration_active";
public static $option_formidable_integration_active_name = "frcaptcha_formidable_integration_active";
public static $option_avada_forms_integration_active_name = "frcaptcha_avada_forms_integration_active";

public static $option_wp_register_integration_active_name = "frcaptcha_wp_register_integration_active";
Expand All @@ -44,15 +45,15 @@ class FriendlyCaptcha_Plugin {
public static $option_wc_login_integration_active_name = "frcaptcha_wc_login_integration_active";
public static $option_wc_lost_password_integration_active_name = "frcaptcha_wc_lost_password_integration_active";
public static $option_wc_checkout_integration_active_name = "frcaptcha_wc_checkout_integration_active";

public static $option_um_login_integration_active_name = "frcaptcha_um_login_integration_active";
public static $option_um_register_integration_active_name = "frcaptcha_um_register_integration_active";
public static $option_um_reset_password_integration_active_name = "frcaptcha_um_reset_password_integration_active";

public static $option_wpum_registration_integration_active_name = "frcaptcha_wpum_registration_integration_active";
public static $option_wpum_login_integration_active_name = "frcaptcha_wpum_login_integration_active";
public static $option_wpum_password_recovery_integration_active_name = "frcaptcha_wpum_password_recovery_integration_active";

public static $option_pb_login_integration_active_name = "frcaptcha_pb_login_integration_active";
public static $option_pb_register_integration_active_name = "frcaptcha_pb_register_integration_active";
public static $option_pb_reset_password_integration_active_name = "frcaptcha_pb_reset_password_integration_active";
Expand All @@ -62,7 +63,7 @@ class FriendlyCaptcha_Plugin {

public static $option_global_puzzle_endpoint_active_name = "frcaptcha_global_endpoint_active";
public static $option_eu_puzzle_endpoint_active_name = "frcaptcha_eu_endpoint_active";


public function init() {
if ( defined( 'FRIENDLY_CAPTCHA_VERSION' ) ) {
Expand Down Expand Up @@ -140,7 +141,11 @@ public function get_html_forms_active() {
public function get_forminator_active() {
return get_option(FriendlyCaptcha_Plugin::$option_forminator_integration_active_name) == 1;
}


public function get_formidable_active() {
return get_option(FriendlyCaptcha_Plugin::$option_formidable_integration_active_name) == 1;
}

public function get_avada_forms_active() {
return get_option(FriendlyCaptcha_Plugin::$option_avada_forms_integration_active_name) == 1;
}
Expand Down Expand Up @@ -258,7 +263,7 @@ public function get_global_puzzle_endpoint_active() {
// These only contain pure functions
require plugin_dir_path( __FILE__ ) . 'helpers.php';
require plugin_dir_path( __FILE__ ) . 'verification.php';

// Register widget routines
require plugin_dir_path( __FILE__ ) . '../public/widgets.php';

Expand Down Expand Up @@ -298,6 +303,10 @@ public function get_global_puzzle_endpoint_active() {
require plugin_dir_path( __FILE__ ) . '../modules/forminator/forminator.php';
}

if (FriendlyCaptcha_Plugin::$instance->get_formidable_active()) {
require plugin_dir_path( __FILE__ ) . '../modules/formidable/formidable.php';
}

if (FriendlyCaptcha_Plugin::$instance->get_avada_forms_active()) {
require plugin_dir_path( __FILE__ ) . '../modules/avada-forms/avada-forms.php';
}
Expand Down
16 changes: 16 additions & 0 deletions friendly-captcha/includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function frcaptcha_settings_init() {
FriendlyCaptcha_Plugin::$option_group,
FriendlyCaptcha_Plugin::$option_forminator_integration_active_name
);
register_setting(
FriendlyCaptcha_Plugin::$option_group,
FriendlyCaptcha_Plugin::$option_formidable_integration_active_name
);
register_setting(
FriendlyCaptcha_Plugin::$option_group,
FriendlyCaptcha_Plugin::$option_avada_forms_integration_active_name
Expand Down Expand Up @@ -308,6 +312,18 @@ function frcaptcha_settings_init() {
)
);

add_settings_field(
'frcaptcha_settings_formidable_integration_field',
'Formidable', 'frcaptcha_settings_field_callback',
'friendly_captcha_admin',
'frcaptcha_integrations_settings_section',
array(
"option_name" => FriendlyCaptcha_Plugin::$option_formidable_integration_active_name,
"description" => "Enable Friendly Captcha for <a href=\"https://wordpress.org/plugins/formidable/\" target=\"_blank\">Formidable</a>.<br /><strong>Important:</strong> Make sure to add the new Friendly Captcha field to your forms.",
"type" => "checkbox"
)
);

add_settings_field(
'frcaptcha_settings_avada_forms_integration_field',
'Avada Form Builder', 'frcaptcha_settings_field_callback',
Expand Down
122 changes: 122 additions & 0 deletions friendly-captcha/modules/formidable/FrcaptchaFieldNewType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

class FrcaptchaFieldNewType extends FrmFieldType {

/**
* @var string
*/
protected $type = 'frcaptcha';

/**
* Set to false if a normal input field should not be displayed.
* @var bool
*/
protected $has_input = true;

/**
* Which Formidable settings should be hidden or displayed?
*/
protected function field_settings_for_type() {
$settings = parent::field_settings_for_type();
$settings['default'] = true;

return $settings;
}

/**
* Need custom options too? Add them here or remove this function.
*/
protected function extra_field_opts() {
return array(
// name => default,
);
}

protected function include_form_builder_file() {
return dirname( __FILE__ ) . '/builder-field.php';
}

/**
* Get the type of field being displayed. This is required to add a settings
* section just for this field. show_extra_field_choices will not be triggered
* without it.
*
* @return array
*/
public function displayed_field_type( $field ) {
return array(
$this->type => true,
);
}

/**
* Add settings in the builder here.
*/
public function show_extra_field_choices( $args ) {
$field = $args['field'];
include( dirname( __FILE__ ) . '/builder-settings.php' );
}

protected function html5_input_type() {
return 'text';
}

/**
* @return array|null If there is an error, return an array.
*/
public function validate( $args ) {

$plugin = FriendlyCaptcha_Plugin::$instance;
if (!$plugin->is_configured() or !$plugin->get_formidable_active()) {
return array( 'field' . $args['id'] => __('FriendlyCaptcha_Plugin is missing') );
}

$errorPrefix = '<strong>' . __( 'Error', 'wp-captcha' ) . '</strong> : ';
$solution = frcaptcha_get_sanitized_frcaptcha_solution_from_post();

$errors = array();

if ( empty( $solution ) ) {
$errors[ 'field' . $args['id'] ] = $errorPrefix . FriendlyCaptcha_Plugin::default_error_user_message();
}

$verification = frcaptcha_verify_captcha_solution($solution, $plugin->get_sitekey(), $plugin->get_api_key());

if (!$verification["success"]) {
$errors[ 'field' . $args['id'] ] = $errorPrefix . FriendlyCaptcha_Plugin::default_error_user_message();
}

return $errors;

}

/**
* If the saved value will be different from the submitted value,
* alter it here.
*/
public function get_value_to_save( $value, $atts ) {
// Make changes to $value or remove this function.
return $value;
}

/**
* Customize the way the value is displayed in emails and views.
*
* @return string
*/
protected function prepare_display_value( $value, $atts ) {
// Make changes to $value here or remove this function.
return $value;
}

/**
* @return string Whatever shows in the front end goes here.
*/
public function front_field_input( $args, $shortcode_atts ) {
ob_start();
include( dirname( __FILE__ ) . '/front-end-field.php' );
$input_html = ob_get_contents();
ob_end_clean();
return $input_html;
}
}
1 change: 1 addition & 0 deletions friendly-captcha/modules/formidable/builder-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Friendly Captcha is load only on frontend.</p>
1 change: 1 addition & 0 deletions friendly-captcha/modules/formidable/builder-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Settings is in the plugin Friendly Captcha</p>
59 changes: 59 additions & 0 deletions friendly-captcha/modules/formidable/formidable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/*
Plugin Name: Formidable Forms Field Friendly Captcha
Description: Friendly Captcha field type in Formidable Forms.
Version: 1.0
Plugin URI: https://formidableforms.com/
Author URI: https://netsuccess.sk/
Author: SlimCo
*/


// TODO: replace 'frmfrcaptcha_' and 'Frcaptcha' with your own.
// TODO: replace 'new-type' with the slug for your field.

/**
* Add the autoloader.
*/
function frmfrcaptcha_load_formidable_field() {
spl_autoload_register( 'frmfrcaptcha_forms_autoloader' );
}
add_action( 'plugins_loaded', 'frmfrcaptcha_load_formidable_field' );

/**
* @since 3.0
*/
function frmfrcaptcha_forms_autoloader( $class_name ) {
// Only load Frcaptcha classes here
if ( ! preg_match( '/^Frcaptcha.+$/', $class_name ) ) {
return;
}

$filepath = dirname( __FILE__ );
$filepath .= '/' . $class_name . '.php';

if ( file_exists( $filepath ) ) {
require( $filepath );
}
}

/**
* Tell Formidable where to find the new field type.
* @return string The name of the new class that extends FrmFieldType.
*/
function frmfrcaptcha_get_field_type_class( $class, $field_type ) {
if ( $field_type === 'frcaptcha' ) { // Replace 'frcaptcha' with your field slug.
$class = 'FrcaptchaFieldNewType'; // Set your class name here.
}
return $class;
}
add_filter( 'frm_get_field_type_class', 'frmfrcaptcha_get_field_type_class', 10, 2 );

function frmfrcaptcha_add_new_field( $fields ) {
$fields['frcaptcha'] = array(
'name' => 'Friendly Captcha',
'icon' => 'frm_icon_font frm_pencil_icon', // Set the class for a custom icon here.
);
return $fields;
}
add_filter( 'frm_available_fields', 'frmfrcaptcha_add_new_field' );
13 changes: 13 additions & 0 deletions friendly-captcha/modules/formidable/front-end-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}

$plugin = FriendlyCaptcha_Plugin::$instance;
if (!$plugin->is_configured() or !$plugin->get_formidable_active()) {
return;
}

echo frcaptcha_generate_widget_tag_from_plugin($plugin);

frcaptcha_enqueue_widget_scripts();

0 comments on commit fcbb19a

Please sign in to comment.