diff --git a/lib/Segments/WP.php b/lib/Segments/WP.php index e7b679694c..4acbf513a6 100644 --- a/lib/Segments/WP.php +++ b/lib/Segments/WP.php @@ -2,6 +2,8 @@ namespace MailPoet\Segments; use MailPoet\Models\ModelValidator; +use MailPoet\Models\StatisticsClicks; +use MailPoet\Models\StatisticsOpens; use MailPoet\Models\Subscriber; use MailPoet\Models\Segment; use MailPoet\Models\SubscriberSegment; @@ -91,6 +93,7 @@ class WP { self::updateFirstNameIfMissing(); self::insertUsersToSegment(); self::removeOrphanedSubscribers(); + self::markSpammyWordpressUsersAsUnconfirmed(); return true; } @@ -220,4 +223,17 @@ class WP { ->set('wp_user_id', null) ->delete(); } + + private static function markSpammyWordpressUsersAsUnconfirmed() { + global $wpdb; + $query = ' + UPDATE %s as subscribers + LEFT JOIN %s as clicks ON subscribers.id=clicks.subscriber_id + LEFT JOIN %s as opens ON subscribers.id=opens.subscriber_id + JOIN %s as usermeta ON usermeta.user_id=subscribers.wp_user_id AND usermeta.meta_key = "default_password_nag" AND usermeta.meta_value = "1" + SET `status` = "unconfirmed" + WHERE `wp_user_id` IS NOT NULL AND `status` = "subscribed" AND `confirmed_at` IS NULL AND clicks.id IS NULL AND opens.id IS NULL + '; + $wpdb->query(sprintf($query, Subscriber::$_table, StatisticsClicks::$_table, StatisticsOpens::$_table, $wpdb->usermeta)); + } } diff --git a/tests/integration/Segments/WPTest.php b/tests/integration/Segments/WPTest.php index 4f4f9bcc27..9c82b30a89 100644 --- a/tests/integration/Segments/WPTest.php +++ b/tests/integration/Segments/WPTest.php @@ -244,6 +244,20 @@ class WPTest extends \MailPoetTest { expect($db_subscriber)->isEmpty(); } + function testItMarksSpammySubscribersAsUnconfirmed() { + $random_number = rand(); + $id = $this->insertUser($random_number); + $subscriber = Subscriber::createOrUpdate([ + 'email' => 'user-sync-test' . $random_number . '@example.com', + 'status' => Subscriber::STATUS_SUBSCRIBED, + 'wp_user_id' => $id, + ]); + update_user_meta($id, 'default_password_nag', '1'); + WP::synchronizeUsers(); + $db_subscriber = Subscriber::findOne($subscriber->id); + expect($db_subscriber->status)->equals(Subscriber::STATUS_UNCONFIRMED); + } + function _before() { parent::_before(); $this->cleanData();