Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ProcessWire gravatar
<?php
/**
* Basic shell of a module for generating Gravatar URLs for ProcessWire users.
*
* Mostly based on official implementation guidance here:
* https://en.gravatar.com/site/implement/images/php/
*
*
* Use it like this:
*
* Get URL only with all defaults:
* $user->gravatar()
*
* Get image tag with different size:
* $user->gravatar(array('img' => true, 's' => 200));
*
*/
class Gravatar extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Gravatar',
'version' => 1,
'summary' => 'Gravatar hook for users',
'singular' => true,
'autoload' => true,
'icon' => 'smile-o',
);
}
public function init() {
$this->addHook('User::gravatar', $this, 'methodGravatar');
}
public function methodGravatar($event) {
$u = $event->object;
if ( ! $u instanceof User) $event->return = false; return;
if ( ! $u->email) $event->return = false; return;
$params = ($event->arguments(0) ? $event->arguments(0) : array());
// Default options
$defaults = array(
's' => 100,
'd' => 'retro',
'r' => 'g',
'img' => false,
'attrs' => array(),
);
$opts = array_merge($defaults, $params);
extract($opts);
$url = '//www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($u->email)));
$url .= "?s=$s&amp;d=$d&amp;r=$r";
if ($img) {
$url = '<img src="' . $url . '"';
foreach ($attrs as $key => $val ) {
$url .= ' ' . $key . '="' . $val . '"';
}
$url .= ' />';
}
$event->return = $url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.