分类权限插件功能更新源码#wordpress

  • 隐藏了列表和搜索列表的隐藏栏目

  • 隐藏了详情页前一后一文章的隐藏栏目

  • 分类列表的隐藏分类取消了隐藏,因为会导致详情页获取不到完整分类,从而详情页隐藏失效

  • 详情页对属于隐藏分类的文章做隐藏;若属于隐藏分类且设置密码方法,则显示密码

  • 发布更新文章时,属于隐藏栏目,且为公布状态,且无密码的文章设置为私密

    <div class="wrap">
        <h2><?php _e( &#039;Ultimate Category Excluder Options&#039;, &#039;ultimate-category-excluder&#039; ); ?></h2>
        <?php if ( isset( $message ) ) { echo $message; } ?>
        <p><?php _e( &#039;Use this page to select the categories you wish to exclude and where you would like to exclude them from.&#039;, &#039;ultimate-category-excluder&#039; ); ?></p>
        <form action="options-general.php?page=ultimate-category-excluder.php" method="post">
        <?php wp_nonce_field( &#039;uce_form&#039; ); ?>
        <table class="widefat">
        <thead>
            <tr>
                <th scope="col"><?php _e( &#039;Category&#039;, &#039;ultimate-category-excluder&#039; ); ?></th>
                <th scope="col"><?php _e( &#039;Exclude from Front Page?&#039;, &#039;ultimate-category-excluder&#039; ); ?></th>
                <th scope="col"><?php _e( &#039;Exclude from Feeds?&#039;, &#039;ultimate-category-excluder&#039; ); ?></th>
                <th scope="col"><?php _e( &#039;Exclude from All Archives?&#039;, &#039;ultimate-category-excluder&#039; ); ?></th>
                <th scope="col"><?php _e( &#039;Exclude from Search?&#039;, &#039;ultimate-category-excluder&#039; ); ?></th>
            </tr>
        </thead>
        <tbody id="the-list">
    <?php
    $args=array(
        &#039;hide_empty&#039; => 0,
        &#039;order&#039; => &#039;ASC&#039;
    );
    $cats = get_categories( $args );
    $alt = 0;
    foreach( $cats as $cat ) {
        ?>
        <tr<?php if ( $alt == 1 ) { echo &#039; class="alternate"&#039;; $alt = 0; } else { $alt = 1; } ?>>
            <th scope="row"><?php echo $cat->cat_name; //. &#039; (&#039; . $cat->cat_ID . &#039;)&#039;; ?></th>
            <td><input type="checkbox" name="exclude_main[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( &#039;-&#039; . $cat->cat_ID, $options[&#039;exclude_main&#039;] ) ) { echo &#039;checked="true" &#039;; } ?>/></td>
            <td><input type="checkbox" name="exclude_feed[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( &#039;-&#039; . $cat->cat_ID, $options[&#039;exclude_feed&#039;] ) ) { echo &#039;checked="true" &#039;; } ?>/></td>
            <td><input type="checkbox" name="exclude_archives[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( &#039;-&#039; . $cat->cat_ID, $options[&#039;exclude_archives&#039;] ) ) { echo &#039;checked="true" &#039;; } ?>/></td>
            <td><input type="checkbox" name="exclude_search[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( &#039;-&#039; . $cat->cat_ID, $options[&#039;exclude_search&#039;] ) ) { echo &#039;checked="true" &#039;; } ?>/></td>
        </tr>         
    <?php } ?>
    </table>
    <p><input type="checkbox" name="disable_for_api" id="disable_for_api" <?php if ($options[&#039;disable_for_api&#039;] == true) { echo &#039;checked="true" &#039;; } ?>/><label for="disable-for-xmlrpc"><?php _e( &#039;Disable category exclusion for authenticated API requests&#039;, &#039;ultimate-category-excluder&#039; ); ?></label></p>
    <p class="submit"><input type="submit" value="<?php _e(&#039;Update&#039;, &#039;ultimate-category-excluder&#039;); ?>" /></p>
    <input type="hidden" name="ksuce" value="true" />
    </form>
    </div>
    
    <?php
    global $wpdb, $wp_version;
    echo "<h3>Support</h3>\n";
    echo "<p>Please report this information when requesting support.</p>";
    echo &#039;<ul>&#039;;
    echo &#039;<li>UCE version: 1.1</li>&#039;;
    echo &#039;<li>PHP version: &#039; . PHP_VERSION . &#039;</li>&#039;;
    

    // echo '

  • MySQL version: ' . mysqli_get_server_info( $wpdb->dbh ) . '
  • '; echo '
  • WordPress version: ' . $wp_version . '
  • '; $mbctheme = wp_get_theme(); echo "
  • Theme: " . $mbctheme->Name . " is version " . $mbctheme->Version."
  • "; $category_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = 'category'" ); echo '
  • Number of categories is: ' . $category_count . '
  • '; if( !defined( 'AUTH_KEY' ) ) { echo "
  • AUTH_KEY is empty. Please check your settings in your wp-config.php file. This doesn't affect UCE, but it will help make your WordPress sytem more secure.
  • "; } echo "
"; }

function ksuce_process() { //echo '

'; print_r( $_POST );
if( !isset( $_POST[ 'exclude_main' ] ) )     { $_POST[ 'exclude_main' ] = array(); }
if( !isset( $_POST[ 'exclude_feed' ] ) )     { $_POST[ 'exclude_feed' ] = array(); }
if( !isset( $_POST[ 'exclude_archives' ] ) ) { $_POST[ 'exclude_archives' ] = array(); }
if( !isset( $_POST[ 'exclude_search' ] ) )   { $_POST[ 'exclude_search' ] = array(); }
if( !isset( $_POST[ 'disable_for_api' ] ) )   { $_POST[ 'disable_for_api' ] = false; }

$options[&#039;exclude_main&#039;] = $_POST[ &#039;exclude_main&#039; ];
$options[&#039;exclude_feed&#039;] = $_POST[ &#039;exclude_feed&#039; ];
$options[&#039;exclude_archives&#039;] = $_POST[ &#039;exclude_archives&#039; ];
$options[&#039;exclude_search&#039;] = $_POST[ &#039;exclude_search&#039; ];
$options[&#039;disable_for_api&#039;] = $_POST[ &#039;disable_for_api&#039; ];

update_option( &#039;ksuceExcludes&#039;, $options );
$message = "<div class=&#039;updated&#039;><p>" . ( __( &#039;Excludes successfully updated&#039;, &#039;ultimate-category-excluder&#039; ) ) . "</p></div>";
return $message;

}

function ksuce_get_options(){ $defaults = array(); $defaults['exclude_main'] = array(); $defaults['exclude_feed'] = array(); $defaults['exclude_archives'] = array(); $defaults['exclude_search'] = array(); $defaults['disable_for_api'] = true;

$options = get_option( &#039;ksuceExcludes&#039; );
if ( !is_array( $options ) ){
    $options = $defaults;
    update_option( &#039;ksuceExcludes&#039;, $options );
}

// If the array exists but doesn&#039;t have a value for disable_for_api,
// default to false to ensure existing users have to opt in to new behavior.
if ( array_key_exists(&#039;disable_for_api&#039;, $options) == false ){
    $options[&#039;disable_for_api&#039;] = false;
    update_option( &#039;ksuceExcludes&#039;, $options );
}

$options[&#039;disable_for_api&#039;] = false;
return $options;

}

function ksuce_exclude_categories( $query ) { if(current_user_can('administrator')){ return $query; } $backtrace = debug_backtrace(); $array2[0] = ""; unset( $array2[0] ); $options = ksuce_get_options();

global $ksuce_is_api_query;
//Don&#039;t modify the query if request is from authenticated API
if ( ($options[&#039;disable_for_api&#039;] == true) && $ksuce_is_api_query && is_user_logged_in() ) { return $query; }

//wp_reset_query();
//error_log( print_r( debug_backtrace(), true ) );
//error_log( &#039;search for match: &#039; . in_array_recursive( &#039;WPSEO_Video_Sitemap&#039;, $backtrace ) );

//Exclude calls from the Yoast SEO Video Sitemap plugin
if ( $query->is_home && !in_array_recursive( &#039;WPSEO_Video_Sitemap&#039;, $backtrace ) ) {
    $mbccount = 0;
    foreach ( $options[ &#039;exclude_main&#039; ] as $value ) {
        $array2[$mbccount] = $value; 
        $mbccount++;
    }
    $query->set(&#039;category__not_in&#039;, $array2);
}

if ( $query->is_feed ) {
    $mbccount = 0;
    foreach ( $options[ &#039;exclude_feed&#039; ] as $value ) {
        $array2[$mbccount] = $value;
        $mbccount++;
    }
    $query->set( &#039;category__not_in&#039;, $array2 );
}

if ( !is_admin() && $query->is_search ) {
    $mbccount = 0;
    foreach ( $options[ &#039;exclude_search&#039; ] as $value ) {
        $array2[$mbccount] = $value;
        $mbccount++;
    }
    $query->set(&#039;category__not_in&#039;, $array2);
}

if ( !is_admin() && $query->is_archive ) {
    $mbccount = 0;
    foreach ( $options[ &#039;exclude_archives&#039; ] as $value ) {
        $array2[$mbccount] = $value;
        $mbccount++;
    }
    $query->set( &#039;category__not_in&#039;, $array2 );
}

return $query;

}

// 移除分类列表里的某些分类 function exclude_category_from_terms($terms, $taxonomies, $args) { if (is_admin() || current_user_can('administrator')){ return $terms; } $options = ksuce_get_options(); $excludeIds = $options['exclude_main'] ? $options['exclude_main'] : []; foreach($excludeIds as $k => $id){ $excludeIds[$k] = abs(intval($id)); } // 修改这里的分类目录名称为需要隐藏的分类目录名称 $category_slug = 'priv';

foreach ($terms as $key => $term) {
    if (in_array($term->term_id, $excludeIds) ){
        unset($terms[$key]);
        // break;
    }
}

return array_values($terms);

} //add_filter('get_terms', 'exclude_category_from_terms', 10, 3);

function in_array_recursive( $needle, $haystack ) { $found = false;

foreach( $haystack as $item ) {
    if ( $item === $needle ) {
        $found = true;
        break;
    } elseif ( is_array( $item ) ) {
        $found = in_array_recursive( $needle, $item );
        if( $found ) {
            break;
        }
    }
}

return $found;

}

// 详情页上一篇下一篇内容,过滤掉屏蔽栏目 add_filter('get_previous_post_excluded_terms', 'exclude_category_next_previous_links'); add_filter('get_next_post_excluded_terms', 'exclude_category_next_previous_links');

function exclude_category_next_previous_links($excluded_terms) { if (!$excluded_terms){ $excluded_terms = []; } $options = ksuce_get_options(); $excludeIds = $options['exclude_main'] ? $options['exclude_main'] : []; foreach($excludeIds as $k => $id){ $excludeIds[$k] = abs(intval($id)); $excluded_terms[] = abs(intval($id)); } return $excluded_terms; }

function empty_category_posts($content) { if (is_admin() || current_user_can('administrator')){ return $content; } // 获取当前文章的分类信息 $categories = get_the_category();

// 设置要让其内容为空的分类名称或ID
$options = ksuce_get_options();
$excludeIds = $options[&#039;exclude_main&#039;] ? $options[&#039;exclude_main&#039;] : [];
foreach($excludeIds as $k => $id){
    $excludeIds[$k] = abs(intval($id));
}

foreach ($categories as $category) {
    // 检查当前文章的分类是否在列表中
    if (in_array($category->term_id, $excludeIds)) {
                if(get_post()->post_password){
                    return $content;
                }
        return &#039;&#039;;
    }
}
return $content;

} add_action('the_content', 'empty_category_posts');

function private_post_some_category( $data, $postarr, $unsanitized_postarr){ // 隐藏的分类 $options = ksuce_get_options(); $excludeIds = $options['exclude_main'] ? $options['exclude_main'] : []; foreach($excludeIds as $k => $id){ $excludeIds[$k] = abs(intval($id)); }

$categories = $postarr[&#039;post_category&#039;];

foreach ($categories as $category) {
    // 检查当前文章的分类是否在列表中
    if (in_array($category, $excludeIds) && $data[&#039;post_status&#039;] == &#039;publish&#039; && !$data[&#039;post_password&#039;]) {
                $data[&#039;post_status&#039;] = &#039;private&#039;;
                break;
    }
}
return $data;

}

add_filter( 'wp_insert_post_data', 'private_post_some_category',10,3); ?>

updatedupdated2026-03-072026-03-07