Skip to content

Commit

Permalink
adding PR reviewer suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
elysium001 committed Nov 29, 2023
1 parent 05eb866 commit fa894bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
21 changes: 16 additions & 5 deletions msm-sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,24 @@ public static function disable_canonical_redirects_for_sitemap_xml( $redirect_ur
}

/**
* Helper method to get the custom post status.
* Hook allows developers to extend the sitemap functionality easily and integrate their custom post statuses.
*
* @return string
* Rather than having to modify the plugin code, developers can use this filter to add their custom post statuses.
*
* @since 1.4.3
*
*/
public static function get_post_status() {
return apply_filters('msm_sitemap_post_status', 'publish');
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 @@ -341,7 +352,7 @@ public static function get_post_year_range() {
global $wpdb;
$post_status = self::get_post_status();

$oldest_post_date_year = $wpdb->get_var( "SELECT DISTINCT YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = '$post_status' AND post_date > 0 ORDER BY year ASC LIMIT 1" );
$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 ) );

if ( null !== $oldest_post_date_year ) {
$current_year = date( 'Y' );
Expand Down
15 changes: 15 additions & 0 deletions tests/test-sitemap-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class WP_Test_Sitemap_Functions extends WP_UnitTestCase {
function setup(): void {
$this->test_base = new MSM_SiteMap_Test();

// register new post status.
register_post_status( 'live', array(
'public' => true,
) );
}

/**
Expand Down Expand Up @@ -389,9 +393,20 @@ function test_get_post_status() {
// add filter and verify value.
add_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) );
$this->assertEquals( 'live', Metro_Sitemap::get_post_status() );

add_filter( 'msm_sitemap_post_status', function() {
return 'bad_status';
} );
$this->assertEquals( 'publish', Metro_Sitemap::get_post_status() );

// remove filter.
remove_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) );

// remove filter.
remove_filter( 'msm_sitemap_post_status', function() {
return 'bad_status';
} );

}

function add_post_status_to_msm_sitemap( $post_status ) {
Expand Down

0 comments on commit fa894bf

Please sign in to comment.