[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-members/ -> bp-members-membership-requests.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Membership Requests
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersMembershipRequest
   7   * @since 10.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Single site: When a user creates a membership request,
  15   * prevent the sending of the activation email so that
  16   * the site admins can send it manually.
  17   *
  18   * @since 10.0.0
  19   *
  20   * @param bool   $send           Whether or not to send the activation key.
  21   * @param int    $user_id        User ID to send activation key to.
  22   * @param string $user_email     User email to send activation key to.
  23   * @param string $activation_key Activation key to be sent.
  24   * @param array  $usermeta       Miscellaneous metadata about the user (blog-specific
  25   *                               signup data, xprofile data, etc).
  26   * @return bool Whether or not to send the activation key.
  27   */
  28  function bp_members_membership_requests_cancel_activation_email( $send, $user_id = 0, $user_email = '', $activation_key = '', $usermeta = array() ) {
  29  
  30      $details = array(
  31          'user_id'        => $user_id,
  32          'user_email'     => $user_email,
  33          'activation_key' => $activation_key,
  34          'usermeta'       => $usermeta,
  35      );
  36  
  37      /**
  38       * Allow some membership requests to be approved immediately.
  39       * For example, you might want to approve all requests
  40       * coming from users with certain email address domains.
  41       * If `true` is returned the activation email will be sent to the user.
  42       *
  43       * @since 10.0.0
  44       *
  45       * @param bool  $send    Whether or not this membership request should be approved
  46       *                       immediately and the activation email sent.
  47       *                       Default is `false` meaning that the request should be
  48       *                       manually approved by a site admin.
  49       * @param array $details The details of the request.
  50       */
  51      $send = apply_filters( 'bp_members_membership_requests_bypass_manual_approval', false, $details );
  52  
  53      // If the registration process has been interrupted, this is a new membership request.
  54      if ( ! $send ) {
  55          $signup = bp_members_get_signup_by( 'activation_key', $activation_key );
  56  
  57          /**
  58           * Fires when a site membership request has been created and is pending.
  59           *
  60           * @since 10.0.0
  61           *
  62           * @param BP_Signup $signup  The signup object that has been created.
  63           * @param array     $details The details of the request.
  64           */
  65          do_action( 'bp_members_membership_request_submitted', $signup, $details );
  66      }
  67  
  68      return $send;
  69  }
  70  add_filter( 'bp_core_signup_send_activation_key', 'bp_members_membership_requests_cancel_activation_email', 10, 5 );
  71  
  72  /**
  73   * WP Multisite: When a user creates a membership request,
  74   * prevent the sending of the activation email so that
  75   * the site admins can send it manually.
  76   *
  77   * @since 10.0.0
  78   *
  79   * @param bool   $send             Whether or not to send the activation key.
  80   * @param string $user_login       User login name.
  81   * @param string $user_email       User email address.
  82   * @param string $activation_key   Activation key created in wpmu_signup_user().
  83   * @param bool   $is_signup_resend Is the site admin sending this email?
  84   * @return bool Whether or not to send the activation key.
  85   */
  86  function bp_members_membership_requests_cancel_activation_email_multisite( $send = true, $user_login = '', $user_email = '', $activation_key = '', $is_signup_resend = false ) {
  87  
  88      $details = array(
  89          'user_login'       => $user_login,
  90          'user_email'       => $user_email,
  91          'activation_key'   => $activation_key,
  92          'is_signup_resend' => $is_signup_resend,
  93      );
  94  
  95      // Allow the site admin to send/resend approval emails.
  96      if ( $is_signup_resend ) {
  97          $to_send = true;
  98      } else {
  99          $to_send = false;
 100      }
 101  
 102      /**
 103       * Allow some membership requests to be approved immediately.
 104       * For example, you might want to approve all requests
 105       * coming from users with certain email address domains.
 106       * If `true` is returned the activation email will be sent to the user.
 107       *
 108       * @since 10.0.0
 109       *
 110       * @param bool  $to_send Whether or not this membership request should be approved
 111       *                       immediately and the activation email sent.
 112       *                       Default is `false` meaning that the request should be
 113       *                       manually approved by a site admin.
 114       * @param array $details The details of the request.
 115       */
 116      $send = apply_filters( 'bp_members_membership_requests_bypass_manual_approval_multisite', $to_send, $details );
 117  
 118      // If the registration process has been interrupted, this is a new membership request.
 119      if ( ! $send ) {
 120          $signup = bp_members_get_signup_by( 'activation_key', $activation_key );
 121  
 122          /**
 123           * Fires when a site membership request has been created and is pending.
 124           *
 125           * @since 10.0.0
 126           *
 127           * @param BP_Signup $signup  The signup object that has been created.
 128           * @param array     $details The details of the request.
 129           */
 130          do_action( 'bp_members_membership_request_submitted', $signup, $details );
 131      }
 132  
 133      return $send;
 134  }
 135  add_filter( 'bp_core_signup_send_activation_key_multisite', 'bp_members_membership_requests_cancel_activation_email_multisite', 10, 5 );
 136  add_filter( 'bp_core_signup_send_activation_key_multisite_blog', 'bp_members_membership_requests_cancel_activation_email_multisite', 10, 5 );
 137  
 138  /**
 139   * Notifications
 140   *********************************************************************/
 141  
 142  /**
 143   * Notify site admins about a new membership request.
 144   *
 145   * @since 10.0.0
 146   *
 147   * @param BP_Signup $signup The signup object that has been created.
 148   */
 149  function bp_members_membership_requests_notify_site_admins( $signup ) {
 150  
 151      if ( ! isset( $signup->signup_id ) ) {
 152          return;
 153      }
 154  
 155      // Notify all site admins so the request can be handled.
 156      $admin_ids = get_users(
 157          array(
 158              'fields' => 'ids',
 159              'role'   => 'administrator',
 160          )
 161      );
 162  
 163      foreach ( $admin_ids as $admin_id ) {
 164          // Trigger a BuddyPress Notification.
 165          if ( bp_is_active( 'notifications' ) ) {
 166              bp_notifications_add_notification(
 167                  array(
 168                      'user_id'          => $admin_id,
 169                      'item_id'          => $signup->signup_id,
 170                      'component_name'   => buddypress()->members->id,
 171                      'component_action' => 'membership_request_submitted',
 172                      'date_notified'    => bp_core_current_time(),
 173                      'is_new'           => 1,
 174                  )
 175              );
 176          }
 177  
 178          // Bail if member opted out of receiving this email.
 179          if ( 'no' === bp_get_user_meta( $admin_id, 'notification_members_membership_request', true ) ) {
 180              return;
 181          }
 182  
 183          $unsubscribe_args = array(
 184              'user_id'           => $admin_id,
 185              'notification_type' => 'members-membership-request',
 186          );
 187  
 188          $manage_url = add_query_arg(
 189              array(
 190                  'mod_req'   => 1,
 191                  'page'      => 'bp-signups',
 192                  'signup_id' => $signup->signup_id,
 193                  'action'    => 'resend',
 194              ),
 195              bp_get_admin_url( 'users.php' )
 196          );
 197  
 198          $args  = array(
 199              'tokens' => array(
 200                  'admin.id'                   => $admin_id,
 201                  'manage.url'                 => esc_url_raw( $manage_url ),
 202                  'requesting-user.user_login' => esc_html( $signup->user_login ),
 203                  'unsubscribe'                => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
 204              ),
 205          );
 206  
 207          bp_send_email( 'members-membership-request', (int) $admin_id, $args );
 208      }
 209  }
 210  add_action( 'bp_members_membership_request_submitted', 'bp_members_membership_requests_notify_site_admins' );
 211  
 212  /**
 213   * Send a message to the requesting user when his or her
 214   * site membership request has been rejected.
 215   *
 216   * @since 10.0.0
 217   *
 218   * @param array $signup_ids Array of pending IDs to delete.
 219   */
 220  function bp_members_membership_requests_send_rejection_mail( $signup_ids ) {
 221      $signups = BP_Signup::get(
 222          array(
 223              'include' => $signup_ids,
 224          )
 225      );
 226  
 227      if ( empty( $signups['signups'] ) ) {
 228          return;
 229      }
 230  
 231      foreach ( $signups['signups'] as $signup ) {
 232          if ( ! empty( $signup->user_email ) ) {
 233              bp_send_email( 'members-membership-request-rejected', $signup->user_email );
 234          }
 235      }
 236  }
 237  add_action( 'bp_core_signup_before_delete', 'bp_members_membership_requests_send_rejection_mail' );
 238  
 239  /**
 240   * When a request is approved, activated or deleted,
 241   * delete the associated notifications.
 242   *
 243   * @since 10.0.0
 244   *
 245   * @param array $signup_ids Array of changing signup IDs.
 246   */
 247  function bp_members_membership_requests_delete_notifications_on_change( $signup_ids ) {
 248      foreach ( $signup_ids as $signup_id ) {
 249          BP_Notifications_Notification::delete(
 250              array(
 251                  'item_id'          => $signup_id,
 252                  'component_action' => 'membership_request_submitted',
 253              )
 254          );
 255      }
 256  }
 257  add_action( 'bp_core_signup_after_resend',   'bp_members_membership_requests_delete_notifications_on_change' );
 258  add_action( 'bp_core_signup_after_activate', 'bp_members_membership_requests_delete_notifications_on_change' );
 259  add_action( 'bp_core_signup_after_delete',   'bp_members_membership_requests_delete_notifications_on_change' );
 260  
 261  /**
 262   * In the Nouveau template pack, when membership requests are required,
 263   * change registration form submit button label to "Submit Request".
 264   *
 265   * @since 10.0.0
 266   *
 267   * @return string $retval the HTML for the request membership link.
 268   */
 269  function bp_members_membership_requests_filter_complete_signup_button( $buttons ) {
 270  
 271      $buttons['register']['attributes']['value'] = __( 'Submit Request', 'buddypress' );
 272      return $buttons;
 273  }
 274  add_filter( 'bp_nouveau_get_submit_button', 'bp_members_membership_requests_filter_complete_signup_button' );
 275  
 276  /**
 277   * Administration: Change certain behavior and labels
 278   * on the WP Admin > Users > Manage Signups screen.
 279   *********************************************************************/
 280  
 281  /**
 282   * Filter the actions available on the signups list table.
 283   *
 284   * @since 10.0.0
 285   *
 286   * @param array  $actions       Array of actions and corresponding links.
 287   * @param object $signup_object The signup data object.
 288   */
 289  function bp_members_membership_requests_filter_signup_row_actions( $actions, $signup_object ) {
 290  
 291      // Rename the "email" resend option when membership requests are active.
 292      $email_link = add_query_arg(
 293          array(
 294              'page'        => 'bp-signups',
 295              'signup_id' => $signup_object->id,
 296              'action'    => 'resend',
 297          ),
 298          bp_get_admin_url( 'users.php' )
 299      );
 300  
 301      $resend_label = ( 0 === $signup_object->count_sent ) ? __( 'Approve Request', 'buddypress' ) : __( 'Resend Approval', 'buddypress' );
 302  
 303      $actions['resend'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $email_link ), esc_html( $resend_label ) );
 304  
 305      // Add a link to view profile info when membership requests and xprofile are active.
 306      if ( bp_is_active( 'xprofile' ) || bp_members_site_requests_enabled() ) {
 307          $profile_link = add_query_arg(
 308              array(
 309                  'page'       => 'bp-signups#TB_inline',
 310                  'inlineId' => 'signup-info-modal-' . $signup_object->id,
 311              ),
 312              bp_get_admin_url( 'users.php' )
 313          );
 314  
 315          $actions['profile'] = sprintf( '<a href="%1$s" class="bp-thickbox">%2$s</a>', esc_url( $profile_link ), esc_html__( 'Profile Info', 'buddypress' ) );
 316      }
 317  
 318      return $actions;
 319  }
 320  add_filter( 'bp_members_ms_signup_row_actions', 'bp_members_membership_requests_filter_signup_row_actions', 10, 2 );
 321  
 322  /**
 323   * Filter the bulk actions available on the signups list table.
 324   *
 325   * @since 10.0.0
 326   *
 327   * @param array $actions Array of actions and corresponding links.
 328   * @return array         List of actions and corresponding links.
 329   */
 330  function bp_members_membership_requests_filter_signup_bulk_actions( $actions ) {
 331      // Rename the "email" resend option when membership requests are active.
 332      $actions['resend'] = esc_html_x( 'Approve', 'Pending signup action', 'buddypress' );
 333      return $actions;
 334  }
 335  add_filter( 'bp_members_ms_signup_bulk_actions', 'bp_members_membership_requests_filter_signup_bulk_actions' );
 336  
 337  /**
 338   * Filter the "Last Sent" column header on the pending users table.
 339   *
 340   * @since 10.0.0
 341   *
 342   * @param array $columns Array of columns to display.
 343   * @return array List of columns to display.
 344   */
 345  function bp_members_membership_requests_filter_signup_table_date_sent_header( $columns ) {
 346      $columns['date_sent'] = esc_html__( 'Approved', 'buddypress' );
 347      return $columns;
 348  }
 349  add_filter( 'bp_members_signup_columns', 'bp_members_membership_requests_filter_signup_table_date_sent_header' );
 350  add_filter( 'bp_members_ms_signup_columns', 'bp_members_membership_requests_filter_signup_table_date_sent_header' );
 351  
 352  /**
 353   * Filter the "Last Sent" column message on the pending users table.
 354   *
 355   * @since 10.0.0
 356   *
 357   * @param string      $message "Not yet sent" message.
 358   * @param object|null $signup  Signup object instance.
 359   * @return string              "Not yet approved" message, if needed. Unchanged message otherwise.
 360   */
 361  function bp_members_membership_requests_filter_signup_table_unsent_message( $message, $signup ) {
 362      if ( 0 === $signup->count_sent ) {
 363          $message = esc_html__( 'Not yet approved', 'buddypress' );
 364      }
 365  
 366      return $message;
 367  }
 368  add_filter( 'bp_members_signup_date_sent_unsent_message', 'bp_members_membership_requests_filter_signup_table_unsent_message', 10, 2 );
 369  add_filter( 'bp_members_ms_signup_date_sent_unsent_message', 'bp_members_membership_requests_filter_signup_table_unsent_message', 10, 2 );
 370  
 371  /**
 372   * Filter/add "Request Membership" links in the following locations:
 373   * - BP login widget,
 374   * - Sidebar register link,
 375   * - WP Toolbar,
 376   * - WP login form.
 377   *********************************************************************/
 378  
 379  /**
 380   * Add "Request Membership" link to Widget login form.
 381   *
 382   * @since 10.0.0
 383   *
 384   * @return string $retval the HTML for the request membership link.
 385   */
 386  function bp_members_membership_requests_add_link_to_widget_login_form() {
 387      ?>
 388      <span class="bp-login-widget-request-membership-link"><a href="<?php echo esc_url( bp_get_signup_page() ); ?>"><?php esc_html_e( 'Request Membership', 'buddypress' ); ?></a></span>
 389      <?php
 390  }
 391  add_action( 'bp_login_widget_form', 'bp_members_membership_requests_add_link_to_widget_login_form' );
 392  
 393  /**
 394   * Filter the "Register" link from `wp_register()` as used in
 395   * `sidebar.php` and the WP Core meta widget.
 396   *
 397   * @since 10.0.0
 398   *
 399   * @param string $link The HTML code for the link to the Registration or Admin page.
 400   * @return string      An empty string or the HTML code for the link to the Membership request page.
 401   */
 402  function bp_members_membership_requests_filter_sidebar_register_link( $link ) {
 403      // $link should be an empty string when public registration is disabled.
 404      if ( ! is_user_logged_in() && empty( $link ) ) {
 405          $link = '<a href="' . esc_url( wp_registration_url() ) . '">' . esc_html__( 'Request Membership', 'buddypress' ) . '</a>';
 406      }
 407  
 408      return $link;
 409  }
 410  add_filter( 'register', 'bp_members_membership_requests_filter_sidebar_register_link' );
 411  
 412  /**
 413   * Add a "Request Membership" link to the WP Toolbar.
 414   * Priority 21 should place it just after the "Log In" link.
 415   *
 416   * @since 10.0.0
 417   *
 418   * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
 419   */
 420  function bp_members_membership_requests_add_toolbar_link( $wp_admin_bar ) {
 421      if ( is_user_logged_in() ) {
 422          return;
 423      }
 424  
 425      $args = array(
 426          'id'    => 'bp-request-membership',
 427          'title' => __( 'Request Membership', 'buddypress' ),
 428          'href'  => wp_registration_url(),
 429          'meta'  => array(
 430              'class' => 'buddypress-request-membership',
 431              'title' => __( 'Request Membership', 'buddypress' ),
 432          ),
 433      );
 434  
 435      $wp_admin_bar->add_node( $args );
 436  }
 437  add_action( 'admin_bar_menu', 'bp_members_membership_requests_add_toolbar_link', 21 );
 438  
 439  /**
 440   * Add a "Request Membership" link to the WP Login form.
 441   *
 442   * @since 10.0.0
 443   *
 444   * @param string $link HTML link to the home URL of the current site.
 445   * @return string      HTML link to the home URL of the current site and the one to request a membership.
 446   */
 447  function bp_members_membership_requests_add_link_wp_login( $link ) {
 448      $link_separator = apply_filters( 'login_link_separator', ' | ' );
 449  
 450      return $link . $link_separator . '<a href="' . esc_url( wp_registration_url() ) . '">' . esc_html__( 'Request Membership', 'buddypress' ) . '</a>';
 451  }
 452  add_action( 'login_site_html_link', 'bp_members_membership_requests_add_link_wp_login' );


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1