Dewdrop: Part Two

At work, we have been working on a plugin tool called Dewdrop. You can find more information on it at GitHub. This is the second in a series of high-level tutorials on Dewdrop plugin development for WordPress.

After having installed and initialized Dewdrop in part one, as well as gone over the overall conceptualization of this plugin, we are going to continue with the first and most basic model being used — County.

County is one of the options that will be selected by a client to determine the total quote price. It will have a Name (name of county, pretty self explanatory) and a Quote Modifier, which will be a dollar amount by which the base quote price should increase (or decrease) for quotes in this county.

Step Zero: Base Plugin File

If you are at all familiar with WordPress plugin development (which you should be), you will know that there needs to be a file in the root directory of your plugin with some information in it. We will need to create it.

dewdropdemo.php
<?php

/**
 * Plugin Name: Dewdrop Demo Plugin
 * Description: This will demo all the things.
 * Version: 0.1
 */

This is the bare minimum you will need for any WordPress plugin. However, we need to add a few lines. The first is

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

This line will handle the autoloading of Dewdrop’s functions. The second chunk you will need to add is

use Dewdrop\Pimple;
$pimple = Pimple::getInstance();
$pimple['view'] = $pimple->share(
    function () {
        return new \Dewdrop\View\View();
    }
);
Pimple::getResource('admin')->registerComponentsInPath();

This wires in Pimple. Pimple is a basic dependency injection container implementation used essentially as a registry of application resources and services.

All together, the dewdropdemo.php file in the plugin root directory should look like this:

<?php

/**
 * Plugin Name: Dewdrop Demo Plugin
 * Description: This will demo all the things.
 * Version: 0.1
 */

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

use Dewdrop\Pimple;
$pimple = Pimple::getInstance();
$pimple['view'] = $pimple->share(
    function () {
        return new \Dewdrop\View\View();
    }
);
Pimple::getResource('admin')->registerComponentsInPath();

Step One: Initialize Dewdrop Admin Component and Model

$ ./vendor/bin/dewdrop gen-admin-component counties

This will create a few files and directories to work with the Counties admin component, in a structure like so:

admin/
└── counties
    ├── Component.php
    └── view-scripts
        ├── edit.phtml
        └── index.phtml

For the most part, this should be pretty straightforward. The next step is to generate the model class, which will also create a dbdeploy script to add the table for counties to the database.

$ ./vendor/bin/dewdrop gen-db-table counties

The pertenint structure now looks like this:

admin/
└── counties
    ├── Component.php
    └── view-scripts
        ├── edit.phtml
        └── index.phtml
db/
└── 00001-add-counties.sql
models/
├── Counties.php
└── metadata
www/

The next thing we will need to do is edit the generated SQL file for our Counties table in the database. Here, I’ve added the attributes for Name and Quote Modifier, as well as a few lines of dummy data. Note: There are also a few lines at the beginning of that SQL file you will need to remove before dbdeploy will work. By the time I’m done with it, this file will look like this:

CREATE TABLE counties (
    county_id INTEGER PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    quote_modifier DECIMAL(10,2) DEFAULT 0.00
) ENGINE=InnoDb CHARSET=utf8;

INSERT INTO counties (name, quote_modifier) VALUES ('Boone', 25.00);
INSERT INTO counties (name, quote_modifier) VALUES ('Cole', 50.00);
INSERT INTO counties (name, quote_modifier) VALUES ('Clay', 150.00);

You will now be able to deploy these changes to your database with dbdeploy. If there are any errors in the SQL syntax, they will appear here and your changes will not be affected.

$ ./vendor/bin/dewdrop dbdeploy

The next step is to activate the plugin — note, if you try to activate it after this step, you will get the following error:

Plugin could not be activated because it triggered a fatal error.
Fatal error: Class 'Dewdrop\Admin\ComponentAbstract' not found in /var/www/wp-content/plugins/dewdropdemo/admin/counties/Component.php on line 8

Let’s take a look at that file… You will see a line that looks something like this:

use Dewdrop\Admin\ComponentAbstract;

This will need to be changed to accurately reflect the correct location of this class, like so:

use Dewdrop\Admin\Component\ComponentAbstract;

Until the Admin Component Generator is fixed, you will need to add that directory to the path for every admin component you generate.

After you’ve changed that line,

Fixed by this patch.

You should now be able to activate your plugin and see ‘counties’ as a navigation option on the nav bar on the far left of the WordPress back end where you are developing.

One Comment

  1. Darby Felton

    January 22, 2015

    > This wires in Pimple, which is a tool that [[Help Brad or Darby I’m not sure what this is or why we’re using it.]]

    Pimple is a basic dependency injection container implementation used essentially as a registry of application resources and services.

Share Your Thoughts

Leave a Reply