diff --git a/.gitignore b/.gitignore index a83d957..769cbc5 100644 --- a/.gitignore +++ b/.gitignore @@ -93,5 +93,5 @@ Thumbs.db # lock files .lock - +composer.lock **/dist/ \ No newline at end of file diff --git a/admin/admin.php b/admin/CF7_AntiSpam_Admin_Core.php similarity index 97% rename from admin/admin.php rename to admin/CF7_AntiSpam_Admin_Core.php index 9683a86..abc726c 100644 --- a/admin/admin.php +++ b/admin/CF7_AntiSpam_Admin_Core.php @@ -1,4 +1,10 @@ plugin_name, CF7ANTISPAM_PLUGIN_URL . '/admin/dist/admin-scripts.js', $asset['dependencies'], $asset['version'], true ); + $asset = include CF7ANTISPAM_PLUGIN_DIR . '/build/admin-scripts.asset.php'; + wp_register_script( $this->plugin_name, CF7ANTISPAM_PLUGIN_URL . '/build/admin-scripts.js', $asset['dependencies'], $asset['version'], true ); wp_enqueue_script( $this->plugin_name ); wp_localize_script( diff --git a/admin/admin-customizations.php b/admin/CF7_AntiSpam_Admin_Customizations.php similarity index 99% rename from admin/admin-customizations.php rename to admin/CF7_AntiSpam_Admin_Customizations.php index f70c56e..5f1e8da 100644 --- a/admin/admin-customizations.php +++ b/admin/CF7_AntiSpam_Admin_Customizations.php @@ -1,4 +1,9 @@ options; diff --git a/admin/admin-display.php b/admin/CF7_AntiSpam_Admin_Display.php similarity index 98% rename from admin/admin-display.php rename to admin/CF7_AntiSpam_Admin_Display.php index c8a5d7a..8128a81 100644 --- a/admin/admin-display.php +++ b/admin/CF7_AntiSpam_Admin_Display.php @@ -1,4 +1,12 @@ 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 87% rename from includes/cf7a-core.php rename to core/CF7_AntiSpam.php index db1da0a..ac590ed 100644 --- a/includes/cf7a-core.php +++ b/core/CF7_AntiSpam.php @@ -1,4 +1,5 @@ */ +namespace CF7_AntiSpam\Core; + +use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Core; +use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Tools; + +use CF7_AntiSpam\Core\CF7_AntiSpam_i18n; +use CF7_AntiSpam\Core\CF7_AntiSpam_Loader; +use CF7_AntiSpam\Core\CF7_AntiSpam_Flamingo; +use CF7_AntiSpam\Core\CF7_AntiSpam_Frontend; /** * 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 @@ -117,7 +127,6 @@ public function __construct() { */ protected function update() { - require_once CF7ANTISPAM_PLUGIN_DIR . '/includes/cf7a-activator.php'; do_action( 'cf7a_update' ); CF7_AntiSpam_Activator::update_options(); @@ -128,40 +137,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 +245,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..2a7b134 100644 --- a/includes/cf7a-activator.php +++ b/core/CF7_AntiSpam_Activator.php @@ -1,4 +1,7 @@ */ - +use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Tools; /** * It's a class that activates the plugin. */ @@ -232,8 +235,6 @@ public static function update_options( $reset_options = false ) { cf7a_log( $new_options, 1 ); - require_once CF7ANTISPAM_PLUGIN_DIR . '/admin/admin-tools.php'; - CF7_AntiSpam_Admin_Tools::cf7a_push_notice( esc_html__( 'CF7 AntiSpam updated successful! Please flush cache to refresh hidden form data', 'cf7-antispam' ), 'success cf7-antispam' ); } diff --git a/includes/cf7a-antispam-b8.php b/core/CF7_AntiSpam_B8.php similarity index 92% rename from includes/cf7a-antispam-b8.php rename to core/CF7_AntiSpam_B8.php index 40f5fe6..a730fb7 100644 --- a/includes/cf7a-antispam-b8.php +++ b/core/CF7_AntiSpam_B8.php @@ -1,4 +1,9 @@ getMessage() ); return false; @@ -117,7 +120,7 @@ public function cf7a_b8_classify( $message, $verbose = false ) { */ public function cf7a_b8_learn_spam( $message ) { if ( ! empty( $message ) ) { - $this->b8->learn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8\b8::SPAM ); + $this->b8->learn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8::SPAM ); } } @@ -128,7 +131,7 @@ public function cf7a_b8_learn_spam( $message ) { */ public function cf7a_b8_unlearn_spam( $message ) { if ( ! empty( $message ) ) { - $this->b8->unlearn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8\b8::SPAM ); + $this->b8->unlearn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8::SPAM ); } } @@ -139,7 +142,7 @@ public function cf7a_b8_unlearn_spam( $message ) { */ public function cf7a_b8_learn_ham( $message ) { if ( ! empty( $message ) ) { - $this->b8->learn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8\b8::HAM ); + $this->b8->learn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8::HAM ); } } @@ -150,7 +153,7 @@ public function cf7a_b8_learn_ham( $message ) { */ public function cf7a_b8_unlearn_ham( $message ) { if ( ! empty( $message ) ) { - $this->b8->unlearn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8\b8::HAM ); + $this->b8->unlearn( htmlspecialchars( $message, ENT_QUOTES, get_option( 'blog_charset' ) ), b8::HAM ); } } diff --git a/includes/cf7a-deactivator.php b/core/CF7_AntiSpam_Deactivator.php similarity index 95% rename from includes/cf7a-deactivator.php rename to core/CF7_AntiSpam_Deactivator.php index cc01030..fbcb784 100644 --- a/includes/cf7a-deactivator.php +++ b/core/CF7_AntiSpam_Deactivator.php @@ -1,4 +1,7 @@ */ +namespace CF7_AntiSpam\Core; + +use Exception; +use WPCF7_Submission; +use CF7_AntiSpam\Core\CF7_Antispam_Geoip; + /** * A class that is used to filter out spam. */ @@ -266,6 +273,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 +313,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 +790,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 +805,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..136c67a 100644 --- a/includes/cf7a-antispam-flamingo.php +++ b/core/CF7_AntiSpam_Flamingo.php @@ -1,4 +1,10 @@ */ +use WP_Query; +use WPCF7_ContactForm; + /** * A class that handles front end related function */ @@ -404,8 +410,8 @@ public function enqueue_scripts() { * class. */ - $asset = include CF7ANTISPAM_PLUGIN_DIR . '/includes/dist/script.asset.php'; - wp_register_script( $this->plugin_name, CF7ANTISPAM_PLUGIN_URL . '/includes/dist/script.js', $asset['dependencies'], $asset['version'], true ); + $asset = include CF7ANTISPAM_PLUGIN_DIR . '/build/script.asset.php'; + wp_register_script( $this->plugin_name, CF7ANTISPAM_PLUGIN_URL . '/build/script.js', $asset['dependencies'], $asset['version'], true ); wp_enqueue_script( $this->plugin_name ); wp_localize_script( diff --git a/includes/cf7a-loader.php b/core/CF7_AntiSpam_Loader.php similarity index 99% rename from includes/cf7a-loader.php rename to core/CF7_AntiSpam_Loader.php index cb3307a..171353d 100644 --- a/includes/cf7a-loader.php +++ b/core/CF7_AntiSpam_Loader.php @@ -1,4 +1,7 @@ */ - +use PharData; +use Exception; use GeoIp2\Database\Reader; +use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Tools; /** * It checks if the CF7ANTISPAM_GEOIP_KEY constant is set, and if it is, it uses that as the license key. Otherwise, it diff --git a/includes/service.php b/core/WPCF7_Service.php similarity index 90% rename from includes/service.php rename to core/WPCF7_Service.php index 25c2571..c6e1326 100644 --- a/includes/service.php +++ b/core/WPCF7_Service.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. * @@ -173,7 +176,7 @@ protected function menu_page_url( $args = '' ) { * The function checks if the action is "setup" and the request method is "POST", and if so, it * performs some actions and redirects the user. * - * @param action The "action" parameter is used to determine the specific action that needs to be + * @param "action" parameter is used to determine the specific action that needs to be * performed. In this code snippet, if the value of the "action" parameter is "setup", it will executehttp://two.wordpress.test/wp-admin/tools.php * the code inside the if statement. */ @@ -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 @@ { - return { - ...defaultConfig, - name: fileName, - entry: path.resolve( __dirname, filePath + fileName ), - output: { - path: path.resolve( __dirname, filePath + '../dist/' ), - filename: fileName, - }, - }; +module.exports = { + ...defaultConfig, + entry: { + script: path.resolve( process.cwd(), `src/script.js` ), + 'admin-scripts': path.resolve( process.cwd(), `src/admin-scripts.js` ), + }, }; - -const mainScript = addModule( 'script.js', 'includes/src/' ); -const adminScript = addModule( 'admin-scripts.js', 'admin/src/' ); - -module.exports = [ mainScript, adminScript ];