We have released the v1.1.5 version of WP Bones with several fixes, improvements and new features. In particular, we have re-designed the demo branch along with the master/boilerplate to start to develop a WordPress plugin. Don’t forget to have a look at the Wiki. We have also created a Discussion to contribute in a better way.

From a framework perspective, we decided to remove the Illuminate Eloquent ORM from the composer. In short, it won’t be installed by default anymore.
Anyway, you will be able to continue to use that out of the box. This means that WP Bones will continue to support the bootstrap of the Illuminate as the previous version.

We have also changed the boilerplate a bit in order to keep the code clean as much as possible.

Below, you’ll find all changes along with how to upgrade from a previous version.

Boilerplate

You will find a couple of new In the master/boilerplate. We have improved the following files:

  • index.php
  • bootstrap/autoload.php
  • bootstrap/plugin.php

index.php

The new index.php file is cleaner than the previous one

<?php

/**
 * Plugin Name: WP Kirk
 * Plugin URI: http://undolog.com
 * Description: WordPress plugin demo bases on WP Bones framework
 * Version: 1.0.0
 * Author: Giovambattista Fazioli
 * Author URI: http://undolog.com
 * Text Domain: wp-kirk
 * Domain Path: localization
 *
 */

if (!defined('ABSPATH')) {
    exit;
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/

require_once __DIR__ . '/bootstrap/autoload.php';

As you can see, now the only required line is the require_once __DIR__ . '/bootstrap/autoload.php';

In addition to that, now you will be able to get the Plugin instance by using a new static class. The most important changes are in the bootstrap/autoload.php file

<?php

if (! defined('ABSPATH')) {
    exit;
}

/*
|--------------------------------------------------------------------------
| Register The Composer Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/

require_once __DIR__ . '/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Plugin Static class
|--------------------------------------------------------------------------
|
| We will use this static class to keep global the plugin information
|
*/

final class WPKirk
{
    public const TEXTDOMAIN = 'wp-kirk';
    public static $plugin;
    public static $start;
}

WPKirk::$plugin = require_once __DIR__ . '/plugin.php';
WPKirk::$start = microtime(true);

// Commodity function to get the plugin instance
if (! function_exists('WPKirk')) {
    /**
     * Return the instance of plugin.
     *
     * @return Plugin
     */
    function WPKirk()
    {
        return WPKirk::$plugin;
    }
}

As you can see above, you’ll be able to use WPKirk static class to get the Plugin instance as well as the Plugin text-domain. Anyway, we left the ability to use the global function WPKirk() as a commodity as well as backward compatibility. In short

$instance = WPKirk::$plugin; // get the plugin instance
$instance = WPKirk(); // get the plugin instance

Remember that WPkirk will be your Plugin Namespace, for example WPMyPlugin

Bones – artisan – command

We have fixed, improved and added new features in the bones console command

First of all, we added the currently Namespace information along with a couple of new amazing commands: version, API and eloquent model.

Versioning, by SemVer

By using the version command, you may handle the Plugin version before releasing it.

php bones version --help 

readme.txt > 1.0.0 (Stable tag: 1.0.0)
index.php  > 1.0.0 ( * Version: 1.0.0)

Usage:
  version [plugin version]

Arguments:
  [plugin version]      The version of plugin. Examples: '1', '2.0', 'v1', 'v1.2', 'v1-beta.4', 'v1.3+007'
  [--major]     Increment the major.y.z of plugin.
  [--minor]     Increment the x.minor.z of plugin.
  [--patch]     Increment the x.y.patch of plugin.

You may use the Semantic Version SemVer standard to create patches, minor revisions, release candidate versions, and so on.

Rest API

We started to implement a new way to use the WordPress API. You will find all info here.
The goal was to be able to create a Rest API in minutes. You may use the folder filesystem to organise your vendor, version and route. That’s an amazing way to manage the WordPress API.

For example, to create a simple API you have to create just a file

/api/vendor/v1/route.php
use WPKirk\WPBones\Routing\API\Route;

Route::get('/example', function () {
    return 'Hello World!';
});

That’s all. Of course, you may use the php bones api:make command to create a Rest API Controller.

Custom Pages

We have also improved the Custom Page management. Now, you may use the new filesystem method by creating a simple file into the pages folder. For example, you may create a custom_page.php file in the pages folder

<?php

use WPKirk\WPBones\Routing\Pages\Support\Page;

class CustomPage extends Page
{
    public function title()
    {
        return "Hello, Custom Page!";
    }

    public function render()
    {
        return $this->plugin->view('dashboard.custom_page');
    }
}

And use the same method $plugin->getPageUrl() to get page link.

Database

Another important feature of this version is the ability to use a light version of DB and Model classes available in Laravel. If you can’t use the Illuminate ORM package, you may use the Bones Query Builder. That’s very similar to the Laravel ORM with some limitations. Anyway, you will be able to get any record from the database with a simple line of code:

use WPKirk\WPBones\Database\DB;

$users = DB::table('users')->get();

foreach ($users as $user) {
    echo $user->name;
}

Of course, you may use the Model pattern

<?php

namespace WPKirk\Http\Controllers;

use WPKirk\WPBones\Database\Model;

class Users extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';
}

That’s all.

Below, some useful links