Advertisement
Fencer04

Original Theme Widget

Jul 27th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.10 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Latest tweets slider widget
  4.  * Display recent post or popular post for each category or all posts
  5.  *
  6.  * @package Wordpress
  7.  * @since 1.2
  8.  */
  9.  
  10. add_action( 'widgets_init', 'penci_latest_tweets_load_widget' );
  11.  
  12. function penci_latest_tweets_load_widget() {
  13.     register_widget( 'penci_latest_tweets_widget' );
  14. }
  15.  
  16. class penci_latest_tweets_widget extends WP_Widget {
  17.  
  18.     /**
  19.      * Widget setup.
  20.      */
  21.     function penci_latest_tweets_widget() {
  22.         /* Widget settings. */
  23.         $widget_ops = array( 'classname' => 'penci_latest_tweets_widget', 'description' => esc_html__('A widget that displays your latest tweets with a slider', 'soledad') );
  24.  
  25.         /* Widget control settings. */
  26.         $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'penci_latest_tweets_widget' );
  27.  
  28.         /* Create the widget. */
  29.         global $wp_version;
  30.         if( 4.3 > $wp_version ) {
  31.             $this->WP_Widget( 'penci_latest_tweets_widget', esc_html__('.Soledad Tweets Slider', 'soledad'), $widget_ops, $control_ops );
  32.         } else {
  33.             parent::__construct( 'penci_latest_tweets_widget', esc_html__('.Soledad Tweets Slider', 'soledad'), $widget_ops, $control_ops );
  34.         }
  35.     }
  36.  
  37.     /**
  38.      * How to display the widget on the screen.
  39.      */
  40.     function widget( $args, $instance ) {
  41.         extract( $args );
  42.  
  43.         /* Our variables from the widget settings. */
  44.         $title    = apply_filters( 'widget_title', $instance['title'] );
  45.         $date     = isset( $instance['date'] ) ? $instance['date'] : false;
  46.         $auto     = isset( $instance['auto'] ) ? $instance['auto'] : false;
  47.         $reply    = isset( $instance['reply'] ) ? $instance['reply'] : esc_html__( 'Reply', 'soledad' );
  48.         $retweet  = isset( $instance['retweet'] ) ? $instance['retweet'] : esc_html__( 'Retweet', 'soledad' );
  49.         $favorite = isset( $instance['favorite'] ) ? $instance['favorite'] : esc_html__( 'Favorite', 'soledad' );
  50.  
  51.         $tweets = getTweets(5);
  52.  
  53.         if( !empty( $tweets ) ):
  54.  
  55.             /* Before widget (defined by themes). */
  56.             echo ent2ncr( $before_widget );
  57.  
  58.             /* Display the widget title if one was input (before and after defined by themes). */
  59.             if ( $title )
  60.                 echo ent2ncr( $before_title . $title . $after_title );
  61.  
  62.             if( isset( $tweets['error'] ) ) {
  63.                 echo 'Missing consumer key - please check your settings in admin > Settings > Twitter Feed Auth';
  64.             } else {
  65.             ?>
  66.             <div class="penci-tweets-widget-content">
  67.                 <span class="icon-tweets"><i class="fa fa-twitter"></i></span>
  68.                 <div class="penci-slider penci-tweets-slider" data-smooth="true" data-direction="horizontal" data-auto="<?php if( $auto ){ echo 'false'; } else { echo 'true'; } ?>" data-dir="true" data-control="false" data-autotime="5000" data-speed="500">
  69.                     <ul class="slides">
  70.                         <?php foreach( $tweets as $tweet ):
  71.                         $date_array = explode( ' ', $tweet['created_at'] );
  72.                         $tweet_id = $tweet['id_str'];
  73.                         $tweet_text = $tweet['text'];
  74.                         $urls = $tweet['entities']['urls'];
  75.  
  76.                         if( isset( $urls ) ) {
  77.                             foreach ( $urls as $ul ) {
  78.                                 $url = $ul['url'];
  79.                                 if( isset( $url ) ):
  80.                                     $tweet_text = str_replace( $url, '<a href="'. $url .'" target="_blank">'. $url .'</a>', $tweet_text );
  81.                                 endif;
  82.                             }
  83.                         }
  84.                         ?>
  85.                             <li class="penci-tweet">
  86.                                 <div class="tweet-text">
  87.                                     <?php echo $tweet_text; ?>
  88.                                 </div>
  89.                                 <?php if( $date_array[1] && $date_array[2] && $date_array[5] && ! $date ): ?>
  90.                                 <p class="tweet-date"><?php echo $date_array[2] . '-' . $date_array[1] . '-' . $date_array[5]; ?></p>
  91.                                 <?php endif; ?>
  92.                                 <div class="tweet-intents">
  93.                                     <div class="tweet-intents-inner">
  94.                                         <span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $reply ); ?></a></span>
  95.                                         <span><a target="_blank" class="retweet" href="https://twitter.com/intent/retweet?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $retweet ); ?></a></span>
  96.                                         <span><a target="_blank" class="favorite" href="https://twitter.com/intent/favorite?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $favorite ); ?></a></span>
  97.                                     </div>
  98.                                 </div>
  99.                             </li>
  100.                         <?php endforeach; ?>
  101.                     </ul>
  102.                 </div>
  103.             </div>
  104.  
  105.             <?php
  106.             }
  107.         endif; /* End check if array $tweets empty or null */
  108.  
  109.         /* After widget (defined by themes). */
  110.         echo ent2ncr( $after_widget );
  111.     }
  112.  
  113.     /**
  114.      * Update the widget settings.
  115.      */
  116.     function update( $new_instance, $old_instance ) {
  117.         $instance = $old_instance;
  118.  
  119.         /* Strip tags for title and name to remove HTML (important for text inputs). */
  120.         $instance['title']    = strip_tags( $new_instance['title'] );
  121.         $instance['date']     = strip_tags( $new_instance['date'] );
  122.         $instance['auto']     = strip_tags( $new_instance['auto'] );
  123.         $instance['reply']    = strip_tags( $new_instance['reply'] );
  124.         $instance['retweet']  = strip_tags( $new_instance['retweet'] );
  125.         $instance['favorite'] = strip_tags( $new_instance['favorite'] );
  126.  
  127.         return $instance;
  128.     }
  129.  
  130.  
  131.     function form( $instance ) {
  132.  
  133.         /* Set up some default widget settings. */
  134.         $defaults = array( 'title' => esc_html__('Tweets', 'soledad'), 'date' => false, 'auto' => false, 'reply' => esc_html__('Reply', 'soledad'), 'retweet' => esc_html__('Retweet', 'soledad'), 'favorite' => esc_html__('Favorite', 'soledad') );
  135.         $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  136.  
  137.         <br>
  138.         <p><span style="color: #ff0000;">Note Important:</span> To use this widget you need fill complete your twitter information <a href="<?php echo admin_url( 'options-general.php?page=tdf_settings' ); ?>">here</a></p>
  139.  
  140.         <!-- Widget Title: Text Input -->
  141.         <p>
  142.             <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e('Title:', 'soledad'); ?></label>
  143.             <input  type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo sanitize_text_field( $instance['title'] ); ?>"  />
  144.         </p>
  145.  
  146.         <!-- Display tweets date -->
  147.         <p>
  148.             <label for="<?php echo esc_attr( $this->get_field_id( 'date' ) ); ?>"><?php esc_html_e('Hide tweets date?:','soledad'); ?></label>
  149.             <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'date' ) ); ?>" <?php checked( (bool) $instance['date'], true ); ?> />
  150.         </p>
  151.  
  152.         <!-- Disable auto play -->
  153.         <p>
  154.             <label for="<?php echo esc_attr( $this->get_field_id( 'auto' ) ); ?>"><?php esc_html_e('Disable Auto Play Tweets Slider?:','soledad'); ?></label>
  155.             <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'auto' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'auto' ) ); ?>" <?php checked( (bool) $instance['auto'], true ); ?> />
  156.         </p>
  157.  
  158.         <!-- Custom reply text -->
  159.         <p>
  160.             <label for="<?php echo esc_attr( $this->get_field_id( 'reply' ) ); ?>"><?php esc_html_e('Custom Reply text:', 'soledad'); ?></label>
  161.             <input  type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'reply' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'reply' ) ); ?>" value="<?php echo esc_attr( $instance['reply'] ); ?>" />
  162.         </p>
  163.  
  164.         <!-- Custom retweet text -->
  165.         <p>
  166.             <label for="<?php echo esc_attr( $this->get_field_id( 'retweet' ) ); ?>"><?php esc_html_e('Custom Retweet text:', 'soledad'); ?></label>
  167.             <input  type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'retweet' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'retweet' ) ); ?>" value="<?php echo esc_attr( $instance['retweet'] ); ?>" />
  168.         </p>
  169.  
  170.         <!-- Custom favorite text -->
  171.         <p>
  172.             <label for="<?php echo esc_attr( $this->get_field_id( 'favorite' ) ); ?>"><?php esc_html_e('Custom Favorite text:', 'soledad'); ?></label>
  173.             <input  type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'favorite' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'favorite' ) ); ?>" value="<?php echo esc_attr( $instance['favorite'] ); ?>" />
  174.         </p>
  175.  
  176.     <?php
  177.     }
  178. }
  179. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement