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