Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions inc/cleantalk-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,48 @@ function apbct_exclusions_check__url()
return false;
}

/**
* Whether public JS/CSS assets are allowed on the current page.
* Full mode — always. Lite mode — only if REQUEST_URI matches listed pages (substring or regexp).
*
* @return bool
*/
function apbct_is_assets_allowed_on_current_page()
{
global $apbct;

// Full mode (0) or unset
if ( empty($apbct->settings['data__protection_mode']) ) {
return true;
}

if ( empty($apbct->settings['data__protection_mode__urls']) ) {
return false;
}

if ( strpos($apbct->settings['data__protection_mode__urls'], "\r\n") !== false ) {
$patterns = explode("\r\n", $apbct->settings['data__protection_mode__urls']);
} elseif ( strpos($apbct->settings['data__protection_mode__urls'], "\n") !== false ) {
$patterns = explode("\n", $apbct->settings['data__protection_mode__urls']);
} else {
$patterns = explode(',', $apbct->settings['data__protection_mode__urls']);
}

$url_haystack = TT::toString(Server::getString('REQUEST_URI'));

foreach ( $patterns as $pattern ) {
$pattern = trim($pattern);
if ( $pattern === '' ) {
continue;
}
if ( @preg_match('@' . $pattern . '@', $url_haystack) === 1 || stripos($url_haystack, $pattern) !== false ) {
return true;
}
Comment thread
svfcode marked this conversation as resolved.
Outdated
}

return false;
}

/**
* Check POST array for the exclusion form signs. Listen for array keys or for value in case if key is "action".
* @param array $form_data The POST array or another filtered array of form data.
Expand Down
10 changes: 7 additions & 3 deletions inc/cleantalk-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function apbct_init()
}

// Localize data
if ( ! apbct_exclusions_check__url() ) {
if ( ! apbct_exclusions_check__url() && apbct_is_assets_allowed_on_current_page() ) {
if (defined('CLEANTALK_PLACE_PUBLIC_JS_SCRIPTS_IN_FOOTER') && CLEANTALK_PLACE_PUBLIC_JS_SCRIPTS_IN_FOOTER) {
add_action('wp_footer', array(LocalizeHandler::class, 'handle'), 1);
add_action('login_footer', array(LocalizeHandler::class, 'handle'), 1);
Expand Down Expand Up @@ -1138,6 +1138,10 @@ function apbct_login__scripts()
{
global $apbct;

if ( ! apbct_is_assets_allowed_on_current_page() ) {
return;
}

apbct_enqueue_and_localize_public_scripts();

$apbct->public_script_loaded = true;
Expand Down Expand Up @@ -1205,7 +1209,7 @@ function ct_enqueue_scripts_public($_hook)
{
global $current_user, $apbct;

if ( apbct_exclusions_check__url() || apbct_is_amp_request() ) {
if ( apbct_exclusions_check__url() || apbct_is_amp_request() || ! apbct_is_assets_allowed_on_current_page() ) {
return;
}

Expand All @@ -1232,7 +1236,7 @@ function ct_enqueue_styles_public()
{
global $apbct, $current_user;

if ( apbct_exclusions_check__url() ) {
if ( apbct_exclusions_check__url() || ! apbct_is_assets_allowed_on_current_page() ) {
return;
}

Expand Down
32 changes: 32 additions & 0 deletions inc/cleantalk-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,28 @@ function apbct_settings__set_fields()
'title' => __('Data Processing', 'cleantalk-spam-protect'),
'section' => 'hidden_section',
'fields' => array(
'data__protection_mode' => array(
'title' => __('Protection mode', 'cleantalk-spam-protect'),
'description' => __(
'Full — load Anti-Spam scripts and styles on all pages. Lite — load them only on the pages listed below. Regular expressions are allowed.',
'cleantalk-spam-protect'
),
'options' => array(
array('val' => 0, 'label' => __('Full', 'cleantalk-spam-protect'), 'childrens_enable' => 0,),
array('val' => 1, 'label' => __('Lite', 'cleantalk-spam-protect'), 'childrens_enable' => 1,),
),
'childrens' => array('data__protection_mode__urls'),
),
'data__protection_mode__urls' => array(
'type' => 'textarea',
'title' => __('Pages to protect (Lite mode)', 'cleantalk-spam-protect'),
'description' => __(
'List pages where Anti-Spam assets should be loaded. One value per line or comma-separated. Plain URL parts and regular expressions are allowed. Example: /contact, /checkout, /wp-login\\.php',
'cleantalk-spam-protect'
),
'parent' => 'data__protection_mode',
'class' => 'apbct_settings-field_wrapper--sub',
),
'data__protect_logged_in' => array(
'title' => __("Protect logged in Users", 'cleantalk-spam-protect'),
'description' => __(
Expand Down Expand Up @@ -1795,6 +1817,7 @@ function apbct_settings__error__output($return = false)
'settings_validate' => 'Validate Settings',
'exclusions_urls' => 'URL Exclusions',
'exclusions_fields' => 'Field Exclusions',
'protection_mode_urls' => 'Pages to protect (Lite mode)',

// Unknown
'unknown' => __('Unknown error type: ', 'cleantalk-spam-protect'),
Expand Down Expand Up @@ -2369,6 +2392,7 @@ function apbct_settings__validate($incoming_settings)
'data__email_decoder_obfuscation_mode',
'data__email_decoder_obfuscation_custom_text',
'data__email_decoder_buffer',
'data__protection_mode__urls',
);
$incoming_settings = apbct_settings__keep_settings_state_values(
$incoming_settings,
Expand Down Expand Up @@ -2448,6 +2472,14 @@ function apbct_settings__validate($incoming_settings)
} // Make HTML code inactive
}

// Sanitize Protection mode pages (Lite) — plain URL parts and regexps both allowed
$result = apbct_settings__sanitize__exclusions(
isset($incoming_settings['data__protection_mode__urls']) ? $incoming_settings['data__protection_mode__urls'] : '',
false
);
$incoming_settings['data__protection_mode__urls'] = $result ? $result : '';
$apbct->errorDelete('protection_mode_urls', true, 'settings_validate');
Comment thread
svfcode marked this conversation as resolved.
Outdated

// Validate Exclusions
// URLs
$is_exclusions_url_like = apbct_settings__sanitize__exclusions(
Expand Down
Loading
Loading