plugin_url = plugins_url( '/', __FILE__ ); $this->plugin_path = plugin_dir_path( __FILE__ ); $this->load_language( 'wp-custom-author-image' ); add_action( 'plugins_loaded', array ( $this, 'init' ) ); $widget_ops = array( 'classname' => 'wp_custom_author_image', 'description' => __('Displays the post author\'s image', 'wp-custom-author-image'), ); parent::__construct('wp_custom_author_image', __('WP Custom Author Image', 'wp-custom-author-image'), $widget_ops); } # wp_custom_author_image() /** * init() * * @return void **/ function init() { if ( get_option('widget_wp_custom_author_image') === false ) { foreach ( array( 'wp_custom_author_image_widgets' => 'upgrade', ) as $ops => $method ) { if ( get_option($ops) !== false ) { $this->alt_option_name = $ops; add_filter('option_' . $ops, array(get_class($this), $method)); break; } } } // more stuff: register actions and filters add_action( 'widgets_init', array($this, 'widgets_init') ); if ( is_admin() ) { foreach ( array('profile', 'user-edit') as $hook ) { add_action("load-$hook.php", array($this, 'wp_custom_author_image_admin')); } } } # init() /** * wp_custom_author_image_admin() * * @return void **/ function wp_custom_author_image_admin() { include_once $this->plugin_path . '/wp-custom-author-image-admin.php'; } # wp_custom_author_image_admin() /** * widgets_init() * * @return void **/ function widgets_init() { register_widget('wp_custom_author_image'); } # widgets_init() /** * widget() * * @param array $args * @param array $instance * @return void **/ function widget($args, $instance) { if ( is_admin() ) return; extract($args, EXTR_SKIP); $instance = wp_parse_args($instance, wp_custom_author_image::defaults()); extract($instance, EXTR_SKIP); if ( $always ) { $author_id = wp_custom_author_image::get_single_id(); } elseif ( in_the_loop() ) { $author_id = get_the_author_meta('ID'); } elseif ( is_singular() ) { global $wp_the_query; $author_id = $wp_the_query->posts[0]->post_author; } elseif ( is_author() ) { global $wp_the_query; $author_id = $wp_the_query->get_queried_object_id(); } else { return; } if ( !$author_id ) return; $image = wp_custom_author_image::get($author_id, $instance, $width, $height); if ( !$image ) return; $desc = $bio ? trim(get_user_meta($author_id, 'description', true)) : false; $title = apply_filters('widget_title', $title); echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; echo $image . "\n"; if ( $desc ) echo apply_filters('wp_custom_author_image_bio', wpautop( $desc )); echo $after_widget; } # widget() /** * get_single_id() * * @return int $author_id **/ static function get_single_id() { $author_id = get_transient('wp_custom_author_image_cache'); if ( $author_id && !wp_custom_wp_custom_author_image_debug ) { return $author_id; } elseif ( $author_id === '' && !wp_custom_wp_custom_author_image_debug ) { return 0; } elseif ( !is_dir(WP_CONTENT_DIR . '/authors') ) { set_transient('wp_custom_author_image_cache', ''); return 0; } # try the site admin first $user = get_user_by('email', get_option('admin_email')); if ( $user && $user->ID && wp_custom_author_image::get($user->ID) ) { set_transient('wp_custom_author_image_cache', $user->ID); return $user->ID; } global $wpdb; $author_id = 0; $i = 0; do { $offset = $i * 10; $limit = ( $i + 1 ) * 10; $authors = $wpdb->get_results(" SELECT $wpdb->users.ID, $wpdb->users.user_login FROM $wpdb->users JOIN $wpdb->usermeta ON $wpdb->usermeta.user_id = $wpdb->users.ID AND $wpdb->usermeta.meta_key = '" . $wpdb->prefix . "capabilities' JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID GROUP BY $wpdb->users.ID ORDER BY $wpdb->users.ID LIMIT $offset, $limit "); if ( !$authors ) { set_transient('wp_custom_author_image_cache', ''); return 0; } foreach ( $authors as $author ) { if ( defined('GLOB_BRACE') ) { $wp_custom_author_image = glob(WP_CONTENT_DIR . '/wp-custom-authors/' . $author->user_login . '{,-*}.{jpg,jpeg,png}', GLOB_BRACE); } else { $wp_custom_author_image = glob(WP_CONTENT_DIR . '/wp-custom-authors/' . $author->user_login . '-*.jpg'); } if ( $wp_custom_author_image ) { $user = new WP_User($author->ID); if ( !$user->has_cap('publish_posts') && !$user->has_cap('publish_pages') ) continue; $author_id = $author->ID; set_transient('wp_custom_author_image_cache', $author_id); return $author_id; } } $i++; } while ( !$author_id ); set_transient('wp_custom_author_image_cache', ''); return 0; } # get_single_id() /** * get() * * @param bool $author_id * @param array $instance * @param int $width * @param int $height * @return string $image */ static function get($author_id = null, $instance = null, $width = null, $height = null) { if ( !$author_id ) { $author_id = wp_custom_author_image::get_author_id(); if (!$author_id) return ""; } $wp_custom_author_image = wp_custom_author_image::get_wp_custom_author_image($author_id, $width, $height); $instance = wp_parse_args($instance, wp_custom_author_image::defaults()); extract($instance, EXTR_SKIP); if ( $link ) { if ( !$always ) { $author_link = get_the_author_meta( 'wp_custom_aboutme_page', $author_id ); if ($author_link == '') $author_link = get_author_posts_url($author_id); } elseif ( get_option('show_on_front') != 'page' || !get_option('page_on_front') ) $author_link = user_trailingslashit(get_option('home')); elseif ( $post_id = get_option('page_for_posts') ) $author_link = apply_filters('the_permalink', get_permalink($post_id)); else $author_link = user_trailingslashit(get_option('home')); $wp_custom_author_image = '' . $wp_custom_author_image . ''; } return '
' . "\n"; } # get() /** * get_author_id() * * @return int $image */ static function get_author_id() { $author_id = null; if ( in_the_loop() ) { $author_id = get_the_author_meta('ID'); } elseif ( is_singular() ) { global $wp_the_query; $author_id = $wp_the_query->posts[0]->post_author; } elseif ( is_author() ) { global $wp_the_query; $author_id = $wp_the_query->get_queried_object_id(); }else{ $author_id = get_post_field( 'post_author', get_the_ID() ); } return $author_id; } #get_author_id() /** * get_wp_custom_author_image() * * @param int $author_id * @param null $width * @param null $height * @return string $image */ static function get_wp_custom_author_image($author_id, $width = null, $height = null, $avatar = false) { $wp_custom_author_image = wp_custom_author_image::get_wp_custom_author_image_url($author_id); if ( $wp_custom_author_image != '' ) { $author_name = wp_custom_author_image::get_author_name($author_id); $image_class = ''; if ($avatar) { $image_class = 'class="avatar photo"'; } if ( !empty($width) ) { if ( empty ($height) ) $height = $width; $wp_custom_author_image = '' . '' . '
' . "\n"; echo '' . '' . "\n"; echo '' . '
' . "\n" . '' . __('Leave width and height at 0 to use dimensions from the uploaded image itself.', 'wp-custom-author-image') . '
' . "\n"; echo '' . '' . '
' . "\n"; echo '' . '' . '
' . "\n"; echo '' . '' . '
' . "\n" . '' . __('Normally, this widget will only output something when in the loop or on singular posts or pages. Check the above checkbox if a single author has an image.', 'wp-custom-author-image') . '
' . "\n"; } # form() /** * defaults() * * @return array $instance **/ static function defaults() { return array( 'title' => '', 'bio' => false, 'link' => false, 'always' => false, 'widget_contexts' => array( 'search' => false, '404_error' => false, ), 'width' => 0, 'height' => 0, ); } # defaults() /** * get_meta() * * @param int $author_id * @return string $image **/ static function get_meta($author_id) { $user = get_userdata($author_id); $author_login = $user->user_login; // if ( empty( $user->user_login ) ) // return false; if ( defined('GLOB_BRACE') ) { if ( $wp_custom_author_image = glob(WP_CONTENT_DIR . '/wp-custom-authors/' . $author_login . '{,-*}.{jpg,jpeg,png}', GLOB_BRACE) ) $wp_custom_author_image = current($wp_custom_author_image); else $wp_custom_author_image = false; } else { if ( $wp_custom_author_image = glob(WP_CONTENT_DIR . '/wp-custom-authors/' . $author_login . '-*.jpg') ) $wp_custom_author_image = current($wp_custom_author_image); else $wp_custom_author_image = false; } if ( $wp_custom_author_image ) { $wp_custom_author_image = basename($wp_custom_author_image); if ( !get_transient('wp_custom_author_image_cache') ) { $user = new WP_User($author_id); if ( $user->has_cap('publish_posts') || $user->has_cap('publish_pages') ) set_transient('wp_custom_author_image_cache', $author_id); } } else { $wp_custom_author_image = ''; } update_user_meta($author_id, 'wp_custom_author_image', $wp_custom_author_image); return $wp_custom_author_image; } # get_meta() /** * get_author_name() * * @param int $author_id * @return string $author_name **/ static function get_author_name($author_id) { $user = get_userdata($author_id); $author_name = $user->display_name; return $author_name; } /** * upgrade() * * @param array $ops * @return array $ops **/ function upgrade($ops) { $widget_contexts = class_exists('widget_contexts') ? get_option('widget_contexts') : false; foreach ( $ops as $k => $o ) { if ( isset($widget_contexts['wp_custom_author_image-' . $k]) ) { $ops[$k]['widget_contexts'] = $widget_contexts['wp_custom_author_image-' . $k]; } } return $ops; } # upgrade() } # wp_custom_author_image $wp_custom_author_image = wp_custom_author_image::get_instance(); /** * the_wp_custom_author_image() * * This is the full html link wrapped in a div and echoed to the webpage * * @param int $author_id * @return void */ function the_wp_custom_author_image($author_id = null) { echo wp_custom_author_image::get($author_id, null); } # the_wp_custom_author_image() /** * get_wp_custom_author_image() * * This is the full html link wrapped in a div and returned for further processing by a theme or plugin * * @param int $author_id * @return string */ function get_wp_custom_author_image($author_id = null) { return wp_custom_author_image::get($author_id, null); } /** * the_wp_custom_author_image_size() * * This is the full html link wrapped in a div and echoed to the webpage. The output size is passed in. * * @param int $width * @param int $height * @param null $author_id * @return void */ function the_wp_custom_author_image_size($width, $height, $author_id = null) { echo wp_custom_author_image::get($author_id, null, $width, $height); } # the_wp_custom_author_image() /** * the_wp_custom_author_image_url() * * This simply returns the url for the given author * * @param null $author_id * @return string */ function the_wp_custom_author_image_url($author_id = null) { return wp_custom_author_image::get_wp_custom_author_image_url($author_id); } # the_wp_custom_author_image_url() /** * wp_custom_author_image_admin() * * @return void **/ /** * shortcode_callback() * * @return void **/ function shortcode_callback() { the_wp_custom_author_image(); } add_shortcode( 'wp-custom-author-image', 'shortcode_callback' ); if ( !function_exists( 'get_avatar' ) ) : /** * get_avatar() * * Cloned from WP core. Retrieve the avatar for a user who provided a user ID or email address. * * @param int|string|object $id_or_email A user ID, email address, or comment object * @param int $size Size of the avatar image * @param string $default URL to a default image to use if no avatar is available * @param string $alt Alternative text to use in image tag. Defaults to blank * @return string