/bp-forums/bbpress/bb-admin/includes/functions.bb-admin.php

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