Skip to content

Instantly share code, notes, and snippets.

@craigrodway
Last active February 22, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save craigrodway/2a0877fc0bd9016ed0f7 to your computer and use it in GitHub Desktop.
Save craigrodway/2a0877fc0bd9016ed0f7 to your computer and use it in GitHub Desktop.
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