[ 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      <h3 class="screen-reader-text"><?php
 148          /* translators: accessibility text */
 149          _e( 'Filter components list', 'buddypress' );
 150      ?></h3>
 151  
 152      <ul class="subsubsub">
 153          <li><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-components', 'action' => 'all'      ), bp_get_admin_url( $page ) ) ); ?>" <?php if ( $action === 'all'      ) : ?>class="current"<?php endif; ?>><?php printf( _nx( 'All <span class="count">(%s)</span>',      'All <span class="count">(%s)</span>',      $all_count,         'plugins', 'buddypress' ), number_format_i18n( $all_count                    ) ); ?></a> | </li>
 154          <li><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-components', 'action' => 'active'   ), bp_get_admin_url( $page ) ) ); ?>" <?php if ( $action === 'active'   ) : ?>class="current"<?php endif; ?>><?php printf( _n(  'Active <span class="count">(%s)</span>',   'Active <span class="count">(%s)</span>',   count( $active_components   ), 'buddypress' ), number_format_i18n( count( $active_components   ) ) ); ?></a> | </li>
 155          <li><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-components', 'action' => 'inactive' ), bp_get_admin_url( $page ) ) ); ?>" <?php if ( $action === 'inactive' ) : ?>class="current"<?php endif; ?>><?php printf( _n(  'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', count( $inactive_components ), 'buddypress' ), number_format_i18n( count( $inactive_components ) ) ); ?></a> | </li>
 156          <li><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-components', 'action' => 'mustuse'  ), bp_get_admin_url( $page ) ) ); ?>" <?php if ( $action === 'mustuse'  ) : ?>class="current"<?php endif; ?>><?php printf( _n(  'Must-Use <span class="count">(%s)</span>', 'Must-Use <span class="count">(%s)</span>', count( $required_components ), 'buddypress' ), number_format_i18n( count( $required_components ) ) ); ?></a> | </li>
 157          <li><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'bp-components', 'action' => 'retired'  ), bp_get_admin_url( $page ) ) ); ?>" <?php if ( $action === 'retired'  ) : ?>class="current"<?php endif; ?>><?php printf( _n(  'Retired <span class="count">(%s)</span>',  'Retired <span class="count">(%s)</span>',  count( $retired_components ),  'buddypress' ), number_format_i18n( count( $retired_components  ) ) ); ?></a></li>
 158      </ul>
 159  
 160      <h3 class="screen-reader-text"><?php
 161          /* translators: accessibility text */
 162          _e( 'Components list', 'buddypress' );
 163      ?></h3>
 164  
 165      <table class="wp-list-table widefat plugins">
 166          <thead>
 167              <tr>
 168                  <td id="cb" class="manage-column column-cb check-column"><input id="cb-select-all-1" type="checkbox" <?php checked( empty( $inactive_components ) ); ?>>
 169                      <label class="screen-reader-text" for="cb-select-all-1"><?php
 170                      /* translators: accessibility text */
 171                      _e( 'Enable or disable all optional components in bulk', 'buddypress' );
 172                  ?></label></td>
 173                  <th scope="col" id="name" class="manage-column column-title column-primary"><?php _e( 'Component', 'buddypress' ); ?></th>
 174                  <th scope="col" id="description" class="manage-column column-description"><?php _e( 'Description', 'buddypress' ); ?></th>
 175              </tr>
 176          </thead>
 177  
 178          <tbody id="the-list">
 179  
 180              <?php if ( !empty( $current_components ) ) : ?>
 181  
 182                  <?php foreach ( $current_components as $name => $labels ) : ?>
 183  
 184                      <?php if ( !in_array( $name, array( 'core', 'members' ) ) ) :
 185                          $class = isset( $active_components[esc_attr( $name )] ) ? 'active' : 'inactive';
 186                      else :
 187                          $class = 'active';
 188                      endif; ?>
 189  
 190                      <tr id="<?php echo esc_attr( $name ); ?>" class="<?php echo esc_attr( $name ) . ' ' . esc_attr( $class ); ?>">
 191                          <th scope="row" class="check-column">
 192  
 193                              <?php if ( !in_array( $name, array( 'core', 'members' ) ) ) : ?>
 194  
 195                                  <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
 196                                      /* translators: accessibility text */
 197                                      printf( __( 'Select %s', 'buddypress' ), esc_html( $labels['title'] ) ); ?></label>
 198  
 199                              <?php endif; ?>
 200  
 201                          </th>
 202                          <td class="plugin-title column-primary">
 203                              <label for="<?php echo esc_attr( "bp_components[$name]" ); ?>">
 204                                  <span aria-hidden="true"></span>
 205                                  <strong><?php echo esc_html( $labels['title'] ); ?></strong>
 206                              </label>
 207                          </td>
 208  
 209                          <td class="column-description desc">
 210                              <div class="plugin-description">
 211                                  <p><?php echo $labels['description']; ?></p>
 212                              </div>
 213  
 214                          </td>
 215                      </tr>
 216  
 217                  <?php endforeach ?>
 218  
 219              <?php else : ?>
 220  
 221                  <tr class="no-items">
 222                      <td class="colspanchange" colspan="3"><?php _e( 'No components found.', 'buddypress' ); ?></td>
 223                  </tr>
 224  
 225              <?php endif; ?>
 226  
 227          </tbody>
 228  
 229          <tfoot>
 230              <tr>
 231                  <td class="manage-column column-cb check-column"><input id="cb-select-all-2" type="checkbox" <?php checked( empty( $inactive_components ) ); ?>>
 232                      <label class="screen-reader-text" for="cb-select-all-2"><?php
 233                      /* translators: accessibility text */
 234                      _e( 'Enable or disable all optional components in bulk', 'buddypress' );
 235                  ?></label></td>
 236                  <th class="manage-column column-title column-primary"><?php _e( 'Component', 'buddypress' ); ?></th>
 237                  <th class="manage-column column-description"><?php _e( 'Description', 'buddypress' ); ?></th>
 238              </tr>
 239          </tfoot>
 240  
 241      </table>
 242  
 243      <input type="hidden" name="bp_components[members]" value="1" />
 244  
 245      <?php
 246  }
 247  
 248  /**
 249   * Handle saving the Component settings.
 250   *
 251   * @since 1.6.0
 252   *
 253   * @todo Use settings API when it supports saving network settings
 254   */
 255  function bp_core_admin_components_settings_handler() {
 256  
 257      // Bail if not saving settings.
 258      if ( ! isset( $_POST['bp-admin-component-submit'] ) )
 259          return;
 260  
 261      // Bail if nonce fails.
 262      if ( ! check_admin_referer( 'bp-admin-component-setup' ) )
 263          return;
 264  
 265      // Settings form submitted, now save the settings. First, set active components.
 266      if ( isset( $_POST['bp_components'] ) ) {
 267  
 268          // Load up BuddyPress.
 269          $bp = buddypress();
 270  
 271          // Save settings and upgrade schema.
 272          require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
 273          require_once( $bp->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php' );
 274  
 275          $submitted = stripslashes_deep( $_POST['bp_components'] );
 276          $bp->active_components = bp_core_admin_get_active_components_from_submitted_settings( $submitted );
 277  
 278          bp_core_install( $bp->active_components );
 279          bp_core_add_page_mappings( $bp->active_components );
 280          bp_update_option( 'bp-active-components', $bp->active_components );
 281      }
 282  
 283      // Where are we redirecting to?
 284      $base_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-components', 'updated' => 'true' ), 'admin.php' ) );
 285  
 286      // Redirect.
 287      wp_redirect( $base_url );
 288      die();
 289  }
 290  add_action( 'bp_admin_init', 'bp_core_admin_components_settings_handler' );
 291  
 292  /**
 293   * Calculates the components that should be active after save, based on submitted settings.
 294   *
 295   * The way that active components must be set after saving your settings must
 296   * be calculated differently depending on which of the Components subtabs you
 297   * are coming from:
 298   * - When coming from All or Active, the submitted checkboxes accurately
 299   *   reflect the desired active components, so we simply pass them through
 300   * - When coming from Inactive, components can only be activated - already
 301   *   active components will not be passed in the $_POST global. Thus, we must
 302   *   parse the newly activated components with the already active components
 303   *   saved in the $bp global
 304   * - When activating a Retired component, the situation is similar to Inactive.
 305   * - When deactivating a Retired component, no value is passed in the $_POST
 306   *   global (because the component settings are checkboxes). So, in order to
 307   *   determine whether a retired component is being deactivated, we retrieve a
 308   *   list of retired components, and check each one to ensure that its checkbox
 309   *   is not present, before merging the submitted components with the active
 310   *   ones.
 311   *
 312   * @since 1.7.0
 313   *
 314   * @param array $submitted This is the array of component settings coming from the POST
 315   *                         global. You should stripslashes_deep() before passing to this function.
 316   * @return array The calculated list of component settings
 317   */
 318  function bp_core_admin_get_active_components_from_submitted_settings( $submitted ) {
 319      $current_action = 'all';
 320  
 321      if ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'active', 'inactive', 'retired' ) ) ) {
 322          $current_action = $_GET['action'];
 323      }
 324  
 325      $current_components = buddypress()->active_components;
 326  
 327      switch ( $current_action ) {
 328          case 'retired' :
 329              $retired_components = bp_core_admin_get_components( 'retired' );
 330              foreach ( array_keys( $retired_components ) as $retired_component ) {
 331                  if ( ! isset( $submitted[ $retired_component ] ) ) {
 332                      unset( $current_components[ $retired_component ] );
 333                  }
 334              } // Fall through.
 335  
 336  
 337          case 'inactive' :
 338              $components = array_merge( $submitted, $current_components );
 339              break;
 340  
 341          case 'all' :
 342          case 'active' :
 343          default :
 344              $components = $submitted;
 345              break;
 346      }
 347  
 348      return $components;
 349  }
 350  
 351  /**
 352   * Return a list of component information.
 353   *
 354   * We use this information both to build the markup for the admin screens, as
 355   * well as to do some processing on settings data submitted from those screens.
 356   *
 357   * @since 1.7.0
 358   *
 359   * @param string $type Optional; component type to fetch. Default value is 'all', or 'optional', 'retired', 'required'.
 360   * @return array Requested components' data.
 361   */
 362  function bp_core_admin_get_components( $type = 'all' ) {
 363      $components = bp_core_get_components( $type );
 364  
 365      /**
 366       * Filters the list of component information.
 367       *
 368       * @since 2.0.0
 369       *
 370       * @param array  $components Array of component information.
 371       * @param string $type       Type of component list requested.
 372       *                           Possible values include 'all', 'optional',
 373       *                           'retired', 'required'.
 374       */
 375      return apply_filters( 'bp_core_admin_get_components', $components, $type );
 376  }


Generated: Fri Jul 19 01:01:43 2019 Cross-referenced by PHPXref 0.7.1