Skip to content

Plugin Documentation

skoh-fley edited this page Oct 12, 2011 · 4 revisions

Plugin Documentation

Introduction

Welcome to the plugin system for Kure. This system allows users to activate and use plugins by simply dropping them into the plugins folder.

At its core, the system is actually pretty simple. The plugin file only needs to specify two things: the text to be inserted, and the position at which to insert it. The file can process whatever it wants before this (as if it were a normal PHP file), but instead of printing or echoing anything, it must assign all of the text it wishes to print to a single variable in the form of a string.

This variable is called the hook, and it's what allows plugins to operate without changing any existing code. All available hooks are assigned to the two-dimensional array $rack (as in, a coat rack full of "hooks"). The variable to use within this array depends on where the text needs to be placed.

The first thing to remember here is the format:

$rack['page']['position'] = "code";`

Hooks

Hooks are locations throughout the content of each page that allow external code to be inserted. These are the only places plugins can insert code into; that said, there aren't many other places you'd need.

All hooks are imaginarily held in the $rack[] array -- to display anything in any viable place on any page, you'll need to do it through this array. It has two dimensions; one for page (when viewing a post, when viewing the doc listing, homepage, etc.) and one for position (before the post title, after the post body, at the top of the page, etc.).

Each has a unique identifier -- for example, if you want to place something below the post body whenever a user is viewing a post individually (like if you were making a commenting plugin), you'd use

$rack['post']['body_after'] = "output text";`

where post is the page to display "output text" on, and body_after is the location.

Of course, you, the author of this commenting system example, would have to generate what text should be outputted -- are you storing comments in files? Then grab the comments from those files, figure out the final string you'd like to output, and assign it to the above variable. Always be sure to correctly set your form action to a page your plugin's being loaded on so you can display any output you need to.

Also, it's important to note that when you attach something to a hook, it's attached directly after the focus of the hook. There are no line breaks between the hook and what you hang on it, so remember to put them in where necessary -- the reason for this is to allow authors to add content onto the same line as their hook (i.e., a category image right after the title of a post).

As you can see, the whole system is based on a pretty simple concept that can be extended pretty far. And there's nothing stopping you from attaching a plugin to more than one hook -- say you wanted to enable comments for docs pages as well, or have them display below each individual post on the homepage -- this is very possible assuming you've researched the location of your hook and are ready to take advantage of the variables available to you.

Check out the HookList to see all hooks available for use, or read on to learn more about plugins.

Dynamic hooks

Dynamic hooks are subsidiaries of certain hooks that are processed more than once in a page execution. An example of such a hook is the $rack['posts']['post-title_after'] hook, which (along with several other hooks) is processed every time a post is displayed.

A hook like this is called dynamic because in addition to being processed as a normal hook, it creates several unique versions of itself to be processed one time each (in this case, for one post each). A new unique version is created every time the hook is processed.

These unique "spawns" of normal hooks can be used like any normal hook by a plugin. Using this method, it is possible for a plugin to print one thing for post x and something entirely different for post y, both on the same page, depending on how it decides to react to, say, the title of the post.

Currently, the only dynamic hooks available are on pages that print more than one post or doc at a time, so their identifier is created by md5 hashing the entry's title (the filename version; not the user-friendly one).

So, to use dynamic hooks (to see which hooks are dynamic, check the HookList), simply succeed the second dimension of the hook with a hash sign (#) followed by the md5 hash of the post or doc title you wish to plug into.

For example, to plug into the spot right below the title of a post called My first day at work, you would do

$title = "My first day at work";
$title = parse_title($title); // the title must NOT be the user-friendly version, otherwise the hashes won't always match
$title = md5($title);
$rack['posts']['post-title_after#' . $title] = "text to be displayed";

or, to be more compact:

$rack['posts']['post-title_after#' . md5(parse_title("My first day at work"))] = "text to be displayed";

Somewhere down the line the hook would end up looking like this:

$rack['posts']['post-title_after#e9d086e7ad99fcef5c166178d85155ba']

although it will probably never be seen that way. Whatever is assigned to this hook will only be printed below the title of the post entitled My first day at work, and nowhere else. This is the product of a dynamic hook.

Avoidance of multiple processings

Because of the way Kure processes plugins (once at the beginning of page execution to figure out what hooks each uses, and once more when execution reaches that hook), plugins may encounter errors or unwanted results by being processed twice or more (e.g. declaring the same function twice).

To avoid this, authors should check the boolean variable $GLOBAL['plugging'] to see if they are being processed via the plug() function (which is used at each hook encountered) or not. If $GLOBAL['plugging'] is true, then this is at least the second time the plugin is being processed (for plugins that use more than one hook, it may be the third, fourth, etc.). If it is false, it is the first time (the pre-hook process).

The $GLOBAL['plugging'] variable can also be used to speed up processing time (by preventing most of the execution during the first time through); the only thing that needs to happen during the first run-through is for the hooks being used to be initialized. The following example:

<?php

$html = "";
if($GLOBAL['plugging']) {
    // all processing goes here
    $html .= "<p>This post has been approved by me!</p><br />";
}
$rack['post']['title_after'] = $html;

?>

would work perfectly fine, and would conserve a decent amount of both processing power and time for larger plugins. If it's preferred to avoid the $GLOBAL['plugging'] variable, the following example would also be suitable, albeit a little less efficient:

<?php

$html = '';
$html .= '<p>This post has been approved by me!</p><br/>';
$rack['post']['title_after'] = $html;

?>

Just be sure to (re-)initialize your variables ($html = '';) so they don't carry over from the first processing of your plugin (or, possibly, from other plugins).

Listing a plugin on the administration interface

For the purpose of the administration interface, Kure supplies a hook that all plugin authors are encouraged to take advantage of: $rack['admplugins']['listing']. This hook will add whatever text is specified to the plugin menu of the administration interface.

Using this hook, plugin authors are able to provide a configuration menu or an about page for their plugin, such as the following example:

<?php

$rack['admplugins']['listing'] = "<a href=\"?plugin=test_plugin\">test_plugin</a> by AuthorName";

if($_GET['plugin'] == "test_plugin") {
    $rack['admplugins']['page'] = 'test_plugin was written by AuthorName for Kure 0.6 in May 2008 by AuthorName@example.com.';
}

?>

Storing configuration values

With the release of 0.6.2, plugins can use the functions set_config() and get_config() to assign and fetch values they wish to store, respectively.

set_config() takes two parameters: set_config($plugin, $config); $plugin is a short, filename-friendly name of your plugin; while $config is an array containing all your desired variables with variable names as keys and variable values as values.

An example of using this function would be the following:

$cfg = array("activated" => true, "debug" => false, "num_to_display" => 12, "feed_title" => "ExampleBlog RSS Feed");
set_config("rss", $cfg);

An important thing to note is to avoid using any variable named $config, as this is an array used by Kure to store its own configuration variables; overwriting it would not be good.

Another thing that shouldn't be overlooked is that set_config() overwrites any variables currently in that plugin's configuration file, including values that were not passed this time around. So, the following:

$cfg = array("activated" => true, "debug" => false, "num_to_display" => 12, "feed_title" => "ExampleBlog RSS Feed");
set_config("rss", $cfg);

$cfg2 = array("activated" => false);
set_config("rss", $cfg2); // after this call, the ONLY variable present anymore will be "activated"

would result in the deletion of the variables that were not passed to the function the second time. Each time you call set_config(), it's like wiping the slate clean -- treat it as if it were the first time it was ever called.

The get_config() function takes just one parameter: get_config($plugin); the plugin's filename-friendly name. This must be the same as the one used with set_config() to fetch the data correctly. get_config() will return the exact array that was last passed to set_config(), and nothing more.

Using the add_filter() function

Note: This function has not yet been introduced.

The add_filter() function allows plugin authors to define custom rules for parsing text. These rules will be applied to all displayed entry text.

To create these rules, you must define your own function to perform what you want to perform to a single string argument. Then pass the name of this function to Kure through the add_filter() function.

function i_dont_like_carrots($string) {
    return str_replace("carrot", "pear", $string);
}
add_filter("i_dont_like_carrots");

By default, any rules passed through this function will be applied both to entry titles and entry content. You can pass an optional second parameter to the function -- "t" for title only or "c" for content only -- to change this (note that passing "ct" or similar is the same as passing no second argument).

function i_dont_like_oranges_in_titles($string) {
    return str_replace("orange", "apple", $string);
}
function but_i_love_them_in_content($string) {
    return str_replace("orange", "ORANGES! YAY!", $string);
}
add_filter("i_dont_like_oranges_in_titles", "t");
add_filter("but_i_love_them_in_content", "c");

It is advised to define these functions and call the add_filter() function when $GLOBAL['plugging'] is false; that is, when the plugin is being processed before any output has been sent. This is to ensure the filters have been set by the time any post or doc output is generated.


Go ahead and start plugging, and don't hesitate to use the forums if you're stumped!