Example Plugins
From Habari Project
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) { /** * return information about your plugin, this is required */ public function info() { return array( 'name' => 'My Plugin', 'version' => '1.0', 'url' => 'http://myplugin.rockhabari.com', // url to some information on your plugin 'author' => 'Sir TeLebing', 'authorurl' => 'http://mysite.tld', 'license' => 'Apache License', // any license, try to be compatible with Apache 'description' => 'My plugin rocks.' // what it does (a little more descriptive than this) ); } /* 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' => $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; } }
Custom Post Type Plugin
<?php class Projects extends Plugin { /** * Return plugin metadata for this plugin */ public function info() { return array( 'name' => 'Simple Project Management', 'version' => '1.0', 'url' => 'http://myurl.tld/my-project-page/', 'author' => 'Arthur Dent', 'authorurl' => 'http://myurl.tld', 'license' => 'Apache License', 'description' => 'A simple project management script' ); } /** * 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 */ $paramarray['fallback']= array( 'project.{$id}', 'project.{$slug}', 'project.tag.{$posttag}', 'project.single', 'project.multiple', 'single', 'multiple', ); // 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 ); } } ?>
