1 (edited by 2013-01-30 12:46:26)

Topic: Plug-ins for Cutenews 1.5

The idea of plug-ins is that there are the functions hook() that pass the data meanings in the particular places of the original CuteNews code, and these data are handled by plug-ins' filters.

To show how the hooks work we can take for example the code for processing the news template from core.php:

    list($output, $news_arr) = hook('template_replacer_news_before', array($output, $news_arr));


Here the hook is called for the function "template_replacer_news before" the changes of the constructions like {title}, {author} and so on.
Explanations: The array $news_arr is the data with the meanings of the current news, $output is the content of tpl-template, for example the content of Default.tpl.

The called hook has a name "template_replacer_news_before". This name is registered when plug-in initialization and can be used for calling other plug-ins,
i.e. if one plug-in registered the name "template_replacer_news_before" and another one did the same then the function of the first plug-in will be called primarily.

Let's create the first plug-in.  Name it my_plugin.php and place in ./cdata/plugins directory.

Here are the the plug-in snippet:

<?php 

    /*
        Connect the hook "template_replacer_news_before" with the function 'my_plugin_trnewsb'.
        It means that while calling the hook ('template_replacer_news_before', $args) the function my_plugin_trnewsb() will be called that will have the parameter $data.
    */       

    add_hook('template_replacer_news_before', 'my_plugin_trnewsb');

    function my_plugin_trnewsb($data)
    {
        // Here the $data is the array, that's why the hook will return the same array 
        return $data;
    }      

?>


As you can see the mechanism is quite easy. You need to register the hook and write for it the appropriate handler function.

You shouldn't forget to do a 'return' of the data meanings (modified probably) or you can have an incorrect input.
Let's write the new field for templates, for example the field "{show_php_version}"

<?php 

    add_hook('template_replacer_news_before', 'my_plugin_trnewsb');
    function my_plugin_trnewsb($data)
    {
        // Unpack the data from the array
        list($output, $news) = $data;

        // Make a change of {show_php_version} to the meaning PHP_VERSION
        $output = str_replace('{show_php_version}', 'PHP Version is '.PHP_VERSION, $output);            

        // Return the data to the code
        return array($output, $news);
    }   

?>


Now add to the template code, for example to the Full Story the fragment {show_php_version}, then create news and see the PHP version data.

It's easy to make the additional menu in options with the help of plug-ins. Two hooks are responsible for this – "more_options" and "options_additional_actions".
The hook  "more_options" is necessary for adding the links to the section Options, the second one processes the incoming data on ?mod=options&action=options.

An example:
Create the file add_options.php in ./cdata/plugins:

 <?php 

    add_hook('more_options', 'aopt_menu');
    add_hook('options_additional_actions', 'aopt_handler');

    // The parameter $options is passed for this hook, we should add

    function aopt_menu($options)
    {

        $PHP_SELF = PHP_SELF;

        $options[] = array
        (
            'name'              => lang('My plugin!', 'options'),
            'url'               => "$PHP_SELF?mod=options&action=my_more_options",
            'access'            => ACL_LEVEL_ADMIN,
        );

        return $options;
    }

    // The data aren't passed, we should use the global variables
    function aopt_handler()
    {
        // Instead of the straight call $_GET you can use the function REQ
        if (REQ('action','GET') == 'my_more_options')
        msg('info', 'Hello!', lang("It's my FIRST plugin"));

    }

?>


Now in Options there is a new menu 'My plugin!'. While clicking to it we will see the message "It's my FIRST plugin".

Good luck with creating of plug-ins!

Best regards,
CN Support team