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

Handle edit post links that have child elements; remove need for data-customize-post-id attribute #332

Merged
merged 2 commits into from Dec 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions js/customize-preview-posts.js
Expand Up @@ -144,7 +144,7 @@
// Prevent not-allowed cursor on edit-post-links.
api.isLinkPreviewable = ( function( originalIsLinkPreviewable ) {
return function( element, options ) {
if ( $( element ).hasClass( 'post-edit-link' ) ) {
if ( $( element ).closest( 'a' ).hasClass( 'post-edit-link' ) ) {
return true;
}
return originalIsLinkPreviewable.call( this, element, options );
Expand All @@ -156,7 +156,7 @@
if ( api.Preview.prototype.handleLinkClick ) {
api.Preview.prototype.handleLinkClick = ( function( originalHandleLinkClick ) {
return function( event ) {
if ( $( event.target ).hasClass( 'post-edit-link' ) ) {
if ( $( event.target ).closest( 'a' ).hasClass( 'post-edit-link' ) ) {
event.preventDefault();
} else {
originalHandleLinkClick.call( this, event );
Expand Down Expand Up @@ -192,8 +192,12 @@
* 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 ), postId;
postId = link.data( 'customize-post-id' );
var link = $( this ), postId, matches;
matches = link.prop( 'search' ).match( /post=(\d+)/ );
if ( ! matches ) {
return;
}
postId = parseInt( matches[1], 10 );
e.preventDefault();
if ( postId ) {
api.preview.send( 'edit-post', postId );
Expand Down
14 changes: 0 additions & 14 deletions php/class-wp-customize-posts-preview.php
Expand Up @@ -94,7 +94,6 @@ public function customize_preview_init() {
add_filter( 'the_posts', array( $this, 'filter_the_posts_to_tally_previewed_posts' ), 1000 );
add_filter( 'the_posts', array( $this, 'filter_the_posts_to_tally_orderby_keys' ), 10, 2 );
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 10 );
add_filter( 'edit_post_link', array( $this, 'filter_edit_post_link' ), 10, 2 );
add_filter( 'get_edit_post_link', array( $this, 'filter_get_edit_post_link' ), 10, 2 );
add_filter( 'get_avatar', array( $this, 'filter_get_avatar' ), 10, 6 );
add_filter( 'infinite_scroll_results', array( $this, 'amend_with_queried_post_ids' ) );
Expand Down Expand Up @@ -1239,19 +1238,6 @@ function filter_get_edit_post_link( $url, $post_id ) {
return $url;
}

/**
* Filter the post edit link so it can open the post in the Customizer.
*
* @param string $link Anchor tag for the edit link.
* @param int $post_id Post ID.
* @return string Edit link.
*/
function filter_edit_post_link( $link, $post_id ) {
$data_attributes = sprintf( ' data-customize-post-id="%d"', $post_id );
$link = preg_replace( '/(?<=<a\s)/', $data_attributes, $link );
return $link;
}

/**
* Filter the avatar to inject the args as context data.
*
Expand Down
13 changes: 0 additions & 13 deletions tests/php/test-class-wp-customize-posts-preview.php
Expand Up @@ -127,7 +127,6 @@ public function test_customize_preview_init() {
$this->assertEquals( 1000, has_filter( 'the_posts', array( $preview, 'filter_the_posts_to_tally_previewed_posts' ) ) );
$this->assertEquals( 10, has_filter( 'the_posts', array( $preview, 'filter_the_posts_to_tally_orderby_keys' ) ) );
$this->assertEquals( 10, has_action( 'wp_footer', array( $preview, 'export_preview_data' ) ) );
$this->assertEquals( 10, has_filter( 'edit_post_link', array( $preview, 'filter_edit_post_link' ) ) );
$this->assertEquals( 10, has_filter( 'get_edit_post_link', array( $preview, 'filter_get_edit_post_link' ) ) );
$this->assertEquals( 10, has_filter( 'get_avatar', array( $preview, 'filter_get_avatar' ) ) );
$this->assertEquals( 10, has_filter( 'infinite_scroll_results', array( $preview, 'amend_with_queried_post_ids' ) ) );
Expand Down Expand Up @@ -1155,18 +1154,6 @@ public function test_filter_get_edit_post_link() {
$this->assertEquals( $edit_post_link, $preview->filter_get_edit_post_link( $edit_post_link, $this->post_id ) );
}

/**
* Test filter_edit_post_link().
*
* @see WP_Customize_Posts_Preview::filter_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-id="%d"', $this->post_id );
$this->assertContains( $contained, $preview->filter_edit_post_link( $link, $this->post_id ) );
}

/**
* Test filter_get_avatar().
*
Expand Down