| 1 |
<?php |
| 2 |
/** |
| 3 |
* MailPoet Service |
| 4 |
* |
| 5 |
* Adds Booktics booking customers to MailPoet lists via the MailPoet public API. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Booktics\Module\Mailpoet; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
use Booktics\Contracts\Hookable_Service_Contract; |
| 13 |
use Booktics\Abstracts\Booktics_Database; |
| 14 |
use Booktics\Models\Guest_Model; |
| 15 |
|
| 16 |
class Mailpoet_Service implements Hookable_Service_Contract { |
| 17 |
/** |
| 18 |
* Register hooks |
| 19 |
*/ |
| 20 |
public function register() { |
| 21 |
add_action( 'booktics_after_order_created', array( $this, 'send_to_mailpoet' ), 10, 2 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Add the booking customer to the configured MailPoet list(s). |
| 26 |
* |
| 27 |
* @param object $order_model The order model. |
| 28 |
* @param array $appointments Created appointments (unused). |
| 29 |
*/ |
| 30 |
public function send_to_mailpoet( $order_model, $appointments = array() ) { |
| 31 |
// MailPoet plugin may be inactive/uninstalled. |
| 32 |
if ( ! class_exists( '\MailPoet\API\API' ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$list_ids = booktics_get_option( 'mailpoet_list_ids' ); |
| 37 |
$list_ids = array_values( array_filter( array_map( 'intval', (array) $list_ids ) ) ); |
| 38 |
if ( empty( $list_ids ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
// Get customer details. customer_id is a Booktics guest record id, not a WP user id. |
| 43 |
$customer = ( new Guest_Model( new Booktics_Database() ) )->find( array( 'id' => $order_model->customer_id ) ); |
| 44 |
if ( ! $customer || is_wp_error( $customer ) || empty( $customer->email ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$name_parts = preg_split( '/\s+/', trim( (string) $customer->name ), 2 ); |
| 49 |
|
| 50 |
$subscriber = array( |
| 51 |
'email' => $customer->email, |
| 52 |
'first_name' => $name_parts[0] ?? '', |
| 53 |
'last_name' => $name_parts[1] ?? '', |
| 54 |
); |
| 55 |
|
| 56 |
try { |
| 57 |
$this->add_subscriber( $subscriber, $list_ids ); |
| 58 |
} catch ( \Throwable $e ) { |
| 59 |
// Ensures silent failure in case of any error. |
| 60 |
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 61 |
error_log( 'MailPoet Error: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Add a subscriber and subscribe to lists. If the subscriber already |
| 68 |
* exists, just subscribe the existing record to the lists. |
| 69 |
* |
| 70 |
* @param array $subscriber Subscriber fields (email, first_name, last_name). |
| 71 |
* @param array $list_ids MailPoet list ids. |
| 72 |
*/ |
| 73 |
private function add_subscriber( array $subscriber, array $list_ids ) { |
| 74 |
$mailpoet = \MailPoet\API\API::MP( 'v1' ); |
| 75 |
|
| 76 |
// Look up first: getSubscriber throws when the email is not found. |
| 77 |
$existing = null; |
| 78 |
try { |
| 79 |
$existing = $mailpoet->getSubscriber( $subscriber['email'] ); |
| 80 |
} catch ( \Exception $e ) { |
| 81 |
$existing = null; |
| 82 |
} |
| 83 |
|
| 84 |
// Existing subscriber - just subscribe the record to the lists. |
| 85 |
if ( ! empty( $existing['id'] ) ) { |
| 86 |
$mailpoet->subscribeToLists( $existing['id'], $list_ids ); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// New subscriber - create and subscribe. Any real error here propagates |
| 91 |
// to send_to_mailpoet() and is logged. |
| 92 |
$mailpoet->addSubscriber( |
| 93 |
$subscriber, |
| 94 |
$list_ids, |
| 95 |
array( 'skip_subscriber_notification' => true ) |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|