Last active
February 22, 2017 15:38
-
-
Save craigrodway/2a0877fc0bd9016ed0f7 to your computer and use it in GitHub Desktop.
ProcessWire gravatar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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&d=$d&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