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

Commit ffd51ae

Browse files
authored
Merge pull request #201 from xwp/bugfix/edit-post-link
Fix edit_post_link()
2 parents 6710463 + a3ecf25 commit ffd51ae

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

js/customize-posts.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,16 @@
445445
} );
446446

447447
/**
448-
* Focus on the section requested from the preview.
448+
* Ensure a post is added to the Customizer and focus on its section when an edit post link is clicked in preview.
449449
*/
450-
api.previewer.bind( 'focus-section', function( sectionId ) {
451-
var section = api.section( sectionId );
452-
if ( section ) {
453-
section.focus();
454-
}
450+
api.previewer.bind( 'edit-post', function( postId ) {
451+
var ensuredPromise = api.Posts.ensurePosts( [ postId ] );
452+
ensuredPromise.done( function( postsData ) {
453+
var postData = postsData[ postId ];
454+
if ( postData ) {
455+
postData.section.focus();
456+
}
457+
} );
455458
} );
456459

457460
/**

js/customize-preview-posts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@
197197
* Focus on the post section in the Customizer pane when clicking an edit-post-link.
198198
*/
199199
$( document.body ).on( 'click', '.post-edit-link', function( e ) {
200-
var link = $( this ), settingId;
201-
settingId = link.data( 'customize-post-setting-id' );
200+
var link = $( this ), postId;
201+
postId = link.data( 'customize-post-id' );
202202
e.preventDefault();
203-
if ( settingId ) {
204-
api.preview.send( 'focus-section', settingId );
203+
if ( postId ) {
204+
api.preview.send( 'edit-post', postId );
205205
}
206206
} );
207207
} );

php/class-wp-customize-posts-preview.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,6 @@ function filter_get_edit_post_link( $url, $post_id ) {
620620
if ( ! $this->component->current_user_can_edit_post( $edit_post ) ) {
621621
return null;
622622
}
623-
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $edit_post );
624-
if ( ! $this->component->manager->get_setting( $setting_id ) ) {
625-
return null;
626-
}
627623
return $url;
628624
}
629625

@@ -635,9 +631,7 @@ function filter_get_edit_post_link( $url, $post_id ) {
635631
* @return string Edit link.
636632
*/
637633
function filter_edit_post_link( $link, $post_id ) {
638-
$edit_post = get_post( $post_id );
639-
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $edit_post );
640-
$data_attributes = sprintf( ' data-customize-post-setting-id="%s"', $setting_id );
634+
$data_attributes = sprintf( ' data-customize-post-id="%d"', $post_id );
641635
$link = preg_replace( '/(?<=<a\s)/', $data_attributes, $link );
642636
return $link;
643637
}

tests/php/test-class-wp-customize-posts-preview.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,6 @@ public function test_filter_get_edit_post_link() {
682682
$this->assertNull( $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );
683683

684684
wp_set_current_user( $this->user_id );
685-
$this->assertNull( $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );
686-
687-
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( get_post( $this->post_id ) );
688-
$preview->component->manager->add_setting( new WP_Customize_Post_Setting( $preview->component->manager, $setting_id ) );
689685
$this->assertEquals( $edit_post_link, $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );
690686
}
691687

@@ -697,7 +693,7 @@ public function test_filter_get_edit_post_link() {
697693
public function test_filter_edit_post_link() {
698694
$preview = new WP_Customize_Posts_Preview( $this->posts_component );
699695
$link = '<a class="edit-me" href="' . esc_url( home_url( '?edit-me' ) ) . '">Edit</a>';
700-
$contained = sprintf( ' data-customize-post-setting-id="%s"', WP_Customize_Post_Setting::get_post_setting_id( get_post( $this->post_id ) ) );
696+
$contained = sprintf( ' data-customize-post-id="%d"', $this->post_id );
701697
$this->assertContains( $contained, $preview->filter_edit_post_link( $link, $this->post_id ) );
702698
}
703699

0 commit comments

Comments
 (0)