From def47ed38f3fa7701f0479e8e314d061b7a751be Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Fri, 13 Oct 2023 22:21:31 +0200 Subject: [PATCH] psr autoload --- ...{admin.php => CF7_AntiSpam_Admin_Core.php} | 7 +- ... => CF7_AntiSpam_Admin_Customizations.php} | 10 ++- ...lay.php => CF7_AntiSpam_Admin_Display.php} | 5 ++ ...tools.php => CF7_AntiSpam_Admin_Tools.php} | 8 ++ cf7-antispam.php | 42 ++++------ composer.json | 19 ++++- .../cf7a-core.php => core/CF7_AntiSpam.php | 78 ++++++++++--------- .../CF7_AntiSpam_Activator.php | 3 + .../CF7_AntiSpam_B8.php | 3 + .../CF7_AntiSpam_Deactivator.php | 3 + .../CF7_AntiSpam_Filters.php | 35 ++++++--- .../CF7_AntiSpam_Flamingo.php | 3 + .../CF7_AntiSpam_Frontend.php | 3 + .../CF7_AntiSpam_Loader.php | 3 + .../CF7_AntiSpam_Uninstaller.php | 3 + .../CF7_AntiSpam_i18n.php | 3 + .../CF7_Antispam_Geoip.php | 3 + .../service.php => core/WPCF7_Service.php | 31 ++++---- .../cf7a-functions.php => core/functions.php | 1 + {includes => core}/index.php | 0 {includes => core}/src/script.js | 0 21 files changed, 169 insertions(+), 94 deletions(-) rename admin/{admin.php => CF7_AntiSpam_Admin_Core.php} (98%) rename admin/{admin-customizations.php => CF7_AntiSpam_Admin_Customizations.php} (99%) rename admin/{admin-display.php => CF7_AntiSpam_Admin_Display.php} (99%) rename admin/{admin-tools.php => CF7_AntiSpam_Admin_Tools.php} (97%) rename includes/cf7a-core.php => core/CF7_AntiSpam.php (88%) rename includes/cf7a-activator.php => core/CF7_AntiSpam_Activator.php (99%) rename includes/cf7a-antispam-b8.php => core/CF7_AntiSpam_B8.php (99%) rename includes/cf7a-deactivator.php => core/CF7_AntiSpam_Deactivator.php (95%) rename includes/cf7a-antispam-filters.php => core/CF7_AntiSpam_Filters.php (97%) rename includes/cf7a-antispam-flamingo.php => core/CF7_AntiSpam_Flamingo.php (99%) rename includes/cf7a-frontend.php => core/CF7_AntiSpam_Frontend.php (99%) rename includes/cf7a-loader.php => core/CF7_AntiSpam_Loader.php (99%) rename includes/cf7a-uninstall.php => core/CF7_AntiSpam_Uninstaller.php (99%) rename includes/cf7a-i18n.php => core/CF7_AntiSpam_i18n.php (95%) rename includes/cf7a-antispam-geoip.php => core/CF7_Antispam_Geoip.php (99%) rename includes/service.php => core/WPCF7_Service.php (91%) rename includes/cf7a-functions.php => core/functions.php (99%) rename {includes => core}/index.php (100%) rename {includes => core}/src/script.js (100%) diff --git a/admin/admin.php b/admin/CF7_AntiSpam_Admin_Core.php similarity index 98% rename from admin/admin.php rename to admin/CF7_AntiSpam_Admin_Core.php index 9683a86..2044d5b 100644 --- a/admin/admin.php +++ b/admin/CF7_AntiSpam_Admin_Core.php @@ -1,4 +1,9 @@ options; diff --git a/admin/admin-display.php b/admin/CF7_AntiSpam_Admin_Display.php similarity index 99% rename from admin/admin-display.php rename to admin/CF7_AntiSpam_Admin_Display.php index c8a5d7a..5ff90b5 100644 --- a/admin/admin-display.php +++ b/admin/CF7_AntiSpam_Admin_Display.php @@ -1,4 +1,9 @@ add_service( 'cf7-antispam', - WPCF7_Antispam::get_instance() + \CF7_AntiSpam\Core\WPCF7_Antispam::get_instance() ); - + } add_action( 'wpcf7_init', 'cf7_antispam_register_service', 1, 0 ); - - + function run_cf7a() { $enabled = get_option( 'cf7a_options' ); - if ( $enabled && $enabled['cf7a_enable'] === true ) { + if ( $enabled && ! empty( $enabled['cf7a_enable'] ) ) { $cf7a = new CF7_AntiSpam(); $cf7a->run(); } - } add_action( 'init', 'run_cf7a', 11, 0 ); - - - - diff --git a/composer.json b/composer.json index dc21038..eef7429 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,9 @@ "wp-coding-standards/wpcs": "^2.3", "automattic/vipwpcs": "^2.3", "phpcompatibility/phpcompatibility-wp": "*", - "phpunit/phpunit": "^8|^9", + "phpunit/phpunit": "^9.6.13", "wp-phpunit/wp-phpunit": "^5.9.5", - "yoast/phpunit-polyfills": "^1.0", + "yoast/phpunit-polyfills": "^1.1", "dealerdirect/phpcodesniffer-composer-installer": "^0.7" }, "require": { @@ -30,7 +30,7 @@ }, "scripts": { "PHPCS": "vendor/bin/phpcbf --standard=phpcs.ruleset.xml -s --report=full,summary,source", - "test": "vendor/bin/phpunit -c phpunit.xml.dist --verbose", + "test": "vendor/bin/phpunit -c phpunit.xml.dist", "test-multisite": "WP_MULTISITE=1 vendor/bin/phpunit -c tests/multisite.xml --verbose" }, "minimum-stability": "dev", @@ -55,5 +55,16 @@ "dealerdirect/phpcodesniffer-composer-installer": true, "phpstan/extension-installer": true } - } + }, + "autoload": { + "psr-4": { + "CF7_AntiSpam\\Core\\": "core/", + "CF7_AntiSpam\\Admin\\": "admin/" + } + }, + "autoload-dev": { + "psr-4": { + "CF7_AntiSpam\\Tests\\": "tests/" + } + } } diff --git a/includes/cf7a-core.php b/core/CF7_AntiSpam.php similarity index 88% rename from includes/cf7a-core.php rename to core/CF7_AntiSpam.php index db1da0a..e13b785 100644 --- a/includes/cf7a-core.php +++ b/core/CF7_AntiSpam.php @@ -1,4 +1,7 @@ */ +namespace CF7_AntiSpam\Core; + +use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Core; +use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Tools; + /** * It sets the version, plugin name, and options. It loads * the dependencies, sets the locale, updates the plugin, and loads the admin and frontend areas @@ -128,40 +136,40 @@ protected function update() { */ private function load_dependencies() { - /** - * The class responsible for orchestrating the actions and filters of the - * core plugin. - */ - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-loader.php'; - - /** - * The class responsible for defining internationalization functionality - * of the plugin. - */ - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-i18n.php'; - - /** - * The class responsible for defining frontend functionality - * of the plugin. - */ - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-frontend.php'; - - /** - * The classes responsible for defining antispam functionality and the related filters - * of the plugin. - */ - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-filters.php'; - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-geoip.php'; - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-flamingo.php'; - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-b8.php'; - - /** - * The classes responsible for defining admin backend functionality - */ - require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-customizations.php'; - require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-display.php'; - require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-tools.php'; - require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin.php'; +// /** +// * The class responsible for orchestrating the actions and filters of the +// * core plugin. +// */ +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-loader.php'; +// +// /** +// * The class responsible for defining internationalization functionality +// * of the plugin. +// */ +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-i18n.php'; +// +// /** +// * The class responsible for defining frontend functionality +// * of the plugin. +// */ +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-frontend.php'; +// +// /** +// * The classes responsible for defining antispam functionality and the related filters +// * of the plugin. +// */ +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-filters.php'; +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-geoip.php'; +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-flamingo.php'; +// require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-antispam-b8.php'; +// +// /** +// * The classes responsible for defining admin backend functionality +// */ +// require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-customizations.php'; +// require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-display.php'; +// require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-tools.php'; +// require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin.php'; $this->loader = new CF7_AntiSpam_Loader(); } @@ -236,7 +244,7 @@ private function load_admin() { add_action( 'admin_init', array( $tools, 'cf7a_handle_actions' ), 1 ); /* the admin area */ - $plugin_admin = new CF7_AntiSpam_Admin( $this->get_plugin_name(), $this->get_version() ); + $plugin_admin = new CF7_AntiSpam_Admin_Core( $this->get_plugin_name(), $this->get_version() ); /* add the admin menu */ $this->loader->add_action( 'admin_menu', $plugin_admin, 'cf7a_admin_menu' ); diff --git a/includes/cf7a-activator.php b/core/CF7_AntiSpam_Activator.php similarity index 99% rename from includes/cf7a-activator.php rename to core/CF7_AntiSpam_Activator.php index 01427fd..b3511b2 100644 --- a/includes/cf7a-activator.php +++ b/core/CF7_AntiSpam_Activator.php @@ -1,4 +1,7 @@ */ +namespace CF7_AntiSpam\Core; + /** * A class that is used to filter out spam. */ @@ -266,6 +271,18 @@ public function cf7a_check_language_allowed( $languages, $disalloweds = array(), return true; } + public function scan_email_tags( $fields ) { + $validEmails = array(); + + foreach ( $fields as $value ) { + if ( filter_var( $value, FILTER_VALIDATE_EMAIL ) ) { + $validEmails[] = sanitize_email( $value ); + } + } + + return $validEmails; + } + /** * CF7_AntiSpam_Filters The antispam filter @@ -294,7 +311,7 @@ public function cf7a_spam_filter( $spam ) { /* get the sender email field using the flamingo defined */ $email_tag = sanitize_title( cf7a_get_mail_meta( $contact_form->pref( 'flamingo_email' ) ) ); - $email = isset( $posted_data[ $email_tag ] ) ? $posted_data[ $email_tag ] : false; + $emails = isset( $posted_data[ $email_tag ] ) ? array($posted_data[ $email_tag ]) : $this->scan_email_tags($mail_tags); /* Getting the message field(s) from the form. */ $message_tag = sanitize_text_field( $contact_form->pref( 'flamingo_message' ) ); @@ -771,14 +788,14 @@ public function cf7a_spam_filter( $spam ) { * because it is an attempt to circumvent the controls, because the e-mail client cannot blacklist the e-mail itself, * we must prevent this. */ - if ( intval( $options['check_bad_email_strings'] ) === 1 && $email ) { + if ( intval( $options['check_bad_email_strings'] ) === 1 && !empty( $emails ) ) { - foreach ( $bad_email_strings as $bad_email_string ) { - - if ( false !== stripos( strtolower( $email ), strtolower( $bad_email_string ) ) ) { - - $spam_score += $score_bad_string; - $reason['email_blackilisted'][] = $bad_email_string; + foreach ($emails as $email) { + foreach ($bad_email_strings as $bad_email_string) { + if (false !== stripos(strtolower($email), strtolower($bad_email_string))) { + $spam_score += $score_bad_string; + $reason['email_blacklisted'][] = $bad_email_string; + } } } @@ -786,7 +803,7 @@ public function cf7a_spam_filter( $spam ) { $reason['email_blackilisted'] = implode( ',', $reason['email_blackilisted'] ); - cf7a_log( "The ip address $remote_ip sent a mail using the email address {$email} that contains the bad string {$reason['email_blackilisted']}", 1 ); + cf7a_log( "The ip address $remote_ip sent a mail using the email address {$reason['email_blackilisted']} that contains the bad string {$reason['email_blackilisted']}", 1 ); } } diff --git a/includes/cf7a-antispam-flamingo.php b/core/CF7_AntiSpam_Flamingo.php similarity index 99% rename from includes/cf7a-antispam-flamingo.php rename to core/CF7_AntiSpam_Flamingo.php index 0c17d74..7b817a1 100644 --- a/includes/cf7a-antispam-flamingo.php +++ b/core/CF7_AntiSpam_Flamingo.php @@ -1,4 +1,7 @@ options = CF7_AntiSpam::get_options(); if ( isset( $_POST['cf7a_submit'] ) ) { - $this->options['cf7a_enable'] = ! $this->options['cf7a_enable']; + $this->options['cf7a_enable'] = empty($this->options['cf7a_enable']) ? true : ! $this->options['cf7a_enable']; CF7_AntiSpam::update_plugin_options( $this->options ); echo '

Settings saved.

'; - } + } /** * Call the options otherwise the plugin will break in integration */ @@ -71,8 +74,8 @@ public function get_title() { * * @return bool value of the 'enabled' key in the array. */ - public function is_active() { - return $this->options['cf7a_enable']; + public function is_active() { + return isset( $this->options['cf7a_enable'] ) ? $this->options['cf7a_enable'] : false; } /** @@ -149,7 +152,7 @@ public function admin_notice( $message = '' ) { * The function `menu_page_url` generates a URL for a specific menu page with additional query * parameters. * - * @param args The `` parameter is an optional array that allows you to add additional query + * @param string $args The `` parameter is an optional array that allows you to add additional query * parameters to the URL. These query parameters can be used to pass data or settings to the page that * the URL points to. * @@ -241,20 +244,18 @@ public function display( $action = '' ) { esc_html( __( 'CF7-Antispam is active on this site.', 'contact-form-7' ) ) ); } - + // Get the current checkbox status from the options - $checked = $this->options['cf7a_enable'] ? 'Disable' : 'Enable'; + $checked = ! empty( $this->options['cf7a_enable'] ) ? 'Disable' : 'Enable'; // Display the form echo '
'; echo '

Checkbox Settings

'; echo '
'; echo ''; - if ( $this->options['cf7a_enable'] === true ) { - echo 'Settings Page'; - } + if ( ! empty( $this->options['cf7a_enable'] ) ) { + echo 'Settings Page'; + } echo '
'; echo '
'; - } - } diff --git a/includes/cf7a-functions.php b/core/functions.php similarity index 99% rename from includes/cf7a-functions.php rename to core/functions.php index cccbd55..64301b4 100644 --- a/includes/cf7a-functions.php +++ b/core/functions.php @@ -1,4 +1,5 @@