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

Show list of settings in snapshot in excerpt #46

Merged
merged 5 commits into from Jun 12, 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
18 changes: 10 additions & 8 deletions js/customize-snapshots.js
Expand Up @@ -136,10 +136,11 @@

if ( component.data.isPreview ) {
api.each( function( value, key ) {
allCustomized[ key ] = {
'value': value(),
'dirty': value._dirty
};
if ( value._dirty ) {
allCustomized[ key ] = {
'value': value()
};
}
} );
retval.snapshot_customized = JSON.stringify( allCustomized );
retval.snapshot_uuid = component.data.uuid;
Expand Down Expand Up @@ -248,10 +249,11 @@

customized = {};
api.each( function( value, key ) {
customized[ key ] = {
'value': value(),
'dirty': value._dirty
};
if ( value._dirty ) {
customized[ key ] = {
'value': value()
};
}
} );

request = wp.ajax.post( 'customize_update_snapshot', {
Expand Down
23 changes: 22 additions & 1 deletion php/class-customize-snapshot-manager.php
Expand Up @@ -284,6 +284,7 @@ public function create_post_type() {
add_action( 'add_meta_boxes_' . self::POST_TYPE, array( $this, 'remove_publish_metabox' ), 100 );
add_filter( 'wp_insert_post_data', array( $this, 'preserve_post_name_in_insert_data' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . self::POST_TYPE, array( $this, 'filter_bulk_actions' ) );
add_filter( 'get_the_excerpt', array( $this, 'filter_snapshot_excerpt' ), 10, 2 );
}

/**
Expand All @@ -297,6 +298,26 @@ public function filter_bulk_actions( $actions ) {
return $actions;
}

/**
* Include the setting IDs in the excerpt.
*
* @param string $excerpt The post excerpt.
* @param \WP_Post $post Post object.
* @return string Excerpt.
*/
public function filter_snapshot_excerpt( $excerpt, $post ) {
if ( self::POST_TYPE === $post->post_type ) {
$excerpt = '<ol>';
foreach ( static::get_post_content( $post ) as $setting_id => $setting_params ) {
if ( ! isset( $setting_params['dirty'] ) || true === $setting_params['dirty'] ) {
$excerpt .= sprintf( '<li><code>%s</code></li>', esc_attr( $setting_id ) );
}
}
$excerpt .= '</ol>';
}
return $excerpt;
}

/**
* Add Customize link to quick edit links.
*
Expand Down Expand Up @@ -546,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
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
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -6,12 +6,12 @@ Allow Customizer states to be drafted, and previewed with a private URL.

**Contributors:** [westonruter](https://profiles.wordpress.org/westonruter), [valendesigns](https://profiles.wordpress.org/valendesigns), [xwp](https://profiles.wordpress.org/xwp), [newscorpau](https://profiles.wordpress.org/newscorpau)
**Tags:** [customizer](https://wordpress.org/plugins/tags/customizer), [customize](https://wordpress.org/plugins/tags/customize), [snapshots](https://wordpress.org/plugins/tags/snapshots)
**Requires at least:** 4.3
**Requires at least:** 4.5
**Tested up to:** trunk
**Stable tag:** 0.4.0
**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)

[![Build Status](https://travis-ci.org/xwp/wp-customize-snapshots.svg?branch=master)](https://travis-ci.org/xwp/wp-customize-snapshots) [![Coverage Status](https://coveralls.io/repos/xwp/wp-customize-snapshots/badge.svg?branch=master)](https://coveralls.io/github/xwp/wp-customize-snapshots) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.svg)](http://gruntjs.com) [![devDependency Status](https://david-dm.org/xwp/wp-customize-snapshots/dev-status.svg)](https://david-dm.org/xwp/wp-customize-snapshots#info=devDependencies)
[![Build Status](https://travis-ci.org/xwp/wp-customize-snapshots.svg?branch=master)](https://travis-ci.org/xwp/wp-customize-snapshots) [![Coverage Status](https://coveralls.io/repos/xwp/wp-customize-snapshots/badge.svg?branch=master)](https://coveralls.io/github/xwp/wp-customize-snapshots) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com) [![devDependency Status](https://david-dm.org/xwp/wp-customize-snapshots/dev-status.svg)](https://david-dm.org/xwp/wp-customize-snapshots#info=devDependencies)

## Description ##

Expand Down
2 changes: 1 addition & 1 deletion readme.txt
@@ -1,6 +1,6 @@
=== Customize Snapshots ===
Contributors: westonruter, valendesigns, xwp, newscorpau
Requires at least: 4.3
Requires at least: 4.5
Tested up to: trunk
Stable tag: 0.4.0
License: GPLv2 or later
Expand Down
8 changes: 4 additions & 4 deletions tests/php/test-class-ajax-customize-snapshot-manager.php
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
56 changes: 39 additions & 17 deletions tests/php/test-class-customize-snapshot-manager.php
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 @@ -532,12 +531,35 @@ public function test_set_post_values() {
*/
public function test_preview() {
wp_set_current_user( $this->user_id );
$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;
$this->manager->preview();
$this->assertTrue( $foo->dirty );
}

/**
* @see Customize_Snapshot_Manager::filter_snapshot_excerpt()
*/
public function test_excerpt() {
global $post;
wp_set_current_user( $this->user_id );
$this->manager->customize_manager = $this->wp_customize;
$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' );
$this->manager->snapshot()->set( $bar, 'bar_custom' );
$this->manager->snapshot()->save();

$post = $this->manager->snapshot()->post();
$excerpt = get_the_excerpt( $post );
$this->assertContains( '<ol>', $excerpt );
$this->assertContains( '<code>foo</code>', $excerpt );
$this->assertContains( '<code>bar</code>', $excerpt );
}
}