[ 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 whitelist of 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 notification keys whitelisting.
  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          $key_whitelist = $matches[1];
 105      } else {
 106          $key_whitelist = array();
 107      }
 108  
 109      return $key_whitelist;
 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 WP user ID.
 225   * @return WP_User_Request|false WP_User_Request object on success, boolean 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          // WP 5.4 changed the user request function name to wp_get_user_request()
 252          $user_request = function_exists( 'wp_get_user_request' ) ? 'wp_get_user_request' : 'wp_get_user_request_data';
 253          return $user_request( $query->post->ID );
 254      } else {
 255          return false;
 256      }
 257  }
 258  
 259  /**
 260   * Fetches the expiration date for when a user request expires.
 261   *
 262   * @since 4.0.0
 263   *
 264   * @param WP_User_Request $request User request object.
 265   * @return string Formatted date.
 266   */
 267  function bp_settings_get_personal_data_expiration_date( WP_User_Request $request ) {
 268      /** This filter is documented in wp-admin/includes/file.php */
 269      $expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
 270  
 271      return bp_format_time( $request->completed_timestamp + $expiration, true );
 272  }
 273  
 274  /**
 275   * Fetches the confirmation date for a user request object.
 276   *
 277   * @since 4.0.0
 278   *
 279   * @param WP_User_Request $request User request object.
 280   * @return string Formatted date for the confirmation date.
 281   */
 282  function bp_settings_get_personal_data_confirmation_date( WP_User_Request $request ) {
 283      return bp_format_time( $request->confirmed_timestamp, true );
 284  }
 285  
 286  /**
 287   * Fetches the URL for a personal data export file.
 288   *
 289   * @since 4.0.0
 290   *
 291   * @param WP_User_Request $request User request object.
 292   * @return string Export file URL.
 293   */
 294  function bp_settings_get_personal_data_export_url( WP_User_Request $request ) {
 295      return get_post_meta( $request->ID, '_export_file_url', true );
 296  }
 297  
 298  /**
 299   * Check if the generated data export file still exists or not.
 300   *
 301   * @since 4.0.0
 302   *
 303   * @param  WP_User_Request $request User request object.
 304   * @return bool
 305   */
 306  function bp_settings_personal_data_export_exists( WP_User_Request $request ) {
 307      $file = get_post_meta( $request->ID, '_export_file_path', true );
 308      if ( file_exists( $file ) ) {
 309          return true;
 310      } else {
 311          return false;
 312      }
 313  }
 314  
 315  /**
 316   * Template tag to output a list of data exporter items.
 317   *
 318   * Piggybacks off of the 'wp_privacy_personal_data_exporters' filter and the
 319   * 'exporter_friendly_name' key, which is meant for the admin area.
 320   *
 321   * @since 4.0.0
 322   * @since 5.0.0 Looks for a potential exporter's BP/custom friendly name.
 323   */
 324  function bp_settings_data_exporter_items() {
 325      /** This filter is documented in /wp-admin/includes/ajax-actions.php */
 326      $exporters             = apply_filters( 'wp_privacy_personal_data_exporters', array() );
 327      $custom_friendly_names = apply_filters( 'bp_settings_data_custom_friendly_names', array(
 328          'wordpress-comments' => _x( 'Comments', 'WP Comments data exporter friendly name', 'buddypress' ),
 329          'wordpress-media'    => _x( 'Media', 'WP Media data exporter friendly name', 'buddypress' ),
 330          'wordpress-user'     => _x( 'Personal information', 'WP Media data exporter friendly name', 'buddypress' ),
 331      ) );
 332  
 333  ?>
 334      <ul>
 335      <?php foreach ( $exporters as $exporter => $data ) :
 336          // Use the exporter friendly name by default.
 337          $friendly_name = $data['exporter_friendly_name'];
 338  
 339          /**
 340           * Use the exporter friendly name if directly available
 341           * into the exporters array.
 342           */
 343          if ( isset( $data['exporter_bp_friendly_name'] ) ) {
 344              $friendly_name = $data['exporter_bp_friendly_name'];
 345  
 346          // Look for a potential match into the custom friendly names.
 347          } elseif ( isset( $custom_friendly_names[ $exporter ] ) ) {
 348              $friendly_name = $custom_friendly_names[ $exporter ];
 349          }
 350  
 351          /**
 352           * Filters the data exporter friendly name for display on the "Settings > Data" page.
 353           *
 354           * @since 4.0.0
 355           * @since 5.0.0 replaces the `$name` parameter with the `$friendly_name` one.
 356           *
 357           * @param string $friendly_name Data exporter friendly name.
 358           * @param string $exporter      Internal exporter name.
 359           */
 360          $item = apply_filters( 'bp_settings_data_exporter_name', esc_html( $friendly_name ), $exporter );
 361      ?>
 362  
 363          <li><?php echo $item; ?></li>
 364  
 365      <?php endforeach; ?>
 366      </ul>
 367  
 368  <?php
 369  }


Generated: Fri Jul 10 01:01:36 2020 Cross-referenced by PHPXref 0.7.1