Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions friendly-captcha/includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ class FriendlyCaptcha_Plugin
"plugins" => array("wp-job-openings/wp-job-openings.php", "pro-pack-for-wp-job-openings/pro-pack.php"),
"settings_description" => "Enable Friendly Captcha for the <a href=\"https://wordpress.org/plugins/wp-job-openings/\" target=\"_blank\">WP Job Openings</a> application form.",
),
array(
"name" => "Generic Integration (for custom and unsupported plugins)",
"slug" => "generic_integration",
"entry" => "generic_integration/generic_integration.php",
"plugins" => [],
"settings_description" => "Enable Friendly Captcha for integrations with user-defined PHP-Code. (not recommended)",
),
);

public function init()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* FriendlyCaptcha Helper to integrate better with custom code.
*/

/** Add captcha to forms
* @param string $html html to append the captcha widget to
* @return string html with captcha widget appended
*/
add_filter("frc_captcha_append_widget", function ($html) {
$plugin = FriendlyCaptcha_Plugin::$instance;
if (!$plugin->is_configured()) {
return $html;
}

frcaptcha_enqueue_widget_scripts();

$widget = frcaptcha_generate_widget_tag_from_plugin($plugin);
return $html . $widget;
});

/** Validate captcha on form submission
* @param string $solution value of $_POST['frc-captcha-solution']
* @param bool $lax_on_failure how to decide on network failure: returning false here means a broken network failure or integration in settings is deactivated is treated as a bot.
* @return bool true = human, false = bot / missing solution
*/
add_filter(
"frc_captcha_validation",
function ($solution, $lax_on_failure) {
$plugin = FriendlyCaptcha_Plugin::$instance;

if (!$plugin->is_configured()) {
return $lax_on_failure;
}

if (empty($solution)) {
return false;
}

$verification = frcaptcha_verify_captcha_solution(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess other plugins could also just call frcaptcha_verify_captcha_solution directly right? I'm not a Wordpress expert so I wonder what the benefit of the additional filter is 🤔

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter name(s) must correspond to the filter name(s) in the FC module file. So in theory, it should be possible to have a generic module with filter names that the actual WP forms use.
However, I remember that I first tried to use the html-forms module for my purposes but it didn't work (may be I did a mistake).
Second:
All supported form plugins are listed in the core.php file like this:

array(
            "name" => "HTML Forms",
            "slug" => 'html_forms',
            "entry" => "html-forms/html-forms.php",
            "plugins" => array("html-forms/html-forms.php"),
            "settings_description" => "Enable Friendly Captcha for <a href=\"https://wordpress.org/plugins/html-forms/\" target=\"_blank\">HTML Forms</a>.",
        ),

That means there are hardcoded plugin names and paths, anyway (entry, plugins). So it makes no difference if we have custom filter names or not (I guess) since every single form plugin that we want to support needs it's own module file.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wanted to keep it simple and version agnostic to implement.

If there is something additional to be handled in the future, it can just be added into the filter without affecting anything else by changing frcaptcha_verify_captcha_solution.

$solution,
$plugin->get_sitekey(),
$plugin->get_api_key(),
"generic_integration",
);

return $verification["success"];
},
10,
2,
);