'tweets', 'description' => '', ); $control_ops = array( 'id_base' => 'tweets-widget', ); parent::__construct( 'tweets-widget', 'Avada: Twitter', $widget_ops, $control_ops ); } /** * Echoes the widget content. * * @access public * @param array $args Display arguments including 'before_title', 'after_title', * 'before_widget', and 'after_widget'. * @param array $instance The settings for the particular instance of the widget. */ public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' ); $twitter_id = isset( $instance['twitter_id'] ) ? $instance['twitter_id'] : ''; $count = (int) isset( $instance['count'] ) ? $instance['count'] : 3; $icon_color = isset( $instance['icon_color'] ) ? $instance['icon_color'] : ''; $consumer_key = isset( $instance['consumer_key'] ) ? $instance['consumer_key'] : ''; $consumer_secret = isset( $instance['consumer_secret'] ) ? $instance['consumer_secret'] : ''; $access_token = isset( $instance['access_token'] ) ? $instance['access_token'] : ''; $access_token_secret = isset( $instance['access_token_secret'] ) ? $instance['access_token_secret'] : ''; $widget_id = isset( $instance['widget_id'] ) ? $instance['widget_id'] : ''; $widget_type = isset( $instance['widget_type'] ) ? $instance['widget_type'] : 'avada_style'; $width = isset( $instance['width'] ) ? $instance['width'] : '400'; $height = isset( $instance['height'] ) ? $instance['height'] : '400'; $theme = isset( $instance['theme'] ) ? $instance['theme'] : 'light'; $link_color = isset( $instance['link_color'] ) ? $instance['link_color'] : Avada()->settings->get( 'link_color' ); $border_color = isset( $instance['border_color'] ) ? $instance['border_color'] : Avada()->settings->get( 'timeline_color' ); $show_header = isset( $instance['show_header'] ) ? $instance['show_header'] : 1; $show_footer = isset( $instance['show_footer'] ) ? $instance['show_footer'] : 1; $show_borders = isset( $instance['show_borders'] ) ? $instance['show_borders'] : 1; $transparent_bg = isset( $instance['transparent_bg'] ) ? $instance['transparent_bg'] : 0; $chrome = $this->get_chrome( $instance ); echo $before_widget; // WPCS: XSS ok. if ( $title ) { echo $before_title . $title . $after_title; // WPCS: XSS ok. } if ( 'twitter_style' == $widget_type || ( 'twitter_preconfig_style' == $widget_type && $widget_id ) ) { $widget_params = array( 'title' => $title, 'twitter_id' => $twitter_id, 'count' => $count, 'widget_id' => $widget_id, 'widget_type' => $widget_type, 'width' => $width, 'height' => $height, 'theme' => $theme, 'link_color' => $link_color, 'border_color' => $border_color, 'show_header' => $show_header, 'show_footer' => $show_footer, 'show_borders' => $show_borders, 'transparent_bg' => $transparent_bg, 'chrome' => $chrome, ); $this->render_new_widget( $widget_params ); } else { if ( $twitter_id && $consumer_key && $consumer_secret && $access_token && $access_token_secret && $count ) { $widget_id = $args['widget_id']; $widget_params = array( 'title' => $title, 'twitter_id' => $twitter_id, 'count' => $count, 'widget_id' => $widget_id, 'icon_color' => $icon_color, 'consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret, 'access_token' => $access_token, 'access_token_secret' => $access_token_secret, ); $this->render_old_widget( $widget_params ); } } // End if(). echo $after_widget; // WPCS: XSS ok. } /** * Updates a particular instance of a widget. * * This function should check that `$new_instance` is set correctly. The newly-calculated * value of `$instance` should be returned. If false is returned, the instance won't be * saved/updated. * * @access public * @param array $new_instance New settings for this instance as input by the user via * WP_Widget::form(). * @param array $old_instance Old settings for this instance. * @return array Settings to save or bool false to cancel saving. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['twitter_id'] = $new_instance['twitter_id']; $instance['count'] = $new_instance['count']; $instance['icon_color'] = $new_instance['icon_color']; $instance['consumer_key'] = $new_instance['consumer_key']; $instance['consumer_secret'] = $new_instance['consumer_secret']; $instance['access_token'] = $new_instance['access_token']; $instance['access_token_secret'] = $new_instance['access_token_secret']; $instance['widget_id'] = $new_instance['widget_id']; $instance['widget_type'] = $new_instance['widget_type']; $instance['width'] = $new_instance['width']; $instance['height'] = $new_instance['height']; $instance['theme'] = $new_instance['theme']; $instance['link_color'] = $new_instance['link_color']; $instance['border_color'] = $new_instance['border_color']; $instance['show_header'] = ! empty( $new_instance['show_header'] ) ? $new_instance['show_header'] : 'off'; $instance['show_footer'] = ! empty( $new_instance['show_footer'] ) ? $new_instance['show_footer'] : 'off'; $instance['show_borders'] = ! empty( $new_instance['show_borders'] ) ? $new_instance['show_borders'] : 'off'; $instance['transparent_bg'] = ! empty( $new_instance['transparent_bg'] ) ? $new_instance['transparent_bg'] : 'off'; return $instance; } /** * Outputs the settings update form. * * @access public * @param array $instance Current settings. */ public function form( $instance ) { $defaults = array( 'title' => __( 'Recent Tweets', 'Avada' ), 'twitter_id' => '', 'count' => 3, 'icon_color' => '', 'consumer_key' => '', 'consumer_secret' => '', 'access_token' => '', 'access_token_secret' => '', 'widget_id' => '', 'widget_type' => 'avada_style', 'width' => '', 'height' => '', 'theme' => 'light', 'link_color' => Avada()->settings->get( 'link_color' ), 'border_color' => Avada()->settings->get( 'timeline_color' ), 'show_header' => 'on', 'show_footer' => 'on', 'show_borders' => 'on', 'transparent_bg' => 'off', ); $instance = wp_parse_args( (array) $instance, $defaults ); $twitter_doc_url = 'https://theme-fusion.com/documentation/avada/twitter-widget/'; ?>
' . esc_attr__( 'How to setup the Avada twitter widget.', 'Avada' ) . '' ); ?>
id="get_field_id( 'show_header' ) ); ?>" name="get_field_name( 'show_header' ) ); ?>" />
id="get_field_id( 'show_footer' ) ); ?>" name="get_field_name( 'show_footer' ) ); ?>" />
id="get_field_id( 'show_borders' ) ); ?>" name="get_field_name( 'show_borders' ) ); ?>" />
id="get_field_id( 'transparent_bg' ) ); ?>" name="get_field_name( 'transparent_bg' ) ); ?>" />
settings->get( 'privacy_embeds' ) && ! Avada()->privacy_embeds->get_consent( 'twitter' ); ?> 'POST', 'httpversion' => '1.1', 'blocking' => true, 'headers' => array( 'Authorization' => 'Basic ' . $to_send, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8', ), 'body' => array( 'grant_type' => 'client_credentials', ), ); add_filter( 'https_ssl_verify', '__return_false' ); $response = wp_remote_post( 'https://api.twitter.com/oauth2/token', $args ); $keys = json_decode( wp_remote_retrieve_body( $response ) ); if ( $keys && isset( $keys->access_token ) ) { // Saving token to wp_options table. update_option( 'cfTwitterToken_' . $widget_id, $keys->access_token ); $token = $keys->access_token; } } // We have bearer token wether we obtained it from API or from options. $args = array( 'httpversion' => '1.1', 'blocking' => true, 'headers' => array( 'Authorization' => "Bearer $token", ), ); add_filter( 'https_ssl_verify', '__return_false' ); $api_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $twitter_id . '&count=' . $count; $response = wp_remote_get( $api_url, $args ); $tweets_body = wp_remote_retrieve_body( $response ); set_site_transient( $consumer_key, $tweets_body, 960 ); } // End if(). $tweets = false; if ( ! empty( $tweets_body ) ) { $tweets = json_decode( $tweets_body, true ); if ( ! is_array( $tweets ) ) { $tweets = false; } } if ( $tweets && is_array( $tweets ) ) { if ( '' !== $icon_color ) { echo ''; } ?> ' . $e['display_url'] . ''; $entities[] = $temp; } } if ( $users && is_array( $tweet['entities']['user_mentions'] ) ) { foreach ( $tweet['entities']['user_mentions'] as $e ) { $temp['start'] = $e['indices'][0]; $temp['end'] = $e['indices'][1]; $temp['replacement'] = '@' . $e['screen_name'] . ''; $entities[] = $temp; } } if ( $hashtags && is_array( $tweet['entities']['hashtags'] ) ) { foreach ( $tweet['entities']['hashtags'] as $e ) { $temp['start'] = $e['indices'][0]; $temp['end'] = $e['indices'][1]; $temp['replacement'] = '#' . $e['text'] . ''; $entities[] = $temp; } } if ( $media && array_key_exists( 'media', $tweet['entities'] ) ) { foreach ( $tweet['entities']['media'] as $e ) { $temp['start'] = $e['indices'][0]; $temp['end'] = $e['indices'][1]; $temp['replacement'] = '' . $e['display_url'] . ''; $entities[] = $temp; } } usort( $entities, array( $this, 'sort_tweets' ) ); foreach ( $entities as $item ) { $return = $this->mb_substr_replace( $return, $item['replacement'], $item['start'], $item['end'] - $item['start'] ); } return $return; } /** * The PHP substr_replace equivalent for multibyte encoded strings. * * @param string $string The string in which replacement should take place. * @param string $replacement The replacement string. * @param int $start The index where the replacement should start. * @param int $length The length of $string that should be replaced. * @return array|string The correctly replaced string. When the result is an array, it runs recursively. */ public function mb_substr_replace( $string, $replacement, $start, $length = null ) { if ( is_array( $string ) ) { $num = count( $string ); // $replacement. $replacement = is_array( $replacement ) ? array_slice( $replacement, 0, $num ) : array_pad( array( $replacement ), $num, $replacement ); // $start. if ( is_array( $start ) ) { $start = array_slice( $start, 0, $num ); foreach ( $start as $key => $value ) { $start[ $key ] = is_int( $value ) ? $value : 0; } } else { $start = array_pad( array( $start ), $num, $start ); } // $length. if ( ! isset( $length ) ) { $length = array_fill( 0, $num, 0 ); } elseif ( is_array( $length ) ) { $length = array_slice( $length, 0, $num ); foreach ( $length as $key => $value ) { $length[ $key ] = isset( $value ) ? ( is_int( $value ) ? $value : $num ) : 0; } } else { $length = array_pad( array( $length ), $num, $length ); } // Recursive call. return array_map( __FUNCTION__, $string, $replacement, $start, $length ); } preg_match_all( '/./us', (string) $string, $smatches ); preg_match_all( '/./us', (string) $replacement, $rmatches ); if ( null === $length ) { $length = mb_strlen( $string ); } array_splice( $smatches[0], $start, $length, $rmatches[0] ); return join( $smatches[0] ); } /** * Compare the start indices of two twitter entities. * * @param array $a A twitter entity. * @param array $b A twitter entity. * @return int The difference of the start indices. */ public function sort_tweets( $a, $b ) { return ( $b['start'] - $a['start'] ); } /** * Function to display the correct time format for each tweet. * * @param int $time A timestamp for the tweet publishing date. * @return string The formatted date for the twwet's publishing date. */ public function ago( $time ) { /* translators: time difference. */ return sprintf( _x( '%s ago', '%s = human-readable time difference', 'Avada' ), human_time_diff( $time, current_time( 'timestamp' ) ) ); } } /* Omit closing PHP tag to avoid "Headers already sent" issues. */