diff --git a/admin/js.js b/admin/js.js index dcd5d32..ddf5920 100644 --- a/admin/js.js +++ b/admin/js.js @@ -1,13 +1,28 @@ const { __ } = wp.i18n; jQuery(document).ready(function(){ - jQuery(".auto_category a").click(function(){ - change_publish_states(jQuery(this)); - }); jQuery(".auto_category a.default_category").parents("tr").addClass('default_category'); + jQuery( document ).on( "ajaxComplete", function( event, xhr, settings ) { + autocategory_set_event(); + }); + autocategory_set_event(); }); -function change_publish_states(el){ +/** + * Set event for table. + */ +function autocategory_set_event() { + jQuery(".auto_category a").click(function(){ + autocategory_change_publish_states(jQuery(this)); + }); +} + +/** + * Change the states. + * + * @param el + */ +function autocategory_change_publish_states(el){ jQuery.getJSON(ajaxurl, { term_id: el.data("termid"), @@ -17,12 +32,12 @@ function change_publish_states(el){ function(data) { if (data.error){ alert(data.error); - }else{ + } else { if( data.result ) { - var oldDefault = jQuery("#tag-" + data.old_default_category_id + " .auto_category > a.default_category"); + let oldDefault = jQuery("#tag-" + data.old_default_category_id + " .auto_category > a.default_category"); oldDefault.parents('tr').removeClass('default_category'); oldDefault.removeClass('default_category').text(__('Set as default', 'auto-category-for-posts')); - var newDefault = jQuery("#tag-" + data.new_default_category_id + " .auto_category > a"); + let newDefault = jQuery("#tag-" + data.new_default_category_id + " .auto_category > a"); newDefault.addClass('default_category').text(__('Default category', 'auto-category-for-posts')); newDefault.parents('tr').addClass('default_category'); } diff --git a/auto-category-for-posts.php b/auto-category-for-posts.php index cfbd490..299c9d5 100644 --- a/auto-category-for-posts.php +++ b/auto-category-for-posts.php @@ -51,7 +51,6 @@ function auto_category_update_check(): void { * @param WP_Post $post The Post-object. * @param bool $update Whether the post has been updated (true) or added/inserts (false). * @return void - * @noinspection PhpUnused */ function auto_category_save_post( int $post_id, WP_Post $post, bool $update ): void { // Only for new posts. @@ -83,21 +82,24 @@ function auto_category_save_post( int $post_id, WP_Post $post, bool $update ): v * @param array $actions List of actions. * @param WP_Term $tag The term. * @return array - * @noinspection PhpUnused */ function auto_category_add_term_action( array $actions, WP_Term $tag ): array { - if ( 'category' === $tag->taxonomy ) : - $tax = get_taxonomy( 'category' ); - if ( current_user_can( $tax->cap->manage_terms ) ) { - $text = __( 'Set as default', 'auto-category-for-posts' ); - $value = ''; - if ( absint( get_option( AUTOCATEGORY_OPTIONNAME ) ) === $tag->term_id ) { - $text = __( 'Default category', 'auto-category-for-posts' ); - $value = ' class="default_category"'; - } - $actions['auto_category'] = '' . $text . ''; - } - endif; + // bail if it is not the category taxonomy. + if ( 'category' !== $tag->taxonomy ) { + return $actions; + } + + // get taxonomy as object. + $tax = get_taxonomy('category'); + if (current_user_can($tax->cap->manage_terms)) { + $text = __('Set as default', 'auto-category-for-posts'); + $value = ''; + if (absint(get_option(AUTOCATEGORY_OPTIONNAME)) === $tag->term_id) { + $text = __('Default category', 'auto-category-for-posts'); + $value = ' class="default_category"'; + } + $actions['auto_category'] = '' . esc_html( $text ) . ''; + } return $actions; } add_filter( 'tag_row_actions', 'auto_category_add_term_action', 10, 2 ); @@ -144,7 +146,6 @@ function auto_category_load_ajax(): void { * Set new auto category by AJAX. * * @return void - * @noinspection PhpUnused */ function auto_category_ajax(): void { $result = array( @@ -188,7 +189,6 @@ function auto_category_ajax(): void { } } // return the result. - echo wp_json_encode( $result ); - exit; + wp_send_json( $result ); } add_action( 'wp_ajax_auto_category_change_state', 'auto_category_ajax' ); diff --git a/build/build.xml b/build/build.xml index af0cd46..9b6aff1 100644 --- a/build/build.xml +++ b/build/build.xml @@ -35,11 +35,10 @@ - + - diff --git a/readme.txt b/readme.txt index 7b428a6..2559676 100644 --- a/readme.txt +++ b/readme.txt @@ -2,15 +2,15 @@ Contributors: threadi Tags: category, auto category, posts Requires at least: 5.8 -Tested up to: 6.4 +Tested up to: 6.5 Requires PHP: 7.4 License: GPL-2.0-or-later License URI: https://www.gnu.org/licenses/gpl-2.0.html -Stable tag: 1.0.7 +Stable tag: 1.0.8 == Description == -This plugin adds the possibility to set a default category for posts within the category overview. The selected category will be set for each new post already when it is called for the first time and not after its first saving. +Define a default category for each new post. This will already be set during creation of the post. You must not choose the category yourself. Use your time for the content of your posts and not for their configuration. == Installation == @@ -47,3 +47,8 @@ This plugin adds the possibility to set a default category for posts within the * Updated compatibility-flag for WordPress 6.4 * Compatible with WordPress Coding Standards 3.0 * Removed language-files from plugin (except the json-files for Block Editor) + += 1.0.8 = +* Small optimizations +* Removed JSON-language files from plugin +* Updated compatibility-flag for WordPress 6.5