Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Pass (snapshot object) to customize_snapshot_save filter. #77

Merged
merged 5 commits into from Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions php/class-customize-snapshot.php
Expand Up @@ -294,10 +294,11 @@ public function save( array $args ) {
/**
* Filter the snapshot's data before it's saved to 'post_content'.
*
* @param array $data Customizer snapshot data, with setting IDs mapped to an array
* containing a `value` array item and potentially other metadata.
* @param array $data Customizer snapshot data, with setting IDs mapped to an array
* containing a `value` array item and potentially other metadata.
* @param Customize_Snapshot $this Snapshot object.
*/
$this->data = apply_filters( 'customize_snapshot_save', $this->data );
$this->data = apply_filters( 'customize_snapshot_save', $this->data, $this );

$result = $this->snapshot_manager->post_type->save( array_merge(
$args,
Expand Down
34 changes: 34 additions & 0 deletions tests/php/test-class-customize-snapshot.php
Expand Up @@ -114,6 +114,7 @@ function tearDown() {
$this->wp_customize = null;
unset( $GLOBALS['wp_customize'] );
unset( $GLOBALS['wp_scripts'] );
$this->filtered_snapshot = null;
parent::tearDown();
}

Expand Down Expand Up @@ -323,4 +324,37 @@ function test_saved() {
'data' => array( 'foo' => array( 'value' => 'bar' ) ),
) );
}

/**
* Snapshot object passed in customize_snapshot_save filter.
*
* @var Customize_Snapshot
*/
public $filtered_snapshot;

/**
* Test that the snapshot object is passed as the second filter param.
*
* @see Customize_Snapshot::save()
*/
function test_filter_customize_snapshot_save() {
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->ensure_customize_manager();
$manager->init();

$snapshot = new Customize_Snapshot( $manager, self::UUID );

$that = $this; // For PHP 5.3.
add_filter( 'customize_snapshot_save', function( $data, $test_snapshot ) use ( $that ) {
$that->filtered_snapshot = $test_snapshot;
return $data;
}, 10, 2 );

$snapshot->save( array(
'uuid' => self::UUID,
'data' => array( 'foo' => array( 'value' => 'bar' ) ),
) );

$this->assertEquals( $snapshot, $this->filtered_snapshot );
}
}