Conversation
Consider all the posts retrieved from the wp.com API when filtering by post type
When no posts are found in wp.com API Fallback to local posts but respecting post type and count
|
Caution: This PR has changes that must be merged to WordPress.com |
|
Thank you for the great PR description! When this PR is ready for review, please apply the Scheduled Jetpack release: April 7, 2020. |
|
Hi @leogermani. Everything works correctly when I'm viewing the website as a guest though. |
modules/widgets/top-posts.php
Outdated
| * @param array $types The post types that should be returned. | ||
| * @return array | ||
| */ | ||
| public function get_posts( $post_ids, $count, $types ) { |
There was a problem hiding this comment.
Should we set $types = array( 'post', 'page' ) to make it an optional parameter?
There was a problem hiding this comment.
we could. I didn't because this variable is treated here:
jetpack/modules/widgets/top-posts.php
Line 244 in 218e6fb
But no harm doing that
There was a problem hiding this comment.
I'm reconsidering this.
This method is not called directly by the main widget method, it is passed through get_by_likes, get_by_views and get_fallback_posts. So this would require a chain of optional and useless arguments...
Both $count and $types are present in the widgets $instance, so a nice refactor would be to use it instead. Not sure if we should do it now... Also, these methods could/should be private (or live somewhere else in jetpack codebase)
I'm always afraid with backward compatibility when I consider refactoring things, but this is a widget and the code, I beleive, is self contained and shouldn't be called from outside...
What do you think? quick refactor?
There was a problem hiding this comment.
I'm okay covering that in a later PR. Could you either open an issue (if you don't have bandwidth) or open a new PR for it?
| function get_posts( $post_ids, $count ) { | ||
| /** | ||
| * Get posts from an array of IDs | ||
| * |
There was a problem hiding this comment.
Could we add a @since 8.4.0 Added $types?
That's expected with a brand new site. When you have an anonymous visitor, it falls back and make a simple query for posts (the second fix on this PR). Before this PR. it would display only one post, no matter how many you configured to be displayed in the widget, and also ignore the post type. |
|
leogermani, Your synced wpcom patch D40778-code has been updated. |
|
r204992-wpcom |
|
D41022-code // r204996-wpcom for the clean-up of the defaults. |
| * Get posts from an array of IDs | ||
| * | ||
| * @since 8.4.0 Added $count and $types parameters | ||
| * @since 8.4.0 Added $types parameters |
There was a problem hiding this comment.
ah yes, the parameter existed, but it was ignored
* Initial changelog entry * Changelog: add #14904 * Changelog: add #14910 * Changelog: add #14913 * Changelog: add #14916 * Changelog: add #14922 * Changelog: add #14924 * Changelog: add #14925 * Changelog: add #14928 * Changelog: add #14840 * Changelog: add #14841 * Changelog: add #14842 * Changelog: add #14826 * Changelog: add #14835 * Changelog: add #14859 * Changelog: add #14884 * Changelog: add #14888 * Changelog: add #14817 * Changelog: add #14814 * Changelog: add #14819 * Changelog;: add #14797 * Changelog: add #14798 * Changelog: add #14802 * Changelog: add #13676 * Changelog: add #13744 * Changelog: add #13777 * Changelog: add #14446 * Changelog: add #14739 * Changelog: add #14770 * Changelog: add #14784 * Changelog: add #14897 * Changelog: add #14898 * Changelog: add #14968 * Changelog: add #14985 * Changelog: add #15044 * Changelog: add #15052 * Update to remove Podcast since it remains in Beta * Changelog: add #14803 * Changelog: add #15028 * Changelog: add #15065 * Changelog:add #14886 * Changelog: add #15118 * Changelog: add #14990 * Changelog: add #14528 * Changelog: add #15120 * Changelog: add #15126 * Changelog: add #15049 * Chanegelog: add #14852 * Changelog: add #15090 * Changelog: add #15138 * Changelog: add #15124 * Changelog:add #15055 * Changelog: add #15017 * Changelog: add #15109 * Changelog: add #15145 * Changelog:add #15096 * Changelog:add #15153 * Changelog: add #15133 * Changelog: add #14960 * Changelog: add #15127 * Changelog: add #15056 * Copy current changelog to changelog archive. * Clarify changelog description
$types was added in 8.4, but we did not add a default value when it's not provided. See #15109 for more details.
$types was added in 8.4, but we did not add a default value when it's not provided. See #15109 for more details.

Fixes #15104,
See #2369
Addresses 4794-jpop-issues, 4988-jpop-issues
Changes proposed in this Pull Request:
This PR improves the way the Top Posts & Pages Widget fetches posts to be displayed.
How it worked before this PR:
summarize=1, it receives up to 500 posts in the responseThe problem here is that if there were 10 posts in the top 10 positions, and you queried for pages, you would get no results.
This PR proposes two changes:
Limitations: This PR does not completely solve the problem. There is no way to query the wp.com API for post types filtering for a specific post type. So the same problem still persists, the only difference is that it now looks in all 500 retrieved posts, and not only on the first 10.
Is this a new feature or does it add/remove features to an existing part of Jetpack?
Testing instructions:
It's very difficult to test the edge cases that this PR fixes, but I'll give some basic testing instructions and describe the edge case afterward.
Run these tests both on a wp.com site and on a JP site.
On an existing site with some stats.
2. in Jetpack settings, activate
Make extra widgets available for use on your site including subscription forms and Twitter streams3. Go to Appearance > Widgets and add two "Top Posts & Pages" Widget
4. Configure one of the widgets to display only posts, and the other one to display only pages
5. Visit the site both logged in and logged out and check if the widgets are working as expected
On a brand new site
The Edge case
The Edge case is a site that has many posts of a certain post type and, when you add a widget to display posts of another post type, this widget would not find any post.
postas its top rankings posts, and few pages with few visitsProposed changelog entry for your changes: