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
28 changes: 20 additions & 8 deletions cleantalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@
die('Not allowed!');
}

global $apbct, $wpdb, $pagenow;
/**
* @var State $apbct
*/
global $apbct;
/**
* @var \wpdb $wpdb
*/
global $wpdb;
/**
* @var mixed|string $pagenow
*/
global $pagenow;

$cleantalk_executed = false;

Expand Down Expand Up @@ -125,6 +136,7 @@
}
define('APBCT_AGENT', 'wordpress-' . $plugin_version__agent); // Prepared agent

//todo make this as $apbct->service_constants
if ( defined('CLEANTALK_SERVER') ) {
define('APBCT_MODERATE_URL', 'https://moderate.' . CLEANTALK_SERVER);
if ( ! defined('CLEANTALK_API_URL') ) {
Expand Down Expand Up @@ -253,7 +265,7 @@ function apbct_register_my_rest_routes()
// Database prefix
global $wpdb, $wp_version;
$apbct->db_prefix = ! APBCT_WPMS || $apbct->allow_custom_key || $apbct->white_label ? $wpdb->prefix : $wpdb->base_prefix;
$apbct->db_prefix = ! $apbct->white_label && defined('CLEANTALK_ACCESS_KEY') ? $wpdb->base_prefix : $wpdb->prefix;
$apbct->db_prefix = ! $apbct->white_label && $apbct->constants->self_owned_access_key->isDefinedAndTypeOK() ? $wpdb->base_prefix : $wpdb->prefix;

/** @todo HARDCODE FIX */
if ( $apbct->plugin_version === '1.0.0' ) {
Expand Down Expand Up @@ -1209,8 +1221,8 @@ function apbct_sfw_update__switch_to_direct()

$apbct->fw_stats['reason_direct_update_log'] = null;

if (defined('APBCT_SFW_FORCE_DIRECT_UPDATE')) {
$apbct->fw_stats['reason_direct_update_log'] = 'const APBCT_SFW_FORCE_DIRECT_UPDATE exists';
if ($apbct->constants->sfw_force_direct_update->isDefined()) {
$apbct->fw_stats['reason_direct_update_log'] = 'constant exists';
return true;
}

Expand Down Expand Up @@ -2771,15 +2783,15 @@ function ct_account_status_check($api_key = null, $process_errors = true)
: 0;

//todo:temporary solution for description, until we found the way to transfer this from cloud
if (defined('APBCT_WHITELABEL_PLUGIN_DESCRIPTION')) {
if ($apbct->constants->whitelabel_plugin_description->isDefinedAndTypeOK()) {
/** @psalm-suppress PossiblyInvalidArrayAssignment */
$result['wl_antispam_description'] = APBCT_WHITELABEL_PLUGIN_DESCRIPTION;
$result['wl_antispam_description'] = esc_html($apbct->constants->whitelabel_plugin_description->getValue());
}

//todo:temporary solution for FAQ
if (defined('APBCT_WHITELABEL_FAQ_LINK')) {
if ($apbct->constants->whitelabel_faq_link->isDefinedAndTypeOK()) {
/** @psalm-suppress PossiblyInvalidArrayAssignment */
$result['wl_faq_url'] = APBCT_WHITELABEL_FAQ_LINK;
$result['wl_faq_url'] = esc_url($apbct->constants->whitelabel_faq_link->getValue());
}

if ( isset($result['wl_status']) && $result['wl_status'] === 'ON' ) {
Expand Down
4 changes: 2 additions & 2 deletions inc/cleantalk-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function apbct_base_call($params = array(), $reg_flag = false)
* @since 6.58.99
*/
if (
$apbct->service_constants->disable_empty_email_exception->isDefined() &&
$apbct->constants->disable_empty_email_exception->isDefined() &&
!$apbct->settings['data__general_postdata_test']
) {
$params['exception_action'] = 0;
Expand Down Expand Up @@ -1841,7 +1841,7 @@ function apbct__bot_detector_get_fd_log()
);
// Initialize result array with default values

if (defined('APBCT_DO_NOT_COLLECT_FRONTEND_DATA_LOGS')) {
if ($apbct->constants->do_not_collect_frontend_data_logs->isDefined()) {
$result['plugin_status'] = 'OK';
$result['error_msg'] = 'bot detector logs collection is disabled via constant definition';
return json_encode($result);
Expand Down
6 changes: 3 additions & 3 deletions inc/cleantalk-pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2001,10 +2001,10 @@ function apbct_settings__get_ajax_type()
global $apbct;

//force ajax route type if constant is defined and compatible
if ($apbct->service_constants->set_ajax_route_type->isDefined()
&& in_array($apbct->service_constants->set_ajax_route_type->getValue(), array('rest','admin_ajax'))
if ($apbct->constants->set_ajax_route_type->isDefinedAndTypeOK()
&& in_array($apbct->constants->set_ajax_route_type->getValue(), array('rest','admin_ajax'))
) {
return $apbct->service_constants->set_ajax_route_type->getValue();
return $apbct->constants->set_ajax_route_type->getValue();
}

// Check rest availability
Expand Down
12 changes: 6 additions & 6 deletions inc/cleantalk-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function apbct_init()

// Localize data
if ( ! apbct_exclusions_check__url() ) {
if (defined('CLEANTALK_PLACE_PUBLIC_JS_SCRIPTS_IN_FOOTER') && CLEANTALK_PLACE_PUBLIC_JS_SCRIPTS_IN_FOOTER) {
if ($apbct->constants->place_public_js_scripts_in_footer->isDefined()) {
add_action('wp_footer', array(LocalizeHandler::class, 'handle'), 1);
add_action('login_footer', array(LocalizeHandler::class, 'handle'), 1);
} else {
Expand Down Expand Up @@ -771,7 +771,7 @@ function apbct_comment__wordpress__show_blacklists($notify_message, $comment_id)
*/
function ct_die($_comment_id, $_comment_status)
{
global $ct_comment, $ct_jp_comments;
global $ct_comment, $ct_jp_comments, $apbct;

// JCH Optimize caching preventing
add_filter('jch_optimize_page_cache_set_caching', static function ($_is_cache_active) {
Expand All @@ -781,7 +781,7 @@ function ct_die($_comment_id, $_comment_status)
do_action('apbct_pre_block_page', $ct_comment);

$message_title = __('Spam protection', 'cleantalk-spam-protect');
if ( defined('CLEANTALK_DISABLE_BLOCKING_TITLE') && CLEANTALK_DISABLE_BLOCKING_TITLE != true ) {
if ( ! $apbct->constants->disable_blocking_title->isDefined() ) {
$message_title = '<b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> ' . $message_title;
}
if ( Post::get('et_pb_contact_email') ) {
Expand Down Expand Up @@ -826,15 +826,15 @@ function ct_die($_comment_id, $_comment_status)
*/
function ct_die_extended($comment_body)
{
global $ct_jp_comments;
global $ct_jp_comments, $apbct;

// JCH Optimize caching preventing
add_filter('jch_optimize_page_cache_set_caching', static function ($_is_cache_active) {
return false;
}, 999, 1);

$message_title = __('Spam protection', 'cleantalk-spam-protect');
if ( defined('CLEANTALK_DISABLE_BLOCKING_TITLE') && CLEANTALK_DISABLE_BLOCKING_TITLE != true ) {
if ( !$apbct->constants->disable_blocking_title->isDefined() ) {
$message_title = '<b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> ' . $message_title;
}

Expand Down Expand Up @@ -1229,7 +1229,7 @@ function apbct_enqueue_and_localize_public_scripts()
{
global $apbct;

$in_footer = defined('CLEANTALK_PLACE_PUBLIC_JS_SCRIPTS_IN_FOOTER') && CLEANTALK_PLACE_PUBLIC_JS_SCRIPTS_IN_FOOTER;
$in_footer = $apbct->constants->place_public_js_scripts_in_footer->isDefined();
// Different JS params
$bundle_name = ApbctJsBundleResolver::getBundleName($apbct->settings);
ApbctEnqueue::getInstance()->js($bundle_name, array(), $in_footer);
Expand Down
19 changes: 14 additions & 5 deletions inc/cleantalk-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ function apbct_settings__set_fields()
'title' => __("Don't check trusted user's comments", 'cleantalk-spam-protect'),
'description' => sprintf(
__("Don't check comments for users with above %d comments.", 'cleantalk-spam-protect'),
defined('CLEANTALK_CHECK_COMMENTS_NUMBER') ? CLEANTALK_CHECK_COMMENTS_NUMBER : 3
$apbct->constants->skip_on_approved_comments_number->isDefinedAndTypeOK()
? $apbct->constants->skip_on_approved_comments_number->getValue()
: 3
),
),
'data__use_ajax' => array(
Expand Down Expand Up @@ -1139,7 +1141,7 @@ function apbct_settings__set_fields__network($fields)
'</a>'
),
'childrens' => array('multisite__white_label__plugin_name'),
'disabled' => defined('CLEANTALK_ACCESS_KEY') ||
'disabled' => $apbct->constants->self_owned_access_key->isDefinedAndTypeOK() ||
! isset($apbct->network_settings['multisite__work_mode']) ||
$apbct->network_settings['multisite__work_mode'] != 1,
'parent' => 'multisite__work_mode',
Expand Down Expand Up @@ -1605,7 +1607,7 @@ function apbct_settings__error__output($return = false)

$out = '';

if ( ! empty($apbct->errors) && ! defined('CLEANTALK_ACCESS_KEY') ) {
if ( ! empty($apbct->errors) && ! $apbct->constants->self_owned_access_key->isDefinedAndTypeOK() ) {
$errors = $apbct->errors;

$error_texts = array(
Expand Down Expand Up @@ -1900,7 +1902,10 @@ function apbct_settings__field__apikey()

$template = @file_get_contents(CLEANTALK_PLUGIN_DIR . 'templates/settings/settings_key_wrapper.html');

$define_key_is_provided_by_admin = APBCT_WPMS && ! is_main_site() && ( ! $apbct->allow_custom_key || defined('CLEANTALK_ACCESS_KEY'));
$define_key_is_provided_by_admin = APBCT_WPMS && ! is_main_site() && (
! $apbct->allow_custom_key ||
$apbct->constants->self_owned_access_key->isDefinedAndTypeOK()
);
$define_show_key_field = ! (apbct_api_key__is_correct($apbct->api_key) && isset($apbct->data["key_changed"]) && $apbct->data["key_changed"]);
$define_show_deobfuscating_href = apbct_api_key__is_correct($apbct->api_key) && $apbct->key_is_ok && (!isset($apbct->data["key_changed"]) || !$apbct->data["key_changed"]);

Expand Down Expand Up @@ -2231,8 +2236,12 @@ function apbct_settings__validate($settings)

$apbct->data['key_changed'] = $settings['apikey'] !== $apbct->settings['apikey'];

$predefined_key = $apbct->constants->self_owned_access_key->isDefinedAndTypeOK()
? $apbct->constants->self_owned_access_key->getValue()
: false;

$settings['apikey'] = ! empty($settings['apikey']) ? trim($settings['apikey']) : '';
$settings['apikey'] = defined('CLEANTALK_ACCESS_KEY') ? CLEANTALK_ACCESS_KEY : $settings['apikey'];
$settings['apikey'] = $predefined_key ? $predefined_key : $settings['apikey'];
$settings['apikey'] = ! is_main_site() && $apbct->white_label && $apbct->settings['apikey'] ? $apbct->settings['apikey'] : $settings['apikey'];
$settings['apikey'] = is_main_site() || $apbct->allow_custom_key || $apbct->white_label ? $settings['apikey'] : $apbct->network_settings['apikey'];
$settings['apikey'] = is_main_site() || ! isset($settings['multisite__white_label']) || ! $settings['multisite__white_label']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function doPrepareActions($argument)
$this->wp_comment_post_id = $comment_post_id;

$this->post_info = array();
$this->comments_check_number_needs_to_skip_request = defined('CLEANTALK_CHECK_COMMENTS_NUMBER') ? CLEANTALK_CHECK_COMMENTS_NUMBER : 3;
$this->comments_check_number_needs_to_skip_request = $apbct->constants->skip_on_approved_comments_number->isDefinedAndTypeOK()
? $apbct->constants->skip_on_approved_comments_number->getValue()
: 3;

/**
* Custom mail notifications processing
Expand Down Expand Up @@ -297,7 +299,7 @@ public function doBlock($message)
{
$ct_result = $this->base_call_result['ct_result'];

global $ct_comment, $ct_stop_words;
global $ct_comment, $ct_stop_words, $apbct;
$ct_comment = $message;
$ct_stop_words = $ct_result->stop_words;
/**
Expand All @@ -311,9 +313,11 @@ public function doBlock($message)

$err_text =
'<center>'
. ((defined('CLEANTALK_DISABLE_BLOCKING_TITLE') && CLEANTALK_DISABLE_BLOCKING_TITLE == true)
? ''
: '<b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> ')
. (
$apbct->constants->disable_blocking_title->isDefined()
? ''
: '<b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> '
)
. __('Spam protection', 'cleantalk-spam-protect')
. "</center><br><br>\n"
. $ct_result->comment;
Expand Down
4 changes: 3 additions & 1 deletion lib/Cleantalk/Antispam/IntegrationsByClass/BuddyPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public function privateMsgCheck($bp_message_obj)
}

// Check for quantity of comments
$comments_check_number = defined('CLEANTALK_CHECK_COMMENTS_NUMBER') ? CLEANTALK_CHECK_COMMENTS_NUMBER : 3;
$comments_check_number = $apbct->constants->skip_on_approved_comments_number->isDefinedAndTypeOK()
? $apbct->constants->skip_on_approved_comments_number->getValue()
: 3;

if ( $apbct->settings['comments__check_comments_number'] ) {
$args = array(
Expand Down
2 changes: 1 addition & 1 deletion lib/Cleantalk/ApbctWP/Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function activation($network_wide, $concrete_blog_id = null)
$apbct->stats['plugin']['activation__times'] += 1;
$apbct->save('stats');

if ( $network_wide && ! defined('CLEANTALK_ACCESS_KEY') ) {
if ( $network_wide && ! $apbct->constants->self_owned_access_key->isDefinedAndTypeOK() ) {
$initial_blog = get_current_blog_id();
$blogs = array_keys($wpdb->get_results('SELECT blog_id FROM ' . $wpdb->blogs, OBJECT_K));
foreach ( $blogs as $blog ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Cleantalk/ApbctWP/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function __construct()
in_array(Get::get('page'), array('cleantalk', 'ct_check_spam', 'ct_check_users'));
$this->user_token = $this->apbct->user_token ?: '';

$self_owned_key = $this->apbct->moderate_ip == 0 && ! defined('CLEANTALK_ACCESS_KEY');
$self_owned_key = $this->apbct->moderate_ip == 0 && !$this->apbct->constants->self_owned_access_key->isDefinedAndTypeOK();
$is_dashboard = is_network_admin() || is_admin();
$is_admin = current_user_can('activate_plugins');
$uid = get_current_user_id();
Expand Down
42 changes: 33 additions & 9 deletions lib/Cleantalk/ApbctWP/ApbctConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ class ApbctConstant
*/
public $description;

public function __construct(array $allowed_public_names, $description = '')
private $type;

private $defined_name = false;

public function __construct(array $allowed_public_names, $type, $description = '')
{
$this->allowed_public_names = $allowed_public_names;
$this->description = $description;
$this->type = $type;
$this->defined_name = $this->getDefinedName();
}

/**
* If defined, returns the name of the first defined constant from the allowed names. Return false if none of the constants are defined.
* @return false|string
* @return string|false
*/
public function isDefined()
private function getDefinedName()
{
foreach ($this->allowed_public_names as $name) {
if (defined($name)) {
Expand All @@ -38,17 +44,35 @@ public function isDefined()
return false;
}

/**
* If defined and type is correct.
* @return bool
*/
public function isDefinedAndTypeOK()
{
return $this->defined_name && gettype(constant($this->defined_name)) === $this->type;
}


/**
* Return the fact of definition
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public function isDefined()
{
return (bool)$this->defined_name;
}

/**
* Returns the value of the first defined constant from the allowed names. Return null if none of the constants are defined.
*
* @return string|null
* @return mixed|null
*/
public function getValue()
{
foreach ($this->allowed_public_names as $name) {
if (defined($name)) {
return (string)constant($name);
}
if ($this->defined_name) {
return constant($this->defined_name);
}
return null;
}
Expand All @@ -59,7 +83,7 @@ public function getValue()
public function getData()
{
return array(
'is_defined' => $this->isDefined(),
'is_defined' => $this->defined_name,
'value' => $this->getValue(),
'description' => $this->description,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/Cleantalk/ApbctWP/Firewall/AntiCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ private function checkExclusions()
}

// skip for RSS Feed requests
if ($apbct->service_constants->skip_anticrawler_on_rss_feed->isDefined()) {
if ($apbct->constants->skip_anticrawler_on_rss_feed->isDefined()) {
if (Server::getString('REQUEST_URI') &&
preg_match_all('/feed/i', Server::getString('REQUEST_URI'))
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function getData()
'data__cookies_type' => $apbct->data['cookies_type'],
'data__ajax_type' => $apbct->data['ajax_type'],
'data__bot_detector_enabled' => $apbct->settings['data__bot_detector_enabled'],
'data__frontend_data_log_enabled' => defined('APBCT_DO_NOT_COLLECT_FRONTEND_DATA_LOGS') ? 0 : 1,
'data__frontend_data_log_enabled' => $apbct->constants->do_not_collect_frontend_data_logs->isDefinedAndTypeOK() ? 0 : 1,
'cookiePrefix' => apbct__get_cookie_prefix(),
'wprocket_detected' => apbct_is_plugin_active('wp-rocket/wp-rocket.php'),
'host_url' => Server::get('HTTP_HOST'),
Expand Down
2 changes: 1 addition & 1 deletion lib/Cleantalk/ApbctWP/RemoteCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public static function action__debug() // phpcs:ignore PSR1.Methods.CamelCapsMet
if ($apbct->settings['data__set_cookies'] == 3 && $apbct->data['cookies_type'] === 'alternative') {
$out['alt_sessions_auto_state_reason'] = $apbct->isAltSessionsRequired(true);
}
$out['active_service_constants'] = $apbct->service_constants->getDefinitionsActive();
$out['active_service_constants'] = $apbct->constants->getDefinitionsActive();

if ( APBCT_WPMS ) {
$out['network_settings'] = $apbct->network_settings;
Expand Down
Loading