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

Commit

Permalink
Remove dirty from codebase and deprecate when used in set method
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 11, 2016
1 parent 182c8c8 commit 31602e0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 55 deletions.
2 changes: 1 addition & 1 deletion php/class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function save( $status = 'draft' ) {
foreach ( $this->customize_manager->settings() as $setting ) {
if ( $this->can_preview( $setting, $this->unsanitized_snapshot_post_data ) ) {
$post_data = $this->unsanitized_snapshot_post_data[ $setting->id ];
$this->snapshot->set( $setting, $post_data['value'], $post_data['dirty'] );
$this->snapshot->set( $setting, $post_data['value'] );
}
}

Expand Down
26 changes: 16 additions & 10 deletions php/class-customize-snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,24 @@ public function status() {
/**
* Store a setting's value in the snapshot's data.
*
* @param \WP_Customize_Setting $setting Setting.
* @param mixed $value Must be JSON-serializable.
* @param bool $dirty Whether the setting is dirty or not.
* @since 0.4.0 Removed support for `$dirty` argument.
*
* @param \WP_Customize_Setting $setting Setting.
* @param mixed $value Must be JSON-serializable.
* @param bool $deprecated Whether the setting is dirty or not.
*/
public function set( \WP_Customize_Setting $setting, $value, $dirty ) {
if ( $dirty ) {
$this->data[ $setting->id ] = array(
'value' => $value,
'dirty' => $dirty,
'sanitized' => false,
);
public function set( \WP_Customize_Setting $setting, $value, $deprecated = null ) {
if ( ! is_null( $deprecated ) ) {
_doing_it_wrong( __METHOD__, 'The $dirty argument has been removed.', '0.4.0' );
if ( false === $deprecated ) {
return;
}
}

$this->data[ $setting->id ] = array(
'value' => $value,
'sanitized' => false,
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/php/test-class-ajax-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function test_ajax_update_snapshot_preview_check() {
'action' => Customize_Snapshot_Manager::AJAX_ACTION,
'nonce' => wp_create_nonce( Customize_Snapshot_Manager::AJAX_ACTION ),
'customize_snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"header_background_color":{"value":"#ffffff","dirty":false}}',
'snapshot_customized' => '{"header_background_color":{"value":"#ffffff"}}',
);

$this->manager->capture_unsanitized_snapshot_post_data();
Expand All @@ -260,7 +260,7 @@ function test_ajax_update_snapshot_success() {
'action' => Customize_Snapshot_Manager::AJAX_ACTION,
'nonce' => wp_create_nonce( Customize_Snapshot_Manager::AJAX_ACTION ),
'customize_snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_default","dirty":true},"bar":{"value":"bar_default","dirty":true}}',
'snapshot_customized' => '{"foo":{"value":"foo_default"},"bar":{"value":"bar_default"}}',
'preview' => 'off',
);

Expand Down Expand Up @@ -289,7 +289,7 @@ function test_ajax_update_snapshot_success_preview() {
'action' => Customize_Snapshot_Manager::AJAX_ACTION,
'nonce' => wp_create_nonce( Customize_Snapshot_Manager::AJAX_ACTION ),
'customize_snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_default","dirty":false},"bar":{"value":"bar_default","dirty":false}}',
'snapshot_customized' => '{"foo":{"value":"foo_default"},"bar":{"value":"bar_default"}}',
'preview' => 'on',
);

Expand All @@ -303,7 +303,7 @@ function test_ajax_update_snapshot_success_preview() {
// Get the results.
$response = json_decode( $this->_last_response, true );
$this->assertSame( self::UUID, $response['data']['customize_snapshot_uuid'] );
$this->assertEmpty( $response['data']['customize_snapshot_settings'] );
$this->assertNotEmpty( $response['data']['customize_snapshot_settings'] );
}

/**
Expand Down
37 changes: 18 additions & 19 deletions tests/php/test-class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ function test_filter_post_row_actions_draft() {
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_default","dirty":true},"bar":{"value":"bar_default","dirty":true}}',
'snapshot_customized' => '{"foo":{"value":"foo_default"},"bar":{"value":"bar_default"}}',
);

$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->capture_unsanitized_snapshot_post_data();
$foo = $manager->customize_manager->get_setting( 'foo' );
$manager->snapshot()->set( $foo, 'foo_custom', true );
$manager->snapshot()->set( $foo, 'foo_custom' );
$manager->snapshot()->save();
$actions = array(
'inline hide-if-no-js' => true,
Expand All @@ -230,7 +230,7 @@ function test_filter_post_row_actions_publish() {
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_default","dirty":true},"bar":{"value":"bar_default","dirty":true}}',
'snapshot_customized' => '{"foo":{"value":"foo_default"},"bar":{"value":"bar_default"}}',
);
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->set_snapshot_uuid();
Expand Down Expand Up @@ -264,7 +264,7 @@ function test_setup_metaboxes() {
function test_render_data_metabox() {
wp_set_current_user( $this->user_id );
$this->do_customize_boot_actions( true );
$snapshot_json = '{"foo":{"value":"foo_value","dirty":true,"sanitized":false}}';
$snapshot_json = '{"foo":{"value":"foo_value","sanitized":false}}';
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
Expand All @@ -273,7 +273,7 @@ function test_render_data_metabox() {
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->capture_unsanitized_snapshot_post_data();
$foo = $manager->customize_manager->get_setting( 'foo' );
$manager->snapshot()->set( $foo, 'foo_value', true );
$manager->snapshot()->set( $foo, 'foo_value' );
$manager->snapshot()->save();
$post = $manager->snapshot()->post();
ob_start();
Expand All @@ -290,7 +290,7 @@ function test_render_data_metabox() {
function test_get_post_content() {
wp_set_current_user( $this->user_id );
$this->do_customize_boot_actions( true );
$snapshot_json = '{"foo":{"value":"foo_value","dirty":true,"sanitized":false}}';
$snapshot_json = '{"foo":{"value":"foo_value","sanitized":false}}';
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
Expand All @@ -299,14 +299,14 @@ function test_get_post_content() {
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->capture_unsanitized_snapshot_post_data();
$foo = $manager->customize_manager->get_setting( 'foo' );
$manager->snapshot()->set( $foo, 'foo_value', true );
$manager->snapshot()->set( $foo, 'foo_value' );
$manager->snapshot()->save();
$post = $manager->snapshot()->post();
$snapshot_content = Customize_Snapshot_Manager::get_post_content( $post );
$this->assertEquals( json_decode( $snapshot_json, true ), $snapshot_content );

// Get the revision post.
$manager->snapshot()->set( $foo, 'foo_revision_value', true );
$manager->snapshot()->set( $foo, 'foo_revision_value' );
$manager->snapshot()->save();
$revisions = wp_get_post_revisions( $post->ID );
$revision = array_shift( $revisions );
Expand Down Expand Up @@ -334,11 +334,10 @@ function test_encode_json() {
$array = array(
'foo' => array(
'value' => 'foo_value',
'dirty' => true,
'sanitized' => false,
),
);
$json = '{"foo":{"value":"foo_value","dirty":true,"sanitized":false}}';
$json = '{"foo":{"value":"foo_value","sanitized":false}}';
$this->assertEquals( $json, preg_replace( '/\s+/', '', Customize_Snapshot_Manager::encode_json( $array ) ) );
}

Expand All @@ -353,7 +352,7 @@ function test_enqueue_scripts() {
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_default","dirty":false},"bar":{"value":"bar_default","dirty":false}}',
'snapshot_customized' => '{"foo":{"value":"foo_default"},"bar":{"value":"bar_default"}}',
);
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->set_snapshot_uuid();
Expand Down Expand Up @@ -387,7 +386,7 @@ function test_save_error() {
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"baz":{"value":"","dirty":true}}',
'snapshot_customized' => '{"baz":{"value":""}}',
);
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->save();
Expand All @@ -403,7 +402,7 @@ function test_save_snapshot() {
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_default","dirty":false},"bar":{"value":"bar_default","dirty":false}}',
'snapshot_customized' => '{"foo":{"value":"foo_default"},"bar":{"value":"bar_default"}}',
);
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->set_snapshot_uuid();
Expand Down Expand Up @@ -465,7 +464,7 @@ public function test_can_preview() {
$_POST = wp_slash( array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"foo":{"value":"foo_custom","dirty":true},"bar":{"value":"bar_default","dirty":false}}',
'snapshot_customized' => '{"foo":{"value":"foo_custom"},"bar":{"value":"bar_default"}}',
) );
$this->do_customize_boot_actions( true );
$foo = $this->wp_customize->get_setting( 'foo' );
Expand Down Expand Up @@ -503,7 +502,7 @@ public function test_can_preview_array_key_exists() {
$_POST = array(
'nonce' => wp_create_nonce( 'save-customize_' . $this->wp_customize->get_stylesheet() ),
'snapshot_uuid' => self::UUID,
'snapshot_customized' => '{"bar":{"value":"bar_default","dirty":false}}',
'snapshot_customized' => '{"bar":{"value":"bar_default"}}',
);
$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->save_snapshot();
Expand All @@ -520,7 +519,7 @@ public function test_can_preview_array_key_exists() {
public function test_set_post_values() {
wp_set_current_user( $this->user_id );
$foo = $this->manager->customize_manager->get_setting( 'foo' );
$this->manager->snapshot()->set( $foo, 'foo_custom', true );
$this->manager->snapshot()->set( $foo, 'foo_custom' );
$this->manager->snapshot()->save();
$this->manager->snapshot()->is_preview = true;
$this->manager->set_post_values();
Expand All @@ -535,7 +534,7 @@ public function test_preview() {
$this->manager->customize_manager = $this->wp_customize;
$this->manager->snapshot = new Customize_Snapshot( $this->manager, null );
$foo = $this->manager->customize_manager->get_setting( 'foo' );
$this->manager->snapshot()->set( $foo, 'foo_custom', true );
$this->manager->snapshot()->set( $foo, 'foo_custom' );
$this->assertFalse( $foo->dirty );
$this->manager->snapshot()->save();
$this->manager->snapshot()->is_preview = true;
Expand All @@ -553,8 +552,8 @@ public function test_excerpt() {
$this->manager->snapshot = new Customize_Snapshot( $this->manager, null );
$foo = $this->manager->customize_manager->get_setting( 'foo' );
$bar = $this->manager->customize_manager->get_setting( 'bar' );
$this->manager->snapshot()->set( $foo, 'foo_custom', true );
$this->manager->snapshot()->set( $bar, 'bar_custom', true );
$this->manager->snapshot()->set( $foo, 'foo_custom' );
$this->manager->snapshot()->set( $bar, 'bar_custom' );
$this->manager->snapshot()->save();

$post = $this->manager->snapshot()->post();
Expand Down
33 changes: 12 additions & 21 deletions tests/php/test-class-customize-snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,9 @@ function test_values() {
$_POST['customized'] = 'on';
$this->wp_customize->setup_theme();

// Has no no dirty values.
$snapshot = new Customize_Snapshot( $this->snapshot_manager, null );
$snapshot->set( $this->foo, 'foo_default', false );
$snapshot->set( $this->bar, 'bar_default', false );
$this->assertEmpty( $snapshot->values() );
$snapshot->save();
$uuid = $snapshot->uuid();

// Has dirty values.
$snapshot = new Customize_Snapshot( $this->snapshot_manager, $uuid );
$snapshot->set( $this->bar, 'bar_custom', true );
$snapshot = new Customize_Snapshot( $this->snapshot_manager, null );
$snapshot->set( $this->bar, 'bar_custom' );
$this->assertNotEmpty( $snapshot->values() );
}

Expand All @@ -210,13 +202,12 @@ function test_data() {
$this->wp_customize->setup_theme();

$snapshot = new Customize_Snapshot( $this->snapshot_manager, null );
$snapshot->set( $this->foo, 'foo_default', false );
$this->assertEmpty( $snapshot->data() );
$snapshot->set( $this->foo, 'foo_custom', true );
$snapshot->set( $this->foo, 'foo_default' );
$this->assertNotEmpty( $snapshot->data() );
$snapshot->set( $this->foo, 'foo_custom' );
$expected = array(
'foo' => array(
'value' => 'foo_custom',
'dirty' => true,
'sanitized' => false,
),
);
Expand All @@ -229,7 +220,7 @@ function test_data() {
function test_settings() {
$snapshot = new Customize_Snapshot( $this->snapshot_manager, null );
$this->assertEmpty( $snapshot->settings() );
$snapshot->set( $this->foo, 'foo_default', true );
$snapshot->set( $this->foo, 'foo_default' );
$this->assertNotEmpty( $snapshot->settings() );
}

Expand All @@ -242,12 +233,12 @@ function test_set_and_get() {

$this->wp_customize->add_setting( 'biz' );
$this->assertEmpty( $snapshot->get( $this->wp_customize->get_setting( 'biz' ) ) );
$snapshot->set( $this->foo, 'foo_default', false );
$snapshot->set( $this->foo, 'foo_default' );
$this->assertNotEmpty( $snapshot->get( $this->foo ) );
$this->assertNotEmpty( $snapshot->get( 'foo' ) );
$this->assertEquals( 'bar_default', $snapshot->get( 'bar' ) );
$this->assertEquals( 'default', $snapshot->get( 'bar', 'default' ) );
$this->assertNull( $snapshot->get( 'baz' ) );
$this->assertNull( $snapshot->get( 'baz' ) );
}

/**
Expand All @@ -256,8 +247,8 @@ function test_set_and_get() {
function test_save() {
$snapshot = new Customize_Snapshot( $this->snapshot_manager, null );

$snapshot->set( $this->foo, 'foo_default', true );
$snapshot->set( $this->bar, 'bar_default', true );
$snapshot->set( $this->foo, 'foo_default' );
$snapshot->set( $this->bar, 'bar_default' );

$this->assertFalse( $snapshot->saved() );
$snapshot->save();
Expand All @@ -270,7 +261,7 @@ function test_save() {

// Update the Snapshot content
$snapshot = new Customize_Snapshot( $this->snapshot_manager, $snapshot->uuid() );
$snapshot->set( $this->bar, 'bar_custom', true );
$snapshot->set( $this->bar, 'bar_custom' );

$snapshot->save( 'publish' );
$decoded = json_decode( $snapshot->post()->post_content, true );
Expand Down Expand Up @@ -298,7 +289,7 @@ function test_save_pending() {
wp_set_current_user( $contributor_id );
$this->snapshot_manager->customize_manager = $wp_customize;
$snapshot = new Customize_Snapshot( $this->snapshot_manager, null );
$snapshot->set( $this->foo, 'foo', true );
$snapshot->set( $this->foo, 'foo' );
$uuid = $snapshot->uuid();
$this->assertNotInstanceOf( 'WP_Error', $snapshot->save( 'pending' ) );
$post = get_post( $snapshot->post() );
Expand Down

0 comments on commit 31602e0

Please sign in to comment.