Advertisement
Guest User

WordPress Widget

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