diff --git a/src/dashboard/infrastructure/scores/readability-scores/readability-scores-collector.php b/src/dashboard/infrastructure/scores/readability-scores/readability-scores-collector.php index 8edb76b1ee0..582a8b2be2d 100644 --- a/src/dashboard/infrastructure/scores/readability-scores/readability-scores-collector.php +++ b/src/dashboard/infrastructure/scores/readability-scores/readability-scores-collector.php @@ -3,6 +3,7 @@ // phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded namespace Yoast\WP\SEO\Dashboard\Infrastructure\Scores\Readability_Scores; +use WPSEO_Utils; use Yoast\WP\Lib\Model; use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type; use Yoast\WP\SEO\Dashboard\Domain\Scores\Readability_Scores\Readability_Scores_Interface; @@ -13,6 +14,8 @@ */ class Readability_Scores_Collector implements Scores_Collector_Interface { + public const READABILITY_SCORES_TRANSIENT = 'wpseo_readability_scores'; + /** * Retrieves the current readability scores for a content type. * @@ -24,13 +27,22 @@ class Readability_Scores_Collector implements Scores_Collector_Interface { */ public function get_current_scores( array $readability_scores, Content_Type $content_type, ?int $term_id ) { global $wpdb; + + $content_type_name = $content_type->get_name(); + $transient_name = self::READABILITY_SCORES_TRANSIENT . '_' . $content_type_name . ( ( $term_id === null ) ? '' : '_' . $term_id ); + + $transient = \get_transient( $transient_name ); + if ( $transient !== false ) { + return \json_decode( $transient, false ); + } + $select = $this->build_select( $readability_scores ); $replacements = \array_merge( \array_values( $select['replacements'] ), [ Model::get_table_name( 'Indexable' ), - $content_type->get_name(), + $content_type_name, ] ); @@ -51,6 +63,7 @@ public function get_current_scores( array $readability_scores, Content_Type $con ) ); //phpcs:enable + \set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) ); return $current_scores; } @@ -79,6 +92,7 @@ public function get_current_scores( array $readability_scores, Content_Type $con ) ); //phpcs:enable + \set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) ); return $current_scores; } diff --git a/src/dashboard/infrastructure/scores/seo-scores/seo-scores-collector.php b/src/dashboard/infrastructure/scores/seo-scores/seo-scores-collector.php index 0a8f4503465..44232fa6974 100644 --- a/src/dashboard/infrastructure/scores/seo-scores/seo-scores-collector.php +++ b/src/dashboard/infrastructure/scores/seo-scores/seo-scores-collector.php @@ -3,6 +3,7 @@ // phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded namespace Yoast\WP\SEO\Dashboard\Infrastructure\Scores\SEO_Scores; +use WPSEO_Utils; use Yoast\WP\Lib\Model; use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type; use Yoast\WP\SEO\Dashboard\Domain\Scores\SEO_Scores\SEO_Scores_Interface; @@ -13,6 +14,8 @@ */ class SEO_Scores_Collector implements Scores_Collector_Interface { + public const SEO_SCORES_TRANSIENT = 'wpseo_seo_scores'; + /** * Retrieves the current SEO scores for a content type. * @@ -24,13 +27,22 @@ class SEO_Scores_Collector implements Scores_Collector_Interface { */ public function get_current_scores( array $seo_scores, Content_Type $content_type, ?int $term_id ) { global $wpdb; + + $content_type_name = $content_type->get_name(); + $transient_name = self::SEO_SCORES_TRANSIENT . '_' . $content_type_name . ( ( $term_id === null ) ? '' : '_' . $term_id ); + + $transient = \get_transient( $transient_name ); + if ( $transient !== false ) { + return \json_decode( $transient, false ); + } + $select = $this->build_select( $seo_scores ); $replacements = \array_merge( \array_values( $select['replacements'] ), [ Model::get_table_name( 'Indexable' ), - $content_type->get_name(), + $content_type_name, ] ); @@ -52,6 +64,7 @@ public function get_current_scores( array $seo_scores, Content_Type $content_typ ) ); //phpcs:enable + \set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) ); return $current_scores; } @@ -81,6 +94,7 @@ public function get_current_scores( array $seo_scores, Content_Type $content_typ ) ); //phpcs:enable + \set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) ); return $current_scores; }