Vulnero

Seamless WordPress and Zend Framework Integration

The WordPress API is accessed mostly through a vast array of functions that exist in the global namespace. Vulnero only accesses these functions through the Vulnero_WordPress broker which distributes requests to the appropriate functions through a more object-oriented interface. The benefits of this include:

  1. Requests and responses can be mocked in a non-WordPress environment when executing cli-scripts or running unit tests.
  2. Parameters can be simplified. For example, callbacks can be automatically assigned to the bootstrap when hooking actions.
  3. When WordPress updates or deprecates parts of their API, it simplifies Vulnero updates by consolidating all of the calls to one consistent source.
  4. We can easily extend the API by adding additional functionality to the calls.

Vulnero_WordPress is instantiated during Vulnero's bootstrap. Obtaining it to make a request from a controller is rather simple:

class MyController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $wp = $this->getInvokeArg('bootstrap')
                   ->bootstrap('wordPress')
                   ->getResource('wordPress');
        $wp->addAction('wp_head', $this);
    }

    public function onWpHead()
    {
        echo '';
    }
}

Of course in most cases you shouldn't need to directly access the API directly. For example, the above code could easily be rewritten to leverage the headScript() view helper and would make more sense to a Zend Framework developer. There are, however, examples where accessing the WordPress API does make sense, such as for getting and setting custom persisted options to and from the wp_options table:

class MyController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $wp = $this->getInvokeArg('bootstrap')
                   ->bootstrap('wordPress')
                   ->getResource('wordPress');
        if (!$colors = $wp->getCustomOptions('my-colors')) {
            $colors = array('red', 'blue');
            $wp->setCustomOption('my-colors', $colors);
        }

        $this->view->colors = $colors;
    }
}

This example leverages WordPress' add_option, update_option and get_option global namespace functions through the Vulnero_WordPress broker. The biggest advantage here is that those options don't support non-scalar values like our PHP array. Vulnero_WordPress automatically serializes and deserializes non-scalar values, prefixes your settings with the plugin name and validates your calls throwing SPL exceptions for anything that might cause the API functions to fail (such as key names exceeding allowed length).

Vulnero_WordPress API Method Brokers

Vulnero Method Function Description
registerActivationHook register_activation_hook The register_activation_hook function registers a plugin function to be run when the plugin is activated. Vulnero already registers this if you implement a onPluginActivated method in your bootstrap.
addAction add_action Actions are triggered by the WordPress core when certain functionality occurs such as adding a post. For a complete list of actions, see the WordPress Codex.
addFilter add_filter Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. For a complete list of filters, see the WordPress Codex.
addFilter add_filter Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. For a complete list of filters, see the WordPress Codex.
getSidebar get_sidebar Renders a WordPress sidebar.
registerWidget register_widget Registers a Vulnero_Widget (extends WP_Widget) class with WordPress.
getBlogInfo get_bloginfo Retrieves information such as options or settings for your WordPress site. Many items are bootstrapped in your Zend_Config. For a complete list see the WordPress Codex.
getOption get_option A safe way of getting values for a named option from the options database table. If the desired option does not exist, or no value is associated with it, FALSE will be returned.
getThemeRoot get_theme_root Retrieve path to themes directory.
getTemplate get_template Retrieve name of the current theme.
getTags get_tags Retrieve an array of objects for each term in post_tag taxonomy.
getCategory get_category Retrieves category data given a category ID or category object.
getCategories get_categories Returns an array of category objects matching the query parameters.
getPostCategories wp_get_post_categories Retrieve the list of categories for a post.
getDatabase None Creates a PDO database connection to the WordPress database using the DB_* constants.
locateTemplate locate_template Retrieve the name of the highest priority template file that exists.
applyFilters apply_filters Call the functions added to a filter hook.
addMenuPage add_menu_page Adds a Vulnero_AdminPage object to the WordPress settings area as a top-level menu on the left bar.
addOptionsPage add_options_page Adds a Vulnero_AdminPage object to the WordPress settings area as a sub-level Settings menu on the left bar.
getCustomOption get_option Returns a custom plugin option in the wp_options table.
setCustomOption update_option, add_option Sets a custom plugin option in the wp_options table.