Skip to content

Commit

Permalink
fix: handle default category gracefully
Browse files Browse the repository at this point in the history
If the user has defined any category to the import, we don't need to
  append the default category on the top of it.

Closes Codeinwp/feedzy-rss-feeds-pro#654
  • Loading branch information
HardeepAsrani committed Dec 7, 2024
1 parent bd3134b commit f6e7156
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1805,8 +1805,6 @@ function ( $attr, $key ) {
}

if ( $import_post_term !== 'none' && strpos( $import_post_term, '_' ) > 0 ) {
// let's get the slug of the uncategorized category, even if it renamed.
$uncategorized = get_category( 1 );
$terms = explode( ',', $import_post_term );
$terms = array_filter(
$terms,
Expand All @@ -1820,31 +1818,35 @@ function( $term ) {
return $term;
}
);

$default_category = (int) get_option( 'default_category' );
$has_default = false;

foreach ( $terms as $term ) {
// this handles both x_2, where 2 is the term id and x is the taxonomy AND x_2_3_4 where 4 is the term id and the taxonomy name is "x 2 3 4".
$array = explode( '_', $term );
$term_id = array_pop( $array );
$taxonomy = implode( '_', $array );

// uncategorized
// 1. may be the unmodified category ID 1
// 2. may have been recreated ('uncategorized') and may have a different slug in different languages.
if ( $default_category === $uncategorized->term_id ) {
wp_remove_object_terms(
$new_post_id, apply_filters(
'feedzy_uncategorized', array(
1,
'uncategorized',
$uncategorized->slug,
), $job->ID
), 'category'
);
// If the term is not default, flag it.
if ( $default_category === (int) $term_id ) {
$has_default = true;
}

$result = wp_set_object_terms( $new_post_id, intval( $term_id ), $taxonomy, true );
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'After creating post in %s/%d, result = %s', $taxonomy, $term_id, print_r( $result, true ) ), 'debug', __FILE__, __LINE__ );
}

// If the default category is not used, remove it.
if ( ! $has_default ) {
wp_remove_object_terms(
$new_post_id, apply_filters(
'feedzy_uncategorized', array(
$default_category,
), $job->ID
), 'category'
);
}
}

do_action( 'feedzy_import_extra', $job, $item_obj, $new_post_id, $import_errors, $import_info );
Expand Down

0 comments on commit f6e7156

Please sign in to comment.