| | 388 | |
| | 389 | /** |
| | 390 | * Test the revisions system for storage of meta values |
| | 391 | * @ticket 20564 |
| | 392 | */ |
| | 393 | function test_revisions_stores_meta_values() { |
| | 394 | // Set up a new post |
| | 395 | $original_post_id = $post_id = $this->factory->post->create(); |
| | 396 | // And update to store an initial revision |
| | 397 | wp_update_post( array( 'post_content' => 'some initial content', 'ID' => $post_id ) ); |
| | 398 | |
| | 399 | // One revision so far |
| | 400 | $revisions = wp_get_post_revisions( $post_id ); |
| | 401 | $this->assertCount( 1, $revisions ); |
| | 402 | /** |
| | 403 | * First set up a meta value |
| | 404 | */ |
| | 405 | |
| | 406 | // Store a custom meta value, which is not revisioned by default |
| | 407 | update_post_meta( $post_id, 'meta_revision_test', 'original' ); |
| | 408 | |
| | 409 | // Update the post, storing a revision |
| | 410 | wp_update_post( array( 'post_content' => 'some more content', 'ID' => $post_id ) ); |
| | 411 | |
| | 412 | $revisions = wp_get_post_revisions( $post_id ); |
| | 413 | $this->assertCount( 2, $revisions ); |
| | 414 | |
| | 415 | |
| | 416 | // Next, store some updated meta values for the same key |
| | 417 | update_post_meta( $post_id, 'meta_revision_test', 'update1' ); |
| | 418 | |
| | 419 | // Save the post, changing content to force a revision |
| | 420 | wp_update_post( array( 'post_content' => 'some updated content', 'ID' => $post_id ) ); |
| | 421 | |
| | 422 | $revisions = wp_get_post_revisions( $post_id ); |
| | 423 | $this->assertCount( 3, $revisions ); |
| | 424 | |
| | 425 | |
| | 426 | /** |
| | 427 | * Now restore the original revision |
| | 428 | */ |
| | 429 | |
| | 430 | // Restore the previous revision |
| | 431 | $revisions = (Array)wp_get_post_revisions( $post_id ); |
| | 432 | // Go back two to load the previous revision |
| | 433 | array_shift( $revisions ); |
| | 434 | $last_revision = array_shift( $revisions ); |
| | 435 | // Restore! |
| | 436 | wp_restore_post_revision( $last_revision->ID ); |
| | 437 | |
| | 438 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 439 | $revisions = wp_get_post_revisions( $post_id ); |
| | 440 | $this->assertCount( 4, $revisions ); |
| | 441 | |
| | 442 | /** |
| | 443 | * Check the meta values to verify they are NOT revisioned - they are not revisioned by default |
| | 444 | */ |
| | 445 | |
| | 446 | // Custom post meta should NOT be restored, orignal value should not be restored, value still 'update1' |
| | 447 | $this->assertEquals( 'update1', get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 448 | |
| | 449 | update_post_meta( $post_id, 'meta_revision_test', 'update2' ); |
| | 450 | |
| | 451 | |
| | 452 | /* |
| | 453 | * Now test the revisioning of custom meta when enabled by the wp_post_revision_meta_keys filter |
| | 454 | */ |
| | 455 | |
| | 456 | // Add the custom field to be revised via the wp_post_revision_meta_keys filter |
| | 457 | add_filter( 'wp_post_revision_meta_keys', function( $keys ) { |
| | 458 | $keys[] = 'meta_revision_test'; |
| | 459 | return $keys; |
| | 460 | }); |
| | 461 | |
| | 462 | // Save the post, changing content to force a revision |
| | 463 | wp_update_post( array( 'post_content' => 'more updated content', 'ID' => $post_id ) ); |
| | 464 | |
| | 465 | $revisions = wp_get_post_revisions( $post_id ); |
| | 466 | $this->assertCount( 5, $revisions ); |
| | 467 | |
| | 468 | // Store custom meta values, which should now be revisioned |
| | 469 | update_post_meta( $post_id, 'meta_revision_test', 'update3' ); |
| | 470 | |
| | 471 | /** |
| | 472 | * Save the post again, custom meta should now be revisioned |
| | 473 | * |
| | 474 | * Note that a revision is saved even though there is no change |
| | 475 | * in post content, becaused the revisioned post_meta has changed |
| | 476 | * |
| | 477 | */ |
| | 478 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 479 | |
| | 480 | // This revision contains the existing post meta ('update3') |
| | 481 | $revisions = wp_get_post_revisions( $post_id ); |
| | 482 | $this->assertCount( 6, $revisions ); |
| | 483 | |
| | 484 | // Verify that previous post meta is set |
| | 485 | $this->assertEquals( 'update3', get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 486 | |
| | 487 | // Retore the previous revision |
| | 488 | $revisions = wp_get_post_revisions( $post_id ); |
| | 489 | |
| | 490 | // Go back two to load the previous revision |
| | 491 | array_shift( $revisions ); |
| | 492 | $last_revision = array_shift( $revisions ); |
| | 493 | wp_restore_post_revision( $last_revision->ID ); |
| | 494 | |
| | 495 | // Verify that previous post meta is restored |
| | 496 | $this->assertEquals( 'update2', get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 497 | |
| | 498 | // Try storing a blank meta |
| | 499 | update_post_meta( $post_id, 'meta_revision_test', '' ); |
| | 500 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 501 | |
| | 502 | update_post_meta( $post_id, 'meta_revision_test', 'update 4' ); |
| | 503 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 504 | |
| | 505 | // Retore the previous revision |
| | 506 | $revisions = wp_get_post_revisions( $post_id ); |
| | 507 | array_shift( $revisions ); |
| | 508 | $last_revision = array_shift( $revisions ); |
| | 509 | wp_restore_post_revision( $last_revision->ID ); |
| | 510 | |
| | 511 | // Verify that previous post meta is restored |
| | 512 | $this->assertEquals( '', get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 513 | |
| | 514 | // Test not tracking a key |
| | 515 | remove_all_filters( 'wp_post_revision_meta_keys' ); |
| | 516 | |
| | 517 | // Should no longer be revisioned |
| | 518 | update_post_meta( $post_id, 'meta_revision_test', 'update 5' ); |
| | 519 | wp_update_post( array( 'ID' => $post_id, 'post_content' => 'changed content' ) ); |
| | 520 | update_post_meta( $post_id, 'meta_revision_test', 'update 6' ); |
| | 521 | wp_update_post( array( 'ID' => $post_id, 'post_content' => 'go updated content' ) ); |
| | 522 | // Retore the previous revision |
| | 523 | $revisions = wp_get_post_revisions( $post_id ); |
| | 524 | array_shift( $revisions ); |
| | 525 | $last_revision = array_shift( $revisions ); |
| | 526 | wp_restore_post_revision( $last_revision->ID ); |
| | 527 | |
| | 528 | // Verify that previous post meta is NOT restored |
| | 529 | $this->assertEquals( 'update 6', get_post_meta( $post_id, 'meta_revision_test', true ) ); |
| | 530 | |
| | 531 | // Add the custom field to be revised via the wp_post_revision_meta_keys filter |
| | 532 | add_filter( 'wp_post_revision_meta_keys', function( $keys ) { |
| | 533 | $keys[] = 'meta_revision_test'; |
| | 534 | return $keys; |
| | 535 | }); |
| | 536 | |
| | 537 | /** |
| | 538 | * Test the revisioning of multiple meta keys |
| | 539 | */ |
| | 540 | |
| | 541 | // Add three values for meta |
| | 542 | update_post_meta( $post_id, 'meta_revision_test', 'update 7' ); |
| | 543 | add_post_meta( $post_id, 'meta_revision_test', 'update 7 number 2' ); |
| | 544 | add_post_meta( $post_id, 'meta_revision_test', 'update 7 number 3' ); |
| | 545 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 546 | |
| | 547 | // Update all three values |
| | 548 | update_post_meta( $post_id, 'meta_revision_test', 'update 8', 'update 7' ); |
| | 549 | update_post_meta( $post_id, 'meta_revision_test', 'update 8 number 2', 'update 7 number 2' ); |
| | 550 | update_post_meta( $post_id, 'meta_revision_test', 'update 8 number 3', 'update 7 number 3' ); |
| | 551 | wp_update_post( array( 'ID' => $post_id ) ); |
| | 552 | |
| | 553 | // Retore the previous revision |
| | 554 | $revisions = wp_get_post_revisions( $post_id ); |
| | 555 | array_shift( $revisions ); |
| | 556 | $last_revision = array_shift( $revisions ); |
| | 557 | wp_restore_post_revision( $last_revision->ID ); |
| | 558 | |
| | 559 | $this->assertEquals( array( 'update 7', 'update 7 number 2', 'update 7 number 3' ), get_post_meta( $post_id, 'meta_revision_test' ) ); |
| | 560 | |
| | 561 | // Cleanup! |
| | 562 | wp_delete_post( $original_post_id ); |
| | 563 | |
| | 564 | } |