Skip to content
Tada Burke edited this page Jun 1, 2017 · 14 revisions

Bootstrapping

Wireframe Plugin uses the wireframe.php file as a bootstrapper where most of the setup happens. The wireframe.php file is split up into easy-to-read and well-documented sections:

💡 TIP
Wireframe Plugin's wireframe.php file can get large. We recommend using code folding in your IDE or Text Editor, then only work on the blocks you need at any given time. In case you're wondering, we use Sublime Text 3 with Seti_UI.

§ 01. Namespaces

Declare your PHP namespaces and any sub-namespace dependencies in all of your class files. In your wireframe.php file, you should also declare any sub-namespace dependencies you need. If you create or extend a class, don't forget to add your namespace dependencies and maybe recompile your autoloader for changes to take effect.

Example of the Wireframe Plugin namespace:

namespace MixaTheme\Wireframe\Plugin;
💡 TIP
PHP Namespaces requires PHP version 5.3.0 or above. Wireframe Plugin is only compatible with PHP 5.6 or above. PHP 7.x is recommended.
💡 TIP
If you need to use another namespace, don't forget to add an alias use MixaTheme\Wireframe\Plugin\<sub-namespace>\<Object> declaration within your files.

Top

§ 02. Access

No direct access to this file. Although this is a small line of code, this can get complex, depending on how you wish to perform your access checks. So we decided this deserves it's own section. Also, all files should be pre-screened in one way or another for security purposes.

Example to deny loading a file directly:

defined( 'ABSPATH' ) or die();

Top

§ 03. Functions

Some of your files and objects may use helper functions. You need to grab them so they're available throughout your development.

Example of the Wireframe Helpers section:

require_once WIREFRAME_PLUGIN_API . 'functions/functions-helpers.php';
require_once WIREFRAME_PLUGIN_API . 'functions/functions-views.php';

Top

§ 04. Objects

You now need to load any class files for your plugin. Use require_once() to load your dependencies 1-by-1. This is the default option because some Developers don't use Composer. You can also load classes via Composer's composer.json file. If you prefer autoloading and add new class files, you must re-compile composer.json`. Autoloading is the preferred option for loading your objects.

Example loading class files manually:

require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-module-abstract.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-container-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-container.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-enqueue-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-enqueue.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-language-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-language.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-plugin-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'core/core-plugin.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/notices/module-notices-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/notices/module-notices.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/admin/module-admin-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/admin/module-admin.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/cpt/module-cpt-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/cpt/module-cpt.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/dbtables/module-dbtables-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/dbtables/module-dbtables.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/options/module-options-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/options/module-options.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/settings/module-settings-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/settings/module-settings.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/shortcode/module-shortcode-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/shortcode/module-shortcode.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/taxonomy/module-taxonomy-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/taxonomy/module-taxonomy.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/ui/module-ui-interface.php';
require_once WIREFRAME_PLUGIN_OBJECTS . 'module/ui/module-ui.php';

Example loading class files with Autoloading:

require_once WIREFRAME_PLUGIN_DEV . 'vendor/autoload.php';
💡 TIP
How to re-compile your composer.json file for autoloading: composer dump-autoload -o

Top

§ 05. Container

Wireframe Plugin needs to wire objects to the Core_Container $_storage array. You must first declare a variable for your Core_Container object. Then, you can add services later using closure syntax.

Example declaration for your services container:

$wireframe_plugin_container = new Core_Container();

Top

§ 06. Configs

Your services objects need data, so you should grab them. You store your data in callable functions which return arrays of data. These functions are individual config files you then pass into your service objects as a parameter.

Example:

require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-language.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-notices.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-admin.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-dbtables.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-cpt.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-taxonomy.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-shortcode.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-options.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-settings.php';
require_once trailingslashit( WIREFRAME_PLUGIN_DIR . 'wireframe_cfg' ) . 'cfg-ui.php';

Top

§ 07. Services

Wireframe Plugin "services" are closures you register to be added to the (object) Core_Container $_storage array. The Core_Container holds all your services so you can have access to them later. Your Core_Plugin object uses these services to instantiate your dependency objects, with config data passed in (as a parameter).

Example of a Wireframe Plugin service with config data passed in:

$wireframe_plugin_container->language = function () {
    return new Core_Language( wireframe_plugin_cfg_language() );
};

What does this code do?

  1. Registers a language service with the Core_Container.
  2. Instantiates the Core_Language object.
  3. Pass-in config data from wireframe_plugin_cfg_language() function.
💡 TIP
Since PHP 5.3 you can use anonymous functions. Wireframe Plugin is not compatible with PHP 5.2. PHP 7.x is recommended.
💡 TIP
Since PHP 5.4 you can use array brackets [ ] but Wireframe Plugin uses array() downstream to better identify arrays or for search & replace situations.

Top

§ 08. Wireframe

You can now "wire" all your dependencies together (hence, the name, Wireframe) by instantiating your dependencies into the Core_Plugin object.

Example of multiple Wireframe Plugin services passed into the Core_Plugin object:

$wireframe_plugin = new Core_Plugin(
    $wireframe_plugin_container->language,
    $wireframe_plugin_container->admin,
    $wireframe_plugin_container->notices,
    $wireframe_plugin_container->cpt,
    $wireframe_plugin_container->taxonomy,
    $wireframe_plugin_container->shortcode,
    $wireframe_plugin_container->options,
    $wireframe_plugin_container->settings,
    $wireframe_plugin_container->ui,
    $wireframe_plugin_container->dbtables
);

What does this code do?

  1. Instantiates the Core_Plugin object.
  2. Pass-in services objects as dependencies.
💡 TIP
You might habitually add a comma at the end of array arguments, but do not add a trailing comma on the last parameter of an object.

Top

§ 09. Housekeeping

Check if Wireframe Plugin is properly initialized. You can perform any clean-up tasks here, or simply output a warning if $wireframe_plugin fails. Your files now have access to objects beyond this point.

Example error notice hook if initialization fails:

add_action( 'admin_notices', array( $wireframe_plugin_container->notices, 'error_init' ), 10, 0 );

Top

§ 10. Hooks

After your objects are initialized, you can target any public object method you need using hooks. You generally do not need to run hooks via the __construct() magic method in your objects, since you can always manually fire hooks like so...

Example of the Wireframe Plugin Hooks section with array callable syntax:

register_deactivation_hook( __FILE__, array( $wireframe_plugin, 'deactivate' ) );

Top

💡 TIP
After you get the hang of Wireframe Plugin, you'll probably want to condense things. Your custom plugin may or may not need all the docblock comments we provided. You may also wish to put all of your includes into a single file, etc. We feel Wireframe Plugin can be more condensed to save on file size and file count, but we wanted to start you off with a structure we feel helps you the most!

Top

This Wiki will continue to be updated. Keep checking back!

Clone this wiki locally