summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/widgets/top-posts.php')
-rw-r--r--plugins/jetpack/modules/widgets/top-posts.php128
1 files changed, 68 insertions, 60 deletions
diff --git a/plugins/jetpack/modules/widgets/top-posts.php b/plugins/jetpack/modules/widgets/top-posts.php
index 4f3fe912..5dd7b266 100644
--- a/plugins/jetpack/modules/widgets/top-posts.php
+++ b/plugins/jetpack/modules/widgets/top-posts.php
@@ -281,6 +281,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
if ( false === $title ) {
$title = $this->default_title;
}
+
/** This filter is documented in core/src/wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title );
@@ -315,6 +316,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
$display = 'text';
}
+ $get_image_options = array();
if ( 'text' !== $display ) {
$get_image_options = array(
'fallback_to_avatars' => true,
@@ -352,42 +354,28 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
$posts = $this->get_by_views( $count, $args, $types );
}
- if ( ! $posts ) {
- $posts = $this->get_fallback_posts( $count, $types );
- }
-
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
+ /*
+ * If we have no posts, add some fallback posts
+ * and display a fallback message for admins.
+ */
if ( ! $posts ) {
- $link = esc_url( Redirect::get_url( 'jetpack-support-getting-more-views-and-traffic' ) );
- if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
- $link = 'https://en.support.wordpress.com/getting-more-site-traffic/';
- }
-
if ( current_user_can( 'edit_theme_options' ) ) {
- echo '<p>' . sprintf(
- wp_kses(
- /* Translators: Placeholder: link to the Jetpack support article. */
- __( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ),
- array(
- 'a' => array(
- 'href' => array(),
- 'target' => array(),
- ),
- )
- ),
- esc_url( $link )
- ) . '</p>';
+ echo $this->fallback_message(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
- echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
-
- return;
+ $posts = $this->get_fallback_posts( $count, $types );
}
+ /*
+ * Display our posts.
+ */
+
/**
* Filter the layout of the Top Posts Widget
*
@@ -395,15 +383,13 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
*
* @since 6.4.0
*
- * @param string $layout layout of the Top Posts Widget (empty string)
- * @param array $posts IDs of the posts to be displayed
- * @param array $display Display option from widget form
+ * @param string $layout layout of the Top Posts Widget (empty string).
+ * @param array $posts IDs of the posts to be displayed.
+ * @param array $display Display option from widget form.
*/
$layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display );
if ( ! empty( $layout ) ) {
echo $layout; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
- echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
- return;
}
switch ( $display ) {
@@ -438,15 +424,13 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
$post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) );
}
}
-
unset( $post );
if ( 'grid' === $display ) {
echo "<div class='widgets-grid-layout no-grav'>\n";
- foreach ( $posts as $post ) :
- ?>
- <div class="widget-grid-view-image">
- <?php
+ foreach ( $posts as $post ) {
+ echo '<div class="widget-grid-view-image">';
+
/**
* Fires before each Top Post result, inside <li>.
*
@@ -490,17 +474,15 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
* @param string $post['post_id'] Post ID.
*/
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
- ?>
- </div>
- <?php
- endforeach;
+
+ echo '</div>';
+ }
echo "</div>\n";
} else {
echo "<ul class='widgets-list-layout no-grav'>\n";
- foreach ( $posts as $post ) :
- ?>
- <li>
- <?php
+ foreach ( $posts as $post ) {
+ echo '<li>';
+
/** This action is documented in modules/widgets/top-posts.php */
do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
@@ -526,19 +508,18 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
/** This action is documented in modules/widgets/top-posts.php */
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
- ?>
- </li>
- <?php
- endforeach;
+
+ echo '</li>';
+ }
echo "</ul>\n";
}
break;
default:
echo '<ul>';
- foreach ( $posts as $post ) :
- ?>
- <li>
- <?php
+
+ foreach ( $posts as $post ) {
+ echo '<li>';
+
/** This action is documented in modules/widgets/top-posts.php */
do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
@@ -554,17 +535,48 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
/** This action is documented in modules/widgets/top-posts.php */
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
- ?>
- </li>
- <?php
- endforeach;
+
+ echo '</li>';
+ }
+
echo '</ul>';
+ break;
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
+ * Display a message with recommendations when there are no recorded top posts.
+ *
+ * @return string $fallback_message
+ */
+ private static function fallback_message() {
+ $link = esc_url( Redirect::get_url( 'jetpack-support-getting-more-views-and-traffic' ) );
+ if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
+ $link = 'https://en.support.wordpress.com/getting-more-site-traffic/';
+ }
+
+ $fallback_message = '<p>';
+ $fallback_message .= sprintf(
+ wp_kses(
+ /* Translators: Placeholder: link to the Jetpack support article. */
+ __( 'There are no popular posts to display. Instead, your visitors will see a list of your recent posts below. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ),
+ array(
+ 'a' => array(
+ 'href' => array(),
+ 'target' => array(),
+ ),
+ )
+ ),
+ esc_url( $link )
+ );
+ $fallback_message .= '<p>';
+
+ return $fallback_message;
+ }
+
+ /**
* Widget default option values.
*/
public static function defaults() {
@@ -678,10 +690,6 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
* @return array
*/
public function get_fallback_posts( $count = 10, $types = array( 'post', 'page' ) ) {
- if ( current_user_can( 'edit_theme_options' ) ) {
- return array();
- }
-
$post_query = new WP_Query();
if ( ! is_array( $types ) || empty( $types ) ) {
@@ -765,7 +773,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget {
$posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
$counter++;
- if ( $counter == $count ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
+ if ( $counter == $count ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
break; // only need to load and show x number of likes.
}
}