Skip to content

Commit

Permalink
Merge pull request #66 from newfold-labs/add/wp-mail-event
Browse files Browse the repository at this point in the history
Add a listener to track `wp_mail` events
  • Loading branch information
wpscholar authored Apr 10, 2024
2 parents efee98f + 37df1ef commit 04217ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
14 changes: 8 additions & 6 deletions includes/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class EventManager {
'\\NewfoldLabs\\WP\\Module\\Data\\Listeners\\Commerce',
'\\NewfoldLabs\\WP\\Module\\Data\\Listeners\\Yoast',
'\\NewfoldLabs\\WP\\Module\\Data\\Listeners\\WonderCart',
'\\NewfoldLabs\\WP\\Module\\Data\\Listeners\\WPMail',
);

/**
Expand Down Expand Up @@ -92,13 +93,14 @@ public function rest_api_init() {
/**
* Add the weekly option to cron schedules if it doesn't exist
*
* @param array $schedules List of cron schedule options
* @param array $schedules List of cron schedule options
*
* @return array
*/
public function add_minutely_schedule( $schedules ) {
if ( ! array_key_exists( 'minutely',
$schedules ) || MINUTE_IN_SECONDS !== $schedules['minutely']['interval'] ) {
if ( ! array_key_exists( 'minutely', $schedules ) ||
MINUTE_IN_SECONDS !== $schedules['minutely']['interval']
) {
$schedules['minutely'] = array(
'interval' => MINUTE_IN_SECONDS,
'display' => __( 'Once Every Minute' ),
Expand Down Expand Up @@ -138,7 +140,7 @@ public function shutdown() {
/**
* Register a new event subscriber
*
* @param SubscriberInterface $subscriber Class subscribing to event updates
* @param SubscriberInterface $subscriber Class subscribing to event updates
*
* @return void
*/
Expand Down Expand Up @@ -183,7 +185,7 @@ public function initialize_listeners() {
/**
* Push event data onto the queue
*
* @param Event $event Details about the action taken
* @param Event $event Details about the action taken
*
* @return void
*/
Expand All @@ -195,7 +197,7 @@ public function push( Event $event ) {
/**
* Send queued events to all subscribers
*
* @param array $events A list of events
* @param array $events A list of events
*
* @return void
*/
Expand Down
34 changes: 34 additions & 0 deletions includes/Listeners/WPMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace NewfoldLabs\WP\Module\Data\Listeners;

/**
* Monitors wp_mail events.
*/
class WPMail extends Listener {

/**
* Register wp_mail_* hooks for the subscriber.
*
* @return void
*/
public function register_hooks() {
add_action( 'wp_mail_succeeded', array( $this, 'mail_succeeded' ), 10, 1 );
}

/**
* Mail sent successfully.
*
* @param array $mail_data An array containing the email recipient(s), subject, message, headers, and attachments.
* @return void
*/
public function mail_succeeded( $mail_data ) {
$this->push(
'wp_mail',
array(
'label_key' => 'subject',
'subject' => $mail_data['subject'],
)
);
}
}

0 comments on commit 04217ce

Please sign in to comment.