array( 'label' => __( 'Booking confirmed', 'tableengine' ), 'audience' => 'guest' ), 'pending' => array( 'label' => __( 'Request received (pending)', 'tableengine' ), 'audience' => 'guest' ), 'cancel' => array( 'label' => __( 'Booking cancelled', 'tableengine' ), 'audience' => 'guest' ), 'reminder' => array( 'label' => __( 'Reminder', 'tableengine' ), 'audience' => 'guest' ), 'admin_new' => array( 'label' => __( 'New booking alert', 'tableengine' ), 'audience' => 'restaurant' ), 'admin_cancel' => array( 'label' => __( 'Cancellation alert', 'tableengine' ), 'audience' => 'restaurant' ), ) ); } /** * @return array */ public static function channels() { return (array) apply_filters( 'tableengine_notification_channels', array( 'email' => array( __CLASS__, 'email' ) ) ); } /** * Send an event to every channel (or only the listed ones). * * @param string[]|null $only Channel ids to use. * @return array Result per channel that handled the event. */ public static function dispatch( $event, array $row, array $args = array(), $only = null ) { if ( ! apply_filters( 'tableengine_should_notify', true, $event, $row, $args ) ) { return array(); } $results = array(); /** E.g. switch to the booking's location so templates and senders match it. */ do_action( 'tableengine_before_notify', $event, $row, $args ); foreach ( self::channels() as $id => $channel ) { if ( ( null !== $only && ! in_array( $id, (array) $only, true ) ) || ! is_callable( $channel ) ) { continue; } $sent = call_user_func( $channel, $event, $row, $args ); if ( null !== $sent ) { $results[ $id ] = (bool) $sent; do_action( 'tableengine_notification_sent', $id, $event, $row, (bool) $sent, $args ); } } do_action( 'tableengine_after_notify', $event, $row, $args ); return $results; } /** * Built-in email channel. */ public static function email( $event, array $row, array $args = array() ) { if ( 0 === strpos( $event, 'admin_' ) ) { return Emails::send_admin( substr( $event, 6 ), $row ); } // Any guest event with an email template (settings or `tableengine_email_template`) is sendable. if ( null === Emails::template( $event, $row, $args ) ) { return null; } return Emails::send( $event, $row, $args ); } }