[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/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( $callback, $key ) ) {
  49                  continue;
  50              }
  51          }
  52          $return[$key] = $value;
  53      }
  54      return $return;
  55  }
  56  
  57  // 'where' arg provided for backward compatibility only
  58  function bb_get_forums( $args = null ) {
  59      global $bbdb;
  60  
  61      if ( is_numeric($args) ) {
  62          $args = array( 'child_of' => $args, 'hierarchical' => 1, 'depth' => 0 );
  63      } elseif ( is_callable($args) ) {
  64          $_args = func_get_args();
  65          $args = array( 'callback' => $args );
  66          if ( 1 < func_num_args() )
  67              $args['callback_args'] = $_args[1];
  68      }
  69  
  70      $defaults = array( 'callback' => false, 'callback_args' => false, 'child_of' => 0, 'hierarchical' => 0, 'depth' => 0, 'cut_branch' => 0, 'where' => '', 'order_by' => 'forum_order' );
  71      $args = wp_parse_args( $args, $defaults );
  72  
  73      extract($args, EXTR_SKIP);
  74      $child_of = (int) $child_of;
  75      $hierarchical = 'false' === $hierarchical ? false : (bool) $hierarchical;
  76      $depth = (int) $depth;
  77  
  78      $where = apply_filters( 'get_forums_where', $where );
  79      $key = md5( serialize( $where . '|' . $order_by ) ); // The keys that change the SQL query
  80      if ( false !== $forum_ids = wp_cache_get( $key, 'bb_forums' ) ) {
  81          $forums = _bb_get_cached_data( $forum_ids, 'bb_forum', 'bb_get_forum' );
  82      } else {
  83          $forum_ids = array();
  84          $forums = array();
  85          $_forums = (array) $bbdb->get_results("SELECT * FROM $bbdb->forums $where ORDER BY `$order_by`;");
  86          $_forums = bb_append_meta( $_forums, 'forum' );
  87          foreach ( $_forums as $f ) {
  88              $forums[(int) $f->forum_id] = $f;
  89              $forum_ids[] = (int) $f->forum_id;
  90              wp_cache_add( $f->forum_id, $f, 'bb_forum' );
  91              wp_cache_add( $f->forum_slug, $f->forum_id, 'bb_forum_slug' );
  92          }
  93          wp_cache_set( $key, $forum_ids, 'bb_forums' );
  94      }
  95  
  96      $forums = (array) apply_filters( 'get_forums', $forums );
  97  
  98      if ( $child_of || $hierarchical || $depth ) {
  99  
 100          $_forums = bb_get_forums_hierarchical( $child_of, $depth, $forums );
 101  
 102          if ( !is_array( $_forums ) )
 103              return false;
 104  
 105          $_forums = (array) bb_flatten_array( $_forums, $cut_branch );
 106  
 107          foreach ( array_keys($_forums) as $_id )
 108              $_forums[$_id] = $forums[$_id];
 109  
 110          $forums = $_forums;
 111      }
 112  
 113      if ( !is_callable($callback) )
 114          return $forums;
 115  
 116      if ( !is_array($callback_args) )
 117          $callback_args = array();
 118  
 119      foreach ( array_keys($forums) as $f ) :
 120          $_callback_args = $callback_args;
 121          array_push( $_callback_args, $forums[$f]->forum_id );
 122          if ( false == call_user_func_array( $callback, $_callback_args ) ) // $forum_id will be last arg;
 123              unset($forums[$f]);
 124      endforeach;
 125      return $forums;
 126  }
 127  
 128  function bb_get_forum( $id ) {
 129      global $bbdb;
 130  
 131      if ( !is_numeric($id) ) {
 132          list($slug, $sql) = bb_get_sql_from_slug( 'forum', $id );
 133          $id = wp_cache_get( $slug, 'bb_forum_slug' );
 134      }
 135  
 136      // not else
 137      if ( is_numeric($id) )
 138          $sql = $bbdb->prepare( "forum_id = %d", $id );
 139  
 140      if ( 0 === $id || empty( $sql ) )
 141          return false;
 142  
 143      // $where is NOT bbdb:prepared
 144      if ( $where = apply_filters( 'get_forum_where', '' ) ) {
 145          $forum = $bbdb->get_row( "SELECT * FROM $bbdb->forums WHERE $sql $where" );
 146          return bb_append_meta( $forum, 'forum' );
 147      }
 148  
 149      if ( is_numeric($id) && false !== $forum = wp_cache_get( $id, 'bb_forum' ) )
 150          return $forum;
 151  
 152      $forum = $bbdb->get_row( "SELECT * FROM $bbdb->forums WHERE $sql" );
 153      $forum = bb_append_meta( $forum, 'forum' );
 154      wp_cache_set( $forum->forum_id, $forum, 'bb_forum' );
 155      wp_cache_add( $forum->forum_slug, $forum->forum_id, 'bb_forum_slug' );
 156  
 157      return $forum;
 158  }


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1