Skip to content

Commit

Permalink
Merge pull request #12 from x-team/matching
Browse files Browse the repository at this point in the history
Matching rules and alerting process
  • Loading branch information
frankiejarrett committed Jan 29, 2014
2 parents 81b05c1 + b9ba72c commit b71608b
Show file tree
Hide file tree
Showing 8 changed files with 1,445 additions and 240 deletions.
45 changes: 36 additions & 9 deletions classes/adapters/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,57 @@
class WP_Stream_Notification_Adapter_Email extends WP_Stream_Notification_Adapter {

public static function register( $title = '' ) {
parent::register( __( 'Email', 'stream_notification' ) );
parent::register( __( 'Email', 'stream-notifications' ) );
}

public static function fields() {
return array(
'to' => array(
'title' => __( 'To', 'stream_notification' ),
'users' => array(
'title' => __( 'To users', 'stream-notifications' ),
'type' => 'hidden',
'multiple' => true,
'ajax' => true,
'key' => 'author',
),
),
'emails' => array(
'title' => __( 'To emails', 'stream-notifications' ),
'type' => 'text',
'tags' => true,
),
'subject' => array(
'title' => __( 'Subject', 'stream_notification' ),
'title' => __( 'Subject', 'stream-notifications' ),
'type' => 'text',
'hint' => __( 'ex: "%%summary%%" or "[%%created%% - %%author%%] %%summary%%", consult FAQ for documentaion.', 'stream_notification' ),
),
'hint' => __( 'ex: "%%summary%%" or "[%%created%% - %%author%%] %%summary%%", consult FAQ for documentaion.', 'stream-notifications' ),
),
'message' => array(
'title' => __( 'Message', 'stream_notification' ),
'title' => __( 'Message', 'stream-notifications' ),
'type' => 'textarea',
),
),
);
}

public function send( $log ) {
$users = $this->params['users'];
$user_emails = array();
if ( $users ) {
$user_query = new WP_User_Query(
array(
'include' => $users,
'fields' => array( 'user_email' ),
)
);
$user_emails = wp_list_pluck( $user_query->results, 'user_email' );
}
$emails = explode( ',', $this->params['emails'] );
if ( ! empty( $user_emails ) ) {
$emails = array_merge( $emails, $user_emails );
}
$emails = array_filter( $emails );
$subject = $this->replace( $this->params['subject'], $log );
$message = $this->replace( $this->params['message'], $log );
wp_mail( $to, $subject, $message );
}

}

WP_Stream_Notification_Adapter_Email::register();
86 changes: 85 additions & 1 deletion classes/wp-stream-notification-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

abstract class WP_Stream_Notification_Adapter {

public $params = array();

public static function register( $title ) {
$class = get_called_class();
$name = strtolower( str_replace( 'WP_Stream_Notification_Adapter_', '', $class ) );
Expand All @@ -12,8 +14,90 @@ public static function fields() {
return array();
}

function send( $log ) {
/**
* Replace placeholders in alert[field]s with proper info from the log
* @param string $haystack Text to replace in
* @param array $log Log array
* @return string
*/
public static function replace( $haystack, $log ) {
if ( preg_match_all( '#%%([^%]+)%%#', $haystack, $placeholders ) ) {

foreach ( $placeholders[1] as $placeholder ) {
$value = false;
switch ( $placeholder ) {
case 'summary':
case 'object_id':
case 'author':
case 'created':
$value = $log[$placeholder];
break;
case ( strpos( $placeholder, 'meta.' ) !== false ):
$meta_key = substr( $placeholder, 5 );
if ( isset( $log['meta'][ $meta_key ] ) ) {
$value = $log['meta'][ $meta_key ];
}
break;
case ( strpos( $placeholder, 'author.' ) !== false ):
$meta_key = substr( $placeholder, 7 );
$author = get_userdata( $log['author'] );
if ( $author && isset( $author->{$meta_key} ) ) {
$value = $author->{$meta_key};
}
break;
// TODO Move this part to Stream base, and abstract it
case ( strpos( $placeholder, 'object.' ) !== false ):
$meta_key = substr( $placeholder, 7 );
$context = key( $log['contexts'] );
// can only guess the object type, since there is no
// actual reference here
switch ( $context ) {
case 'post':
case 'page':
case 'media':
$object = get_post( $log['object_id'] );
break;
case 'users':
$object = get_userdata( $log['object_id'] );
break;
case 'comment':
$object = get_comment( $log['object_id'] );
break;
case 'term':
case 'category':
case 'post_tag':
case 'link_category':
$object = get_term( $log['object_id'], $log['meta']['taxonomy'] );
break;
default:
$object = apply_filters( 'stream_notifications_record_object', $log['object_id'], $log );
break;
}
if ( is_object( $object ) && isset( $object->{$meta_key} ) ) {
$value = $object->{$meta_key};
}
break;
}
if ( $value ) {
$haystack = str_replace( "%%$placeholder%%", $value, $haystack );
}
}
}
return $haystack;
}

function load( $alert ) {
$params = array();
$fields = $this::fields();
foreach ( $fields as $field => $options ) {
$params[ $field ] = isset( $alert[ $field ] )
? $alert[ $field ]
: null;
}
$this->params = $params;
return $this;
}

abstract function send( $log );

}
Loading

0 comments on commit b71608b

Please sign in to comment.