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

Allow the post year range to be short-circuited and cached #179

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.phpunit.cache/
/composer.lock
/vendor/
.vscode
36 changes: 27 additions & 9 deletions msm-sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,13 @@ public static function disable_canonical_redirects_for_sitemap_xml( $redirect_ur
public static function get_post_status(): string {
$default_status = 'publish';
$post_status = apply_filters('msm_sitemap_post_status', $default_status);

$allowed_statuses = get_post_stati();

if (!in_array($post_status, $allowed_statuses)) {
$post_status = $default_status;
}

return $post_status;
}

Expand All @@ -352,18 +352,36 @@ public static function get_post_status(): string {
* @return int[] valid years
*/
public static function get_post_year_range() {
global $wpdb;
$post_status = self::get_post_status();
/**
* Allow the post year range to be short-circuited.
*
* @param array|false $pre The pre-filtered value. If false, the default value will be used.
*/
$pre = apply_filters( 'msm_sitemap_pre_get_post_year_range', false );

$oldest_post_date_year = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = %s AND post_date > 0 ORDER BY year ASC LIMIT 1", $post_status ) );
// Return the pre-filtered value if it's an array.
if ( false !== $pre && is_array( $pre ) ) {
return $pre;
}

$oldest_post_date_year = wp_cache_get( 'oldest_post_date_year', 'msm_sitemap' );

if ( false === $oldest_post_date_year ) {
global $wpdb;

$post_status = self::get_post_status();

$oldest_post_date_year = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = %s AND post_date > 0 ORDER BY year ASC LIMIT 1", $post_status ) );

wp_cache_set( 'oldest_post_date_year', $oldest_post_date_year, 'msm_sitemap', WEEK_IN_SECONDS );
}

if ( null !== $oldest_post_date_year ) {
$current_year = date( 'Y' );
return range( (int) $oldest_post_date_year, $current_year );
}

return array();

return [];
}

/**
Expand Down Expand Up @@ -623,7 +641,7 @@ public static function get_last_modified_posts() {
/**
* Filter the query used to get the last modified posts.
* $wpdb->prepare() should be used for security if a new replacement query is created in the callback.
*
*
* @param string $query The query to use to get the last modified posts.
* @param string $post_types_in A comma-separated list of post types to include in the query.
* @param string $date The date to use as the cutoff for the query.
Expand Down