Advertisement
Guest User

Untitled

a guest
Jan 31st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.48 KB | None | 0 0
  1. <?php
  2. /*-----------------------------------------------------------------------------------*/
  3. /*  Author Widget Class
  4. /*-----------------------------------------------------------------------------------*/
  5.  
  6. class WPS_Author_Widget extends WP_Widget {
  7.  
  8. function mytheme_get_avatar( $myavatar ) {
  9.     $myavatar = '<img class="'.$image_align.'" src="<' . get_template_directory_uri() . '/images/authors/' . get_the_author_meta('ID', $user_id) . '.jpg" alt="' . get_the_author() . ' ">';
  10.     return $myavatar;
  11. }
  12. //add_filter( 'get_avatar', 'mytheme_get_avatar' );
  13.  
  14.  
  15. add_filter('get_avatar', 'remove_photo_class');
  16. function remove_photo_class($avatar) {
  17.     return str_replace(' avatar', $image_align , $avatar);
  18. }
  19.  
  20.  
  21.     function WPS_Author_Widget() {
  22.         $widget_ops = array( 'classname' => 'wps_author_widget', 'description' => __('Use this widget to display author/user profile info', 'twid') );
  23.         $control_ops = array( 'id_base' => 'wps_author_widget' );
  24.         $this->WP_Widget( 'wps_author_widget', __('FRONT User Profile', 'twid'), $widget_ops, $control_ops );
  25.        
  26.         if(!is_admin()){
  27.           add_action( 'wp_enqueue_scripts', array($this,'enqueue_styles'));
  28.         }
  29.     }
  30.    
  31.  
  32.     function enqueue_styles(){
  33.         wp_register_style( 'twid-author-widget', WPS_AUTHOR_WIDGET_URL.'css/style.css', false, '1.0.0' );
  34.     wp_enqueue_style( 'twid-author-widget' );
  35.  }
  36.  
  37.  
  38.  
  39.    
  40.     function widget( $args, $instance ) {
  41.         extract( $args );
  42.        
  43.         //Check for user_id
  44.             $user_id = $instance['author'];
  45.             if($instance['auto_detect']){
  46.                 if(is_author()){
  47.                     $obj = get_queried_object();
  48.                     $user_id = $obj->data->ID;
  49.                 } elseif(is_single()){
  50.                     $obj = get_queried_object();
  51.                     $user_id = $obj->post_author;
  52.                 }
  53.             }
  54.            
  55.         $author_link = $instance['display_all_posts'] ? get_author_posts_url(get_the_author_meta('ID',$user_id)) : false;
  56.         $image_align = $instance ['image_alignment'];
  57.         $title =  $instance['name_to_title'] ? get_the_author_meta('display_name', $user_id) : apply_filters('widget_title', $instance['title'] );
  58.                
  59.         echo $before_widget;
  60.  
  61.         if ( !empty($title) ) {
  62.             echo $before_title . $title . $after_title;
  63.         }
  64.                
  65.  
  66.         ?>
  67.        
  68.  
  69.        
  70.         <?php if($instance['display_avatar']) : ?>
  71.             <?php
  72.                 if($author_link && $instance['link_to_avatar']){
  73.                     $pre_avatar = '<a href="'.$author_link.'">';
  74.                     $post_avatar = '</a>';
  75.                 } else {
  76.                     $pre_avatar = '';
  77.                     $post_avatar = '';
  78.                 }
  79.  
  80.                                  
  81.                              
  82.  
  83.                 echo get_avatar( get_the_author_meta('ID', $user_id), $instance['avatar_size'] ) . $post_avatar;
  84.             ?>
  85.         <?php endif; ?>
  86.        
  87.         <?php if($instance['display_name'] && !($instance['name_to_title'])) : ?>
  88.           <?php
  89.             if($author_link && $instance['link_to_name']){
  90.                     $pre_name = '<a href="'.$author_link.'">';
  91.                     $post_name = '</a>';
  92.                 } else {
  93.                     $pre_name = '';
  94.                     $post_name = '';
  95.                 }
  96.                 echo '<h3>' . $pre_name . get_the_author_meta('display_name', $user_id) . $post_name. '</h3>';
  97.             ?>
  98.         <?php endif; ?>
  99.        
  100.         <?php if($instance['display_desc']) : ?>
  101.             <?php echo wpautop(get_the_author_meta('description',$user_id)); ?>
  102.         <?php endif; ?>
  103.            
  104.         <?php if($author_link && $instance['link_text']) : ?>
  105.             <a href="<?php echo $author_link; ?>" class="wps_author_link"><?php echo $instance['link_text']; ?></a>
  106.         <?php endif; ?>
  107.  
  108.         <?php
  109.         echo $after_widget;
  110.     }
  111.  
  112.  
  113.  
  114.    
  115.     function update( $new_instance, $old_instance ) {
  116.         $instance = $old_instance;
  117.         $instance['title'] = strip_tags( $new_instance['title'] );
  118.         $instance['author'] = absint( $new_instance['author'] );
  119.                 $instance['image_alignment'] = $new_instance['image_alignment'];
  120.         $instance['auto_detect'] = isset($new_instance['auto_detect']) ? 1 : 0;
  121.         $instance['display_name'] = isset($new_instance['display_name']) ? 1 : 0;
  122.         $instance['display_avatar'] = isset($new_instance['display_avatar']) ? 1 : 0;
  123.         $instance['display_desc'] = isset($new_instance['display_desc']) ? 1 : 0;
  124.         $instance['display_all_posts'] = isset($new_instance['display_all_posts']) ? 1 : 0;
  125.         $instance['name_to_title'] = isset($new_instance['name_to_title']) ? 1 : 0;
  126.         $instance['link_to_name'] = isset($new_instance['link_to_name']) ? 1 : 0;
  127.         $instance['link_to_avatar'] = isset($new_instance['link_to_avatar']) ? 1 : 0;
  128.         $instance['link_text'] = strip_tags( $new_instance['link_text'] );
  129.         $instance['avatar_size'] = !empty($new_instance['avatar_size']) ? absint($new_instance['avatar_size']) : 64;
  130.  
  131.         return $instance;
  132.     }
  133.  
  134.     function form( $instance ) {
  135.  
  136.         $defaults = array(
  137.                 'title' => __('About Author', 'twid'),
  138.                 'author' => 0,
  139.                 'auto_detect' => 0,
  140.                 'display_name' => 1,
  141.                 'display_avatar' => 1,
  142.                                 'image_alignment' => '',
  143.                 'display_desc' => 1,
  144.                 'display_all_posts' => 1,
  145.                 'avatar_size' => 64,
  146.                 'name_to_title' => 0,
  147.                 'link_to_name' => 0,
  148.                 'link_to_avatar' => 0,
  149.                 'link_text' => __('View all posts', 'twid'),
  150.             );
  151.            
  152.         $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  153.        
  154.         <p>
  155.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title', 'twid'); ?>:</label>
  156.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" />
  157.         </p>
  158.        
  159.         <p>
  160.             <?php $authors = get_users(); ?>
  161.             <label for="<?php echo $this->get_field_id( 'author' ); ?>"><?php _e('Choose author/user', 'twid'); ?>:</label>
  162.             <select name="<?php echo $this->get_field_name( 'author' ); ?>" id="<?php echo $this->get_field_id( 'author' ); ?>" class="widefat">
  163.             <?php foreach($authors as $author) : ?>
  164.                 <option value="<?php echo $author->ID; ?>" <?php selected($author->ID, $instance['author']); ?>><?php echo $author->data->user_login; ?></option>
  165.             <?php endforeach; ?>
  166.             </select>
  167.         </p>
  168.        
  169.         <p>
  170.         <input id="<?php echo $this->get_field_id( 'auto_detect' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'auto_detect' ); ?>" value="1" <?php checked(1, $instance['auto_detect']); ?>/>
  171.         <label for="<?php echo $this->get_field_id( 'auto_detect' ); ?>"><?php _e('Automatically detect author', 'twid'); ?></label>
  172.         <small class="howto"><?php _e('Use this option to automatically detect author if this sidebar is used on single post template or author template', 'twid'); ?></small>
  173.       </p>
  174.       <h4><?php _e('Display Options', 'twid'); ?></h4>
  175.       <ul>
  176.         <li>
  177.             <input id="<?php echo $this->get_field_id( 'display_avatar' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'display_avatar' ); ?>" value="1" <?php checked(1, $instance['display_avatar']); ?>/>
  178.             <label for="<?php echo $this->get_field_id( 'display_avatar' ); ?>"><?php _e('Display author avatar', 'twid'); ?></label>
  179.         </li>
  180.  
  181.  
  182. <li>
  183.     <label for="<?php echo $this->get_field_id( 'image_alignment' ); ?>"><?php _e( 'Image Alignment', 'twid' ); ?>:</label>
  184.             <select id="<?php echo $this->get_field_id( 'image_alignment' ); ?>" name="<?php echo $this->get_field_name( 'image_alignment' ); ?>">
  185.                 <option value="alignnone">- <?php _e( 'None', 'twid' ); ?> -</option>
  186.                 <option value="alignleft" <?php selected( 'alignleft', $instance['image_alignment'] ); ?>><?php _e( 'Left', 'twid' ); ?></option>
  187.                 <option value="alignright" <?php selected( 'alignright', $instance['image_alignment'] ); ?>><?php _e( 'Right', 'twid' ); ?></option>
  188.             </select>
  189.  
  190.  
  191. </li>
  192.         <li>
  193.             <label for="<?php echo $this->get_field_id( 'avatar_size' ); ?>"><?php _e('Avatar size:', 'twid'); ?></label>
  194.             <input id="<?php echo $this->get_field_id( 'avatar_size' ); ?>" type="text" name="<?php echo $this->get_field_name( 'avatar_size' ); ?>" value="<?php echo $instance['avatar_size']; ?>" class="small-text"/> px
  195.         </li>
  196.       </ul>
  197.       <hr/>
  198.       <ul>
  199.         <li>
  200.             <input id="<?php echo $this->get_field_id( 'display_name' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'display_name' ); ?>" value="1" <?php checked(1, $instance['display_name']); ?>/>
  201.             <label for="<?php echo $this->get_field_id( 'display_name' ); ?>"><?php _e('Display author name', 'twid'); ?></label>
  202.         </li>
  203.         <li>
  204.             <input id="<?php echo $this->get_field_id( 'name_to_title' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'name_to_title' ); ?>" value="1" <?php checked(1, $instance['name_to_title']); ?>/>
  205.             <label for="<?php echo $this->get_field_id( 'name_to_title' ); ?>"><?php _e('Overwrite widget title with author name', 'twid'); ?></label>
  206.         </li>
  207.       </ul>
  208.       <hr/>
  209.       <ul>
  210.         <li>
  211.             <input id="<?php echo $this->get_field_id( 'display_desc' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'display_desc' ); ?>" value="1" <?php checked(1, $instance['display_desc']); ?>/>
  212.             <label for="<?php echo $this->get_field_id( 'display_desc' ); ?>"><?php _e('Display author description', 'twid'); ?></label>
  213.         </li>
  214.       </ul>
  215.       <hr/>
  216.       <ul>
  217.         <li>
  218.             <input id="<?php echo $this->get_field_id( 'display_all_posts' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'display_all_posts' ); ?>" value="1" <?php checked(1, $instance['display_all_posts']); ?>/>
  219.             <label for="<?php echo $this->get_field_id( 'display_all_posts' ); ?>"><?php _e('Display author "all posts" archive link', 'twid'); ?></label>
  220.         </li>
  221.         <li>
  222.             <input id="<?php echo $this->get_field_id( 'link_to_name' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'link_to_name' ); ?>" value="1" <?php checked(1, $instance['link_to_name']); ?>/>
  223.             <label for="<?php echo $this->get_field_id( 'link_to_name' ); ?>"><?php _e('Link author name', 'twid'); ?></label>
  224.         </li>
  225.         <li>
  226.             <input id="<?php echo $this->get_field_id( 'link_to_avatar' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'link_to_avatar' ); ?>" value="1" <?php checked(1, $instance['link_to_avatar']); ?>/>
  227.             <label for="<?php echo $this->get_field_id( 'link_to_avatar' ); ?>"><?php _e('Link author avatar', 'twid'); ?></label>
  228.         </li>
  229.         <li>
  230.             <label for="<?php echo $this->get_field_id( 'link_text' ); ?>"><?php _e('Link text:', 'twid'); ?></label>
  231.             <input id="<?php echo $this->get_field_id( 'link_text' ); ?>" type="text" name="<?php echo $this->get_field_name( 'link_text' ); ?>" value="<?php echo $instance['link_text']; ?>" class="widefat"/>
  232.             <small class="howto"><?php _e('Specify text for "all posts" link if you want to show separate link', 'twid'); ?></small>
  233.         </li>
  234.       </ul>
  235.        
  236.        
  237.     <?php
  238.     }
  239. }
  240. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement