2015-06-07 17:03:09 -04:00
< ? php
2016-04-01 20:18:20 -04:00
/**
* Class Display_Featured_Image_Genesis_Author_Widget
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
2016-07-03 07:42:41 -04:00
* @link https://robincornett.com
2017-02-17 09:43:46 -05:00
* @copyright 2014-2017 Robin Cornett Creative, LLC
2016-04-01 20:18:20 -04:00
*/
2015-06-07 17:03:09 -04:00
class Display_Featured_Image_Genesis_Author_Widget extends WP_Widget {
/**
2017-08-09 08:05:09 -04:00
* @var $form_class \DisplayFeaturedImageGenesisWidgets
2015-06-07 17:03:09 -04:00
*/
2017-08-09 08:05:09 -04:00
protected $form_class ;
2015-06-07 17:03:09 -04:00
/**
* Constructor. Set the default widget options and create widget.
*/
2017-08-09 08:05:09 -04:00
public function __construct () {
2015-06-07 17:03:09 -04:00
$widget_ops = array (
2016-03-25 13:26:44 -04:00
'classname' => 'author-profile' ,
'description' => __ ( 'Displays user profile block with Gravatar' , 'display-featured-image-genesis' ),
'customize_selective_refresh' => true ,
2015-06-07 17:03:09 -04:00
);
$control_ops = array (
'id_base' => 'featured-author' ,
'width' => 200 ,
'height' => 250 ,
);
parent :: __construct ( 'featured-author' , __ ( 'Display Featured Author Profile' , 'display-featured-image-genesis' ), $widget_ops , $control_ops );
}
2017-08-09 08:05:09 -04:00
/**
* Get the widget defaults.
*
* @return array
*/
public function defaults () {
return array (
'title' => '' ,
'show_featured_image' => 0 ,
'featured_image_alignment' => 'alignnone' ,
'featured_image_size' => 'medium' ,
'gravatar_alignment' => 'left' ,
'user' => '' ,
'show_gravatar' => 0 ,
'size' => 45 ,
'author_info' => '' ,
'bio_text' => '' ,
'page' => '' ,
'page_link_text' => __ ( 'Read More' , 'display-featured-image-genesis' ) . '…' ,
'posts_link' => 0 ,
'link_text' => __ ( 'View My Blog Posts' , 'display-featured-image-genesis' ),
);
}
2015-06-07 17:03:09 -04:00
/**
* Echo the widget content.
*
2017-08-09 08:05:09 -04:00
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
2015-06-07 17:03:09 -04:00
* @param array $instance The settings for the particular instance of the widget
*/
2017-08-09 08:05:09 -04:00
public function widget ( $args , $instance ) {
2015-06-07 17:03:09 -04:00
// Merge with defaults
2017-08-09 08:05:09 -04:00
$instance = wp_parse_args ( ( array ) $instance , $this -> defaults () );
2015-06-07 17:03:09 -04:00
echo $args [ 'before_widget' ];
2017-08-09 08:05:09 -04:00
if ( ! empty ( $instance [ 'title' ] ) ) {
echo $args [ 'before_title' ] . apply_filters ( 'widget_title' , $instance [ 'title' ], $instance , $this -> id_base ) . $args [ 'after_title' ];
}
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
if ( $instance [ 'show_featured_image' ] ) {
$image_id = get_the_author_meta ( 'displayfeaturedimagegenesis' , $instance [ 'user' ] );
$image_src = wp_get_attachment_image_src ( $image_id , $instance [ 'featured_image_size' ] );
if ( $image_src ) {
echo '<img src="' . esc_url ( $image_src [ 0 ] ) . '" alt="' . esc_html ( get_the_author_meta ( 'display_name' , $instance [ 'user' ] ) ) . '" class="' . esc_attr ( $instance [ 'featured_image_alignment' ] ) . '" />' ;
2015-06-07 17:03:09 -04:00
}
2017-08-09 08:05:09 -04:00
}
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
$text = '' ;
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
if ( $instance [ 'show_gravatar' ] ) {
if ( ! empty ( $instance [ 'gravatar_alignment' ] ) ) {
$text .= '<span class="align' . esc_attr ( $instance [ 'gravatar_alignment' ] ) . '">' ;
}
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
$text .= get_avatar ( $instance [ 'user' ], $instance [ 'size' ] );
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
if ( ! empty ( $instance [ 'gravatar_alignment' ] ) ) {
$text .= '</span>' ;
2015-06-07 17:03:09 -04:00
}
2017-08-09 08:05:09 -04:00
}
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
if ( $instance [ 'author_info' ] ) {
$text .= 'text' === $instance [ 'author_info' ] ? $instance [ 'bio_text' ] : get_the_author_meta ( 'description' , $instance [ 'user' ] );
}
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
$text .= $instance [ 'page' ] ? sprintf ( ' <a class="pagelink" href="%s">%s</a>' , get_page_link ( $instance [ 'page' ] ), $instance [ 'page_link_text' ] ) : '' ;
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
// Echo $text
echo wp_kses_post ( wpautop ( $text ) );
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
// If posts link option checked, add posts link to output
$display_name = get_the_author_meta ( 'display_name' , $instance [ 'user' ] );
$user_name = ! empty ( $display_name ) && function_exists ( 'genesis_a11y' ) && genesis_a11y () ? '<span class="screen-reader-text">' . $display_name . ': </span>' : '' ;
2015-06-07 17:03:09 -04:00
2017-08-09 08:05:09 -04:00
if ( $instance [ 'posts_link' ] && $instance [ 'link_text' ] ) {
printf ( '<div class="posts_link posts-link"><a href="%s">%s%s</a></div>' , esc_url ( get_author_posts_url ( $instance [ 'user' ] ) ), wp_kses_post ( $user_name ), esc_attr ( $instance [ 'link_text' ] ) );
}
2015-06-07 17:03:09 -04:00
echo $args [ 'after_widget' ];
}
/**
* Update a particular instance.
*
* This function should check that $new_instance is set correctly.
* The newly calculated value of $instance should be returned.
* If "false" is returned, the instance won't be saved/updated.
*
* @param array $new_instance New settings for this instance as input by the user via form()
* @param array $old_instance Old settings for this instance
2017-08-09 08:05:09 -04:00
*
2015-06-07 17:03:09 -04:00
* @return array Settings to save or bool false to cancel saving
*/
2017-08-09 08:05:09 -04:00
public function update ( $new_instance , $old_instance ) {
2017-10-24 10:36:33 -04:00
$new_instance [ 'user' ] = ( int ) $new_instance [ 'user' ];
$updater = new DisplayFeaturedImageGenesisWidgetsUpdate ();
return $updater -> update ( $new_instance , $old_instance , $this -> get_fields () );
}
/**
* Get all widget fields.
* @return array
*/
public function get_fields () {
return array_merge (
2017-08-09 08:05:09 -04:00
$this -> get_image_fields (),
$this -> get_gravatar_fields (),
$this -> get_description_fields (),
$this -> get_archive_fields ()
);
2015-06-07 17:03:09 -04:00
}
/**
* Echo the settings update form.
*
* @param array $instance Current settings
*/
2017-08-09 08:05:09 -04:00
public function form ( $instance ) {
2015-06-07 17:03:09 -04:00
// Merge with defaults
2017-08-09 08:05:09 -04:00
$instance = wp_parse_args ( ( array ) $instance , $this -> defaults () );
$form = $this -> get_form_class ( $instance );
2017-05-18 22:01:20 -04:00
$form -> do_text ( $instance , array (
'id' => 'title' ,
'label' => __ ( 'Title:' , 'display-featured-image-genesis' ),
'class' => 'widefat' ,
) );
$form -> do_select ( $instance , array (
'id' => 'user' ,
'label' => __ ( 'Select a user. The email address for this account will be used to pull the Gravatar image.' , 'display-featured-image-genesis' ),
'flex' => true ,
'choices' => $this -> get_users (),
) );
$form -> do_boxes ( array (
2017-08-09 08:05:09 -04:00
'author' => $this -> get_image_fields (),
), 'genesis-widget-column-box-top' );
$form -> do_boxes ( array (
'gravatar' => $this -> get_gravatar_fields (),
) );
$form -> do_boxes ( array (
'description' => $this -> get_description_fields (),
) );
$form -> do_boxes ( array (
'archive' => $this -> get_archive_fields (),
) );
}
/**
* Get the plugin widget forms class.
*
* @param array $instance
*
* @return \DisplayFeaturedImageGenesisWidgets
*/
protected function get_form_class ( $instance = array () ) {
if ( isset ( $this -> form_class ) ) {
return $this -> form_class ;
}
$this -> form_class = new DisplayFeaturedImageGenesisWidgets ( $this , $instance );
return $this -> form_class ;
}
/**
* Get the image fields (unique to this widget).
*
* @return array
*/
protected function get_image_fields () {
$form = $this -> get_form_class ();
return array (
array (
'method' => 'checkbox' ,
'args' => array (
'id' => 'show_featured_image' ,
'label' => __ ( 'Show the user\'s featured image.' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'select' ,
'args' => array (
'id' => 'featured_image_size' ,
'label' => __ ( 'Image Size:' , 'display-featured-image-genesis' ),
'flex' => true ,
'choices' => $form -> get_image_size (),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'select' ,
'args' => array (
'id' => 'featured_image_alignment' ,
'label' => __ ( 'Image Alignment:' , 'display-featured-image-genesis' ),
'flex' => true ,
'choices' => $form -> get_image_alignment (),
2017-05-18 22:01:20 -04:00
),
),
2017-08-09 08:05:09 -04:00
);
}
2017-05-18 22:01:20 -04:00
2017-08-09 08:05:09 -04:00
/**
* Get the gravatar fields.
*
* @return array
*/
protected function get_gravatar_fields () {
return array (
array (
'method' => 'checkbox' ,
'args' => array (
'id' => 'show_gravatar' ,
'label' => __ ( 'Show the user\'s gravatar.' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'select' ,
'args' => array (
'id' => 'size' ,
'label' => __ ( 'Gravatar Size:' , 'display-featured-image-genesis' ),
'flex' => true ,
'choices' => $this -> get_gravatar_sizes (),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'select' ,
'args' => array (
'id' => 'gravatar_alignment' ,
'label' => __ ( 'Gravatar Alignment:' , 'display-featured-image-genesis' ),
'flex' => true ,
'choices' => array (
'' => __ ( 'None' , 'display-featured-image-genesis' ),
'left' => __ ( 'Left' , 'display-featured-image-genesis' ),
'right' => __ ( 'Right' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
),
),
2017-08-09 08:05:09 -04:00
);
}
2017-05-18 22:01:20 -04:00
2017-08-09 08:05:09 -04:00
/**
* Get the author description fields.
*
* @return array
*/
protected function get_description_fields () {
return array (
array (
'method' => 'select' ,
'args' => array (
'id' => 'author_info' ,
'label' => __ ( 'Text to use as the author description:' , 'display-featured-image-genesis' ),
'flex' => true ,
'choices' => array (
'' => __ ( 'None' , 'display-featured-image-genesis' ),
'bio' => __ ( 'Author Bio (from profile)' , 'display-featured-image-genesis' ),
'text' => __ ( 'Custom Text (below)' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'textarea' ,
'args' => array (
'id' => 'bio_text' ,
'label' => __ ( 'Custom Text Content' , 'display-featured-image-genesis' ),
'flex' => true ,
'rows' => 6 ,
2017-05-18 22:01:20 -04:00
),
),
2017-08-09 08:05:09 -04:00
);
}
2017-05-18 22:01:20 -04:00
2017-08-09 08:05:09 -04:00
/**
* Get the archive fields.
*
* @return array
*/
protected function get_archive_fields () {
return array (
array (
'method' => 'select' ,
'args' => array (
'id' => 'page' ,
'label' => __ ( 'Choose your extended "About Me" page from the list below. This will be the page linked to at the end of the author description.' , 'display-featured-image-genesis' ),
'choices' => $this -> get_pages (),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'text' ,
'args' => array (
'id' => 'page_link_text' ,
'label' => __ ( 'Extended page link text' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'checkbox' ,
'args' => array (
'id' => 'posts_link' ,
'label' => __ ( 'Show Author Archive Link?' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
2017-08-09 08:05:09 -04:00
),
array (
'method' => 'text' ,
'args' => array (
'id' => 'link_text' ,
'label' => __ ( 'Link Text:' , 'display-featured-image-genesis' ),
2017-05-18 22:01:20 -04:00
),
),
2017-08-09 08:05:09 -04:00
);
2017-05-18 22:01:20 -04:00
}
2015-06-07 17:03:09 -04:00
2017-05-18 22:01:20 -04:00
/**
2017-08-09 08:05:09 -04:00
* Get the authors on the site.
*
2017-05-18 22:01:20 -04:00
* @return array
*/
protected function get_users () {
$users = get_users ( array (
'who' => 'authors' ,
) );
$options = array ();
foreach ( $users as $user ) {
$options [ $user -> ID ] = $user -> data -> display_name ;
}
return $options ;
}
2015-06-07 17:03:09 -04:00
2017-05-18 22:01:20 -04:00
/**
2017-08-09 08:05:09 -04:00
* Get gravatar sizes.
*
2017-05-18 22:01:20 -04:00
* @return array
*/
protected function get_gravatar_sizes () {
$sizes = apply_filters ( 'genesis_gravatar_sizes' , array (
__ ( 'Small' , 'display-featured-image-genesis' ) => 45 ,
__ ( 'Medium' , 'display-featured-image-genesis' ) => 65 ,
__ ( 'Large' , 'display-featured-image-genesis' ) => 85 ,
__ ( 'Extra Large' , 'display-featured-image-genesis' ) => 125 ,
) );
$options = array ();
foreach ( ( array ) $sizes as $label => $size ) {
$options [ $size ] = $label ;
}
return $options ;
2015-06-07 17:03:09 -04:00
}
2017-05-18 22:01:20 -04:00
/**
2017-08-09 08:05:09 -04:00
* Get the pages on the site.
*
2017-05-18 22:01:20 -04:00
* @return array
*/
protected function get_pages () {
$page_ids = get_pages ( array (
'post_type' => 'page' ,
) );
$pages = array ();
if ( $page_ids ) {
$pages [] = __ ( 'None' , 'display-featured-image-genesis' );
foreach ( $page_ids as $id ) {
$title = empty ( $id -> post_title ) ? '#' . $id -> ID . __ ( ' (no title)' , 'sixtenpress-featured-content' ) : $id -> post_title ;
$pages [ $id -> ID ] = $title ;
}
}
return $pages ;
}
2015-06-07 17:03:09 -04:00
}