Skip to content

Commit 44c9900

Browse files
authored
Merge pull request #1015 from Codeinwp/fix/default-category
fix: handle default category gracefully
2 parents cbd9e0a + 5b94dcb commit 44c9900

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,8 +1907,6 @@ function ( $attr, $key ) {
19071907
}
19081908

19091909
if ( $import_post_term !== 'none' && strpos( $import_post_term, '_' ) > 0 ) {
1910-
// let's get the slug of the uncategorized category, even if it renamed.
1911-
$uncategorized = get_category( 1 );
19121910
$terms = explode( ',', $import_post_term );
19131911
$terms = array_filter(
19141912
$terms,
@@ -1922,31 +1920,35 @@ function( $term ) {
19221920
return $term;
19231921
}
19241922
);
1923+
19251924
$default_category = (int) get_option( 'default_category' );
1925+
$has_default = false;
1926+
19261927
foreach ( $terms as $term ) {
19271928
// 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".
19281929
$array = explode( '_', $term );
19291930
$term_id = array_pop( $array );
19301931
$taxonomy = implode( '_', $array );
19311932

1932-
// uncategorized
1933-
// 1. may be the unmodified category ID 1
1934-
// 2. may have been recreated ('uncategorized') and may have a different slug in different languages.
1935-
if ( $default_category === $uncategorized->term_id ) {
1936-
wp_remove_object_terms(
1937-
$new_post_id, apply_filters(
1938-
'feedzy_uncategorized', array(
1939-
1,
1940-
'uncategorized',
1941-
$uncategorized->slug,
1942-
), $job->ID
1943-
), 'category'
1944-
);
1933+
// If the term is not default, flag it.
1934+
if ( $default_category === (int) $term_id ) {
1935+
$has_default = true;
19451936
}
19461937

19471938
$result = wp_set_object_terms( $new_post_id, intval( $term_id ), $taxonomy, true );
19481939
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__ );
19491940
}
1941+
1942+
// If the default category is not used, remove it.
1943+
if ( ! $has_default ) {
1944+
wp_remove_object_terms(
1945+
$new_post_id, apply_filters(
1946+
'feedzy_uncategorized', array(
1947+
$default_category,
1948+
), $job->ID
1949+
), 'category'
1950+
);
1951+
}
19501952
}
19511953

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

0 commit comments

Comments
 (0)