-
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-2708: Save sophi responses in CPT #298
Conversation
sophi.php
Outdated
@@ -60,6 +61,7 @@ function() { | |||
// Bootstrap. | |||
SophiWP\Core\setup(); | |||
SophiWP\Settings\setup(); | |||
SophiWP\PostType\post_type(); |
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.
To keep consistency, we probably should have a setup() wrapping this function as well @oscarssanchez . What do you think?
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.
Sounds good @felipeelia i've addressed it here 9b104eb
if ( ! $response ) { | ||
return []; | ||
} | ||
|
||
if ( ! $bypass_cache ) { | ||
update_option( "sophi_site_automation_data_{$this->page}_{$this->widget}", $response ); | ||
if ( ! $bypass_cache && post_type_exists('sophi-response') ) { |
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.
Can we make post_type_exists('sophi-response')
to post_type_exists( 'sophi-response')
? :)
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.
Sure addressed it here 8089267
if ( $post_id ) { | ||
update_post_meta( $post_id, "sophi_site_automation_data_{$this->page}_{$this->widget}", $response ); | ||
update_post_meta( $post_id, 'sophi_site_automation_last_updated', time() ); | ||
} else { | ||
$post_id = wp_insert_post( | ||
[ | ||
'post_type' => 'sophi-response', | ||
'post_title' => "sophi_site_automation_data_{$this->page}_{$this->widget}", | ||
] | ||
); | ||
if ( $post_id ) { | ||
update_post_meta( $post_id, "sophi_site_automation_data_{$this->page}_{$this->widget}", $response ); | ||
update_post_meta( $post_id, 'sophi_site_automation_last_updated', 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.
Realizing this is much more of a personal preference than anything, can't we check for ! $post_id
and create the post, and only then call the update_post_meta lines (only once)?
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 think that simplifies the logic and makes it more readable, I've addressed it here: bd0b247
if ( ! $bypass_cache && post_type_exists('sophi-response' ) ) { | ||
$query = new \WP_Query( | ||
[ | ||
's' => "sophi_site_automation_data_{$page}_{$widget}", |
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.
Did you explore using post_name__in
here instead? Just wondering if someone has ElasticPress and Protected Content enabled (just as an example) if this wouldn't bring the wrong result.
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.
(Obviously, this would require an adjustment in the wp_insert_post call below)
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.
@cadic @Sidsector9 @iamdharmesh looking to see if one of you can also review this given your experience on Sophi over the last few months. Thanks in advance! |
@@ -56,9 +54,27 @@ function render_block_callback( $attributes, $content, $block ) { | |||
*/ | |||
$bypass_cache = apply_filters( 'sophi_bypass_curated_posts_cache', false, $page_name, $widget_name ); | |||
|
|||
$curated_posts = get_transient( $curated_posts_transient_key ); | |||
$sophi_cached_response = new \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.
When $bypass_cache === true
, the if
statement at 76 will not check for $curated_posts
at all. I think we are safe to skip 57:74 in this case.
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.
In this case I think we should still do the query. The most important check is if $bypass_cache === true
, and the next one should be if there's $curated_posts
does it makes sense @cadic ?
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 thank you for explaining. I mean we could make the query optional.
- When
$bypass_cache === true
, we will execute contents of if statement in 78:90 regardless the$curated_posts
initial value, it will be overwrited withget_posts
. - When
$bypass_cache === false
, this if statement will only be executed if$curate_posts
is negative.
This means to me that we only need to take care about the initial value for $curated_posts
when $bypass_cache === false
$curated_posts = false;
if ( false === $bypass_cache ) {
$sophi_cached_response = new \WP_Query(
[
'post_name__in' => [ "sophi-site-automation-data-{$page_name}-{$widget_name}" ],
'post_type' => 'sophi-response',
'posts_per_page' => 1,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_term_cache' => false
]
);
if ( $sophi_cached_response->found_posts ) {
$last_update = get_post_meta( $sophi_cached_response->posts[0], 'sophi_site_automation_last_updated', true );
if ( $last_update + 5 * MINUTE_IN_SECONDS > time() ) {
$curated_posts = get_post_meta( $sophi_cached_response->posts[0], 'sophi_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.
@oscarssanchez was ^ this addressed in your latest commits?
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.
Co-authored-by: Max Lyuchin <maxim.lyuchin@gmail.com>
…ail/sophi-for-wordpress into feature/sophi-response-caching
…ail/sophi-for-wordpress into feature/sophi-response-caching
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Description of the Change
This approach creates a new CPT
sophi-response
that will save the sophi response as post meta. To check the expiration date, we store a post meta field and compare it to the current timestamp to check if we need to update the data from Sophi.Closes #
Alternate Designs
Possible Drawbacks
Verification Process
Checklist:
Changelog Entry
Credits
Props @