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

Commit

Permalink
Merge pull request #201 from xwp/bugfix/edit-post-link
Browse files Browse the repository at this point in the history
Fix edit_post_link()
  • Loading branch information
westonruter committed Jul 24, 2016
2 parents 6710463 + a3ecf25 commit ffd51ae
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
15 changes: 9 additions & 6 deletions js/customize-posts.js
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
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
8 changes: 1 addition & 7 deletions php/class-wp-customize-posts-preview.php
Expand Up @@ -620,10 +620,6 @@ function filter_get_edit_post_link( $url, $post_id ) {
if ( ! $this->component->current_user_can_edit_post( $edit_post ) ) {
return null;
}
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $edit_post );
if ( ! $this->component->manager->get_setting( $setting_id ) ) {
return null;
}
return $url;
}

Expand All @@ -635,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
6 changes: 1 addition & 5 deletions tests/php/test-class-wp-customize-posts-preview.php
Expand Up @@ -682,10 +682,6 @@ public function test_filter_get_edit_post_link() {
$this->assertNull( $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );

wp_set_current_user( $this->user_id );
$this->assertNull( $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );

$setting_id = WP_Customize_Post_Setting::get_post_setting_id( get_post( $this->post_id ) );
$preview->component->manager->add_setting( new WP_Customize_Post_Setting( $preview->component->manager, $setting_id ) );
$this->assertEquals( $edit_post_link, $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );
}

Expand All @@ -697,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 ffd51ae

Please sign in to comment.