Skip to content

Instantly share code, notes, and snippets.

@craigrodway
Last active February 22, 2017 15:38
Show Gist options
  • Select an option

  • Save craigrodway/2a0877fc0bd9016ed0f7 to your computer and use it in GitHub Desktop.

Select an option

Save craigrodway/2a0877fc0bd9016ed0f7 to your computer and use it in GitHub Desktop.

Revisions

  1. Craig A Rodway revised this gist Aug 12, 2015. 1 changed file with 23 additions and 10 deletions.
    33 changes: 23 additions & 10 deletions Gravatar.module
    Original file line number Diff line number Diff line change
    @@ -2,31 +2,43 @@

    /**
    * 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->template->name !== 'user') return;
    if ( ! $u->email) return;
    if ( ! $u instanceof User) $event->return = false; return;
    if ( ! $u->email) $event->return = false; return;

    $params = ($event->arguments(0) ? $event->arguments(0) : array());

    @@ -56,4 +68,5 @@

    $event->return = $url;
    }


    }
  2. craigrodway revised this gist Oct 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Gravatar.module
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    /**
    * Basic shell of a module for generating Gravat URLs for ProcessWire users.
    * 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/
  3. craigrodway created this gist Oct 18, 2014.
    59 changes: 59 additions & 0 deletions Gravatar.module
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?php

    /**
    * Basic shell of a module for generating Gravat 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));
    *
    */

    public function init() {
    $this->addHook('User::gravatar', $this, 'methodGravatar');
    }


    public function methodGravatar($event) {

    $u = $event->object;
    if ($u->template->name !== 'user') return;
    if ( ! $u->email) 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;
    }