From 8c5953a49b1d509443d2a4dcb5f0b70403c68bb8 Mon Sep 17 00:00:00 2001 From: lgedeon Date: Fri, 19 Aug 2016 21:05:43 -0400 Subject: [PATCH 1/5] Pass (snapshot object) to customize_snapshot_save filter. --- php/class-customize-snapshot.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/php/class-customize-snapshot.php b/php/class-customize-snapshot.php index 5596af0e..b8e6387d 100644 --- a/php/class-customize-snapshot.php +++ b/php/class-customize-snapshot.php @@ -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, From b82823bce00dc2039794bc60d946bd1deda260cd Mon Sep 17 00:00:00 2001 From: lgedeon Date: Sun, 21 Aug 2016 00:13:33 -0400 Subject: [PATCH 2/5] Add test for customize-snapshot-save filter. --- tests/php/test-class-customize-snapshot.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/php/test-class-customize-snapshot.php b/tests/php/test-class-customize-snapshot.php index a5e5ea87..cac05fb7 100644 --- a/tests/php/test-class-customize-snapshot.php +++ b/tests/php/test-class-customize-snapshot.php @@ -323,4 +323,25 @@ function test_saved() { 'data' => array( 'foo' => array( 'value' => 'bar' ) ), ) ); } + + /** + * 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->init(); + + $snapshot = new Customize_Snapshot( $manager, self::UUID ); + + add_filter ( 'customize_snapshot_save', function( $data, $test_snapshot ) use ( $snapshot ) { + $this->assertEquals( $test_snapshot, $snapshot ); + }, 10, 2 ); + + $snapshot->save( array( + 'uuid' => self::UUID, + 'data' => array( 'foo' => array( 'value' => 'bar' ) ), + ) ); + } } From d22d95c92e8d7768c7d01e9601c1b783cf2c3af6 Mon Sep 17 00:00:00 2001 From: lgedeon Date: Sun, 21 Aug 2016 00:22:11 -0400 Subject: [PATCH 3/5] Remove a misplaced space. --- tests/php/test-class-customize-snapshot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/test-class-customize-snapshot.php b/tests/php/test-class-customize-snapshot.php index cac05fb7..60ae08fd 100644 --- a/tests/php/test-class-customize-snapshot.php +++ b/tests/php/test-class-customize-snapshot.php @@ -335,7 +335,7 @@ function test_filter_customize_snapshot_save() { $snapshot = new Customize_Snapshot( $manager, self::UUID ); - add_filter ( 'customize_snapshot_save', function( $data, $test_snapshot ) use ( $snapshot ) { + add_filter( 'customize_snapshot_save', function( $data, $test_snapshot ) use ( $snapshot ) { $this->assertEquals( $test_snapshot, $snapshot ); }, 10, 2 ); From 71100365ceba1f05b12f90ed3e6266618cf7e8d2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 21 Aug 2016 20:31:49 -0700 Subject: [PATCH 4/5] Fix PHP 5.3 compat and ensure that filter actually runs --- tests/php/test-class-customize-snapshot.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/php/test-class-customize-snapshot.php b/tests/php/test-class-customize-snapshot.php index 60ae08fd..6859aa80 100644 --- a/tests/php/test-class-customize-snapshot.php +++ b/tests/php/test-class-customize-snapshot.php @@ -114,6 +114,7 @@ function tearDown() { $this->wp_customize = null; unset( $GLOBALS['wp_customize'] ); unset( $GLOBALS['wp_scripts'] ); + $this->filtered_snapshot = null; parent::tearDown(); } @@ -324,6 +325,13 @@ function test_saved() { ) ); } + /** + * Snapshot object passed in customize_snapshot_save filter. + * + * @var Customize_Snapshot + */ + protected $filtered_snapshot; + /** * Test that the snapshot object is passed as the second filter param. * @@ -331,17 +339,22 @@ function test_saved() { */ 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 ); - add_filter( 'customize_snapshot_save', function( $data, $test_snapshot ) use ( $snapshot ) { - $this->assertEquals( $test_snapshot, $snapshot ); + $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 ); } } From 17b82ddf579e6edaa2e27bf3417251991b8264b2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 21 Aug 2016 20:37:35 -0700 Subject: [PATCH 5/5] Fix error in object variable visibility related to 5.3 workaround --- tests/php/test-class-customize-snapshot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/test-class-customize-snapshot.php b/tests/php/test-class-customize-snapshot.php index 6859aa80..652d08dd 100644 --- a/tests/php/test-class-customize-snapshot.php +++ b/tests/php/test-class-customize-snapshot.php @@ -330,7 +330,7 @@ function test_saved() { * * @var Customize_Snapshot */ - protected $filtered_snapshot; + public $filtered_snapshot; /** * Test that the snapshot object is passed as the second filter param.