Recent Topics

1 Jul 28, 2007 23:27    

I'm trying to create a rendering plugin which will add tags at the end of every post, based on the categories the post belongs to. For instance, if a post belongs to the category "Economy", the tag is of the form:

<a href="http://ww.blogalaxia.com/tags/economy" rel="tag">economy</a>

I tried doing this as a simple skin hack and was successful, but afterwards I realized that the Blogalaxia crawler only looks into the post content in the RSS feed. So I need to create the tag from RenderItemAsXml and RenderItemAsHtml hooks.

The code is this:

<?php if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );


/**
 * Blogalaxia_Tags Plugin
 *
 * This plugin responds to virtually all possible plugin events :P
 *
 * @package plugins
 */
class blogalaxia_tags extends Plugin
{
  /**
   * Variables below MUST be overriden by plugin implementations,
   * either in the subclass declaration or in the subclass constructor.
   */
  var $name = 'Blogalaxia Tags';
  var $code = 'b2BlgxTags';
  var $priority = 90;
  var $version = '1.9';
  var $group = 'rendering';
  var $author = 'Larry Nieves';
  var $help_url = '';  // empty URL defaults to manual wiki


  /*
   * These variables MAY be overriden.
   */
  var $apply_rendering = 'opt-in';
  var $number_of_installs = 1;


  /**
   * Init
   *
   * This gets called after a plugin has been registered/instantiated.
   */
  function PluginInit( & $params )
  {
    $this->short_desc = T_('Blogalaxia Tags');
    $this->long_desc = T_('It adds blogalaxia-type tags at end of post');
  }

  /* Define HTML Hook */
  function RenderItemAsHtml( & $params )
  {
    $params['data'] = $this->insert_blogalaxia_tags( & $params ) ;
    return true ;
  }

  /* Define XML Hooke. The same as before */
  function RenderItemAsXml ( & $params )
  {
    $this -> RenderItemAsHtml ( & $params) ;
    return true ;
  }

  /**
   * We trigger an extra event ourself (which we also provide ourselves).
   *
   *
   */
  function insert_blogalaxia_tags( & $params )
  {
    global $cache_postcats;
    $content = & $params['data'] ;
    $item = & $params['Item'] ;
    $base_url = 'http://www.blogalaxia.com/tags/' ;
    $img_path = dirname( __FILE__ ) . '/img/blogalaxia.gif' ;

    cat_load_postcats_cache();
    $categoryIDs = $cache_postcats[$item->ID];
    $taglinks = array();
       foreach( $categoryIDs as $cat_ID )
    {
      $cat = get_the_category_by_ID($cat_ID);
      $cat_name = format_to_output( $cat["cat_name"], $format );
      $tag = htmlentities( trim( $cat_name ), ENT_NOQUOTES ) ;
      // Keep only one char in entities!
      $tag = preg_replace( '/&(.).+?;/', '$1', $tag );
      // Substituye espacios por '+'
      $tag = strtolower( preg_replace( '/(\s)+/', '+', $tag ) ) ;
      $taglinks[] = '<a href="'. $base_url . $tag . '" rel="tag">' . $cat_name . '</a>' ;
    }
    $content .= '<div class="bg-tags"><img src="' ;
    $content .=  $img_path . ' alt="Etiquetas Blogalaxia" />&nbsp;' ;
    $content .= implode( ', ', $taglinks ) ;
    $content .= '</div>' ;
    return $content ;
  }
}

?>

This code I place in a file called _blogalaxia_tags.plugin.php inside the directory plugins/blogalaxia_tags_plugin/

I upload to the server and when I go to the Plugins Admin panel, the plugin doesn't appear among the "Available" plugins for installation. However, if I deliberately introduce a syntax error into the plugin code, for example, a missing ';' I get an error when trying to load the Plugins Admin panel.

So, the question is: is there any obvious error in the code above, that would produce such behaviour? I don't find any, but I'm not a very seasoned PHP programer. Thus the question.

My current B2evo version is 1.10.2.

By the way, part of the above code is borrowed from the function Item::categories() (you might recognize it).

Update: Silly me. The class definition has to be

class blogalaxia_tags_plugin extends Plugin

and not as it was above.

Now I have to see if it does what I want.


Form is loading...