| | 722 | |
| | 723 | /** |
| | 724 | * Test that a revision with an array in meta saves correctly even when included in `_wp_post_revision_fields`. |
| | 725 | * |
| | 726 | * @ticket 59827 |
| | 727 | */ |
| | 728 | public function test_wp_save_post_revision_with_array_meta() { |
| | 729 | |
| | 730 | // Add a meta field to revision that includes a default value. |
| | 731 | register_post_meta( |
| | 732 | 'post', |
| | 733 | 'meta_revision_test', |
| | 734 | array( |
| | 735 | 'single' => true, |
| | 736 | 'type' => 'array', |
| | 737 | 'revisions_enabled' => true, |
| | 738 | ) |
| | 739 | ); |
| | 740 | |
| | 741 | // Add the meta value to `_wp_post_revision_fields` which triggers it being compared for save consideration. |
| | 742 | add_filter( |
| | 743 | '_wp_post_revision_fields', |
| | 744 | function ( $fields ) { |
| | 745 | $fields['meta_revision_test'] = 'Meta Revisions Test'; |
| | 746 | return $fields; |
| | 747 | } |
| | 748 | ); |
| | 749 | |
| | 750 | // Set up a new post. |
| | 751 | $post_id = $this->factory->post->create( |
| | 752 | array( |
| | 753 | 'post_content' => 'initial content', |
| | 754 | 'meta_input' => array( |
| | 755 | 'meta_revision_test' => 'foo', |
| | 756 | ), |
| | 757 | ) |
| | 758 | ); |
| | 759 | |
| | 760 | // Set the test meta to an array. |
| | 761 | update_post_meta( $post_id, 'meta_revision_test', array( 'test' ) ); |
| | 762 | |
| | 763 | // Update to save. |
| | 764 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 765 | |
| | 766 | // Check that the meta is stored correctly. |
| | 767 | $stored_data = get_post_meta( $post_id, 'meta_revision_test', true ); |
| | 768 | $this->assertEquals( array( 'test' ), $stored_data ); |
| | 769 | |
| | 770 | // Also verify that the latest revision has stored the meta. |
| | 771 | $revisions = wp_get_post_revisions( $post_id ); |
| | 772 | $last_revision = array_shift( $revisions ); |
| | 773 | $stored_data = get_post_meta( $last_revision->ID, 'meta_revision_test', true ); |
| | 774 | $this->assertEquals( array( 'test' ), $stored_data ); |
| | 775 | |
| | 776 | // Update the meta. |
| | 777 | update_post_meta( $post_id, 'meta_revision_test', array( 'changed' ) ); |
| | 778 | |
| | 779 | // Update to save. |
| | 780 | wp_update_post( |
| | 781 | array( |
| | 782 | 'ID' => $post_id, |
| | 783 | 'post_content' => 'content update 1', |
| | 784 | ) |
| | 785 | ); |
| | 786 | |
| | 787 | // Check that the correct meta value is returned. |
| | 788 | $this->assertEquals( array( 'changed' ), get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 789 | |
| | 790 | // Also verify that the latest revision has the correct meta value. |
| | 791 | $revisions = wp_get_post_revisions( $post_id ); |
| | 792 | $last_revision = array_shift( $revisions ); |
| | 793 | |
| | 794 | // Set the test meta again. |
| | 795 | update_post_meta( $post_id, 'meta_revision_test', 'test' ); |
| | 796 | |
| | 797 | // Update to save. |
| | 798 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 799 | |
| | 800 | // Now restore the previous revision. |
| | 801 | wp_restore_post_revision( $last_revision->ID ); |
| | 802 | |
| | 803 | // Verify the correct meta value is still returned. |
| | 804 | $this->assertEquals( array( 'changed' ), get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 805 | } |