diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 00000000..3a03163d --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"WP_Test_Sitemap_Cron::test_cron_jobs_scheduling":3,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #1":3,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #2":3,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #3":3,"WP_Test_Sitemap_Functions::test_check_year_has_posts":3,"WP_Test_Sitemap_Index::test_sitemap_index_by_year":3,"WP_Test_Sitemap_Functions::test_get_post_status":3,"WP_Test_Sitemap_Index::test_single_sitemap_index":4,"WP_Test_Sitemap_Stats::test_site_stats_creation":3,"WP_Test_Sitemap_Stats::test_site_stats_for_new_post":3,"WP_Test_Sitemap_Stats::test_site_stats_for_deleted_post":3,"WP_Test_Sitemap_CPT::test_cpt_ignored_by_default":3,"WP_Test_Sitemap_CPT::test_cpt_included_by_filter":3,"WP_Test_Sitemap_Creation::test_sitemap_posts_were_created":3,"WP_Test_Sitemap_Creation::test_get_sitemap_post_id":3,"WP_Test_Sitemap_Creation::test_delete_empty_sitemap":4,"WP_Test_Sitemap_Functions::test_get_recent_sitemap_url_counts with data set #0":3,"WP_Test_Sitemap_Functions::test_get_recent_sitemap_url_counts with data set #1":3,"WP_Test_Sitemap_Functions::test_get_recent_sitemap_url_counts with data set #2":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #2":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #3":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #4":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #5":3,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date with data set #1":3,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date with data set #2":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #1":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #2":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #3":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #4":3,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #5":3,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #1":3,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #2":3,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #3":3,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date_custom_status with data set #1":3,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date_custom_status with data set #2":3},"times":{"WP_Test_Sitemap_Cron::test_cron_jobs_scheduling":2.82,"WP_Test_Sitemap_CPT::test_cpt_ignored_by_default":0.283,"WP_Test_Sitemap_CPT::test_cpt_included_by_filter":0.475,"WP_Test_Sitemap_Creation::test_sitemap_posts_were_created":0.346,"WP_Test_Sitemap_Creation::test_get_sitemap_post_id":0.305,"WP_Test_Sitemap_Creation::test_delete_empty_sitemap":0.333,"WP_Test_Sitemap_Filter::test_bypass_main_query":0.004,"WP_Test_Sitemap_Filter::test_secondary_query_not_bypassed":0.004,"WP_Test_Sitemap_Filter::test_msm_sitemap_index_filter_ran":0.002,"WP_Test_Sitemap_Functions::test_get_recent_sitemap_url_counts with data set #0":0.978,"WP_Test_Sitemap_Functions::test_get_recent_sitemap_url_counts with data set #1":0.921,"WP_Test_Sitemap_Functions::test_get_recent_sitemap_url_counts with data set #2":0.939,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #0":0.001,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #1":0.057,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #2":0.056,"WP_Test_Sitemap_Functions::test_get_post_year_range with data set #3":0.06,"WP_Test_Sitemap_Functions::test_check_year_has_posts":0.1,"WP_Test_Sitemap_Functions::test_get_date_stamp with data set #0":0.001,"WP_Test_Sitemap_Functions::test_get_date_stamp with data set #1":0,"WP_Test_Sitemap_Functions::test_get_date_stamp with data set #2":0,"WP_Test_Sitemap_Functions::test_get_date_stamp with data set #3":0,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #0":0.156,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #1":0.157,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #2":0.147,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #3":0.164,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #4":0.15,"WP_Test_Sitemap_Functions::test_date_range_has_posts with data set #5":0.161,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date with data set #0":0.05,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date with data set #1":1.043,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date with data set #2":1.131,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date with data set #3":0.042,"WP_Test_Sitemap_Index::test_single_sitemap_index":0.267,"WP_Test_Sitemap_Index::test_sitemap_index_by_year":0.342,"WP_Test_Sitemap_Stats::test_site_stats_creation":0.223,"WP_Test_Sitemap_Stats::test_site_stats_for_new_post":0.42,"WP_Test_Sitemap_Stats::test_site_stats_for_deleted_post":0.356,"WP_Test_Sitemap_Functions::test_get_post_status":0.001,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #0":0.154,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #1":0.172,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #2":0.138,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #3":0.143,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #4":0.135,"WP_Test_Sitemap_Functions::test_date_range_has_posts_custom_status with data set #5":0.134,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #0":0.002,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #1":0.053,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #2":0.035,"WP_Test_Sitemap_Functions::test_get_post_year_range_custom_status_posts with data set #3":0.036,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date_custom_status with data set #0":0.052,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date_custom_status with data set #1":0.927,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date_custom_status with data set #2":0.984,"WP_Test_Sitemap_Functions::test_get_post_ids_for_date_custom_status with data set #3":0.053}} \ No newline at end of file diff --git a/msm-sitemap.php b/msm-sitemap.php index ceeab3d0..5c25c900 100644 --- a/msm-sitemap.php +++ b/msm-sitemap.php @@ -124,7 +124,7 @@ public static function ajax_get_sitemap_counts() { $data = array( 'total_indexed_urls' => number_format( Metro_Sitemap::get_total_indexed_url_count() ), - 'total_sitemaps' => number_format( Metro_Sitemap::count_sitemaps() ), + 'total_sitemaps' => number_format( Metro_Sitemap::count_sitemaps() ), 'sitemap_indexed_urls' => self::get_recent_sitemap_url_counts( $n ), ); @@ -156,7 +156,7 @@ public static function render_sitemap_options_page() { if ( isset( $_POST['action'] ) ) { check_admin_referer( 'msm-sitemap-action' ); foreach ( $actions as $slug => $action ) { - if ( $action['text'] !== $_POST['action'] ) continue; + if ( $action['text'] !== $_POST['action'] ) continue; do_action( 'msm_sitemap_action-' . $slug ); break; @@ -323,14 +323,24 @@ public static function disable_canonical_redirects_for_sitemap_xml( $redirect_ur return $redirect_url; } + /** + * Helper method to get the custom post status. + * + * @return string + */ + public static function get_post_status() { + return apply_filters('msm_sitemap_post_status', 'publish'); + } + /** * Return range of years for posts in the database * @return int[] valid years */ 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 = 'publish' AND post_date > 0 ORDER BY year ASC LIMIT 1" ); + $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" ); if ( null !== $oldest_post_date_year ) { $current_year = date( 'Y' ); @@ -381,9 +391,10 @@ public static function date_range_has_posts( $start_date, $end_date ) { $start_date .= ' 00:00:00'; $end_date .= ' 23:59:59'; + $post_status = self::get_post_status(); $post_types_in = self::get_supported_post_types_in(); - return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_date >= %s AND post_date <= %s AND post_type IN ( {$post_types_in} ) LIMIT 1", $start_date, $end_date ) ); + return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_status = %s AND post_date >= %s AND post_date <= %s AND post_type IN ( {$post_types_in} ) LIMIT 1", $post_status, $start_date, $end_date ) ); } /** @@ -396,11 +407,12 @@ public static function date_range_has_posts( $start_date, $end_date ) { public static function get_post_ids_for_date( $sitemap_date, $limit = 500 ) { global $wpdb; + $post_status = self::get_post_status(); $start_date = $sitemap_date . ' 00:00:00'; $end_date = $sitemap_date . ' 23:59:59'; $post_types_in = self::get_supported_post_types_in(); - $posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_date >= %s AND post_date <= %s AND post_type IN ( {$post_types_in} ) LIMIT %d", $start_date, $end_date, $limit ) ); + $posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_date FROM $wpdb->posts WHERE post_status = %s AND post_date >= %s AND post_date <= %s AND post_type IN ( {$post_types_in} ) LIMIT %d", $post_status, $start_date, $end_date, $limit ) ); usort( $posts, array( __CLASS__ , 'order_by_post_date' ) ); @@ -425,7 +437,7 @@ public static function generate_sitemap_for_date( $sitemap_date ) { 'post_name' => $sitemap_name, 'post_title' => $sitemap_name, 'post_type' => self::SITEMAP_CPT, - 'post_status' => 'publish', + 'post_status' => self::get_post_status(), 'post_date' => $sitemap_date, ); diff --git a/tests/test-sitemap-functions.php b/tests/test-sitemap-functions.php index 85398ca2..8f230283 100644 --- a/tests/test-sitemap-functions.php +++ b/tests/test-sitemap-functions.php @@ -134,6 +134,31 @@ function test_get_post_year_range( $years, $range_values ) { $this->assertEquals( $range_values, count( $year_range ) ); } + /** + * Verify get_post_year_range returns proper year ranges with custom status hook + * + * @dataProvider postYearRangeDataProvider + * @param int @years # of Years. + * @param int @range_values # of years in range. + */ + function test_get_post_year_range_custom_status_posts( $years, $range_values ) { + // set msm_sitemap_post_status filter to custom_status. + add_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + + // Add a post for each day in the last x years. + if ( 'none' !== $years ) { + $date = strtotime( "-$years year", time() ); + $cur_day = date( 'Y', $date ) . '-' . date( 'm', $date ) . '-' . date( 'd', $date ) . ' 00:00:00'; + $this->test_base->create_dummy_post( $cur_day, 'live' ); + } + + $year_range = Metro_Sitemap::get_post_year_range(); + $this->assertEquals( $range_values, count( $year_range ) ); + + // remove filter. + remove_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + } + /** * Verify check_year_has_posts returns only years with posts */ @@ -201,6 +226,18 @@ public function dateRangeHasPostsDataProvider() { ); } + /** + * Data Provider for date_range_has_posts + * + * @return array( str, str, boolean ) Array of Test parameters. + */ + public function dateRangeHasPostsCustomStatusDataProvider() { + return array( + array( '2016-11-01', '2016-12-15', false ), + array( '2014-12-28', '2016-05-04', true ), + ); + } + /** * Verify date_range_has_posts returns expected value * @@ -229,6 +266,40 @@ function test_date_range_has_posts( $start_date, $end_date, $has_post ) { } + /** + * Verify date_range_has_posts returns expected value with custom status hook + * + * @dataProvider dateRangeHasPostsCustomStatusDataProvider + * @param Str $start_date Start Date of Range in Y-M-D format. + * @param Str $end_date End Date of Range in Y-M-D format. + * @param boolean $has_post Does Range have Post. + */ + function test_date_range_has_posts_custom_status( $start_date, $end_date, $has_post ) { + + // set msm_sitemap_post_status filter to custom_status. + add_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + + // 1 for 2016-10-12 in "live" status. + $this->test_base->create_dummy_post( '2015-10-12 00:00:00', 'live' ); + + // 1 for 2016-01-01. + $this->test_base->create_dummy_post( '2016-01-01 00:00:00' ); + + // // 1 for 2015-06-02. + $this->test_base->create_dummy_post( '2015-06-02 00:00:00' ); + + // Validate Range result. + if ( $has_post ) { + $this->assertNotNull( Metro_Sitemap::date_range_has_posts( $start_date, $end_date ) ); + } else { + $this->assertNull( Metro_Sitemap::date_range_has_posts( $start_date, $end_date ) ); + } + + // remove filter. + remove_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + + } + /** * Data Provider for get_post_ids_for_date @@ -273,4 +344,58 @@ function test_get_post_ids_for_date( $sitemap_date, $limit, $expected_count ) { } + /** + * Verify get_post_ids_for_date returns expected value with custom status hook + * + * @dataProvider postIdsForDateDataProvider + * @param str $sitemap_date Date in Y-M-D format. + * @param str $limit max number of posts to return. + * @param int $expected_count Number of posts expected to be returned. + */ + function test_get_post_ids_for_date_custom_status( $sitemap_date, $limit, $expected_count ) { + + // set msm_sitemap_post_status filter to custom_status. + add_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + + // 1 for 2016-10-03 in "draft" status. + $this->test_base->create_dummy_post( '2016-10-01 00:00:00', 'draft' ); + + $created_post_ids = array(); + // 20 for 2016-10-02. + for ( $i = 0; $i < 20; $i ++ ) { + $hour = $i < 10 ? '0' . $i : $i; + if ( '2016-10-02' === $sitemap_date ) { + $created_post_ids[] = $this->test_base->create_dummy_post( '2016-10-02 ' . $hour . ':00:00', 'live' ); + } + } + + + $post_ids = Metro_Sitemap::get_post_ids_for_date( $sitemap_date, $limit ); + $this->assertEquals( $expected_count, count( $post_ids ) ); + $this->assertEquals( array_slice( $created_post_ids, 0, $limit ), $post_ids ); + + // remove filter. + remove_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + } + + /** + * Verify msm_sitemap_post_status filter returns expected value + */ + function test_get_post_status() { + + // verify default value. + $this->assertEquals( 'publish', Metro_Sitemap::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() ); + + // remove filter. + remove_filter( 'msm_sitemap_post_status', array( $this, 'add_post_status_to_msm_sitemap' ) ); + } + + function add_post_status_to_msm_sitemap( $post_status ) { + return 'live'; + } + }