Changeset 5524 for trunk/bp-core/bp-core-functions.php
- Timestamp:
- 12/12/2011 02:33:04 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-functions.php
r5510 r5524 253 253 } 254 254 255 function bp_core_do_network_admin() {256 $do_network_admin = false;257 258 if ( is_multisite() && !bp_is_multiblog_mode() )259 $do_network_admin = true;260 261 return apply_filters( 'bp_core_do_network_admin', $do_network_admin );262 }263 264 function bp_core_admin_hook() {265 $hook = bp_core_do_network_admin() ? 'network_admin_menu' : 'admin_menu';266 267 return apply_filters( 'bp_core_admin_hook', $hook );268 }269 270 /**271 * Initializes the wp-admin area "BuddyPress" menus and sub menus.272 *273 * @package BuddyPress Core274 * @uses bp_current_user_can() returns true if the current user is a site admin, false if not275 */276 function bp_core_admin_menu_init() {277 278 // Bail if not in admin279 if ( !is_admin() )280 return false;281 282 // Bail if user cannot moderate283 if ( !bp_current_user_can( 'bp_moderate' ) )284 return false;285 286 add_action( bp_core_admin_hook(), 'bp_core_add_admin_menu', 9 );287 288 require ( BP_PLUGIN_DIR . '/bp-core/admin/bp-core-admin.php' );289 }290 add_action( 'bp_init', 'bp_core_admin_menu_init' );291 292 /**293 * In BP 1.6, the top-level admin menu was removed. For backpat, this function294 * keeps the top-level menu if a plugin has registered a menu into the old295 * 'bp-general-settings' menu.296 *297 * The old "bp-general-settings" page was renamed "bp-general-config".298 *299 * @global array $_parent_pages300 * @global array $_registered_pages301 * @global array $submenu302 * @since 1.6303 */304 function bp_core_admin_backpat_menu() {305 global $_parent_pages, $_registered_pages, $submenu;306 307 if ( ! is_super_admin() )308 return;309 310 // Don't do anything if a BP upgrade is in progress, or if the bp-wizard is in progress.311 if ( defined( 'BP_IS_UPGRADE' ) && BP_IS_UPGRADE || empty( $submenu['bp-general-settings'] ) )312 return;313 314 /**315 * By default, only the core "Help" submenu is added under the top-level BuddyPress menu.316 * This means that if no third-party plugins have registered their admin pages into the317 * 'bp-general-settings' menu, it will only contain one item. Kill it.318 */319 if ( 1 == count( $submenu['bp-general-settings'] ) ) {320 // This removes the top-level menu321 remove_submenu_page( 'bp-general-settings', 'bp-general-settings' );322 remove_menu_page( 'bp-general-settings' );323 324 // These stop people accessing the URL directly325 unset( $_parent_pages['bp-general-settings'] );326 unset( $_registered_pages['toplevel_page_bp-general-settings'] );327 }328 }329 add_action( bp_core_admin_hook(), 'bp_core_admin_backpat_menu', 999 );330 331 /**332 * Registers BuddyPress' admin menus.333 *334 * @since 1.0335 */336 function bp_core_add_admin_menu() {337 if ( ! bp_current_user_can( 'bp_moderate' ) )338 return;339 340 /**341 * Don't add this version of the admin menu if a BP upgrade is in progress.342 * See bp_core_update_add_admin_menu().343 */344 if ( defined( 'BP_IS_UPGRADE' ) && BP_IS_UPGRADE )345 return;346 347 $hooks = array();348 $page = bp_core_do_network_admin() ? 'settings.php' : 'options-general.php';349 350 // Changed in BP 1.6 . See bp_core_admin_backpat_menu()351 $hooks[] = add_menu_page( __( 'BuddyPress', 'buddypress' ), __( 'BuddyPress', 'buddypress' ), 'manage_options', 'bp-general-settings', 'bp_core_admin_backpat_menu', '' );352 $hooks[] = add_submenu_page( 'bp-general-settings', __( 'BuddyPress Help', 'buddypress' ), __( 'Help', 'buddypress' ), 'manage_options', 'bp-general-settings', 'bp_core_admin_backpat_page' );353 354 // Add the option pages355 $hooks[] = add_submenu_page( $page, __( 'BuddyPress Components', 'buddypress' ), __( 'BuddyPress', 'buddypress' ), 'manage_options', 'bp-general-config', 'bp_core_admin_component_setup' );356 $hooks[] = add_submenu_page( $page, __( 'BuddyPress Pages', 'buddypress' ), __( 'BuddyPress Pages', 'buddypress' ), 'manage_options', 'bp-page-settings', 'bp_core_admin_page_setup' );357 $hooks[] = add_submenu_page( $page, __( 'BuddyPress Settings', 'buddypress' ), __( 'BuddyPress Settings', 'buddypress' ), 'manage_options', 'bp-settings', 'bp_core_admin_settings' );358 359 foreach( $hooks as $hook ) {360 // Add a hook for common BP admin CSS/JS scripts361 add_action( "admin_print_styles-$hook", 'bp_core_add_admin_menu_styles' );362 363 // Fudge the highlighted subnav item when on a BuddyPress admin page364 add_action( "admin_head-$hook", 'bp_core_modify_admin_menu_highlight' );365 }366 }367 368 /**369 * Tweak the Settings subnav menu to show only one BuddyPress menu item (Settings > BuddyPress).370 *371 * @since 1.6372 */373 function bp_core_modify_admin_menu() {374 $page = bp_core_do_network_admin() ? 'settings.php' : 'options-general.php';375 376 remove_submenu_page( $page, 'bb-forums-setup' );377 remove_submenu_page( $page, 'bp-page-settings' );378 remove_submenu_page( $page, 'bp-settings' );379 }380 add_action( "admin_head", 'bp_core_modify_admin_menu', 999 );381 382 /**383 * This tells WP to highlight the Settings > BuddyPress menu item,384 * regardless of which actual BuddyPress admin screen we are on.385 *386 * The conditional prevents the behaviour when the user is viewing the387 * backpat "Help" page, the Activity page, or any third-party plugins.388 *389 * @global string $plugin_page390 * @global array $submenu391 * @since 1.6392 */393 function bp_core_modify_admin_menu_highlight() {394 global $plugin_page, $submenu_file;395 396 // This tweaks the Settings subnav menu to show only one BuddyPress menu item397 if ( ! in_array( $plugin_page, array( 'bp-activity', 'bp-general-settings', ) ) )398 $submenu_file = 'bp-general-config';399 }400 401 /**402 * Print admin messages to admin_notices or network_admin_notices403 *404 * BuddyPress combines all its messages into a single notice, to avoid a preponderance of yellow405 * boxes.406 *407 * @package BuddyPress Core408 * @since 1.5409 *410 * @global object $bp Global BuddyPress settings object411 * @uses bp_current_user_can() to check current user permissions before showing the notices412 * @uses bp_is_root_blog()413 */414 function bp_core_print_admin_notices() {415 global $bp;416 417 // Only the super admin should see messages418 if ( !bp_current_user_can( 'bp_moderate' ) )419 return;420 421 // On multisite installs, don't show on the Site Admin of a non-root blog, unless422 // do_network_admin is overridden423 if ( is_multisite() && bp_core_do_network_admin() && !bp_is_root_blog() )424 return;425 426 // Show the messages427 if ( !empty( $bp->admin->notices ) ) {428 ?>429 <div id="message" class="updated fade">430 <?php foreach( $bp->admin->notices as $notice ) : ?>431 <p><?php echo $notice ?></p>432 <?php endforeach ?>433 </div>434 <?php435 }436 }437 add_action( 'admin_notices', 'bp_core_print_admin_notices' );438 add_action( 'network_admin_notices', 'bp_core_print_admin_notices' );439 440 /**441 * Add an admin notice to the BP queue442 *443 * Messages added with this function are displayed in BuddyPress's general purpose admin notices444 * box. It is recommended that you hook this function to admin_init, so that your messages are445 * loaded in time.446 *447 * @package BuddyPress Core448 * @since 1.5449 *450 * @global object $bp Global BuddyPress settings object451 * @param string $notice The notice you are adding to the queue452 */453 function bp_core_add_admin_notice( $notice ) {454 global $bp;455 456 if ( empty( $bp->admin->notices ) ) {457 $bp->admin->notices = array();458 }459 460 $bp->admin->notices[] = $notice;461 }462 463 /**464 * Verify that some BP prerequisites are set up properly, and notify the admin if not465 *466 * On every Dashboard page, this function checks the following:467 * - that pretty permalinks are enabled468 * - that a BP-compatible theme is activated469 * - that every BP component that needs a WP page for a directory has one470 * - that no WP page has multiple BP components associated with it471 * The administrator will be shown a notice for each check that fails.472 *473 * @package BuddyPress Core474 */475 function bp_core_activation_notice() {476 global $wp_rewrite, $wpdb, $bp;477 478 // Only the super admin gets warnings479 if ( !bp_current_user_can( 'bp_moderate' ) )480 return;481 482 // On multisite installs, don't load on a non-root blog, unless do_network_admin is483 // overridden484 if ( is_multisite() && bp_core_do_network_admin() && !bp_is_root_blog() )485 return;486 487 // Don't show these messages during setup or upgrade488 if ( !empty( $bp->maintenance_mode ) )489 return;490 491 /**492 * Check to make sure that the blog setup routine has run. This can't happen during the493 * wizard because of the order which the components are loaded. We check for multisite here494 * on the off chance that someone has activated the blogs component and then disabled MS495 */496 if ( bp_is_active( 'blogs' ) ) {497 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$bp->blogs->table_name}" ) );498 499 if ( !$count )500 bp_blogs_record_existing_blogs();501 }502 503 /**504 * Are pretty permalinks enabled?505 */506 if ( isset( $_POST['permalink_structure'] ) )507 return false;508 509 if ( empty( $wp_rewrite->permalink_structure ) ) {510 bp_core_add_admin_notice( sprintf( __( '<strong>BuddyPress is almost ready</strong>. You must <a href="%s">update your permalink structure</a> to something other than the default for it to work.', 'buddypress' ), admin_url( 'options-permalink.php' ) ) );511 }512 513 /**514 * Are you using a BP-compatible theme?515 */516 517 // Get current theme info518 $ct = current_theme_info();519 520 // The best way to remove this notice is to add a "buddypress" tag to521 // your active theme's CSS header.522 if ( !defined( 'BP_SILENCE_THEME_NOTICE' ) && !in_array( 'buddypress', (array)$ct->tags ) ) {523 bp_core_add_admin_notice( sprintf( __( "You'll need to <a href='%s'>activate a <strong>BuddyPress-compatible theme</strong></a> to take advantage of all of BuddyPress's features. We've bundled a default theme, but you can always <a href='%s'>install some other compatible themes</a> or <a href='%s'>update your existing WordPress theme</a>.", 'buddypress' ), admin_url( 'themes.php' ), network_admin_url( 'theme-install.php?type=tag&s=buddypress&tab=search' ), network_admin_url( 'plugin-install.php?type=term&tab=search&s=%22bp-template-pack%22' ) ) );524 }525 526 /**527 * Check for orphaned BP components (BP component is enabled, no WP page exists)528 */529 530 $orphaned_components = array();531 $wp_page_components = array();532 533 // Only components with 'has_directory' require a WP page to function534 foreach( $bp->loaded_components as $component_id => $is_active ) {535 if ( !empty( $bp->{$component_id}->has_directory ) ) {536 $wp_page_components[] = array(537 'id' => $component_id,538 'name' => isset( $bp->{$component_id}->name ) ? $bp->{$component_id}->name : ucwords( $bp->{$component_id}->id )539 );540 }541 }542 543 // Activate and Register are special cases. They are not components but they need WP pages.544 // If user registration is disabled, we can skip this step.545 if ( bp_get_signup_allowed() ) {546 $wp_page_components[] = array(547 'id' => 'activate',548 'name' => __( 'Activate', 'buddypress' )549 );550 551 $wp_page_components[] = array(552 'id' => 'register',553 'name' => __( 'Register', 'buddypress' )554 );555 }556 557 foreach( $wp_page_components as $component ) {558 if ( !isset( $bp->pages->{$component['id']} ) ) {559 $orphaned_components[] = $component['name'];560 }561 }562 563 if ( !empty( $orphaned_components ) ) {564 $admin_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) );565 $notice = sprintf( __( 'The following active BuddyPress Components do not have associated WordPress Pages: %2$s. <a href="%1$s" class="button-secondary">Repair</a>', 'buddypress' ), $admin_url, '<strong>' . implode( '</strong>, <strong>', $orphaned_components ) . '</strong>' );566 567 bp_core_add_admin_notice( $notice );568 }569 570 /**571 * BP components cannot share a single WP page. Check for duplicate assignments, and post572 * a message if found.573 */574 $dupe_names = array();575 $page_ids = (array)bp_core_get_directory_page_ids();576 $dupes = array_diff_assoc( $page_ids, array_unique( $page_ids ) );577 578 if ( !empty( $dupes ) ) {579 foreach( $dupes as $dupe_component => $dupe_id ) {580 $dupe_names[] = $bp->pages->{$dupe_component}->title;581 }582 583 // Make sure that there are no duplicate duplicates :)584 $dupe_names = array_unique( $dupe_names );585 }586 587 // If there are duplicates, post a message about them588 if ( !empty( $dupe_names ) ) {589 $admin_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) );590 $notice = sprintf( __( 'Each BuddyPress Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %2$s. <a href="%1$s" class="button-secondary">Repair</a>', 'buddypress' ), $admin_url, '<strong>' . implode( '</strong>, <strong>', $dupe_names ) . '</strong>' );591 592 bp_core_add_admin_notice( $notice );593 }594 }595 add_action( 'admin_init', 'bp_core_activation_notice' );596 597 255 /** 598 256 * Returns the domain for the root blog. … … 1084 742 1085 743 /** 1086 * When switching from single to multisite we need to copy blog options to1087 * site options.1088 *1089 * @package BuddyPress Core1090 * @todo Does this need to be here anymore after the introduction of bp_get_option etc?1091 */1092 function bp_core_activate_site_options( $keys = array() ) {1093 global $bp;1094 1095 if ( !empty( $keys ) && is_array( $keys ) ) {1096 $errors = false;1097 1098 foreach ( $keys as $key => $default ) {1099 if ( empty( $bp->site_options[ $key ] ) ) {1100 $bp->site_options[ $key ] = bp_get_option( $key, $default );1101 1102 if ( !bp_update_option( $key, $bp->site_options[ $key ] ) )1103 $errors = true;1104 }1105 }1106 1107 if ( empty( $errors ) )1108 return true;1109 }1110 1111 return false;1112 }1113 1114 /**1115 * BuddyPress uses common options to store configuration settings. Many of these1116 * settings are needed at run time. Instead of fetching them all and adding many1117 * initial queries to each page load, let's fetch them all in one go.1118 *1119 * @package BuddyPress Core1120 * @todo Use settings API and audit these methods1121 */1122 function bp_core_get_root_options() {1123 global $wpdb;1124 1125 // These options come from the root blog options table1126 $root_blog_options = apply_filters( 'bp_core_site_options', array(1127 1128 // BuddyPress core settings1129 'bp-deactivated-components' => serialize( array( ) ),1130 'bp-blogs-first-install' => '0',1131 'bp-disable-blogforum-comments' => '0',1132 'bp-xprofile-base-group-name' => 'Base',1133 'bp-xprofile-fullname-field-name' => 'Name',1134 'bp-disable-profile-sync' => '0',1135 'bp-disable-avatar-uploads' => '0',1136 'bp-disable-account-deletion' => '0',1137 'bp-disable-blogforum-comments' => '0',1138 'bb-config-location' => ABSPATH . 'bb-config.php',1139 'hide-loggedout-adminbar' => '0',1140 1141 // Useful WordPress settings1142 'registration' => '0',1143 'avatar_default' => 'mysteryman'1144 ) );1145 1146 $root_blog_option_keys = array_keys( $root_blog_options );1147 $blog_options_keys = "'" . join( "', '", (array) $root_blog_option_keys ) . "'";1148 $blog_options_table = bp_is_multiblog_mode() ? $wpdb->options : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'options';1149 1150 $blog_options_query = $wpdb->prepare( "SELECT option_name AS name, option_value AS value FROM {$blog_options_table} WHERE option_name IN ( {$blog_options_keys} )" );1151 $root_blog_options_meta = $wpdb->get_results( $blog_options_query );1152 1153 // On Multisite installations, some options must always be fetched from sitemeta1154 if ( is_multisite() ) {1155 $network_options = apply_filters( 'bp_core_network_options', array(1156 'tags_blog_id' => '0',1157 'sitewide_tags_blog' => '',1158 'registration' => '0',1159 'fileupload_maxk' => '1500'1160 ) );1161 1162 $current_site = get_current_site();1163 $network_option_keys = array_keys( $network_options );1164 $sitemeta_options_keys = "'" . join( "', '", (array) $network_option_keys ) . "'";1165 $sitemeta_options_query = $wpdb->prepare( "SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ( {$sitemeta_options_keys} ) AND site_id = %d", $current_site->id );1166 $network_options_meta = $wpdb->get_results( $sitemeta_options_query );1167 1168 // Sitemeta comes second in the merge, so that network 'registration' value wins1169 $root_blog_options_meta = array_merge( $root_blog_options_meta, $network_options_meta );1170 }1171 1172 // Missing some options, so do some one-time fixing1173 if ( empty( $root_blog_options_meta ) || ( count( $root_blog_options_meta ) < count( $root_blog_option_keys ) ) ) {1174 1175 // Unset the query - We'll be resetting it soon1176 unset( $root_blog_options_meta );1177 1178 // Loop through options1179 foreach ( $root_blog_options as $old_meta_key => $old_meta_default ) {1180 // Clear out the value from the last time around1181 unset( $old_meta_value );1182 1183 // Get old site option1184 if ( is_multisite() )1185 $old_meta_value = get_site_option( $old_meta_key );1186 1187 // No site option so look in root blog1188 if ( empty( $old_meta_value ) )1189 $old_meta_value = bp_get_option( $old_meta_key, $old_meta_default );1190 1191 // Update the root blog option1192 bp_update_option( $old_meta_key, $old_meta_value );1193 1194 // Update the global array1195 $root_blog_options_meta[$old_meta_key] = $old_meta_value;1196 }1197 1198 // We're all matched up1199 } else {1200 // Loop through our results and make them usable1201 foreach ( $root_blog_options_meta as $root_blog_option )1202 $root_blog_options[$root_blog_option->name] = $root_blog_option->value;1203 1204 // Copy the options no the return val1205 $root_blog_options_meta = $root_blog_options;1206 1207 // Clean up our temporary copy1208 unset( $root_blog_options );1209 }1210 1211 return apply_filters( 'bp_core_get_root_options', $root_blog_options_meta );1212 }1213 1214 /**1215 744 * This function originally let plugins add support for pages in the root of the install. 1216 745 * These root level pages are now handled by actual WordPress pages and this function is now … … 1466 995 } 1467 996 997 /** Embeds ********************************************************************/ 998 1468 999 /** 1469 1000 * Are oembeds allowed in activity items? … … 1505 1036 return apply_filters( 'bp_use_embed_in_private_messages', !defined( 'BP_EMBED_DISABLE_PRIVATE_MESSAGES' ) || !BP_EMBED_DISABLE_PRIVATE_MESSAGES ); 1506 1037 } 1038 1039 /** Admin *********************************************************************/ 1507 1040 1508 1041 /** … … 1546 1079 } 1547 1080 1081 function bp_core_do_network_admin() { 1082 $do_network_admin = false; 1083 1084 if ( is_multisite() && !bp_is_multiblog_mode() ) 1085 $do_network_admin = true; 1086 1087 return apply_filters( 'bp_core_do_network_admin', $do_network_admin ); 1088 } 1089 1090 function bp_core_admin_hook() { 1091 $hook = bp_core_do_network_admin() ? 'network_admin_menu' : 'admin_menu'; 1092 1093 return apply_filters( 'bp_core_admin_hook', $hook ); 1094 } 1095 1548 1096 /** Global Manipulators *******************************************************/ 1549 1097
Note: See TracChangeset
for help on using the changeset viewer.