Completed
Push — v2/videopress-sideloading ( 341d44...ce2243 )
by
unknown
09:54
created

Jetpack_Top_Posts_Widget::get_by_views()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 45
rs 5.3846
cc 8
eloc 21
nc 14
nop 1
1
<?php
2
3
/*
4
 * Currently, this widget depends on the Stats Module. To not load this file
5
 * when the Stats Module is not active would potentially bypass Jetpack's
6
 * fatal error detection on module activation, so we always load this file.
7
 * Instead, we don't register the widget if the Stats Module isn't active.
8
 */
9
10
/**
11
 * Register the widget for use in Appearance -> Widgets
12
 */
13
add_action( 'widgets_init', 'jetpack_top_posts_widget_init' );
14
15
function jetpack_top_posts_widget_init() {
16
	// Currently, this widget depends on the Stats Module
17
	if (
18
		( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
19
	&&
20
		! function_exists( 'stats_get_csv' )
21
	) {
22
		return;
23
	}
24
25
	register_widget( 'Jetpack_Top_Posts_Widget' );
26
}
27
28
class Jetpack_Top_Posts_Widget extends WP_Widget {
29
	public $alt_option_name = 'widget_stats_topposts';
30
	public $default_title = '';
31
32 View Code Duplication
	function __construct() {
33
		parent::__construct(
34
			'top-posts',
35
			/** This filter is documented in modules/widgets/facebook-likebox.php */
36
			apply_filters( 'jetpack_widget_name', __( 'Top Posts &amp; Pages', 'jetpack' ) ),
37
			array(
38
				'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ),
39
			)
40
		);
41
42
		$this->default_title =  __( 'Top Posts &amp; Pages', 'jetpack' );
43
44
		if ( is_active_widget( false, false, $this->id_base ) ) {
45
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
46
		}
47
	}
48
49
	function enqueue_style() {
50
		wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' );
51
		wp_enqueue_style( 'jetpack-top-posts-widget' );
52
	}
53
54
	function form( $instance ) {
55
		$instance = wp_parse_args( (array) $instance, $this->defaults() );
56
57
		$title = stripslashes( $instance['title'] );
58
59
		$count = isset( $instance['count'] ) ? (int) $instance['count'] : 10;
60
		if ( $count < 1 || 10 < $count ) {
61
			$count = 10;
62
		}
63
64
		$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
65
		$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
66
67
		// 'likes' are not available in Jetpack
68
		$ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering'] ? 'likes' : 'views';
69
70 View Code Duplication
		if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text'  ) ) ) {
71
			$display = $instance['display'];
72
		} else {
73
			$display = 'text';
74
		}
75
76
		?>
77
78
		<p>
79
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
80
			<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 ); ?>" />
81
		</p>
82
83
		<p>
84
			<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'jetpack' ); ?></label>
85
			<input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" />
86
		</p>
87
88
		<?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?>
89
		<p>
90
			<label><?php esc_html_e( 'Order Top Posts &amp; Pages By:', 'jetpack' ); ?></label>
91
			<ul>
92
				<li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-likes" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="likes" <?php checked( 'likes', $ordering ); ?> /> <?php esc_html_e( 'Likes', 'jetpack' ); ?></label></li>
93
				<li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-views" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="views" <?php checked( 'views', $ordering ); ?> /> <?php esc_html_e( 'Views', 'jetpack' ); ?></label></li>
94
			</ul>
95
		</p>
96
		<?php endif; ?>
97
98
		<p>
99
			<label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label>
100
			<ul>
101
				<?php foreach( $allowed_post_types as $type ) {
102
					// Get the Post Type name to display next to the checkbox
103
					$post_type_object = get_post_type_object( $type );
104
					$label = $post_type_object->labels->name;
105
106
					$checked = '';
107
					if ( in_array( $type, $types ) ) {
108
						$checked = 'checked="checked" ';
109
					} ?>
110
111
					<li><label>
112
						<input value="<?php echo esc_attr( $type ); ?>" name="<?php echo $this->get_field_name( 'types' ); ?>[]" id="<?php echo $this->get_field_id( 'types' ); ?>-<?php echo $type; ?>" type="checkbox" <?php echo $checked; ?>>
113
						<?php echo esc_html( $label ); ?>
114
					</label></li>
115
116
				<?php } // End foreach ?>
117
			</ul>
118
		</p>
119
120
		<p>
121
			<label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label>
122
			<ul>
123
				<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-text" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="text" <?php checked( 'text', $display ); ?> /> <?php esc_html_e( 'Text List', 'jetpack' ); ?></label></li>
124
				<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-list" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="list" <?php checked( 'list', $display ); ?> /> <?php esc_html_e( 'Image List', 'jetpack' ); ?></label></li>
125
				<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-grid" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="grid" <?php checked( 'grid', $display ); ?> /> <?php esc_html_e( 'Image Grid', 'jetpack' ); ?></label></li>
126
			</ul>
127
		</p>
128
129
		<p><?php esc_html_e( 'Top Posts &amp; Pages by views are calculated from 24-48 hours of stats. They take a while to change.', 'jetpack' ); ?></p>
130
131
		<?php
132
	}
133
134
	function update( $new_instance, $old_instance ) {
135
		$instance = array();
136
		$instance['title'] = wp_kses( $new_instance['title'], array() );
137
		if ( $instance['title'] === $this->default_title ) {
138
			$instance['title'] = false; // Store as false in case of language change
139
		}
140
141
		$instance['count'] = (int) $new_instance['count'];
142
		if ( $instance['count'] < 1 || 10 < $instance['count'] ) {
143
			$instance['count'] = 10;
144
		}
145
146
		// 'likes' are not available in Jetpack
147
		$instance['ordering'] = isset( $new_instance['ordering'] ) && 'likes' == $new_instance['ordering'] ? 'likes' : 'views';
148
149
		$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
150
		$instance['types'] = $new_instance['types'];
151
		foreach( $new_instance['types'] as $key => $type ) {
152
			if ( ! in_array( $type, $allowed_post_types ) ) {
153
				unset( $new_instance['types'][ $key ] );
154
			}
155
		}
156
157 View Code Duplication
		if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text'  ) ) ) {
158
			$instance['display'] = $new_instance['display'];
159
		} else {
160
			$instance['display'] = 'text';
161
		}
162
163
		return $instance;
164
	}
165
166
	function widget( $args, $instance ) {
167
		$instance = wp_parse_args( (array) $instance, $this->defaults() );
168
169
		$title = isset( $instance['title' ] ) ? $instance['title'] : false;
170
		if ( false === $title ) {
171
			$title = $this->default_title;
172
		}
173
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
174
		$title = apply_filters( 'widget_title', $title );
175
176
		$count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
177
		if ( $count < 1 || 10 < $count ) {
178
			$count = 10;
179
		}
180
		/**
181
		 * Control the number of displayed posts.
182
		 *
183
		 * @module widgets
184
		 *
185
		 * @since 3.3.0
186
		 *
187
		 * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10.
188
		 */
189
		$count = apply_filters( 'jetpack_top_posts_widget_count', $count );
190
191
		$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
192
193
		// 'likes' are not available in Jetpack
194
		$ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views';
195
196 View Code Duplication
		if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text'  ) ) ) {
197
			$display = $instance['display'];
198
		} else {
199
			$display = 'text';
200
		}
201
202
		if ( 'text' != $display ) {
203
			$get_image_options = array(
204
				'fallback_to_avatars' => true,
205
				/** This filter is documented in modules/shortcodes/audio.php */
206
				'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'http://en.wordpress.com/i/logo/white-gray-80.png' ) ),
207
			);
208
			if ( 'grid' == $display ) {
209
				$get_image_options['avatar_size'] = 200;
210
			} else {
211
				$get_image_options['avatar_size'] = 40;
212
			}
213
			/**
214
			 * Top Posts Widget Image options.
215
			 *
216
			 * @module widgets
217
			 *
218
			 * @since 1.8.0
219
			 *
220
			 * @param array $get_image_options {
221
			 * Array of Image options.
222
			 * @type bool true Should we default to Gravatars when no image is found? Default is true.
223
			 * @type string $gravatar_default Default Image URL if no Gravatar is found.
224
			 * @type int $avatar_size Default Image size.
225
			 * }
226
			 */
227
			$get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options );
228
		}
229
230
		if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) {
231
			$posts = $this->get_by_likes( $count );
232
		} else {
233
			$posts = $this->get_by_views( $count );
234
		}
235
236
		// Filter the returned posts. Remove all posts that do not match the chosen Post Types.
237
		if ( isset( $types ) ) {
238
			foreach ( $posts as $k => $post ) {
239
				if ( ! in_array( $post['post_type'], $types ) ) {
240
					unset( $posts[$k] );
241
				}
242
			}
243
		}
244
245
		if ( ! $posts ) {
246
			$posts = $this->get_fallback_posts();
247
		}
248
249
		echo $args['before_widget'];
250
		if ( ! empty( $title ) )
251
			echo $args['before_title'] . $title . $args['after_title'];
252
253
		if ( ! $posts ) {
254
			if ( current_user_can( 'edit_theme_options' ) ) {
255
				echo '<p>' . sprintf(
256
					__( 'There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack' ),
257
					'http://en.support.wordpress.com/getting-more-site-traffic/'
258
				) . '</p>';
259
			}
260
261
			echo $args['after_widget'];
262
			return;
263
		}
264
265
		switch ( $display ) {
266
		case 'list' :
267
		case 'grid' :
268
			wp_enqueue_style( 'widget-grid-and-list' );
269
			foreach ( $posts as &$post ) {
270
				$image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) );
271
				$post['image'] = $image['src'];
272
				if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) {
273
					$size = (int) $get_image_options['avatar_size'];
0 ignored issues
show
Bug introduced by
The variable $get_image_options does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
274
					$post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size,$size" ) );
275
				}
276
			}
277
278
			unset( $post );
279
280
			if ( 'grid' == $display ) {
281
				echo "<div class='widgets-grid-layout no-grav'>\n";
282
				foreach ( $posts as $post ) :
283
				?>
284
					<div class="widget-grid-view-image">
285
						<?php
286
						/**
287
						 * Fires before each Top Post result, inside <li>.
288
						 *
289
						 * @module widgets
290
						 *
291
						 * @since 3.2.0
292
						 *
293
						 * @param string $post['post_id'] Post ID.
294
						 */
295
						do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
296
						?>
297
						<a href="<?php echo esc_url( $post['permalink'] ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp">
298
							<?php $size = (int) $get_image_options['avatar_size']; ?>
299
							<img width="<?php echo absint( $size ); ?>" height="<?php echo absint( $size ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" />
300
						</a>
301
						<?php
302
						/**
303
						 * Fires after each Top Post result, inside <li>.
304
						 *
305
						 * @module widgets
306
						 *
307
						 * @since 3.2.0
308
						 *
309
						 * @param string $post['post_id'] Post ID.
310
						 */
311
						do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
312
						?>
313
					</div>
314
				<?php
315
				endforeach;
316
				echo "</div>\n";
317
			} else {
318
				echo "<ul class='widgets-list-layout no-grav'>\n";
319
				foreach ( $posts as $post ) :
320
				?>
321
					<li>
322
						<?php
323
						/** This action is documented in modules/widgets/top-posts.php */
324
						do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
325
						?>
326
						<a href="<?php echo esc_url( $post['permalink'] ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp">
327
							<?php $size = (int) $get_image_options['avatar_size']; ?>
328
							<img width="<?php echo absint( $size ); ?>" height="<?php echo absint( $size ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" class='widgets-list-layout-blavatar' alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" />
329
						</a>
330
						<div class="widgets-list-layout-links">
331
							<a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp">
332
								<?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
333
							</a>
334
						</div>
335
						<?php
336
						/** This action is documented in modules/widgets/top-posts.php */
337
						do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
338
						?>
339
					</li>
340
				<?php
341
				endforeach;
342
				echo "</ul>\n";
343
			}
344
			break;
345
		default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
346
			echo '<ul>';
347
			foreach ( $posts as $post ) :
348
			?>
349
				<li>
350
					<?php
351
					/** This action is documented in modules/widgets/top-posts.php */
352
					do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
353
					?>
354
					<a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp">
355
						<?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
356
					</a>
357
					<?php
358
					/** This action is documented in modules/widgets/top-posts.php */
359
					do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
360
					?>
361
				</li>
362
			<?php
363
			endforeach;
364
			echo '</ul>';
365
		}
366
367
		echo $args['after_widget'];
368
	}
369
370
	public static function defaults() {
371
		return array(
372
			'title'    => esc_html__( 'Top Posts &amp; Pages', 'jetpack' ),
373
			'count'    => absint( 10 ),
374
			'types'    => array( 'post', 'page' ),
375
			'ordering' => 'views',
376
			'display'  => 'text',
377
		);
378
	}
379
380
	/*
381
	 * Get most liked posts
382
	 *
383
	 * ONLY TO BE USED IN WPCOM
384
	 */
385
	function get_by_likes( $count ) {
386
		$post_likes = wpl_get_blogs_most_liked_posts();
387
		if ( !$post_likes ) {
388
			return array();
389
		}
390
391
		return $this->get_posts( array_keys( $post_likes ), $count );
392
	}
393
394
	function get_by_views( $count ) {
395
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
396
			global $wpdb;
397
398
			$post_views = wp_cache_get( "get_top_posts_$count", 'stats' );
399
			if ( false === $post_views ) {
400
				$post_views = array_shift( stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ) );
0 ignored issues
show
Bug introduced by
stats_get_daily_history(... $count * 2 + 10, true) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
401
				unset( $post_views[0] );
402
				wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200);
403
			}
404
405
			return $this->get_posts( array_keys( $post_views ), $count );
406
		}
407
408
		/**
409
		 * Filter the number of days used to calculate Top Posts for the Top Posts widget.
410
		 *
411
		 * @module widgets
412
		 *
413
		 * @since 2.8.0
414
		 *
415
		 * @param int 2 Number of days. Default is 2.
416
		 */
417
		$days = (int) apply_filters( 'jetpack_top_posts_days', 2 );
418
419
		if ( $days < 1 ) {
420
			$days = 2;
421
		}
422
423
		if ( $days > 10 ) {
424
			$days = 10;
425
		}
426
427
		$post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $days ), 'limit' => 11 ) );
428
		if ( ! $post_view_posts ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post_view_posts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
429
			return array();
430
		}
431
432
		$post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) );
433
		if ( ! $post_view_ids ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post_view_ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
434
			return array();
435
		}
436
437
		return $this->get_posts( $post_view_ids, $count );
438
	}
439
440
	function get_fallback_posts() {
441
		if ( current_user_can( 'edit_theme_options' ) ) {
442
			return array();
443
		}
444
445
		$post_query = new WP_Query;
446
447
		$posts = $post_query->query( array(
448
			'posts_per_page' => 1,
449
			'post_status' => 'publish',
450
			'post_type' => array( 'post', 'page' ),
451
			'no_found_rows' => true,
452
		) );
453
454
		if ( ! $posts ) {
455
			return array();
456
		}
457
458
		$post = array_pop( $posts );
459
460
		return $this->get_posts( $post->ID, 1 );
461
	}
462
463
	function get_posts( $post_ids, $count ) {
464
		$counter = 0;
465
466
		$posts = array();
467
		foreach ( (array) $post_ids as $post_id ) {
468
			$post = get_post( $post_id );
469
470
			if ( ! $post )
471
				continue;
472
473
			// hide private and password protected posts
474
			if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $post->ID ) )
475
				continue;
476
477
			// Both get HTML stripped etc on display
478
			if ( empty( $post->post_title ) ) {
479
				$title_source = $post->post_content;
480
				$title = wp_html_excerpt( $title_source, 50 );
481
				$title .= '&hellip;';
482
			} else {
483
				$title = $post->post_title;
484
			}
485
486
			$permalink = get_permalink( $post->ID );
487
488
			$post_type = $post->post_type;
489
490
			$posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
491
			$counter++;
492
493
			if ( $counter == $count ) {
494
				break; // only need to load and show x number of likes
495
			}
496
		}
497
498
		/**
499
		 * Filter the Top Posts and Pages.
500
		 *
501
		 * @module widgets
502
		 *
503
		 * @since 3.0.0
504
		 *
505
		 * @param array $posts Array of the most popular posts.
506
		 * @param array $post_ids Array of Post IDs.
507
		 * @param string $count Number of Top Posts we want to display.
508
		 */
509
		return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );
510
	}
511
}
512
513
/**
514
 * Create a shortcode to display the widget anywhere.
515
 *
516
 * @since 3.9.2
517
 */
518
function jetpack_do_top_posts_widget( $instance ) {
519
	// Post Types can't be entered as an array in the shortcode parameters.
520
	if ( isset( $instance['types'] ) && is_array( $instance['types'] ) ) {
521
		$instance['types'] = implode( ',', $instance['types'] );
522
	}
523
524
	$instance = shortcode_atts(
525
		Jetpack_Top_Posts_Widget::defaults(),
526
		$instance,
527
		'jetpack_top_posts_widget'
528
	);
529
530
	// Add a class to allow styling
531
	$args = array(
532
		'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ),
533
	);
534
535
	ob_start();
536
	the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args );
537
	$output = ob_get_clean();
538
539
	return $output;
540
}
541
add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' );
542