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

Commit

Permalink
Ensure edit_post_link works even when post is not queried
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jul 23, 2016
1 parent 3d03bc2 commit a3ecf25
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
15 changes: 9 additions & 6 deletions js/customize-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,16 @@
} );

/**
* Focus on the section requested from the preview.
* Ensure a post is added to the Customizer and focus on its section when an edit post link is clicked in preview.
*/
api.previewer.bind( 'focus-section', function( sectionId ) {
var section = api.section( sectionId );
if ( section ) {
section.focus();
}
api.previewer.bind( 'edit-post', function( postId ) {
var ensuredPromise = api.Posts.ensurePosts( [ postId ] );
ensuredPromise.done( function( postsData ) {
var postData = postsData[ postId ];
if ( postData ) {
postData.section.focus();
}
} );
} );

/**
Expand Down
8 changes: 4 additions & 4 deletions js/customize-preview-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@
* Focus on the post section in the Customizer pane when clicking an edit-post-link.
*/
$( document.body ).on( 'click', '.post-edit-link', function( e ) {
var link = $( this ), settingId;
settingId = link.data( 'customize-post-setting-id' );
var link = $( this ), postId;
postId = link.data( 'customize-post-id' );
e.preventDefault();
if ( settingId ) {
api.preview.send( 'focus-section', settingId );
if ( postId ) {
api.preview.send( 'edit-post', postId );
}
} );
} );
Expand Down
4 changes: 1 addition & 3 deletions php/class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,7 @@ function filter_get_edit_post_link( $url, $post_id ) {
* @return string Edit link.
*/
function filter_edit_post_link( $link, $post_id ) {
$edit_post = get_post( $post_id );
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $edit_post );
$data_attributes = sprintf( ' data-customize-post-setting-id="%s"', $setting_id );
$data_attributes = sprintf( ' data-customize-post-id="%d"', $post_id );
$link = preg_replace( '/(?<=<a\s)/', $data_attributes, $link );
return $link;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/test-class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public function test_filter_get_edit_post_link() {
public function test_filter_edit_post_link() {
$preview = new WP_Customize_Posts_Preview( $this->posts_component );
$link = '<a class="edit-me" href="' . esc_url( home_url( '?edit-me' ) ) . '">Edit</a>';
$contained = sprintf( ' data-customize-post-setting-id="%s"', WP_Customize_Post_Setting::get_post_setting_id( get_post( $this->post_id ) ) );
$contained = sprintf( ' data-customize-post-id="%d"', $this->post_id );
$this->assertContains( $contained, $preview->filter_edit_post_link( $link, $this->post_id ) );
}

Expand Down

0 comments on commit a3ecf25

Please sign in to comment.