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

Commit 089cf81

Browse files
authored
Merge pull request #77 from xwp/feature/pass-snapshot
Pass snapshot object as 2nd context param in customize_snapshot_save filter
2 parents 856d135 + 17b82dd commit 089cf81

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

php/class-customize-snapshot.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,11 @@ public function save( array $args ) {
294294
/**
295295
* Filter the snapshot's data before it's saved to 'post_content'.
296296
*
297-
* @param array $data Customizer snapshot data, with setting IDs mapped to an array
298-
* containing a `value` array item and potentially other metadata.
297+
* @param array $data Customizer snapshot data, with setting IDs mapped to an array
298+
* containing a `value` array item and potentially other metadata.
299+
* @param Customize_Snapshot $this Snapshot object.
299300
*/
300-
$this->data = apply_filters( 'customize_snapshot_save', $this->data );
301+
$this->data = apply_filters( 'customize_snapshot_save', $this->data, $this );
301302

302303
$result = $this->snapshot_manager->post_type->save( array_merge(
303304
$args,

tests/php/test-class-customize-snapshot.php

+34
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ function tearDown() {
114114
$this->wp_customize = null;
115115
unset( $GLOBALS['wp_customize'] );
116116
unset( $GLOBALS['wp_scripts'] );
117+
$this->filtered_snapshot = null;
117118
parent::tearDown();
118119
}
119120

@@ -323,4 +324,37 @@ function test_saved() {
323324
'data' => array( 'foo' => array( 'value' => 'bar' ) ),
324325
) );
325326
}
327+
328+
/**
329+
* Snapshot object passed in customize_snapshot_save filter.
330+
*
331+
* @var Customize_Snapshot
332+
*/
333+
public $filtered_snapshot;
334+
335+
/**
336+
* Test that the snapshot object is passed as the second filter param.
337+
*
338+
* @see Customize_Snapshot::save()
339+
*/
340+
function test_filter_customize_snapshot_save() {
341+
$manager = new Customize_Snapshot_Manager( $this->plugin );
342+
$manager->ensure_customize_manager();
343+
$manager->init();
344+
345+
$snapshot = new Customize_Snapshot( $manager, self::UUID );
346+
347+
$that = $this; // For PHP 5.3.
348+
add_filter( 'customize_snapshot_save', function( $data, $test_snapshot ) use ( $that ) {
349+
$that->filtered_snapshot = $test_snapshot;
350+
return $data;
351+
}, 10, 2 );
352+
353+
$snapshot->save( array(
354+
'uuid' => self::UUID,
355+
'data' => array( 'foo' => array( 'value' => 'bar' ) ),
356+
) );
357+
358+
$this->assertEquals( $snapshot, $this->filtered_snapshot );
359+
}
326360
}

0 commit comments

Comments
 (0)