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

Fix edit_post_link() #201

Merged
merged 2 commits into from
Jul 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 1 addition & 7 deletions php/class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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