mircobabini
12/23/2013 - 1:48 PM

wp-hide-structure-categories.php

<?php

/*
Plugin Name: Filter Categories
Plugin URI: 
Description: Defines which categories are enabled to be shown as meta for a post under defined categories. It's not a show/not-show filter for posts, but for categories.
Version: 1.0.0
Author: mirkolofio
Author URI: 
License: GPLv2
*/

/* 
Copyright (C) 2013 mirkolofio

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

class FilterCategoriesPlugin {
	/**
	 * @param string $output
	 * @return string
	 */
	function wp_list_categories ($output) {
		$categories = explode (', ', $output);
		
		$enabled_categories = array ();
		foreach ($categories as $category) {
			$category_name = trim (strip_tags ($category));
			if (self::_category_enabled ($category_name))
				$enabled_categories[] = $category;
		}
		
		return implode (', ', $enabled_categories);
	}
	
	/**
	 * @param string $category_name
	 * @return bool
	 */
	private function _category_enabled ($category_name) {
		return (in_array (strtolower ($category_name), self::_get_enabled_categories ()));
	}
	
	/**
	 * @staticvar array $_enabled_categories
	 * @return array
	 */
	private function _get_enabled_categories () {
		static $_enabled_categories;
		if ($_enabled_categories === null)
			$_enabled_categories = get_option ('cf-enabled-categories', array ());
		
		return $_enabled_categories;
	}
}

add_filter ('the_category', array ('MircoBabiniPlugin', 'wp_list_categories'));