CustomerFieldsFactory.php
11 months ago
CustomerOrderFieldsFactory.php
1 year ago
CustomerReviewFieldsFactory.php
1 year ago
CustomerSubscriptionFieldsFactory.php
11 months ago
OrderFieldsFactory.php
3 months ago
TermOptionsBuilder.php
2 years ago
TermParentsLoader.php
1 year ago
index.php
3 years ago
CustomerReviewFieldsFactory.php
111 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Fields; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use DateTimeImmutable; |
| 9 | use MailPoet\Automation\Engine\Data\Field; |
| 10 | use MailPoet\Automation\Engine\WordPress; |
| 11 | use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload; |
| 12 | use WC_Customer; |
| 13 | |
| 14 | class CustomerReviewFieldsFactory { |
| 15 | /** @var WordPress */ |
| 16 | private $wordPress; |
| 17 | |
| 18 | public function __construct( |
| 19 | WordPress $wordPress |
| 20 | ) { |
| 21 | $this->wordPress = $wordPress; |
| 22 | } |
| 23 | |
| 24 | /** @return Field[] */ |
| 25 | public function getFields(): array { |
| 26 | return [ |
| 27 | new Field( |
| 28 | 'woocommerce:customer:review-count', |
| 29 | Field::TYPE_INTEGER, |
| 30 | __('Review count', 'mailpoet'), |
| 31 | function (CustomerPayload $payload, array $params = []) { |
| 32 | $customer = $payload->getCustomer(); |
| 33 | if (!$customer) { |
| 34 | return 0; |
| 35 | } |
| 36 | $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; |
| 37 | return $this->getUniqueProductReviewCount($customer, $inTheLastSeconds); |
| 38 | }, |
| 39 | [ |
| 40 | 'params' => ['in_the_last'], |
| 41 | ] |
| 42 | ), |
| 43 | new Field( |
| 44 | 'woocommerce:customer:last-review-date', |
| 45 | Field::TYPE_DATETIME, |
| 46 | __('Last review date', 'mailpoet'), |
| 47 | function (CustomerPayload $payload) { |
| 48 | $customer = $payload->getCustomer(); |
| 49 | return $customer ? $this->getLastReviewDate($customer) : null; |
| 50 | } |
| 51 | ), |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Calculate the customer's review count excluding multiple reviews on the same product. |
| 57 | * Inspired by AutomateWoo implementation. |
| 58 | */ |
| 59 | private function getUniqueProductReviewCount(WC_Customer $customer, ?int $inTheLastSeconds = null): int { |
| 60 | global $wpdb; |
| 61 | |
| 62 | $inTheLastFilter = isset($inTheLastSeconds) ? 'AND c.comment_date_gmt >= DATE_SUB(current_timestamp, INTERVAL %d SECOND)' : ''; |
| 63 | |
| 64 | return (int)$wpdb->get_var( |
| 65 | // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The number of replacements is dynamic. |
| 66 | $wpdb->prepare( |
| 67 | " |
| 68 | SELECT COUNT(DISTINCT c.comment_post_ID) FROM {$wpdb->comments} c |
| 69 | JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID |
| 70 | WHERE p.post_type = 'product' |
| 71 | AND c.comment_parent = 0 |
| 72 | AND c.comment_approved = 1 |
| 73 | AND c.comment_type = 'review' |
| 74 | AND (c.user_ID = %d OR c.comment_author_email = %s) |
| 75 | " . $inTheLastFilter . /* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The condition uses placeholders. */ " |
| 76 | ", |
| 77 | array_merge( |
| 78 | [ |
| 79 | $customer->get_id(), |
| 80 | $customer->get_email(), |
| 81 | ], |
| 82 | $inTheLastSeconds ? [$inTheLastSeconds] : [] |
| 83 | ) |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | private function getLastReviewDate(WC_Customer $customer): ?DateTimeImmutable { |
| 89 | global $wpdb; |
| 90 | |
| 91 | $date = $wpdb->get_var( |
| 92 | $wpdb->prepare( |
| 93 | " |
| 94 | SELECT c.comment_date |
| 95 | FROM {$wpdb->comments} c |
| 96 | JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID |
| 97 | WHERE p.post_type = 'product' |
| 98 | AND c.comment_parent = 0 |
| 99 | AND c.comment_approved = 1 |
| 100 | AND c.comment_type = 'review' |
| 101 | AND (c.user_ID = %d OR c.comment_author_email = %s) |
| 102 | ORDER BY c.comment_date DESC |
| 103 | LIMIT 1 |
| 104 | ", |
| 105 | [$customer->get_id(), $customer->get_email()] |
| 106 | ) |
| 107 | ); |
| 108 | return $date ? new DateTimeImmutable($date, $this->wordPress->wpTimezone()) : null; |
| 109 | } |
| 110 | } |
| 111 |