[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-settings/ -> bp-settings-functions.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Settings Functions
   4   *
   5   * @package BuddyPress
   6   * @subpackage SettingsFunctions
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Update email notification settings for a specific user.
  15   *
  16   * @since 2.3.5
  17   *
  18   * @param int   $user_id  ID of the user whose settings are being updated.
  19   * @param array $settings Settings array.
  20   */
  21  function bp_settings_update_notification_settings( $user_id, $settings ) {
  22      $user_id = (int) $user_id;
  23  
  24      $settings = bp_settings_sanitize_notification_settings( $settings );
  25      foreach ( $settings as $setting_key => $setting_value ) {
  26          bp_update_user_meta( $user_id, $setting_key, $setting_value );
  27      }
  28  }
  29  
  30  /**
  31   * Sanitize email notification settings as submitted by a user.
  32   *
  33   * @since 2.3.5
  34   *
  35   * @param array $settings Array of settings.
  36   * @return array Sanitized settings.
  37   */
  38  function bp_settings_sanitize_notification_settings( $settings = array() ) {
  39      $sanitized_settings = array();
  40  
  41      if ( empty( $settings ) ) {
  42          return $sanitized_settings;
  43      }
  44  
  45      // Get registered notification keys.
  46      $registered_notification_settings = bp_settings_get_registered_notification_keys();
  47  
  48      /*
  49       * We sanitize values for core notification keys.
  50       *
  51       * @todo use register_meta()
  52       */
  53      $core_notification_settings = array(
  54          'notification_messages_new_message',
  55          'notification_activity_new_mention',
  56          'notification_activity_new_reply',
  57          'notification_groups_invite',
  58          'notification_groups_group_updated',
  59          'notification_groups_admin_promotion',
  60          'notification_groups_membership_request',
  61          'notification_membership_request_completed',
  62          'notification_friends_friendship_request',
  63          'notification_friends_friendship_accepted',
  64      );
  65  
  66      foreach ( (array) $settings as $key => $value ) {
  67          // Skip if not a registered setting.
  68          if ( ! in_array( $key, $registered_notification_settings, true ) ) {
  69              continue;
  70          }
  71  
  72          // Force core keys to 'yes' or 'no' values.
  73          if ( in_array( $key, $core_notification_settings, true ) ) {
  74              $value = 'yes' === $value ? 'yes' : 'no';
  75          }
  76  
  77          $sanitized_settings[ $key ] = $value;
  78      }
  79  
  80      return $sanitized_settings;
  81  }
  82  
  83  /**
  84   * Build a dynamic list of allowed notification keys, based on what's hooked to 'bp_notification_settings'.
  85   *
  86   * @since 2.3.5
  87   *
  88   * @return array
  89   */
  90  function bp_settings_get_registered_notification_keys() {
  91  
  92      ob_start();
  93      /**
  94       * Fires at the start of the building of the notification keys allowed list.
  95       *
  96       * @since 1.0.0
  97       */
  98      do_action( 'bp_notification_settings' );
  99      $screen = ob_get_clean();
 100  
 101      $matched = preg_match_all( '/<input[^>]+name="notifications\[([^\]]+)\]/', $screen, $matches );
 102  
 103      if ( $matched && isset( $matches[1] ) ) {
 104          $allowed_key_list = $matches[1];
 105      } else {
 106          $allowed_key_list = array();
 107      }
 108  
 109      return $allowed_key_list;
 110  }
 111  
 112  /**
 113   * Finds and exports personal data associated with an email address from the Settings component.
 114   *
 115   * @since 4.0.0
 116   *
 117   * @param string $email_address  The user's email address.
 118   * @param int    $page           Batch number.
 119   * @return array An array of personal data.
 120   */
 121  function bp_settings_personal_data_exporter( $email_address, $page ) {
 122      $email_address = trim( $email_address );
 123  
 124      $data_to_export = array();
 125  
 126      $user = get_user_by( 'email', $email_address );
 127  
 128      if ( ! $user ) {
 129          return array(
 130              'data' => array(),
 131              'done' => true,
 132          );
 133      }
 134  
 135      $yes = __( 'Yes', 'buddypress' );
 136      $no  = __( 'No', 'buddypress' );
 137  
 138      $user_settings = array();
 139  
 140      // These settings all default to 'yes' when nothing is saved, so we have to do some pre-processing.
 141      $notification_settings = array();
 142  
 143      if ( bp_is_active( 'activity' ) ) {
 144          $notification_settings[] = array(
 145              'name' => __( 'Receive email when a member mentions you in an update?', 'buddypress' ),
 146              'key'  => 'notification_activity_new_mention',
 147          );
 148          $notification_settings[] = array(
 149              'name' => __( 'Receive email when a member replies to an update or comment you\'ve posted?', 'buddypress' ),
 150              'key'  => 'notification_activity_new_reply',
 151          );
 152      }
 153  
 154      if ( bp_is_active( 'messages' ) ) {
 155          $notification_settings[] = array(
 156              'name' => __( 'Receive email when a member sends you a new message?', 'buddypress' ),
 157              'key'  => 'notification_messages_new_message',
 158          );
 159      }
 160  
 161      if ( bp_is_active( 'friends' ) ) {
 162          $notification_settings[] = array(
 163              'name' => __( 'Receive email when a member invites you to join a group?', 'buddypress' ),
 164              'key'  => 'notification_groups_invite',
 165          );
 166      }
 167  
 168      if ( bp_is_active( 'groups' ) ) {
 169          $notification_settings[] = array(
 170              'name' => __( 'Receive email when group information is updated?', 'buddypress' ),
 171              'key'  => 'notification_groups_group_updated',
 172          );
 173          $notification_settings[] = array(
 174              'name' => __( 'Receive email when you are promoted to a group administrator or moderator?', 'buddypress' ),
 175              'key'  => 'notification_groups_admin_promoted',
 176          );
 177          $notification_settings[] = array(
 178              'name' => __( 'Receive email when a member requests to join a private group for which you are an admin?', 'buddypress' ),
 179              'key'  => 'notification_groups_membership_request',
 180          );
 181          $notification_settings[] = array(
 182              'name' => __( 'Receive email when your request to join a group has been approved or denied?', 'buddypress' ),
 183              'key'  => 'notification_membership_request_completed',
 184          );
 185      }
 186  
 187      foreach ( $notification_settings as $notification_setting ) {
 188          $user_notification_setting = bp_get_user_meta( $user->ID, $notification_setting['key'], true );
 189          if ( empty( $user_notification_setting ) ) {
 190              $user_notification_setting = 'yes';
 191          }
 192  
 193          $user_settings[] = array(
 194              'name'  => $notification_setting['name'],
 195              'value' => 'yes' === $user_notification_setting ? $yes : $no,
 196          );
 197      }
 198  
 199      if ( function_exists( 'bp_nouveau_groups_get_group_invites_setting' ) ) {
 200          $user_settings[] = array(
 201              'name'  => __( 'Receive group invitations from my friends only?', 'buddypress' ),
 202              'value' => bp_nouveau_groups_get_group_invites_setting() ? $yes : $no,
 203          );
 204      }
 205  
 206      $data_to_export[] = array(
 207          'group_id'    => 'bp_settings',
 208          'group_label' => __( 'Settings', 'buddypress' ),
 209          'item_id'     => "bp-settings-{$user->ID}",
 210          'data'        => $user_settings,
 211      );
 212  
 213      return array(
 214          'data' => $data_to_export,
 215          'done' => true,
 216      );
 217  }
 218  
 219  /**
 220   * Fetches a user's personal data request.
 221   *
 222   * @since 4.0.0
 223   *
 224   * @param int $user_id WP user ID.
 225   * @return WP_User_Request|bool WP_User_Request object on success, bool false on failure.
 226   */
 227  function bp_settings_get_personal_data_request( $user_id = 0 ) {
 228      if ( empty( $user_id ) ) {
 229          $user_id = bp_displayed_user_id();
 230      }
 231  
 232      if ( empty( $user_id ) ) {
 233          return false;
 234      }
 235  
 236      $user = get_userdata( $user_id );
 237      if ( empty( $user ) ) {
 238          return false;
 239      }
 240  
 241      $query = new WP_Query( array(
 242          'author'        => (int) $user_id,
 243          'post_type'     => 'user_request',
 244          'post_status'   => 'any',
 245          'post_name__in' => array(
 246              'export_personal_data',
 247          ),
 248      ) );
 249  
 250      if ( ! empty( $query->post ) ) {
 251          return wp_get_user_request( $query->post->ID );
 252      } else {
 253          return false;
 254      }
 255  }
 256  
 257  /**
 258   * Fetches the expiration date for when a user request expires.
 259   *
 260   * @since 4.0.0
 261   *
 262   * @param WP_User_Request $request User request object.
 263   * @return string Formatted date.
 264   */
 265  function bp_settings_get_personal_data_expiration_date( WP_User_Request $request ) {
 266      /** This filter is documented in wp-admin/includes/file.php */
 267      $expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
 268  
 269      return bp_format_time( $request->completed_timestamp + $expiration, true );
 270  }
 271  
 272  /**
 273   * Fetches the confirmation date for a user request object.
 274   *
 275   * @since 4.0.0
 276   *
 277   * @param WP_User_Request $request User request object.
 278   * @return string Formatted date for the confirmation date.
 279   */
 280  function bp_settings_get_personal_data_confirmation_date( WP_User_Request $request ) {
 281      return bp_format_time( $request->confirmed_timestamp, true );
 282  }
 283  
 284  /**
 285   * Fetches the URL for a personal data export file.
 286   *
 287   * @since 4.0.0
 288   *
 289   * @param WP_User_Request $request User request object.
 290   * @return string Export file URL.
 291   */
 292  function bp_settings_get_personal_data_export_url( WP_User_Request $request ) {
 293      return get_post_meta( $request->ID, '_export_file_url', true );
 294  }
 295  
 296  /**
 297   * Check if the generated data export file still exists or not.
 298   *
 299   * @since 4.0.0
 300   *
 301   * @param  WP_User_Request $request User request object.
 302   * @return bool
 303   */
 304  function bp_settings_personal_data_export_exists( WP_User_Request $request ) {
 305      $file = get_post_meta( $request->ID, '_export_file_path', true );
 306      return file_exists( $file );
 307  }
 308  
 309  /**
 310   * Template tag to output a list of data exporter items.
 311   *
 312   * Piggybacks off of the 'wp_privacy_personal_data_exporters' filter and the
 313   * 'exporter_friendly_name' key, which is meant for the admin area.
 314   *
 315   * @since 4.0.0
 316   * @since 5.0.0 Looks for a potential exporter's BP/custom friendly name.
 317   */
 318  function bp_settings_data_exporter_items() {
 319      /** This filter is documented in /wp-admin/includes/ajax-actions.php */
 320      $exporters             = apply_filters( 'wp_privacy_personal_data_exporters', array() );
 321      $custom_friendly_names = apply_filters( 'bp_settings_data_custom_friendly_names', array(
 322          'wordpress-comments' => _x( 'Comments', 'WP Comments data exporter friendly name', 'buddypress' ),
 323          'wordpress-media'    => _x( 'Media', 'WP Media data exporter friendly name', 'buddypress' ),
 324          'wordpress-user'     => _x( 'Personal information', 'WP Media data exporter friendly name', 'buddypress' ),
 325      ) );
 326  
 327  ?>
 328      <ul>
 329      <?php foreach ( $exporters as $exporter => $data ) :
 330          // Use the exporter friendly name by default.
 331          $friendly_name = $data['exporter_friendly_name'];
 332  
 333          /**
 334           * Use the exporter friendly name if directly available
 335           * into the exporters array.
 336           */
 337          if ( isset( $data['exporter_bp_friendly_name'] ) ) {
 338              $friendly_name = $data['exporter_bp_friendly_name'];
 339  
 340          // Look for a potential match into the custom friendly names.
 341          } elseif ( isset( $custom_friendly_names[ $exporter ] ) ) {
 342              $friendly_name = $custom_friendly_names[ $exporter ];
 343          }
 344  
 345          /**
 346           * Filters the data exporter friendly name for display on the "Settings > Data" page.
 347           *
 348           * @since 4.0.0
 349           * @since 5.0.0 replaces the `$name` parameter with the `$friendly_name` one.
 350           *
 351           * @param string $friendly_name Data exporter friendly name.
 352           * @param string $exporter      Internal exporter name.
 353           */
 354          $item = apply_filters( 'bp_settings_data_exporter_name', esc_html( $friendly_name ), $exporter );
 355      ?>
 356  
 357          <li><?php echo $item; ?></li>
 358  
 359      <?php endforeach; ?>
 360      </ul>
 361  
 362  <?php
 363  }


Generated: Thu Dec 5 01:00:58 2024 Cross-referenced by PHPXref 0.7.1