Advertisement
Guest User

Untitled

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