-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #32 Support Divi Contact Forms * #32 Divi: Improve and use existing mutation-observer script * Add option to frcaptcha_enqueue_widget_scripts to force-add mutation observer when needed * Make mutation-observer script more bulletproof: * Ensure to load after main scripts, so the friendly captcha functions are always available * Load after DOMload * Also make async/defer like the main script * Remove custom script.js * #32 Divi: Fix Notice in Admin * #32 Init Mutationobserver without waiting for scripts
- Loading branch information
Showing
7 changed files
with
236 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
add_filter('et_core_get_third_party_components', array('frcaptcha_divi_load_addon', 'register')); | ||
add_filter('et_core_api_spam_enabled_providers', array('frcaptcha_divi_load_addon', 'enable')); | ||
add_filter('option_et_core_api_spam_options', array('frcaptcha_divi_load_addon', 'injectAccount')); | ||
add_action('init', array('frcaptcha_divi_load_addon', 'fakeToken')); | ||
add_filter('do_shortcode_tag', array('frcaptcha_divi_load_addon', 'addWidget'), 10, 2); | ||
|
||
class frcaptcha_divi_load_addon | ||
{ | ||
const SUPPORTED_SHORTCODES = ['et_pb_contact_form']; | ||
|
||
const INSERT_BEFORE = '<button type="submit"'; | ||
|
||
public static function register($instances = [], $group = '') | ||
{ | ||
if ($group !== 'api/spam' && $group !== '') { | ||
return $instances; | ||
} | ||
|
||
require_once 'frcaptcha_divi_core_addon.php'; | ||
|
||
$instances['frcaptcha'] = new frcaptcha_divi_core_addon(); | ||
|
||
return $instances; | ||
} | ||
|
||
public static function enable($_names_by_slug) { | ||
$plugin = FriendlyCaptcha_Plugin::$instance; | ||
|
||
if (!$plugin->is_configured() or !$plugin->get_divi_active()) { | ||
return; | ||
} | ||
|
||
$_names_by_slug['third-party']['frcaptcha'] = 'FriendlyCaptcha verification'; | ||
return $_names_by_slug; | ||
} | ||
|
||
public static function injectAccount($value, $option = 'et_core_api_spam_options') | ||
{ | ||
$plugin = FriendlyCaptcha_Plugin::$instance; | ||
|
||
if (!$plugin->is_configured() or !$plugin->get_divi_active()) { | ||
return; | ||
} | ||
|
||
$value = maybe_unserialize($value); | ||
$value['accounts']['frcaptcha']['default'] = [ | ||
'site_key' => 'default', | ||
'secret_key' => 'default' | ||
]; | ||
|
||
return $value; | ||
|
||
} | ||
|
||
public static function addWidget($output, $tag = null) | ||
{ | ||
if (!in_array($tag, self::SUPPORTED_SHORTCODES)) { | ||
return $output; | ||
} | ||
|
||
$plugin = FriendlyCaptcha_Plugin::$instance; | ||
|
||
if (!$plugin->is_configured() or !$plugin->get_divi_active()) { | ||
return; | ||
} | ||
|
||
$output = str_replace(self::INSERT_BEFORE, | ||
frcaptcha_generate_widget_tag_from_plugin(FriendlyCaptcha_Plugin::$instance) . self::INSERT_BEFORE, | ||
$output | ||
); | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* Fake Token in POST | ||
* | ||
* @see \ET_Builder_Module_Type_WithSpamProtection::is_spam_submission | ||
*/ | ||
public static function fakeToken() | ||
{ | ||
if (isset($_POST['frc-captcha-solution'])) { | ||
$_POST['token'] = $_POST['frc-captcha-solution']; | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
friendly-captcha/modules/divi/frcaptcha_divi_core_addon.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
class frcaptcha_divi_core_addon extends ET_Core_API_Spam_Provider { | ||
public $name = 'FriendlyCaptcha'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public $slug = 'frcaptcha'; | ||
|
||
public $custom_fields = null; // avoid notice from \ET_Core_API_Email_Providers::_initialize which expects this field | ||
|
||
public function __construct( $owner = 'frcaptcha', $account_name = '', $api_key = '' ) { | ||
parent::__construct( $owner, $account_name, $api_key ); | ||
|
||
$this->_add_actions_and_filters(); | ||
} | ||
|
||
protected function _add_actions_and_filters() { | ||
if ( ! is_admin() && ! et_core_is_fb_enabled() ) { | ||
add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ) ); | ||
} | ||
} | ||
|
||
public function action_wp_enqueue_scripts() { | ||
$plugin = FriendlyCaptcha_Plugin::$instance; | ||
|
||
if ( !$plugin->is_configured() ) { | ||
return; | ||
} | ||
|
||
if ( ! $this->is_enabled() ) { | ||
return; | ||
} | ||
|
||
frcaptcha_enqueue_widget_scripts(true); | ||
|
||
wp_dequeue_script('et-core-api-spam-recaptcha'); | ||
} | ||
|
||
public function is_enabled() { | ||
$has_frcaptcha_module = true; | ||
|
||
if ( class_exists( 'ET_Dynamic_Assets' ) ) { | ||
$et_dynamic_module_framework = et_builder_dynamic_module_framework(); | ||
$is_dynamic_framework_enabled = et_builder_is_frontend() && 'on' === $et_dynamic_module_framework; | ||
$is_dynamic_css_enabled = et_builder_is_frontend() && et_use_dynamic_css(); | ||
|
||
if ( $is_dynamic_framework_enabled && $is_dynamic_css_enabled ) { | ||
$et_dynamic_assets = ET_Dynamic_Assets::init(); | ||
$saved_shortcodes = $et_dynamic_assets->get_saved_page_shortcodes(); | ||
$frcaptcha_modules = array( 'et_pb_contact_form', 'et_pb_signup' ); | ||
$has_frcaptcha_module = ! empty( array_intersect( $saved_shortcodes, $frcaptcha_modules ) ); | ||
} | ||
} | ||
|
||
return $has_frcaptcha_module; | ||
} | ||
|
||
public function verify_form_submission() { | ||
$plugin = FriendlyCaptcha_Plugin::$instance; | ||
|
||
if ( !$plugin->is_configured() ) { | ||
return array( | ||
'success' => true, | ||
'score' => 100000, | ||
); | ||
} | ||
|
||
if ( ! $this->is_enabled() ) { | ||
return array( | ||
'success' => true, | ||
'score' => 100000, | ||
); | ||
} | ||
|
||
$solution = et_()->array_get_sanitized( $_POST, 'token' ); | ||
|
||
$plugin = FriendlyCaptcha_Plugin::$instance; | ||
$verification = frcaptcha_verify_captcha_solution($solution, $plugin->get_sitekey(), $plugin->get_api_key()); | ||
|
||
if ( $verification["success"] ) { | ||
return array( | ||
'success' => true, | ||
'score' => 100000, | ||
); | ||
} else { | ||
return 'Captcha error'; | ||
} | ||
} | ||
|
||
public function get_account_fields() { | ||
return array(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php // Silence is golden. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters