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

Commit

Permalink
Merge pull request #87 from xwp/feature/concurrency-hooks
Browse files Browse the repository at this point in the history
Add hooks to support customize-concurrency plugin.
  • Loading branch information
westonruter committed Aug 28, 2016
2 parents f9adc9d + cad91c8 commit b074773
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
5 changes: 3 additions & 2 deletions js/customize-snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@
}
} );

request.done( function() {
request.done( function( response ) {
var url = api.previewer.previewUrl(),
regex = new RegExp( '([?&])customize_snapshot_uuid=.*?(&|$)', 'i' ),
notFound = -1,
Expand Down Expand Up @@ -642,7 +642,8 @@
api.trigger( 'customize-snapshots-update', {
previewUrl: url,
customizeUrl: customizeUrl,
uuid: component.data.uuid
uuid: component.data.uuid,
response: response
} );
} );

Expand Down
10 changes: 10 additions & 0 deletions php/class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,14 @@ public function handle_update_snapshot_request() {
) );
}

/**
* Add any additional checks before saving snapshot.
*
* @param Customize_Snapshot $snapshot Snapshot to be saved.
* @param Customize_Snapshot_Manager $snapshot_manager Snapshot manager.
*/
do_action( 'customize_snapshot_save_before', $this->snapshot, $this );

// Set the snapshot UUID.
$post_type = get_post_type_object( Post_Type::SLUG );
$authorized = ( $post ?
Expand Down Expand Up @@ -1190,6 +1198,8 @@ function( $value ) {
wp_send_json_error( $data );
}

/** This filter is documented in wp-includes/class-wp-customize-manager.php */
$data = apply_filters( 'customize_save_response', $data, $this->customize_manager );
wp_send_json_success( $data );
}

Expand Down
70 changes: 70 additions & 0 deletions tests/php/test-class-ajax-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ class Test_Ajax_Customize_Snapshot_Manager extends \WP_Ajax_UnitTestCase {
*/
protected $manager;

/**
* Test snapshot for test_handle_update_snapshot_request_actions_and_filters().
*
* @var Customize_Snapshot
*/
public $actioned_snapshot;

/**
* Test snapshot manager for test_handle_update_snapshot_request_actions_and_filters().
*
* @var Customize_Snapshot_Manager
*/
public $actioned_snapshot_manager;

/**
* Test customize manager for test_handle_update_snapshot_request_actions_and_filters().
*
* @var \WP_Customize_Manager
*/
public $filtered_customizer;

/**
* Set up before class.
*/
Expand Down Expand Up @@ -532,4 +553,53 @@ function test_ajax_update_snapshot_ok_for_draft_and_pending_but_not_future() {
);
$this->assertSame( $expected_results, $response );
}

/**
* Test actions and filters to make sure they are passing correct params.
*
* @covers \CustomizeSnapshots\Customize_Snapshot_Manager::handle_update_snapshot_request()
*/
function test_handle_update_snapshot_request_actions_and_filters() {
unset( $GLOBALS['wp_customize'] );
remove_all_actions( 'wp_ajax_' . Customize_Snapshot_Manager::AJAX_ACTION );

add_filter( 'user_has_cap', function( $allcaps, $caps, $args ) {
$allcaps['customize'] = true;
if ( ! empty( $allcaps['edit_posts'] ) && ! empty( $args ) && 'customize' === $args[0] ) {
$allcaps = array_merge( $allcaps, array_fill_keys( $caps, true ) );
}
return $allcaps;
}, 10, 3 );
$this->set_current_user( 'contributor' );
$post_vars = array(
'action' => Customize_Snapshot_Manager::AJAX_ACTION,
'nonce' => wp_create_nonce( Customize_Snapshot_Manager::AJAX_ACTION ),
'customize_snapshot_uuid' => self::UUID,
);

$this->plugin = new Plugin();
$this->plugin->init();
$this->add_setting();

$that = $this; // For PHP 5.3.
add_action( 'customize_snapshot_save_before', function( $test_snapshot, $test_snapshot_manager ) use ( $that ) {
$that->actioned_snapshot = $test_snapshot;
$that->actioned_snapshot_manager = $test_snapshot_manager;
}, 10, 2 );
add_filter( 'customize_save_response', function( $data, $test_customizer ) use ( $that ) {
$that->filtered_customizer = $test_customizer;
return $data;
}, 10, 2 );

$this->set_input_vars( $post_vars );
$this->make_ajax_call( Customize_Snapshot_Manager::AJAX_ACTION );

$manager = new Customize_Snapshot_Manager( $this->plugin );
$manager->ensure_customize_manager();
$manager->init();

$this->assertEquals( $manager->snapshot(), $this->actioned_snapshot );
$this->assertEquals( $manager, $this->actioned_snapshot_manager );
$this->assertEquals( $manager->customize_manager, $this->filtered_customizer );
}
}

0 comments on commit b074773

Please sign in to comment.