2017 © Pedro Peláez
 

library component-hooks

Hook system helper for WordPress

image

flexpress/component-hooks

Hook system helper for WordPress

  • Wednesday, August 20, 2014
  • by timperry
  • Repository
  • 1 Watchers
  • 0 Stars
  • 94 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

FlexPress theme component

Install via pimple

Before we done anything else we need to setup the configs in pimple, (*1)

$pimple['configHookable'] = function() {
  return new Config();
};

$pimple['hooker'] = function ($c) {
  return new Hooker($c['objectStorage'], array(
    $c['configHookable']
  ));
};

What we have done here is created two configs, one for the hooker and one for a hookable class, we have then passed in the configHookable to the hooker so it can hook all its hooks up., (*2)

Note that that $c['objectStorage'] is the SPLObjectStorage class., (*3)

Setting up hooks

To add hook we must first create a class that uses the HookableTrait, lets make a very basic class that does this:, (*4)

class Config {

  use HookableTrait;

}

Simples, all done but we should probably add a hook method, so lets do that now:, (*5)

class Config {

  use HookableTrait;

  /**
   * @type action
   */
  public function adminInit() {
    echo "Hello, this is the admin hook being fired";
  }

}

This example adds a hook for the config class and the adminInit function for the action admin_init, not that the method is in camelcase., (*6)

As well as setting up actions you can add filters like this:, (*7)

class Config {

  use HookableTrait;

  /**
   * @type action
   */
  public function adminInit() {
    echo "Hello, this is the admin hook being fired";
  }

  /**
   * @type filter
   */
  public function theTitle($title) {
    return strip_tags($title);
  }

}

In this example we have added a filter that strips the tags from the title by hooking into the the_title filter., (*8)

So for filters you simply add this to the docblock above the method:, (*9)

/**
 * @type action
 */

and for filters you do the same but change the type to filter:, (*10)

/**
 * @type filter
 */

Advanced usage

As well as specifying the type of hook you can also specify the priority like this:, (*11)

/**
 * @type action
 * @priority 10
 */

And finally you can also specify the number of paramters you expect like this:, (*12)

/**
 * @type action
 * @priority 10
 * @params 3
 */

Which allows you to add both action and filter hooks, speficy the priority as well as the number of params you expect, so nothing is taken away from the add_action and add_filter functions., (*13)

Public methods - Hooker

  • hookUp() - Loops through all the hookables passed to the hooker and hooks them up.

Public methods - HookableTrait

  • hookUp() - Uses reflection to find all public methods with a valid docblock and hook them up.

Protected methods - HookableTrait

  • getMethodAttributes() - Gets all the attibutes of a methods docblock.
  • getHookName($methodName, $attributes) - For a given method name and attibutes, gets the hook name. e.g. theContent becomes the_content
  • registerHook($hook_name, $method_name, $attributes) - Actuallys registers the hook depending on what type it is.

The Versions

20/08 2014

dev-master

9999999-dev

Hook system helper for WordPress

  Sources   Download

13/08 2014

v1.0.0

1.0.0.0

Hook system helper for WordPress

  Sources   Download