Fencer04

New Twitter Widget Code

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