Recent Topics

[PLUGIN] Author Gravatar - v0.1.1b - Includes comments hack

Started by on Feb 24, 2005 – Contents updated: Feb 24, 2005

Feb 24, 2005 13:08    

Example of this plugin & hack at work can be seen at http://blogs.mikkysworld.com/index.php/blurb/2005/02/24/gravatars_now_active

This is my first plugin, current version is v0.1.1b -- developed using b2evo 9.0.10

This will put your gravatar (http://www.gravatar.com) onto all blog posts. I've got a todo list already, any other features anyone can suggest and I'll try to implement them. Ideally I want all the settings to be configured from the admin page, they are currently hardcoded.

This is a plugin & hack combination. The plugin will render gravatars onto blog posts. The hack will render gravatars onto user comments.

/plugins/renderer/_gravatar.renderer.php

<?php
/*
 * Plugin Name: Author Gravatar
 * Version: 0.1.1b
 *
 * Author: Mike Thomas (mikkyT) - http://mikkysworld.com
 *
 * Thanks to Ed Bennett - http://wonderwinds.com whose Author Avatar plugin
 * I used as a basis for this one.
 *
 * Filename: plugins/renderers/_gravatar.renderer.php
 *
 * Change log:
 *
 * 0.1.1b - 20050225
 * Instead of creating the gravatar url in author_gravatar, we are now calling
 * gravatar() which is contained in hacks.php.
 *
 * hacks.php should be contained along with this release, if you do not have it
 * try visiting http://forums.b2evolution.net/viewtopic.php?t=3312
 *
 * 0.1a - 20050224
 * Initial alpha release
 *
 * BEFORE YOU USE THIS PLUGIN: Scroll down a wee bit and set your parameters
 * described at the beginning of function author_gravatar().
 *
 * TODO:
 * 1/ Work out how to get the parameters out of the function below and into the database.
 * 2/ Create/append/hack in a settings page to configure the above mentioned parameters.
 *
 */
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

/**
 * Includes:
 */
require_once dirname(__FILE__).'/../renderer.class.php';

/**
 * @package plugins
 */
class b2gravatar_Rendererplugin extends RendererPlugin
{
   var $code = 'b2gravatar';
   var $name = 'Author Gravatar';
   var $priority = 125;
   var $apply_when = 'opt-out';
   var $apply_to_html = true;
   var $apply_to_xml = false;  // strip the BBcode
   var $short_desc = 'inserts a gravatar in your blog posts';
   var $long_desc = 'This checks http://www.gravatar.com for the Gravatar registered against your email address and inserts it in the post.  If no gravatar is found, a defualt image will be used instead.';

   /**
    * Constructor
   *
    * {@internal b2gravatar_Rendererplugin::b2gravatar_Rendererplugin(-)}}
    */
   function b2gravatar_Rendererplugin()
   {
      $this->short_desc = T_('inserts an avatar in your blog posts');
      $this->long_desc = T_('This checks http://www.gravatar.com for the Gravatar registered against your email address and inserts it in the post.  If no gravatar is found, a defualt image will be used instead.');
   }

   function render( & $content, $format )
   {
      if( ! parent::render( $content, $format ) )
      {   // We cannot render the required format
         return false;
      }
      $content = author_gravatar($content);
      return true;
   }

}

function author_gravatar($text)
{
   global $Item, $DB, $tableusers;

/************************** Edit These Settings ***************************/
   // $default_image_path must be the full absolute URL to the image you
   // will use if a gravatar cannot be located.
   $default_image_path = "http://your_domain_name.xyz/whatever/path/media/default.jpg";

   // $size = An optional "size" parameter may follow that specifies the
   // desired width and height of the gravatar. Valid values are from 1 to
   // 80 inclusive. Any size other than 80 will cause the original gravatar
   // image to be downsampled using bicubic resampling before output.
   $size = "80";

   // An optional "rating" parameter may follow with a value of
   // [ G | PG | R | X ] that determines the highest rating (inclusive) that
   // will be returned.
   $rating = "G";

   // An optional "border" parameter may follow that specifies the
   // hexadecimal value of a 1px border to be overlaid on the gravatar.
   // The supplied value may be either the full six character hex string
   // (e.g. FF0000 for red) or the abbreviated three character hex
   // string (e.g. F00 for red).
   $border = "";
   
   // rightmargin and leftmargin exist in rsc/img.css, or create your
   // own class in your css
   $class = "rightmargin";
/************************ END: Edit These Settings *************************/

   $author_email = $Item->Author->get('email');

   $grav_url = gravatar($author_email, $default_image_path, $size, $rating, $border);

   $img_alt_text = $Item->Author->get('preferedname');
   
   $content = '<img src="'.$grav_url;
   $content .= '" alt="'.$img_alt_text;
   $content .= '" title="'.$img_alt_text;
   $content .= '" class="'.$class;
   $content .= '" />';
   $content .= "\n";

   $text = $content.$text;

   return $text;
} // end function author_gravatar

// Register the plugin:
$this->register( new b2gravatar_Rendererplugin() );

?>

/conf/hacks.php

ADD (or create the file)

<?php
/*
 * hacks.php
 */

/*
 * gravatar()
 *
 * This function returns the full gravatar url given an email address.
 * Specifying optional parameters will change the look of the returned
 * gravatar
 *
 * See http://www.gravatar.com/implement for information on each
 * parameter.
 *
 */
function gravatar( $email, $default_image_path = false, $size = false, $rating = false, $border = false )
{
   $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email);
   if($default_image_path && $default_image_path!= '')
      $grav_url = $grav_url . "&amp;default=".urlencode($default_image_path);
   if($size && $size != '')
      $grav_url = $grav_url . "&amp;size=".$size;
   if($border && $border != '')
      $grav_url = $grav_url . "&amp;border=".$border;
   if($rating && $rating != '')
      $grav_url = $grav_url . "&amp;rating=".$rating;
   return $grav_url;
}

?>

/skins/_feedback.php

BEFORE

<?php $Comment->content() ?>

ADD

<?php
            if($Comment->author_User == NULL)
               $grav_url = gravatar( $Comment->author_email, '', '20');
            else
               $grav_url = gravatar( $Comment->author_User->get('email'), '', '20');
            ?>
            <img src="<?php echo T_($grav_url); ?>" alt_text="<?php $Comment->author();?>" title="<?php $Comment->author();?>" class="rightmargin" /> 

Feb 24, 2005 14:08

Looks good, I'll be using it once you get comments working. I did look into this at one point, but I thought it would be a little silly just to have it for me and not everyone else... Maybe the wordpress plugin will be of some help?

http://www.gravatar.com/implement.php#section_2_2

edit: One other thing I just thought of, I'd prefer not to have a default image if there is no gravatar - perhaps and option to turn it on and off if the user doesn't have one?

Feb 24, 2005 14:28

It would be fairly straightforward to hack the _class_comments.php to include the gravatar url ... but I want to implement it as a plugin rather than a hack for ease of upgrade.

I don't think it's going to be possible unless either:

1) I upgrade to 0.9.2 and get an event added for when a comment is displayed, allowing me to hook in the gravatar url.

or

2) Get coments to go through renderer in the same way that original blog posts go through the renderer.

Feb 24, 2005 14:57

There is a hack that allows smilies in comments, so that must go through the smilie renderer.

Edit: Just had a bit of a search for it, but it seems like it went down when the forums were hacked.

Feb 24, 2005 15:11

Just had a look at the wordpress plugin, and I think it could be made to work in the hacks.php file, thus preserving it during upgrades. I shall have a play a bit later, unless you wish to do it?

Feb 24, 2005 15:22

Sticking it in hacks.php would still require a one line change to the comments ... looks like at the moment that's the best way forward.

Hopefully though, the plugin will 'just work(tm)' when and if comments go through the renderer plugin.

I've got the latest source from CVS so I may play about with that when I get some time. Right now I had better get back to work, i've extended my lunch break too long :)

I'll post up the hack for comment gravatars shortly.

Feb 24, 2005 15:31

That was back in September! Since no one knows when a next release will happen I think you should look into making it work completely with the software as-is. Any registered member can use the Avatar plugin assuming the admin gave them upload permission. This Gravatar plugin is going to be of the greatest benefit to non-members who are leaving comments, so to have it not work with comments means . . . what?

Using conf/hacks.php is perfectly cool and very likely to survive upgrades, depending on exactly what you put in conf/hacks. In other words if you call a function from conf/hacks.php that gets changed in the next release then your mod won't work. I've got a couple of functions in my conf/hacks that I already know won't fly when 0.9.whatever comes out. Oh well eh?

If it can't be done in conf/hacks then do it by hacking files! No biggie, since anyone using hacks does so knowing it is not part of the core code and therefore not automagically certainly absolutely going to survive the next release.

Feb 24, 2005 15:52

Ok, it seems like I've got to wait for my avatar to be approved, so does anyone want to PM me an email address that has a gravatar attached so I can have a play with this locally? I think I've got a quick and dirty hack to get this to work, but I'm not sure at the moment. Need to test it!

Feb 24, 2005 16:43

EdB wrote:

That was back in September! Since no one knows when a next release will happen I think you should look into making it work completely with the software as-is. Any registered member can use the Avatar plugin assuming the admin gave them upload permission. This Gravatar plugin is going to be of the greatest benefit to non-members who are leaving comments, so to have it not work with comments means . . . what?

Very good points... I've gotten the next version of the plugin already to put into 0.9.2 - unfortunately comment rendering still isn't in the CVS version so it looks like I'll be placing that onto the back burner for now.

A hack/plugin combo seems the best way forward. I already have the hacks.php function sorted out, so give me a brief while to test this.

Feb 24, 2005 16:55

Dammit, I did one too! All I did was take the WP plugin and changed the comment author to work with b2evo. That way classes and image size, border, default image etc can be changed on a per skin basis.

I'm interested to see how yours works. Could you PM me your gravatar email address so I can test mine too?

Feb 24, 2005 17:35

Add this function to your hacks.php


<?php
/*
 * hacks.php
 */

/*
 * gravatar()
 *
 * This function returns the full gravatar url given an email address
 * Settings for gravatars are hardcoded, you will need to edit them
 */
function gravatar( $email, $default_image_path = false, $size = false, $rating = false, $border = false )
{
	$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email);
	if($default_image_path && $default_image_path!= '')
		$grav_url = $grav_url . "&amp;default=".urlencode($default_image_path);
	if($size && $size != '')
		$grav_url = $grav_url . "&amp;size=".$size;
	if($border && $border != '')
		$grav_url = $grav_url . "&amp;border=".$border;
	if($rating && $rating != '')
		$grav_url = $grav_url . "&amp;rating=".$rating;
	return $grav_url;
}

 ?>

To add onto all comments (skin independant) edit _class_comments.php, function content()

		if($this->author_User == NULL)
			$comment = '<img src="' . gravatar( $this->author_email, '', '20') . '" alt_text="' . $this->author . '" title="' . $this->author.'" class="rightmargin" />' . $comment;
		else
			$comment = '<img src="' . gravatar( $this->author_User->get('email'), '', '20') . '" class="rightmargin" />' . $comment;

OR, to add to a skin, you want to edit _lastcomments.php for the skin of your choice

Feb 24, 2005 17:58

What about adding it to _feedback.php? That would be better than hard coding it into the core, so it will survive upgrades. And it can then be enabled and configured on a per skin basis.

To put this into yourskins/yourskinname/ _feedback.php, you'll need to first copy the contents of skins/_feedback.php if you have a link back to the main handler. Many skins already have this though.

Anyway, in your new (or existing) _feedback.php file, stick the following wherever your gravatar should go:

<img src="<?php gravatar( $Comment->author_email, '', '20');?>" alt_text="<?php $Comment->author();?>" title="<?php $Comment->author();?>" class="rightmargin" />"

If you want to include a default image for those who don't have a gravatar, put the full url in between the first two commas (eg. http://yourserver/default.jpg). To change the size, change the '20' to whatever you want.

Thanks again for a great hack.

Feb 24, 2005 18:10

Don't worry, I've already done it, just writing it up, with a couple of usage instructions for the less technically able. Thanks for a great hack! Just hope they approve my image soon...

Edited post above with instructions for putting it in your _feedback.php

Feb 24, 2005 18:17

Just noticed that my gravatar wasn't working for logged in users posting comments... will that feedback.php have the same problem?

Feb 24, 2005 18:58

Cheers for the pointers Graham...

I've updated the _feedback.php code so it looks like:

				<?php 
				if($Comment->author_User == NULL)
					$grav_url = gravatar( $Comment->author_email, '', '20');
				else
					$grav_url = gravatar( $Comment->author_User->get('email'), '', '20');
				?>
				<img src="<?php echo T_($grav_url); ?>" alt_text="<?php $Comment->author();?>" title="<?php $Comment->author();?>" class="rightmargin" />

I put this before:

				<?php $Comment->content() ?>

Feb 25, 2005 10:24

Okay, plugin is now at version 0.1.1b. I've editted the first post to include full details for installing plugin and hack.

Next stop, gravatars on trackback posts!!!

Feb 25, 2005 10:39

Okay now I'm slightly confused as to why Grahams trackback post didn't have a gravatar, unless he left the email field blank...

Feb 25, 2005 10:39

How are you going to get them on trackbacks? The user's email address isn't sent, is it? I thought only the permalink address got sent. Tackback one of my posts, and I'll test this. I did an internal TB, but it might just be my funky setup. Got a fair few hacks running on here. But nothing near the comments and trackbacks, I think...

Feb 25, 2005 10:48

Trackback.php inserts a blank email address, I don't think trackbacks support sending of email. Nevermind.

$comment_author = $blog_name;
	$comment_author_email = '';
	$comment_author_url = $url;

	$query = "INSERT INTO $tablecomments( comment_post_ID, comment_type, comment_author, etc etc etc 

This blog stuffs all new to me. I think I should revolutionise the protocol :D

Feb 25, 2005 12:32

Well, you're not doing too badly for a n00b ;) A plugin and a hack, and you've only made 17 posts... I've been here since the beginning, and I've never really posted anything other than simple hacks... Plugins confuse me at the moment :oops:

Feb 25, 2005 13:02

Plugins will be so much easier when 0.9.2 is released! They are based on OO architecture and are event driven, making everything fit neatly into place. Well, for me anyway.

I'm a Symbian C++ software engineer in my day job, so it's not really difficult for me to take a look at other languages/scripting tools. I always say that concepts are far more important than learning the semantics of a language. Once you have design and coding concepts under your belt, you can apply them to anything you want to craft (broadly speaking), be it a java applet or a php web script.

Mar 09, 2005 23:46

Woot, excellent, thanks! Just what i've been after!!!! :D

Mar 13, 2005 18:06

Alrighty, I have a query. I've modified the coding and created the hacks.php file, but I get this error when trying to post a comment on my blog:

Warning: Cannot modify header information - headers already sent by (output started at /home/rd1701uk/public_html/blog/conf/hacks.php:26)

Any suggestions? I'm using the latest version of B2evolution

May 13, 2005 15:45


<?php
function gravatar( $email, $default_image_path = false, $size = false, $rating = false, $border = false )
{
   $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email);
   if($default_image_path && $default_image_path!= '')
      $grav_url = $grav_url . "&amp;default=".urlencode($default_image_path);
   if($size && $size != '')
      $grav_url = $grav_url . "&amp;size=".$size;
   if($border && $border != '')
      $grav_url = $grav_url . "&amp;border=".$border;
   if($rating && $rating != '')
      $grav_url = $grav_url . "&amp;rating=".$rating;
   return $grav_url;
}
?>

Where do I set the rating and the url for default avatar?

May 16, 2005 20:45

gravatar( $Comment->author_email, 'default image path', '','rating here');

Where you do this will depend on what part you're modifying. Since you haven't said I'll say to change that in _feedback.php. You'll need to change that in two different places. They're right next to each other, so don't worry.

May 16, 2005 22:43

thank you :) I noticed that and got it working, but i thought that maybe there was a way to set it only once on hacks.php

Thank you again :)

May 17, 2005 00:35

You could change the function in hacks to have a default value (replace false) for $rating, that way you wouldnt need to modify it elsewhere...

May 17, 2005 00:54

mikkyT wrote:

You could change the function in hacks to have a default value (replace false) for $rating, that way you wouldnt need to modify it elsewhere...

can't I set a default avatar as well in hacks.php? or is rating the only thing I can set there? Because I keep trying but they don't seem to work.

Other than this the hack works well :) great work

Nov 08, 2005 21:20

Just upgraded to b2evolution 0.9.1 and this plugin applies without modifications.

Nov 17, 2005 05:33

mikkyT wrote:

Example of this plugin & hack at work can be seen at http://blogs.mikkysworld.com/index.php/blurb/2005/02/24/gravatars_now_active

This is my first plugin, current version is v0.1.1b -- developed using b2evo 9.0.10

This will put your gravatar (http://www.gravatar.com) onto all blog posts. I've got a todo list already, any other features anyone can suggest and I'll try to implement them. Ideally I want all the settings to be configured from the admin page, they are currently hardcoded.

This is a plugin & hack combination. The plugin will render gravatars onto blog posts. The hack will render gravatars onto user comments.

/plugins/renderer/_gravatar.renderer.php

<?php
/*
 * Plugin Name: Author Gravatar
 * Version: 0.1.1b
 *
 * Author: Mike Thomas (mikkyT) - http://mikkysworld.com
 *
 * Thanks to Ed Bennett - http://wonderwinds.com whose Author Avatar plugin
 * I used as a basis for this one.
 *
 * Filename: plugins/renderers/_gravatar.renderer.php
 *
 * Change log:
 *
 * 0.1.1b - 20050225
 * Instead of creating the gravatar url in author_gravatar, we are now calling
 * gravatar() which is contained in hacks.php.
 *
 * hacks.php should be contained along with this release, if you do not have it
 * try visiting http://forums.b2evolution.net/viewtopic.php?t=3312
 *
 * 0.1a - 20050224
 * Initial alpha release
 *
 * BEFORE YOU USE THIS PLUGIN: Scroll down a wee bit and set your parameters
 * described at the beginning of function author_gravatar().
 *
 * TODO:
 * 1/ Work out how to get the parameters out of the function below and into the database.
 * 2/ Create/append/hack in a settings page to configure the above mentioned parameters.
 *
 */
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

/**
 * Includes:
 */
require_once dirname(__FILE__).'/../renderer.class.php';

/**
 * @package plugins
 */
class b2gravatar_Rendererplugin extends RendererPlugin
{
   var $code = 'b2gravatar';
   var $name = 'Author Gravatar';
   var $priority = 125;
   var $apply_when = 'opt-out';
   var $apply_to_html = true;
   var $apply_to_xml = false;  // strip the BBcode
   var $short_desc = 'inserts a gravatar in your blog posts';
   var $long_desc = 'This checks http://www.gravatar.com for the Gravatar registered against your email address and inserts it in the post.  If no gravatar is found, a defualt epulseone online classifieds image will be used instead.';

   /**
    * Constructor
   *
    * {@internal b2gravatar_Rendererplugin::b2gravatar_Rendererplugin(-)}}
    */
   function b2gravatar_Rendererplugin()
   {
      $this->short_desc = T_('inserts an avatar in your blog posts');
      $this->long_desc = T_('This checks http://www.gravatar.com for the Gravatar registered against your email address and inserts it in the post.  If no gravatar is found, a defualt image will be used instead.');
   }

   function render( & $content, $format )
   {
      if( ! parent::render( $content, $format ) )
      {   // We cannot render the required format
         return false;
      }
      $content = author_gravatar($content);
      return true;
   }

}

function author_gravatar($text)
{
   global $Item, $DB, $tableusers;

/************************** Edit These Settings ***************************/
   // $default_image_path must be the full absolute URL to the image you
   // will use if a gravatar cannot be located.
   $default_image_path = "http://your_domain_name.xyz/whatever/path/media/default.jpg";

   // $size = An optional "size" parameter may follow that specifies the
   // desired width and height of the gravatar. Valid values are from 1 to
   // 80 inclusive. Any size other than 80 will cause the original gravatar
   // image to be downsampled using bicubic resampling before output.
   $size = "80";

   // An optional "rating" parameter may follow with a value of
   // [ G | PG | R | X ] that determines the highest rating (inclusive) that
   // will be returned.
   $rating = "G";

   // An optional "border" parameter may follow that specifies the
   // hexadecimal value of a 1px border to be overlaid on the gravatar.
   // The supplied value may be either the full six character hex string
   // (e.g. FF0000 for red) or the abbreviated three character hex
   // string (e.g. F00 for red).
   $border = "";
   
   // rightmargin and leftmargin exist in rsc/img.css, or create your
   // own class in your css
   $class = "rightmargin";
/************************ END: Edit These Settings *************************/

   $author_email = $Item->Author->get('email');

   $grav_url = gravatar($author_email, $default_image_path, $size, $rating, $border);

   $img_alt_text = $Item->Author->get('preferedname');
   
   $content = '<img src="'.$grav_url;
   $content .= '" alt="'.$img_alt_text;
   $content .= '" title="'.$img_alt_text;
   $content .= '" class="'.$class;
   $content .= '" />';
   $content .= "\n";

   $text = $content.$text;

   return $text;
} // end function author_gravatar

// Register the plugin:
$this->register( new b2gravatar_Rendererplugin() );

?>

/conf/hacks.php

ADD (or create the file)

<?php
/*
 * hacks.php
 */

/*
 * gravatar()
 *
 * This function returns the full gravatar url given an email address.
 * Specifying optional parameters will change the look of the returned
 * gravatar
 *
 * See http://www.gravatar.com/implement for information on each
 * parameter.
 *
 */
function gravatar( $email, $default_image_path = false, $size = false, $rating = false, $border = false )
{
   $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email);
   if($default_image_path && $default_image_path!= '')
      $grav_url = $grav_url . "&amp;default=".urlencode($default_image_path);
   if($size && $size != '')
      $grav_url = $grav_url . "&amp;size=".$size;
   if($border && $border != '')
      $grav_url = $grav_url . "&amp;border=".$border;
   if($rating && $rating != '')
      $grav_url = $grav_url . "&amp;rating=".$rating;
   return $grav_url;
}

?>

/skins/_feedback.php

BEFORE

<?php $Comment->content() ?>

ADD

<?php
            if($Comment->author_User == NULL)
               $grav_url = gravatar( $Comment->author_email, '', '20');
            else
               $grav_url = gravatar( $Comment->author_User->get('email'), '', '20');
            ?>
            <img src="<?php echo T_($grav_url); ?>" alt_text="<?php $Comment->author();?>" title="<?php $Comment->author();?>" class="rightmargin" /> 

online classifieds www.epulseone.com online classifieds online realestate

Nov 17, 2005 19:23

The spammers are resorting to posting here now that they cant get into my blog any more!? (No spam in the last 6 days!)

Jul 31, 2006 20:21

mikkyT wrote:

Just noticed that my gravatar wasn't working for logged in users posting comments... will that feedback.php have the same problem?

I have been changing your code from the one Graham wrote on the other side, but I can't get the members who is logged in, to show (the gravatars ;-)

Any idea what i have done wrong?


Form is loading...

b2evolution CCMS – This forum is powered by b2evolution CMS, a complete engine for your website.