[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/admin/ -> bp-core-admin-functions.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Common Admin Functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage CoreAdministration
   7   * @since 2.3.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /** Menu **********************************************************************/
  14  
  15  /**
  16   * Initializes the wp-admin area "BuddyPress" menus and sub menus.
  17   */
  18  function bp_core_admin_menu_init() {
  19      add_action( bp_core_admin_hook(), 'bp_core_add_admin_menu', 9 );
  20  }
  21  
  22  /**
  23   * In BP 1.6, the top-level admin menu was removed. For backpat, this function
  24   * keeps the top-level menu if a plugin has registered a menu into the old
  25   * 'bp-general-settings' menu.
  26   *
  27   * The old "bp-general-settings" page was renamed "bp-components".
  28   *
  29   * @global array $_parent_pages
  30   * @global array $_registered_pages
  31   * @global array $submenu
  32   *
  33   * @since 1.6.0
  34   */
  35  function bp_core_admin_backpat_menu() {
  36      global $_parent_pages, $_registered_pages, $submenu;
  37  
  38      // If there's no bp-general-settings menu (perhaps because the current
  39      // user is not an Administrator), there's nothing to do here.
  40      if ( ! isset( $submenu['bp-general-settings'] ) ) {
  41          return;
  42      }
  43  
  44      /**
  45       * By default, only the core "Help" submenu is added under the top-level BuddyPress menu.
  46       * This means that if no third-party plugins have registered their admin pages into the
  47       * 'bp-general-settings' menu, it will only contain one item. Kill it.
  48       */
  49      if ( 1 != count( $submenu['bp-general-settings'] ) ) {
  50          return;
  51      }
  52  
  53      // This removes the top-level menu.
  54      remove_submenu_page( 'bp-general-settings', 'bp-general-settings' );
  55      remove_menu_page( 'bp-general-settings' );
  56  
  57      // These stop people accessing the URL directly.
  58      unset( $_parent_pages['bp-general-settings'] );
  59      unset( $_registered_pages['toplevel_page_bp-general-settings'] );
  60  }
  61  add_action( bp_core_admin_hook(), 'bp_core_admin_backpat_menu', 999 );
  62  
  63  /**
  64   * This tells WP to highlight the Settings > BuddyPress menu item,
  65   * regardless of which actual BuddyPress admin screen we are on.
  66   *
  67   * The conditional prevents the behavior when the user is viewing the
  68   * backpat "Help" page, the Activity page, or any third-party plugins.
  69   *
  70   * @global string $plugin_page
  71   * @global array $submenu
  72   *
  73   * @since 1.6.0
  74   */
  75  function bp_core_modify_admin_menu_highlight() {
  76      global $plugin_page, $submenu_file;
  77  
  78      // This tweaks the Settings subnav menu to show only one BuddyPress menu item.
  79      if ( ! in_array( $plugin_page, array( 'bp-activity', 'bp-general-settings' ) ) ) {
  80          $submenu_file = 'bp-components';
  81      }
  82  
  83      // Network Admin > Tools.
  84      if ( in_array( $plugin_page, array( 'bp-tools', 'available-tools' ) ) ) {
  85          $submenu_file = $plugin_page;
  86      }
  87  
  88      // Keep the BuddyPress tools menu highlighted when using a tools tab.
  89      if ( 'bp-optouts' === $plugin_page || 'bp-members-invitations' === $plugin_page ) {
  90          $submenu_file = 'bp-tools';
  91      }
  92  }
  93  
  94  /**
  95   * Generates markup for a fallback top-level BuddyPress menu page, if the site is running
  96   * a legacy plugin which hasn't been updated. If the site is up to date, this page
  97   * will never appear.
  98   *
  99   * @see bp_core_admin_backpat_menu()
 100   *
 101   * @since 1.6.0
 102   *
 103   * @todo Add convenience links into the markup once new positions are finalized.
 104   */
 105  function bp_core_admin_backpat_page() {
 106      $url          = bp_core_do_network_admin() ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
 107      $settings_url = add_query_arg( 'page', 'bp-components', $url ); ?>
 108  
 109      <div class="wrap">
 110          <h1 class="wp-heading-inline"><?php esc_html_e( 'Why have all my BuddyPress menus disappeared?', 'buddypress' ); ?></h1>
 111          <hr class="wp-header-end">
 112  
 113          <p><?php esc_html_e( "Don't worry! We've moved the BuddyPress options into more convenient and easier to find locations. You're seeing this page because you are running a legacy BuddyPress plugin which has not been updated.", 'buddypress' ); ?></p>
 114          <p>
 115              <?php
 116              printf(
 117                  // Translators: 1: is the url to the BP Components settings screen. 2: is the url to the xProfile administration screen.
 118                  __( 'Components, Pages, Settings, and Forums, have been moved to <a href="%1$s">Settings &gt; BuddyPress</a>. Profile Fields has been moved into the <a href="%2$s">Users</a> menu.', 'buddypress' ),
 119                  esc_url( $settings_url ),
 120                  bp_get_admin_url( 'users.php?page=bp-profile-setup' )
 121              );
 122              ?>
 123          </p>
 124      </div>
 125  
 126      <?php
 127  }
 128  
 129  /** Notices *******************************************************************/
 130  
 131  /**
 132   * Print admin messages to admin_notices or network_admin_notices.
 133   *
 134   * BuddyPress combines all its messages into a single notice, to avoid a preponderance of yellow
 135   * boxes.
 136   *
 137   * @since 1.5.0
 138   */
 139  function bp_core_print_admin_notices() {
 140  
 141      // Only the super admin should see messages.
 142      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 143          return;
 144      }
 145  
 146      // On multisite installs, don't show on a non-root blog, unless
 147      // 'do_network_admin' is overridden.
 148      if ( is_multisite() && bp_core_do_network_admin() && ! bp_is_root_blog() ) {
 149          return;
 150      }
 151  
 152      $notice_types = array();
 153      foreach ( buddypress()->admin->notices as $notice ) {
 154          $notice_types[] = $notice['type'];
 155      }
 156      $notice_types = array_unique( $notice_types );
 157  
 158      foreach ( $notice_types as $type ) {
 159          $notices = wp_list_filter( buddypress()->admin->notices, array( 'type' => $type ) );
 160          printf( '<div id="message" class="fade %s notice is-dismissible">', sanitize_html_class( $type ) );
 161  
 162          foreach ( $notices as $notice ) {
 163              printf( '<p>%s</p>', $notice['message'] );
 164          }
 165  
 166          printf( '</div>' );
 167      }
 168  }
 169  add_action( 'admin_notices', 'bp_core_print_admin_notices' );
 170  add_action( 'network_admin_notices', 'bp_core_print_admin_notices' );
 171  
 172  /**
 173   * Add an admin notice to the BP queue.
 174   *
 175   * Messages added with this function are displayed in BuddyPress's general purpose admin notices
 176   * box. It is recommended that you hook this function to admin_init, so that your messages are
 177   * loaded in time.
 178   *
 179   * @since 1.5.0
 180   *
 181   * @param string $notice The notice you are adding to the queue.
 182   * @param string $type   The notice type; optional. Usually either "updated" or "error".
 183   */
 184  function bp_core_add_admin_notice( $notice = '', $type = 'updated' ) {
 185  
 186      // Do not add if the notice is empty.
 187      if ( empty( $notice ) ) {
 188          return;
 189      }
 190  
 191      // Double check the object before referencing it.
 192      if ( ! isset( buddypress()->admin->notices ) ) {
 193          buddypress()->admin->notices = array();
 194      }
 195  
 196      // Add the notice.
 197      buddypress()->admin->notices[] = array(
 198          'message' => $notice,
 199          'type'    => $type,
 200      );
 201  }
 202  
 203  /**
 204   * Verify that some BP prerequisites are set up properly, and notify the admin if not.
 205   *
 206   * On every Dashboard page, this function checks the following:
 207   *   - that pretty permalinks are enabled.
 208   *   - that every BP component that needs a WP page for a directory has one.
 209   *   - that no WP page has multiple BP components associated with it.
 210   * The administrator will be shown a notice for each check that fails.
 211   *
 212   * @global WPDB $wpdb WordPress DB object
 213   * @global WP_Rewrite $wp_rewrite
 214   *
 215   * @since 1.2.0
 216   */
 217  function bp_core_activation_notice() {
 218      global $wp_rewrite, $wpdb;
 219  
 220      // Only the super admin gets warnings.
 221      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 222          return;
 223      }
 224  
 225      // Bail in user admin.
 226      if ( is_user_admin() ) {
 227          return;
 228      }
 229  
 230      // On multisite installs, don't load on a non-root blog, unless do_network_admin is overridden.
 231      if ( is_multisite() && bp_core_do_network_admin() && ! bp_is_root_blog() ) {
 232          return;
 233      }
 234  
 235      // Bail if in network admin, and BuddyPress is not network activated.
 236      if ( is_network_admin() && ! bp_is_network_activated() ) {
 237          return;
 238      }
 239  
 240      /**
 241       * Check to make sure that the blog setup routine has run. This can't
 242       * happen during the wizard because of the order which the components
 243       * are loaded.
 244       */
 245      if ( bp_is_active( 'blogs' ) ) {
 246          $bp    = buddypress();
 247          $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$bp->blogs->table_name}" );
 248  
 249          if ( empty( $count ) ) {
 250              bp_blogs_record_existing_blogs();
 251          }
 252      }
 253  
 254      // Add notice if no rewrite rules are enabled.
 255      if ( empty( $wp_rewrite->permalink_structure ) ) {
 256          bp_core_add_admin_notice(
 257              sprintf(
 258                  // Translators: %s is the url to the permalink settings.
 259                  __( '<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' ),
 260                  admin_url( 'options-permalink.php' )
 261              ),
 262              'error'
 263          );
 264      }
 265  
 266      // Get BuddyPress instance.
 267      $bp = buddypress();
 268  
 269      /**
 270       * Check for orphaned BP components (BP component is enabled, no WP page exists).
 271       */
 272      $orphaned_components = array();
 273      $wp_page_components  = array();
 274  
 275      // Only components with 'has_directory' require a WP page to function.
 276      foreach ( array_keys( $bp->loaded_components ) as $component_id ) {
 277          if ( ! empty( $bp->{$component_id}->has_directory ) ) {
 278              $wp_page_components[] = array(
 279                  'id'   => $component_id,
 280                  'name' => isset( $bp->{$component_id}->name ) ? $bp->{$component_id}->name : ucwords( $bp->{$component_id}->id ),
 281              );
 282          }
 283      }
 284  
 285      // Activate and Register are special cases. They are not components but they need WP pages.
 286      // If user registration is disabled, we can skip this step.
 287      if ( bp_allow_access_to_registration_pages() ) {
 288          $wp_page_components[] = array(
 289              'id'   => 'activate',
 290              'name' => __( 'Activate', 'buddypress' ),
 291          );
 292  
 293          $wp_page_components[] = array(
 294              'id'   => 'register',
 295              'name' => __( 'Register', 'buddypress' ),
 296          );
 297      }
 298  
 299      // On the first admin screen after a new installation, this isn't set, so grab it to suppress
 300      // a misleading error message.
 301      if ( empty( $bp->pages->members ) ) {
 302          $bp->pages = bp_core_get_directory_pages();
 303      }
 304  
 305      foreach ( $wp_page_components as $component ) {
 306          if ( ! isset( $bp->pages->{$component['id']} ) ) {
 307              $orphaned_components[] = $component['name'];
 308          }
 309      }
 310  
 311      if ( ! empty( $orphaned_components ) ) {
 312          $admin_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) );
 313          $notice    = sprintf(
 314              '%1$s <a href="%2$s">%3$s</a>',
 315              sprintf(
 316                  // Translators: %s is the comma separated list of components needing a directory page.
 317                  __( 'The following active BuddyPress Components do not have associated WordPress Pages: %s.', 'buddypress' ),
 318                  '<strong>' . implode( '</strong>, <strong>', array_map( 'esc_html', $orphaned_components ) ) . '</strong>'
 319              ),
 320              esc_url( $admin_url ),
 321              __( 'Repair', 'buddypress' )
 322          );
 323  
 324          bp_core_add_admin_notice( $notice );
 325      }
 326  
 327      // BP components cannot share a single WP page. Check for duplicate assignments, and post a message if found.
 328      $dupe_names = array();
 329      $page_ids   = bp_core_get_directory_page_ids();
 330      $dupes      = array_diff_assoc( $page_ids, array_unique( $page_ids ) );
 331  
 332      if ( ! empty( $dupes ) ) {
 333          foreach ( array_keys( $dupes ) as $dupe_component ) {
 334              $dupe_names[] = $bp->pages->{$dupe_component}->title;
 335          }
 336  
 337          // Make sure that there are no duplicate duplicates :).
 338          $dupe_names = array_unique( $dupe_names );
 339      }
 340  
 341      // If there are duplicates, post a message about them.
 342      if ( ! empty( $dupe_names ) ) {
 343          $admin_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) );
 344          $notice    = sprintf(
 345              '%1$s <a href="%2$s">%3$s</a>',
 346              sprintf(
 347                  // Translators: %s is the list of directory pages associated to more than one component.
 348                  __( 'Each BuddyPress Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %s.', 'buddypress' ),
 349                  '<strong>' . implode( '</strong>, <strong>', array_map( 'esc_html', $dupe_names ) ) . '</strong>'
 350              ),
 351              esc_url( $admin_url ),
 352              __( 'Repair', 'buddypress' )
 353          );
 354  
 355          bp_core_add_admin_notice( $notice );
 356      }
 357  }
 358  
 359  /**
 360   * Redirect user to BuddyPress's What's New page on activation.
 361   *
 362   * @since 1.7.0
 363   *
 364   * @internal Used internally to redirect BuddyPress to the about page on activation.
 365   */
 366  function bp_do_activation_redirect() {
 367  
 368      // Bail if no activation redirect.
 369      if ( ! get_transient( '_bp_activation_redirect' ) ) {
 370          return;
 371      }
 372  
 373      // Delete the redirect transient.
 374      delete_transient( '_bp_activation_redirect' );
 375  
 376      // Bail if activating from network, or bulk.
 377      if ( isset( $_GET['activate-multi'] ) ) {
 378          return;
 379      }
 380  
 381      $query_args = array();
 382      if ( get_transient( '_bp_is_new_install' ) ) {
 383          $query_args['is_new_install'] = '1';
 384          delete_transient( '_bp_is_new_install' );
 385      }
 386  
 387      // Redirect to dashboard and trigger the Hello screen.
 388      wp_safe_redirect( add_query_arg( $query_args, bp_get_admin_url( '?hello=buddypress' ) ) );
 389  }
 390  
 391  /** UI/Styling ****************************************************************/
 392  
 393  /**
 394   * Output the tabs in the admin area.
 395   *
 396   * @since 1.5.0
 397   * @since 8.0.0 Adds the `$context` parameter.
 398   *
 399   * @param string $active_tab Name of the tab that is active. Optional.
 400   * @param string $context    The context of use for the tabs. Defaults to 'settings'.
 401   *                           Possible values are 'settings' & 'tools'.
 402   */
 403  function bp_core_admin_tabs( $active_tab = '', $context = 'settings' ) {
 404      $tabs_html    = '';
 405      $idle_class   = 'nav-tab';
 406      $active_class = 'nav-tab nav-tab-active';
 407  
 408      /**
 409       * Filters the admin tabs to be displayed.
 410       *
 411       * @since 1.9.0
 412       *
 413       * @param array $value Array of tabs to output to the admin area.
 414       */
 415      $tabs = apply_filters( 'bp_core_admin_tabs', bp_core_get_admin_tabs( $active_tab, $context ) );
 416  
 417      // Loop through tabs and build navigation.
 418      foreach ( array_values( $tabs ) as $tab_data ) {
 419          $is_current = (bool) ( $tab_data['name'] == $active_tab );
 420          $tab_class  = $is_current ? $active_class : $idle_class;
 421          $tabs_html .= '<a href="' . esc_url( $tab_data['href'] ) . '" class="' . esc_attr( $tab_class ) . '">' . esc_html( $tab_data['name'] ) . '</a>';
 422      }
 423  
 424      echo $tabs_html;
 425  
 426      /**
 427       * Fires after the output of tabs for the admin area.
 428       *
 429       * @since 1.5.0
 430       * @since 8.0.0 Adds the `$context` parameter.
 431       *
 432       * @param string $context The context of use for the tabs.
 433       */
 434      do_action( 'bp_admin_tabs', $context );
 435  }
 436  
 437  /**
 438   * Get the data for the tabs in the admin area.
 439   *
 440   * @since 2.2.0
 441   * @since 8.0.0 Adds the `$context` parameter.
 442   *
 443   * @param string $active_tab Name of the tab that is active. Optional.
 444   * @param string $context    The context of use for the tabs. Defaults to 'settings'.
 445   *                           Possible values are 'settings' & 'tools'.
 446   * @return string
 447   */
 448  function bp_core_get_admin_tabs( $active_tab = '', $context = 'settings' ) {
 449      $tabs = array();
 450  
 451      if ( 'settings' === $context ) {
 452          $tabs = array(
 453              '0' => array(
 454                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-components' ), 'admin.php' ) ),
 455                  'name' => __( 'Components', 'buddypress' ),
 456              ),
 457              '2' => array(
 458                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-settings' ), 'admin.php' ) ),
 459                  'name' => __( 'Options', 'buddypress' ),
 460              ),
 461              '1' => array(
 462                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) ),
 463                  'name' => __( 'Pages', 'buddypress' ),
 464              ),
 465              '3' => array(
 466                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-credits' ), 'admin.php' ) ),
 467                  'name' => __( 'Credits', 'buddypress' ),
 468              ),
 469          );
 470      } elseif ( 'tools' === $context ) {
 471          $tools_page = 'tools.php';
 472          if ( bp_core_do_network_admin() ) {
 473              $tools_page = 'admin.php';
 474          }
 475  
 476          $tabs = array(
 477              '0' => array(
 478                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-tools' ), $tools_page ) ),
 479                  'name' => __( 'Repair', 'buddypress' ),
 480              ),
 481              '1' => array(
 482                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-members-invitations' ), $tools_page ) ),
 483                  'name' => __( 'Manage Invitations', 'buddypress' ),
 484              ),
 485              '2' => array(
 486                  'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-optouts' ), $tools_page ) ),
 487                  'name' => __( 'Manage Opt-outs', 'buddypress' ),
 488              ),
 489          );
 490      }
 491  
 492      /**
 493       * Filters the tab data used in our wp-admin screens.
 494       *
 495       * @since 2.2.0
 496       * @since 8.0.0 Adds the `$context` parameter.
 497       *
 498       * @param array  $tabs    Tab data.
 499       * @param string $context The context of use for the tabs.
 500       */
 501      return apply_filters( 'bp_core_get_admin_tabs', $tabs, $context );
 502  }
 503  
 504  /** Help **********************************************************************/
 505  
 506  /**
 507   * Adds contextual help to BuddyPress admin pages.
 508   *
 509   * @since 1.7.0
 510   * @todo Make this part of the BP_Component class and split into each component.
 511   *
 512   * @param string $screen Current screen.
 513   */
 514  function bp_core_add_contextual_help( $screen = '' ) {
 515  
 516      $screen = get_current_screen();
 517  
 518      switch ( $screen->id ) {
 519  
 520          // Component page.
 521          case 'settings_page_bp-components':
 522              // Help tabs.
 523              $screen->add_help_tab(
 524                  array(
 525                      'id'      => 'bp-comp-overview',
 526                      'title'   => __( 'Overview', 'buddypress' ),
 527                      'content' => bp_core_add_contextual_help_content( 'bp-comp-overview' ),
 528                  )
 529              );
 530  
 531              // Help panel - sidebar links.
 532              $screen->set_help_sidebar(
 533                  '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
 534                  '<p>' . __( '<a href="https://codex.buddypress.org/getting-started/configure-components/">Managing Components</a>', 'buddypress' ) . '</p>' .
 535                  '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
 536              );
 537              break;
 538  
 539          // Pages page.
 540          case 'settings_page_bp-page-settings':
 541              // Help tabs.
 542              $screen->add_help_tab(
 543                  array(
 544                      'id'      => 'bp-page-overview',
 545                      'title'   => __( 'Overview', 'buddypress' ),
 546                      'content' => bp_core_add_contextual_help_content( 'bp-page-overview' ),
 547                  )
 548              );
 549  
 550              // Help panel - sidebar links.
 551              $screen->set_help_sidebar(
 552                  '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
 553                  '<p>' . __( '<a href="https://codex.buddypress.org/getting-started/configure-components/#settings-buddypress-pages">Managing Pages</a>', 'buddypress' ) . '</p>' .
 554                  '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
 555              );
 556  
 557              break;
 558  
 559          // Settings page.
 560          case 'settings_page_bp-settings':
 561              // Help tabs.
 562              $screen->add_help_tab(
 563                  array(
 564                      'id'      => 'bp-settings-overview',
 565                      'title'   => __( 'Overview', 'buddypress' ),
 566                      'content' => bp_core_add_contextual_help_content( 'bp-settings-overview' ),
 567                  )
 568              );
 569  
 570              // Help panel - sidebar links.
 571              $screen->set_help_sidebar(
 572                  '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
 573                  '<p>' . __( '<a href="https://codex.buddypress.org/getting-started/configure-components/#settings-buddypress-settings">Managing Settings</a>', 'buddypress' ) . '</p>' .
 574                  '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
 575              );
 576  
 577              break;
 578  
 579          // Profile fields page.
 580          case 'users_page_bp-profile-setup':
 581              // Help tabs.
 582              $screen->add_help_tab(
 583                  array(
 584                      'id'      => 'bp-profile-overview',
 585                      'title'   => __( 'Overview', 'buddypress' ),
 586                      'content' => bp_core_add_contextual_help_content( 'bp-profile-overview' ),
 587                  )
 588              );
 589  
 590              // Help panel - sidebar links.
 591              $screen->set_help_sidebar(
 592                  '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
 593                  '<p>' . __( '<a href="https://codex.buddypress.org/administrator-guide/extended-profiles/">Managing Profile Fields</a>', 'buddypress' ) . '</p>' .
 594                  '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
 595              );
 596  
 597              break;
 598      }
 599  }
 600  add_action( 'load-settings_page_bp-components', 'bp_core_add_contextual_help' );
 601  add_action( 'load-settings_page_bp-page-settings', 'bp_core_add_contextual_help' );
 602  add_action( 'load-settings_page_bp-settings', 'bp_core_add_contextual_help' );
 603  add_action( 'load-users_page_bp-profile-setup', 'bp_core_add_contextual_help' );
 604  
 605  /**
 606   * Renders contextual help content to contextual help tabs.
 607   *
 608   * @since 1.7.0
 609   *
 610   * @param string $tab Current help content tab.
 611   * @return string
 612   */
 613  function bp_core_add_contextual_help_content( $tab = '' ) {
 614  
 615      switch ( $tab ) {
 616          case 'bp-comp-overview':
 617              $retval = __( 'By default, all but four of the BuddyPress components are enabled. You can selectively enable or disable any of the components by using the form below. Your BuddyPress installation will continue to function. However, the features of the disabled components will no longer be accessible to anyone using the site.', 'buddypress' );
 618              break;
 619  
 620          case 'bp-page-overview':
 621              $retval = __( 'BuddyPress Components use WordPress Pages for their root directory/archive pages. You can change the page associations for each active component by using the form below.', 'buddypress' );
 622              break;
 623  
 624          case 'bp-settings-overview':
 625              $retval = __( 'Extra configuration settings are provided and activated. You can selectively enable or disable any setting by using the form on this screen.', 'buddypress' );
 626              break;
 627  
 628          case 'bp-profile-overview':
 629              $retval = __( 'Your users will distinguish themselves through their profile page. Create relevant profile fields that will show on each users profile.', 'buddypress' ) . '<br /><br />' . __( 'Note: Drag fields from other groups and drop them on the "Signup Fields" tab to include them into your registration form.', 'buddypress' );
 630              break;
 631  
 632          default:
 633              $retval = false;
 634              break;
 635      }
 636  
 637      // Wrap text in a paragraph tag.
 638      if ( ! empty( $retval ) ) {
 639          $retval = '<p>' . $retval . '</p>';
 640      }
 641  
 642      return $retval;
 643  }
 644  
 645  /** Separator *****************************************************************/
 646  
 647  /**
 648   * Add a separator to the WordPress admin menus.
 649   *
 650   * @since 1.7.0
 651   */
 652  function bp_admin_separator() {
 653  
 654      // Bail if BuddyPress is not network activated and viewing network admin.
 655      if ( is_network_admin() && ! bp_is_network_activated() ) {
 656          return;
 657      }
 658  
 659      // Bail if BuddyPress is network activated and viewing site admin.
 660      if ( ! is_network_admin() && bp_is_network_activated() ) {
 661          return;
 662      }
 663  
 664      // Prevent duplicate separators when no core menu items exist.
 665      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 666          return;
 667      }
 668  
 669      // Bail if there are no components with admin UI's. Hardcoded for now, until
 670      // there's a real API for determining this later.
 671      if ( ! bp_is_active( 'activity' ) && ! bp_is_active( 'groups' ) ) {
 672          return;
 673      }
 674  
 675      global $menu;
 676  
 677      $menu[] = array( '', 'read', 'separator-buddypress', '', 'wp-menu-separator buddypress' );
 678  }
 679  
 680  /**
 681   * Tell WordPress we have a custom menu order.
 682   *
 683   * @since 1.7.0
 684   *
 685   * @param bool $menu_order Menu order.
 686   * @return bool Always true.
 687   */
 688  function bp_admin_custom_menu_order( $menu_order = false ) {
 689  
 690      // Bail if user cannot see admin pages.
 691      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 692          return $menu_order;
 693      }
 694  
 695      return true;
 696  }
 697  
 698  /**
 699   * Move our custom separator above our custom post types.
 700   *
 701   * @since 1.7.0
 702   *
 703   * @param array $menu_order Menu Order.
 704   * @return array Modified menu order.
 705   */
 706  function bp_admin_menu_order( $menu_order = array() ) {
 707  
 708      // Bail if user cannot see admin pages.
 709      if ( empty( $menu_order ) || ! bp_current_user_can( 'bp_moderate' ) ) {
 710          return $menu_order;
 711      }
 712  
 713      // Initialize our custom order array.
 714      $bp_menu_order = array();
 715  
 716      // Menu values.
 717      $last_sep = is_network_admin() ? 'separator1' : 'separator2';
 718  
 719      /**
 720       * Filters the custom admin menus.
 721       *
 722       * @since 1.7.0
 723       *
 724       * @param array $value Empty array.
 725       */
 726      $custom_menus = (array) apply_filters( 'bp_admin_menu_order', array() );
 727  
 728      // Bail if no components have top level admin pages.
 729      if ( empty( $custom_menus ) ) {
 730          return $menu_order;
 731      }
 732  
 733      // Add our separator to beginning of array.
 734      array_unshift( $custom_menus, 'separator-buddypress' );
 735  
 736      // Loop through menu order and do some rearranging.
 737      foreach ( (array) $menu_order as $item ) {
 738  
 739          // Position BuddyPress menus above appearance.
 740          if ( $last_sep == $item ) {
 741  
 742              // Add our custom menus.
 743              foreach ( (array) $custom_menus as $custom_menu ) {
 744                  if ( array_search( $custom_menu, $menu_order ) ) {
 745                      $bp_menu_order[] = $custom_menu;
 746                  }
 747              }
 748  
 749              // Add the appearance separator.
 750              $bp_menu_order[] = $last_sep;
 751  
 752              // Skip our menu items.
 753          } elseif ( ! in_array( $item, $custom_menus ) ) {
 754              $bp_menu_order[] = $item;
 755          }
 756      }
 757  
 758      // Return our custom order.
 759      return $bp_menu_order;
 760  }
 761  
 762  /** Utility  *****************************************************************/
 763  
 764  /**
 765   * When using a WP_List_Table, get the currently selected bulk action.
 766   *
 767   * WP_List_Tables have bulk actions at the top and at the bottom of the tables,
 768   * and the inputs have different keys in the $_REQUEST array. This function
 769   * reconciles the two values and returns a single action being performed.
 770   *
 771   * @since 1.7.0
 772   *
 773   * @return string
 774   */
 775  function bp_admin_list_table_current_bulk_action() {
 776  
 777      $action = ! empty( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
 778  
 779      // If the bottom is set, let it override the action.
 780      if ( ! empty( $_REQUEST['action2'] ) && $_REQUEST['action2'] != '-1' ) {
 781          $action = $_REQUEST['action2'];
 782      }
 783  
 784      return $action;
 785  }
 786  
 787  /** Menus *********************************************************************/
 788  
 789  /**
 790   * Register meta box and associated JS for BuddyPress WP Nav Menu.
 791   *
 792   * @since 1.9.0
 793   */
 794  function bp_admin_wp_nav_menu_meta_box() {
 795      if ( ! bp_is_root_blog() ) {
 796          return;
 797      }
 798  
 799      add_meta_box( 'add-buddypress-nav-menu', __( 'BuddyPress Member', 'buddypress' ), 'bp_admin_do_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
 800  
 801      add_action( 'admin_print_footer_scripts', 'bp_admin_wp_nav_menu_restrict_items' );
 802  }
 803  
 804  /**
 805   * BP Member nav menu filter to short-circuit WP's query.
 806   *
 807   * @since 7.0.0
 808   *
 809   * @param null     $null     A null value.
 810   * @param WP_Query $wp_query The WP_Query instance (passed by reference).
 811   * @return array   The BP Member nav items to short-circuit WP's query,
 812   */
 813  function bp_admin_get_wp_nav_menu_items( $null, $wp_query ) {
 814      if ( isset( $wp_query->query['orderby'], $wp_query->query['order'] ) && 'post_date' === $wp_query->query['orderby'] && 'DESC' === $wp_query->query['order'] ) {
 815          return bp_nav_menu_get_loggedin_pages();
 816      } elseif ( isset( $wp_query->query['nopaging'] ) && true === $wp_query->query['nopaging'] ) {
 817          return array_merge( bp_nav_menu_get_loggedin_pages(), bp_nav_menu_get_loggedout_pages() );
 818      }
 819  
 820      return bp_nav_menu_get_loggedout_pages();
 821  }
 822  
 823  /**
 824   * Build and populate the BuddyPress accordion on Appearance > Menus.
 825   *
 826   * @since 1.9.0
 827   * @since 7.0.0 Uses wp_nav_menu_item_post_type_meta_box()
 828   *
 829   * @global $nav_menu_selected_id
 830   */
 831  function bp_admin_do_wp_nav_menu_meta_box( $object = '', $box = array() ) {
 832      global $nav_menu_selected_id;
 833  
 834      $box['args'] = (object) array(
 835          'name'           => 'bp_nav_menu_item',
 836          '_default_query' => array(),
 837      );
 838  
 839      // Temporarly register a post type.
 840      register_post_type(
 841          'bp_nav_menu_item',
 842          array(
 843              'label'  => 'BuddyPress',
 844              'labels' => array(
 845                  'search_items' => __( 'Search BuddyPress member menu items', 'buddypress' ),
 846                  'all_items'    => __( 'All BuddyPress Member menu items', 'buddypress' ),
 847              ),
 848              'public' => true,
 849              'hierarchical' => false,
 850              'has_archive'  => false,
 851              'rewrite'      => false,
 852          )
 853      );
 854  
 855      // Temporarly override the posts query results.
 856      add_filter( 'posts_pre_query', 'bp_admin_get_wp_nav_menu_items', 10, 2 );
 857  
 858      ob_start();
 859      wp_nav_menu_item_post_type_meta_box( 'buddypress', $box );
 860      $output = ob_get_clean();
 861  
 862      $get_bp_items = new WP_Query;
 863      $all_bp_items = $get_bp_items->query( array( 'nopaging' => true ) );
 864      $walker       = new Walker_Nav_Menu_Checklist();
 865      $all_bp_tabs  = sprintf(
 866          '<div id="bp_nav_menu_item-all" class="tabs-panel tabs-panel-view-all tabs-panel-inactive" role="region" aria-label="%1$s" tabindex="0">
 867              <ul id="bp_nav_menu_itemchecklist" data-wp-lists="list:bp_nav_menu_item" class="categorychecklist form-no-clear">
 868                  %2$s
 869              </ul>
 870          </div>',
 871          esc_html__( 'All BuddyPress Member menu items', 'buddypress' ),
 872          walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $all_bp_items ), 0, (object) array( 'walker' => $walker ) )
 873      );
 874  
 875      // Remove temporary post type and filter.
 876      unregister_post_type( 'bp_nav_menu_item' );
 877      remove_filter( 'posts_pre_query', 'bp_admin_get_wp_nav_menu_items', 10, 2 );
 878  
 879      $tab_name    = 'bp_nav_menu_item-tab';
 880      $current_tab = 'logged-in';
 881      $tabs        = array(
 882          'logged-in'  => __( 'Logged-In', 'buddypress' ),
 883          'logged-out' => __( 'Logged-Out', 'buddypress' ),
 884          'all'        => __( 'All', 'buddypress' ),
 885      );
 886      $tab_urls    = array(
 887          'all'        => '',
 888          'logged-in'  => '',
 889          'logged-out' => '',
 890      );
 891  
 892      if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array_keys( $tabs ), true ) ) {
 893          $current_tab = $_REQUEST[ $tab_name ];
 894      }
 895  
 896      $removed_args = array(
 897          'action',
 898          'customlink-tab',
 899          'edit-menu-item',
 900          'menu-item',
 901          'page-tab',
 902          '_wpnonce',
 903      );
 904  
 905      if ( $nav_menu_selected_id ) {
 906          $tab_urls['all']        = esc_url( add_query_arg( $tab_name, 'all', remove_query_arg( $removed_args ) ) );
 907          $tab_urls['logged-in']  = esc_url( add_query_arg( $tab_name, 'logged-in', remove_query_arg( $removed_args ) ) );
 908          $tab_urls['logged-out'] = esc_url( add_query_arg( $tab_name, 'logged-out', remove_query_arg( $removed_args ) ) );
 909      }
 910  
 911      $bp_tabs_nav = '';
 912      foreach ( $tabs as $tab => $tab_text ) {
 913          $class    = '';
 914          $datatype = 'bp_nav_menu_item-' . $tab;
 915  
 916          if ( $current_tab === $tab ) {
 917              $class = ' class="tabs"';
 918          }
 919  
 920          if ( 'all' !== $tab ) {
 921              $datatype = 'tabs-panel-posttype-bp_nav_menu_item-' . $tab;
 922          }
 923  
 924          $bp_tabs_nav .= sprintf(
 925              '<li%1$s>
 926                  <a class="nav-tab-link" data-type="%2$s" href="%3$s">
 927                      %4$s
 928                  </a>
 929              </li>',
 930              $class,
 931              $datatype,
 932              esc_url( $tab_urls[ $tab ] ) . '#' . $datatype,
 933              esc_html( $tab_text )
 934          );
 935      }
 936  
 937      $output = str_replace(
 938          array(
 939              'tabs-panel-posttype-bp_nav_menu_item-most-recent',
 940              'bp_nav_menu_itemchecklist-most-recent',
 941              'bp_nav_menu_item-all',
 942              'bp_nav_menu_itemchecklist',
 943          ),
 944          array(
 945              'tabs-panel-posttype-bp_nav_menu_item-logged-in',
 946              'bp_nav_menu_itemchecklist-logged-in',
 947              'tabs-panel-posttype-bp_nav_menu_item-logged-out',
 948              'bp_nav_menu_itemchecklist-logged-out',
 949          ),
 950          $output
 951      );
 952  
 953      preg_match( '/\<ul\sid=\"posttype-bp_nav_menu_item-tabs\"[^>]*>(.*?)\<\/ul\>\<!-- \.posttype-tabs --\>/s', $output, $tabs_nav );
 954  
 955      if ( isset( $tabs_nav[1] ) ) {
 956          $output = str_replace( $tabs_nav[1], $bp_tabs_nav, $output );
 957      }
 958  
 959      echo preg_replace( '/\<div\sclass=\".*\"\sid=\"tabs-panel-posttype-bp_nav_menu_item-search\"[^>]*>(.*?)\<\/div\>/s', $all_bp_tabs, $output );
 960  }
 961  
 962  /**
 963   * In admin emails list, for non-en_US locales, add notice explaining how to reinstall emails.
 964   *
 965   * If BuddyPress installs before its translations are in place, tell people how to reinstall
 966   * the emails so they have their contents in their site's language.
 967   *
 968   * @since 2.5.0
 969   */
 970  function bp_admin_email_maybe_add_translation_notice() {
 971      if ( get_current_screen()->post_type !== bp_get_email_post_type() || get_locale() === 'en_US' ) {
 972          return;
 973      }
 974  
 975      // If user can't access BP Tools, there's no point showing the message.
 976      if ( ! current_user_can( buddypress()->admin->capability ) ) {
 977          return;
 978      }
 979  
 980      if ( bp_core_do_network_admin() ) {
 981          $admin_page = 'admin.php';
 982      } else {
 983          $admin_page = 'tools.php';
 984      }
 985  
 986      bp_core_add_admin_notice(
 987          sprintf(
 988              // Translators: %s is the url to the BuddyPress tools administration screen.
 989              __( 'Are these emails not written in your site\'s language? Go to <a href="%s">BuddyPress Tools and try the "reinstall emails"</a> tool.', 'buddypress' ),
 990              esc_url( add_query_arg( 'page', 'bp-tools', bp_get_admin_url( $admin_page ) ) )
 991          ),
 992          'updated'
 993      );
 994  }
 995  add_action( 'admin_head-edit.php', 'bp_admin_email_maybe_add_translation_notice' );
 996  
 997  /**
 998   * In emails editor, add notice linking to token documentation on Codex.
 999   *
1000   * @since 2.5.0
1001   */
1002  function bp_admin_email_add_codex_notice() {
1003      if ( get_current_screen()->post_type !== bp_get_email_post_type() ) {
1004          return;
1005      }
1006  
1007      bp_core_add_admin_notice(
1008          sprintf(
1009              // Translators: %s is the url to the BuddyPress codex page about BP Email tokens.
1010              __( 'Phrases wrapped in braces <code>{{ }}</code> are email tokens. <a href="%s">Learn about tokens on the BuddyPress Codex</a>.', 'buddypress' ),
1011              esc_url( 'https://codex.buddypress.org/emails/email-tokens/' )
1012          ),
1013          'error'
1014      );
1015  }
1016  add_action( 'admin_head-post.php', 'bp_admin_email_add_codex_notice' );
1017  
1018  /**
1019   * Display metabox for email taxonomy type.
1020   *
1021   * Shows the term description in a list, rather than the term name itself.
1022   *
1023   * @since 2.5.0
1024   *
1025   * @param WP_Post $post Post object.
1026   * @param array   $box {
1027   *     Tags meta box arguments.
1028   *
1029   *     @type string   $id       Meta box ID.
1030   *     @type string   $title    Meta box title.
1031   *     @type callable $callback Meta box display callback.
1032   * }
1033   */
1034  function bp_email_tax_type_metabox( $post, $box ) {
1035      $r = array(
1036          'taxonomy' => bp_get_email_tax_type(),
1037      );
1038  
1039      $tax_name = esc_attr( $r['taxonomy'] );
1040      $taxonomy = get_taxonomy( $r['taxonomy'] );
1041      ?>
1042      <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">
1043          <div id="<?php echo $tax_name; ?>-all" class="tabs-panel">
1044              <?php
1045              $name = ( $tax_name == 'category' ) ? 'post_category' : 'tax_input[' . $tax_name . ']';
1046              echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
1047              ?>
1048              <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
1049                  <?php
1050                  wp_terms_checklist(
1051                      $post->ID,
1052                      array(
1053                          'taxonomy' => $tax_name,
1054                          'walker'   => new BP_Walker_Category_Checklist(),
1055                      )
1056                  );
1057                  ?>
1058              </ul>
1059          </div>
1060  
1061          <p><?php esc_html_e( 'Choose when this email will be sent.', 'buddypress' ); ?></p>
1062      </div>
1063      <?php
1064  }
1065  
1066  /**
1067   * Custom metaboxes used by our 'bp-email' post type.
1068   *
1069   * @since 2.5.0
1070   */
1071  function bp_email_custom_metaboxes() {
1072      // Remove default 'Excerpt' metabox and replace with our own.
1073      remove_meta_box( 'postexcerpt', null, 'normal' );
1074      add_meta_box( 'postexcerpt', __( 'Plain text email content', 'buddypress' ), 'bp_email_plaintext_metabox', null, 'normal', 'high' );
1075  }
1076  add_action( 'add_meta_boxes_' . bp_get_email_post_type(), 'bp_email_custom_metaboxes' );
1077  
1078  /**
1079   * Customized version of the 'Excerpt' metabox for our 'bp-email' post type.
1080   *
1081   * We are using the 'Excerpt' metabox as our plain-text email content editor.
1082   *
1083   * @since 2.5.0
1084   *
1085   * @param WP_Post $post
1086   */
1087  function bp_email_plaintext_metabox( $post ) {
1088      ?>
1089  
1090      <label class="screen-reader-text" for="excerpt">
1091      <?php
1092          /* translators: accessibility text */
1093          _e( 'Plain text email content', 'buddypress' );
1094      ?>
1095      </label><textarea rows="5" cols="40" name="excerpt" id="excerpt"><?php echo $post->post_excerpt; // textarea_escaped ?></textarea>
1096  
1097      <p><?php _e( 'Most email clients support HTML email. However, some people prefer to receive plain text email. Enter a plain text alternative version of your email here.', 'buddypress' ); ?></p>
1098  
1099      <?php
1100  }
1101  
1102  /**
1103   * Restrict various items from view if editing a BuddyPress menu.
1104   *
1105   * If a person is editing a BP menu item, that person should not be able to
1106   * see or edit the following fields:
1107   *
1108   * - CSS Classes - We use the 'bp-menu' CSS class to determine if the
1109   *   menu item belongs to BP, so we cannot allow manipulation of this field to
1110   *   occur.
1111   * - URL - This field is automatically generated by BP on output, so this
1112   *   field is useless and can cause confusion.
1113   *
1114   * Note: These restrictions are only enforced if JavaScript is enabled.
1115   *
1116   * @since 1.9.0
1117   */
1118  function bp_admin_wp_nav_menu_restrict_items() {
1119      ?>
1120      <script type="text/javascript">
1121      jQuery( '#menu-to-edit').on( 'click', 'a.item-edit', function() {
1122          var settings  = jQuery(this).closest( '.menu-item-bar' ).next( '.menu-item-settings' );
1123          var css_class = settings.find( '.edit-menu-item-classes' );
1124  
1125          if( css_class.val().indexOf( 'bp-menu' ) === 0 ) {
1126              css_class.attr( 'readonly', 'readonly' );
1127              settings.find( '.field-url' ).css( 'display', 'none' );
1128          }
1129      });
1130      </script>
1131      <?php
1132  }
1133  
1134  /**
1135   * Add "Mark as Spam/Ham" button to user row actions.
1136   *
1137   * @since 2.0.0
1138   *
1139   * @param array  $actions     User row action links.
1140   * @param object $user_object Current user information.
1141   * @return array $actions User row action links.
1142   */
1143  function bp_core_admin_user_row_actions( $actions, $user_object ) {
1144  
1145      // Setup the $user_id variable from the current user object.
1146      $user_id = 0;
1147      if ( ! empty( $user_object->ID ) ) {
1148          $user_id = absint( $user_object->ID );
1149      }
1150  
1151      // Bail early if user cannot perform this action, or is looking at themselves.
1152      if ( current_user_can( 'edit_user', $user_id ) && ( bp_loggedin_user_id() !== $user_id ) ) {
1153  
1154          // Admin URL could be single site or network.
1155          $url = bp_get_admin_url( 'users.php' );
1156  
1157          // If spammed, create unspam link.
1158          if ( bp_is_user_spammer( $user_id ) ) {
1159              $url            = add_query_arg(
1160                  array(
1161                      'action' => 'ham',
1162                      'user'   => $user_id,
1163                  ),
1164                  $url
1165              );
1166              $unspam_link    = wp_nonce_url( $url, 'bp-spam-user' );
1167              $actions['ham'] = sprintf('<a href="%1$s">%2$s</a>', esc_url( $unspam_link ), esc_html__( 'Not Spam', 'buddypress' ) );
1168  
1169              // If not already spammed, create spam link.
1170          } else {
1171              $url             = add_query_arg(
1172                  array(
1173                      'action' => 'spam',
1174                      'user'   => $user_id,
1175                  ),
1176                  $url
1177              );
1178              $spam_link       = wp_nonce_url( $url, 'bp-spam-user' );
1179              $actions['spam'] = sprintf( '<a class="submitdelete" href="%1$s">%2$s</a>', esc_url( $spam_link ), esc_html__( 'Spam', 'buddypress' ) );
1180          }
1181      }
1182  
1183      // Create a "View" link.
1184      $url             = bp_core_get_user_domain( $user_id );
1185      $actions['view'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $url ), esc_html__( 'View', 'buddypress' ) );
1186  
1187      // Return new actions.
1188      return $actions;
1189  }
1190  
1191  /**
1192   * Catch requests to mark individual users as spam/ham from users.php.
1193   *
1194   * @since 2.0.0
1195   */
1196  function bp_core_admin_user_manage_spammers() {
1197  
1198      // Print our inline scripts on non-Multisite.
1199      add_action( 'admin_footer', 'bp_core_admin_user_spammed_js' );
1200  
1201      $action  = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : false;
1202      $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
1203      $mode    = isset( $_POST['mode'] ) ? $_POST['mode'] : false;
1204  
1205      // If this is a multisite, bulk request, stop now!
1206      if ( 'list' == $mode ) {
1207          return;
1208      }
1209  
1210      // Process a spam/ham request.
1211      if ( ! empty( $action ) && in_array( $action, array( 'spam', 'ham' ) ) ) {
1212  
1213          check_admin_referer( 'bp-spam-user' );
1214  
1215          $user_id = ! empty( $_REQUEST['user'] ) ? intval( $_REQUEST['user'] ) : false;
1216  
1217          if ( empty( $user_id ) ) {
1218              return;
1219          }
1220  
1221          $redirect = wp_get_referer();
1222  
1223          $status = ( $action == 'spam' ) ? 'spam' : 'ham';
1224  
1225          // Process the user.
1226          bp_core_process_spammer_status( $user_id, $status );
1227  
1228          $redirect = add_query_arg( array( 'updated' => 'marked-' . $status ), $redirect );
1229  
1230          wp_redirect( $redirect );
1231      }
1232  
1233      // Display feedback.
1234      if ( ! empty( $updated ) && in_array( $updated, array( 'marked-spam', 'marked-ham' ) ) ) {
1235  
1236          if ( 'marked-spam' === $updated ) {
1237              $notice = __( 'User marked as spammer. Spam users are visible only to site admins.', 'buddypress' );
1238          } else {
1239              $notice = __( 'User removed from spam.', 'buddypress' );
1240          }
1241  
1242          bp_core_add_admin_notice( $notice );
1243      }
1244  }
1245  
1246  /**
1247   * Inline script that adds the 'site-spammed' class to spammed users.
1248   *
1249   * @since 2.0.0
1250   */
1251  function bp_core_admin_user_spammed_js() {
1252      ?>
1253      <script type="text/javascript">
1254          jQuery( document ).ready( function($) {
1255              $( '.row-actions .ham' ).each( function() {
1256                  $( this ).closest( 'tr' ).addClass( 'site-spammed' );
1257              });
1258          });
1259      </script>
1260      <?php
1261  }
1262  
1263  /**
1264   * Catch and process an admin notice dismissal.
1265   *
1266   * @since 2.7.0
1267   */
1268  function bp_core_admin_notice_dismiss_callback() {
1269      if ( ! current_user_can( 'install_plugins' ) ) {
1270          wp_send_json_error();
1271      }
1272  
1273      if ( empty( $_POST['nonce'] ) || empty( $_POST['notice_id'] ) ) {
1274          wp_send_json_error();
1275      }
1276  
1277      $notice_id = wp_unslash( $_POST['notice_id'] );
1278  
1279      if ( ! wp_verify_nonce( $_POST['nonce'], 'bp-dismissible-notice-' . $notice_id ) ) {
1280          wp_send_json_error();
1281      }
1282  
1283      bp_update_option( "bp-dismissed-notice-$notice_id", 1 );
1284  
1285      wp_send_json_success();
1286  }
1287  add_action( 'wp_ajax_bp_dismiss_notice', 'bp_core_admin_notice_dismiss_callback' );
1288  
1289  /**
1290   * Add a "buddypress" class to body element of wp-admin.
1291   *
1292   * @since 2.8.0
1293   *
1294   * @param string $classes CSS classes for the body tag in the admin, a comma separated string.
1295   *
1296   * @return string
1297   */
1298  function bp_core_admin_body_classes( $classes ) {
1299      return $classes . ' buddypress';
1300  }
1301  add_filter( 'admin_body_class', 'bp_core_admin_body_classes' );
1302  
1303  /**
1304   * Adds a BuddyPress category to house BuddyPress blocks.
1305   *
1306   * @since 5.0.0
1307   * @since 8.0.0 The `bp_block_category_post_types` filter has been deprecated.
1308   *
1309   * @param array          $categories Array of block categories.
1310   * @param string|WP_Post $post       Post being loaded.
1311   */
1312  function bp_block_category( $categories = array(), $editor_name_or_post = null ) {
1313      if ( $editor_name_or_post instanceof WP_Post ) {
1314          $post_types = array( 'post', 'page' );
1315  
1316          /*
1317           * As blocks are always loaded even if the category is not available, there's no more interest
1318           * in disabling the BuddyPress category.
1319           */
1320          apply_filters_deprecated( 'bp_block_category_post_types', array( $post_types ), '8.0.0' );
1321      }
1322  
1323      return array_merge(
1324          $categories,
1325          array(
1326              array(
1327                  'slug'  => 'buddypress',
1328                  'title' => __( 'BuddyPress', 'buddypress' ),
1329                  'icon'  => 'buddicons-buddypress-logo',
1330              ),
1331          )
1332      );
1333  }
1334  
1335  /**
1336   * Select the right `block_categories` filter according to WP version.
1337   *
1338   * @since 8.0.0
1339   */
1340  function bp_block_init_category_filter() {
1341      if ( function_exists( 'get_default_block_categories' ) ) {
1342          add_filter( 'block_categories_all', 'bp_block_category', 1, 2 );
1343      } else {
1344          add_filter( 'block_categories', 'bp_block_category', 1, 2 );
1345      }
1346  }
1347  add_action( 'bp_init', 'bp_block_init_category_filter' );


Generated: Tue Oct 26 01:00:55 2021 Cross-referenced by PHPXref 0.7.1