Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions inc/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions inc/class-workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = microtime( true );
$parsed_message['data'] = [];
Comment thread
shadyvb marked this conversation as resolved.
$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(
Expand Down
17 changes: 14 additions & 3 deletions lib/destinations/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
'type' => 'string',
'required' => true,
],
'time' => [
'type' => 'int',
'required' => true,
],
'data' => [
'type' => 'object',
],
'actions' => [
'type' => 'array',
'items' => [
Expand Down Expand Up @@ -211,14 +218,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 ) ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably make use of the enhancements Rob suggested, where a more thorough function is checking object items based on the value type. It needs some work on the original ticket anyway, and probably then some.

$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'] ),
Expand Down Expand Up @@ -292,9 +301,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.
Expand Down