New Markup Plugin: Postscript

Creating and modifying plugins.
Post Reply
User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

New Markup Plugin: Postscript

Post by RobA » Wed Jun 22, 2005 5:40 pm

I was looking for a plugin to automatically add a disclaimer (or postscript, or sig) to the end of every article, similar to the closingwords plugin for Blosxom.

I couldn't find one in the archives, so a cobbled one together.

It provides an HTML area that can be used to create your postscript. It will append the postscript to the end of the extended body, or the body (if there is no extended body).

It also has an option (default) to only display the postscript when in single article view. Setting this to No will display the postscript after the article body when in multi-entry view as well. This is most useful if you never use the extended entry componenet, and want the full article with the postscript added on every page.

This doesn't play well with the simple cache (extended entry plugin), as the code to check for single entry view isn't supported by the cache. A better way to do this might just be in the Smarty templating, rather than a plugin. (watch the word wraps...)

Plugin code follows as an exercise in plugin writing more than anything :):

Code: Select all

<?php # $Id: serendipity_event_postscript.php,v 1.0 2005/06/21 Rob Antonishen 
/* By Rob Antonishen */
/* This plugin doesn't play very will with the extended entry properties cache unless the Single Entry Only option is set to No */
switch ($serendipity['lang']) {

    case 'en':
    default:
        @define('PLUGIN_EVENT_POSTSCRIPT_NAME', 'Markup: Postscript');
        @define('PLUGIN_EVENT_POSTSCRIPT_DESC', 'A postscript that will be added to the end of every article body or extended body (if there is one).');
        @define('PLUGIN_EVENT_POSTSCRIPT_TEXT', 'Postscript Content');
        @define('PLUGIN_EVENT_POSTSCRIPT_TEXT_BLAHBLAH', 'Enter the postscript here.  Markups may apply depending on order of plugins.');
        @define('PLUGIN_EVENT_POSTSCRIPT_SINGLE', 'Single Entry Only');
        @define('PLUGIN_EVENT_POSTSCRIPT_SINGLE_BLAHBLAH', 'Display postscript only on single entry view.  Setting to no will add postscript in multiple entry views.');

        break;
}

class serendipity_event_postscript extends serendipity_event
{
    var $title = PLUGIN_EVENT_POSTSCRIPT_NAME;

    function introspect(&$propbag)
    {
        global $serendipity;

        $propbag->add('name',          PLUGIN_EVENT_POSTSCRIPT_NAME);
        $propbag->add('description',   PLUGIN_EVENT_POSTSCRIPT_DESC);
        $propbag->add('stackable',     false);
        $propbag->add('author', 'Rob Antonishen');
        $propbag->add('version', '1.0');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('cachable_events', array('frontend_display' => true));
        $propbag->add('event_hooks',   array('frontend_display' => true));
        $propbag->add('configuration', array('postscript_text', 'postscript_single'));
    }

    function install() {
        serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
    }

    function uninstall() {
        serendipity_plugin_api::hook_event('backend_cache_purge', $this->title);
        serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
    }

    /* the standard thing */
    function generate_content(&$title) {
        $title = $this->title;
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
            case 'postscript_text':
                $propbag->add('type',        'html');
                $propbag->add('name',        PLUGIN_EVENT_POSTSCRIPT_TEXT);
                $propbag->add('description', PLUGIN_EVENT_POSTSCRIPT_TEXT_BLAHBLAH);
                $propbag->add('default',     '<p>©2005, John Smith</p>');
                break;

            case 'postscript_single':
                $propbag->add('type',        'boolean');
                $propbag->add('name',        PLUGIN_EVENT_POSTSCRIPT_SINGLE);
                $propbag->add('description', PLUGIN_EVENT_POSTSCRIPT_SINGLE_BLAHBLAH);
                $propbag->add('default',     'true');
                break;

            default:
                return false;

        }

        return true;
    }


    function event_hook($event, &$bag, &$eventData) {
        global $serendipity;

        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
            switch($event) {
                case 'frontend_display':

                    /* check if in single item view */
                    if ( (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])) || !serendipity_db_bool($this->get_config('postscript_single',true)) ){
                        if (!empty($eventData['extended'])) {
                            $eventData['extended'] = $eventData['extended'] . $this->get_config('postscript_text','');                       
                        }
                        else {
                            $eventData['body'] = $eventData['body'] . $this->get_config('postscript_text','');
                        }

                    }                  

                    return true;
                    break;

                default:
                    return false;
            }
        } else {
            return false;
        }
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>

Post Reply