Route Pages going somewhere 01

After much set up and some more test-related experimentation I’ve decided to walk the same way WP Router took to trigger Route Pages functions.

The problem

Route pages offer is two-fold: on one side the WordPress plugin wraps WP Router methods and hooks in a Laravel-like fashion to allow for easy and streamlined usage of the routing possibility (see the wp-routing package at its core) while, on the other hand, it tries to make routes usable by the end-user creating “route pages” for some routes.
The first plugin function is firmly in developer hands allowing any interaction with it by calls like this in a plugin file

WPRouting_Route::get('hello', function(){
    echo "Hello there";
})->withTitle('Hello there!')
  ->withTemplate(array(
    'sample-page',
    dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sample-page.php'
    );

I’ve been thinking about the second plugin function and came to the conclusion that it will stay in the same field since it’s up to the developer to decide:

  • which routes to use (first plugin function)
  • which of those routes will generate a route page

A first review

I can describe my first implementation of the route page creation process just by pasting some test code

   public function it_should_create_a_route_page_if_a_persisted_route_meta_info_is_in_the_db(UITester $I)
{
    // Prepare
    // arrays are stored serialized in wp db
    $routeMeta = serialize(array('route-one' => array('title' => 'Route one', 'permalink' => 'route-one', 'generate' => 'page')));
    $I->haveOptionInDatabase(RoutePages_GeneratingRoute::OPTION_ID, $routeMeta);

    // Execute
    $I->loginAsAdmin();
    $I->amOnPluginsPage();
    $I->activatePlugin('wp-router');
    $I->activatePlugin('route-pages');

    // Verify
    $I->amOnPagesPage();
    $I->see('Route one', '.page-title');
} 

What’s happening is that activating the plugin will trigger route page creation; creating route pages at plugin activation time seemed like a good idea except that there are situations and setups where activation/deactivation hooks might not get called.
I will add a filter-based route page creation system allowing developers wishing to generate route pages to:

  1. set a route as a page generating one
  2. trigger route page creation returning truthy or falsy values in a filter hook

The filter

The route generation is happening if truthy values are returned in the RoutePages_should_generate_route_pages filter. The idea is that I might install the plugin to use WP Router methods wrapping alone and not be interested in route pages generation at all; it works like a dead man trigger.
The workflow to use the route page generation is to add a route and setting it to persist in a plugin or theme

// plugin.php

WPRouting_Route::get('my-route', function(){
    echo "This is my route";
})->withTitle('Not a page')
  ->withTemplate(array(
    'sample-page',
    dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sample-page.php'
    )->shouldBePersisted();

and then hook into the filter to generate the route pages and return truthy values

add_filter(RoutePages::SHOULD_GENERATE_ROUTE_POSTS, '__return_true');

Next

I’m staying with this mechanic and will be testing ti further to spot shortcomings and pitfalls and will be removing UI for the route pages not to give the user a wrong idea about his possibilities.