diff options
Diffstat (limited to 'plugins/jetpack/modules/widgets')
-rw-r--r-- | plugins/jetpack/modules/widgets/facebook-likebox.php | 253 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/image-widget.php | 156 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/rsslinks-widget.php | 168 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/twitter-widget.php | 273 |
4 files changed, 850 insertions, 0 deletions
diff --git a/plugins/jetpack/modules/widgets/facebook-likebox.php b/plugins/jetpack/modules/widgets/facebook-likebox.php new file mode 100644 index 00000000..ebf6e9b5 --- /dev/null +++ b/plugins/jetpack/modules/widgets/facebook-likebox.php @@ -0,0 +1,253 @@ +<?php + +/** + * Facebook Like Box widget class + * Display a Facebook Like Box as a widget + * http://developers.facebook.com/docs/reference/plugins/like-box/ + */ +class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { + + private $default_height = 432; + private $default_width = 200; + private $max_width = 400; + private $min_width = 100; + private $default_colorscheme = 'light'; + private $allowed_colorschemes = array( 'light', 'dark' ); + + function __construct() { + parent::__construct( 'facebook-likebox', __( 'Facebook Like Box', 'jetpack' ), array( 'classname' => 'widget_facebook_likebox', 'description' => __( 'Display a Facebook Like Box to connect visitors to your Facebook Page', 'jetpack' ) ) ); + } + + function widget( $args, $instance ) { + + extract( $args ); + + $like_args = $this->normalize_facebook_args( $instance['like_args'] ); + + if( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) { + echo '<!-- Invalid Facebook Page URL -->'; + return; + } + + + $title = apply_filters( 'widget_title', $instance['title'] ); + $page_url = ( is_ssl() ) ? str_replace( 'http://', 'https://', $like_args['href'] ) : $like_args['href']; + + // Calculate the height based on the features enabled + if( $like_args['show_faces'] && $like_args['stream'] ) { + $like_args['height'] = 580; + } else if( ! $like_args['show_faces'] && ! $like_args['stream'] ) { + $like_args['height'] = 110; + } else { + $like_args['height'] = 432; + } + + $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false'; + $like_args['stream'] = (bool) $like_args['stream'] ? 'true' : 'false'; + $like_args['force_wall'] = (bool) $like_args['force_wall'] ? 'true' : 'false'; + $like_args['header'] = (bool) $like_args['header'] ? 'true' : 'false'; + $like_bg_colour = ( 'dark' == $like_args['colorscheme'] ) ? '#000' : '#fff'; + + $locale = $this->get_locale(); + if( $locale && 'en_US' != $locale ) + $like_args['locale'] = $locale; + + $like_args = urlencode_deep( $like_args ); + $like_url = add_query_arg( $like_args, sprintf( '%swww.facebook.com/plugins/likebox.php', ( is_ssl() ) ? 'https://' : 'http://' ) ); + + echo $before_widget; + + if( ! empty( $title ) ) : + echo $before_title; + ?><a href="<?php echo esc_url( $page_url ); ?>"><?php esc_html_e( $title , 'jetpack' ); ?></a><?php + echo $after_title; + endif; + + ?><iframe src="<?php echo esc_url( $like_url ); ?>" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: <?php esc_html_e( $like_args['width'] , 'jetpack' ); ?>px; height: <?php esc_html_e( $like_args['height'] , 'jetpack' ); ?>px; background: <?php esc_html_e( $like_bg_colour , 'jetpack' ); ?>"></iframe><?php + + echo $after_widget; + + do_action( 'jetpack_stats_extra', 'widget', 'facebook-likebox' ); + } + + function update( $new_instance, $old_instance ) { + $instance = array( + 'title' => '', + 'like_args' => $this->get_default_args(), + ); + + $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) ); + + // Set up widget values + $instance['like_args'] = array( + 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ), + 'width' => (int) $new_instance['width'], + 'height' => (int) $new_instance['height'], + 'colorscheme' => $new_instance['colorscheme'], + 'show_faces' => (bool) $new_instance['show_faces'], + 'stream' => (bool) $new_instance['stream'], + 'header' => false, // The header just displays "Find us on Facebook"; it's redundant with the title + 'force_wall' => (bool) $new_instance['force_wall'], + ); + + $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] ); + + return $instance; + } + + function form( $instance ) { + $instance = wp_parse_args( (array) $instance, array( + 'title' => '', + 'like_args' => $this->get_default_args() + ) ); + $like_args = $this->normalize_facebook_args( $instance['like_args'] ); + ?> + + <p> + <label for="<?php echo $this->get_field_id( 'title' ); ?>"> + <?php _e( 'Title', 'jetpack' ); ?> + <input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" /> + </label> + </p> + + <p> + <label for="<?php echo $this->get_field_id( 'href' ); ?>"> + <?php _e( 'Facebook Page URL', 'jetpack' ); ?> + <input type="text" name="<?php echo $this->get_field_name( 'href' ); ?>" id="<?php echo $this->get_field_id( 'href' ); ?>" value="<?php echo esc_url( $like_args['href'] ); ?>" class="widefat" /> + <br /> + <small><?php _e( 'The Like Box only works with <a href="http://www.facebook.com/help/?faq=174987089221178">Facebook Pages</a>.', 'jetpack' ); ?></small> + </label> + </p> + + <p> + <label for="<?php echo $this->get_field_id( 'width' ); ?>"> + <?php _e( 'Width', 'jetpack' ); ?> + <input type="text" maxlength="3" name="<?php echo $this->get_field_name( 'width' ); ?>" id="<?php echo $this->get_field_id( 'width' ); ?>" value="<?php echo esc_attr( $like_args['width'] ); ?>" style="width: 30px; text-align: center;" /> + </label> + </p> + + <p> + <label for="<?php echo $this->get_field_id( 'colorscheme' ); ?>"> + <?php _e( 'Color Scheme', 'jetpack' ); ?> + <select name="<?php echo $this->get_field_name( 'colorscheme' ); ?>" id="<?php echo $this->get_field_id( 'colorscheme' ); ?>"> + <option value="light" <?php selected( $like_args['colorscheme'], 'light' ); ?>><?php _e( 'Light', 'jetpack' ); ?></option> + <option value="dark" <?php selected( $like_args['colorscheme'], 'dark' ); ?>><?php _e( 'Dark', 'jetpack' ); ?></option> + </select> + </label> + </p> + + + <p> + <label for="<?php echo $this->get_field_id( 'show_faces' ); ?>"> + <input type="checkbox" name="<?php echo $this->get_field_name( 'show_faces' ); ?>" id="<?php echo $this->get_field_id( 'show_faces' ); ?>" <?php checked( $like_args['show_faces'] ); ?> /> + <?php _e( 'Show Faces', 'jetpack' ); ?> + <br /> + <small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small> + </label> + </p> + + <p> + <label for="<?php echo $this->get_field_id( 'stream' ); ?>"> + <input type="checkbox" name="<?php echo $this->get_field_name( 'stream' ); ?>" id="<?php echo $this->get_field_id( 'stream' ); ?>" <?php checked( $like_args['stream'] ); ?> /> + <?php _e( 'Show Stream', 'jetpack' ); ?> + <br /> + <small><?php _e( 'Show the profile stream for the public profile.', 'jetpack' ); ?></small> + </label> + </p> + + <p> + <label for="<?php echo $this->get_field_id( 'force_wall' ); ?>"> + <input type="checkbox" name="<?php echo $this->get_field_name( 'force_wall' ); ?>" id="<?php echo $this->get_field_id( 'force_wall' ); ?>" <?php checked( $like_args['force_wall'] ); ?> /> + <?php _e( 'Show Wall', 'jetpack' ); ?> + <br /> + <small><?php _e( 'Show the wall for a Places page rather than friend activity.', 'jetpack' ); ?></small> + </label> + </p> + + <?php + } + + function get_default_args() { + $defaults = array( + 'href' => '', + 'width' => $this->default_width, + 'height' => $this->default_height, + 'colorscheme' => $this->default_colorscheme, + 'show_faces' => true, + 'stream' => false, + 'header' => false, + 'force_wall' => false, + ); + + return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults ); + } + + function normalize_facebook_args( $args ) { + $args = wp_parse_args( (array) $args, $this->get_default_args() ); + + // Validate the Facebook Page URL + if ( $this->is_valid_facebook_url( $args['href'] ) ) { + $temp = explode( '?', $args['href'] ); + $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] ); + } else { + $args['href'] = ''; + } + + $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width ); + $args['colorscheme'] = $this->normalize_text_value( $args['colorscheme'], $this->default_colorscheme, $this->allowed_colorschemes ); + $args['show_faces'] = (bool) $args['show_faces']; + $args['stream'] = (bool) $args['stream']; + $args['force_wall'] = (bool) $args['force_wall']; + + return $args; + } + + function is_valid_facebook_url( $url ) { + return ( FALSE !== strpos( $url, 'facebook.com' ) ) ? TRUE : FALSE; + } + + function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { + $value = (int) $value; + + if( ! $value || $max < $value || $min > $value ) + $value = $default; + + return (int) $value; + } + + function normalize_text_value( $value, $default = '', $allowed = array() ) { + $allowed = (array) $allowed; + + if( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) + $value = $default; + + return $value; + } + + function guess_locale_from_lang( $lang ) { + $lang = strtolower( str_replace( '-', '_', $lang ) ); + + if ( 5 == strlen( $lang ) ) { + $lang = substr( $lang, 0, 3 ) . strtoupper( substr( $lang, 3, 2 ) ); + } else if ( 3 == strlen( $lang ) ) { + $lang = $lang; + } else { + $lang = $lang . '_' . strtoupper( $lang ); + } + + if ( 'en_EN' == $lang ) { + $lang = 'en_US'; + } else if ( 'he_HE' == $lang ) { + $lang = 'he_IL'; + } else if ( 'ja_JA' == $lang ) + $lang = 'ja_JP'; + + return $lang; + } + + function get_locale() { + return $this->guess_locale_from_lang( get_locale() ); + } +} + +// END diff --git a/plugins/jetpack/modules/widgets/image-widget.php b/plugins/jetpack/modules/widgets/image-widget.php new file mode 100644 index 00000000..0e7e227b --- /dev/null +++ b/plugins/jetpack/modules/widgets/image-widget.php @@ -0,0 +1,156 @@ +<?php +/** + * Module Name: Image Widget + * Module Description: Easily add images to your theme's sidebar. + * Sort Order: 20 + * First Introduced: 1.2 + */ + +class Jetpack_Image_Widget extends WP_Widget { + + function Jetpack_Image_Widget() { + $widget_ops = array( 'classname' => 'widget_image', 'description' => __( "Display an image in your sidebar", 'jetpack' ) ); + $control_ops = array( 'width' => 400 ); + $this->WP_Widget( 'image', __( 'Image (Jetpack)', 'jetpack' ), $widget_ops, $control_ops ); + } + + function widget( $args, $instance ) { + extract( $args ); + + echo $before_widget; + + $title = apply_filters( 'widget_title', $instance['title'] ); + + if ( $title ) + echo $before_title . esc_html( $title ) . $after_title; + + if ( '' != $instance['img_url'] ) { + + $output = '<img src="' . esc_attr( $instance['img_url'] ) .'" '; + if ( '' != $instance['alt_text'] ) + $output .= 'alt="' . esc_attr( $instance['alt_text'] ) .'" '; + if ( '' != $instance['img_title'] ) + $output .= 'title="' . esc_attr( $instance['img_title'] ) .'" '; + if ( '' == $instance['caption'] ) + $output .= 'class="align' . esc_attr( $instance['align'] ) . '" '; + if ( '' != $instance['img_width'] ) + $output .= 'width="' . esc_attr( $instance['img_width'] ) .'" '; + if ( '' != $instance['img_height'] ) + $output .= 'height="' . esc_attr( $instance['img_height'] ) .'" '; + $output .= '/>'; + if ( '' != $instance['link'] ) + $output = '<a href="' . esc_attr( $instance['link'] ) . '">' . $output . '</a>'; + if ( '' != $instance['caption'] ) + $output = '[caption align="align' . esc_attr( $instance['align'] ) . '" width="' . esc_attr( $instance['img_width'] ) .'" caption="' . esc_attr( $instance['caption'] ) . '"]' . $output . '[/caption]'; + + echo '<div style="overflow:hidden;">' . do_shortcode( $output ) . '</div>'; + } + + echo "\n" . $after_widget; + } + + function update( $new_instance, $old_instance ) { + $instance = $old_instance; + + $instance['title'] = strip_tags( $new_instance['title'] ); + $instance['img_url'] = esc_url( $new_instance['img_url'], null, 'display' ); + $instance['alt_text'] = strip_tags( $new_instance['alt_text'] ); + $instance['img_title'] = strip_tags( $new_instance['img_title'] ); + $instance['caption'] = strip_tags( $new_instance['caption'] ); + $instance['align'] = $new_instance['align']; + $instance['img_width'] = absint( $new_instance['img_width'] ); + $instance['img_height'] = absint( $new_instance['img_height'] ); + $instance['link'] = esc_url( $new_instance['link'], null, 'display' ); + + return $instance; + } + + function form( $instance ) { + // Defaults + $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'img_url' => '', 'alt_text' => '', 'img_title' => '', 'caption' => '', 'align' => 'none', 'img_width' => '', 'img_height' => '', 'link' => '' ) ); + + $title = esc_attr( $instance['title'] ); + $img_url = esc_url( $instance['img_url'], null, 'display' ); + $alt_text = esc_attr( $instance['alt_text'] ); + $img_title = esc_attr( $instance['img_title'] ); + $caption = esc_attr( $instance['caption'] ); + $align = esc_attr( $instance['align'] ); + $img_width = esc_attr( $instance['img_width'] ); + $img_height = esc_attr( $instance['img_height'] ); + + if ( !empty( $instance['img_url'] ) ) { + // Download the url to a local temp file and then process it with getimagesize so we can filter out domains which are blocking us + $tmp_file = download_url( $instance['img_url'], 30 ); + if ( ! is_wp_error( $tmp_file ) ) { + $size = getimagesize( $tmp_file ); + + if ( '' == $instance['img_width'] ) { + $width = $size[0]; + $img_width = $width; + } else { + $img_width = absint( $instance['img_width'] ); + } + + if ( '' == $instance['img_height'] ) { + $height = $size[1]; + $img_height = $height; + } else { + $img_height = absint( $instance['img_height'] ); + } + + unlink( $tmp_file ); + } + } + + $link = esc_url( $instance['link'], null, 'display' ); + + echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . ' + <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" /> + </label></p> + <p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . ' + <input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" /> + </label></p> + <p><label for="' . $this->get_field_id( 'alt_text' ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a> + <input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" /> + </label></p> + <p><label for="' . $this->get_field_id( 'img_title' ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a> + <input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" /> + </label></p> + <p><label for="' . $this->get_field_id( 'caption' ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a> + <input class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" type="text" value="' . $caption . '" /> + </label></p>'; + + $alignments = array( + 'none' => __( 'None', 'jetpack' ), + 'left' => __( 'Left', 'jetpack' ), + 'center' => __( 'Center', 'jetpack' ), + 'right' => __( 'Right', 'jetpack' ), + ); + echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . ' + <select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">'; + foreach ( $alignments as $alignment => $alignment_name ) { + echo '<option value="' . esc_attr( $alignment ) . '" '; + if ( $alignment == $align ) + echo 'selected="selected" '; + echo '>' . esc_html($alignment_name) . "</option>\n"; + } + echo '</select></label></p>'; + + echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width:', 'jetpack' ) . ' + <input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" /> + </label> + <label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height:', 'jetpack' ) . ' + <input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" /> + </label><br /> + <small>' . esc_html__( "If empty, we will attempt to determine the image size.", 'jetpack' ) . '</small></p> + <p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . ' + <input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" /> + </label></p>'; + } + +} //Class Jetpack_Image_Widget + +function jetpack_image_widget_init() { + register_widget( 'Jetpack_Image_Widget' ); +} +add_action( 'widgets_init', 'jetpack_image_widget_init' ); diff --git a/plugins/jetpack/modules/widgets/rsslinks-widget.php b/plugins/jetpack/modules/widgets/rsslinks-widget.php new file mode 100644 index 00000000..15309151 --- /dev/null +++ b/plugins/jetpack/modules/widgets/rsslinks-widget.php @@ -0,0 +1,168 @@ +<?php +/** + * Module Name: RSS Links Widget + * Module Description: Easily add RSS links to your theme's sidebar. + * Sort Order: 20 + * First Introduced: 1.2 + */ + +class Jetpack_RSS_Links_Widget extends WP_Widget { + + function Jetpack_RSS_Links_Widget() { + $widget_ops = array('classname' => 'widget_rss_links', 'description' => __("Links to your blog's RSS feeds", 'jetpack') ); + $this->WP_Widget('rss_links', __('RSS Links (Jetpack)', 'jetpack'), $widget_ops); + } + + function widget($args, $instance) { + extract( $args ); + + $title = apply_filters( 'widget_title', $instance['title'] ); + echo $before_widget; + + if ( $title ) + echo $before_title . stripslashes( $title ) . $after_title; + + if ( 'text' == $instance['format'] ) echo '<ul>'; + + if ( 'posts' == $instance['display'] ) { + $this->_rss_link('posts', $instance); + } elseif ( 'comments' == $instance['display'] ) { + $this->_rss_link('comments', $instance); + } elseif ( 'posts-comments' == $instance['display'] ) { + $this->_rss_link('posts', $instance); + $this->_rss_link('comments', $instance); + } + + if ( 'text' == $instance['format'] ) echo '</ul>'; + + echo "\n" . $after_widget; + } + + function update($new_instance, $old_instance) { + $instance = $old_instance; + + $instance['title'] = wp_filter_nohtml_kses( $new_instance['title'] ); + $instance['display'] = $new_instance['display']; + $instance['format'] = $new_instance['format']; + $instance['imagesize'] = $new_instance['imagesize']; + $instance['imagecolor'] = $new_instance['imagecolor']; + + return $instance; + } + + function form($instance) { + $instance = wp_parse_args( (array) $instance, array('title' => '', 'display' => 'posts-comments', 'format' => 'text') ); + + $title = stripslashes( $instance['title'] ); + $display = $instance['display']; + $format = $instance['format']; + $image_size = isset( $instance['imagesize'] ) ? $instance['imagesize'] : 0 ; + $image_color = isset( $instance['imagecolor'] ) ? $instance['imagecolor'] : 'red'; + + echo '<p><label for="' . $this->get_field_id('title') . '">' . esc_html__('Title:', 'jetpack') . ' + <input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . esc_attr($title) . '" /> + </label></p>'; + + $displays = array( + 'posts' => __('Posts', 'jetpack'), + 'comments' => __('Comments', 'jetpack'), + 'posts-comments' => __('Posts & Comments', 'jetpack') + ); + echo '<p><label for="' . $this->get_field_id('display') . '">' . esc_html__('Feed(s) to Display:', 'jetpack') . ' + <select class="widefat" id="' . $this->get_field_id('display') . '" name="' . $this->get_field_name('display') . '">'; + foreach ( $displays as $display_option => $label ) { + echo '<option value="' . esc_attr($display_option) . '"'; + if ( $display_option == $display ) echo ' selected="selected"'; + echo '>' . esc_html($label) . '</option>' . "\n"; + } + echo '</select></label></p>'; + + $formats = array( + 'text' => __('Text Link', 'jetpack'), + 'image' => __('Image Link', 'jetpack'), + 'text-image' => __('Text & Image Links', 'jetpack') + ); + echo '<p><label for="' . $this->get_field_id('format') . '">' . __('Format:', 'jetpack') . ' + <select class="widefat" id="' . $this->get_field_id('format') . '" name="' . $this->get_field_name('format') . '" onchange="if ( this.value == \'text\' ) jQuery( \'#' . $this->get_field_id('image-settings') . '\' ).fadeOut(); else jQuery( \'#' . $this->get_field_id('image-settings') . '\' ).fadeIn();">'; + foreach ( $formats as $format_option => $label ) { + echo '<option value="' . esc_attr($format_option) . '"'; + if ( $format_option == $format ) echo ' selected="selected"'; + echo '>' . esc_html($label) . '</option>' . "\n"; + } + echo '</select></label></p>'; + + echo '<div id="' . $this->get_field_id('image-settings') . '"'; + if ( 'text' == $format ) echo ' style="display: none;"'; + echo '><h3>' . esc_html__('Image Settings:', 'jetpack') . '</h3>'; + + $sizes = array( + 'small' => __('Small', 'jetpack'), + 'medium' => __('Medium', 'jetpack'), + 'large' => __('Large', 'jetpack') + ); + echo '<p><label for="' . $this->get_field_id('imagesize') . '">' . esc_html__('Image Size:', 'jetpack') . ' + <select class="widefat" id="' . $this->get_field_id('imagesize') . '" name="' . $this->get_field_name('imagesize') . '">'; + foreach ( $sizes as $size => $label ) { + echo '<option value="' . esc_attr($size) . '"'; + if ( $size == $image_size ) echo ' selected="selected"'; + echo '>' . esc_html($label) . '</option>' . "\n"; + } + echo '</select></label></p>'; + + $colors = array( + 'red' => __('Red', 'jetpack'), + 'orange' => __('Orange', 'jetpack'), + 'green' => __('Green', 'jetpack'), + 'blue' => __('Blue', 'jetpack'), + 'purple' => __('Purple', 'jetpack'), + 'pink' => __('Pink', 'jetpack'), + 'silver' => __('Silver', 'jetpack'), + ); + echo '<p><label for="' . $this->get_field_id('imagecolor') . '">' . esc_html__('Image Color:', 'jetpack') . ' + <select class="widefat" id="' . $this->get_field_id('imagecolor') . '" name="' . $this->get_field_name('imagecolor') . '">'; + foreach ( $colors as $color => $label ) { + echo '<option value="' . esc_attr($color) . '"'; + if ( $color == $image_color ) echo ' selected="selected"'; + echo '>' . esc_html($label) . '</option>' . "\n"; + } + echo '</select></label></p></div>'; + } + + function _rss_link( $type = 'posts', $args ) { + if ( 'posts' == $type ) { + $type_text = __( 'Posts', 'jetpack' ); + $rss_type = 'rss2_url'; + } elseif ( 'comments' == $type ) { + $type_text = __( 'Comments', 'jetpack' ); + $rss_type = 'comments_rss2_url'; + } + + $subscribe_to = sprintf( __( 'Subscribe to %s', 'jetpack'), $type_text ); + + $link_item = ''; + $format = $args['format']; + if ( 'image' == $format || 'text-image' == $format ) + $link_item = '<a href="' . get_bloginfo($rss_type) . '" title="' . esc_attr( $subscribe_to ) . '"><img src="' . esc_url( plugins_url( '_inc/images/rss/' . $args['imagecolor'] . '-' . $args['imagesize'] . '.png', dirname( dirname( __FILE__ ) ) ) ) . '" alt="RSS Feed" /></a>'; + if ( 'text-image' == $format ) + $link_item .= ' <a href="' . get_bloginfo($rss_type) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__('RSS - ' . $type_text, 'jetpack'). '</a>'; + if ( 'text' == $format ) + $link_item = '<a href="' . get_bloginfo($rss_type) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__('RSS - ' . $type_text, 'jetpack'). '</a>'; + + if ( 'text' == $format ) + echo '<li>'; + else + echo '<p>'; + echo $link_item; + if ( 'text' == $format ) + echo '</li>'; + else + echo '</p>'; + + } +} //Class Jetpack_RSS_Links_Widget + +function jetpack_rss_links_widget_init() { + register_widget('Jetpack_RSS_Links_Widget'); +} +add_action( 'widgets_init', 'jetpack_rss_links_widget_init' ); +?> diff --git a/plugins/jetpack/modules/widgets/twitter-widget.php b/plugins/jetpack/modules/widgets/twitter-widget.php new file mode 100644 index 00000000..a7f359cd --- /dev/null +++ b/plugins/jetpack/modules/widgets/twitter-widget.php @@ -0,0 +1,273 @@ +<?php +/** + * Module Name: Twitter Widget + * Module Description: Display the latest updates from a Twitter user inside your theme's widgets. + * Sort Order: 20 + * First Introduced: 1.1 + */ + +/* +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +if ( !function_exists('wpcom_time_since') ) : +/* + * Time since function taken from WordPress.com + */ + +function wpcom_time_since( $original, $do_more = 0 ) { + // array of time period chunks + $chunks = array( + array( 60 * 60 * 24 * 365 , 'year' ), + array( 60 * 60 * 24 * 30 , 'month' ), + array( 60 * 60 * 24 * 7, 'week' ), + array( 60 * 60 * 24 , 'day' ), + array( 60 * 60 , 'hour' ), + array( 60 , 'minute' ), + ); + + $today = time(); + $since = $today - $original; + + for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) { + $seconds = $chunks[$i][0]; + $name = $chunks[$i][1]; + + if ( ( $count = floor( $since / $seconds ) ) != 0 ) + break; + } + + $print = ( $count == 1 ) ? '1 ' . $name : "$count {$name}s"; + + if ( $i + 1 < $j ) { + $seconds2 = $chunks[$i + 1][0]; + $name2 = $chunks[$i + 1][1]; + + // add second item if it's greater than 0 + if ( ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) && $do_more ) + $print .= ( $count2 == 1 ) ? ', 1 ' . $name2 : ", $count2 {$name2}s"; + } + return $print; +} +endif; + +class Wickett_Twitter_Widget extends WP_Widget { + + function Wickett_Twitter_Widget() { + $widget_ops = array( 'classname' => 'widget_twitter', 'description' => __( 'Display your tweets from Twitter', 'jetpack' ) ); + parent::WP_Widget( 'twitter', __( 'Twitter (Jetpack)', 'jetpack' ), $widget_ops ); + } + + function widget( $args, $instance ) { + extract( $args ); + + $account = trim( urlencode( $instance['account'] ) ); + if ( empty($account) ) return; + $title = apply_filters( 'widget_title', $instance['title'] ); + if ( empty( $title ) ) $title = __( 'Twitter Updates', 'jetpack' ); + $show = absint( $instance['show'] ); // # of Updates to show + if ( $show > 200 ) // Twitter paginates at 200 max tweets. update() should not have accepted greater than 20 + $show = 200; + $hidereplies = (bool) $instance['hidereplies']; + $include_retweets = (bool) $instance['includeretweets']; + + echo "{$before_widget}{$before_title}<a href='" . esc_url( "http://twitter.com/{$account}" ) . "'>" . esc_html($title) . "</a>{$after_title}"; + + if ( false === ( $tweets = get_transient( 'widget-twitter-' . $this->number ) ) ) { + $params = array( + 'screen_name'=>$account, // Twitter account name + 'trim_user'=>true, // only basic user data (slims the result) + 'include_entities' => true + ); + + /** + * The exclude_replies parameter filters out replies on the server. If combined with count it only filters that number of tweets (not all tweets up to the requested count) + * If we are not filtering out replies then we should specify our requested tweet count + */ + if ( $hidereplies ) + $params['exclude_replies'] = true; + else + $params['count'] = $show; + if ( $include_retweets ) + $params['include_rts'] = true; + $twitter_json_url = esc_url_raw( 'http://api.twitter.com/1/statuses/user_timeline.json?' . http_build_query( $params ), array( 'http', 'https' ) ); + unset( $params ); + $response = wp_remote_get( $twitter_json_url, array( 'User-Agent' => 'WordPress.com Twitter Widget' ) ); + $response_code = wp_remote_retrieve_response_code( $response ); + if ( 200 == $response_code ) { + $tweets = wp_remote_retrieve_body( $response ); + $tweets = json_decode( $tweets, true ); + $expire = 900; + if ( !is_array( $tweets ) || isset( $tweets['error'] ) ) { + $tweets = 'error'; + $expire = 300; + } + } else { + $tweets = 'error'; + $expire = 300; + set_transient( 'widget-twitter-response-code-' . $this->number, $response_code, $expire ); + } + + set_transient( 'widget-twitter-' . $this->number, $tweets, $expire ); + } + + if ( 'error' != $tweets ) : + $before_timesince = ' '; + if ( isset( $instance['beforetimesince'] ) && !empty( $instance['beforetimesince'] ) ) + $before_timesince = esc_html( $instance['beforetimesince'] ); + $before_tweet = ''; + if ( isset( $instance['beforetweet'] ) && !empty( $instance['beforetweet'] ) ) + $before_tweet = stripslashes( wp_filter_post_kses( $instance['beforetweet'] ) ); + + echo '<ul class="tweets">' . "\n"; + + $tweets_out = 0; + + foreach ( (array) $tweets as $tweet ) { + if ( $tweets_out >= $show ) + break; + + if ( empty( $tweet['text'] ) ) + continue; + + $text = esc_html( $tweet['text'] ); + + // expand t.co links + if ( !empty( $tweet['entities']['urls'] ) ) { + foreach ( $tweet['entities']['urls'] as $entity_url ) { + if ( !empty( $entity_url['expanded_url'] ) ) { + $expanded = '<a href="' . esc_url( $entity_url['expanded_url'] ) . '"> ' . esc_html( $entity_url['display_url'] ) . '</a>'; + $text = str_replace( $entity_url['url'], $expanded, $text ); + } + } + } + + $text = make_clickable( $text ); + + /* + * Create links from plain text based on Twitter patterns + * @link http://github.com/mzsanford/twitter-text-rb/blob/master/lib/regex.rb Official Twitter regex + */ + $text = preg_replace_callback( '/(^|[^0-9A-Z&\/]+)(#|\xef\xbc\x83)([0-9A-Z_]*[A-Z_]+[a-z0-9_\xc0-\xd6\xd8-\xf6\xf8\xff]*)/iu', array( $this, '_wpcom_widget_twitter_hashtag' ), $text ); + $text = preg_replace_callback( '/([^a-zA-Z0-9_]|^)([@\xef\xbc\xa0]+)([a-zA-Z0-9_]{1,20})(\/[a-zA-Z][a-zA-Z0-9\x80-\xff-]{0,79})?/u', array( $this, '_wpcom_widget_twitter_username' ), $text ); + if ( isset( $tweet['id_str'] ) ) + $tweet_id = urlencode( $tweet['id_str'] ); + else + $tweet_id = urlencode( $tweet['id'] ); + $created_at = str_replace( '+0000', '', $tweet['created_at'] ) . ' UTC'; // Twitter's datetime format is strange, refactor for the sake of PHP4 + echo "<li>{$before_tweet}{$text}{$before_timesince}<a href=\"" . esc_url( "http://twitter.com/{$account}/statuses/{$tweet_id}" ) . '" class="timesince">' . str_replace( ' ', ' ', wpcom_time_since( strtotime( $created_at ) ) ) . " ago</a></li>\n"; + unset( $tweet_id ); + $tweets_out++; + } + + echo "</ul>\n"; + else : + if ( 401 == get_transient( 'widget-twitter-response-code-' . $this->number ) ) + echo '<p>' . wp_kses( sprintf( __( 'Error: Please make sure the Twitter account is <a href="%s">public</a>.', 'jetpack' ), 'http://support.twitter.com/forums/10711/entries/14016' ), array( 'a' => array( 'href' => true ) ) ) . '</p>'; + else + echo '<p>' . esc_html__( 'Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'jetpack' ) . '</p>'; + endif; + + echo $after_widget; + } + + function update( $new_instance, $old_instance ) { + $instance = $old_instance; + + $instance['account'] = trim( strip_tags( stripslashes( $new_instance['account'] ) ) ); + $instance['account'] = str_replace( 'http://twitter.com/', '', $instance['account'] ); + $instance['account'] = str_replace( '/', '', $instance['account'] ); + $instance['account'] = str_replace( '@', '', $instance['account'] ); + $instance['account'] = str_replace( '#!', '', $instance['account'] ); // account for the Ajax URI + $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) ); + $instance['show'] = absint( $new_instance['show'] ); + $instance['hidereplies'] = isset( $new_instance['hidereplies'] ); + $instance['includeretweets'] = isset( $new_instance['includeretweets'] ); + $instance['beforetimesince'] = $new_instance['beforetimesince']; + + delete_transient( 'widget-twitter-' . $this->number ); + delete_transient( 'widget-twitter-response-code-' . $this->number ); + + return $instance; + } + + function form( $instance ) { + //Defaults + $instance = wp_parse_args( (array) $instance, array( 'account' => '', 'title' => '', 'show' => 5, 'hidereplies' => false, 'includeretweets' => false, 'beforetimesince' => '' ) ); + + $account = esc_attr( $instance['account'] ); + $title = esc_attr( $instance['title'] ); + $show = absint( $instance['show'] ); + if ( $show < 1 || 20 < $show ) + $show = 5; + $hidereplies = (bool) $instance['hidereplies']; + $include_retweets = (bool) $instance['includeretweets']; + $before_timesince = esc_attr( $instance['beforetimesince'] ); + + echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title:', 'jetpack' ) . ' + <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" /> + </label></p> + <p><label for="' . $this->get_field_id( 'account' ) . '">' . esc_html__( 'Twitter username:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/twitter-widget/#twitter-username" target="_blank">( ? )</a> + <input class="widefat" id="' . $this->get_field_id( 'account' ) . '" name="' . $this->get_field_name( 'account' ) . '" type="text" value="' . $account . '" /> + </label></p> + <p><label for="' . $this->get_field_id( 'show' ) . '">' . esc_html__( 'Maximum number of tweets to show:', 'jetpack' ) . ' + <select id="' . $this->get_field_id( 'show' ) . '" name="' . $this->get_field_name( 'show' ) . '">'; + + for ( $i = 1; $i <= 20; ++$i ) + echo "<option value='$i' " . ( $show == $i ? "selected='selected'" : '' ) . ">$i</option>"; + + echo ' </select> + </label></p> + <p><label for="' . $this->get_field_id( 'hidereplies' ) . '"><input id="' . $this->get_field_id( 'hidereplies' ) . '" class="checkbox" type="checkbox" name="' . $this->get_field_name( 'hidereplies' ) . '"'; + if ( $hidereplies ) + echo ' checked="checked"'; + echo ' /> ' . esc_html__( 'Hide replies', 'jetpack' ) . '</label></p>'; + + echo '<p><label for="' . $this->get_field_id( 'includeretweets' ) . '"><input id="' . $this->get_field_id( 'includeretweets' ) . '" class="checkbox" type="checkbox" name="' . $this->get_field_name( 'includeretweets' ) . '"'; + if ( $include_retweets ) + echo ' checked="checked"'; + echo ' /> ' . esc_html__( 'Include retweets', 'jetpack' ) . '</label></p>'; + + echo '<p><label for="' . $this->get_field_id( 'beforetimesince' ) . '">' . esc_html__( 'Text to display between tweet and timestamp:', 'jetpack' ) . ' + <input class="widefat" id="' . $this->get_field_id( 'beforetimesince' ) . '" name="' . $this->get_field_name( 'beforetimesince' ) . '" type="text" value="' . $before_timesince . '" /> + </label></p>'; + } + + /** + * Link a Twitter user mentioned in the tweet text to the user's page on Twitter. + * + * @param array $matches regex match + * @return string Tweet text with inserted @user link + */ + function _wpcom_widget_twitter_username( $matches ) { // $matches has already been through wp_specialchars + return "$matches[1]@<a href='" . esc_url( 'http://twitter.com/' . urlencode( $matches[3] ) ) . "'>$matches[3]</a>"; + } + + /** + * Link a Twitter hashtag with a search results page on Twitter.com + * + * @param array $matches regex match + * @return string Tweet text with inserted #hashtag link + */ + function _wpcom_widget_twitter_hashtag( $matches ) { // $matches has already been through wp_specialchars + return "$matches[1]<a href='" . esc_url( 'http://twitter.com/search?q=%23' . urlencode( $matches[3] ) ) . "'>#$matches[3]</a>"; + } + +} + +add_action( 'widgets_init', 'wickett_twitter_widget_init' ); +function wickett_twitter_widget_init() { + register_widget( 'Wickett_Twitter_Widget' ); +} |