From f5fd4380f4c62b2ab1dfb4c422fef3fa3d180d38 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 8 Jul 2019 14:27:52 +0100 Subject: [PATCH 1/3] Add data to notification --- inc/class-event.php | 6 ++++++ inc/class-workflow.php | 6 ++++++ lib/destinations/dashboard.php | 18 +++++++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/inc/class-event.php b/inc/class-event.php index ea8dd3e2..ec174e9f 100644 --- a/inc/class-event.php +++ b/inc/class-event.php @@ -158,6 +158,12 @@ public function add_message_tags( array $tags ): Event { return $this; } + public function add_message_data_callback( callable $callback ): Event { + $this->message_data_callback = $callback; + + return $this; + } + /** * Adds a message action. * diff --git a/inc/class-workflow.php b/inc/class-workflow.php index ef08aafb..08cd5427 100644 --- a/inc/class-workflow.php +++ b/inc/class-workflow.php @@ -359,8 +359,14 @@ public function run( array $args = [] ) { $parsed_message = []; $parsed_message['subject'] = str_replace( array_keys( $tags ), array_values( $tags ), $subject ); $parsed_message['text'] = str_replace( array_keys( $tags ), array_values( $tags ), $text ); + $parsed_message['time'] = time(); + $parsed_message['data'] = []; $parsed_message['actions'] = []; + if ( $this->event->message_data_callback && is_callable( $this->event->message_data_callback ) ) { + $parsed_message['data'] = call_user_func_array( $this->event->message_data_callback, $args ); + } + // Add actions from the message if any. foreach ( $message['actions'] as $id => $action ) { $this->event->add_message_action( diff --git a/lib/destinations/dashboard.php b/lib/destinations/dashboard.php index 74de9136..cb1b271f 100644 --- a/lib/destinations/dashboard.php +++ b/lib/destinations/dashboard.php @@ -30,6 +30,14 @@ 'type' => 'string', 'required' => true, ], + 'time' => [ + 'type' => 'int', + 'required' => true, + ], + 'data' => [ + 'type' => 'string', + 'required' => true, + ], 'actions' => [ 'type' => 'array', 'items' => [ @@ -211,14 +219,16 @@ function sanitize_notification( $notification ) { */ $data_sanitization_fn = apply_filters( 'hm.workflows.action.data.sanitizer', 'sanitize_text_field' ); - if ( ! is_callable( $data_sanitization_fn ) ) { + if ( ! $data_sanitization_fn || ! is_callable( $data_sanitization_fn ) ) { $data_sanitization_fn = 'sanitize_text_field'; } $sanitized_notification = [ 'subject' => wp_kses( $notification['subject'] ?? '', [] ), 'text' => wp_kses_post( $notification['text'] ?? '' ), - 'actions' => array_values( array_map( function ( $action, $id ) { + 'time' => intval( $notification['time'] ?? 0 ), + 'data' => $notification['data'] ?? [], + 'actions' => array_values( array_map( function ( $action, $id ) use ( $data_sanitization_fn ) { return [ 'id' => isset( $action['id'] ) ? sanitize_key( $action['id'] ) : sanitize_key( $id ), 'text' => sanitize_text_field( $action['text'] ), @@ -292,9 +302,11 @@ function create( WP_REST_Request $request ) { } $notification = sanitize_notification( [ + 'actions' => $request->get_param( 'actions' ), + 'data' => $request->get_param( 'data' ), 'subject' => $request->get_param( 'subject' ), 'text' => $request->get_param( 'text' ), - 'actions' => $request->get_param( 'actions' ), + 'time' => $request->get_param( 'time' ), ] ); // Store a placeholder to get a meta ID. From a67c5688c9cb065b587d68a4e9cd28ef6469aeee Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Tue, 9 Jul 2019 09:16:41 +0100 Subject: [PATCH 2/3] Use microtime not time --- inc/class-workflow.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-workflow.php b/inc/class-workflow.php index 08cd5427..b01df3de 100644 --- a/inc/class-workflow.php +++ b/inc/class-workflow.php @@ -359,7 +359,7 @@ public function run( array $args = [] ) { $parsed_message = []; $parsed_message['subject'] = str_replace( array_keys( $tags ), array_values( $tags ), $subject ); $parsed_message['text'] = str_replace( array_keys( $tags ), array_values( $tags ), $text ); - $parsed_message['time'] = time(); + $parsed_message['time'] = microtime( true ); $parsed_message['data'] = []; $parsed_message['actions'] = []; From ac13a8ab81df3c94cea3062cefbfdc9ccc46032d Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Tue, 9 Jul 2019 09:16:58 +0100 Subject: [PATCH 3/3] Better data prop schema --- lib/destinations/dashboard.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/destinations/dashboard.php b/lib/destinations/dashboard.php index cb1b271f..b26152fa 100644 --- a/lib/destinations/dashboard.php +++ b/lib/destinations/dashboard.php @@ -35,8 +35,7 @@ 'required' => true, ], 'data' => [ - 'type' => 'string', - 'required' => true, + 'type' => 'object', ], 'actions' => [ 'type' => 'array',