diff --git a/classes/class-page-types.php b/classes/class-page-types.php index fff857131..afee7f684 100644 --- a/classes/class-page-types.php +++ b/classes/class-page-types.php @@ -35,7 +35,8 @@ public function __construct() { \add_action( 'init', [ $this, 'maybe_update_terms' ] ); // Add hook when updating the `page_on_front` option. - \add_action( 'update_option_page_on_front', [ $this, 'update_option_page_on_front' ] ); + \add_action( 'update_option_page_on_front', [ $this, 'update_option_page_on_front' ], 10, 2 ); + \add_action( 'update_option_show_on_front', [ $this, 'update_option_show_on_front' ], 10, 2 ); // Add activity when a post is added or updated. \add_action( 'post_updated', [ $this, 'post_updated' ], 10, 2 ); @@ -366,39 +367,67 @@ public function get_taxonomy_name() { } /** - * Update the `page_on_front` option. + * Move the homepage page-type to the new `page_on_front` page. * - * @param int $page_id The page ID. + * WordPress passes the old value first: tagging that one would mark the + * previous homepage, and post_updated() would then make it the homepage + * again the next time it is saved. + * + * @param mixed $old_value The previous page ID (unused). + * @param mixed $page_id The new page ID. * * @return void */ - public function update_option_page_on_front( $page_id ) { - // Get the posts for the homepage. - $posts = $this->get_posts_by_type( 'page', 'homepage' ); - $term = \get_term_by( 'slug', 'homepage', self::TAXONOMY_NAME ); + public function update_option_page_on_front( $old_value, $page_id ) { + $page_id = (int) $page_id; + $term = \get_term_by( 'slug', 'homepage', self::TAXONOMY_NAME ); if ( ! $term ) { return; } - $same_page = false; - foreach ( $posts as $post ) { - if ( (int) $post->ID === (int) $page_id ) { - $same_page = true; - continue; - } + // Everything that has the homepage page-type, in any status. Not + // get_posts_by_type(): its per-request cache would miss a page tagged + // earlier in the same request. + $tagged = \get_objects_in_term( $term->term_id, self::TAXONOMY_NAME ); + $tagged = \is_array( $tagged ) ? \array_map( 'intval', $tagged ) : []; - // Remove the term from the post. - \wp_remove_object_terms( $post->ID, $term->term_id, self::TAXONOMY_NAME ); + foreach ( $tagged as $object_id ) { + if ( $object_id !== $page_id ) { + \wp_remove_object_terms( $object_id, $term->term_id, self::TAXONOMY_NAME ); + } } - if ( ! $same_page ) { - // Add the term to the new homepage. + // No page (page_on_front set to 0): nothing to tag. + if ( $page_id > 0 && ! \in_array( $page_id, $tagged, true ) ) { \wp_set_object_terms( $page_id, $term->term_id, self::TAXONOMY_NAME ); self::assign_child_pages( $page_id, $term->term_id ); } } + /** + * Remove the homepage page-type when the site stops using a static homepage. + * + * WordPress keeps page_on_front when "Your latest posts" is chosen, so + * update_option_page_on_front() does not run. Without this, the former + * homepage keeps the page-type, and post_updated() switches the site back + * to a static homepage the next time that page is saved. + * + * @param mixed $old_value The previous value (unused). + * @param mixed $value The new value. + * + * @return void + */ + public function update_option_show_on_front( $old_value, $value ) { + if ( 'page' === $value ) { + // Back to a static homepage: tag page_on_front again. + $this->update_option_page_on_front( null, \get_option( 'page_on_front' ) ); + return; + } + + $this->update_option_page_on_front( null, 0 ); + } + /** * Post updated. * diff --git a/tests/phpunit/test-class-page-types.php b/tests/phpunit/test-class-page-types.php index e55eef843..1f4331837 100644 --- a/tests/phpunit/test-class-page-types.php +++ b/tests/phpunit/test-class-page-types.php @@ -151,6 +151,75 @@ public function test_get_default_page_type() { * @return void */ public function test_update_option_page_on_front() { + $taxonomy = \progress_planner()->get_page_types()::TAXONOMY_NAME; + $first = self::factory()->post->create( [ 'post_type' => 'page' ] ); + $second = self::factory()->post->create( [ 'post_type' => 'page' ] ); + + \update_option( 'show_on_front', 'page' ); + \update_option( 'page_on_front', $first ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $first ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $second ) ); + + // The new homepage gets the page-type, not the previous one. + \update_option( 'page_on_front', $second ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $second ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $first ) ); + + // Saving the previous homepage must not make it the homepage again. + \wp_update_post( + [ + 'ID' => $first, + 'post_title' => 'Saved again', + ] + ); + $this->assertSame( $second, (int) \get_option( 'page_on_front' ) ); + + // A draft that still has the page-type (from before this fix) loses it too. + $draft = self::factory()->post->create( + [ + 'post_type' => 'page', + 'post_status' => 'draft', + ] + ); + \wp_set_object_terms( $draft, 'homepage', $taxonomy ); + \update_option( 'page_on_front', $first ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $draft ) ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $first ) ); + + // No homepage: the page-type is removed and nothing else is tagged. + \update_option( 'page_on_front', 0 ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $first ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, 0 ) ); + } + + /** + * Switching to "Your latest posts" removes the homepage page-type, and + * switching back restores it on page_on_front. + * + * @return void + */ + public function test_update_option_show_on_front() { + $taxonomy = \progress_planner()->get_page_types()::TAXONOMY_NAME; + $page = self::factory()->post->create( [ 'post_type' => 'page' ] ); + + \update_option( 'show_on_front', 'page' ); + \update_option( 'page_on_front', $page ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $page ) ); + + \update_option( 'show_on_front', 'posts' ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $page ) ); + + // Saving the former homepage keeps the latest posts on the front page. + \wp_update_post( + [ + 'ID' => $page, + 'post_title' => 'Saved again', + ] + ); + $this->assertSame( 'posts', \get_option( 'show_on_front' ) ); + + \update_option( 'show_on_front', 'page' ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $page ) ); } /**