<?php

/*

Plugin Name: Author Info Widget
Description: This Widget shows the "about me" text, gravatar and social network/contact links of an author of your blog. You can add this widget to sidebars on author relevant sections, i.e. pages, posts or author archives.
Plugin URI: http://dennishoppe.de/wordpress-plugins/author-info-widget
Version: 1.0.4
Author: Dennis Hoppe
Author URI: http://DennisHoppe.de

*/


If (!Class_Exists('widget_author_info')){
Class widget_author_info Extends WP_Widget {
  var $base_url;
  var $text_domain;
  
  Function widget_author_info(){
    // Get ready to translate
    $this->Load_TextDomain();
    
    // Setup the Widget data
    $this->WP_Widget ( False, $this->t('Author Info'),
                       Array('description' => $this->t('You can add this widget to sidebars on author relevant sections, i.e. pages, posts or author archives.')));
    $this->base_url = get_option('siteurl').'/'.Str_Replace("\\", '/', SubStr(RealPath(DirName(__FILE__)), Strlen(ABSPATH)));

    // Register as Widget
    Add_Action ('widgets_init', Array($this, 'Register'));
  }
  
  Function Register(){
    Register_Widget(get_class($this));
    Add_Action('wp_head', Array($this, 'Include_CSS'));
  }
  
  Function Load_TextDomain(){
    $this->text_domain = get_class($this);
    load_textdomain ($this->text_domain, DirName(__FILE__).'/language/'.get_locale().'.mo');
  }
  
  Function t ($text, $context = ''){
    // Translates the string $text with context $context
    If ($context == '')
      return __($text, $this->text_domain);
    Else
      return _x($text, $context, $this->text_domain);
  }
  
  Function Include_CSS(){
    Echo '<link rel="stylesheet" href="'.$this->base_url.'/widget-author-info.css" type="text/css" />';
  }
 
  Function widget ($args, $settings){
    If ($settings['only_logged_in'] && !is_user_logged_in())
      return False;
    
    If ($settings['title'] == '')
      $settings['title'] = $this->t('About the author');
    
    
    If ( $settings['author_id'] != '' ){
      $obj_author = get_userdata($settings['author_id']);
    }
    ElseIf ( is_author() ){
      Global $wp_query;
      $obj_author = $wp_query->get_queried_object();
    }
    ElseIf ( is_singular() ) {
      Global $post;
      $obj_author = get_userdata( $post->post_author );
    }
    Else {
      return False;
    }
    
    Echo $args['before_widget'];
          
      Echo $args['before_title'] . $settings['title'] . $args['after_title'];
      
      Echo '<p>'.sprintf($this->t('Author: <b>%s</b>'), $obj_author->display_name).'</p>';
      
      // Show gravatar
      If ($settings['show_avatar'] && IsSet ($obj_author->user_email))
        Echo get_avatar($obj_author->user_email, ($settings['avatar_size'] == '' ? 80 : IntVal($settings['avatar_size'])));
      
      // Show the profile text
      If (IsSet ($obj_author->description))
        Echo '<p class="author-description">'.nl2br($obj_author->description).'</p>';
      
      // Show contact links
      $contact_p = '';
      
      If (IsSet($settings['show_website']) && $settings['show_website'] == 'yes' && $obj_author->user_url != '')
        $contact_p .= '<span class="author-meta website"><a href="'.$obj_author->user_url.'"><img src="'.$this->base_url.'/website.gif" alt="Website" /></a></span>';
      
      If (IsSet($settings['show_jabber']) && $settings['show_jabber'] == 'yes' && IsSet ($obj_author->jabber))
        $contact_p .= '<span class="author-meta jabber"><a href="xmpp:'.$obj_author->jabber.'"><img src="'.$this->base_url.'/jabber.gif" alt="Jabber" /></a></span>';

      If (IsSet($settings['show_aim']) && $settings['show_aim'] == 'yes' && IsSet ($obj_author->aim))
        $contact_p .= '<span class="author-meta aim"><a href="aim:AddBuddy?ScreenName='.UrlEncode($obj_author->aim).'"><img src="'.$this->base_url.'/aim.gif" alt="AIM" /></a></span>';
      
      If (IsSet($settings['show_yim']) && $settings['show_yim'] == 'yes' && IsSet ($obj_author->yim))
        $contact_p .= '<span class="author-meta yim"><a href="http://profiles.yahoo.com/'.$obj_author->yim.'"><img src="'.$this->base_url.'/yim.gif" alt="Yahoo!" /></a></span>';
      
      If ($contact_p != '') Echo '<p class="author-contact">'.$contact_p.'</p>';
      
      // Show link to the authors posts
      $posts_link_caption = SPrintF($this->t('All posts by %s'), $obj_author->display_name);
      Echo '<p class="author-posts-link"><a href="'.get_author_posts_url($obj_author->ID).'" title="'.$posts_link_caption.'">'.$posts_link_caption.'</a></p>';      

    Echo $args['after_widget'];
  }
 
  Function form ($settings){
    // Show Form:
    ?>
    <p>
    <?php Echo $this->t('Title:') ?>
    <input type="text" name="<?php Echo $this->get_field_name('title')?>" value="<?php Echo $settings['title']?>" />
    </p>
    
    <p>
    <input type="checkbox" name="<?php Echo $this->get_field_name('only_logged_in')?>" value="yes" <?php Checked($settings['only_logged_in'], 'yes')?> />
    <?php Echo $this->t('Show this widget only to logged in users.')?>
    </p>
    
    <p>
    <?php Echo $this->t('Show the profile of this author:') ?>
    <select name="<?php Echo $this->get_field_name('author_id') ?>">
      <option value="" <?php Selected($settings['author_id'], '') ?>><?php Echo $this->t('Author of the current post/page') ?></option>
      <?php ForEach($this->get_authors() AS $author) : ?>
        <option value="<?php Echo $author->ID ?>" <?php Selected($settings['author_id'], $author->ID) ?>>
          <?php Echo HTMLSpecialChars($author->display_name) ?>
        </option>
      <?php EndForEach ?>
    </select>
    </p>
    
    <p>
    <input type="checkbox" name="<?php Echo $this->get_field_name('show_avatar')?>" value="yes" <?php Checked($settings['show_avatar'], 'yes')?> />
    <?php Echo $this->t('Show the authors avatar') ?>
    </p>

    <p>
    <?php Echo $this->t('Avatar size (one side):') ?>
    <input type="text" name="<?php Echo $this->get_field_name('avatar_size')?>" value="<?php Echo ($settings['avatar_size'] == '' ? 80 : $settings['avatar_size']) ?>" size="3" />
    px
    </p>

    <p>
    <input type="checkbox" name="<?php Echo $this->get_field_name('show_website')?>" value="yes" <?php Checked($settings['show_website'], 'yes')?> />
    <?php Echo $this->t('Show the authors url link')?>
    </p>
    
    <p>
    <input type="checkbox" name="<?php Echo $this->get_field_name('show_jabber')?>" value="yes" <?php Checked($settings['show_jabber'], 'yes')?> />
    <?php Echo $this->t('Show the authors jabber name')?>
    </p>
    
    <p>
    <input type="checkbox" name="<?php Echo $this->get_field_name('show_aim')?>" value="yes" <?php Checked($settings['show_aim'], 'yes')?> />
    <?php Echo $this->t('Show the authors AIM name')?>
    </p>
    
    <p>
    <input type="checkbox" name="<?php Echo $this->get_field_name('show_yim')?>" value="yes" <?php Checked($settings['show_yim'], 'yes')?> />
    <?php Echo $this->t('Show the authors YIM name')?>
    </p>
    
    <?php
  }
 
  Function update ($new_settings, $old_settings){
    return $new_settings;
  }
  
  Function get_authors(){    
    $arr_author = Array();
    
    ForEach ( (Array) get_author_user_ids() AS $author_id)
      $arr_author[] = get_userdata( $author_id );
    
    If (Empty($arr_author))
      return False;
    Else
      return $arr_author;
  }


} /* End of Class */
New widget_author_info();
} /* End of If-Class-Exists-Condition */
/* End of File */