[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/users/ -> signups.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Signups
   5   *
   6   * This file contains functions for assisting with adding forum data to user
   7   * accounts during signup, account creation, and invitation.
   8   *
   9   * @package bbPress
  10   * @subpackage Signups
  11   */
  12  
  13  // Exit if accessed directly
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Output the forum-role field when adding a new user
  18   *
  19   * @since 2.6.0 bbPress (r6674)
  20   */
  21  function bbp_add_user_form_role_field() {
  22  
  23      // Bail if current user cannot promote users
  24      if ( ! current_user_can( 'promote_users' ) ) {
  25          return;
  26      } ?>
  27  
  28      <table class="form-table">
  29          <tr class="form-field">
  30              <th scope="row"><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th>
  31              <td><?php
  32  
  33                  // Default user role
  34                  $default_role  = isset( $_POST['bbp-forums-role'] )
  35                      ? sanitize_key( $_POST['bbp-forums-role'] )
  36                      : bbp_get_default_role();
  37  
  38                  // Get the folum roles
  39                  $dynamic_roles = bbp_get_dynamic_roles();
  40  
  41                  // Only keymasters can set other keymasters
  42                  if ( ! bbp_is_user_keymaster() ) {
  43                      unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
  44                  } ?>
  45  
  46                  <select name="bbp-forums-role" id="bbp-forums-role">
  47  
  48                      <?php foreach ( $dynamic_roles as $role => $details ) : ?>
  49  
  50                          <option <?php selected( $default_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
  51  
  52                      <?php endforeach; ?>
  53  
  54                  </select>
  55              </td>
  56          </tr>
  57      </table>
  58  
  59  <?php
  60  }
  61  
  62  /**
  63   * Maybe add forum role to signup meta array
  64   *
  65   * @since 2.6.0 bbPress (r6674)
  66   *
  67   * @param array $meta
  68   *
  69   * @return array
  70   */
  71  function bbp_user_add_role_to_signup_meta( $meta = array() ) {
  72  
  73      // Bail if already added
  74      if ( ! empty( $meta['bbp_new_role'] ) ) {
  75          return $meta;
  76      }
  77  
  78      // Role to validate
  79      $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] )
  80          ? sanitize_key( $_POST['bbp-forums-role'] )
  81          : '';
  82  
  83      // Validate the signup role
  84      $valid_role = bbp_validate_registration_role( $to_validate );
  85  
  86      // Bail if errors
  87      if ( bbp_has_errors() ) {
  88          return $meta;
  89      }
  90  
  91      // Add role to meta
  92      $meta['bbp_new_role'] = $valid_role;
  93  
  94      // Return meta
  95      return $meta;
  96  }
  97  
  98  /**
  99   * Add forum meta data when inviting a user to a site
 100   *
 101   * @since 2.6.0 bbPress (r6674)
 102   *
 103   * @param int    $user_id     The invited user's ID.
 104   * @param array  $role        The role of invited user.
 105   * @param string $newuser_key The key of the invitation.
 106   *
 107   * @return void
 108   */
 109  function bbp_user_add_role_on_invite( $user_id = '', $role = '', $newuser_key = '' ) {
 110  
 111      // Role to validate
 112      $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] )
 113          ? sanitize_key( $_POST['bbp-forums-role'] )
 114          : '';
 115  
 116      // Validate the signup role
 117      $valid_role = bbp_validate_registration_role( $to_validate );
 118  
 119      // Bail if errors
 120      if ( bbp_has_errors() ) {
 121          return;
 122      }
 123  
 124      // Bail if malformed user key
 125      if ( empty( $newuser_key ) || ! is_string( $newuser_key ) ) {
 126          return;
 127      }
 128  
 129      // Option key
 130      $option_key = 'new_user_' . $newuser_key;
 131  
 132      // Get the user option
 133      $user_option = get_option( $option_key, array() );
 134  
 135      // Add the new role
 136      $user_option['bbp_new_role'] = $valid_role;
 137  
 138      // Update the invitation
 139      update_option( $option_key, $user_option );
 140  }
 141  
 142  /**
 143   * Single-site handler for adding a new user
 144   *
 145   * @since 2.6.0 bbPress (r6674)
 146   *
 147   * @param int $user_id
 148   *
 149   * @return void
 150   */
 151  function bbp_user_add_role_on_register( $user_id = '' ) {
 152  
 153      // Role to validate
 154      $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] )
 155          ? sanitize_key( $_POST['bbp-forums-role'] )
 156          : '';
 157  
 158      // Validate the signup role
 159      $valid_role = bbp_validate_registration_role( $to_validate );
 160  
 161      // Bail if errors
 162      if ( bbp_has_errors() ) {
 163          return;
 164      }
 165  
 166      // Set the user role
 167      bbp_set_user_role( $user_id, $valid_role );
 168  }
 169  
 170  /**
 171   * Multi-site handler for adding a new user
 172   *
 173   * @since 2.6.0 bbPress (r6674)
 174   *
 175   * @param int    $user_id  User ID
 176   * @param string $password User password
 177   * @param array  $meta     Array of metadata
 178   *
 179   * @return void
 180   */
 181  function bbp_user_add_role_on_activate( $user_id = 0, $password = '', $meta = array() ) {
 182  
 183      // Role to validate
 184      $to_validate = ! empty( $meta['bbp_new_role'] ) && is_string( $meta['bbp_new_role'] )
 185          ? sanitize_key( $meta['bbp_new_role'] )
 186          : '';
 187  
 188      // Validate the signup role
 189      $valid_role = bbp_validate_activation_role( $to_validate );
 190  
 191      // Bail if errors
 192      if ( bbp_has_errors() ) {
 193          return;
 194      }
 195  
 196      // Set the user role
 197      bbp_set_user_role( $user_id, $valid_role );
 198  }
 199  
 200  /** Validators ****************************************************************/
 201  
 202  /**
 203   * Validate the Forum role during signup
 204   *
 205   * This helper function performs a number of generic checks, and encapsulates
 206   * the logic used to validate if a Forum Role is valid, typically during new
 207   * user registration, but also when adding an existing user to a site in
 208   * Multisite installations.
 209   *
 210   * @since 2.6.5
 211   *
 212   * @param string $to_validate A role ID to validate
 213   *
 214   * @return string A valid role ID, or empty string on error
 215   */
 216  function bbp_validate_signup_role( $to_validate = '' ) {
 217  
 218      // Default return value
 219      $retval = '';
 220  
 221      // Add error if role is empty
 222      if ( empty( $to_validate ) ) {
 223          bbp_add_error( 'bbp_signup_role_empty', __( '<strong>Error</strong>: Empty role.', 'bbpress' ) );
 224      }
 225  
 226      // Add error if posted role is not a valid role
 227      if ( ! bbp_is_valid_role( $to_validate ) ) {
 228          bbp_add_error( 'bbp_signup_role_invalid', __( '<strong>Error</strong>: Invalid role.', 'bbpress' ) );
 229      }
 230  
 231      // If no errors, set return value to the role to validate
 232      if ( ! bbp_has_errors() ) {
 233          $retval = $to_validate;
 234      }
 235  
 236      // Filter & return
 237      return (string) apply_filters( 'bbp_validate_signup_role', $retval, $to_validate );
 238  }
 239  
 240  /**
 241   * Validate the Forum role during the registration process
 242   *
 243   * @since 2.6.5
 244   *
 245   * @param string $to_validate A role ID to validate
 246   *
 247   * @return string A valid role ID, or empty string on error
 248   */
 249  function bbp_validate_registration_role( $to_validate = '' ) {
 250  
 251      // Default return value
 252      $retval = bbp_get_default_role();
 253  
 254      /**
 255       * Conditionally accept admin-area posted values for capable users. This is
 256       * to allow for Site/Network Admins to assign a default role when inviting
 257       * or creating a new User account.
 258       */
 259      if ( is_admin() && current_user_can( 'create_users' ) ) {
 260          $retval = $to_validate;
 261      }
 262  
 263      // Validate & return
 264      return bbp_validate_signup_role( $retval );
 265  }
 266  
 267  /**
 268   * Validate the Forum role during multisite activation
 269   *
 270   * This function exists simply for parity with registrations, and to maintain an
 271   * intentional layer of abstraction from the more generic function it uses.
 272   *
 273   * Note: this will not fire inside of wp-activate.php unless it is hooked in
 274   * during sunrise.php, and is considered an advanced use-case.
 275   *
 276   * @since 2.6.5
 277   *
 278   * @param string $to_validate A role ID to validate
 279   *
 280   * @return string A valid role ID, or empty string on error
 281   */
 282  function bbp_validate_activation_role( $to_validate = '' ) {
 283  
 284      // Validate & return
 285      return bbp_validate_signup_role( $to_validate );
 286  }


Generated: Tue Jul 14 01:01:21 2020 Cross-referenced by PHPXref 0.7.1