Completed
Push — add/sync-rest-comments ( 1da85c )
by
unknown
09:46
created

Jetpack_Top_Posts_Widget::get_by_likes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
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
	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 View Code Duplication
		if ( is_active_widget( false, false, $this->id_base ) ) {
45
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
46
		}
47
48
		/**
49
		 * Add explanation about how the statistics are calculated.
50
		 *
51
		 * @module widgets
52
		 *
53
		 * @since 3.9.3
54
		 */
55
		add_action( 'jetpack_widget_top_posts_after_fields', array( $this, 'stats_explanation' ) );
56
	}
57
58
	function enqueue_style() {
59
		wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' );
60
		wp_enqueue_style( 'jetpack-top-posts-widget' );
61
	}
62
63
	function form( $instance ) {
64
		$instance = wp_parse_args( (array) $instance, $this->defaults() );
65
66
		$title = stripslashes( $instance['title'] );
67
68
		$count = isset( $instance['count'] ) ? (int) $instance['count'] : 10;
69
		if ( $count < 1 || 10 < $count ) {
70
			$count = 10;
71
		}
72
73
		$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
74
		$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
75
76
		// 'likes' are not available in Jetpack
77
		$ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering'] ? 'likes' : 'views';
78
79 View Code Duplication
		if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text'  ) ) ) {
80
			$display = $instance['display'];
81
		} else {
82
			$display = 'text';
83
		}
84
85
		?>
86
87
		<p>
88
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
89
			<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 ); ?>" />
90
		</p>
91
92
		<p>
93
			<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>
94
			<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" />
95
		</p>
96
97
		<?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?>
98
		<p>
99
			<label><?php esc_html_e( 'Order Top Posts &amp; Pages By:', 'jetpack' ); ?></label>
100
			<ul>
101
				<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>
102
				<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>
103
			</ul>
104
		</p>
105
		<?php endif; ?>
106
107
		<p>
108
			<label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label>
109
			<ul>
110
				<?php foreach( $allowed_post_types as $type ) {
111
					// Get the Post Type name to display next to the checkbox
112
					$post_type_object = get_post_type_object( $type );
113
					$label = $post_type_object->labels->name;
114
115
					$checked = '';
116
					if ( in_array( $type, $types ) ) {
117
						$checked = 'checked="checked" ';
118
					} ?>
119
120
					<li><label>
121
						<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; ?>>
122
						<?php echo esc_html( $label ); ?>
123
					</label></li>
124
125
				<?php } // End foreach ?>
126
			</ul>
127
		</p>
128
129
		<p>
130
			<label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label>
131
			<ul>
132
				<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>
133
				<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>
134
				<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>
135
			</ul>
136
		</p><?php
137
138
		/**
139
		 * Fires after the fields are displayed in the Top Posts Widget settings in wp-admin.
140
		 *
141
		 * Allow adding extra content after the fields are displayed.
142
		 *
143
		 * @module widgets
144
		 *
145
		 * @since 3.9.3
146
		 *
147
		 * @param array $args {
148
		 *     @param array $instance The widget instance.
149
		 *     @param object $this The class object.
150
		 * }
151
		 */
152
		do_action( 'jetpack_widget_top_posts_after_fields', array( $instance, $this ) );
153
	}
154
155
	/**
156
	 * Explains how the statics are calculated.
157
	 */
158
	function stats_explanation() {
159
		?>
160
161
		<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><?php
162
	}
163
164
	function update( $new_instance, $old_instance ) {
165
		$instance = array();
166
		$instance['title'] = wp_kses( $new_instance['title'], array() );
167
		if ( $instance['title'] === $this->default_title ) {
168
			$instance['title'] = false; // Store as false in case of language change
169
		}
170
171
		$instance['count'] = (int) $new_instance['count'];
172
		if ( $instance['count'] < 1 || 10 < $instance['count'] ) {
173
			$instance['count'] = 10;
174
		}
175
176
		// 'likes' are not available in Jetpack
177
		$instance['ordering'] = isset( $new_instance['ordering'] ) && 'likes' == $new_instance['ordering'] ? 'likes' : 'views';
178
179
		$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
180
		$instance['types'] = $new_instance['types'];
181
		foreach( $new_instance['types'] as $key => $type ) {
182
			if ( ! in_array( $type, $allowed_post_types ) ) {
183
				unset( $new_instance['types'][ $key ] );
184
			}
185
		}
186
187 View Code Duplication
		if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text'  ) ) ) {
188
			$instance['display'] = $new_instance['display'];
189
		} else {
190
			$instance['display'] = 'text';
191
		}
192
193
		/**
194
		 * Filters Top Posts Widget settings before they're saved.
195
		 *
196
		 * @module widgets
197
		 *
198
		 * @since 3.9.3
199
		 *
200
		 * @param array $instance The santized widget instance. Only contains data processed by the current widget.
201
		 * @param array $new_instance The new widget instance before sanitization.
202
		 */
203
		$instance = apply_filters( 'jetpack_top_posts_saving', $instance, $new_instance );
204
205
		return $instance;
206
	}
207
208
	function widget( $args, $instance ) {
209
		$instance = wp_parse_args( (array) $instance, $this->defaults() );
210
211
		$title = isset( $instance['title' ] ) ? $instance['title'] : false;
212
		if ( false === $title ) {
213
			$title = $this->default_title;
214
		}
215
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
216
		$title = apply_filters( 'widget_title', $title );
217
218
		$count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
219
		if ( $count < 1 || 10 < $count ) {
220
			$count = 10;
221
		}
222
		/**
223
		 * Control the number of displayed posts.
224
		 *
225
		 * @module widgets
226
		 *
227
		 * @since 3.3.0
228
		 *
229
		 * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10.
230
		 */
231
		$count = apply_filters( 'jetpack_top_posts_widget_count', $count );
232
233
		$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
234
235
		// 'likes' are not available in Jetpack
236
		$ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views';
237
238 View Code Duplication
		if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text'  ) ) ) {
239
			$display = $instance['display'];
240
		} else {
241
			$display = 'text';
242
		}
243
244
		if ( 'text' != $display ) {
245
			$get_image_options = array(
246
				'fallback_to_avatars' => true,
247
				/** This filter is documented in modules/shortcodes/audio.php */
248
				'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'http://en.wordpress.com/i/logo/white-gray-80.png' ) ),
249
			);
250
			if ( 'grid' == $display ) {
251
				$get_image_options['avatar_size'] = 200;
252
			} else {
253
				$get_image_options['avatar_size'] = 40;
254
			}
255
			/**
256
			 * Top Posts Widget Image options.
257
			 *
258
			 * @module widgets
259
			 *
260
			 * @since 1.8.0
261
			 *
262
			 * @param array $get_image_options {
263
			 * Array of Image options.
264
			 * @type bool true Should we default to Gravatars when no image is found? Default is true.
265
			 * @type string $gravatar_default Default Image URL if no Gravatar is found.
266
			 * @type int $avatar_size Default Image size.
267
			 * }
268
			 */
269
			$get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options );
270
		}
271
272
		if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) {
273
			$posts = $this->get_by_likes( $count );
274
		} else {
275
			$posts = $this->get_by_views( $count, $args );
276
		}
277
278
		// Filter the returned posts. Remove all posts that do not match the chosen Post Types.
279
		if ( isset( $types ) ) {
280
			foreach ( $posts as $k => $post ) {
281
				if ( ! in_array( $post['post_type'], $types ) ) {
282
					unset( $posts[$k] );
283
				}
284
			}
285
		}
286
287
		if ( ! $posts ) {
288
			$posts = $this->get_fallback_posts();
289
		}
290
291
		echo $args['before_widget'];
292
		if ( ! empty( $title ) )
293
			echo $args['before_title'] . $title . $args['after_title'];
294
295
		if ( ! $posts ) {
296
			if ( current_user_can( 'edit_theme_options' ) ) {
297
				echo '<p>' . sprintf(
298
					__( 'There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack' ),
299
					'http://en.support.wordpress.com/getting-more-site-traffic/'
300
				) . '</p>';
301
			}
302
303
			echo $args['after_widget'];
304
			return;
305
		}
306
307
		switch ( $display ) {
308
		case 'list' :
309
		case 'grid' :
310
			wp_enqueue_style( 'widget-grid-and-list' );
311
			foreach ( $posts as &$post ) {
312
				$image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) );
313
				$post['image'] = $image['src'];
314
				if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) {
315
					$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...
316
					$post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size,$size" ) );
317
				}
318
			}
319
320
			unset( $post );
321
322
			if ( 'grid' == $display ) {
323
				echo "<div class='widgets-grid-layout no-grav'>\n";
324
				foreach ( $posts as $post ) :
325
				?>
326
					<div class="widget-grid-view-image">
327
						<?php
328
						/**
329
						 * Fires before each Top Post result, inside <li>.
330
						 *
331
						 * @module widgets
332
						 *
333
						 * @since 3.2.0
334
						 *
335
						 * @param string $post['post_id'] Post ID.
336
						 */
337
						do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
338
						?>
339
						<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">
340
							<?php $size = (int) $get_image_options['avatar_size']; ?>
341
							<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" />
342
						</a>
343
						<?php
344
						/**
345
						 * Fires after each Top Post result, inside <li>.
346
						 *
347
						 * @module widgets
348
						 *
349
						 * @since 3.2.0
350
						 *
351
						 * @param string $post['post_id'] Post ID.
352
						 */
353
						do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
354
						?>
355
					</div>
356
				<?php
357
				endforeach;
358
				echo "</div>\n";
359
			} else {
360
				echo "<ul class='widgets-list-layout no-grav'>\n";
361
				foreach ( $posts as $post ) :
362
				?>
363
					<li>
364
						<?php
365
						/** This action is documented in modules/widgets/top-posts.php */
366
						do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
367
						?>
368
						<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">
369
							<?php $size = (int) $get_image_options['avatar_size']; ?>
370
							<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" />
371
						</a>
372
						<div class="widgets-list-layout-links">
373
							<a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp">
374
								<?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
375
							</a>
376
						</div>
377
						<?php
378
						/** This action is documented in modules/widgets/top-posts.php */
379
						do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
380
						?>
381
					</li>
382
				<?php
383
				endforeach;
384
				echo "</ul>\n";
385
			}
386
			break;
387
		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...
388
			echo '<ul>';
389
			foreach ( $posts as $post ) :
390
			?>
391
				<li>
392
					<?php
393
					/** This action is documented in modules/widgets/top-posts.php */
394
					do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
395
					?>
396
					<a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp">
397
						<?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
398
					</a>
399
					<?php
400
					/** This action is documented in modules/widgets/top-posts.php */
401
					do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
402
					?>
403
				</li>
404
			<?php
405
			endforeach;
406
			echo '</ul>';
407
		}
408
409
		echo $args['after_widget'];
410
	}
411
412
	public static function defaults() {
413
		return array(
414
			'title'    => esc_html__( 'Top Posts &amp; Pages', 'jetpack' ),
415
			'count'    => absint( 10 ),
416
			'types'    => array( 'post', 'page' ),
417
			'ordering' => 'views',
418
			'display'  => 'text',
419
		);
420
	}
421
422
	/*
423
	 * Get most liked posts
424
	 *
425
	 * ONLY TO BE USED IN WPCOM
426
	 */
427
	function get_by_likes( $count ) {
428
		$post_likes = wpl_get_blogs_most_liked_posts();
429
		if ( !$post_likes ) {
430
			return array();
431
		}
432
433
		return $this->get_posts( array_keys( $post_likes ), $count );
434
	}
435
436
	function get_by_views( $count, $args ) {
437
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
438
			global $wpdb;
439
440
			$post_views = wp_cache_get( "get_top_posts_$count", 'stats' );
441
			if ( false === $post_views ) {
442
				$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...
443
				unset( $post_views[0] );
444
				wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200);
445
			}
446
447
			return $this->get_posts( array_keys( $post_views ), $count );
448
		}
449
450
		/**
451
		 * Filter the number of days used to calculate Top Posts for the Top Posts widget.
452
		 *
453
		 * @module widgets
454
		 *
455
		 * @since 3.9.3
456
		 *
457
		 * @param int 2 Number of days. Default is 2.
458
		 * @param array $args The widget arguments.
459
		 */
460
		$days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );
461
462
		if ( $days < 1 ) {
463
			$days = 2;
464
		}
465
466
		if ( $days > 10 ) {
467
			$days = 10;
468
		}
469
470
		$post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $days ), 'limit' => 11 ) );
471
		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...
472
			return array();
473
		}
474
475
		$post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) );
476
		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...
477
			return array();
478
		}
479
480
		return $this->get_posts( $post_view_ids, $count );
481
	}
482
483
	function get_fallback_posts() {
484
		if ( current_user_can( 'edit_theme_options' ) ) {
485
			return array();
486
		}
487
488
		$post_query = new WP_Query;
489
490
		$posts = $post_query->query( array(
491
			'posts_per_page' => 1,
492
			'post_status' => 'publish',
493
			'post_type' => array( 'post', 'page' ),
494
			'no_found_rows' => true,
495
		) );
496
497
		if ( ! $posts ) {
498
			return array();
499
		}
500
501
		$post = array_pop( $posts );
502
503
		return $this->get_posts( $post->ID, 1 );
504
	}
505
506
	function get_posts( $post_ids, $count ) {
507
		$counter = 0;
508
509
		$posts = array();
510
		foreach ( (array) $post_ids as $post_id ) {
511
			$post = get_post( $post_id );
512
513
			if ( ! $post )
514
				continue;
515
516
			// hide private and password protected posts
517
			if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $post->ID ) )
518
				continue;
519
520
			// Both get HTML stripped etc on display
521
			if ( empty( $post->post_title ) ) {
522
				$title_source = $post->post_content;
523
				$title = wp_html_excerpt( $title_source, 50 );
524
				$title .= '&hellip;';
525
			} else {
526
				$title = $post->post_title;
527
			}
528
529
			$permalink = get_permalink( $post->ID );
530
531
			$post_type = $post->post_type;
532
533
			$posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
534
			$counter++;
535
536
			if ( $counter == $count ) {
537
				break; // only need to load and show x number of likes
538
			}
539
		}
540
541
		/**
542
		 * Filter the Top Posts and Pages.
543
		 *
544
		 * @module widgets
545
		 *
546
		 * @since 3.0.0
547
		 *
548
		 * @param array $posts Array of the most popular posts.
549
		 * @param array $post_ids Array of Post IDs.
550
		 * @param string $count Number of Top Posts we want to display.
551
		 */
552
		return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );
553
	}
554
}
555
556
/**
557
 * Create a shortcode to display the widget anywhere.
558
 *
559
 * @since 3.9.2
560
 */
561
function jetpack_do_top_posts_widget( $instance ) {
562
	// Post Types can't be entered as an array in the shortcode parameters.
563
	if ( isset( $instance['types'] ) && is_array( $instance['types'] ) ) {
564
		$instance['types'] = implode( ',', $instance['types'] );
565
	}
566
567
	$instance = shortcode_atts(
568
		Jetpack_Top_Posts_Widget::defaults(),
569
		$instance,
570
		'jetpack_top_posts_widget'
571
	);
572
573
	// Add a class to allow styling
574
	$args = array(
575
		'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ),
576
	);
577
578
	ob_start();
579
	the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args );
580
	$output = ob_get_clean();
581
582
	return $output;
583
}
584
add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' );
585