Example Plugins

From Habari Project

Jump to: navigation, search

If you want to know more about plugin creation, see Dev:Creating A Plugin

A Basic Filter Plugin

All plugins in Habari must extend the core Plugin class. This allows the system to easily find and register your plugin. The following code creates a plugin called MyPlugin.

// First you need to create a plugin class
class MyPlugin extends Plugin // always extend Plugin (for plugins)
{
 
	/** Now, it comes the hard part
	 * Habari has two key features for using their API
	 * One is an Action and is called when events happen.
	 * The second is a Filter which is used to change data.
	 *
	 * Within a plugin you can specify functions with
	 * action_ or filter_ in the begining of the function name
	 * and if the action or filter exists it will be called
	 * by Habari.
	 */
 
	/**
	 * Filter "theme_act_display_post" is called when a post is viewed
	 */
	public function filter_theme_act_display_post( $handled, $theme )
	{
		// This is the Slug of the post being viewed.
		$post_slug = $theme->matched_rule->named_arg_values['slug'];
 
		/**
		 * Now you can use that slug to go and find the
		 * Post ID using the Post::get() function like..
		 */
		$post = Post::get(array('slug' => $post_slug));
 
		/**
		 * Now you have all the post information.
		 * $post->id will be the ID. You can use that
		 * for creating a statistics database.
		 */
 
		/**
		 * Also, in this case, you must return false
		 * If you return true, Habari will think that this Plugin
		 * Just handled the request and won't actually show the
		 * post.
		 */
		return false;
	}
 
	/**
	 * Filter "theme_act_display_page" is called when a page is viewed
	 */
	public function filter_theme_act_display_page( $handled, $theme )
	{
		return false;
	}
}

You also need to create a myplugin.plugin.xml file to describe the plugin. See the example here.

Custom Post Type Plugin

<?php
 
class Projects extends Plugin
{
 
	/**
	 * On plugin activation
	 */
	public function action_plugin_activation( $file )
	{
		// Don't process other plugins
		if ( Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__) ) {
 
			// Insert new post content types
			Post::add_new_type( 'project', true );
 
			// Create new rewrite rule for showing projects
			$rule = RewriteRule::create_url_rule('"project"/{$slug}', 'UserThemeHandler', 'display_project');
			$rule->parse_regex = '%project/(?P<slug>[^/]+)/?$%i';
			$rule->build_str   = 'project/{$slug}';
			$rule->description = 'Simple Project Management';
			$rule->insert();
		}
	}
 
 
	/**
	 * Manage Projects
	 */
	public function action_form_publish( $form, $post )
	{
		if ( $form->content_type->value == Post::type( 'project' ) ) {
 
			// Add project settings fields
			$settings = $form->publish_controls->append('fieldset', 'projectSettings', _t('Project Settings'));
 
			// Add version entry
			$settings->append('text', 'version', 'null:null', _t('Version'), 'tabcontrol_text');
			$settings->version->value = $post->info->version;
 
			// Add license entry
			$settings->append('text', 'license', 'null:null', _t('License'), 'tabcontrol_text');
			$settings->license->value = $post->info->license;
		}
	}
 
 
	/**
	 * Now we need to save our custom entries
	 */
	public function action_publish_post( $post, $form )
	{
		if ( $post->content_type == Post::type( 'project' ) ) {
 
			// Save settings
			$post->info->version = $form->version->value;
			$post->info->license = $form->license->value;
			// No, it really is that easy to save data
		}
	}
 
 
	/**
	 * Handle displays
	 */
	public function filter_theme_act_display_project($handled, $theme)
	{
		/**
		 * Tell Habari which files are to be used,
		 * we attempt to get any project theme file first.
		 * if that fails we goto single and then multiple
		 * THESE files are searched in your current theme DIRECTORY
		 */
		$paramarray['fallback'] = array(
		 'project.{$id}', //match project.234.php , where 234 is the id of the post
		 'project.{$slug}', //match project.my-project.php, where my-project is the slug of the post
		 'project.tag.{$posttag}', //match project.tag1.php, project.tag2.php...where tag1,tag2... are post's tag
		 'project.single', //match project.single.php
		 'project.multiple', //match project.multiple.php
		 'single', //single.php
		 'multiple', //multiple.php
		);
 
		// This is like Post::get().. Get one row, one item
		$paramarray['user_filters'] = array(
		 'fetch_fn' => 'get_row',
		 'limit' => 1,
		);
 
		return $theme->act_display( $paramarray );
	}
}
 
?>

You also need to create a myplugin.plugin.xml file to describe the plugin. See the example here.

To show project information, that you have added in "action_form_publish()", inside the theme files(e.g. project.single.php), use "echo $post->info->license" or, better, see the Custom Content Type Guide

Personal tools
This is a cached copy of the requested page, and may not be up to date.

Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.

(Cannot contact the database server: Access denied for user 'habari'@'localhost' to database 'habari_wiki' (localhost))


You can try searching via Google in the meantime.
Note that their indexes of our content may be out of date.