-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SIT-2911: Return empty post list from Sophi response #183
Conversation
return $query->num_posts; | ||
} | ||
|
||
return $found_posts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oscarssanchez wondering why you're taking the route of adding a filter?
Adding a filter is perhaps the "more correct" way to update found_posts
, at the same time I'm thinking this filter runs on every query. Perhaps just flat out setting it directly in stead of the hop skip & jump of the $query->num_posts value would be more straightforward. Jetpack doesn't seem to mind doing that (if that's anything to go by...)
Also, and perhaps I'm not reading this correctly, but if someone decides to use the sophi_curated_post_list filter, they might need to override this filter as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @barryceelen ,
I believe this will be overwritten as there's some places where set_found_posts()
is called after posts_pre_query
. I ran a few tests and it seems to be the case.
If someone uses the sophi_curated_post_list
, since $query->num_posts
has been defined as well, developers can alter its value so that it later replaces $query->found_posts
@@ -81,7 +81,7 @@ public function get( $page, $widget ) { | |||
$this->status = $this->get_status(); | |||
$site_automation_data = get_option( "sophi_site_automation_data_{$page}_{$widget}" ); | |||
|
|||
if ( ! empty( $this->status['success'] ) && $site_automation_data ) { | |||
if ( ! empty( $this->status['success'] ) && $site_automation_data && ( defined( 'SOPHI_BYPASS_CACHE' ) && ! SOPHI_BYPASS_CACHE ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SOPHI_BYPASS_CACHE doesn't bypass data completely: if is_wp_error( $response )
it will still return $site_automation_data on line 101 if that is available. To prevent that from happening, perhaps first define $site_automation_data = false
and then fetch using get_option(...) only if ! SOPHI_BYPASS_CACHE
?
$site_automation_data = false;
if ( ! defined( 'SOPHI_BYPASS_CACHE' ) || ! SOPHI_BYPASS_CACHE ) {
$site_automation_data = get_option( "sophi_site_automation_data_{$page}_{$widget}" );
}
if ( $site_automation_data && ! empty( $this->status['success'] ) ) {
return $site_automation_data;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call @barryceelen I've adjusted here: 749241e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay!
Perhaps nitpicking: While the approach here works for the immediate need of bypassing cache in this function, from an architectural point of view I would expect setting a global variable to disable cache would also prevent the plugin from creating those caches and as well as retrieving them.
One fairly straightforward approach could be to introduce something along the lines of sophi_cache_set( .... )
and sophi_cache_get( ... )
functions, in stead of directly using the get_option() and set_option() (or ..transient() ) functions like the plugin currently does, and check the bypass cache global in those functions in stead. (That would also provide a nice point to introduce a filter that could decide to disable cache for a specific page/widget combination, or all of them, in stead of using a global that always disables all of them) @jeffpaul for thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree that would be nice to have, though wondering how would it fit with the schedule and if leaving this for now would have a huge impact in the future for people using that constant if we go the filter approach as well @felipeelia
* @param {string} $query_vars['sophi_curated_page'] Sophi curated page param. | ||
* @param {string} $query_vars['sophi_curated_widget'] Sophi curated widget param. | ||
* @param {array} $request_status The request status, whether it was successful or not. | ||
* @param {object} $query Original query. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker @oscarssanchez but as we know the type of object here, it would be good to explicitly state @param {WP_Query}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here 489f019
|
||
// Determine how we should format the results based on the fields parameter. | ||
$fields = $query->get( 'fields', '' ); | ||
if ( $request_status['success'] ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it seems we are not guaranteeing that $this->request->get_status()
will return an array nor if that array will contain a 'success'
index, it would be to check if ! empty( $request_status['success'] )
. What do you think @oscarssanchez? Otherwise, I think people can get a not defined
warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @felipeelia I agree. Resolved here fa6b527
@@ -79,9 +79,13 @@ public function get( $page, $widget ) { | |||
$this->api_url = $this->set_api_url( $page, $widget ); | |||
|
|||
$this->status = $this->get_status(); | |||
$site_automation_data = get_option( "sophi_site_automation_data_{$page}_{$widget}" ); | |||
$site_automation_data = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, the return type is set to array|null
, it seems it should be array|bool
. Also, would it make sense to also have a third parameter to this function to skip cache or not instead of relying solely on a constant that will affect the behavior every time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed return type here 4ca47c7 @felipeelia
Kudos, SonarCloud Quality Gate passed!
|
* | ||
* @param {bool} $bypass_cache True or false. | ||
* @param {bool} $page Page name. | ||
* @param {bool} $widget Widget name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oscarssanchez the last two parameters are specified as bool, they should be string. (Nitpicking: parameter descriptions should be aligned)
Also, correct me if I'm wrong, I believe phpdoc does not require your parameter type inside of { ...} curly brackets (that's a jsdoc convention).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(The same goes for the 'sophi_curated_post_list' filter doc in Integration.php)
Description of the Change
This PR fixes the response coming from Sophi when it returns an empty post list. Previously, the response was ignored and WP_Query's default was used instead.
Alternate Designs
Benefits
Possible Drawbacks
Verification Process
Checklist:
Applicable Issues
Changelog Entry