Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 34 additions & 49 deletions js/unique-title-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,40 @@
* @package unique-title-checker
*/

jQuery( document ).ready(
function () {

jQuery( '#title' ).blur(
function () {

var title = jQuery( this ).val();

// Show no warning on empty titles.
if ( '' === title ) {
return;
}

var request_data = {
action : 'unique_title_check',
ajax_nonce : unique_title_checker.nonce,
post__not_in: [ jQuery( '#post_ID' ).val() ],
post_type : jQuery( '#post_type' ).val(),
post_title : title
};

jQuery.ajax(
{
url : ajaxurl,
data : request_data,
dataType: 'json'
}
).done(
function ( data ) {
jQuery( '#unique-title-message' ).remove();
document.addEventListener( "DOMContentLoaded", function () {
document.getElementById( "title" ).addEventListener( "blur", function () {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap these into multiple lines (PHPCS)

var title = this.value;

// Show no warning on empty titles.
if (title === "") {
return;
}

var requestData = new URLSearchParams( {
action: "unique_title_check",
ajax_nonce: unique_title_checker.nonce,
post_id: document.getElementById( "post_ID" ).value,
post_type: document.getElementById( "post_type" ).value,
post_title: title
} );

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap these in multiple lines as well.


fetch( ajaxurl + "?" + requestData.toString() )
.then( response => response.json() )
.then( data => {
var messageElement = document.getElementById( "unique-title-message" );
if (messageElement) {
messageElement.remove();
}

if ( 'error' === data.status || ! unique_title_checker.only_unique_error ) {
jQuery( '#post' ).before(
jQuery(
'<div>',
{
id: 'unique-title-message',
class: data.status
}
).append(
jQuery( '<p>' ).text( data.message )
)
);
}
}
if (data.status === "error" || !unique_title_checker.only_unique_error) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "globals" to get rid of the warnings.

document.getElementById( "post" ).insertAdjacentHTML(
"beforebegin",
`<div id="unique-title-message" class="${ data.status }">
<p>${ data.message }</p>
</div>`
Comment on lines +40 to +42

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe wrap in multiple lines.

);

}
);

}
);
} )
.catch( error => console.error( "Error fetching unique title check:", error ) );
} );
} );
8 changes: 7 additions & 1 deletion unique-title-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function unique_title_check() {
array(
'action',
'ajax_nonce',
'post__not_in',
'post_id',
'post_type',
'post_title',
)
Expand Down Expand Up @@ -241,6 +241,12 @@ public function check_uniqueness( $args ) {
// Use the posts_where hook to add thr filter for the post_title, as it is not available through WP_Query args.
add_filter( 'posts_where', array( $this, 'post_title_where' ), 10, 1 );

// Use the `post_id` parameter for an excluding post filter.
if ( isset( $args['post_id'] ) ) {
$args['post__not_in'][] = $args['post_id'];
unset( $args['post_id'] );
}

// Providing a filter to overwrite the search arguments.
$args = apply_filters( 'unique_title_checker_arguments', $args );

Expand Down