[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Admin Component 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  /**
  14   * Renders the Component Setup admin panel.
  15   *
  16   * @since 1.6.0
  17   *
  18   */
  19  function bp_core_admin_components_settings() {
  20  ?>
  21  
  22      <div class="wrap">
  23  
  24          <h1><?php _e( 'BuddyPress Settings', 'buddypress' ); ?> </h1>
  25  
  26          <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Components', 'buddypress' ) ); ?></h2>
  27          <form action="" method="post" id="bp-admin-component-form">
  28  
  29              <?php bp_core_admin_components_options(); ?>
  30  
  31              <p class="submit clear">
  32                  <input class="button-primary" type="submit" name="bp-admin-component-submit" id="bp-admin-component-submit" value="<?php esc_attr_e( 'Save Settings', 'buddypress' ) ?>"/>
  33              </p>
  34  
  35              <?php wp_nonce_field( 'bp-admin-component-setup' ); ?>
  36  
  37          </form>
  38      </div>
  39  
  40  <?php
  41  }
  42  
  43  /**
  44   * Creates reusable markup for component setup on the Components and Pages dashboard panel.
  45   *
  46   * @since 1.6.0
  47   *
  48   * @todo Use settings API
  49   */
  50  function bp_core_admin_components_options() {
  51  
  52      // Declare local variables.
  53      $deactivated_components = array();
  54  
  55      /**
  56       * Filters the array of available components.
  57       *
  58       * @since 1.5.0
  59       *
  60       * @param mixed $value Active components.
  61       */
  62      $active_components      = apply_filters( 'bp_active_components', bp_get_option( 'bp-active-components' ) );
  63  
  64      // The default components (if none are previously selected).
  65      $default_components = array(
  66          'xprofile' => array(
  67              'title'       => __( 'Extended Profiles', 'buddypress' ),
  68              'description' => __( 'Customize your community with fully editable profile fields that allow your users to describe themselves.', 'buddypress' )
  69          ),
  70          'settings' => array(
  71              'title'       => __( 'Account Settings', 'buddypress' ),
  72              'description' => __( 'Allow your users to modify their account and notification settings directly from within their profiles.', 'buddypress' )
  73          ),
  74          'notifications' => array(
  75              'title'       => __( 'Notifications', 'buddypress' ),
  76              'description' => __( 'Notify members of relevant activity with a toolbar bubble and/or via email, and allow them to customize their notification settings.', 'buddypress' )
  77          ),
  78      );
  79  
  80      $optional_components = bp_core_admin_get_components( 'optional' );
  81      $required_components = bp_core_admin_get_components( 'required' );
  82      $retired_components  = bp_core_admin_get_components( 'retired'  );
  83  
  84      // Merge optional and required together.
  85      $all_components = $optional_components + $required_components;
  86  
  87      // If this is an upgrade from before BuddyPress 1.5, we'll have to convert
  88      // deactivated components into activated ones.
  89      if ( empty( $active_components ) ) {
  90          $deactivated_components = bp_get_option( 'bp-deactivated-components' );
  91          if ( !empty( $deactivated_components ) ) {
  92  
  93              // Trim off namespace and filename.
  94              $trimmed = array();
  95              foreach ( array_keys( (array) $deactivated_components ) as $component ) {
  96                  $trimmed[] = str_replace( '.php', '', str_replace( 'bp-', '', $component ) );
  97              }
  98  
  99              // Loop through the optional components to create an active component array.
 100              foreach ( array_keys( (array) $optional_components ) as $ocomponent ) {
 101                  if ( !in_array( $ocomponent, $trimmed ) ) {
 102                      $active_components[$ocomponent] = 1;
 103                  }
 104              }
 105          }
 106      }
 107  
 108      // On new install, set active components to default.
 109      if ( empty( $active_components ) ) {
 110          $active_components = $default_components;
 111      }
 112  
 113      // Core component is always active.
 114      $active_components['core'] = $all_components['core'];
 115      $inactive_components       = array_diff( array_keys( $all_components ) , array_keys( $active_components ) );
 116  
 117      /** Display **************************************************************
 118       */
 119  
 120      // Get the total count of all plugins.
 121      $all_count = count( $all_components );
 122      $page      = bp_core_do_network_admin()  ? 'settings.php' : 'options-general.php';
 123      $action    = !empty( $_GET['action'] ) ? $_GET['action'] : 'all';
 124  
 125      switch( $action ) {
 126          case 'all' :
 127              $current_components = $all_components;
 128              break;
 129          case 'active' :
 130              foreach ( array_keys( $active_components ) as $component ) {
 131                  $current_components[$component] = $all_components[$component];
 132              }
 133              break;
 134          case 'inactive' :
 135              foreach ( $inactive_components as $component ) {
 136                  $current_components[$component] = $all_components[$component];
 137              }
 138              break;
 139          case 'mustuse' :
 140              $current_components = $required_components;
 141              break;
 142          case 'retired' :
 143              $current_components = $retired_components;
 144              break;
 145      }
 146  
 147      $component_views = array(
 148          array(
 149              'action' => 'all',
 150              'view'   => sprintf(
 151                  /* translators: %s: the number of installed components */
 152                  _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $all_count, 'plugins', 'buddypress' ),
 153                  number_format_i18n( $all_count )
 154              ),
 155          ),
 156          array(
 157              'action' => 'active',
 158              'view'   => sprintf(
 159                  /* translators: %s: the number of active components */
 160                  _n( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', count( $active_components ), 'buddypress' ),
 161                  number_format_i18n( count( $active_components ) )
 162              ),
 163          ),
 164          array(
 165              'action' => 'inactive',
 166              'view'   => sprintf(
 167                  /* translators: %s: the number of inactive components */
 168                  _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', count( $inactive_components ), 'buddypress' ),
 169                  number_format_i18n( count( $inactive_components ) )
 170              ),
 171          ),
 172          array(
 173              'action' => 'mustuse',
 174              'view'   => sprintf(
 175                  /* translators: %s: the number of must-Use components */
 176                  _n( 'Must-Use <span class="count">(%s)</span>', 'Must-Use <span class="count">(%s)</span>', count( $required_components ), 'buddypress' ),
 177                  number_format_i18n( count( $required_components ) )
 178              ),
 179          ),
 180          array(
 181              'action' => 'retired',
 182              'view'   => sprintf(
 183                  /* translators: %s: the number of retired components */
 184                  _n( 'Retired <span class="count">(%s)</span>',  'Retired <span class="count">(%s)</span>',  count( $retired_components ),  'buddypress' ),
 185                  number_format_i18n( count( $retired_components ) )
 186              ),
 187          ),
 188      );
 189      ?>
 190  
 191      <h3 class="screen-reader-text"><?php
 192          /* translators: accessibility text */
 193          esc_html_e( 'Filter components list', 'buddypress' );
 194      ?></h3>
 195  
 196      <ul class="subsubsub">
 197          <?php foreach ( $component_views as $component_view ) : ?>
 198              <li>
 199                  <a href="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-components', 'action' => $component_view['action'] ), bp_get_admin_url( $page ) ) ); ?>" <?php if ( $action === $component_view['action'] ) : ?>class="current"<?php endif; ?>>
 200                      <?php echo wp_kses( $component_view['view'], array( 'span' => array( 'class' => true ) ) ); ?>
 201                  </a><?php echo 'retired' !== $component_view['action'] ? ' |' : ''; ?>
 202              </li>
 203          <?php endforeach ;?>
 204      </ul>
 205  
 206      <h3 class="screen-reader-text"><?php
 207          /* translators: accessibility text */
 208          esc_html_e( 'Components list', 'buddypress' );
 209      ?></h3>
 210  
 211      <table class="wp-list-table widefat plugins">
 212          <thead>
 213              <tr>
 214                  <td id="cb" class="manage-column column-cb check-column"><input id="cb-select-all-1" type="checkbox" <?php checked( empty( $inactive_components ) ); ?>>
 215                      <label class="screen-reader-text" for="cb-select-all-1"><?php
 216                      /* translators: accessibility text */
 217                      _e( 'Enable or disable all optional components in bulk', 'buddypress' );
 218                  ?></label></td>
 219                  <th scope="col" id="name" class="manage-column column-title column-primary"><?php _e( 'Component', 'buddypress' ); ?></th>
 220                  <th scope="col" id="description" class="manage-column column-description"><?php _e( 'Description', 'buddypress' ); ?></th>
 221              </tr>
 222          </thead>
 223  
 224          <tbody id="the-list">
 225  
 226              <?php if ( !empty( $current_components ) ) : ?>
 227  
 228                  <?php foreach ( $current_components as $name => $labels ) : ?>
 229  
 230                      <?php if ( !in_array( $name, array( 'core', 'members' ) ) ) :
 231                          $class = isset( $active_components[esc_attr( $name )] ) ? 'active' : 'inactive';
 232                      else :
 233                          $class = 'active';
 234                      endif; ?>
 235  
 236                      <tr id="<?php echo esc_attr( $name ); ?>" class="<?php echo esc_attr( $name ) . ' ' . esc_attr( $class ); ?>">
 237                          <th scope="row" class="check-column">
 238  
 239                              <?php if ( !in_array( $name, array( 'core', 'members' ) ) ) : ?>
 240  
 241                                  <input type="checkbox" id="<?php echo esc_attr( "bp_components[$name]" ); ?>" name="<?php echo esc_attr( "bp_components[$name]" ); ?>" value="1"<?php checked( isset( $active_components[esc_attr( $name )] ) ); ?> /><label for="<?php echo esc_attr( "bp_components[$name]" ); ?>" class="screen-reader-text"><?php
 242                                      /* translators: accessibility text */
 243                                      printf( __( 'Select %s', 'buddypress' ), esc_html( $labels['title'] ) ); ?></label>
 244  
 245                              <?php endif; ?>
 246  
 247                          </th>
 248                          <td class="plugin-title column-primary">
 249                              <label for="<?php echo esc_attr( "bp_components[$name]" ); ?>">
 250                                  <span aria-hidden="true"></span>
 251                                  <strong><?php echo esc_html( $labels['title'] ); ?></strong>
 252                              </label>
 253                          </td>
 254  
 255                          <td class="column-description desc">
 256                              <div class="plugin-description">
 257                                  <p><?php echo $labels['description']; ?></p>
 258                              </div>
 259  
 260                          </td>
 261                      </tr>
 262  
 263                  <?php endforeach ?>
 264  
 265              <?php else : ?>
 266  
 267                  <tr class="no-items">
 268                      <td class="colspanchange" colspan="3"><?php _e( 'No components found.', 'buddypress' ); ?></td>
 269                  </tr>
 270  
 271              <?php endif; ?>
 272  
 273          </tbody>
 274  
 275          <tfoot>
 276              <tr>
 277                  <td class="manage-column column-cb check-column"><input id="cb-select-all-2" type="checkbox" <?php checked( empty( $inactive_components ) ); ?>>
 278                      <label class="screen-reader-text" for="cb-select-all-2"><?php
 279                      /* translators: accessibility text */
 280                      _e( 'Enable or disable all optional components in bulk', 'buddypress' );
 281                  ?></label></td>
 282                  <th class="manage-column column-title column-primary"><?php _e( 'Component', 'buddypress' ); ?></th>
 283                  <th class="manage-column column-description"><?php _e( 'Description', 'buddypress' ); ?></th>
 284              </tr>
 285          </tfoot>
 286  
 287      </table>
 288  
 289      <input type="hidden" name="bp_components[members]" value="1" />
 290  
 291      <?php
 292  }
 293  
 294  /**
 295   * Handle saving the Component settings.
 296   *
 297   * @since 1.6.0
 298   *
 299   * @todo Use settings API when it supports saving network settings
 300   */
 301  function bp_core_admin_components_settings_handler() {
 302  
 303      // Bail if not saving settings.
 304      if ( ! isset( $_POST['bp-admin-component-submit'] ) )
 305          return;
 306  
 307      // Bail if nonce fails.
 308      if ( ! check_admin_referer( 'bp-admin-component-setup' ) )
 309          return;
 310  
 311      // Settings form submitted, now save the settings. First, set active components.
 312      if ( isset( $_POST['bp_components'] ) ) {
 313  
 314          // Load up BuddyPress.
 315          $bp = buddypress();
 316  
 317          // Save settings and upgrade schema.
 318          require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
 319          require_once( $bp->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php' );
 320  
 321          $submitted = stripslashes_deep( $_POST['bp_components'] );
 322          $bp->active_components = bp_core_admin_get_active_components_from_submitted_settings( $submitted );
 323  
 324          bp_core_install( $bp->active_components );
 325          bp_core_add_page_mappings( $bp->active_components );
 326          bp_update_option( 'bp-active-components', $bp->active_components );
 327      }
 328  
 329      // Where are we redirecting to?
 330      $base_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-components', 'updated' => 'true' ), 'admin.php' ) );
 331  
 332      // Redirect.
 333      wp_redirect( $base_url );
 334      die();
 335  }
 336  add_action( 'bp_admin_init', 'bp_core_admin_components_settings_handler' );
 337  
 338  /**
 339   * Calculates the components that should be active after save, based on submitted settings.
 340   *
 341   * The way that active components must be set after saving your settings must
 342   * be calculated differently depending on which of the Components subtabs you
 343   * are coming from:
 344   * - When coming from All or Active, the submitted checkboxes accurately
 345   *   reflect the desired active components, so we simply pass them through
 346   * - When coming from Inactive, components can only be activated - already
 347   *   active components will not be passed in the $_POST global. Thus, we must
 348   *   parse the newly activated components with the already active components
 349   *   saved in the $bp global
 350   * - When activating a Retired component, the situation is similar to Inactive.
 351   * - When deactivating a Retired component, no value is passed in the $_POST
 352   *   global (because the component settings are checkboxes). So, in order to
 353   *   determine whether a retired component is being deactivated, we retrieve a
 354   *   list of retired components, and check each one to ensure that its checkbox
 355   *   is not present, before merging the submitted components with the active
 356   *   ones.
 357   *
 358   * @since 1.7.0
 359   *
 360   * @param array $submitted This is the array of component settings coming from the POST
 361   *                         global. You should stripslashes_deep() before passing to this function.
 362   * @return array The calculated list of component settings
 363   */
 364  function bp_core_admin_get_active_components_from_submitted_settings( $submitted ) {
 365      $current_action = 'all';
 366  
 367      if ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'active', 'inactive', 'retired' ) ) ) {
 368          $current_action = $_GET['action'];
 369      }
 370  
 371      $current_components = buddypress()->active_components;
 372  
 373      switch ( $current_action ) {
 374          case 'retired' :
 375              $retired_components = bp_core_admin_get_components( 'retired' );
 376              foreach ( array_keys( $retired_components ) as $retired_component ) {
 377                  if ( ! isset( $submitted[ $retired_component ] ) ) {
 378                      unset( $current_components[ $retired_component ] );
 379                  }
 380              } // Fall through.
 381  
 382  
 383          case 'inactive' :
 384              $components = array_merge( $submitted, $current_components );
 385              break;
 386  
 387          case 'all' :
 388          case 'active' :
 389          default :
 390              $components = $submitted;
 391              break;
 392      }
 393  
 394      return $components;
 395  }
 396  
 397  /**
 398   * Return a list of component information.
 399   *
 400   * We use this information both to build the markup for the admin screens, as
 401   * well as to do some processing on settings data submitted from those screens.
 402   *
 403   * @since 1.7.0
 404   *
 405   * @param string $type Optional; component type to fetch. Default value is 'all', or 'optional', 'retired', 'required'.
 406   * @return array Requested components' data.
 407   */
 408  function bp_core_admin_get_components( $type = 'all' ) {
 409      $components = bp_core_get_components( $type );
 410  
 411      /**
 412       * Filters the list of component information.
 413       *
 414       * @since 2.0.0
 415       *
 416       * @param array  $components Array of component information.
 417       * @param string $type       Type of component list requested.
 418       *                           Possible values include 'all', 'optional',
 419       *                           'retired', 'required'.
 420       */
 421      return apply_filters( 'bp_core_admin_get_components', $components, $type );
 422  }


Generated: Mon Jul 6 01:01:29 2020 Cross-referenced by PHPXref 0.7.1