PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/svntrunk/bp-forums/bp-forums-admin.php

https://bitbucket.org/simplemediacode/bptrunk
PHP | 419 lines | 335 code | 65 blank | 19 comment | 29 complexity | 5d6f21944f2ec00b7e8c8aaf80125558 MD5 | raw file
  1. <?php
  2. // Exit if accessed directly
  3. if ( !defined( 'ABSPATH' ) ) exit;
  4. function bp_forums_add_admin_menu() {
  5. if ( !is_super_admin() )
  6. return false;
  7. $page = bp_core_do_network_admin() ? 'settings.php' : 'options-general.php';
  8. // Add the administration tab under the "Site Admin" tab for site administrators
  9. $hook = add_submenu_page( $page, __( 'Forums', 'buddypress' ), __( 'Forums', 'buddypress' ), 'manage_options', 'bb-forums-setup', "bp_forums_bbpress_admin" );
  10. // Fudge the highlighted subnav item when on the BuddyPress Forums admin page
  11. add_action( "admin_head-$hook", 'bp_core_modify_admin_menu_highlight' );
  12. }
  13. add_action( bp_core_admin_hook(), 'bp_forums_add_admin_menu' );
  14. /**
  15. * Outputs the markup for the bb-forums-admin panel
  16. */
  17. function bp_forums_bbpress_admin() {
  18. // The text and URL of the Site Wide Forums button differs depending on whether bbPress
  19. // is running
  20. if ( is_plugin_active( 'bbpress/bbpress.php' ) ) {
  21. // The bbPress admin page will always be on the root blog. switch_to_blog() will
  22. // pass through if we're already there.
  23. switch_to_blog( bp_get_root_blog_id() );
  24. $button_url = admin_url( add_query_arg( array( 'page' => 'bbpress' ), 'options-general.php' ) );
  25. restore_current_blog();
  26. $button_text = __( 'Configure Site Wide Forums', 'buddypress' );
  27. } else {
  28. $button_url = bp_get_admin_url( add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => 'bbpress', 'TB_iframe' => 'true', 'width' => '640', 'height' => '500' ), 'plugin-install.php' ) );
  29. $button_text = __( 'Install Site Wide Forums', 'buddypress' );
  30. }
  31. $action = bp_get_admin_url( 'admin.php?page=bb-forums-setup&reinstall=1' ); ?>
  32. <div class="wrap">
  33. <?php screen_icon( 'buddypress' ); ?>
  34. <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Forums', 'buddypress' ) ); ?></h2>
  35. <?php if ( isset( $_POST['submit'] ) ) : ?>
  36. <div id="message" class="updated fade">
  37. <p><?php _e( 'Settings Saved.', 'buddypress' ) ?></p>
  38. </div>
  39. <?php endif; ?>
  40. <?php
  41. if ( isset( $_REQUEST['reinstall'] ) || !bp_forums_is_installed_correctly() ) :
  42. // Delete the bb-config.php location option
  43. bp_delete_option( 'bb-config-location' );
  44. bp_forums_bbpress_install_wizard();
  45. else : ?>
  46. <div style="width: 45%; float: left; margin-top: 20px;">
  47. <h3><?php _e( '(Installed)', 'buddypress' ); ?> <?php _e( 'Forums for Groups', 'buddypress' ) ?></h3>
  48. <p><?php _e( 'Give each individual group its own discussion forum. Choose this if you\'d like to keep your members\' conversations separated into distinct areas.' , 'buddypress' ); ?></p>
  49. <p class="description"><?php _e( 'You may use an existing bbPress installation if you have one.', 'buddypress' ); ?></p>
  50. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  51. <ul class="description" style="list-style: square; margin-left: 30px;">
  52. <li><?php _e( 'Group Integration', 'buddypress' ); ?></p></li>
  53. <li><?php _e( 'Member Profile Integration', 'buddypress' ); ?></p></li>
  54. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  55. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  56. </ul>
  57. <div>
  58. <a class="button button-primary" href="<?php echo $action ?>"><?php _e( 'Uninstall Group Forums', 'buddypress' ) ?></a> &nbsp;
  59. </div>
  60. </div>
  61. <div style="width: 45%; float: left; margin: 20px 0 20px 20px; padding: 0 20px 20px 20px; border: 1px solid #ddd; background-color: #fff;">
  62. <h3><?php _e( 'New! Site Wide Forums', 'buddypress' ) ?></h3>
  63. <p><?php _e( 'Your site will have central forums that are not isolated to any specific group. Choose this if you\'d like to have a central forum area for your members.', 'buddypress' ) ?></p>
  64. <p class="description"><?php _e( 'You may activate both Group and Site Wide forums, but this may create a poor experience for your members.', 'buddypress' ) ?></p>
  65. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  66. <ul class="description" style="list-style: square; margin-left: 30px;">
  67. <li><?php _e( 'Central Discussion Area', 'buddypress' ); ?></p></li>
  68. <li><?php _e( 'Forum Plugins Available', 'buddypress' ); ?></p></li>
  69. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  70. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  71. </ul>
  72. <div>
  73. <a class="button thickbox button-primary" href="<?php echo esc_attr( $button_url ) ?>"><?php echo esc_html( $button_text ) ?></a> &nbsp;
  74. </div>
  75. </div>
  76. <?php endif; ?>
  77. <p class="clear description"><?php printf( __( 'Need help deciding between Group Forums and Site Wide Forums? Visit <a href="%s">the BuddyPress codex</a> for more information.', 'buddypress' ), 'http://codex.buddypress.org/getting-started/installing-group-and-sitewide-forums/' ) ?></p>
  78. </div>
  79. <?php
  80. }
  81. function bp_forums_bbpress_install_wizard() {
  82. $post_url = bp_get_admin_url( 'admin.php?page=bb-forums-setup' );
  83. $bbpress_plugin_is_active = false;
  84. $step = isset( $_REQUEST['step'] ) ? $_REQUEST['step'] : '';
  85. // The text and URL of the Site Wide Forums button differs depending on whether bbPress
  86. // is running
  87. if ( is_plugin_active( 'bbpress/bbpress.php' ) ) {
  88. $bbpress_plugin_is_active = true;
  89. // The bbPress admin page will always be on the root blog. switch_to_blog() will
  90. // pass through if we're already there.
  91. switch_to_blog( bp_get_root_blog_id() );
  92. $button_url = admin_url( add_query_arg( array( 'page' => 'bbpress' ), 'options-general.php' ) );
  93. restore_current_blog();
  94. $button_text = __( 'Configure Site Wide Forums', 'buddypress' );
  95. } else {
  96. $button_url = bp_get_admin_url( add_query_arg( array( 'tab' => 'plugin-information', 'plugin' => 'bbpress', 'TB_iframe' => 'true', 'width' => '640', 'height' => '500' ), 'plugin-install.php' ) );
  97. $button_text = __( 'Install Site Wide Forums', 'buddypress' );
  98. }
  99. switch( $step ) {
  100. case 'existing':
  101. if ( isset( $_REQUEST['doinstall'] ) && ( 1 == (int) $_REQUEST['doinstall'] ) ) {
  102. if ( !bp_forums_configure_existing_install() ) {
  103. _e( 'The bb-config.php file was not found at that location, please try again.', 'buddypress' );
  104. } else {
  105. ?>
  106. <h3><?php _e( 'Forums were set up correctly using your existing bbPress install!', 'buddypress' ) ?></h3>
  107. <p><?php _e( 'BuddyPress will now use its internal copy of bbPress to run the forums on your site. If you wish, you can remove your old bbPress installation files, as long as you keep the bb-config.php file in the same location.', 'buddypress' ) ?></p><?php
  108. }
  109. } else { ?>
  110. <form action="" method="post">
  111. <h3><?php _e( 'Existing bbPress Installation', 'buddypress' ) ?></h3>
  112. <p><?php _e( "BuddyPress can make use of your existing bbPress install. Just provide the location of your <code>bb-config.php</code> file, and BuddyPress will do the rest.", 'buddypress' ) ?></p>
  113. <p><label><code>bb-config.php</code> file location:</label><br /><input style="width: 50%" type="text" name="bbconfigloc" id="bbconfigloc" value="<?php echo str_replace( 'buddypress', '', $_SERVER['DOCUMENT_ROOT'] ) ?>" /></p>
  114. <p><input type="submit" class="button-primary" value="<?php _e( 'Complete Installation', 'buddypress' ) ?>" /></p>
  115. <input type="hidden" name="step" value="existing" />
  116. <input type="hidden" name="doinstall" value="1" />
  117. <?php wp_nonce_field( 'bp_forums_existing_install_init' ) ?>
  118. </form>
  119. <?php
  120. }
  121. break;
  122. case 'new':
  123. if ( isset( $_REQUEST['doinstall'] ) && 1 == (int)$_REQUEST['doinstall'] ) {
  124. $result = bp_forums_bbpress_install();
  125. switch ( $result ) {
  126. case 1:
  127. _e( '<p>All done! Configuration settings have been saved to the file <code>bb-config.php</code> in the root of your WordPress install.</p>', 'buddypress' );
  128. break;
  129. default:
  130. // Just write the contents to screen
  131. _e( '<p>A configuration file could not be created. No problem, but you will need to save the text shown below into a file named <code>bb-config.php</code> in the root directory of your WordPress installation before you can start using the forum functionality.</p>', 'buddypress' ); ?>
  132. <textarea style="display:block; margin-top: 30px; width: 80%;" rows="50"><?php echo htmlspecialchars( $result ); ?></textarea>
  133. <?php
  134. break;
  135. }
  136. } else { ?>
  137. <h3><?php _e( 'New bbPress Installation', 'buddypress' ) ?></h3>
  138. <p><?php _e( "You've decided to set up a new installation of bbPress for forum management in BuddyPress. This is very simple and is usually just a one click
  139. process. When you're ready, hit the link below.", 'buddypress' ) ?></p>
  140. <p><a class="button-primary" href="<?php echo wp_nonce_url( $post_url . '&step=new&doinstall=1', 'bp_forums_new_install_init' ) ?>"><?php _e( 'Complete Installation', 'buddypress' ) ?></a></p>
  141. <?php
  142. }
  143. break;
  144. default:
  145. if ( !file_exists( BP_PLUGIN_DIR . '/bp-forums/bbpress/' ) ) { ?>
  146. <div id="message" class="error">
  147. <p><?php printf( __( 'bbPress files were not found. To install the forums component you must download a copy of bbPress and make sure it is in the folder: "%s"', 'buddypress' ), 'wp-content/plugins/buddypress/bp-forums/bbpress/' ) ?></p>
  148. </div>
  149. <?php } else {
  150. // Include the plugin install
  151. add_thickbox();
  152. wp_enqueue_script( 'plugin-install' );
  153. wp_admin_css( 'plugin-install' );
  154. ?>
  155. <div style="width: 45%; float: left; margin-top: 20px;">
  156. <h3><?php _e( 'Forums for Groups', 'buddypress' ) ?></h3>
  157. <p><?php _e( 'Give each individual group its own discussion forum. Choose this if you\'d like to keep your members\' conversations separated into distinct areas.' , 'buddypress' ); ?></p>
  158. <p class="description"><?php _e( 'You may use an existing bbPress installation if you have one.', 'buddypress' ); ?></p>
  159. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  160. <ul class="description" style="list-style: square; margin-left: 30px;">
  161. <li><?php _e( 'Group Integration', 'buddypress' ); ?></p></li>
  162. <li><?php _e( 'Member Profile Integration', 'buddypress' ); ?></p></li>
  163. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  164. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  165. </ul>
  166. <div>
  167. <a class="button button-primary" href="<?php echo $post_url . '&step=new' ?>"><?php _e( 'Install Group Forums', 'buddypress' ) ?></a> &nbsp;
  168. <a class="button" href="<?php echo $post_url . '&step=existing' ?>"><?php _e( 'Use Existing Installation', 'buddypress' ) ?></a>
  169. </div>
  170. </div>
  171. <div style="width: 45%; float: left; margin: 20px 0 20px 20px; padding: 0 20px 20px 20px; border: 1px solid #ddd; background-color: #fff;">
  172. <h3><?php _e( 'New! Site Wide Forums', 'buddypress' ) ?></h3>
  173. <p><?php _e( 'Your site will have central forums that are not isolated to any specific group. Choose this if you\'d like to have a central forum area for your members.', 'buddypress' ) ?></p>
  174. <p class="description"><?php _e( 'You may activate both Group and Site Wide forums, but this may create a poor experience for your members.', 'buddypress' ) ?></p>
  175. <h4 style="margin-bottom: 10px;"><?php _e( 'Features', 'buddypress' ); ?></h4>
  176. <ul class="description" style="list-style: square; margin-left: 30px;">
  177. <li><?php _e( 'Central Discussion Area', 'buddypress' ); ?></p></li>
  178. <li><?php _e( 'Forum Plugins Available', 'buddypress' ); ?></p></li>
  179. <li><?php _e( 'Activity Stream Integration', 'buddypress' ); ?></p></li>
  180. <li><?php _e( '@ Mention Integration', 'buddypress' ); ?></p></li>
  181. </ul>
  182. <div>
  183. <a class="button button-primary <?php if ( ! $bbpress_plugin_is_active ) { echo esc_attr( 'thickbox' ); }?>" href="<?php echo esc_attr( $button_url ) ?>"><?php echo esc_html( $button_text ) ?></a> &nbsp;
  184. </div>
  185. </div>
  186. <?php }
  187. break;
  188. }
  189. }
  190. function bp_forums_configure_existing_install() {
  191. global $wpdb, $bbdb;
  192. check_admin_referer( 'bp_forums_existing_install_init' );
  193. // Sanitize $_REQUEST['bbconfigloc']
  194. $_REQUEST['bbconfigloc'] = apply_filters( 'bp_forums_bbconfig_location', $_REQUEST['bbconfigloc'] );
  195. if ( false === strpos( $_REQUEST['bbconfigloc'], 'bb-config.php' ) ) {
  196. if ( '/' != substr( $_REQUEST['bbconfigloc'], -1, 1 ) )
  197. $_REQUEST['bbconfigloc'] .= '/';
  198. $_REQUEST['bbconfigloc'] .= 'bb-config.php';
  199. }
  200. bp_update_option( 'bb-config-location', $_REQUEST['bbconfigloc'] );
  201. if ( !file_exists( $_REQUEST['bbconfigloc'] ) ) {
  202. return false;
  203. }
  204. return true;
  205. }
  206. function bp_forums_bbpress_install( $location = '' ) {
  207. global $wpdb, $bbdb, $bp;
  208. check_admin_referer( 'bp_forums_new_install_init' );
  209. if ( empty( $location ) ) {
  210. $location = ABSPATH . 'bb-config.php';
  211. }
  212. // Create the bb-config.php file
  213. $initial_write = bp_forums_bbpress_write(
  214. BP_PLUGIN_DIR . '/bp-forums/bbpress/bb-config-sample.php',
  215. $location,
  216. array(
  217. "define( 'BBDB_NAME'," => array( "'bbpress'", "'" . DB_NAME . "'" ),
  218. "define( 'BBDB_USER'," => array( "'username'", "'" . DB_USER . "'" ),
  219. "define( 'BBDB_PASSWO" => array( "'password'", "'" . DB_PASSWORD . "'" ),
  220. "define( 'BBDB_HOST'," => array( "'localhost'", "'" . DB_HOST . "'" ),
  221. "define( 'BBDB_CHARSE" => array( "'utf8'", "'" . DB_CHARSET . "'" ),
  222. "define( 'BBDB_COLLAT" => array( "''", "'" . DB_COLLATE . "'" ),
  223. "define( 'BB_AUTH_KEY" => array( "'put your unique phrase here'", "'" . addslashes( AUTH_KEY ) . "'" ),
  224. "define( 'BB_SECURE_A" => array( "'put your unique phrase here'", "'" . addslashes( SECURE_AUTH_KEY ) . "'" ),
  225. "define( 'BB_LOGGED_I" => array( "'put your unique phrase here'", "'" . addslashes( LOGGED_IN_KEY ) . "'" ),
  226. "define( 'BB_NONCE_KE" => array( "'put your unique phrase here'", "'" . addslashes( NONCE_KEY ) . "'" ),
  227. "\$bb_table_prefix = '" => array( "'bb_'", "'" . $bp->table_prefix . "bb_'" ),
  228. "define( 'BB_LANG', '" => array( "''", "'" . get_locale() . "'" )
  229. )
  230. );
  231. // Add the custom user and usermeta entries to the config file
  232. if ( $initial_write == 1 ) {
  233. $file = file_get_contents( $location );
  234. } else {
  235. $file = &$initial_write;
  236. }
  237. $file = trim( $file );
  238. if ( '?>' == substr( $file, -2, 2 ) ) {
  239. $file = substr( $file, 0, -2 );
  240. }
  241. $file .= "\n" . '$bb->custom_user_table = \'' . $wpdb->users . '\';';
  242. $file .= "\n" . '$bb->custom_user_meta_table = \'' . $wpdb->usermeta . '\';';
  243. $file .= "\n\n" . '$bb->uri = \'' . BP_PLUGIN_URL . '/bp-forums/bbpress/\';';
  244. $file .= "\n" . '$bb->name = \'' . get_blog_option( bp_get_root_blog_id(), 'blogname' ) . ' ' . __( 'Forums', 'buddypress' ) . '\';';
  245. if ( is_multisite() ) {
  246. $file .= "\n" . '$bb->wordpress_mu_primary_blog_id = ' . bp_get_root_blog_id() . ';';
  247. }
  248. if ( defined( 'AUTH_SALT' ) ) {
  249. $file .= "\n\n" . 'define(\'BB_AUTH_SALT\', \'' . addslashes( AUTH_SALT ) . '\');';
  250. }
  251. if ( defined( 'LOGGED_IN_SALT' ) ) {
  252. $file .= "\n" . 'define(\'BB_LOGGED_IN_SALT\', \'' . addslashes( LOGGED_IN_SALT ) . '\');';
  253. }
  254. if ( defined( 'SECURE_AUTH_SALT' ) ) {
  255. $file .= "\n" . 'define(\'BB_SECURE_AUTH_SALT\', \'' . addslashes( SECURE_AUTH_SALT ) . '\');';
  256. }
  257. $file .= "\n\n" . 'define(\'WP_AUTH_COOKIE_VERSION\', 2);';
  258. $file .= "\n\n" . '?>';
  259. if ( $initial_write == 1 ) {
  260. $file_handle = fopen( $location, 'w' );
  261. fwrite( $file_handle, $file );
  262. fclose( $file_handle );
  263. } else {
  264. $initial_write = $file;
  265. }
  266. bp_update_option( 'bb-config-location', $location );
  267. return $initial_write;
  268. }
  269. function bp_forums_bbpress_write( $file_source, $file_target, $alterations ) {
  270. if ( empty( $file_source ) || !file_exists( $file_source ) || !is_file( $file_source ) ) {
  271. return -1;
  272. }
  273. if ( empty( $file_target ) ) {
  274. $file_target = $file_source;
  275. }
  276. if ( empty( $alterations ) || !is_array( $alterations ) ) {
  277. return -2;
  278. }
  279. // Get the existing lines in the file
  280. $lines = file( $file_source );
  281. // Initialise an array to store the modified lines
  282. $modified_lines = array();
  283. // Loop through the lines and modify them
  284. foreach ( (array) $lines as $line ) {
  285. if ( isset( $alterations[substr( $line, 0, 20 )] ) ) {
  286. $alteration = $alterations[substr( $line, 0, 20 )];
  287. $modified_lines[] = str_replace( $alteration[0], $alteration[1], $line );
  288. } else {
  289. $modified_lines[] = $line;
  290. }
  291. }
  292. $writable = true;
  293. if ( file_exists( $file_target ) ) {
  294. if ( !is_writable( $file_target ) ) {
  295. $writable = false;
  296. }
  297. } else {
  298. $dir_target = dirname( $file_target );
  299. if ( file_exists( $dir_target ) ) {
  300. if ( !is_writable( $dir_target ) || !is_dir( $dir_target ) ) {
  301. $writable = false;
  302. }
  303. } else {
  304. $writable = false;
  305. }
  306. }
  307. if ( empty( $writable ) ) {
  308. return trim( join( null, $modified_lines ) );
  309. }
  310. // Open the file for writing - rewrites the whole file
  311. $file_handle = fopen( $file_target, 'w' );
  312. // Write lines one by one to avoid OS specific newline hassles
  313. foreach ( (array) $modified_lines as $modified_line ) {
  314. if ( strlen( $modified_line ) - 2 === strrpos( $modified_line, '?>' ) ) {
  315. $modified_line = '?>';
  316. }
  317. fwrite( $file_handle, $modified_line );
  318. if ( $modified_line == '?>' ) {
  319. break;
  320. }
  321. }
  322. // Close the config file
  323. fclose( $file_handle );
  324. @chmod( $file_target, 0666 );
  325. return 1;
  326. }
  327. ?>