[ Index ]

PHP Cross Reference of bbPress

title

Body

[close]

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

   1  <?php
   2  
   3  function bb_get_admin_header()
   4  {
   5      do_action( 'bb_admin-header.php' );
   6      include( 'admin-header.php' );
   7      do_action( 'bb_get_admin_header' );
   8  }
   9  
  10  function bb_get_admin_footer()
  11  {
  12      do_action( 'bb_admin-footer.php' );
  13      include( 'admin-footer.php' );
  14  }
  15  
  16  function bb_admin_notice( $message, $class = false )
  17  {
  18      if ( is_string( $message ) ) {
  19          $message = '<p>' . $message . '</p>';
  20          $class = $class ? $class : 'updated';
  21      } elseif ( is_wp_error( $message ) ) {
  22          $errors = $message->get_error_messages();
  23          switch ( count( $errors ) ) {
  24              case 0:
  25                  return false;
  26                  break;
  27              case 1:
  28                  $message = '<p>' . $errors[0] . '</p>';
  29                  break;
  30              default:
  31                  $message = '<ul>' . "\n\t" . '<li>' . join( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>';
  32                  break;
  33          }
  34          $class = $class ? $class : 'error';
  35      } else {
  36          return false;
  37      }
  38  
  39      $message = '<div id="message" class="' . esc_attr( $class ) . '">' . $message . '</div>';
  40      $message = str_replace( "'", "\'", $message );
  41      $lambda = create_function( '', "echo '$message';" );
  42      add_action( 'bb_admin_notices', $lambda );
  43      return $lambda;
  44  }
  45  
  46  /* Menu */
  47  
  48  function bb_admin_menu_generator()
  49  {
  50      global $bb_menu, $bb_submenu;
  51      $bb_menu = array();
  52      $bb_submenu = array();
  53  
  54      // Dashboard menu items < 50
  55      $bb_menu[0]  = array( __( 'Dashboard' ), 'moderate', 'index.php', '', 'bb-menu-dashboard' );
  56          $bb_submenu['index.php'][5]   = array( __( 'Dashboard' ), 'moderate', 'index.php' );
  57  
  58      // 50 < Plugin added menu items < 100
  59  
  60      $bb_menu[100] = array( '', 'read', 'separator' );
  61  
  62      // 100 < Plugin added menu items < 150
  63  
  64      // 150 < First menu items < 200
  65      $bb_menu[150] = array( __( 'Forums' ), 'manage_forums', 'forums.php', '', 'bb-menu-forums' );
  66          $bb_submenu['forums.php'][5]   = array( __( 'Forums' ), 'manage_forums', 'forums.php' );
  67      $bb_menu[155] = array( __( 'Topics' ), 'moderate', 'topics.php', '', 'bb-menu-topics' );
  68          $bb_submenu['topics.php'][5]   = array( __( 'Topics' ), 'moderate', 'topics.php' );
  69      $bb_menu[160] = array( __( 'Posts' ), 'moderate', 'posts.php', '', 'bb-menu-posts' );
  70          $bb_submenu['posts.php'][5]   = array( __( 'Posts' ), 'moderate', 'posts.php' );
  71  
  72      // 200 < Plugin added menu items < 250
  73  
  74      $bb_menu[250] = array( '', 'read', 'separator' );
  75  
  76      // 250 < Plugin added menu items < 300
  77  
  78      // 300 < Second menu items < 350
  79      $bb_menu[300] = array( __( 'Appearance' ), 'manage_themes', 'themes.php', '', 'bb-menu-appearance' );
  80          $bb_submenu['themes.php'][5]   = array(__('Themes'), 'manage_themes', 'themes.php');
  81      $bb_menu[305] = array( __( 'Plugins' ), 'use_keys', 'plugins.php', '', 'bb-menu-plugins' );
  82          $bb_submenu['plugins.php'][5]  = array( __( 'Installed' ), 'manage_plugins', 'plugins.php' );
  83      $bb_menu[310] = array( __( 'Users' ), 'moderate', 'users.php', '', 'bb-menu-users' );
  84          $bb_submenu['users.php'][5]  = array( __( 'Users' ), 'moderate', 'users.php' );
  85          $bb_submenu['users.php'][10]  = array( __( 'Add New' ), 'manage_options', 'user-add-new.php' );
  86      $bb_menu[315] = array( __( 'Tools' ), 'recount', 'tools-recount.php', '', 'bb-menu-tools' );
  87          $bb_submenu['tools-recount.php'][5] = array( __( 'Re-count' ), 'recount', 'tools-recount.php' );
  88      $bb_menu[320] = array( __( 'Settings' ), 'manage_options', 'options-general.php', '', 'bb-menu-settings' );
  89          $bb_submenu['options-general.php'][5]  = array( __( 'General' ), 'manage_options', 'options-general.php' );
  90          //$bb_submenu['options-general.php'][10] = array( __( 'Date and Time' ), 'manage_options', 'options-time.php' );
  91          $bb_submenu['options-general.php'][15] = array( __( 'Writing' ), 'manage_options', 'options-writing.php' );
  92          $bb_submenu['options-general.php'][20] = array( __( 'Reading' ), 'manage_options', 'options-reading.php' );
  93          $bb_submenu['options-general.php'][25] = array( __( 'Discussion' ), 'manage_options', 'options-discussion.php' );
  94          $bb_submenu['options-general.php'][30] = array( __( 'Permalinks' ), 'manage_options', 'options-permalinks.php' );
  95          $bb_submenu['options-general.php'][35] = array( __( 'WordPress Integration' ), 'manage_options', 'options-wordpress.php' );
  96  
  97      // 350 < Plugin added menu items
  98  
  99      do_action( 'bb_admin_menu_generator' );
 100      ksort( $bb_menu );
 101  
 102      $last_key = false;
 103      foreach ( $bb_menu as $key => $m ) {
 104          if ( $last_key === false || $bb_menu[$last_key][2] === 'separator' ) {
 105              $bb_menu[$key][3] .= ' bb-menu-first';
 106          }
 107          if ( $bb_menu[$key][2] === 'separator' ) {
 108              $bb_menu[$last_key][3] .= ' bb-menu-last';
 109          }
 110          $last_key = $key;
 111          if ( isset( $bb_submenu[$m[2]] ) ) {
 112              ksort( $bb_submenu[$m[2]] );
 113          }
 114      }
 115      $bb_menu[$last_key][3] .= ' bb-menu-last';
 116  }
 117  
 118  function bb_admin_add_menu( $display_name, $capability, $file_name, $menu_position = false, $class = '', $id = '' )
 119  {
 120      global $bb_menu;
 121      global $bb_registered_plugin_callbacks;
 122      if ( empty( $bb_registered_plugin_callbacks ) ) {
 123          $bb_registered_plugin_callbacks = array();
 124      }
 125  
 126      if ( $display_name && $capability && $file_name ) {
 127          // Get an array of the keys
 128          $menu_keys = array_keys( $bb_menu );
 129  
 130          if ( $menu_position ) {
 131              if ( is_numeric( $menu_position ) ) {
 132                  if ( !isset( $bb_menu[$menu_position] ) ) {
 133                      $plugin_menu_next = $menu_position;
 134                  } else {
 135                      return bb_admin_add_menu( $display_name, $capability, $file_name, ( $menu_position + 1 ), $class, $id );
 136                  }
 137              } else {
 138                  // Set the bounds for different menu groups (main or side)
 139                  switch ( $menu_position ) {
 140                      case 'dash':
 141                          $lower = 50;
 142                          $upper = 100;
 143                          break;
 144                      case 'main':
 145                          $lower = 200;
 146                          $upper = 250;
 147                          break;
 148                      default:
 149                          $lower = 350;
 150                          $upper = 500;
 151                          break;
 152                  }
 153  
 154                  // Get an array of all plugin added keys
 155                  $plugin_menu_keys = array_filter( $menu_keys, create_function( '$v', 'if ($v >= ' . $lower . ' && $v < ' . $upper . ') { return $v; }' ) );
 156  
 157                  // If there is an array of keys
 158                  if ( is_array( $plugin_menu_keys ) && count( $plugin_menu_keys ) ) {
 159                      // Get the highest key value and add one
 160                      $plugin_menu_next = max( $plugin_menu_keys ) + 1;
 161                  } else {
 162                      // It's the first one
 163                      $plugin_menu_next = $lower;
 164                  }
 165              }
 166          } else {
 167              $plugin_menu_next = max( array_keys( $bb_menu ) ) + 1;
 168              $bb_menu[$plugin_menu_next] = array( '', 'read', 'separator' );
 169              $plugin_menu_next++;
 170          }
 171  
 172          if ( strpos( $file_name, '.php' ) === false ) {
 173              $bb_registered_plugin_callbacks[] = $file_name;
 174          }
 175  
 176          // Add the menu item at the given key
 177          $bb_menu[$plugin_menu_next] = array( $display_name, $capability, $file_name, $class, $id );
 178  
 179          ksort( $bb_menu );
 180  
 181          return $plugin_menu_next;
 182      }
 183  
 184      return false;
 185  }
 186  
 187  function bb_admin_add_submenu( $display_name, $capability, $file_name, $parent = 'plugins.php' )
 188  {
 189      global $bb_submenu;
 190      global $bb_registered_plugin_callbacks;
 191      if ( empty( $bb_registered_plugin_callbacks ) ) {
 192          $bb_registered_plugin_callbacks = array();
 193      }
 194  
 195      if ( $display_name && $capability && $file_name ) {
 196          if ( strpos( $file_name, '.php' ) === false ) {
 197              $bb_registered_plugin_callbacks[] = $file_name;
 198          }
 199          $bb_submenu[$parent][] = array( $display_name, $capability, $file_name );
 200          ksort( $bb_submenu );
 201      }
 202  }
 203  
 204  function bb_get_current_admin_menu()
 205  {
 206      global $bb_menu, $bb_submenu, $bb_admin_page, $bb_current_menu, $bb_current_submenu;
 207      foreach ( $bb_submenu as $m => $b ) {
 208          foreach ( $b as $s ) {
 209              if ( $s[2] == $bb_admin_page ) {
 210                  $bb_current_submenu = $s;
 211                  $bb_current_menu = $m;
 212                  break;
 213              }
 214          }
 215      }
 216      if ( !isset($bb_current_menu) ) {
 217          $bb_current_menu = $bb_menu[0];
 218          $bb_current_submenu = $bb_submenu['index.php'][5];
 219      } else {
 220          foreach ( $bb_menu as $m ) {
 221              if ( $m[2] == $bb_current_menu ) {
 222                  $bb_current_menu = $m;
 223                  break;
 224              }
 225          }
 226      }
 227      if ( $bb_current_submenu && !bb_current_user_can( $bb_current_submenu[1] ) || !bb_current_user_can( $bb_current_menu[1] ) ) {
 228          wp_redirect( bb_get_uri(null, null, BB_URI_CONTEXT_HEADER) );
 229          exit;
 230      }
 231  }
 232  
 233  function bb_admin_title()
 234  {
 235      global $bb_current_menu, $bb_current_submenu;
 236  
 237      $title = $bb_current_menu[0] . ' &lsaquo; ' . bb_get_option( 'name' ) . ' &#8212; ' . __( 'bbPress' );
 238  
 239      if ( $bb_current_submenu && $bb_current_submenu[0] !== $bb_current_menu[0] ) {
 240          $title = $bb_current_submenu[0] . ' &lsaquo; ' . $title;
 241      }
 242  
 243      echo esc_html( $title );
 244  }
 245  
 246  function bb_admin_menu()
 247  {
 248      global $bb_menu, $bb_submenu, $bb_current_menu, $bb_current_submenu;
 249  
 250      if ( !is_array( $bb_menu ) || !count( $bb_menu ) ) {
 251          return '';
 252      }
 253  
 254      $r = "\t\t\t" . '<ul id="bbAdminMenu">' . "\n";
 255  
 256      foreach ( $bb_menu as $key => $m ) {
 257          if ( !bb_current_user_can( $m[1] ) ) {
 258              continue;
 259          }
 260          $class = 'bb-menu';
 261          if ( isset( $m[3] ) ) {
 262              $class .= ' ' . $m[3];
 263          }
 264          $id = '';
 265          if ( isset( $m[4] ) ) {
 266              $id .= ' id="' . $m[4] . '"';
 267          }
 268          $m[0] = esc_html( $m[0] );
 269          if ( $m[2] === 'separator' ) {
 270              if ( 'f' == bb_get_user_setting( 'fm' ) ) {
 271                  $href = '?foldmenu=0';
 272              } else {
 273                  $href = '?foldmenu=1';
 274              }
 275              $m[0] = '<br />';
 276              $class .= ' bb-menu-separator';
 277          } elseif ( strpos( $m[2], 'http://' ) === 0 || strpos( $m[2], 'https://' ) === 0 ) {
 278              $href = esc_url( $m[2] );
 279              $class .= ' bb-menu-external';
 280          } else {
 281              $href = esc_url( bb_get_option( 'path' ) . 'bb-admin/' . bb_get_admin_tab_link( $m[2] ) );
 282          }
 283          if ( $m[2] == $bb_current_menu[2] ) {
 284              $class .= ' bb-menu-current';
 285          }
 286  
 287          $sr = '';
 288          if ( $m[2] !== 'separator' && isset( $bb_submenu[$m[2]] ) && is_array( $bb_submenu[$m[2]] ) && count( $bb_submenu[$m[2]] ) ) {
 289              $sr .= "\t\t\t\t\t" . '<div class="bb-menu-sub-wrap"><span>' . $m[0] . '</span>' . "\n";
 290              $sr .= "\t\t\t\t\t\t" . '<ul>' . "\n";
 291              $sc = 0;
 292              foreach ( $bb_submenu[$m[2]] as $skey => $sm ) {
 293                  if ( !bb_current_user_can( $sm[1] ) ) {
 294                      continue;
 295                  }
 296                  if ( $sc === 0 && $sm[2] === $m[2] ) {
 297                      $no_submenu = true;
 298                  }
 299                  if ( $sc > 0 ) {
 300                      $no_submenu = false;
 301                  }
 302                  $sc++;
 303                  $sclass = 'bb-menu-sub';
 304                  if ( isset( $sm[3] ) ) {
 305                      $sclass .= ' ' . $sm[3];
 306                  }
 307                  if ( strpos( $sm[2], 'http://' ) === 0 || strpos( $sm[2], 'https://' ) === 0 ) {
 308                      $shref = $sm[2];
 309                      $sclass .= ' bb-menu-external';
 310                  } else {
 311                      $shref = bb_get_option( 'path' ) . 'bb-admin/' . bb_get_admin_tab_link( $sm[2] );
 312                  }
 313                  if ( $sm[2] == $bb_current_submenu[2] ) {
 314                      $sclass .= ' bb-menu-sub-current';
 315                  }
 316                  $sr .= "\t\t\t\t\t\t\t" . '<li class="' . esc_attr( trim( $sclass ) ) . '"><a href="' . esc_url( $shref ) . '">' . esc_html( $sm[0] ) . '</a></li>' . "\n";
 317              }
 318              $sr .= "\t\t\t\t\t\t" . '</ul>' . "\n";
 319              $sr .= "\t\t\t\t\t" . '</div>' . "\n";
 320          }
 321  
 322          if ( $sr && !$no_submenu ) {
 323              $class .= ' bb-menu-has-submenu';
 324              if ( $m[2] == $bb_current_menu[2] ) {
 325                  $class .= ' bb-menu-open';
 326              }
 327          }
 328  
 329          $r .= "\t\t\t\t" . '<li' . $id . ' class="' . esc_attr( trim( $class ) ) . '"><a href="' . $href . '">';
 330  
 331          if ( $m[2] !== 'separator' ) {
 332              $r .= '<div class="bb-menu-icon"></div>';
 333          }
 334  
 335          $r .= '<span>' . $m[0] . '</span></a>' . "\n";
 336  
 337          if ( $sr && !$no_submenu ) {
 338              $r .= '<div class="bb-menu-toggle"></div>';
 339              $r .= $sr;
 340          }
 341  
 342          $r .= "\t\t\t\t" . '</li>' . "\n";
 343      }
 344  
 345      $r .= "\t\t\t" . '</ul>' . "\n";
 346  
 347      echo $r;
 348  }
 349  
 350  function bb_get_admin_tab_link( $tab )
 351  {
 352      if ( is_array( $tab ) ) {
 353          $tab = $tab[2];
 354      }
 355      if ( strpos( $tab, '.php' ) !== false ) {
 356          return $tab;
 357      } else {
 358          return 'admin-base.php?plugin=' . $tab;
 359      }
 360  }
 361  
 362  /* Stats */
 363  
 364  function bb_get_recently_moderated_objects( $num = 5 ) {
 365      $post_query  = new BB_Query( 'post', array( 'per_page' => $num, 'post_status' => '-normal', 'topic_status' => 0 ) ); // post_time != moderation_time;
 366      $topic_query = new BB_Query( 'topic', array( 'per_page' => $num, 'topic_status' => '-normal' ) ); // topic_time == topic_start_time != moderation_time;
 367  
 368      $objects = array();
 369      if ( $post_query->results )
 370          foreach ( array_keys($post_query->results) as $key )
 371              $objects[bb_gmtstrtotime($post_query->results[$key]->post_time)] = array('type' => 'post', 'data' => $post_query->results[$key]);
 372      if ( $topic_query->results )
 373          foreach ( array_keys($topic_query->results) as $key )
 374              $objects[bb_gmtstrtotime($topic_query->results[$key]->topic_time)] = array('type' => 'topic', 'data' => $topic_query->results[$key]);
 375      krsort($objects);
 376      return array_slice($objects, 0, $num);
 377  }
 378  
 379  /* Users */
 380  
 381  function bb_manage_user_fields( $edit_user = '' ) {
 382      global $wp_roles, $wp_users_object, $bbdb;
 383  
 384      // Cap checks
 385      $user_roles    = $wp_roles->role_names;
 386      $can_keep_gate = bb_current_user_can( 'keep_gate' );
 387  
 388      if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) ) {
 389  
 390          bb_check_admin_referer( 'user-manage' );
 391  
 392          // Instantiate required vars
 393          $_POST                   = stripslashes_deep( $_POST );
 394          $create_user_errors      = new WP_Error;
 395  
 396          // User login
 397          $trimmed_user_login      = str_replace( ' ', '', $_POST['user_login'] );
 398          $user_login              = sanitize_user( $_POST['user_login'], true );
 399          $user_meta['first_name'] = $_POST['first_name'];
 400          $user_meta['last_name']  = $_POST['last_name'];
 401          $user_display_name       = $_POST['display_name'];
 402          $user_email              = $_POST['user_email'];
 403          $user_url                = $_POST['user_url'];
 404          $user_meta['from']       = $_POST['from'];
 405          $user_meta['occ']        = $_POST['occ'];
 406          $user_meta['interest']   = $_POST['interest'];
 407          $user_role               = $_POST['userrole'];
 408          $user_meta['throttle']   = $_POST['throttle'];
 409          $user_pass1              = $_POST['pass1'];
 410          $user_pass2              = $_POST['pass2'];
 411          $user_status             = 0;
 412          $user_pass               = false;
 413          $user_url                = $user_url ? bb_fix_link( $user_url ) : '';
 414  
 415          // Check user_login
 416          if ( !isset( $_GET['action'] ) && empty( $user_login ) ) {
 417              $create_user_errors->add( 'user_login', __( 'Username is a required field.' ) );
 418          } else {
 419              if ( $user_login !== $trimmed_user_login ) {
 420                  $create_user_errors->add( 'user_login', sprintf( __( '%s is an invalid username. How\'s this one?' ), esc_html( $_POST['user_login'] ) ) );
 421                  $user_login = $trimmed_user_login;
 422              }
 423          }
 424  
 425          // Check email
 426          if ( isset( $user_email ) && empty( $user_email ) )
 427              $create_user_errors->add( 'user_email', __( 'Email address is a required field.' ) );
 428  
 429          // Password Sanity Check
 430          if ( ( !empty( $user_pass1 ) || !empty( $user_pass2 ) ) && $user_pass1 !== $user_pass2 )
 431              $create_user_errors->add( 'pass', __( 'You must enter the same password twice.' ) );
 432          elseif ( !isset( $_GET['action'] ) && ( empty( $user_pass1 ) && empty( $user_pass2 ) ) )
 433              $create_user_errors->add( 'pass', __( 'You must enter a password.' ) );
 434          elseif ( isset( $_GET['action'] ) && ( empty( $user_pass1 ) && empty( $user_pass2 ) ) )
 435              $user_pass = '';
 436          else
 437              $user_pass = $user_pass1;
 438  
 439          // No errors
 440          if ( !$create_user_errors->get_error_messages() ) {
 441  
 442              // Create or udpate
 443              switch ( $_POST['action'] ) {
 444                  case 'create' :
 445                      $goback = bb_get_uri( 'bb-admin/users.php', array( 'created' => 'true' ), BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN );
 446                      $user   = $wp_users_object->new_user( compact( 'user_login', 'user_email', 'user_url', 'user_nicename', 'user_status', 'user_pass' ) );
 447  
 448                      // Error handler
 449                      if ( is_wp_error( $user ) ) {
 450                          bb_admin_notice( $user );
 451                          unset( $goback );
 452  
 453                      // Update additional user data
 454                      } else {
 455                          // Update caps
 456                          bb_update_usermeta( $user['ID'], $bbdb->prefix . 'capabilities', array( $user_role => true ) );
 457  
 458                          // Update all user meta
 459                          foreach ( $user_meta as $key => $value )
 460                              bb_update_usermeta( $user['ID'], $key, $value );
 461  
 462                          // Don't send email if empty
 463                          if ( !empty( $user_pass ) )
 464                              bb_send_pass( $user['ID'], $user_pass );
 465  
 466                          do_action( 'bb_new_user',    $user['ID'], $user_pass );
 467                      }
 468  
 469                      break;
 470  
 471                  case 'update' :
 472                      $goback = bb_get_uri( 'bb-admin/users.php', array( 'updated' => 'true' ), BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN );
 473                      $user   = $wp_users_object->get_user( $_GET['user_id'], array( 'output' => ARRAY_A ) );
 474                      bb_update_user( $user['ID'], $user_email, $user_url, $user_display_name );
 475  
 476                      // Don't change PW if empty
 477                      if ( !empty( $user_pass ) )
 478                          bb_update_user_password( $user['ID'], $user_pass );
 479  
 480                      // Error handler
 481                      if ( is_wp_error( $user ) ) {
 482                          bb_admin_notice( $user );
 483                          unset( $goback );
 484  
 485                      // Update additional user data
 486                      } else {
 487                          // Update caps
 488                          bb_update_usermeta( $user['ID'], $bbdb->prefix . 'capabilities', array( $user_role => true ) );
 489  
 490                          // Update all user meta
 491                          foreach ( $user_meta as $key => $value )
 492                              bb_update_usermeta( $user['ID'], $key, $value );
 493  
 494                          // Don't send email if empty
 495                          if ( !empty( $user_pass ) )
 496                              bb_send_pass( $user['ID'], $user_pass );
 497  
 498                          do_action( 'bb_update_user', $user['ID'], $user_pass );
 499                      }
 500  
 501                      break;
 502              }
 503  
 504              // Redirect
 505              if ( isset( $goback ) && !empty( $goback ) )
 506                  bb_safe_redirect( $goback );
 507  
 508          // Error handler
 509          } else {
 510              bb_admin_notice( $create_user_errors );
 511          }
 512      } elseif ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' ) {
 513          if ( isset( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ) {
 514              $disabled = true;
 515  
 516              // Get the user
 517              if ( empty( $edit_user ) )
 518                  $edit_user = bb_get_user( bb_get_user_id( $_GET['user_id'] ) );
 519  
 520              // Instantiate required vars
 521              $user_login              = $edit_user->user_login;
 522              $user_meta['first_name'] = $edit_user->first_name;
 523              $user_meta['last_name']  = $edit_user->last_name;
 524              $user_display_name       = $edit_user->display_name;
 525              $user_email              = $edit_user->user_email;
 526              $user_url                = $edit_user->user_url;
 527              $user_meta['from']       = $edit_user->from;
 528              $user_meta['occ']        = $edit_user->occ;
 529              $user_meta['interest']   = $edit_user->interest;
 530              $user_role               = array_search( 'true', $edit_user->capabilities );
 531              $user_meta['throttle']   = $edit_user->throttle;
 532  
 533              // Keymasters can't demote themselves
 534              if ( ( $edit_user->ID == bb_get_current_user_info( 'id' ) && $can_keep_gate ) || ( isset( $edit_user->capabilities ) && is_array( $edit_user->capabilities ) && array_key_exists( 'keymaster', $edit_user->capabilities ) && !$can_keep_gate ) )
 535                  $user_roles = array( 'keymaster' => $user_roles['keymaster'] );
 536  
 537              // only keymasters can promote others to keymaster status
 538              elseif ( !$can_keep_gate )
 539                  unset( $user_roles['keymaster'] );
 540          }
 541      }
 542  
 543      // Load password strength checker
 544      wp_enqueue_script( 'password-strength-meter' );
 545      wp_enqueue_script( 'profile-edit' );
 546  
 547      // Generate a few PW hints
 548      $some_pass_hints = '';
 549      for ( $l = 3; $l != 0; $l-- )
 550          $some_pass_hints .= '<p>' . bb_generate_password() . '</p>';
 551  
 552      // Create  the user fields
 553      $user_fields = array(
 554          'user_login' => array(
 555              'title'    => __( 'Username' ),
 556              'note'     => __( 'Required! Unique identifier for new user.' ),
 557              'value'    => $user_login,
 558              'disabled' => $disabled
 559          ),
 560          'first_name' => array(
 561              'title'    => __( 'First Name' ),
 562              'value'    => $user_meta['first_name']
 563          ),
 564          'last_name' => array(
 565              'title'    => __( 'Last Name' ),
 566              'value'    => $user_meta['last_name']
 567          ),
 568          'display_name' => array(
 569              'title'    => __( 'Display Name' ),
 570              'value'    => $user_display_name
 571          ),
 572          'user_email' => array(
 573              'title'    => __( 'Email' ),
 574              'note'     => __( 'Required! Will be used for notifications and profile settings changes.' ),
 575              'value'    => $user_email
 576          ),
 577          'user_url' => array(
 578              'title'    => __( 'Website' ),
 579              'class'    => array( 'long', 'code' ),
 580              'note'     => __( 'The full URL of user\'s homepage or blog.' ),
 581              'value'    => $user_url
 582          ),
 583          'from' => array(
 584              'title'    => __( 'Location' ),
 585              'class'    => array( 'long' ),
 586              'value'    => $user_meta['from']
 587          ),
 588          'occ' => array(
 589              'title'    => __( 'Occupation' ),
 590              'class'    => array( 'long' ),
 591              'value'    => $user_meta['occ']
 592          ),
 593          'interest' => array(
 594              'title'    => __( 'Interests' ),
 595              'class'    => array( 'long' ),
 596              'value'    => $user_meta['interest']
 597          ),
 598          'userrole' => array(
 599              'title'    => __( 'User Role' ),
 600              'type'     => 'select',
 601              'options'  => $user_roles,
 602              'note'     => __( 'Allow user the above privileges.' ),
 603              'value'    => $user_role,
 604          ),
 605          'pass1' => array(
 606              'title'    => __( 'New Password' ),
 607              'type'     => 'password',
 608              'class'    => array( 'short', 'text', 'code' ),
 609              'note'     => __( 'Hints: ' ) . $some_pass_hints,
 610              'value'    => $user_pass1,
 611          ),
 612          'pass2' => array(
 613              'title'    => __( 'Repeat New Password' ),
 614              'type'     => 'password',
 615              'class'    => array( 'short', 'text', 'code' ),
 616              'note'     => __( 'If you ignore hints, remember: the password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).' ),
 617              'value'    => $user_pass2,
 618          ),
 619          'email_pass' => array(
 620              'title'    => '',
 621              'type'     => 'checkbox',
 622              'options'  => array(
 623                  '1' => array(
 624                      'label'      => __( 'Email the new password.' ),
 625                      'attributes' => array( 'checked' => true )
 626                  )
 627              ),
 628          ),
 629          'pass-strength-fake-input' => array(
 630              'title' => __( 'Password Strength' ),
 631              'type'  => 'hidden',
 632          ),
 633      );
 634  
 635      return apply_filters( 'bb_manage_user_fields', $user_fields );
 636  }
 637  
 638  // Not bbdb::prepared
 639  function bb_get_ids_by_role( $role = 'moderator', $sort = 0, $page = 1, $limit = 50 ) {
 640      global $bbdb, $bb_last_countable_query;
 641      $sort = $sort ? 'DESC' : 'ASC';
 642      $key = $bbdb->escape( $bbdb->prefix . 'capabilities' );
 643  
 644      if ( !$page = abs( (int) $page ) )
 645          $page = 1;
 646      $limit = abs( (int) $limit );
 647  
 648      $limit = ($limit * ($page - 1)) . ", $limit";
 649  
 650      $role = $bbdb->escape_deep($role);
 651  
 652      if ( is_array($role) )
 653          $and_where = "( meta_value LIKE '%" . join("%' OR meta_value LIKE '%", $role) . "%' )";
 654      else
 655          $and_where = "meta_value LIKE '%$role%'";
 656      $bb_last_countable_query = "SELECT user_id FROM $bbdb->usermeta WHERE meta_key = '$key' AND $and_where ORDER BY user_id $sort LIMIT $limit";
 657  
 658      $ids = false;
 659  
 660      $_tuple = compact( 'ids', 'role', 'sort', 'page', 'key', 'limit', 'bb_last_countable_query' );
 661      $_tuple = apply_filters( 'bb_get_ids_by_role', $_tuple );
 662      extract( $_tuple, EXTR_OVERWRITE );
 663  
 664      if ( !$ids ) {
 665          $ids = (array) $bbdb->get_col( $bb_last_countable_query );
 666      }
 667  
 668      if ( $ids ) {
 669          bb_cache_users( $ids );
 670      }
 671  
 672      return $ids;
 673  }
 674  
 675  function bb_user_row( $user, $role = '', $email = false ) {
 676      $actions = "<a href='" . esc_attr( get_user_profile_link( $user->ID ) ) . "'>" . __('View') . "</a>";
 677      $title = '';
 678      if ( bb_current_user_can( 'edit_user', $user_id ) ) {
 679          $actions .= " | <a href='" . esc_attr( bb_get_user_admin_link( $user->ID ) ) . "'>" . __('Edit') . "</a>";
 680          $title = " title='" . esc_attr( sprintf( __( 'User ID: %d' ), $user->ID ) ) . "'";
 681      }
 682      $r  = "\t<tr id='user-$user->ID'" . get_alt_class("user-$role") . ">\n";
 683      $r .= "\t\t<td class=\"user\">" . bb_get_avatar( $user->ID, 32 ) . "<span class=\"row-title\"><a href='" . get_user_profile_link( $user->ID ) . "'" . $title . ">" . get_user_name( $user->ID ) . "</a></span><div><span class=\"row-actions\">$actions</span>&nbsp;</div></td>\n";
 684      $r .= "\t\t<td><a href='" . get_user_profile_link( $user->ID ) . "'>" . get_user_display_name( $user->ID ) . "</a></td>\n";
 685      if ( $email ) {
 686          $email = bb_get_user_email( $user->ID );
 687          $r .= "\t\t<td><a href='mailto:$email'>$email</a></td>\n";
 688      }
 689      
 690      $registered_time = bb_gmtstrtotime( $user->user_registered );
 691      if ( $registered_time < ( time() - 86400 ) ) {
 692          $time = date( 'Y/m/d\<\b\r \/\>H:i:s', bb_offset_time( $registered_time ) );
 693      } else {
 694          $time = sprintf( __( '%s ago' ), bb_since( $registered_time ) );
 695      }
 696      
 697      $r .= "\t\t<td>" . $time . "</td>\n";
 698      
 699      if (
 700          !isset($user->capabilities) ||
 701          !is_array($user->capabilities) ||
 702          empty($user->capabilities)
 703      ) {
 704          $role = array( __('Inactive (no role)') );
 705      } else {
 706          global $wp_roles;
 707          $_roles = $wp_roles->get_names();
 708          $role = array();
 709          foreach ( $user->capabilities as $cap => $cap_set ) {
 710              if (!$cap_set) {
 711                  continue;
 712              }
 713              $role[] = $_roles[$cap];
 714          }
 715          if ( !count( $role ) ) {
 716              $role[] = __('None');
 717          }
 718      }
 719      
 720      $r .= "\t\t<td>" . join(', ', $role) . "</td>\n\t</tr>";
 721      return $r;
 722  }
 723  
 724  // BB_User_Search class
 725  // by Mark Jaquith
 726  
 727  class BB_User_Search {
 728      var $results;
 729      var $search_term;
 730      var $page;
 731      var $raw_page;
 732      var $users_per_page = 50;
 733      var $first_user;
 734      var $last_user;
 735      var $query_limit;
 736      var $total_users_for_query = 0;
 737      var $search_errors;
 738      var $paging_text;
 739      var $paging_text_bottom;
 740  
 741  	function BB_User_Search ($search_term = false, $page = 1, $roles = false ) { // constructor
 742          $this->search_term = $search_term ? stripslashes($search_term) : false;
 743          $this->raw_page = ( '' == $page ) ? false : (int) $page;
 744          $page = (int) $page;
 745          $this->page = $page < 2 ? 1 : $page;
 746          $roles = (array) $roles;
 747          $_roles = array();
 748          foreach ( $roles as $role ) {
 749              if ( false !== $role ) {
 750                  $_roles[] = stripslashes( $role );
 751              }
 752          }
 753          $this->roles = empty( $_roles ) ? false : $_roles;
 754  
 755          $this->prepare_query();
 756          $this->query();
 757          $this->prepare_vars_for_template_usage();
 758          $this->do_paging();
 759      }
 760  
 761  	function prepare_query() {
 762          $this->first_user = ($this->page - 1) * $this->users_per_page;
 763      }
 764  
 765  	function query() {
 766          $users = bb_user_search( array(
 767                  'query' => $this->search_term,
 768                  'user_email' => true,
 769                  'users_per_page' => $this->users_per_page,
 770                  'page' => $this->page,
 771                  'roles' => $this->roles
 772          ) );
 773  
 774          if ( is_wp_error($users) )
 775              $this->search_errors = $users;
 776          else if ( $users )
 777              $this->results = $users;
 778          //    foreach ( (array) $users as $user )
 779          //        $this->results[] = $user->ID;
 780  
 781          if ( $this->results )
 782              $this->total_users_for_query = bb_count_last_query();
 783          elseif ( !is_wp_error($this->search_errors) )
 784              $this->search_errors = new WP_Error( 'no_matching_users_found', __( '<strong>No matching users were found!</strong>' ) );
 785  
 786          if ( is_wp_error( $this->search_errors ) )
 787              bb_admin_notice( $this->search_errors );
 788      }
 789  
 790  	function prepare_vars_for_template_usage() {
 791          $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
 792      }
 793  
 794  	function do_paging() {
 795          global $bb_current_submenu;
 796          $displaying_num = sprintf(
 797              __( '%1$s to %2$s of %3$s' ),
 798              bb_number_format_i18n( ( $this->page - 1 ) * $this->users_per_page + 1 ),
 799              $this->page * $this->users_per_page < $this->total_users_for_query ? bb_number_format_i18n( $this->page * $this->users_per_page ) : '<span class="total-type-count">' . bb_number_format_i18n( $this->total_users_for_query ) . '</span>',
 800              '<span class="total-type-count">' . bb_number_format_i18n( $this->total_users_for_query ) . '</span>'
 801          );
 802          $page_number_links = $this->total_users_for_query > $this->users_per_page ? get_page_number_links( $this->page, $this->total_users_for_query, $this->users_per_page, false ) : '';
 803          $this->paging_text = "<div class='tablenav-pages'><span class='displaying-num'>$displaying_num</span><span class=\"displaying-pages\">$page_number_links</span><div class=\"clear\"></div></div>\n";
 804          $this->paging_text_bottom = "<div class='tablenav-pages'><span class=\"displaying-pages\">$page_number_links</span><div class=\"clear\"></div></div>\n";
 805      }
 806  
 807  	function get_results() {
 808          return (array) $this->results;
 809      }
 810  
 811  	function page_links() {
 812          echo $this->paging_text;
 813      }
 814  
 815  	function results_are_paged() {
 816          if ( isset($this->paging_text) && $this->paging_text )
 817              return true;
 818          return false;
 819      }
 820  
 821  	function is_search() {
 822          if ( $this->search_term )
 823              return true;
 824          return false;
 825      }
 826  
 827  	function display( $show_search = true, $show_email = false ) {
 828          global $wp_roles;
 829  
 830          $r = '';
 831  
 832          if ( isset($this->title) )
 833              $title = $this->title;
 834          elseif ( $this->is_search() )
 835              $title = sprintf(__('Users Matching "%s" by Role'), esc_html( $this->search_term ));
 836  
 837          $h2_search = $this->search_term;
 838          $h2_role   = $this->roles[0];
 839  
 840          $roles = $wp_roles->get_names();
 841          if ( in_array( $h2_role, array_keys( $roles ) ) ) {
 842              $h2_role = $roles[$h2_role];
 843          }
 844  
 845          $h2_search = $h2_search ? ' ' . sprintf( __('containing &#8220;%s&#8221;'), esc_html( $h2_search ) ) : '';
 846          $h2_role  = $h2_role  ? ' ' . sprintf( __('with role &#8220;%s&#8221;'), esc_html( $h2_role ) ) : '';
 847  
 848          $h2_span = '<span class="subtitle">';
 849          $h2_span .= apply_filters( 'bb_user_search_description', sprintf( __( '%1$s%2$s' ), $h2_search, $h2_role ), $h2_search, $h2_role, $this );
 850          $h2_span .= '</span>';
 851  
 852          echo "<h2 class=\"first\">" . apply_filters( 'bb_user_search_title', __('Users') ) . $h2_span . "</h2>\n";
 853          do_action( 'bb_admin_notices' );
 854  
 855          if ( $show_search ) {
 856              $roles = apply_filters( 'bb_user_search_form_roles', $wp_roles->get_names() );
 857              
 858              $r .= "<form action='' method='get' id='search' class='search-form'>\n";
 859              $r .= "<fieldset>\n";
 860              $r .= "<div>\n";
 861              $r .= "\t\t<label for='usersearch'>" . __('Search term') . "</label>";
 862              $r .= "\t\t<div><input type='text' name='usersearch' id='usersearch' class='text-input' value='" . esc_html( $this->search_term, 1) . "' /></div>\n";
 863              $r .= "</div>\n";
 864              $r .= "<div>\n";
 865              $r .= "\t\t<label for='userrole'>" . __('Role') . "</label>";
 866              $r .= "\t\t<div><select name='userrole[]' id='userrole'>\n";
 867              $r .= "\t\t\t<option value=''>" . _x( 'All', 'user roles' ) . "</option>\n";
 868              
 869              foreach ( $roles as $role => $display ) {
 870                  $selected = '';
 871                  if ( is_array( $this->roles ) && in_array( $role, $this->roles ) ) {
 872                      $selected = ' selected="selected"';
 873                  }
 874                  $value = esc_attr($role);
 875                  $display = esc_html(translate($display));
 876                  $r .= "\t\t\t<option value='$value'$selected>$display</option>\n";
 877              }
 878              
 879              $r .= "\t\t</select></div>\n";
 880              $r .= "</div>\n";
 881              
 882              $r = apply_filters( 'bb_user_search_form_inputs', $r, $this );
 883              
 884              $r .= "<div class=\"submit\">\n";
 885              $r .= "\t\t<label class='hidden' for='submit'>" . __('Search') . "</label>";
 886              $r .= "\t\t<div><input type='submit' id='submit' class='button submit-input' value='" . __('Filter') . "' /></div>\n";
 887              $r .= "</div>\n";
 888              $r .= "</fieldset>\n";
 889              $r .= "</form>\n\n";
 890          }
 891  
 892          if ( $this->get_results() ) {
 893              if ( $this->results_are_paged() )
 894                  $r .= "<div class='tablenav'>\n" . $this->paging_text . "</div><div class=\"clear\"></div>\n\n";
 895  
 896              //foreach($roleclasses as $role => $roleclass) {
 897                  //ksort($roleclass);
 898                  //if ( !empty($role) )
 899                  //    $r .= "\t\t<h3>{$wp_roles->role_names[$role]}</h3>\n";
 900                  //else
 901                  //    $r .= "\t\t<h3><em>" . __('Users with no role in these forums') . "</h3>\n";
 902                  $r .= "<table class='widefat'>\n";
 903                  $r .= "<thead>\n";
 904                  $r .= "\t<tr>\n";
 905                  if ( $show_email ) {
 906                      $r .= "\t\t<th style='width:30%;'>" . __('Username') . "</th>\n";
 907                      $r .= "\t\t<th style='width:20%;'>" . __('Name') . "</th>\n";
 908                      $r .= "\t\t<th style='width:20%;'>" . __('E-mail') . "</th>\n";
 909                  } else {
 910                      $r .= "\t\t<th style='width:40%;'>" . __('Username') . "</th>\n";
 911                      $r .= "\t\t<th style='width:30%;'>" . __('Name') . "</th>\n";
 912                  }
 913                  $r .= "\t\t<th style='width:15%;'>" . __('Registered') . "</th>\n";
 914                  $r .= "\t\t<th style='width:15%;'>" . __('Role') . "</th>\n";
 915                  $r .= "\t</tr>\n";
 916                  $r .= "</thead>\n\n";
 917                  $r .= "<tfoot>\n";
 918                  $r .= "\t<tr>\n";
 919                  if ( $show_email ) {
 920                      $r .= "\t\t<th style='width:30%;'>" . __('Username') . "</th>\n";
 921                      $r .= "\t\t<th style='width:20%;'>" . __('Name') . "</th>\n";
 922                      $r .= "\t\t<th style='width:20%;'>" . __('E-mail') . "</th>\n";
 923                  } else {
 924                      $r .= "\t\t<th style='width:40%;'>" . __('Username') . "</th>\n";
 925                      $r .= "\t\t<th style='width:30%;'>" . __('Name') . "</th>\n";
 926                  }
 927                  $r .= "\t\t<th style='width:15%;'>" . __('Registered') . "</th>\n";
 928                  $r .= "\t\t<th style='width:15%;'>" . __('Role') . "</th>\n";
 929                  $r .= "\t</tr>\n";
 930                  $r .= "</tfoot>\n\n";
 931  
 932                  $r .= "<tbody id='role-$role'>\n";
 933                  foreach ( (array) $this->get_results() as $user_object )
 934                      $r .= bb_user_row($user_object, $role, $show_email);
 935                  $r .= "</tbody>\n";
 936                  $r .= "</table>\n\n";
 937              //}
 938  
 939              if ( $this->results_are_paged() )
 940                  $r .= "<div class='tablenav bottom'>\n" . $this->paging_text_bottom . "</div><div class=\"clear\"></div>\n\n";
 941          }
 942          echo $r;
 943      }
 944  
 945  }
 946  
 947  class BB_Users_By_Role extends BB_User_Search {
 948      var $role = '';
 949      var $title = '';
 950  
 951  	function BB_Users_By_Role($role = '', $page = '') { // constructor
 952          $this->role = $role ? $role : 'member';
 953          $this->raw_page = ( '' == $page ) ? false : (int) $page;
 954          $this->page = (int) ( '' == $page ) ? 1 : $page;
 955  
 956          $this->prepare_query();
 957          $this->query();
 958          $this->do_paging();
 959      }
 960  
 961  	function query() {
 962          if ( $_results = bb_get_ids_by_role( $this->role, 0, $this->page, $this->users_per_page ) ) {
 963              $this->results = bb_get_user($_results);
 964              $this->total_users_for_query = bb_count_last_query();
 965          } else
 966              $this->search_errors = new WP_Error( 'no_matching_users_found', __( '<strong>No matching users were found!</strong>' ) );
 967  
 968          if ( is_wp_error( $this->search_errors ) )
 969              bb_admin_notice( $this->search_errors );
 970      }
 971  }
 972  
 973  /* Forums */
 974  
 975  // Expects forum_name, forum_desc to be pre-escaped
 976  function bb_new_forum( $args ) {
 977      global $bbdb;
 978      if ( !bb_current_user_can( 'manage_forums' ) )
 979          return false;
 980  
 981      $defaults = array( 'forum_name' => '', 'forum_desc' => '', 'forum_parent' => 0, 'forum_order' => false, 'forum_is_category' => 0 );
 982      $args = wp_parse_args( $args, $defaults );
 983      if ( 1 < func_num_args() ) : // For back compat
 984          $args['forum_name']  = func_get_arg(0);
 985          $args['forum_desc']  = func_get_arg(1);
 986          $args['forum_order'] = 2 < func_num_args() ? func_get_arg(2) : 0;
 987      endif;
 988  
 989      extract($args, EXTR_SKIP);
 990  
 991      if ( !is_numeric($forum_order) )
 992          $forum_order = (int) $bbdb->get_var("SELECT MAX(forum_order) FROM $bbdb->forums") + 1;
 993  
 994      $forum_order = (int) $forum_order;
 995      $forum_parent = (int) $forum_parent;
 996      $forum_is_category = (int) $forum_is_category;
 997  
 998      $forum_name = apply_filters( 'bb_pre_forum_name', stripslashes( wp_specialchars_decode( $forum_name, ENT_QUOTES ) ) );
 999      $forum_desc = apply_filters( 'bb_pre_forum_desc', stripslashes($forum_desc) );
1000  
1001      if ( strlen($forum_name) < 1 )
1002          return false;
1003  
1004      $forum_sql = "SELECT forum_slug FROM $bbdb->forums WHERE forum_slug = %s";
1005  
1006      $forum_slug = $_forum_slug = bb_slug_sanitize($forum_name);
1007      if ( strlen($_forum_slug) < 1 )
1008          return false;
1009  
1010      while ( is_numeric($forum_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $forum_sql, $forum_slug ) ) )
1011          $forum_slug = bb_slug_increment($_forum_slug, $existing_slug);
1012  
1013      $bbdb->insert( $bbdb->forums, compact( 'forum_name', 'forum_slug', 'forum_desc', 'forum_parent', 'forum_order' ) );
1014      $forum_id = $bbdb->insert_id;
1015      if ($forum_id && $forum_is_category)
1016          bb_update_forummeta($forum_id, 'forum_is_category', $forum_is_category);
1017      wp_cache_flush( 'bb_forums' );
1018  
1019      return $forum_id;
1020  }
1021  
1022  // Expects forum_name, forum_desc to be pre-escaped
1023  function bb_update_forum( $args ) {
1024      global $bbdb;
1025      if ( !bb_current_user_can( 'manage_forums' ) )
1026          return false;
1027  
1028      $defaults = array( 'forum_id' => 0, 'forum_name' => '', 'forum_slug' => '', 'forum_desc' => '', 'forum_parent' => 0, 'forum_order' => 0, 'forum_is_category' => 0 );
1029      $fields = array( 'forum_name', 'forum_desc', 'forum_parent', 'forum_order' );
1030      $args = wp_parse_args( $args, $defaults );
1031      if ( 1 < func_num_args() ) : // For back compat
1032          $args['forum_id']    = func_get_arg(0);
1033          $args['forum_name']  = func_get_arg(1);
1034          $args['forum_desc']  = 2 < func_num_args() ? func_get_arg(2) : '';
1035          $args['forum_order'] = 3 < func_num_args() && is_numeric(func_get_arg(3)) ? func_get_arg(3) : 0;
1036      endif;
1037  
1038      extract($args, EXTR_SKIP);
1039  
1040      if ( !$forum_id = (int) $forum_id )
1041          return false;
1042      if ( !$forum = bb_get_forum( $forum_id ) )
1043          return false;
1044      $forum_order = (int) $forum_order;
1045      $forum_parent = (int) $forum_parent;
1046      $forum_is_category = (int) $forum_is_category;
1047  
1048      $forum_name = apply_filters( 'bb_pre_forum_name', stripslashes( wp_specialchars_decode( $forum_name, ENT_QUOTES ) ), $forum_id );
1049      $forum_desc = apply_filters( 'bb_pre_forum_desc', stripslashes($forum_desc), $forum_id );
1050  
1051      if ( strlen($forum_name) < 1 )
1052          return false;
1053  
1054      // Slug is not changing, don't update it
1055      if ( !$forum_slug || $forum_slug == $forum->forum_slug ) {
1056          // [sic]
1057      } else {
1058          $forum_slug = $_forum_slug = bb_slug_sanitize($forum_slug);
1059          if ( strlen($_forum_slug) < 1 )
1060              return false;
1061  
1062          $forum_sql = "SELECT forum_slug FROM $bbdb->forums WHERE forum_slug = %s";
1063  
1064          while ( is_numeric($forum_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $forum_sql, $forum_slug ) ) )
1065              $forum_slug = bb_slug_increment($_forum_slug, $existing_slug);
1066  
1067          $fields[] = 'forum_slug';
1068      }
1069  
1070      wp_cache_delete( $forum_id, 'bb_forum' );
1071      wp_cache_flush( 'bb_forums' );
1072  
1073      $update_result = $bbdb->update( $bbdb->forums, compact( $fields ), compact( 'forum_id' ) );
1074  
1075      if ($forum_is_category)
1076          bb_update_forummeta($forum_id, 'forum_is_category', $forum_is_category);
1077      else
1078          bb_delete_forummeta($forum_id, 'forum_is_category');
1079  
1080      return $update_result;
1081  }
1082  
1083  // When you delete a forum, you delete *everything*
1084  // NOT bbdb::prepared
1085  function bb_delete_forum( $forum_id ) {
1086      global $bbdb;
1087      if ( !bb_current_user_can( 'delete_forum', $forum_id ) )
1088          return false;
1089      if ( !$forum_id = (int) $forum_id )
1090          return false;
1091  
1092      if ( !$forum = bb_get_forum( $forum_id ) )
1093          return false;
1094  
1095      if ( $topic_ids = $bbdb->get_col( $bbdb->prepare( "SELECT topic_id FROM $bbdb->topics WHERE forum_id = %d", $forum_id ) ) ) {
1096          foreach ($topic_ids as $topic_id) {
1097              bb_remove_topic_tags( $topic_id );
1098          }
1099          $_topic_ids = join(',', array_map('intval', $topic_ids));
1100          $bbdb->query("DELETE FROM $bbdb->posts WHERE topic_id IN ($_topic_ids) AND topic_id != 0");
1101          $bbdb->query("DELETE FROM $bbdb->meta WHERE object_type = 'bb_topic' AND object_id IN ($_topic_ids)");
1102          $bbdb->query( $bbdb->prepare( "DELETE FROM $bbdb->topics WHERE forum_id = %d", $forum_id ) );
1103      }
1104      
1105      $bbdb->update( $bbdb->forums, array( 'forum_parent' => $forum->forum_parent ), array( 'forum_parent' => $forum_id ) );
1106  
1107      $return = $bbdb->query( $bbdb->prepare( "DELETE FROM $bbdb->forums WHERE forum_id = %d", $forum_id ) );
1108  
1109      wp_cache_flush( 'bb_post' );
1110  
1111      if ( $topic_ids )
1112          foreach ( $topic_ids as $topic_id ) {
1113              // should maybe just flush these groups instead
1114              wp_cache_delete( $topic_id, 'bb_topic' );
1115              wp_cache_delete( $topic_id, 'bb_thread' );
1116          }
1117  
1118      wp_cache_delete( $forum_id, 'bb_forum' );
1119      wp_cache_flush( 'bb_forums' );
1120  
1121      return $return;
1122  }
1123  
1124  function bb_forum_row( $forum_id = 0, $echo = true, $close = false ) {
1125      global $forum, $forums_count;
1126      if ( $forum_id )
1127          $_forum = bb_get_forum( $forum_id );
1128      else
1129          $_forum =& $forum;
1130  
1131      if ( !$_forum )
1132          return;
1133  
1134      $description = get_forum_description( $_forum->forum_id );
1135  
1136      $r  = '';
1137      if ( $close )
1138          $r .= "\t<li id='forum-$_forum->forum_id'" . get_alt_class( 'forum', 'forum clear list-block' ) . ">\n";
1139      $r .= "\t\t<div class='list-block posrel'>\n";
1140      $r .= "\t\t\t<div class=\"row-title\">" . get_forum_name( $_forum->forum_id ) . "</div>\n";
1141      if ( $description )
1142          $r .= "\t\t\t<p class=\"row-description\">" . get_forum_description( $_forum->forum_id ) . "</p>\n";
1143      $r .= "\t\t\t<div class=\"row-actions\"><span>\n";
1144          $r .= "\t\t\t\t<a class='edit' href='" . get_forum_link() . "'>" . __('View') . "</a>\n";
1145      if ( bb_current_user_can( 'manage_forums' ) )
1146          $r .= "\t\t\t\t| <a class='edit' href='" . esc_attr( bb_get_uri('bb-admin/forums.php', array('action' => 'edit', 'id' => $_forum->forum_id), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN) ) . "'>" . __('Edit') . "</a>\n";
1147      if ( bb_current_user_can( 'delete_forum', $_forum->forum_id ) && 1 < $forums_count )
1148          $r .= "\t\t\t\t| <a class='delete' href='" . esc_attr( bb_get_uri('bb-admin/forums.php', array('action' => 'delete', 'id' => $_forum->forum_id), BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN) ) . "'>" . __('Delete') . "</a>\n";
1149      $r .= "\t\t\t</span>&nbsp;</div>\n";
1150      $r .= "\t\t</div>\n";
1151      if ( $close )
1152          $r .= "\t</li>\n";
1153  
1154      if ( $echo )
1155          echo $r;
1156      return $r;
1157  }
1158  
1159  function bb_forum_form( $forum_id = 0 ) {
1160      $forum_id = (int) $forum_id;
1161      if ( $forum_id && !$forum = bb_get_forum( $forum_id ) ) {
1162          return;
1163      }
1164  
1165      $forum_name = '';
1166      $forum_slug = '';
1167      $forum_description = '';
1168      $forum_position = '';
1169  
1170      if ( $forum_id ) {
1171          $forum_name = get_forum_name( $forum_id );
1172              $forum_slug = apply_filters('editable_slug', $forum->forum_slug); 
1173          $forum_description = get_forum_description( $forum_id );
1174          $forum_position = get_forum_position( $forum_id );
1175          $legend = __( 'Edit Forum' );
1176          $submit = __( 'Save Changes' );
1177          $action = 'update';
1178      } else {
1179          $legend = __( 'Add Forum' );
1180          $submit = __( 'Add Forum' );
1181          $action = 'add';
1182      }
1183  
1184      $forum_options = array(
1185          'forum_name' => array(
1186              'title' => __( 'Name' ),
1187              'value' => $forum_name
1188          ),
1189          'forum_slug' => array(
1190              'title' => __( 'Slug' ),
1191              'value' => $forum_slug
1192          ),
1193          'forum_desc' => array(
1194              'title' => __( 'Description' ),
1195              'value' => $forum_description,
1196              'class' => 'long'
1197          ),
1198          'forum_parent' => array(
1199              'title' => __( 'Parent' ),
1200              'type' => 'select',
1201              'options' => bb_get_forum_dropdown( array(
1202                  'cut_branch' => $forum_id,
1203                  'id' => 'forum_parent',
1204                  'none' => true,
1205                  'selected' => $forum_id ? get_forum_parent( $forum_id ) : 0,
1206                  'disable_categories' => 0,
1207                  'options_only' => true
1208              ) )
1209          ),
1210          'forum_order' => array(
1211              'title' => __( 'Position' ),
1212              'value' => $forum_position,
1213              'class' => 'short'
1214          ),
1215          'forum_is_category' => array(
1216              'title' => __( 'Category' ),
1217              'type' => 'checkbox',
1218              'options' => array(
1219                  1 => array(
1220                      'label' => __( 'Make this forum a category' ),
1221                      'value' => bb_get_forum_is_category( $forum_id ),
1222                  )
1223              ),
1224              'note' => __( 'Categories are forums where new topics cannot be created. Categories usually contain a group of sub-forums.' )
1225          )
1226      );
1227      
1228      if ( !$forum_id ) {
1229          unset( $forum_options['forum_slug'] );
1230          unset( $forum_options['forum_order'] );
1231      }
1232      
1233  ?>
1234  <form class="settings" method="post" id="<?php echo $action; ?>-forum" action="<?php bb_uri('bb-admin/bb-forum.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN); ?>" class="add:forum-list: forum-form">
1235      <fieldset>
1236          <legend><?php echo $legend; ?></legend>
1237  <?php
1238  foreach ( $forum_options as $option => $args ) {
1239      bb_option_form_element( $option, $args );
1240  }
1241  ?>
1242          <fieldset class="submit">
1243  <?php if ( $forum_id ) : ?>
1244              <input type="hidden" name="forum_id" value="<?php echo $forum_id; ?>" />
1245  <?php endif; ?>
1246              <?php bb_nonce_field( 'order-forums', 'order-nonce' ); ?>
1247              <?php bb_nonce_field( $action . '-forum' ); ?>
1248              <input type="hidden" name="action" value="<?php echo $action; ?>" />
1249              <input class="submit" type="submit" name="submit" value="<?php echo $submit; ?>" />
1250          </fieldset>
1251      </fieldset>
1252  </form>
1253  <?php
1254  }
1255  
1256  class BB_Walker_ForumAdminlistitems extends BB_Walker {
1257      var $tree_type = 'forum';
1258      var $db_fields = array ('parent' => 'forum_parent', 'id' => 'forum_id'); //TODO: decouple this
1259      
1260  	function start_lvl($output, $depth) {
1261          $indent = str_repeat("\t", $depth) . '    ';
1262          $output .= $indent . "<ul id='forum-root-$this->forum_id' class='list-block holder'>\n";
1263          return $output;
1264      }
1265      
1266  	function end_lvl($output, $depth) {
1267          $indent = str_repeat("\t", $depth) . '    ';
1268          $output .= $indent . "</ul>\n";
1269          return $output;
1270      }
1271      
1272  	function start_el($output, $forum, $depth) {
1273          $this->forum_id = $forum->forum_id;
1274          $indent = str_repeat("\t", $depth + 1);
1275          $output .= $indent . "<li id='forum-$this->forum_id'" . get_alt_class( 'forum', 'forum clear list-block' ) . ">\n";
1276  
1277          return $output;
1278      }
1279      
1280  	function end_el($output, $forum, $depth) {
1281          $indent = str_repeat("\t", $depth + 1);
1282          $output .= $indent . "</li>\n";
1283          return $output;
1284      }
1285  }
1286  
1287  
1288  
1289  /* Topics */
1290  
1291  function bb_move_forum_topics( $from_forum_id, $to_forum_id ) {
1292      global $bbdb;
1293      
1294      $from_forum_id = (int) $from_forum_id ;
1295      $to_forum_id = (int) $to_forum_id;
1296      
1297      add_filter('get_forum_where', 'bb_no_where'); // Just in case
1298      
1299      $from_forum = bb_get_forum( $from_forum_id );
1300      if ( !$to_forum = bb_get_forum( $to_forum_id ) )
1301          return false;
1302  
1303      $posts = $to_forum->posts + ( $from_forum ? $from_forum->posts : 0 );
1304      $topics = $to_forum->topics + ( $from_forum ? $from_forum->topics : 0 );
1305      
1306      $bbdb->update( $bbdb->forums, compact( 'topics', 'posts' ), array( 'forum_id' => $to_forum_id ) );
1307      $bbdb->update( $bbdb->forums, array( 'topics' => 0, 'posts' => 0 ), array( 'forum_id' => $from_forum_id ) );
1308      $bbdb->update( $bbdb->posts, array( 'forum_id' => $to_forum_id ), array( 'forum_id' => $from_forum_id ) );
1309      $topic_ids = $bbdb->get_col( $bbdb->prepare( "SELECT topic_id FROM $bbdb->topics WHERE forum_id = %d", $from_forum_id ) );
1310      $return = $bbdb->update( $bbdb->topics, array( 'forum_id' => $to_forum_id ), array( 'forum_id' => $from_forum_id ) );
1311  
1312      wp_cache_flush( 'bb_post' );
1313  
1314      if ( $topic_ids )
1315          foreach ( $topic_ids as $topic_id ) {
1316              // should maybe just flush these groups
1317              wp_cache_delete( $topic_id, 'bb_topic' );
1318              wp_cache_delete( $topic_id, 'bb_thread' );
1319          }
1320  
1321      wp_cache_delete( $from_forum_id, 'bb_forum' );
1322      wp_cache_delete( $to_forum_id, 'bb_forum' );
1323      wp_cache_flush( 'bb_forums' );
1324      
1325      return $return;
1326  }
1327  
1328  /* Posts */
1329  
1330  function bb_admin_list_posts() {
1331      global $bb_posts, $bb_post;
1332      
1333      if ( !$bb_posts ) {
1334  ?>
1335  <p class="no-results"><?php _e('No posts found.'); ?></p>
1336  <?php
1337      } else {
1338  ?>
1339  <table id="posts-list" class="widefat" cellspacing="0" cellpadding="0">
1340  <thead>
1341      <tr>
1342          <th scope="col" class="check-column"><input type="checkbox" /></th>
1343          <th scope="col"><?php _e( 'Post' ); ?></th>
1344          <th scope="col"><?php _e( 'Author' ); ?></th>
1345          <th scope="col"><?php _e( 'Topic' ); ?></th>
1346          <th scope="col"><?php _e( 'Date' ); ?></th>
1347      </tr>
1348  </thead>
1349  <tfoot>
1350      <tr>
1351          <th scope="col" class="check-column"><input type="checkbox" /></th>
1352          <th scope="col"><?php _e( 'Post' ); ?></th>
1353          <th scope="col"><?php _e( 'Author' ); ?></th>
1354          <th scope="col"><?php _e( 'Topic' ); ?></th>
1355          <th scope="col"><?php _e( 'Date' ); ?></th>
1356      </tr>
1357  </tfoot>
1358  <tbody>
1359  <?php
1360          foreach ( $bb_posts as $bb_post ) {
1361  ?>
1362      <tr id="post-<?php post_id(); ?>"<?php alt_class('post', post_del_class()); ?>>
1363          <td class="check-column"><input type="checkbox" name="post[]" value="<?php post_id(); ?>" /></td>
1364          <td class="post">
1365              <?php post_text(); ?>
1366              <div>
1367                  <span class="row-actions">
1368                      <a href="<?php echo esc_url( get_post_link() ); ?>"><?php _e( 'View' ); ?></a>
1369  <?php
1370      bb_post_admin( array(
1371          'before_each' => ' | ',
1372          'each' => array(
1373              'undelete' => array(
1374                  'before' => ' '
1375              )
1376          ),
1377          'last_each' => array(
1378              'before' => ' | '
1379          )
1380      ) );
1381  ?>
1382                  </span>&nbsp;
1383              </div>
1384          </td>
1385  
1386          <td class="author">
1387              <?php if ( get_post_author_id() ) : ?>
1388  
1389                  <a href="<?php user_profile_link( get_post_author_id() ); ?>">
1390                      <?php post_author_avatar( '16' ); ?>
1391                      <?php post_author(); ?>
1392                  </a>
1393  
1394              <?php else : ?>
1395  
1396                  <span>
1397                      <?php post_author_avatar( '16' ); ?>
1398                      <?php post_author(); ?>
1399                  </span>
1400  
1401              <?php endif; ?>
1402          </td>
1403  
1404          <td class="topic">
1405              <a href="<?php topic_link( $bb_post->topic_id ); ?>"><?php topic_title( $bb_post->topic_id ); ?></a>
1406          </td>
1407          
1408          <td class="date">
1409  <?php
1410      if ( bb_get_post_time( 'U' ) < ( time() - 86400 ) ) {
1411          bb_post_time( 'Y/m/d\<\b\r \/\>H:i:s' );
1412      } else {
1413          printf( __( '%s ago' ), bb_get_post_time( 'since' ) );
1414      }
1415  ?>
1416          </td>
1417      </tr>
1418  <?php 
1419          }
1420  ?>
1421  </tbody>
1422  </table>
1423  <?php
1424      }
1425  }
1426  
1427  /* Recounts */
1428  
1429  function bb_recount_list()
1430  {
1431      global $recount_list;
1432      $recount_list = array(
1433          5  => array( 'topic-posts', __( 'Count posts of every topic' ) ),
1434          6  => array( 'topic-voices', __( 'Count voices of every topic' ) ),
1435          10 => array( 'topic-deleted-posts', __( 'Count deleted posts on every topic' ) ),
1436          15 => array( 'forums', __( 'Count topics and posts in every forum' ) ),
1437          20 => array( 'topics-replied', __( 'Count topics to which each user has replied' ) ),
1438          25 => array( 'topic-tag-count', __( 'Count tags for every topic' ) ),
1439          30 => array( 'tags-tag-count', __( 'Count topics for every tag' ) ),
1440          35 => array( 'tags-delete-empty', __( 'Delete tags with no topics' ) ),
1441          40 => array( 'clean-favorites', __( 'Remove deleted topics from users\' favorites' ) )
1442      );
1443      do_action( 'bb_recount_list' );
1444      ksort( $recount_list );
1445      return $recount_list;
1446  }
1447  
1448  /* Themes */
1449  
1450  function bb_get_current_theme_data( $property = 'all' ) {
1451      if (!$property) {
1452          $property = 'all';
1453      }
1454      $directory = bb_get_active_theme_directory();
1455      $stylesheet = $directory . 'style.css';
1456      if (file_exists($stylesheet)) {
1457          $data = bb_get_theme_data($stylesheet);
1458      }
1459      if ($property == 'all') {
1460          return $data;
1461      } elseif (isset($data[$property])) {
1462          return $data[$property];
1463      } else {
1464          return false;
1465      }
1466  }
1467  
1468  // Output sanitized for display
1469  function bb_get_theme_data( $theme_file )
1470  {
1471      if ( strpos($theme_file, '#') !== false ) {
1472          $theme_file = bb_get_theme_directory( $theme_file ) . 'style.css';
1473      }
1474      $theme_code = implode( '', file( $theme_file ) );
1475      $theme_code = str_replace ( '\r', '\n', $theme_code );
1476      // Grab just the first commented area from the file
1477      preg_match( '|/\*(.*)\*/|msU', $theme_code, $theme_block );
1478      $theme_data = trim( $theme_block[1] );
1479      preg_match( '|Theme Name:(.*)|i', $theme_data, $theme_name );
1480      preg_match( '|Theme URI:(.*)|i', $theme_data, $theme_uri );
1481      preg_match( '|Description:(.*)|i', $theme_data, $description );
1482      preg_match( '|Author:(.*)|i', $theme_data, $author_name );
1483      preg_match( '|Author URI:(.*)|i', $theme_data, $author_uri );
1484      preg_match( '|Ported By:(.*)|i', $theme_data, $porter_name );
1485      preg_match( '|Porter URI:(.*)|i', $theme_data, $porter_uri );
1486  //    preg_match( '|Template:(.*)|i', $theme_data, $template );
1487      if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
1488          $version = esc_html( trim( $version[1] ) );
1489      else
1490          $version ='';
1491      if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
1492          $status = esc_html( trim($status[1]) );
1493      else
1494          $status = 'publish';
1495  
1496      $description = trim($description[1]);
1497      $description = bb_encode_bad( $description );
1498      $description = bb_code_trick( $description );
1499      $description = force_balance_tags( $description );
1500      $description = bb_filter_kses( $description );
1501      $description = bb_autop( $description );
1502  
1503      $name = $theme_name[1];
1504      $name = esc_html( trim($name) );
1505      $theme = $name;
1506  
1507      if ( $author_name || $author_uri ) {
1508          if ( empty($author_uri[1]) ) {
1509              $author = bb_filter_kses( trim($author_name[1]) );
1510          } else {
1511              $author = '<a href="' . esc_url( trim($author_uri[1]) ) . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . bb_filter_kses( trim($author_name[1]) ) . '</a>';
1512          }
1513      } else {
1514          $author = '';
1515      }
1516  
1517      if ( $porter_name || $porter_uri ) {
1518          if ( empty($porter_uri[1]) ) {
1519              $porter = bb_filter_kses( trim($porter_name[1]) );
1520          } else {
1521              $porter = '<a href="' . esc_url( trim($porter_uri[1]) ) . '" title="' . esc_attr__( 'Visit porter homepage' ) . '">' . bb_filter_kses( trim($porter_name[1]) ) . '</a>';
1522          }
1523      } else {
1524          $porter = '';
1525      }
1526  
1527      global $bb;
1528  
1529      // Normalise the path to the theme
1530      $theme_file = str_replace( '\\', '/', $theme_file );
1531  
1532      foreach ( $bb->theme_locations as $_name => $_data ) {
1533          $_directory = str_replace( '\\', '/', $_data['dir'] );
1534          if ( 0 === strpos( $theme_file, $_directory ) ) {
1535              $location = $_name;
1536              break;
1537          }
1538      }
1539  
1540      return array(
1541          'Location' => $location,
1542          'Name' => $name,
1543          'Title' => $theme,
1544          'Description' => $description,
1545          'Author' => $author,
1546          'Porter' => $porter,
1547          'Version' => $version,
1548  //        'Template' => $template[1],
1549          'Status' => $status,
1550          'URI' => esc_url( $theme_uri[1] )
1551      );
1552  }
1553  
1554  if ( !function_exists( 'checked' ) ) :
1555  function checked( $checked, $current) {
1556      if ( $checked == $current)
1557          echo ' checked="checked"';
1558  }
1559  endif;
1560  
1561  if ( !function_exists( 'selected' ) ) :
1562  function selected( $selected, $current) {
1563      if ( $selected === $current)
1564          echo ' selected="selected"';
1565  }
1566  endif;
1567  
1568  /* Options */
1569  
1570  function bb_option_form_element( $name = 'name', $args = null ) {
1571      global $bb_hardcoded;
1572  
1573      $defaults = array(
1574          'title' => 'title',
1575          'type' => 'text',
1576          'value' => false,
1577          'options' => false,
1578          'message' => false,
1579          'class' => false,
1580          'default' => false,
1581          'before' => '',
1582          'after' => '',
1583          'note' => false,
1584          'attributes' => false,
1585          'disabled' => false,
1586      );
1587  
1588      $args = wp_parse_args( $args, $defaults );
1589  
1590      $id = str_replace( array( '_', '[', ']' ), array( '-', '-', '' ), $name );
1591      if ( false !== strpos( $name, '[' ) ) {
1592          list( $option_name, $option_key ) = preg_split( '/[\[\]]/', $name, -1, PREG_SPLIT_NO_EMPTY );
1593          $option = bb_get_option( $option_name );
1594          $value = false === $args['value'] ? esc_attr( $option[$option_key] ) : esc_attr( $args['value'] );
1595          $hardcoded = isset( $bb_hardcoded[$option_name][$option_key] );
1596      } else {
1597          $value = false === $args['value'] ? bb_get_form_option( $name ) : esc_attr( $args['value'] );
1598          $hardcoded = isset( $bb_hardcoded[$name] );
1599      }
1600  
1601      $class = $args['class'] ? (array) $args['class'] : array();
1602      array_unshift( $class, $args['type'] );
1603  
1604      if ( $hardcoded || $args['disabled'] )
1605          $disabled = ' disabled="disabled"';
1606      else
1607          $disabled = false;
1608  
1609      if ( $args['attributes'] ) {
1610          $attributes = array();
1611          foreach ( $args['attributes'] as $k => $v )
1612              $attributes[] = "$k='$v'";
1613          $attributes = ' ' . join( ' ', $attributes );
1614      } else {
1615          $attributes = '';
1616      }
1617  
1618  ?>
1619  
1620          <div id="option-<?php echo $id; ?>"<?php if ( !empty( $disabled ) ) echo ' class="disabled"'; ?>>
1621  <?php
1622              switch ( $args['type'] ) {
1623                  case 'radio' :
1624                  case 'checkbox' :
1625                  case 'message' :
1626  ?>
1627              <div class="label">
1628                  <?php echo $args['title']; ?>
1629              </div>
1630  
1631  <?php
1632                      break;
1633                  case 'select' :
1634                  default :
1635  ?>
1636              <label for="<?php echo $id; ?>">
1637                  <?php echo $args['title']; ?>
1638              </label>
1639  
1640  <?php
1641                      break;
1642              }
1643  ?>
1644              <div class="inputs">
1645  
1646  <?php
1647              if ( $args['before'] ) {
1648                  echo '<span class="before">' . $args['before'] . '</span>';
1649              }
1650              switch ( $args['type'] ) {
1651                  case 'select' :
1652                      echo "<select$disabled class='" . join( ' ', $class ) . "' name='$name' id='$id'$attributes>\n";
1653                      if ( is_array( $args['options'] ) ) {
1654                          foreach ( $args['options'] as $option => $label )
1655                              echo "\t<option value='$option'" . ( $value == $option ? " selected='selected'" : '' ) . ">$label</option>\n";
1656                      } elseif ( is_string( $args['options'] ) ) {
1657                          echo $args['options'] . "\n";
1658                      }
1659                      echo "</select>\n";
1660                      break;
1661                  case 'radio' :
1662                  case 'checkbox' :
1663                      if ( is_array( $args['options'] ) ) {
1664                          $_id = 0;
1665                          if ( 'radio' === $args['type'] && !in_array( $value, array_keys( $args['options'] ) ) && empty( $value ) ) {
1666                              $use_first_value = true;
1667                          }
1668                          $type = $args['type'];
1669                          foreach ( $args['options'] as $option => $label ) {
1670                              if ( $use_first_value ) {
1671                                  $use_first_value = false;
1672                                  $value = $option;
1673                              }
1674                              if ( is_array( $label ) ) {
1675                                  if ( isset( $label['attributes'] ) ) {
1676                                      $attributes = array();
1677                                      foreach ( $label['attributes'] as $k => $v )
1678                                          $attributes[] = "$k='$v'";
1679                                      $attributes = ' ' . join( ' ', $attributes );
1680                                  } else {
1681                                      $attributes = '';
1682                                  }
1683                                  if ( isset( $label['name'] ) ) {
1684                                      $name = $label['name'];
1685                                      $id = str_replace( array( '_', '[', ']' ), array( '-', '-', '' ), $name );
1686                                      $hardcoded = isset( $bb_hardcoded[$name] );
1687                                  }
1688                                  if ( isset( $label['value'] ) ) {
1689                                      $_value = $label['value'];
1690                                  } else {
1691                                      $_value = $args['value'];
1692                                  }
1693                                  $value = false === $_value ? bb_get_form_option( $name ) : esc_attr( $_value );
1694                                  $label = $label['label'];
1695                              }
1696                              echo "<label class=\"{$type}s\"><input$disabled type='$type' class='" . join( ' ', $class ) . "' name='$name' id='$id-$_id' value='$option'" . ( $value == $option ? " checked='checked'" : '' ) . "{$attributes} /> $label</label>\n";
1697                              $_id++;
1698                          }
1699                      } elseif ( is_string( $args['options'] ) ) {
1700                          echo $args['options'] . "\n";
1701                      }
1702                      break;
1703                  case 'message' :
1704                      if ( $args['message'] ) {
1705                          echo $args['message'];
1706                      }
1707                      break;
1708                  default :
1709                      echo "<input$disabled type='$args[type]' class='" . join( ' ', $class ) . "' name='$name' id='$id' value='$value'$attributes />\n";
1710                      break;
1711              }
1712              if ( $args['after'] ) {
1713                  echo '<span class="after">' . $args['after'] . '</span>';
1714              }
1715  
1716              if ( $args['note'] ) {
1717                  foreach ( (array) $args['note'] as $note ) {
1718  ?>
1719  
1720                  <p><?php echo $note; ?></p>
1721  
1722  <?php
1723                  }
1724              }
1725  ?>
1726  
1727              </div>
1728          </div>
1729  
1730  <?php
1731  }


Generated: Mon May 20 03:58:35 2013 Hosted by follow the white rabbit.