-
Notifications
You must be signed in to change notification settings - Fork 8
Fix. Integrations. GiveWP now has bot detector scripts in iframes. #790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: fix
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,99 @@ | ||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| namespace Cleantalk\Antispam\ScriptsIntegration; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| class CleantalkScriptsIntegrator | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * List of plugins selected for inline script integration, | ||||||||||||||||||||||||||||||||||||||||
| * indexed by their corresponding WordPress hook name. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @var ScriptIntegrationPlugin[] | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| public $plugins_loaded = []; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * Executes the integration process. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * This method: | ||||||||||||||||||||||||||||||||||||||||
| * - Retrieves available integrations | ||||||||||||||||||||||||||||||||||||||||
| * - Filters plugins that should be loaded | ||||||||||||||||||||||||||||||||||||||||
| * - Registers WordPress hooks to execute plugin integration logic | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @return void | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| public function run() | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| $integrations = $this->getIntegrations(); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $this->plugins_loaded = !empty($integrations) | ||||||||||||||||||||||||||||||||||||||||
| ? $this->getPluginsToInline($integrations) | ||||||||||||||||||||||||||||||||||||||||
| : []; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (!empty($this->plugins_loaded)) { | ||||||||||||||||||||||||||||||||||||||||
| foreach ($this->plugins_loaded as $_hook => $plugin) { | ||||||||||||||||||||||||||||||||||||||||
| if ($plugin instanceof ScriptIntegrationPlugin) { | ||||||||||||||||||||||||||||||||||||||||
| add_action($_hook, function () use ($plugin) { | ||||||||||||||||||||||||||||||||||||||||
| $plugin->integrate(); | ||||||||||||||||||||||||||||||||||||||||
| }, 100); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * Filters plugins that are eligible for inline script integration. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * A plugin is included only if: | ||||||||||||||||||||||||||||||||||||||||
| * - It is active | ||||||||||||||||||||||||||||||||||||||||
| * - It matches the current URI context | ||||||||||||||||||||||||||||||||||||||||
| * - It passes additional runtime checks | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * Each hook can only be assigned to one plugin (first match wins). | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @param ScriptIntegrationPlugin[] $integrations List of available plugin integrations. | ||||||||||||||||||||||||||||||||||||||||
| * @return ScriptIntegrationPlugin[] Filtered plugins indexed by hook name. | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| public function getPluginsToInline($integrations) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| $plugins_loaded = []; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| foreach ($integrations as $plugin) { | ||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||
| $plugin->is_plugin_active && | ||||||||||||||||||||||||||||||||||||||||
| $plugin->is_in_uri && | ||||||||||||||||||||||||||||||||||||||||
| $plugin->additional_checks_passed | ||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||
| if (!isset($plugins_loaded[$plugin->hook_name])) { | ||||||||||||||||||||||||||||||||||||||||
| $plugins_loaded[$plugin->hook_name] = $plugin; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return $plugins_loaded; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * Returns a list of all available plugin integrations. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * Each integration defines: | ||||||||||||||||||||||||||||||||||||||||
| * - Activation rules | ||||||||||||||||||||||||||||||||||||||||
| * - Context conditions (URI, environment, etc.) | ||||||||||||||||||||||||||||||||||||||||
| * - Hook target for script injection | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @return ScriptIntegrationPlugin[] | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| public function getIntegrations() | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| $integrations = [ | ||||||||||||||||||||||||||||||||||||||||
| new GiveWPScript(), | ||||||||||||||||||||||||||||||||||||||||
| new FluentFormScript(), | ||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||
| } catch (\Exception $e) { | ||||||||||||||||||||||||||||||||||||||||
| $integrations = []; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+88
to
+96
|
||||||||||||||||||||||||||||||||||||||||
| try { | |
| $integrations = [ | |
| new GiveWPScript(), | |
| new FluentFormScript(), | |
| ]; | |
| } catch (\Exception $e) { | |
| $integrations = []; | |
| } | |
| $integrations = []; | |
| try { | |
| $integrations[] = new GiveWPScript(); | |
| } catch (\Throwable $e) { | |
| } | |
| try { | |
| $integrations[] = new FluentFormScript(); | |
| } catch (\Throwable $e) { | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| namespace Cleantalk\Antispam\ScriptsIntegration; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class FluentFormScript extends ScriptIntegrationPlugin | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| public $hook_name = 'wp_head'; | ||||||||||||||||||||||||||||||||||||||||||
| public $plugin_file = 'fluentformpro/fluentformpro.php'; | ||||||||||||||||||||||||||||||||||||||||||
| public $uri_chunk = 'ff_landing='; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public function integrate() | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| echo '<script data-pagespeed-no-defer="" src="' | ||||||||||||||||||||||||||||||||||||||||||
| . APBCT_URL_PATH | ||||||||||||||||||||||||||||||||||||||||||
| . '/js/apbct-public-bundle.min.js' | ||||||||||||||||||||||||||||||||||||||||||
| . '?ver=' . APBCT_VERSION . '" id="ct_public_functions-js"></script>'; | ||||||||||||||||||||||||||||||||||||||||||
| echo '<script src="' . APBCT_BOT_DETECTOR_SCRIPT_URL . '?ver=' | ||||||||||||||||||||||||||||||||||||||||||
| . APBCT_VERSION . '" async id="ct_bot_detector-js" data-wp-strategy="async"></script>'; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public function additionalChecks() | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| return $this->is_in_uri || ( | ||||||||||||||||||||||||||||||||||||||||||
| function_exists('apbct_is_user_logged_in') && | ||||||||||||||||||||||||||||||||||||||||||
| apbct_is_user_logged_in() && | ||||||||||||||||||||||||||||||||||||||||||
| (defined('APBCT_FF_JS_SCRIPTS_LOAD') && APBCT_FF_JS_SCRIPTS_LOAD == true) | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+27
|
||||||||||||||||||||||||||||||||||||||||||
| public function additionalChecks() | |
| { | |
| return $this->is_in_uri || ( | |
| function_exists('apbct_is_user_logged_in') && | |
| apbct_is_user_logged_in() && | |
| (defined('APBCT_FF_JS_SCRIPTS_LOAD') && APBCT_FF_JS_SCRIPTS_LOAD == true) | |
| ); | |
| public function isInUri() | |
| { | |
| return parent::isInUri() || ( | |
| function_exists('apbct_is_user_logged_in') && | |
| apbct_is_user_logged_in() && | |
| defined('APBCT_FF_JS_SCRIPTS_LOAD') && | |
| APBCT_FF_JS_SCRIPTS_LOAD == true | |
| ); | |
| } | |
| public function additionalChecks() | |
| { | |
| return false; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace Cleantalk\Antispam\ScriptsIntegration; | ||
|
|
||
| class GiveWPScript extends ScriptIntegrationPlugin | ||
| { | ||
| public $hook_name = 'givewp_donation_form_enqueue_scripts'; | ||
| public $plugin_file = 'give/give.php'; | ||
| public $uri_chunk = 'givewp-route=donation-form-view'; | ||
|
|
||
| /** | ||
| * @return void | ||
| * @psalm-suppress InvalidArgument | ||
| */ | ||
| public function integrate() | ||
| { | ||
| // Bot detector | ||
| if ( apbct__is_bot_detector_enabled() && ! apbct_bot_detector_scripts_exclusion()) { | ||
| // Attention! Skip old enqueue way for external script. | ||
| wp_enqueue_script( | ||
| 'ct_bot_detector', | ||
| APBCT_BOT_DETECTOR_SCRIPT_URL, | ||
| [], | ||
| APBCT_VERSION, | ||
| array( | ||
| 'in_footer' => true, | ||
| 'strategy' => 'async' | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Cleantalk\Antispam\ScriptsIntegration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract class ScriptIntegrationPlugin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public $is_plugin_active; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public $is_in_uri; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public $additional_checks_passed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public $plugin_file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public $uri_chunk; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public $hook_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function __construct() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !isset($this->plugin_file) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !is_string($this->plugin_file) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !isset($this->uri_chunk) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !is_string($this->uri_chunk) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !isset($this->hook_name) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !is_string($this->hook_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new \Exception('Plugin file, URI chunk and hook name must be set'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+27
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | |
| !isset($this->plugin_file) || | |
| !is_string($this->plugin_file) || | |
| !isset($this->uri_chunk) || | |
| !is_string($this->uri_chunk) || | |
| !isset($this->hook_name) || | |
| !is_string($this->hook_name) | |
| ) { | |
| throw new \Exception('Plugin file, URI chunk and hook name must be set'); | |
| } | |
| $invalid_fields = array(); | |
| if (!isset($this->plugin_file) || !is_string($this->plugin_file)) { | |
| $invalid_fields[] = 'plugin_file'; | |
| } | |
| if (!isset($this->uri_chunk) || !is_string($this->uri_chunk)) { | |
| $invalid_fields[] = 'uri_chunk'; | |
| } | |
| if (!isset($this->hook_name) || !is_string($this->hook_name)) { | |
| $invalid_fields[] = 'hook_name'; | |
| } | |
| if (!empty($invalid_fields)) { | |
| throw new \InvalidArgumentException( | |
| sprintf( | |
| 'Invalid script integration configuration for %s: missing or invalid field(s): %s', | |
| static::class, | |
| implode(', ', $invalid_fields) | |
| ) | |
| ); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getPluginsToInline()requiresis_in_uriANDadditional_checks_passed, which prevents integrations from supporting “URI match OR alternative condition” (e.g., the FluentForm case that previously allowed loading for logged-in users viaAPBCT_FF_JS_SCRIPTS_LOAD). IfadditionalChecks()is meant to extend/override the URI gating, update the condition to use( $plugin->is_in_uri || $plugin->additional_checks_passed )and adjust the baseScriptIntegrationPlugin::additionalChecks()default accordingly (e.g., returnfalse) so scripts don’t load everywhere by default.