Skip to content
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

Merged
merged 18 commits into from
Jun 13, 2022
Merged

Conversation

oscarssanchez
Copy link
Contributor

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:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Credits

Props @

@oscarssanchez oscarssanchez requested a review from felipeelia May 31, 2022 19:35
@jeffpaul jeffpaul added this to the 1.1.4 milestone May 31, 2022
@jeffpaul jeffpaul changed the title Save sophi responses in CPT SIT-2708: Save sophi responses in CPT May 31, 2022
@jeffpaul jeffpaul linked an issue May 31, 2022 that may be closed by this pull request
sophi.php Outdated
@@ -60,6 +61,7 @@ function() {
// Bootstrap.
SophiWP\Core\setup();
SophiWP\Settings\setup();
SophiWP\PostType\post_type();

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?

Copy link
Contributor Author

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') ) {

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')? :)

Copy link
Contributor Author

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

Comment on lines 288 to 302
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() );
}
}

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)?

Copy link
Contributor Author

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}",

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.

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a more reliable approach indeed, I have addressed it here 39094a0 and here c737ab6

@oscarssanchez oscarssanchez requested a review from felipeelia June 1, 2022 18:38
@jeffpaul jeffpaul modified the milestones: 1.1.4, 1.2.0 Jun 1, 2022
felipeelia
felipeelia previously approved these changes Jun 2, 2022
@jeffpaul
Copy link
Contributor

jeffpaul commented Jun 2, 2022

@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(
Copy link
Contributor

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.

Copy link
Contributor Author

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 ?

Copy link
Contributor

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.

  1. When $bypass_cache === true, we will execute contents of if statement in 78:90 regardless the $curated_posts initial value, it will be overwrited with get_posts.
  2. 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' );
		}
	}
}

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cadic Thanks for clarifying, that makes sense to me! I have addressed this here: c40cf6e

includes/classes/SiteAutomation/Request.php Outdated Show resolved Hide resolved
includes/classes/SiteAutomation/Request.php Outdated Show resolved Hide resolved
includes/classes/SiteAutomation/Request.php Outdated Show resolved Hide resolved
includes/classes/SiteAutomation/Request.php Outdated Show resolved Hide resolved
oscarssanchez and others added 5 commits June 6, 2022 10:54
Co-authored-by: Max Lyuchin <maxim.lyuchin@gmail.com>
…ail/sophi-for-wordpress into feature/sophi-response-caching
@oscarssanchez oscarssanchez requested a review from cadic June 6, 2022 16:12
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@jeffpaul jeffpaul merged commit e551ace into develop Jun 13, 2022
@jeffpaul jeffpaul deleted the feature/sophi-response-caching branch June 13, 2022 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SIT-2708: Should curation result posts be saved in a site option?
4 participants