-
-
Notifications
You must be signed in to change notification settings - Fork 4
Migrate jQuery code to native JavaScript #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b4a0a1c
Migrate jQuery code to native JavaScript
2ndkauboy 670d3d4
Prevent fatal error if `post__not_in` is not an array
2ndkauboy 42feba3
Fix some PHPCS issues
2ndkauboy 8fd9d1e
Fix some linting issues
2ndkauboy 3a9b273
Remove jQuery from Block Editor script
2ndkauboy 3099574
Format JS code
2ndkauboy c9647c4
Mark compatible with latest WordPress version
2ndkauboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,55 +4,47 @@ | |
| * @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 () { | ||
| const title = this.value; | ||
|
|
||
| // Show no warning on empty titles. | ||
| if (title === '') { | ||
| return; | ||
| } | ||
|
|
||
| const postId = document.querySelector( '#post_ID' )?.value; | ||
| const postType = document.querySelector( '#post_type' )?.value; | ||
|
|
||
| // Build the request payload | ||
| const requestData = new URLSearchParams( { | ||
| action: 'unique_title_check', | ||
| ajax_nonce: unique_title_checker.nonce, | ||
| post_id: postId || '', | ||
| post_type: postType || '', | ||
| post_title: title | ||
| } ); | ||
|
|
||
| // Perform the AJAX request | ||
| fetch( ajaxurl + '?' + requestData.toString() ) | ||
| .then( ( response ) => response.json() ) | ||
| .then( ( data ) => { | ||
| const 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) { | ||
| document.getElementById( 'post' ).insertAdjacentHTML( | ||
| 'beforebegin', | ||
| `<div id="unique-title-message" class="${ data.status }"> | ||
| <p>${ data.message }</p> | ||
| </div>` | ||
|
Comment on lines
+40
to
+42
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
| } ); | ||
| } ); | ||
| } ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.