> Read the accompanying readme.txt file for instructions and documentation.
=>> Also, visit the plugin's homepage for additional information and updates.
=>> Or visit: http://wordpress.org/extend/plugins/author-images/
*/
/*
Copyright (c) 2005-2010 by Scott Reilly (aka coffee2code)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
if ( !class_exists( 'c2c_AuthorImages' ) ) :
require_once( 'c2c-plugin.php' );
class c2c_AuthorImages extends C2C_Plugin_012 {
/**
* Constructor
*/
function c2c_AuthorImages() {
$this->C2C_Plugin_012( '3.0', 'author-images', 'c2c', __FILE__, array() );
}
/**
* Initializes the plugin's config data array.
*
* @return void
*/
function load_config() {
$this->name = __( 'Author Images', $this->textdomain );
$this->menu_name = __( 'Author Images', $this->textdomain );
$this->config = array(
'show_name' => array( 'input' => 'checkbox', 'default' => true,
'label' => __( 'Show author name as well?', $this->textdomain ),
'help' => __( 'Should the author\'s name be shown in addition to the author\'s image?', $this->textdomain ) ),
'show_name_if_no_image' => array( 'input' => 'checkbox', 'default' => true,
'label' => __( 'Show author name if no image is found?', $this->textdomain ),
'help' => __( 'Should the author\'s name be shown if the author\'s image can\'t be found?', $this->textdomain ) ),
'link_type' => array( 'input' => 'select', 'default' => 'posts',
'options' => array( 'posts', 'site', 'none' ),
'label' => __( 'Image linking', $this->textdomain ),
'help' => __( 'How should the author\'s image be linked?
posts : link to the archive of the author\'s posts
site : link to the author\'s website (if defined for the author)
none : don\'t link the author image', $this->textdomain ) ),
'width' => array( 'input' => 'short_text',
'label' => __( 'Image width', $this->textdomain ),
'help' => __( 'Specify this to force the browser to scale the width of the image to this size. If blank, then the image\'s original width will be left intact (or scaled in proportion to a specified height).', $this->textdomain ) ),
'height' => array( 'input' => 'short_text',
'label' => __( 'Image height', $this->textdomain ),
'help' => __( 'Specify this to force the browser to scale the height of the image to this size. If blank, then the image\'s original height will be left intact (or scaled in proportion to a specified width).', $this->textdomain ) ),
'image_extensions' => array( 'input' => 'text', 'default' => 'png gif jpg',
'label' => __( 'Supported image extensions', $this->textdomain ),
'help' => __( 'Space-separated list of possible image extensions for image. More efficient if you only include extensions you\'ll actually use.', $this->textdomain ) ),
'image_dir' => array( 'input' => 'text', 'default' => 'wp-content/images/authors/',
'label' => __( 'Image directory', $this->textdomain ),
'help' => __( 'Directory, relative to the root of your site, containing the author images.', $this->textdomain ) ),
'custom_field' => array( 'input' => 'text', 'default' => 'author_image',
'label' => __( 'Custom field name for overriding author image', $this->textdomain ),
'help' => __( 'If this custom field is specified for a post, its value will be given preference as the potential author image, should the image file exist.', $this->textdomain ) )
);
}
function register_filters() {
add_action( 'contextual_help', array( &$this, 'contextual_help' ), 10, 3 );
}
function contextual_help( $contextual_help, $screen_id, $screen ) {
if ( $screen_id != $this->options_page )
return $contextual_help;
$help_url = admin_url( "plugin-install.php?tab=plugin-information&plugin={$this->id_base}&TB_iframe=true&width=640&height=656" );
echo '' . __( 'Click for more help.', $this->textdomain ) . '';
return;
echo <<
Template functions
There are two template functions made available by this plugins.
c2c_the_author_image( \$before = '', \$after = '', \$image_dir = '' )
A drop-in replacement for WordPress's the_author() , allowing the author for the post to have an image displayed in lieu of the name (if an image can be found).
function c2c_wp_list_authors_images( \$args = '' )
A drop-in replacement for WordPress's wp_list_authors(), allowing all authors for a blog to be listed with an image (if present).
Arguments:
- \$before
- Optional. The text and/or HTML to appear before the author image/text, if any such text would be returned..
- Default value:
''
- \$after
- Optional. The text and/or HTML to appear after the author image/text, if any such text would be returned.
- Default value:
''
- \$image_dir
- Optional. The directory, relative to the root of your blog, in which to find the author images. If not set, it defaults to the value configured via the plugin's admin options page.
- Default value:
''
- \$args
- Optional. An array of configuration options. All but the last two match up with the supported arguments of the
wp_list_authors() function.
- optioncount : default of false;
- exclude_admin : default of true; should the admin user be excluded from the listing?
- show_fullname : default of false; should the user's first and last name be shown?
- hide_empty : default of true; should authors who have not made any posts be excluded from the listings?
- feed : default of
''
- feed_image : default of
''
- echo : default of true; should the listing be echoed to the page?
- show_name : default of
'' which means it'll abide by the plugin's setting value; should the name of the author be shown in addition to the image?
- show_name_if_no_image : default of
'' which means it'll abide by the plugin's setting value; should the author's name be shown if the author doesn't have an image?
HTML;
}
/**
* Finds the URL for the author image associated with the specified author.
*
* @param int $author_id The author ID
* @param string $image_dir (optional) The directory containing the author images. If '', then use the value configured in the plugin's settings.
* @param bool $check_custom Should the post's custom field be checked for the custom field containing the filename of the author's image?
* @return string URL to author image
*/
function find_author_image( $author_id, $image_dir = '', $check_custom = false ) {
$options = $this->get_options();
$image_dir = '/' . trim( ( $image_dir ? $image_dir : $options['image_dir'] ), '/' ) . '/';
$img_path = get_option( 'siteurl' ) . $image_dir;
$image_extensions = explode( ' ', $options['image_extensions'] );
$output = '';
// Check to see if user specified image filename to use as author image
$custom = ( $check_custom && $options['custom_field'] ) ? trim( get_post_custom_keys( $options['custom_field'] ), '/' ) : '';
if ( !empty( $custom ) ) {
if ( file_exists( ABSPATH . $image_dir . $custom ) )
$output = $img_path . $custom;
else {
foreach ( $image_extensions as $image_extension ) {
if ( file_exists( ABSPATH . $image_dir . $custom . '.' . $image_extension ) ) {
$output = $img_path . $custom . '.' . $image_extension;
break;
}
}
}
}
// If no author image by this point, look for image file based on user login or user id
if ( empty( $output ) ) {
foreach ( $image_extensions as $image_extension ) {
$authordata = get_userdata( $author_id );
if ( file_exists(ABSPATH . $image_dir . $authordata->user_login . '.' . $image_extension) ) {
$output = get_option( 'siteurl' ) . $image_dir . $authordata->user_login . '.' . $image_extension;
break;
}
elseif ( file_exists( ABSPATH . $image_dir . $author_id . '.' . $image_extension ) ) {
$output = get_option( 'siteurl' ) . $image_dir . $author_id . '.' . $image_extension;
break;
}
}
}
return $output;
} // end function find_author_image()
/**
* Returns the formatted author image markup.
*
* @param string $before (optional) Text to display before the image. Default is ''.
* @param string $after (optional) Text to display after the image. Default is ''.
* @param string $image_dir (optional) The directory containing the author images. If '', then use the value configured in the plugin's settings.
* @param string $width (optional) The forced width of the image (will cause browser to resize if image is of different width). Leave blank to retain image's original width (or for the width to be scaled in proportion to a specified height). It is recommended that images exist at the desired size.
* @param string $height (optional) The forced height of the image (will cause browser to resize if image is of different height). Leave blank to retain image's original height (or for the height to be scaled in proportion to a specified width). It is recommended that images exist at the desired size.
* @return void (Text is echoed)
*/
function get_the_author_image( $before = '', $after = '', $image_dir = '', $width = '', $height = '' ) {
global $authordata;
$options = $this->get_options();
// Get the link to the image, if there is one
if ( empty($options['image_extensions']) )
$output = '';
else
$output = $this->find_author_image( $authordata->ID, $image_dir, true );
$author_name = get_the_author();
if ( $output ) {
$output = "
' . $output . '';
elseif ( ('site' == $options['link_type']) && $authordata->user_url )
$output = '' . $output . '';
return $before . $output . $after;
} // end function get_the_author_image()
}
$GLOBALS['c2c_author_images'] = new c2c_AuthorImages();
endif;
if ( !function_exists( 'c2c_get_the_author_image' ) ) :
/**
* Returns the formatted author image markup.
*
* @param string $before (optional) Text to display before the image. Default is ''.
* @param string $after (optional) Text to display after the image. Default is ''.
* @param string $image_dir (optional) The directory containing the author images. If '', then use the value configured in the plugin's settings.
* @param string $width (optional) The forced width of the image (will cause browser to resize if image is of different width). Leave blank to retain image's original width (or for the width to be scaled in proportion to a specified height). It is recommended that images exist at the desired size.
* @param string $height (optional) The forced height of the image (will cause browser to resize if image is of different height). Leave blank to retain image's original height (or for the height to be scaled in proportion to a specified width). It is recommended that images exist at the desired size.
* @return void (Text is echoed)
*/
function c2c_get_the_author_image( $before = '', $after = '', $image_dir = '', $width = '', $height = '' ) {
return $GLOBALS['c2c_author_images']->get_the_author_image( $before, $after, $image_dir, $width, $height );
}
add_filter( 'c2c_get_the_author_image', 'c2c_get_the_author_image', 10, 5 );
endif;
if ( !function_exists( 'c2c_the_author_image' ) ) :
/**
* Echoes the result of c2c_get_the_author_image()
*
* @param string $before (optional) Text to display before the image. Default is ''.
* @param string $after (optional) Text to display after the image. Default is ''.
* @param string $image_dir (optional) The directory containing the author images. If '', then use the value configured in the plugin's settings.
* @param string $width (optional) The forced width of the image (will cause browser to resize if image is of different width). Leave blank to retain image's original width (or for the width to be scaled in proportion to a specified height). It is recommended that images exist at the desired size.
* @param string $height (optional) The forced height of the image (will cause browser to resize if image is of different height). Leave blank to retain image's original height (or for the height to be scaled in proportion to a specified width). It is recommended that images exist at the desired size.
* @return void (Text is echoed)
*/
function c2c_the_author_image( $before = '', $after = '', $image_dir = '', $width = '', $height = '' ) {
echo c2c_get_the_author_image( $before, $after, $image_dir, $width, $height );
}
add_action( 'c2c_the_author_image', 'c2c_the_author_image', 10, 5 );
endif;
if ( !function_exists( 'c2c_wp_list_authors_images' ) ) :
/**
* List all the authors of the blog, with several options available.
*
*
* - optioncount (boolean) (false): Show the count in parenthesis next to the
* author's name.
* - exclude_admin (boolean) (true): Exclude the 'admin' user that is
* installed bydefault.
* - show_fullname (boolean) (false): Show their full names.
* - hide_empty (boolean) (true): Don't show authors without any posts.
* - feed (string) (''): If isn't empty, show links to author's feeds.
* - feed_image (string) (''): If isn't empty, use this image to link to
* feeds.
* - echo (boolean) (true): Set to false to return the output, instead of
* echoing.
* - style (string) ('list'): Whether to display list of authors in list form
* or as a string.
* - html (bool) (true): Whether to list the items in html for or plaintext.
*
* - show_name (bool): Whether to show the name along with author image
* - show_name_if_no_image (bool): Whether to show name if not author image
* was found for author
*
*
* @param array|string $args (optional) The arguments array
* @return null|string The output, if echo is set to false.
*/
function c2c_wp_list_authors_images( $args = '' ) {
global $wpdb, $c2c_author_images;
$options = $c2c_author_images->get_options();
if ( is_array($args) )
$r = &$args;
else
parse_str($args, $r);
$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true,
'show_name' => false, 'show_name_if_no_image' => false);
$r = wp_parse_args($args, $defaults);
extract($r, EXTR_SKIP);
$return = '';
/** @todo Move select to get_authors(). */
$users = get_users_of_blog();
$author_ids = array();
foreach ( (array) $users as $user )
$author_ids[] = $user->user_id;
if ( count($author_ids) > 0 ) {
$author_ids = implode(',', $author_ids );
$authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" );
} else {
$authors = array();
}
$author_count = array();
foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
$author_count[$row->post_author] = $row->count;
}
foreach ( (array) $authors as $author ) {
$link = '';
$author = get_userdata( $author->ID );
$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
$name = $author->display_name;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
$name = "$author->first_name $author->last_name";
if( !$html ) {
if ( $posts == 0 ) {
if ( ! $hide_empty )
$return .= $name . ', ';
} else
$return .= $name . ', ';
// No need to go further to process HTML.
continue;
}
// First determine what to show (author image or text of author name)
$image = $c2c_author_images->find_author_image($author->ID);
if (! empty($image) ) {
$image = "
";
if ( $show_name || (($show_name == '') && $options['show_name']) )
$image .= $name;
} else {
if ( $show_name_if_no_image || (($show_name_if_no_image == '') && $options['show_name_if_no_image']) )
$image = $name;
else
continue;
}
// Wrap $image in link, if applicable
if ( 'posts' == $options['link_type'] && $posts != 0 )
$link = 'display_name) ) . '">' . $image . '';
elseif ( ('site' == $options['link_type']) && $author->user_url )
$link = '' . $image . '';
if ( empty( $link ) )
$link = $image;
if ( !($posts == 0 && $hide_empty) && 'list' == $style )
$return .= '';
if ( $posts == 0 ) {
if ( $hide_empty )
$link = '';
} else {
if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';
if (empty($feed_image))
$link .= '(';
$link .= '';
else
$link .= $name;
$link .= '';
if ( empty($feed_image) )
$link .= ')';
}
if ( $optioncount )
$link .= ' (' . $posts . ')';
}
if ( $posts || ! $hide_empty )
$return .= $link . ( ( 'list' == $style ) ? '' : ', ' );
}
$return = trim($return, ', ');
if ( ! $echo )
return $return;
echo $return;
} // end function c2c_wp_list_authors_images()
add_action( 'c2c_wp_list_authors_images', 'c2c_wp_list_authors_images' );
endif;
?>