[ Root ] [ Search ] [ Index ]

PHP Cross Reference of bbPress Trunk

Provided by Yoast

title

Body

[close]

/bb-includes/ -> functions.bb-forums.php (source)

   1  <?php
   2  
   3  /* Forums */
   4  
   5  function bb_get_forums_hierarchical( $root = 0, $depth = 0, $leaves = false, $_recursed = false ) {
   6      static $_leaves = false;
   7  
   8      if (!$_recursed)
   9          $_leaves = false;
  10  
  11      $root = (int) $root;
  12  
  13      if ( false === $_leaves )
  14          $_leaves = $leaves ? $leaves : bb_get_forums();
  15  
  16      if ( !$_leaves )
  17          return false;
  18  
  19      $branch = array();
  20  
  21      reset($_leaves);
  22  
  23      while ( list($l, $leaf) = each($_leaves) ) {
  24          if ( $root == $leaf->forum_parent ) {
  25              $new_root = (int) $leaf->forum_id;
  26              unset($_leaves[$l]);
  27              $branch[$new_root] = 1 == $depth ? true : bb_get_forums_hierarchical( $new_root, $depth - 1, false, true );
  28              reset($_leaves);
  29          }
  30      }
  31  
  32      if ( !$_recursed ) {
  33          if ( !$root )
  34              foreach ( $_leaves as $leaf ) // Attach orphans to root
  35                  $branch[$leaf->forum_id] = true;
  36          $_leaves = false;
  37          return ( empty($branch) ? false : $branch );
  38      }
  39  
  40      return $branch ? $branch : true;
  41  }
  42  
  43  function _bb_get_cached_data( $keys, $group, $callback ) {
  44      $return = array();
  45      foreach ( $keys as $key ) {
  46          // should use wp_cache_get_multi if available
  47          if ( false === $value = wp_cache_get( $key, $group ) )
  48              if ( !$value = call_user_func( $group, $key ) )
  49                  continue;
  50          $return[$key] = $value;
  51      }
  52      return $return;
  53  }
  54  
  55  // 'where' arg provided for backward compatibility only
  56  function bb_get_forums( $args = null ) {
  57      global $bbdb;
  58  
  59      if ( is_numeric($args) ) {
  60          $args = array( 'child_of' => $args, 'hierarchical' => 1, 'depth' => 0 );
  61      } elseif ( is_callable($args) ) {
  62          $args = array( 'callback' => $args );
  63          if ( 1 < func_num_args() )
  64              $args['callback_args'] = func_get_arg(1);
  65      }
  66  
  67      $defaults = array( 'callback' => false, 'callback_args' => false, 'child_of' => 0, 'hierarchical' => 0, 'depth' => 0, 'cut_branch' => 0, 'where' => '', 'order_by' => 'forum_order' );
  68      $args = wp_parse_args( $args, $defaults );
  69  
  70      extract($args, EXTR_SKIP);
  71      $child_of = (int) $child_of;
  72      $hierarchical = 'false' === $hierarchical ? false : (bool) $hierarchical;
  73      $depth = (int) $depth;
  74  
  75      $where = apply_filters( 'get_forums_where', $where );
  76      $key = md5( serialize( $where . '|' . $order_by ) ); // The keys that change the SQL query
  77      if ( false !== $forum_ids = wp_cache_get( $key, 'bb_forums' ) ) {
  78          $forums = _bb_get_cached_data( $forum_ids, 'bb_forum', 'get_forum' );
  79      } else {
  80          $forum_ids = array();
  81          $forums = array();
  82          $_forums = (array) $bbdb->get_results("SELECT * FROM $bbdb->forums $where ORDER BY `$order_by`;");
  83          $_forums = bb_append_meta( $_forums, 'forum' );
  84          foreach ( $_forums as $f ) {
  85              $forums[(int) $f->forum_id] = $f;
  86              $forum_ids[] = (int) $f->forum_id;
  87              wp_cache_add( $f->forum_id, $f, 'bb_forum' );
  88              wp_cache_add( $f->forum_slug, $f->forum_id, 'bb_forum_slug' );
  89          }
  90          wp_cache_set( $key, $forum_ids, 'bb_forums' );
  91      }
  92  
  93      $forums = (array) apply_filters( 'get_forums', $forums );
  94  
  95      if ( $child_of || $hierarchical || $depth ) {
  96  
  97          $_forums = bb_get_forums_hierarchical( $child_of, $depth, $forums );
  98  
  99          if ( !is_array( $_forums ) )
 100              return false;
 101  
 102          $_forums = (array) bb_flatten_array( $_forums, $cut_branch );
 103  
 104          foreach ( array_keys($_forums) as $_id )
 105              $_forums[$_id] = $forums[$_id];
 106  
 107          $forums = $_forums;
 108      }
 109  
 110      if ( !is_callable($callback) )
 111          return $forums;
 112  
 113      if ( !is_array($callback_args) )
 114          $callback_args = array();
 115  
 116      foreach ( array_keys($forums) as $f ) :
 117          $_callback_args = $callback_args;
 118          array_push( $_callback_args, $forums[$f]->forum_id );
 119          if ( false == call_user_func_array( $callback, $_callback_args ) ) // $forum_id will be last arg;
 120              unset($forums[$f]);
 121      endforeach;
 122      return $forums;
 123  }
 124  
 125  function bb_get_forum( $id ) {
 126      global $bbdb;
 127  
 128      if ( !is_numeric($id) ) {
 129          list($slug, $sql) = bb_get_sql_from_slug( 'forum', $id );
 130          $id = wp_cache_get( $slug, 'bb_forum_slug' );
 131      }
 132  
 133      // not else
 134      if ( is_numeric($id) ) {
 135          $id = (int) $id;
 136          $sql = "forum_id = $id";
 137      }
 138  
 139      if ( 0 === $id || !$sql )
 140          return false;
 141  
 142      // $where is NOT bbdb:prepared
 143      if ( $where = apply_filters( 'get_forum_where', '' ) ) {
 144          $forum = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->forums WHERE forum_id = %d", $id ) . " $where" );
 145          return bb_append_meta( $forum, 'forum' );
 146      }
 147  
 148      if ( is_numeric($id) && false !== $forum = wp_cache_get( $id, 'bb_forum' ) )
 149          return $forum;
 150  
 151      $forum = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->forums WHERE $sql", $id ) );
 152      $forum = bb_append_meta( $forum, 'forum' );
 153      wp_cache_set( $forum->forum_id, $forum, 'bb_forum' );
 154      wp_cache_add( $forum->forum_slug, $forum, 'bb_forum_slug' );
 155  
 156      return $forum;
 157  }


Generated: Mon Nov 15 04:45:27 2010 Cross-referenced by PHPXref 0.7