Skip to content

Commit

Permalink
Merge pull request #14 from threadi/feature/updateReadme0324
Browse files Browse the repository at this point in the history
updated readme & optimized some things
  • Loading branch information
threadi authored Mar 16, 2024
2 parents dda2f9a + 52b9e3f commit 29b66e3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
29 changes: 22 additions & 7 deletions admin/js.js
Original file line number Diff line number Diff line change
@@ -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"),
Expand All @@ -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');
}
Expand Down
34 changes: 17 additions & 17 deletions auto-category-for-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'] = '<a href="#"' . $value . ' data-termid="' . $tag->term_id . '" data-nonce="' . wp_create_nonce( 'auto_category_change_state' ) . '">' . $text . '</a>';
}
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'] = '<a href="#"' . $value . ' data-termid="' . $tag->term_id . '" data-nonce="' . wp_create_nonce('auto_category_change_state') . '">' . esc_html( $text ) . '</a>';
}
return $actions;
}
add_filter( 'tag_row_actions', 'auto_category_add_term_action', 10, 2 );
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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' );
3 changes: 1 addition & 2 deletions build/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
</target>

<!-- create release-zip -->
<target name="build" description="builds a new release" depends="json-translations, setVersionNumber">
<target name="build" description="builds a new release" depends="setVersionNumber">
<delete file="${build.dir}/${plugin.slug}_${plugin.version}.zip"/>
<zip destfile="${build.dir}/${plugin.slug}_${plugin.version}.zip">
<zipfileset dir="${src.dir}/admin" prefix="${plugin.slug}/admin" />
<zipfileset dir="${src.dir}/languages" includes="*.json" prefix="${plugin.slug}/languages" />
<zipfileset dir="${src.dir}" includes="uninstall.php,readme.txt" prefix="${plugin.slug}" />
<zipfileset dir="${src.dir}" includes="${plugin.slug}-release.php" fullpath="${plugin.slug}/${plugin.slug}.php" />
</zip>
Expand Down
11 changes: 8 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==

Expand Down Expand Up @@ -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

0 comments on commit 29b66e3

Please sign in to comment.