WordPress as a Middleware?

I’m handling the technical things for a scout project (crover.info). We use many different open source tools like WordPress for the website, Mattermost for communication and Pretix for our booking tools. And while they all work quite well and also offer extensibility there was easy connection to get bookings from Pretix showing up in Mattermost, even thought Pretix offered a connection to Mattermosts alternative Slack.

But luckily there are webhooks which Pretix can sent out and Mattermost can receive in order to show a message. The second issue was: The webhook from Pretix couldn’t be directly consumed by Mattermost as the content was different.

To solve this I came up with a solution: Add a translator in the middle. So I first started to write some basic JavaScript code that can run as a “NodeJS middleware” but as this was another service we needed to host I started to look at alternatives and there it was: WordPress was already running and available and thanks to the plugin system and the WordPress REST API it should be pretty easy to act as a middleware in between Pretix and Mattermost.

The functionality is easy:

  • The plugin provides a new API route behind a randomly generated string e.g.: /wp-json/pretix/v1/13293012381
add_action('rest_api_init', function () {
    $suffix = get_webhook_suffix();
    register_rest_route('pretix/v1', '/' . $webhook-secret, [
        'methods' => 'POST',
        'callback' => 'pretix_mattermost_webhook',
        'permission_callback' => '__return_true'
    ]);
});
  • An interface in the wp-admin settings page for some general configuration:
  • A function to get the booking ID from the incoming webhook, check the API from Pretix for additional details, combine those data into a JSON-format expected by Mattermost and sent it
    • For increased security I added a feature that only works with the incoming webhook if it is received from the right system (URL and IP need to match)
  • Register a custom post type “Bookings” to also track the bookings in our WP backend for convenience reasons.
Code to register the Custom Post Type
function pretix_mattermost_webhook_create_cpt() {
    $labels = array(
        'name' => _x('Pretix Buchungen', 'Post Type General Name', 'textdomain'),
        'singular_name' => _x('Pretix Buchung', 'Post Type Singular Name', 'textdomain'),
        'menu_name' => _x('Pretix Buchungen', 'Admin Menu text', 'textdomain'),
        'name_admin_bar' => _x('Pretix Buchung', 'Add New on Toolbar', 'textdomain'),
        'add_new' => __('Neue hinzufügen', 'textdomain'),
        'add_new_item' => __('Neue Pretix Buchung hinzufügen', 'textdomain'),
        'new_item' => __('Neue Pretix Buchung', 'textdomain'),
        'edit_item' => __('Pretix Buchung bearbeiten', 'textdomain'),
        'view_item' => __('Pretix Buchung anzeigen', 'textdomain'),
        'all_items' => __('Alle Pretix Buchungen', 'textdomain'),
        'search_items' => __('Pretix Buchungen durchsuchen', 'textdomain'),
        'not_found' => __('Keine Pretix Buchungen gefunden.', 'textdomain'),
        'not_found_in_trash' => __('Keine Pretix Buchungen im Papierkorb gefunden.', 'textdomain'),
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'pretix_booking'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 20,
        'supports' => array('title', 'editor', 'custom-fields'),
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-tickets-alt',
    );

    register_post_type('pretix_booking', $args);
}

add_action('init', 'pretix_mattermost_webhook_create_cpt');

In conclusion, the successful implementation of these open source tools in the scout project’s technical setup stands as a testament to the ingenuity and adaptability of modern web development. By embracing the potential of webhooks, the flexibility of open-source systems and crafting bespoke solutions, we have not only streamlined operations but also set a precedent for future endeavors.


Leave a Reply

Discover more from Crixus Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading