Remove the rest of the old model usage

[MAILPOET-3753]
This commit is contained in:
Jan Lysý
2021-11-19 18:53:02 +01:00
committed by Veljko V
parent a30d25e32f
commit c0c7e23b0e
3 changed files with 36 additions and 28 deletions

View File

@@ -6,11 +6,10 @@ use MailPoet\Config\Env;
use MailPoet\Entities\SubscriberEntity; use MailPoet\Entities\SubscriberEntity;
use MailPoet\Entities\SubscriberSegmentEntity; use MailPoet\Entities\SubscriberSegmentEntity;
use MailPoet\Models\ModelValidator; use MailPoet\Models\ModelValidator;
use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\Source; use MailPoet\Subscribers\Source;
use MailPoet\Subscribers\SubscriberSaveController;
use MailPoet\Subscribers\SubscriberSegmentRepository;
use MailPoet\Subscribers\SubscribersRepository; use MailPoet\Subscribers\SubscribersRepository;
use MailPoet\WooCommerce\Helper as WCHelper; use MailPoet\WooCommerce\Helper as WCHelper;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
@@ -42,6 +41,12 @@ class WooCommerce {
/** @var SegmentsRepository */ /** @var SegmentsRepository */
private $segmentsRepository; private $segmentsRepository;
/** @var SubscriberSegmentRepository */
private $subscriberSegmentRepository;
/** @var SubscriberSaveController */
private $subscriberSaveController;
/** @var WCHelper */ /** @var WCHelper */
private $woocommerceHelper; private $woocommerceHelper;
@@ -57,6 +62,8 @@ class WooCommerce {
WCHelper $woocommerceHelper, WCHelper $woocommerceHelper,
SubscribersRepository $subscribersRepository, SubscribersRepository $subscribersRepository,
SegmentsRepository $segmentsRepository, SegmentsRepository $segmentsRepository,
SubscriberSegmentRepository $subscriberSegmentRepository,
SubscriberSaveController $subscriberSaveController,
WP $wpSegment, WP $wpSegment,
EntityManager $entityManager, EntityManager $entityManager,
Connection $connection Connection $connection
@@ -66,6 +73,8 @@ class WooCommerce {
$this->wpSegment = $wpSegment; $this->wpSegment = $wpSegment;
$this->subscribersRepository = $subscribersRepository; $this->subscribersRepository = $subscribersRepository;
$this->segmentsRepository = $segmentsRepository; $this->segmentsRepository = $segmentsRepository;
$this->subscriberSegmentRepository = $subscriberSegmentRepository;
$this->subscriberSaveController = $subscriberSaveController;
$this->woocommerceHelper = $woocommerceHelper; $this->woocommerceHelper = $woocommerceHelper;
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->connection = $connection; $this->connection = $connection;
@@ -82,9 +91,7 @@ class WooCommerce {
} }
public function synchronizeRegisteredCustomer($wpUserId, $currentFilter = null) { public function synchronizeRegisteredCustomer($wpUserId, $currentFilter = null) {
$wcSegment = Segment::getWooCommerceSegment(); $wcSegment = $this->segmentsRepository->getWooCommerceSegment();
if ($wcSegment === false) return;
$currentFilter = $currentFilter ?: $this->wp->currentFilter(); $currentFilter = $currentFilter ?: $this->wp->currentFilter();
switch ($currentFilter) { switch ($currentFilter) {
@@ -98,10 +105,9 @@ class WooCommerce {
case 'woocommerce_update_customer': case 'woocommerce_update_customer':
default: default:
$wpUser = $this->wp->getUserdata($wpUserId); $wpUser = $this->wp->getUserdata($wpUserId);
$subscriber = Subscriber::where('wp_user_id', $wpUserId) $subscriber = $this->subscribersRepository->findOneBy(['wpUserId' => $wpUserId]);
->findOne();
if ($wpUser === false || $subscriber === false) { if ($wpUser === false || $subscriber === null) {
// registered customers should exist as WP users and WP segment subscribers // registered customers should exist as WP users and WP segment subscribers
return false; return false;
} }
@@ -112,21 +118,19 @@ class WooCommerce {
if (!empty($newCustomer)) { if (!empty($newCustomer)) {
$data['source'] = Source::WOOCOMMERCE_USER; $data['source'] = Source::WOOCOMMERCE_USER;
} }
$data['id'] = $subscriber->id(); $data['id'] = $subscriber->getId();
if ($wpUser->first_name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps if ($wpUser->first_name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$data['first_name'] = $wpUser->first_name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $data['first_name'] = $wpUser->first_name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
} }
if ($wpUser->last_name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps if ($wpUser->last_name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$data['last_name'] = $wpUser->last_name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $data['last_name'] = $wpUser->last_name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
} }
$subscriber = Subscriber::createOrUpdate($data); $subscriber = $this->subscriberSaveController->createOrUpdate($data, $subscriber);
if ($subscriber->getErrors() === false && $subscriber->id > 0) { // add subscriber to the WooCommerce Customers segment
// add subscriber to the WooCommerce Customers segment $this->subscriberSegmentRepository->subscribeToSegments(
SubscriberSegment::subscribeToSegments( $subscriber,
$subscriber, [$wcSegment]
[$wcSegment->id] );
);
}
break; break;
} }
@@ -135,9 +139,9 @@ class WooCommerce {
public function synchronizeGuestCustomer($orderId) { public function synchronizeGuestCustomer($orderId) {
$wcOrder = $this->woocommerceHelper->wcGetOrder($orderId); $wcOrder = $this->woocommerceHelper->wcGetOrder($orderId);
$wcSegment = Segment::getWooCommerceSegment(); $wcSegment = $this->segmentsRepository->getWooCommerceSegment();
if ((!$wcOrder instanceof \WC_Order) || $wcSegment === false) return; if (!$wcOrder instanceof \WC_Order) return;
$signupConfirmation = $this->settings->get('signup_confirmation'); $signupConfirmation = $this->settings->get('signup_confirmation');
$status = SubscriberEntity::STATUS_UNCONFIRMED; $status = SubscriberEntity::STATUS_UNCONFIRMED;
if ((bool)$signupConfirmation['enabled'] === false) { if ((bool)$signupConfirmation['enabled'] === false) {
@@ -149,20 +153,19 @@ class WooCommerce {
if (empty($insertedEmails[0])) { if (empty($insertedEmails[0])) {
return false; return false;
} }
$subscriber = Subscriber::where('email', $insertedEmails[0]) $subscriber = $this->subscribersRepository->findOneBy(['email' => $insertedEmails[0]]);
->findOne();
if ($subscriber !== false) { if ($subscriber) {
$firstName = $wcOrder->get_billing_first_name(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $firstName = $wcOrder->get_billing_first_name(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$lastName = $wcOrder->get_billing_last_name(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $lastName = $wcOrder->get_billing_last_name(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
if ($firstName) { if ($firstName) {
$subscriber->firstName = $firstName; $subscriber->setFirstName($firstName);
} }
if ($lastName) { if ($lastName) {
$subscriber->lastName = $lastName; $subscriber->setLastName($lastName);
} }
if ($firstName || $lastName) { if ($firstName || $lastName) {
$subscriber->save(); $this->subscribersRepository->flush();
} }
} }
} }
@@ -246,7 +249,7 @@ class WooCommerce {
", ['capabilities' => $wpdb->prefix . 'capabilities', 'source' => Source::WOOCOMMERCE_USER]); ", ['capabilities' => $wpdb->prefix . 'capabilities', 'source' => Source::WOOCOMMERCE_USER]);
} }
private function insertSubscribersFromOrders($orderId = null, $status = Subscriber::STATUS_SUBSCRIBED): array { private function insertSubscribersFromOrders($orderId = null, $status = SubscriberEntity::STATUS_SUBSCRIBED): array {
global $wpdb; global $wpdb;
$validator = new ModelValidator(); $validator = new ModelValidator();
$orderId = !is_null($orderId) ? (int)$orderId : null; $orderId = !is_null($orderId) ? (int)$orderId : null;

View File

@@ -189,6 +189,7 @@ class SubscriberSaveController {
if (isset($data['wp_user_id'])) $subscriber->setWpUserId($data['wp_user_id']); if (isset($data['wp_user_id'])) $subscriber->setWpUserId($data['wp_user_id']);
if (isset($data['subscribed_ip'])) $subscriber->setSubscribedIp($data['subscribed_ip']); if (isset($data['subscribed_ip'])) $subscriber->setSubscribedIp($data['subscribed_ip']);
if (isset($data['confirmed_ip'])) $subscriber->setConfirmedIp($data['confirmed_ip']); if (isset($data['confirmed_ip'])) $subscriber->setConfirmedIp($data['confirmed_ip']);
if (isset($data['is_woocommerce_user'])) $subscriber->setIsWoocommerceUser((bool)$data['is_woocommerce_user']);
$createdAt = isset($data['created_at']) ? Carbon::createFromFormat('Y-m-d H:i:s', $data['created_at']) : null; $createdAt = isset($data['created_at']) ? Carbon::createFromFormat('Y-m-d H:i:s', $data['created_at']) : null;
if ($createdAt) $subscriber->setCreatedAt($createdAt); if ($createdAt) $subscriber->setCreatedAt($createdAt);
$confirmedAt = isset($data['confirmed_at']) ? Carbon::createFromFormat('Y-m-d H:i:s', $data['confirmed_at']) : null; $confirmedAt = isset($data['confirmed_at']) ? Carbon::createFromFormat('Y-m-d H:i:s', $data['confirmed_at']) : null;

View File

@@ -1,4 +1,4 @@
<?php <?php declare(strict_types = 1);
namespace MailPoet\Test\Segments; namespace MailPoet\Test\Segments;
@@ -13,6 +13,8 @@ use MailPoet\Segments\WooCommerce;
use MailPoet\Segments\WP; use MailPoet\Segments\WP;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\Source; use MailPoet\Subscribers\Source;
use MailPoet\Subscribers\SubscriberSaveController;
use MailPoet\Subscribers\SubscriberSegmentRepository;
use MailPoet\Subscribers\SubscribersRepository; use MailPoet\Subscribers\SubscribersRepository;
use MailPoet\WooCommerce\Helper; use MailPoet\WooCommerce\Helper;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
@@ -773,6 +775,8 @@ class WooCommerceTest extends \MailPoetTest {
$wooHelperMock ?? $this->diContainer->get(Helper::class), $wooHelperMock ?? $this->diContainer->get(Helper::class),
$this->diContainer->get(SubscribersRepository::class), $this->diContainer->get(SubscribersRepository::class),
$this->diContainer->get(SegmentsRepository::class), $this->diContainer->get(SegmentsRepository::class),
$this->diContainer->get(SubscriberSegmentRepository::class),
$this->diContainer->get(SubscriberSaveController::class),
$this->diContainer->get(WP::class), $this->diContainer->get(WP::class),
$this->entityManager, $this->entityManager,
$this->entityManager->getConnection() $this->entityManager->getConnection()