Forum Replies Created

Viewing 1 replies (of 1 total)
  • In WP >= 2.9.x there is no wp-includes/streams.php or an equivalent FileReader class anymore. Maybe using wp-includes/pomo/streams.php and replacing FileReader with POMO_CachedFileReader may work, but I haven’ tried.

    This is the code I’m using with WP 2.9.x. DGmike, thanks for the idea and code base – It’s still yours, I’m just bugfixing. The code contains the needed FileReader class directly (as I didn’t had the time to find and test a suitable replacement) and some spelling changes. It still needs a complete refactoring…

    It also contains an additional filter on category_save_pre – whatever the user does, the post is assigned to his categories on save. If there are no categories specified for that user, the categories selected by the user will be used.

    If you don’t want that feature, just comment out the line
    add_filter ('category_save_pre', array('UserCatsManager', 'categorySavePre')); in the init() method.

    The code hasn’t been tested very intensively and I’m not able to provide support…

    <?php
    
    /*
      Plugin Name: User-Cats Manager
      Plugin URI: http://dgmike.wordpress.com/user-cats-manager
      Description: Provides to admin users a way to select what categorie determined users can write. Version 2.3 has been fixed
                   "quick & dirty" for WP >= 2.9.x as original author doesn't seem to work on the plugin anymore.
      Version: 2.3
      Modified: 2010-01-08: HerrB
      Author: DGmike/HerrB
      Author URI: http://dgmike.wordpress.com
    */
    
    // Bugfix WP < 2.9.x
    //require_once(ABSPATH . 'wp-includes/streams.php');
    
    // Bugfix WP >= 2.9.x
    // FileReader (wp-includes/streams.php) doesn't exist in WP 2.9.x anymore
    // POMO_FileReader in wp-includes/pomo/streams.php would work, but doesn't provide length attribute.
    // POMO_CachedFileReader may work, but seems to provide unneccessary overhead
    class FileReader {
      var $_pos;
      var $_fd;
      var $_length;
    
      function FileReader($filename) {
        if (file_exists($filename)) {
    
          $this->_length=filesize($filename);
          $this->_pos = 0;
          $this->_fd = fopen($filename,'rb');
          if (!$this->_fd) {
    	$this->error = 3; // Cannot read file, probably permissions
    	return false;
          }
        } else {
          $this->error = 2; // File doesn't exist
          return false;
        }
      }
    
      function read($bytes) {
        if ($bytes) {
          fseek($this->_fd, $this->_pos);
    
          // PHP 5.1.1 does not read more than 8192 bytes in one fread()
          // the discussions at PHP Bugs suggest it's the intended behaviour
          while ($bytes > 0) {
            $chunk  = fread($this->_fd, $bytes);
            $data  .= $chunk;
            $bytes -= strlen($chunk);
          }
          $this->_pos = ftell($this->_fd);
    
          return $data;
        } else return '';
      }
    
      function seekto($pos) {
        fseek($this->_fd, $pos);
        $this->_pos = ftell($this->_fd);
        return $this->_pos;
      }
    
      function currentpos() {
        return $this->_pos;
      }
    
      function length() {
        return $this->_length;
      }
    
      function close() {
        fclose($this->_fd);
      }
    } // FileReader
    
    class UserCatsManager {
      static $wpdb;
      static $info;
    
      public static function init() {
        global $wpdb;
        UserCatsManager::$wpdb = $wpdb;
        //Outros mapeamentos
        UserCatsManager::$info['plugin_fpath'] = dirname(__FILE__);
        add_action ('admin_menu', array('UserCatsManager','options'));
        add_action ('load-post.php', array('UserCatsManager','loadpost'));
        add_filter ('get_terms', array ('UserCatsManager', 'filterCats'), 0);
        add_filter ('category_save_pre', array('UserCatsManager', 'categorySavePre')); // this filter is used when publishing a post
      }
    
      static function install (){
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        UserCatsManager::$wpdb->query (sprintf('
          CREATE TABLE %suser_cats_manager (
            <code>user_id</code> INT NOT NULL,
            <code>term_id</code> INT NOT NULL,
            PRIMARY KEY (<code>user_id</code>, <code>term_id</code>)
          )
        ', UserCatsManager::$wpdb->prefix));
      }
    
      static function uninstall () {
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        UserCatsManager::$wpdb->query (sprintf ('
          DROP TABLE %suser_cats_manager
        ', UserCatsManager::$wpdb->prefix));
      }
    
      static function options () {
        add_options_page (__('Categories and Users'), 'Categories And Users', 10, __FILE__, array('UserCatsManager','optionsMenu'));
      }
    
      static function optionsMenu ($nick = '') {
        if ($_POST['user']) $nick = $_POST['user'];
    
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
    
        if ($nick) $nick = UserCatsManager::$wpdb->get_results(sprintf('SELECT * FROM %susers WHERE ID=\'%s\'', UserCatsManager::$wpdb->prefix, $nick));
    
        $tplObj = new FileReader(UserCatsManager::$info['plugin_fpath'] . '/options.html');
    
        $tpl = $tplObj->read($tplObj->length());
    
        $items = array (
          '{LEGEND}'               => __('Choose the categories that the user can use'),
          '{LEGEND_BM}'            => __('Choose the categories of bookmarks that the user can use'),
          '{ACTION}'               => $_SERVER['REQUEST_URI'],
          '{ALL_CATS}'             => UserCatsManager::allCats($nick[0]->ID),
          '{ALL_CATS_BM}'          => UserCatsManager::allCatsBm($nick[0]->ID),
          '{USERS}'                => UserCatsManager::allUsersSelect(),
          '{NICK}'                 => $nick[0]->ID,
          '{SELECT_USER}'          => __('Select the user <small>(admin users have all access)</small>'),
          '{EDITING}'              => __('Editing user ') . $nick[0]->user_nicename,
          '{EDIT}'                 => __('edit'),
          '{SAVE}'                 => __('Save'),
          '{SAVED}'                => __(''),
          '{MESSAGE_DEFAULT_CATS}' => __('If the user does not choose a category, wordpress puts the default category on post and/or bookmark. Tip: give your user the privileges on the default category. To edit the default category, go to <strong>Writing Settings</strong>.')
        );
        if ($_POST['nick']) {
          UserCatsManager::save($_POST['nick'], (array) $_POST['categoria']);
          $items['{SAVED}'] = __('<div id="message" class="updated fade"><p>The changes has been taked</p></div>');
        }
        $tpl = str_replace("\n", '\n', $tpl);
        if ($nick == '')
          $tpl = preg_replace("/{POST}.*{\/POST}/", '', $tpl);
        $tpl = str_replace('\n', "\n", $tpl);
        $tpl = preg_replace("/{\/?POST}/", '', $tpl);
        foreach ($items as $key=>$value)
        	$tpl = str_replace ($key, $value, $tpl);
        print $tpl;
      }
    
      static function getCats($n=0, $user='', $type='category'){
        if ($_POST) $user = $_POST['user'];
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        $wpdb = UserCatsManager::$wpdb;
        $sqlCats = 'SELECT %sterms.term_id, slug, name FROM %sterms NATURAL JOIN %sterm_taxonomy WHERE taxonomy = \'%s\' AND parent = \'%s\'';
        $cats = $wpdb->get_results(sprintf($sqlCats, $wpdb->prefix, $wpdb->prefix, $wpdb->prefix, $type, $n), ARRAY_N);
        $base = '%s<li class="popular-category"><label for="ck_%s"><input id="ck_%s" type="checkbox" name="categoria[]" value="%s"%s  /> %s%s</label></li>';
        $return = array ();
        if (count($cats)) {
          foreach ($cats as $cat){
            $uniq = uniqid();
            $isSelect = $wpdb->get_results(sprintf(
                'SELECT * FROM %suser_cats_manager WHERE user_id = \'%s\' AND term_id = \'%s\'',
                $wpdb->prefix,
                $user,
                $cat[0]
              )
            );
            $checked = count($isSelect) ? ' checked="checked"' : '';
            $default = in_array($cat[0], array(get_option('default_category'), get_option('default_link_category') )) ?
                       __(' <strong>(default)</strong>') :
                       '';
          	$return[] = sprintf ($base, "\n    ", $uniq, $uniq, $cat[0], $checked, $cat[1], $default);
            if (count(UserCatsManager::getCats($cat[0])))
              $return[] = "\n<ul>" . implode ('', UserCatsManager::getCats($cat[0], $user)) . "\n</ul>\n";
          }
        }
        return $return;
      }
    
      static function allCats($user = '') {
        $return = UserCatsManager::getCats(0, $user);
        return '<ul>'.implode ('', $return).'</ul>';
      }
    
      static function allCatsBm($user = '') {
        $return = UserCatsManager::getCats(0, $user, 'link_category');
        return '<ul>'.implode ('', $return).'</ul>';
      }
    
      static function allUsersSelect($user = '') {
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        if ($_POST) $user = $_POST['user'];
        $wpdb = UserCatsManager::$wpdb;
        $res = $wpdb->get_results(sprintf('SELECT id, user_nicename FROM %susers', $wpdb->prefix));
        $base = '<option value="%s"%s>%s</option>';
        $return = '';
        foreach ($res as $value) {
          $usr = new WP_User($value->id);
          if ((int) $usr->user_level === 10) continue;
          $selected = ( preg_match ('/\d+/', $user) && $user == $value->id) ? ' selected="selected"' : '';
          $return .= sprintf($base, $value->id, $selected, $value->user_nicename);
        }
        return $return;
      }
    
      static function save ($user, $cats) {
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        $wpdb = UserCatsManager::$wpdb;
        $wpdb->query(sprintf('DELETE FROM %suser_cats_manager WHERE user_id = \'%s\'', $wpdb->prefix, $user));
        foreach ($cats as $cat) {
          $sql = sprintf('INSERT INTO %suser_cats_manager VALUES (\'%s\', \'%s\');', $wpdb->prefix, $user, $cat);
        	$wpdb->query($sql);
        }
      }
    
      static function filterCats($cats) {
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        $wpdb = UserCatsManager::$wpdb;
        $current_user = wp_get_current_user();
        // Bugfix
        // Old: if ($current_user->user_level == 10)
        if ($current_user->user_level == 10 || !is_admin()) {
          return $cats;
        }
        if (gettype($cats) != 'array') return $cats;
        foreach ($cats as $key=>$cat) {
          $sql = sprintf('SELECT * FROM %suser_cats_manager WHERE user_id = \'%s\' AND term_id = \'%s\' ', $wpdb->prefix, $current_user->ID, $cat->term_id);
          $catH = $wpdb->get_results($sql);
          if (!count($catH))
            unset ($cats[$key]);
        }
    
        return $cats;
        //$accepteds = $wpdb->get_results()
      }
    
      function categorySavePre($categories_checked) {
        // filter checked categories when a post is saved
    
        if ($current_user->user_level == 10 || !is_admin()) {
          return $categories_checked;
        }
    
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
        $wpdb = UserCatsManager::$wpdb;
        $current_user = wp_get_current_user();
    
        $sql = sprintf('SELECT term_id FROM %suser_cats_manager WHERE user_id = \'%s\'', $wpdb->prefix, $current_user->ID);
        $oCategories = $wpdb->get_results($sql, 'ARRAY_N');
        $aValidCategories = array();
    
        foreach ($oCategories as $aCategory) {
          $aValidCategories[] = $aCategory[0];
        }
    
        if (count($aValidCategories) == 0) {
          // There is no specifically allowed category for the user, allow every category
          return $categories_checked;
        } else {
          // Set the allowed categories as selected categories. Ignore specific selections.
          return $aValidCategories;
        }
      }
    
      static function set_current_user () {
        global $current_user;
        if ($current_user->user_level == 10) return;
        unset($current_user->allcaps['manage_categories']);
      }
    
      static function loadpost () {
        if ($_REQUEST['action'] != 'edit' && !isset($_REQUEST['post'])) return;
    
        global $current_user;
        if ($current_user->user_level == 10) return;
    
        if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init();
    
        $categories = UserCatsManager::$wpdb->get_results(sprintf(
            'SELECT term_id FROM %suser_cats_manager WHERE user_id = \'%s\'',
            UserCatsManager::$wpdb->prefix,
            $current_user->ID
          ), 'ARRAY_N'
        );
        $validCategories = array();
        foreach ($categories as $cat) $validCategories[] = $cat[0];
        $categories = wp_get_post_categories($_GET['post']);
    
        if ((bool) array_diff_assoc($categories, $validCategories) === true) {
          $_GET = array();
          $_POST = array();
          $_REQUEST = array();
        }
      }
    }
    
    $ucmPluginFile = substr(strrchr(dirname(__FILE__),DIRECTORY_SEPARATOR),1).DIRECTORY_SEPARATOR.basename(__FILE__);
    register_activation_hook($ucmPluginFile,array('UserCatsManager','install'));
    register_deactivation_hook($ucmPluginFile,array('UserCatsManager','uninstall'));
    
    add_action('set_current_user', array('UserCatsManager','set_current_user'));
    add_filter('init', array('UserCatsManager','init'));
    
    ?>

    Regards,

    HerrB

Viewing 1 replies (of 1 total)