Experiments in WordPress routing 03

Can I users?

WordPress flow

WordPress packs an amazing amount of functions and APIs and I’ve tapped into two (filters and the routing one) to make things go the way I want.
The purpose of the experiment is to find if this alternative routing solution can make sense and be a feasible and convenient way to build some projects.
To have such a result, as a first step, I want to have the possibility to continue normal WordPress execution should the requested path not be matched by any defined route.
It takes little time to verify that


/**
     * Plugin Name: theAverageDev Routes
     * Plugin URI: http://theAverageDev.com
     * Description: Routing for WordPress
     * Version: 1.0
     * Author: theAverageDev
     * Author URI: http://theAverageDev.com
     * License: GPL 2.0
     */

    require 'vendor/autoload_52.php';

    /**
     * Support methods
     */
    function _json_post( $request, $response ) {
        $post_id = $request->param( 'id' );
        $post    = get_post( $post_id );
        if ( empty( $post ) ) {
            echo json_encode( [ 'Error' => 'Not a valid post ID' ] );
        } else {
            echo( json_encode( $post ) );
        }
    }

    /**
     * Routes
     */
    function route_posts() {
        respond( 'GET', '/[i:id]/json', '_json_post' );
    }

    function _dispatch() {
        $found = dispatch( null, null, null, true );
        if ( $found ) {
            die($found);
        }
    }

    /**
     * Parse request
     */
    add_filter( 'do_parse_request', 'tad_routes_do_parse_request', 1, 3 );
    function tad_routes_do_parse_request( $continue, WP $wp, $extra_query_vars ) {
        with( '/posts', 'route_posts' );
        _dispatch();

        return $continue;
    }

Hitting the /posts/[id]/route will yield the json version of the post while hitting any other route will go on with the usual WordPress flow; WordPress is not lost to me.

Users

I’d like to know if, at this point of the WordPress flow, I will be able to have information about the current user; this is a fundamental feature for any routing to make sense.
The code below tests for such a feature at the /who-am-i route:


/**
     * Plugin Name: theAverageDev Routes
     * Plugin URI: http://theAverageDev.com
     * Description: Routing for WordPress
     * Version: 1.0
     * Author: theAverageDev
     * Author URI: http://theAverageDev.com
     * License: GPL 2.0
     */

    require 'vendor/autoload_52.php';

    /**
     * Support methods
     */
    function _json_post( $request, $response ) {
        $post_id = $request->param( 'id' );
        $post    = get_post( $post_id );
        if ( empty( $post ) ) {
            echo json_encode( [ 'Error' => 'Not a valid post ID' ] );
        } else {
            echo( json_encode( $post ) );
        }
    }

    function _dispatch() {
        $found = dispatch( null, null, null, true );
        if ( $found ) {
            die( $found );
        }
    }


    /**
     * Routes
     */
    function route_posts() {
        respond( 'GET', '/[i:id]/json', '_json_post' );
    }

    function route_who_am_i() {
        $user = wp_get_current_user();
        echo $user->ID > 0 ? sprintf( 'Hi %s', $user->display_name ) : 'You are not logged in';
    }

    /**
     * Parse request
     */
    add_filter( 'do_parse_request', 'tad_routes_do_parse_request', 1, 3 );
    function tad_routes_do_parse_request( $continue, WP $wp, $extra_query_vars ) {
        with( '/posts', 'route_posts' );
        respond( '/who-am-i', 'route_who_am_i' );
        _dispatch();

        return $continue;
    }

Hitting the route as a logged in user will show a message with the user name
Logged in
while hitting it as a non-logged in user will show this message
Logged out
This is another good thing to have to use WordPress as a back-end tool.

Next

The code used in this post in on GitHub and the next steps will be to explore the limits of this solution; I know I’m renouncing the WordPress query and routing system but do not know the exact limits of this choice and am working to know them.

I appreciate your input