Writing plugins

From AffiliateBang Wiki

(Redirected from Plugins)
Jump to: navigation, search

Plugins consist of three different types; Filters, Pages, and Midge extensions. By creating a plugin, you can add new features or significantly change current features. Plugins are implemented with a callback system that is mostly compatible with Wordpress plugins.

Contents

Creating a Plugin

A plugin is a PHP file placed inside of a sub-directory of the /plugins directory inside an AffiliateBang installation.

At the start of the plugin, you must place the following PHP code:

/*
Name: Plugin name
Description: Plugin description
*/

This lets AffiliateBang identify the theme from the plugins page. Additional comments or information can be placed inside the comment block if so desired.

Filter Plugins

Filter plugins are very similar to Wordpress plugins. They are registered through the use of the callback function add_function_filter(). Check the list of Hookable Functions to determine the appropriate place to hook into. Most functions are hookable, allowing you to significantly alter the behavior of any existing code.

add_function_filter( string $override_function, string $hook_function [, int $priority = 0, string $type = "filter"]);

override_function (string)
The AffiliateBang function being overridden.

hook_function (string)
The function being called after the overridden function completes. The hook function is a user
created function that accepts a single string argument containing the original output of the
hooked function.

priority (integer) optional
The order in which this function is called if there are multiple hooks to the same function. This
currently is not implemented

type (string)  optional
The type of this hook, it can either be "filter" or "add". If it is set to add, the return string
of the callback function will simply be added to end of the original function. If it is set to
filter. The return string of the callback function replaces the original output.


Filter example code

add_function_filter("the_content", "hook_demo");
function hook_demo($output) {
    return str_replace  ("apples","oranges",$output);
}

This example would hook into the_content function and replace all instances of apples to oranges.

Add example code

add_function_filter("the_content", "add_demo",0,"add");
function add_demo($output) {
    return "
Powered by AffiliateBang"; }

This example would put Powered by AffiliateBang at the bottom of the content.

Page Plugins

While filter plugins are useful for changing pre-existing behavior, it can be tricky to add brand new existing behavior with them. With the inclusion of pages, you can create alternate content that exists outside of any already, pre-existing content.

add_page(string page_id, string callback_function);

page_id (string)
The unique ID of the page you're registering.

callback_function
The function to call when a request for that page is generated. Thus function is to expect no arguments.

When http://yourstoreurl/?plugin=page_id is visited, callback_function is called to generate the information. Hooking into the_content function will allow you to show content where normal page content would be.

Simple example code

add_page("test","test");

function test() {
	print "Hello, ".$_GET['myname'];
	die();
}

When http://yourstoreurl/?plugin=test&myname=Sam is visited, the page is invoked and prints the message, then terminates the script so no further output is shown.

Advanced example code

add_page("time","the_time");
function the_time() {
   add_filter("the_content","show_the_time"); // Hook into the function that shows the main content area
} 

function show_the_time($content) {
    return "The time is ". strftime("%Y-%m-%d %H:%M:%S %Y");
}

This example registers a page then hooks into the_content function, which is the main area of the page. When the plugin's page is visited, it shows the time in the main area.

Midge Plugins

Midge handles the content of AffiliateBang, with plugins it's possible to override existing directives for Midge or create new ones.

add_midge(string directive_name, callback_function);

directive_name (string)
The name of the directive you're creating, if this is an already existing directive, then this will
override any calls to the built-in directive.

callback_function
The function to call when any requests for the directive are made. This function is to expect a
single variable containing an array with the arguments the directive was
called with, the first variable in the array will be the original directive called.

Example code

function sitemap_custom($midge) {
 	$rows = 3;
 	usort($GLOBALS['categories'], "cmpabc");
 	$counter = 0;
 	print "<table><tr>";
 	foreach($GLOBALS['categories'] as $category) {
 		if($category->parent != "") continue;
 			print '<td valign="top"><a href="'.buildPermCat($category->id).'">'.$category->name.'</a><br>';
 			foreach($GLOBALS['categories'] as $child) if ($child->parent == $category->id) 
 				print '<font size="-2"><a href="'.buildPermCat($child->id).'">'.$child->name.'</a></font> ';
 			print '</td>';
 		$counter++;
 		if (($counter % $rows) == 0) print '</tr><tr>';
 	}
 	print "</table>";
 
 }
 
 add_midge("sitemap", "sitemap_custom");

This is a bit more of an advanced plugin, demonstrating what can be done. Here we override the built-in sitemap directive and replace it with our own. Which goes through all the root categories and shows their children below.

Internal Variables

While being able to provide new information, it does help to be able to access already existing information from within AffiliateBang. There are a few important variables to remember while extending AffiliateBang.

Global variable 'content' contains the current XML response from Amazon processed by the SimpleXML handler built into PHP5.

Global variable 'pages' contains a list of all the non-plugin pages and content boxes stored in AffiliateBang, each page has the following structure:

class page {
	public $id; The unique identifier for this page
	public $name; The name of this page
	public $content; The URL/HTML/Midge content of this page
	public $top; Boolean value, if this page should be listed at the top of the theme
	public $side; Boolean value, if this page should be listed in the sidebar of the theme
	public $url_only; Boolean value, if this page has no content and is simply a link to an external source
	public $priority; Integer value, containing the priority of this page to be displayed
	public $post_parent; No internal use, used for Wordpress theme/plugin compatability
	public $ID; No internal use, used for Wordpress theme/plugin compatability
	public $embedded; Boolean value, Used to indicate if this is a stand-alone page, or a content box visible in the sidebars
	public $first; Boolean value, If embedded is set, this will show in the first sidebar called, otherwise it will appear in the second sidebar if it is available
	public $main; Boolean value, If embedded is set, this allows the content box to appear on the main page of your store
	public $cart; Boolean value, If embedded is set, this allows the content box to appear on shopping cart pages
	public $items; Boolean value, If embedded is set, this allows the content box to appear on item pages
	public $categories; Boolean value, If embedded is set, this allows the content box to appear on category pages
	public $filter;  If embedded is set, this is the filter string to determine where the content box will appear
}
Personal tools