diff options
author | Theo Chatzimichos <tampakrap@gentoo.org> | 2013-10-03 21:53:38 +0200 |
---|---|---|
committer | Theo Chatzimichos <tampakrap@gentoo.org> | 2013-10-03 21:53:38 +0200 |
commit | 61f7269ffabd11b7de56507c69191be42d7cfa60 (patch) | |
tree | 2ad81bec8d0c124019b23c841d80882c303801bc /plugins/jetpack/modules/widgets | |
parent | forgot to include new files of jetpack (diff) | |
download | blogs-gentoo-61f7269ffabd11b7de56507c69191be42d7cfa60.tar.gz blogs-gentoo-61f7269ffabd11b7de56507c69191be42d7cfa60.tar.bz2 blogs-gentoo-61f7269ffabd11b7de56507c69191be42d7cfa60.zip |
update jetpack
Diffstat (limited to 'plugins/jetpack/modules/widgets')
-rw-r--r-- | plugins/jetpack/modules/widgets/gallery.php | 392 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/gallery/css/admin.css | 11 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/gallery/css/rtl/admin-rtl.css | 13 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/gallery/js/admin.js | 207 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/gallery/js/gallery.js | 12 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/gallery/templates/form.php | 89 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/top-posts.php | 2 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/twitter-timeline.php | 15 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/twitter-widget.php | 273 | ||||
-rw-r--r-- | plugins/jetpack/modules/widgets/twitter.php | 407 |
10 files changed, 737 insertions, 684 deletions
diff --git a/plugins/jetpack/modules/widgets/gallery.php b/plugins/jetpack/modules/widgets/gallery.php new file mode 100644 index 00000000..0d33f5c5 --- /dev/null +++ b/plugins/jetpack/modules/widgets/gallery.php @@ -0,0 +1,392 @@ +<?php + +/* +Plugin Name: Gallery +Description: Gallery widget +Author: Automattic, Inc. +Version: 1.0 +Author URI: http://automattic.com +*/ + +class Jetpack_Gallery_Widget extends WP_Widget { + const THUMB_SIZE = 45; + const DEFAULT_WIDTH = 265; + + protected $_instance_width ; + + public function __construct() { + $widget_ops = array( + 'classname' => 'widget-gallery', + 'description' => __( 'Display a photo gallery or slideshow', 'jetpack' ) + ); + $control_ops = array( 'width' => 250 ); + + add_action( 'admin_init', array( $this, 'admin_init' ) ); + + $this->WP_Widget( 'gallery', apply_filters( 'jetpack_widget_name', __( 'Gallery', 'jetpack' ) ), $widget_ops, $control_ops ); + } + + /** + * @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 ) { + $this->enqueue_frontend_scripts(); + + extract( $args ); + + $instance['attachments'] = $this->get_attachments( $instance ); + + $classes = array(); + + $classes[] = 'widget-gallery-' . $instance['type']; + + // Due to a bug in the carousel plugin, carousels will be triggered for all tiled galleries that exist on a page + // with other tiled galleries, regardless of whether or not the widget was set to Carousel mode. The onClick selector + // is simply too broad, since it was not written with widgets in mind. This special class prevents that behavior, via + // an override handler in gallery.js + if( 'carousel' != $instance['link'] && 'slideshow' != $instance['type'] ) + $classes[] = 'no-carousel'; + else + $classes[] = 'carousel'; + + $classes = implode( ' ', $classes ); + + if ( 'carousel' == $instance['link'] ) { + require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../carousel/jetpack-carousel.php' ) ) . 'jetpack-carousel.php'; + + if ( class_exists( 'Jetpack_Carousel' ) ) { + // Create new carousel so we can use the enqueue_assets() method. Not ideal, but there is a decent amount + // of logic in that method that shouldn't be duplicated. + $carousel = new Jetpack_Carousel(); + + // First parameter is $output, which comes from filters, and causes bypass of the asset enqueuing. Passing null is correct. + $carousel->enqueue_assets( null ); + } + } + + echo $before_widget . "\n"; + + $title = apply_filters( 'widget_title', $instance['title'] ); + + if ( $title ) + echo $before_title . esc_html( $title ) . $after_title . "\n"; + + echo '<div class="' . esc_attr( $classes ) . '">' . "\n"; + + $method = $instance['type'] . '_widget'; + + // Allow the width of a gallery to be altered by themes or other code + $this->_instance_width = apply_filters( 'gallery_widget_content_width', self::DEFAULT_WIDTH, $args, $instance ); + + // Register a filter to modify the tiled_gallery_content_width, so Jetpack_Tiled_Gallery + // can appropriately size the tiles. + add_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) ); + + if ( method_exists( $this, $method ) ) + echo $this->$method( $args, $instance ); + + // Remove the stored $_instance_width, as it is no longer needed + $this->_instance_width = null; + + // Remove the filter, so any Jetpack_Tiled_Gallery in a post is not affected + remove_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) ); + + echo "\n" . '</div>'; // .widget-gallery-$type + + echo "\n" . $after_widget; + } + + /** + * Fetch the images attached to the gallery Widget + * + * @param array $instance The Widget instance for which you'd like attachments + * @return array Array of attachment objects for the Widget in $instance + */ + public function get_attachments( $instance ){ + $ids = explode( ',', $instance['ids'] ); + + $order = ( isset( $instance['random'] ) && $instance['random'] ) ? 'rand' : 'post__in'; + + $attachments_query = new WP_Query( array( + 'post__in' => $ids, + 'post_status' => 'inherit', + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + 'posts_per_page' => -1, + 'orderby' => $order + ) ); + + $attachments = $attachments_query->get_posts(); + + wp_reset_postdata(); + + return $attachments; + } + + /** + * Generate HTML for a rectangular, tiled Widget + * + * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. + * @param array $instance The Widget instance to generate HTML for + * @return string String of HTML representing a rectangular gallery + */ + public function rectangular_widget( $args, $instance ) { + if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) ) + return; + + $widget_tiled_gallery = new Jetpack_Tiled_Gallery(); + + $widget_tiled_gallery->set_atts( array( + 'link' => $instance['link'], + ) ); + + $widget_tiled_gallery->default_scripts_and_styles(); + + $html = $widget_tiled_gallery->rectangular_talavera( $instance['attachments'] ); + + return $html; + } + + /** + * Generate HTML for a square (grid style) Widget + * + * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. + * @param array $instance The Widget instance to generate HTML for + * @return string String of HTML representing a square gallery + */ + public function square_widget( $args, $instance ) { + if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) ) + return; + + $widget_tiled_gallery = new Jetpack_Tiled_Gallery(); + + $widget_tiled_gallery->set_atts( array( + 'link' => $instance['link'], + //'columns' => $instance['columns'] + ) ); + + $widget_tiled_gallery->default_scripts_and_styles(); + + $html = $widget_tiled_gallery->circle_talavera( $instance['attachments'] ); + + return $html; + } + + /** + * Generate HTML for a circular (grid style) Widget + * + * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. + * @param array $instance The Widget instance to generate HTML for + * @return string String of HTML representing a circular gallery + */ + public function circle_widget( $args, $instance ) { + if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) ) + return; + + $widget_tiled_gallery = new Jetpack_Tiled_Gallery(); + + // Tell the Tiled_Gallery what we want the images to link to + $widget_tiled_gallery->set_atts( array( + 'link' => $instance['link'], + //'columns' => $instance['columns'], + 'type' => 'circle' + ) ); + + $widget_tiled_gallery->default_scripts_and_styles(); + + $html = $widget_tiled_gallery->circle_talavera( $instance['attachments'] ); + + return $html; + } + + /** + * Generate HTML for a slideshow Widget + * + * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. + * @param array $instance The Widget instance to generate HTML for + * @return string String of HTML representing a slideshow gallery + */ + public function slideshow_widget( $args, $instance ) { + global $content_width; + + require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../shortcodes/slideshow.php' ) ) . 'slideshow.php'; + + if ( ! class_exists( 'Jetpack_Slideshow_Shortcode' ) ) + return; + + $slideshow = new Jetpack_Slideshow_Shortcode(); + + $slideshow->enqueue_scripts(); + + $gallery_instance = "widget-" . $args['widget_id']; + + $gallery = array(); + + foreach ( $instance['attachments'] as $attachment ) { + $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' ); + $attachment_image_src = $attachment_image_src[0]; // [url, width, height] + + $caption = wptexturize( strip_tags( $attachment->post_excerpt ) ); + + $gallery[] = (object) array( + 'src' => (string) esc_url_raw( $attachment_image_src ), + 'id' => (string) $attachment->ID, + 'caption' => (string) $caption, + ); + } + + $max_width = intval( get_option( 'large_size_w' ) ); + $max_height = 175; + + if ( intval( $content_width ) > 0 ) + $max_width = min( intval( $content_width ), $max_width ); + + $js_attr = array( + 'gallery' => $gallery, + 'selector' => $gallery_instance, + 'width' => $max_width, + 'height' => $max_height, + 'trans' => 'fade', + ); + + $html = $slideshow->slideshow_js( $js_attr ); + + return $html; + } + + /** + * tiled_gallery_content_width filter + * + * Used to adjust the content width of Jetpack_Tiled_Gallery's in sidebars + * + * $this->_instance_width is filtered in widget() and this filter is added then removed in widget() + * + * @param int $width int The original width value + * @return int The filtered width + */ + public function tiled_gallery_content_width( $width ) { + return $this->_instance_width; + } + + public function form( $instance ) { + $defaults = $this->defaults(); + $allowed_values = $this->allowed_values(); + + $instance = wp_parse_args( (array) $instance, $defaults ); + + include dirname( __FILE__ ) . '/gallery/templates/form.php'; + } + + public function update( $new_instance, $old_instance ) { + $instance = $this->sanitize( $new_instance ); + + return $instance; + } + + /** + * Sanitize the $instance's values to the set of allowed values. If a value is not acceptable, + * it is set to its default. + * + * Helps keep things nice and secure by whitelisting only allowed values + * + * @param array $instance The Widget instance to sanitize values for + * @return array $instance The Widget instance with values sanitized + */ + public function sanitize( $instance ) { + $allowed_values = $this->allowed_values(); + $defaults = $this->defaults(); + + foreach ( $instance as $key => $value ) { + $value = trim( $value ); + + if ( isset( $allowed_values[ $key ] ) && $allowed_values[ $key ] && ! array_key_exists( $value, $allowed_values[ $key ] ) ) + $instance[ $key ] = $defaults[ $key ]; + } + + return $instance; + } + + /** + * Return a multi-dimensional array of allowed values (and their labels) for all widget form + * elements + * + * To allow all values on an input, omit it from the returned array + * + * @return array Array of allowed values for each option + */ + public function allowed_values() { + $max_columns = 5; + + // Create an associative array of allowed column values. This just automates the generation of + // column <option>s, from 1 to $max_columns + $allowed_columns = array_combine( range( 1, $max_columns ), range( 1, $max_columns ) ); + + return array( + 'type' => array( + 'rectangular' => __( 'Tiles', 'jetpack' ), + 'square' => __( 'Square Tiles', 'jetpack' ), + 'circle' => __( 'Circles', 'jetpack' ), + 'slideshow' => __( 'Slideshow', 'jetpack' ), + ), + 'columns' => $allowed_columns, + 'link' => array( + 'carousel' => __( 'Carousel', 'jetpack' ), + 'post' => __( 'Attachment Page', 'jetpack' ), + 'file' => __( 'Media File', 'jetpack' ), + ) + ); + } + + /** + * Return an associative array of default values + * + * These values are used in new widgets as well as when sanitizing input. If a given value is not allowed, + * as defined in allowed_values(), that input is set to the default value defined here. + * + * @return array Array of default values for the Widget's options + */ + public function defaults() { + return array( + 'title' => '', + 'type' => 'rectangular', + 'ids' => '', + 'columns' => 3, + 'link' => 'carousel' + ); + } + + public function enqueue_frontend_scripts() { + wp_register_script( 'gallery-widget', plugins_url( '/gallery/js/gallery.js', __FILE__ ) ); + + wp_enqueue_script( 'gallery-widget' ); + } + + public function admin_init() { + global $pagenow; + + if ( 'widgets.php' == $pagenow ) { + wp_enqueue_media(); + + wp_enqueue_script( 'gallery-widget-admin', plugins_url( '/gallery/js/admin.js', __FILE__ ), array( + 'media-models', + 'media-views' + ) ); + + $js_settings = array( + 'thumbSize' => self::THUMB_SIZE + ); + + wp_localize_script( 'gallery-widget-admin', '_wpGalleryWidgetAdminSettings', $js_settings ); + + wp_enqueue_style( 'gallery-widget-admin', plugins_url( '/gallery/css/admin.css', __FILE__ ) ); + } + } +} + +add_action( 'widgets_init', 'jetpack_gallery_widget_init' ); + +function jetpack_gallery_widget_init() { + if ( ! method_exists( 'Jetpack', 'is_module_active' ) || Jetpack::is_module_active( 'tiled-gallery' ) ) + register_widget( 'Jetpack_Gallery_Widget' ); +} diff --git a/plugins/jetpack/modules/widgets/gallery/css/admin.css b/plugins/jetpack/modules/widgets/gallery/css/admin.css new file mode 100644 index 00000000..c3d96d80 --- /dev/null +++ b/plugins/jetpack/modules/widgets/gallery/css/admin.css @@ -0,0 +1,11 @@ +.gallery-widget-thumbs-wrapper { + margin: -5px 0 0.3em 0; +} + +.gallery-widget-thumbs img { + border: 1px solid #ccc; + padding: 2px; + background-color: #fff; + margin: 0 5px 5px 0; + float: left; +}
\ No newline at end of file diff --git a/plugins/jetpack/modules/widgets/gallery/css/rtl/admin-rtl.css b/plugins/jetpack/modules/widgets/gallery/css/rtl/admin-rtl.css new file mode 100644 index 00000000..e0e4b615 --- /dev/null +++ b/plugins/jetpack/modules/widgets/gallery/css/rtl/admin-rtl.css @@ -0,0 +1,13 @@ +/* This file was automatically generated on Mar 22 2013 21:33:14 */ + +.gallery-widget-thumbs-wrapper { + margin: -5px 0 0.3em 0; +} + +.gallery-widget-thumbs img { + border: 1px solid #ccc; + padding: 2px; + background-color: #fff; + margin: 0 0 5px 5px; + float: right; +}
\ No newline at end of file diff --git a/plugins/jetpack/modules/widgets/gallery/js/admin.js b/plugins/jetpack/modules/widgets/gallery/js/admin.js new file mode 100644 index 00000000..58afb347 --- /dev/null +++ b/plugins/jetpack/modules/widgets/gallery/js/admin.js @@ -0,0 +1,207 @@ +(function($){ + var $ids; + var $thumbs; + + $(function(){ + $( '.widgets-holder-wrap, .editwidget' ).on( 'click', '.gallery-widget-choose-images', function( event ) { + event.preventDefault(); + + var widget_form = $( this ).closest( 'form' ); + + $ids = widget_form.find( '.gallery-widget-ids' ); + $thumbs = widget_form.find( '.gallery-widget-thumbs' ); + + var idsString = $ids.val(); + + var attachments = getAttachments( idsString ); + + var selection = null; + var editing = false; + + if ( attachments ) { + var selection = getSelection( attachments ); + + editing = true; + } + + var options = { + state: 'gallery-edit', + title: wp.media.view.l10n.addMedia, + multiple: true, + editing: editing, + selection: selection + }; + + var workflow = getWorkflow( options ); + + workflow.open(); + }); + + // Setup an onchange handler to toggle various options when changing style. The different style options + // require different form inputs to be presented in the widget; this event will keep the UI in sync + // with the selected style + $( ".widget-inside" ).on( 'change', '.gallery-widget-style', setupStyleOptions); + + // Setup the Link To options for all forms currently on the page. Does the same as the onChange handler, but + // is called once to display the correct form inputs for each widget on the page + setupStyleOptions(); + }); + + var media = wp.media, + Attachment = media.model.Attachment, + Attachments = media.model.Attachments, + Query = media.model.Query, + l10n; + + // Link any localized strings. + l10n = media.view.l10n = typeof _wpMediaViewsL10n === 'undefined' ? {} : _wpMediaViewsL10n; + + /** + * wp.media.view.MediaFrame.GalleryWidget + * + * This behavior can be very nearly had by setting the workflow's state to 'gallery-edit', but + * we cannot use the custom WidgetGalleryEdit controller with it (must overide createStates(), + * which is necessary to disable the sidebar gallery settings in the media browser) + */ + media.view.MediaFrame.GalleryWidget = media.view.MediaFrame.Post.extend({ + createStates: function() { + var options = this.options; + + this.states.add([ + new media.controller.WidgetGalleryEdit({ + library: options.selection, + editing: options.editing, + menu: 'gallery' + }), + new media.controller.GalleryAdd({ + + }) + ]); + } + }); + + /** + * wp.media.controller.WidgetGalleryEdit + * + * Removes the gallery settings sidebar when editing widgets...settings are instead handled + * via the standard widget interface form + * + */ + media.controller.WidgetGalleryEdit = media.controller.GalleryEdit.extend({ + gallerySettings: function( browser ) { + return; + } + }); + + function setupStyleOptions(){ + $( '.widget-inside .gallery-widget-style' ).each( function( i ){ + var style = $( this ).val(); + + var form = $( this ).parents( 'form' ); + + switch ( style ) { + case 'slideshow': + form.find( '.gallery-widget-link-wrapper' ).hide(); + form.find( '.gallery-widget-columns-wrapper' ).hide(); + + break; + + default: + form.find( '.gallery-widget-link-wrapper' ).show(); + form.find( '.gallery-widget-columns-wrapper' ).show(); + } + }); + } + + /** + * Take a given Selection of attachments and a thumbs wrapper div (jQuery object) + * and fill it with thumbnails + */ + function setupThumbs( selection, wrapper ){ + wrapper.empty(); + + var imageSize = _wpGalleryWidgetAdminSettings.thumbSize; + + selection.each( function( model ){ + var sizedUrl = model.get('url') + '?w=' + imageSize + '&h=' + imageSize + '&crop=true'; + + var thumb = '<img src="' + sizedUrl + '" alt="' + model.get('title') + '" \ + title="' + model.get('title') + '" width="' + imageSize + '" height="' + imageSize + '" class="thumb" />'; + + wrapper.append( thumb ); + }); + } + + /** + * Take a csv string of ids (as stored in db) and fetch a full Attachments collection + */ + function getAttachments( idsString ) { + if( ! idsString ) + return null; + + // Found in /wp-includes/js/media-editor.js + var shortcode = wp.shortcode.next( 'gallery', '[gallery ids="' + idsString + '"]' ); + + // Ignore the rest of the match object, to give attachments() below what it expects + shortcode = shortcode.shortcode; + + var attachments = wp.media.gallery.attachments( shortcode ); + + return attachments; + } + + /** + * Take an Attachments collection and return a corresponding Selection model that can be + * passed to a MediaFrame to prepopulate the gallery picker + */ + function getSelection( attachments ) { + var selection = new wp.media.model.Selection( attachments.models, { + props: attachments.props.toJSON(), + multiple: true + }); + + selection.gallery = attachments.gallery; + + // Fetch the query's attachments, and then break ties from the + // query to allow for sorting. + selection.more().done( function() { + // Break ties with the query. + selection.props.set( { query: false } ); + selection.unmirror(); + selection.props.unset( 'orderby' ); + }); + + return selection; + } + + /** + * Create a media 'workflow' (MediaFrame). This is the main entry point for the media picker + */ + function getWorkflow( options ) { + var workflow = new wp.media.view.MediaFrame.GalleryWidget( options ); + + workflow.on( 'update', function( selection ) { + var state = workflow.state(); + + selection = selection || state.get( 'selection' ); + + if ( ! selection ) + return; + + // Map the Models down into a simple array of ids that can be easily imploded to a csv string + var ids = selection.map( function( model ){ + return model.get( 'id' ); + } ); + + var id_string = ids.join( ',' ); + + $ids.val( id_string ); + + setupThumbs( selection, $thumbs ); + }, this ); + + workflow.setState( workflow.options.state ); + + return workflow; + } +})(jQuery); diff --git a/plugins/jetpack/modules/widgets/gallery/js/gallery.js b/plugins/jetpack/modules/widgets/gallery/js/gallery.js new file mode 100644 index 00000000..b5764f87 --- /dev/null +++ b/plugins/jetpack/modules/widgets/gallery/js/gallery.js @@ -0,0 +1,12 @@ +(function($){ + $(function(){ + // Fixes a bug with carousels being triggered even when a widget's Link To option is not set to carousel. + // Happens when another gallery is loaded on the page, either in a post or separate widget + $( '.widget-gallery .no-carousel .tiled-gallery-item a' ).on( 'click', function( event ){ + // Have to trigger default, instead of carousel + event.stopPropagation(); + + return true; + }); + }); +})(jQuery);
\ No newline at end of file diff --git a/plugins/jetpack/modules/widgets/gallery/templates/form.php b/plugins/jetpack/modules/widgets/gallery/templates/form.php new file mode 100644 index 00000000..08833657 --- /dev/null +++ b/plugins/jetpack/modules/widgets/gallery/templates/form.php @@ -0,0 +1,89 @@ +<p> + <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:' , 'jetpack' ); ?> + <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" + type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> + </label> +</p> + +<p> + <label> + <?php esc_html_e( 'Images:' , 'jetpack' ); ?> + </label> +</p> + +<div class="gallery-widget-thumbs-wrapper"> + <div class="gallery-widget-thumbs"> + <?php + + // Add the thumbnails to the widget box + $attachments = $this->get_attachments( $instance ); + + foreach( $attachments as $attachment ){ + $url = add_query_arg( array( + 'w' => self::THUMB_SIZE, + 'h' => self::THUMB_SIZE, + 'crop' => 'true' + ), wp_get_attachment_url( $attachment->ID ) ); + + ?> + + <img src="<?php echo esc_url( $url ); ?>" title="<?php echo esc_attr( $attachment->post_title ); ?>" alt="<?php echo esc_attr( $attachment->post_title ); ?>" + width="<?php echo self::THUMB_SIZE; ?>" height="<?php echo self::THUMB_SIZE; ?>" class="thumb" /> + <?php } ?> + </div> + + <div style="clear: both;"></div> +</div> + +<p> + <a class="button gallery-widget-choose-images" title="Choose Images"><span class="wp-media-buttons-icon"></span> Choose Images</a> +</p> + +<p class="gallery-widget-link-wrapper"> + <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php esc_html_e( 'Link To:' , 'jetpack' ); ?></label> + <select name="<?php echo $this->get_field_name( 'link' ); ?>" id="<?php echo $this->get_field_id( 'link' ); ?>" class="widefat"> + <?php foreach ( $allowed_values['link'] as $key => $label ) { + $selected = ''; + + if ( $instance['link'] == $key ) { + $selected = "selected='selected' "; + } ?> + + <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php esc_html_e( $label , 'jetpack' ); ?></option> + <?php } ?> + </select> +</p> + +<p> + <label for="<?php echo $this->get_field_id( 'random' ); ?>"><?php esc_html_e( 'Random Order:' , 'jetpack' ); ?></label> + <?php $checked = ''; + + if ( isset( $instance['random'] ) && $instance['random'] ) + $checked = 'checked="checked"'; + + ?> + <input name="<?php echo $this->get_field_name( 'random' ); ?>" id="<?php echo $this->get_field_id( 'random' ); ?>" type="checkbox" <?php echo $checked; ?>> +</p> + +<p class="gallery-widget-style-wrapper"> + <label for="<?php echo $this->get_field_id( 'type' ); ?>"><?php esc_html_e( 'Style:' , 'jetpack' ); ?></label> + <select name="<?php echo $this->get_field_name( 'type' ); ?>" id="<?php echo $this->get_field_id( 'type' ); ?>" class="widefat gallery-widget-style"> + <?php foreach ( $allowed_values['type'] as $key => $label ) { + $selected = ''; + + if ( $instance['type'] == $key ) { + $selected = "selected='selected' "; + } ?> + + <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php esc_html_e( $label, 'jetpack' ); ?></option> + <?php } ?> + </select> +</p> + +<?php + + +?> + +<?php // Hidden input to hold the selected image ids as a csv list ?> +<input type="hidden" class="gallery-widget-ids" name="<?php echo $this->get_field_name( 'ids' ); ?>" id="<?php echo $this->get_field_id( 'ids' ); ?>" value="<?php echo esc_attr( $instance['ids'] ); ?>" /> diff --git a/plugins/jetpack/modules/widgets/top-posts.php b/plugins/jetpack/modules/widgets/top-posts.php index ec55fe74..252903aa 100644 --- a/plugins/jetpack/modules/widgets/top-posts.php +++ b/plugins/jetpack/modules/widgets/top-posts.php @@ -222,7 +222,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { function get_by_views( $count ) { global $wpdb; - $post_view_posts = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => 10 ) ); + $post_view_posts = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => 11 ) ); if ( !$post_view_posts ) { return array(); } diff --git a/plugins/jetpack/modules/widgets/twitter-timeline.php b/plugins/jetpack/modules/widgets/twitter-timeline.php index 4ab6fcb4..75235294 100644 --- a/plugins/jetpack/modules/widgets/twitter-timeline.php +++ b/plugins/jetpack/modules/widgets/twitter-timeline.php @@ -49,7 +49,7 @@ class Jetpack_Twitter_Timeline_Widget extends WP_Widget { if ( $instance['title'] ) echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; - $data_attribs = array( 'widget-id', 'theme', 'link-color', 'border-color', 'chrome' ); + $data_attribs = array( 'widget-id', 'theme', 'link-color', 'border-color', 'chrome', 'tweet-limit' ); $attribs = array( 'width', 'height', 'lang' ); // Start tag output @@ -57,6 +57,9 @@ class Jetpack_Twitter_Timeline_Widget extends WP_Widget { foreach ( $data_attribs as $att ) { if ( !empty( $instance[$att] ) ) { + if ( 'tweet-limit' == $att && 0 === $instance[$att] ) + continue; + if ( is_array( $instance[$att] ) ) echo ' data-' . esc_attr( $att ) . '="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"'; else @@ -94,8 +97,9 @@ class Jetpack_Twitter_Timeline_Widget extends WP_Widget { $instance['title'] = sanitize_text_field( $new_instance['title'] ); $instance['width'] = (int) $new_instance['width']; $instance['height'] = (int) $new_instance['height']; - $instance['width'] = ( 0 !== (int) $instance['width'] ) ? (int) $instance['width'] : 225; - $instance['height'] = ( 0 !== (int) $instance['height'] ) ? (int) $instance['height'] : 400; + $instance['width'] = ( 0 !== (int) $new_instance['width'] ) ? (int) $new_instance['width'] : 225; + $instance['height'] = ( 0 !== (int) $new_instance['height'] ) ? (int) $new_instance['height'] : 400; + $instance['tweet-limit'] = (int) $new_instance['tweet-limit']; // If they entered something that might be a full URL, try to parse it out if ( is_string( $new_instance['widget-id'] ) ) { @@ -167,6 +171,11 @@ class Jetpack_Twitter_Timeline_Widget extends WP_Widget { <input class="widefat" id="<?php echo $this->get_field_id( 'height' ); ?>" name="<?php echo $this->get_field_name( 'height' ); ?>" type="text" value="<?php echo esc_attr( $instance['height'] ); ?>" /> </p> + <p> + <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"><?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?></label> + <input class="widefat" id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>" name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>" type="text" value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>" /> + </p> + <p><small> <?php echo wp_kses_post( diff --git a/plugins/jetpack/modules/widgets/twitter-widget.php b/plugins/jetpack/modules/widgets/twitter-widget.php deleted file mode 100644 index a7f359cd..00000000 --- a/plugins/jetpack/modules/widgets/twitter-widget.php +++ /dev/null @@ -1,273 +0,0 @@ -<?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' ); -} diff --git a/plugins/jetpack/modules/widgets/twitter.php b/plugins/jetpack/modules/widgets/twitter.php deleted file mode 100644 index ff49abf7..00000000 --- a/plugins/jetpack/modules/widgets/twitter.php +++ /dev/null @@ -1,407 +0,0 @@ -<?php - -/** - * Twitter widget class - * Display the latest N tweets from a Twitter screenname as a widget - * Customize screenname, maximum number of tweets displayed, show or hide @replies, and text displayed between tweet text and a timestamp - * - */ - -/** - * Register the widget for use in Appearance -> Widgets - */ -add_action( 'widgets_init', 'jetpack_twitter_widget_init' ); - -function jetpack_twitter_widget_init() { - register_widget( 'Jetpack_Widget_Twitter' ); -} - -class Jetpack_Widget_Twitter extends WP_Widget { - - function __construct() { - parent::__construct( - 'twitter', - apply_filters( 'jetpack_widget_name', __( 'Twitter', 'jetpack' ) ), - array( - 'classname' => 'widget_twitter', - 'description' => __( 'Display your Tweets from Twitter', 'jetpack' ) - ) - ); - - if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) { - add_action( 'wp_head', array( $this, 'style' ) ); - } - } - - function style() { -?> -<style type="text/css"> -.widget_twitter li { - word-wrap: break-word; -} -</style> -<?php - } - - function widget( $args, $instance ) { - $account = trim( urlencode( $instance['account'] ) ); - - if ( empty( $account ) ) { - if ( current_user_can('edit_theme_options') ) { - echo $args['before_widget']; - echo '<p>' . sprintf( __( 'Please configure your Twitter username for the <a href="%s">Twitter Widget</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>'; - echo $args['after_widget']; - } - - 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']; - $hidepublicized = (bool) $instance['hidepublicized']; - $include_retweets = (bool) $instance['includeretweets']; - $follow_button = (bool) $instance['followbutton']; - - echo "{$args['before_widget']}{$args['before_title']}<a href='" . esc_url( "http://twitter.com/{$account}" ) . "'>" . esc_html( $title ) . "</a>{$args['after_title']}"; - - $tweets = $this->fetch_twitter_user_stream( $account, $hidereplies, $show, $include_retweets ); - - if ( isset( $tweets['error'] ) && ( isset( $tweets['data'] ) && ! empty( $tweets['data'] ) ) ) - $tweets['error'] = ''; - - if ( empty( $tweets['error'] ) ) { - $before_tweet = isset( $instance['beforetweet'] ) ? stripslashes( wp_filter_post_kses( $instance['beforetweet'] ) ) : ''; - $before_timesince = ( isset( $instance['beforetimesince'] ) && ! empty( $instance['beforetimesince'] ) ) ? esc_html( $instance['beforetimesince'] ) : ' '; - - $this->display_tweets( $show, $tweets['data'], $hidepublicized, $before_tweet, $before_timesince, $account ); - - if ( $follow_button ) - $this->display_follow_button( $account ); - - add_action( 'wp_footer', array( $this, 'twitter_widget_script' ) ); - } else { - echo $tweets['error']; - } - - echo $args['after_widget']; - do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter' ); - } - - function display_tweets( $show, $tweets, $hidepublicized, $before_tweet, $before_timesince, $account ) { - $tweets_out = 0; - ?><ul class='tweets'><?php - - foreach( (array) $tweets as $tweet ) { - if ( $tweets_out >= $show ) - break; - - if ( empty( $tweet['text'] ) ) - continue; - - if( $hidepublicized && false !== strstr( $tweet['source'], 'http://publicize.wp.com/' ) ) - continue; - - $tweet['text'] = esc_html( $tweet['text'] ); // escape here so that Twitter handles in Tweets don't get mangled - $tweet = $this->expand_tco_links( $tweet ); - $tweet['text'] = make_clickable( $tweet['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 - */ - $tweet['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, '_jetpack_widget_twitter_hashtag' ), $tweet['text'] ); - $tweet['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, '_jetpack_widget_twitter_username' ), $tweet['text'] ); - - if ( isset( $tweet['id_str'] ) ) - $tweet_id = urlencode( $tweet['id_str'] ); - else - $tweet_id = urlencode( $tweet['id'] ); - - ?> - - <li> - <?php echo esc_attr( $before_tweet ) . $tweet['text'] . esc_attr( $before_timesince ) ?> - <a href="<?php echo esc_url( "http://twitter.com/{$account}/statuses/{$tweet_id}" ); ?>" class="timesince"><?php echo esc_html( str_replace( ' ', ' ', $this->time_since( strtotime( $tweet['created_at'] ) ) ) ); ?> ago</a> - </li> - - <?php - - unset( $tweet_it ); - $tweets_out++; - } - - ?></ul><?php - } - - function display_follow_button( $account ) { - global $themecolors; - - $follow_colors = isset( $themecolors['link'] ) ? " data-link-color='#{$themecolors['link']}'" : ''; - $follow_colors .= isset( $themecolors['text'] ) ? " data-text-color='#{$themecolors['text']}'" : ''; - $follow_button_attrs = " class='twitter-follow-button' data-show-count='false'{$follow_colors}"; - - ?><a href="http://twitter.com/<?php echo esc_attr( $account ); ?>" <?php echo $follow_button_attrs; ?>>Follow @<?php echo esc_attr( $account ); ?></a><?php - } - - function expand_tco_links( $tweet ) { - if ( ! empty( $tweet['entities']['urls'] ) && is_array( $tweet['entities']['urls'] ) ) { - foreach ( $tweet['entities']['urls'] as $entity_url ) { - if ( ! empty( $entity_url['expanded_url'] ) ) { - $tweet['text'] = str_replace( - $entity_url['url'], - '<a href="' . esc_url( $entity_url['expanded_url'] ) . '"> ' . esc_html( $entity_url['display_url'] ) . '</a>', - $tweet['text'] - ); - } - } - } - - return $tweet; - } - - function fetch_twitter_user_stream( $account, $hidereplies, $show, $include_retweets ) { - $tweets = get_transient( 'widget-twitter-' . $this->number ); - $the_error = get_transient( 'widget-twitter-error-' . $this->number ); - - if ( ! $tweets ) { - $params = array( - 'screen_name' => $account, // Twitter account name - 'trim_user' => true, // only basic user data (slims the result) - 'include_entities' => true - ); - - // If combined with $count, $exclude_replies only filters that number of tweets (not all tweets up to the requested 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 ); - - switch( $response_code ) { - case 200 : // process tweets and display - $tweets = json_decode( wp_remote_retrieve_body( $response ), true ); - - if ( ! is_array( $tweets ) || isset( $tweets['error'] ) ) { - do_action( 'jetpack_bump_stats_extras', 'twitter_widget', "request-fail-{$response_code}-bad-data" ); - $the_error = '<p>' . esc_html__( 'Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'jetpack' ) . '</p>'; - $tweet_cache_expire = 300; - break; - } else { - set_transient( 'widget-twitter-backup-' . $this->number, $tweets, 86400 ); // A one day backup in case there is trouble talking to Twitter - } - - do_action( 'jetpack_bump_stats_extras', 'twitter_widget', 'request-success' ); - $tweet_cache_expire = 900; - break; - case 401 : // display private stream notice - do_action( 'jetpack_bump_stats_extras', 'twitter_widget', "request-fail-{$response_code}" ); - - $tweets = array(); - $the_error = '<p>' . sprintf( esc_html__( 'Error: Please make sure the Twitter account is %1$spublic%2$s.', 'jetpack' ), '<a href="http://support.twitter.com/forums/10711/entries/14016">', '</a>' ) . '</p>'; - $tweet_cache_expire = 300; - break; - default : // display an error message - do_action( 'jetpack_bump_stats_extras', 'twitter_widget', "request-fail-{$response_code}" ); - - $tweets = get_transient( 'widget-twitter-backup-' . $this->number ); - $the_error = '<p>' . esc_html__( 'Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'jetpack' ) . '</p>'; - $tweet_cache_expire = 300; - break; - } - - set_transient( 'widget-twitter-' . $this->number, $tweets, $tweet_cache_expire ); - set_transient( 'widget-twitter-error-' . $this->number, $the_error, $tweet_cache_expire ); - } - - return array( 'data' => $tweets, 'error' => $the_error ); - } - - function update( $new_instance, $old_instance ) { - $instance = array(); - - $instance['account'] = trim( wp_kses( $new_instance['account'], array() ) ); - $instance['account'] = str_replace( array( 'http://twitter.com/', '/', '@', '#!', ), array( '', '', '', '', ), $instance['account'] ); - - $instance['title'] = wp_kses( $new_instance['title'], array() ); - $instance['show'] = absint( $new_instance['show'] ); - $instance['hidereplies'] = isset( $new_instance['hidereplies'] ); - $instance['hidepublicized'] = isset( $new_instance['hidepublicized'] ); - $instance['includeretweets'] = isset( $new_instance['includeretweets'] ); - - if ( $instance['followbutton'] != $new_instance['followbutton'] ) { - if ( $new_instance['followbutton'] ) - do_action( 'jetpack_bump_stats_extras', 'twitter_widget', 'follow_button_enabled' ); - else - do_action( 'jetpack_bump_stats_extras', 'twitter_widget', 'follow_button_disabled' ); - } - - $instance['followbutton'] = ! isset( $new_instance['followbutton'] ) ? 0 : 1; - $instance['beforetimesince'] = $new_instance['beforetimesince']; - - delete_transient( 'widget-twitter-' . $this->number ); - delete_transient( 'widget-twitter-error-' . $this->number ); - - return $instance; - } - - function form( $instance ) { - //Defaults - $account = isset( $instance['account'] ) ? wp_kses( $instance['account'], array() ) : ''; - $title = isset( $instance['title'] ) ? $instance['title'] : ''; - $show = isset( $instance['show'] ) ? absint( $instance['show'] ) : 5; - $show = ( $show < 1 || 20 < $show ) ? 5 : $show; - $hidereplies = isset( $instance['hidereplies'] ) && ! empty( $instance['hidereplies'] ) ? (bool) $instance['hidereplies'] : false; - $hidepublicized = isset( $instance['hidepublicized'] ) && ! empty( $instance['hidepublicized'] ) ? (bool) $instance['hidepublicized'] : false; - $include_retweets = isset( $instance['includeretweets'] ) && ! empty( $instance['includeretweets'] ) ? (bool) $instance['includeretweets'] : false; - $follow_button = isset( $instance['followbutton'] ) && ! empty( $instance['followbutton'] ) ? 1 : 0; - $before_timesince = isset( $instance['beforetimesince'] ) && ! empty( $instance['beforetimesince'] ) ? esc_attr( $instance['beforetimesince'] ) : ''; - ?> - - <p> - <label for="<?php echo $this->get_field_id( 'title' ); ?>"> - <?php esc_html_e( 'Title:', 'jetpack' )?> - <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'account' ); ?>"> - <?php esc_html_e( 'Twitter username:', 'jetpack' ); ?> <a href="http://support.wordpress.com/widgets/twitter-widget/#twitter-username" target="_blank">( ? )</a> - <input class="widefat" id="<?php echo $this->get_field_id( 'account' ); ?>" name="<?php echo $this->get_field_name( 'account' ); ?>" type="text" value="<?php echo esc_attr( $account ); ?>" /> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'show' ); ?>"> - <?php esc_html_e( 'Maximum number of Tweets to show:', 'jetpack' ); ?> - <select id="<?php echo $this->get_field_id( 'show' ); ?>" name="<?php echo $this->get_field_name( 'show' ); ?>"> - <?php - for ( $i = 1; $i <= 20; ++$i ) : - ?><option value="<?php echo esc_attr( $i ); ?>" <?php selected( $show, $i ); ?>><?php echo esc_attr( $i ); ?></option><?php - endfor; - ?> - </select> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'hidereplies' ); ?>"> - <input id="<?php echo $this->get_field_id( 'hidereplies' );?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'hidereplies' ); ?>" <?php checked( $hidereplies, true ); ?> /> - <?php esc_html_e( 'Hide replies', 'jetpack' ); ?> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'hidepublicized' ); ?>"> - <input id="<?php echo $this->get_field_id( 'hidepublicized' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'hidepublicized' ); ?>" <?php checked( $hidepublicized, true ); ?> /> - <?php esc_html_e( 'Hide Tweets pushed by Publicize', 'jetpack' ); ?> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'includeretweets' ); ?>"> - <input id="<?php echo $this->get_field_id( 'includeretweets' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'includeretweets' ); ?>" <?php checked( $include_retweets, true ); ?> /> - <?php esc_html_e( 'Include retweets', 'jetpack' ); ?> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'followbutton' ); ?>"> - <input id="<?php echo $this->get_field_id( 'followbutton' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'followbutton' ); ?>" <?php checked( $follow_button, 1 ); ?> /> - <?php esc_html_e( 'Display Follow Button', 'jetpack' ); ?> - </label> - </p> - - <p> - <label for="<?php echo $this->get_field_id( 'beforetimesince' ); ?>"> - <?php esc_html_e( 'Text to display between Tweet and timestamp:', 'jetpack' ); ?> - <input class="widefat" id="<?php echo $this->get_field_id( 'beforetimesince' ); ?>" name="<?php echo $this->get_field_name( 'beforetimesince' ); ?>" type="text" value="<?php echo esc_attr( $before_timesince ); ?>" /> - </label> - </p> - - <?php - } - - function 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; - } - - /** - * 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 _jetpack_widget_twitter_username( array $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 _jetpack_widget_twitter_hashtag( array $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>"; - } - - function twitter_widget_script() { - if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) { - if ( is_ssl() ) - $twitter_widget_js = 'https://platform.twitter.com/widgets.js'; - else - $twitter_widget_js = 'http://platform.twitter.com/widgets.js'; - wp_register_script( 'twitter-widgets', $twitter_widget_js, array(), '20111117', true ); - wp_print_scripts( 'twitter-widgets' ); - } - } -} |