[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/ -> bp-xprofile-activity.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Activity & Notification Functions.
   4   *
   5   * These functions handle the recording, deleting and formatting of activity
   6   * items and notifications for the user and for this specific component.
   7   *
   8   * @package BuddyPress
   9   * @subpackage XProfileActivity
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Register the activity actions for the Extended Profile component.
  18   *
  19   * @since 1.0.0
  20   *
  21   */
  22  function xprofile_register_activity_actions() {
  23  
  24      // Register the activity stream actions for this component.
  25      bp_activity_set_action(
  26          // Older avatar activity items use 'profile' for component. See r4273.
  27          'profile',
  28          'new_avatar',
  29          __( 'Member changed profile picture', 'buddypress' ),
  30          'bp_xprofile_format_activity_action_new_avatar',
  31          __( 'Updated Profile Photos', 'buddypress' )
  32      );
  33  
  34      bp_activity_set_action(
  35          buddypress()->profile->id,
  36          'updated_profile',
  37          __( 'Updated Profile', 'buddypress' ),
  38          'bp_xprofile_format_activity_action_updated_profile',
  39          __( 'Profile Updates', 'buddypress' ),
  40          array( 'activity' )
  41      );
  42  
  43      /**
  44       * Fires after the registration of default activity actions for Extended Profile component.
  45       *
  46       * @since 1.1.0
  47       */
  48      do_action( 'xprofile_register_activity_actions' );
  49  }
  50  add_action( 'bp_register_activity_actions', 'xprofile_register_activity_actions' );
  51  
  52  /**
  53   * Format 'new_avatar' activity actions.
  54   *
  55   * @since 2.0.0
  56   *
  57   * @param string $action   Static activity action.
  58   * @param object $activity Activity object.
  59   * @return string
  60   */
  61  function bp_xprofile_format_activity_action_new_avatar( $action, $activity ) {
  62      $userlink = bp_core_get_userlink( $activity->user_id );
  63  
  64      /* translators: %s: user link */
  65      $action = sprintf( esc_html__( '%s changed their profile picture', 'buddypress' ), $userlink );
  66  
  67      // Legacy filter - pass $user_id instead of $activity.
  68      if ( has_filter( 'bp_xprofile_new_avatar_action' ) ) {
  69          $action = apply_filters( 'bp_xprofile_new_avatar_action', $action, $activity->user_id );
  70      }
  71  
  72      /**
  73       * Filters the formatted 'new_avatar' activity stream action.
  74       *
  75       * @since 2.0.0
  76       *
  77       * @param string $action   Formatted action for activity stream.
  78       * @param object $activity Activity object.
  79       */
  80      return apply_filters( 'bp_xprofile_format_activity_action_new_avatar', $action, $activity );
  81  }
  82  
  83  /**
  84   * Format 'updated_profile' activity actions.
  85   *
  86   * @since 2.0.0
  87   *
  88   * @param string $action   Static activity action.
  89   * @param object $activity Activity object.
  90   * @return string
  91   */
  92  function bp_xprofile_format_activity_action_updated_profile( $action, $activity ) {
  93  
  94      // Note for translators: The natural phrasing in English, "Joe updated
  95      // his profile", requires that we know Joe's gender, which we don't. If
  96      // your language doesn't have this restriction, feel free to use a more
  97      // natural translation.
  98      $profile_link = trailingslashit( bp_core_get_user_domain( $activity->user_id ) . bp_get_profile_slug() );
  99  
 100      /* translators: %s: user profile link */
 101      $action = sprintf( esc_html__( "%s's profile was updated", 'buddypress' ), '<a href="' . esc_url( $profile_link ) . '">' . bp_core_get_user_displayname( $activity->user_id ) . '</a>' );
 102  
 103      /**
 104       * Filters the formatted 'updated_profile' activity stream action.
 105       *
 106       * @since 2.0.0
 107       *
 108       * @param string $action   Formatted action for activity stream.
 109       * @param object $activity Activity object.
 110       */
 111      return apply_filters( 'bp_xprofile_format_activity_action_updated_profile', $action, $activity );
 112  }
 113  
 114  /**
 115   * Records activity for the logged in user within the profile component so that
 116   * it will show in the users activity stream (if installed).
 117   *
 118   * @since 1.0.0
 119   *
 120   * @param array|string $args String containing all variables used after bp_parse_args() call.
 121   * @return WP_Error|bool|int
 122   */
 123  function xprofile_record_activity( $args = '' ) {
 124  
 125      // Bail if activity component is not active.
 126      if ( ! bp_is_active( 'activity' ) ) {
 127          return false;
 128      }
 129  
 130      // Parse the arguments.
 131      $r = bp_parse_args( $args, array(
 132          'user_id'           => bp_loggedin_user_id(),
 133          'action'            => '',
 134          'content'           => '',
 135          'primary_link'      => '',
 136          'component'         => buddypress()->profile->id,
 137          'type'              => false,
 138          'item_id'           => false,
 139          'secondary_item_id' => false,
 140          'recorded_time'     => bp_core_current_time(),
 141          'hide_sitewide'     => false
 142      ) );
 143  
 144      return bp_activity_add( $r );
 145  }
 146  
 147  /**
 148   * Deletes activity for a user within the profile component so that it will be
 149   * removed from the users activity stream and sitewide stream (if installed).
 150   *
 151   * @since 1.0.0
 152   *
 153   *                            for a specific activity.
 154   *
 155   * @param array|string $args Containing all variables used after bp_parse_args() call.
 156   * @return bool
 157   */
 158  function xprofile_delete_activity( $args = '' ) {
 159  
 160      // Bail if activity component is not active.
 161      if ( ! bp_is_active( 'activity' ) ) {
 162          return false;
 163      }
 164  
 165      // Parse the arguments.
 166      $r = bp_parse_args( $args, array(
 167          'component' => buddypress()->profile->id
 168      ), 'xprofile_delete_activity' );
 169  
 170      // Delete the activity item.
 171      bp_activity_delete_by_item_id( $r );
 172  }
 173  
 174  /**
 175   * Register an activity action for the Extended Profiles component.
 176   *
 177   * @since 1.0.0
 178   *
 179   * @param string $key Key.
 180   * @param string $value Value.
 181   * @return bool True if success, false on failure.
 182   */
 183  function xprofile_register_activity_action( $key, $value ) {
 184  
 185      // Bail if activity component is not active.
 186      if ( ! bp_is_active( 'activity' ) ) {
 187          return false;
 188      }
 189  
 190      /**
 191       * Filters the return value of bp_activity_set_action.
 192       *
 193       * @since 1.1.0
 194       *
 195       * @param bool   $value Whether or not an action was successfully registered.
 196       * @param string $key   Key used for the registered action.
 197       * @param string $value Value used for the registered action.
 198       */
 199      return apply_filters( 'xprofile_register_activity_action', bp_activity_set_action( buddypress()->profile->id, $key, $value ), $key, $value );
 200  }
 201  
 202  /**
 203   * Adds an activity stream item when a user has uploaded a new avatar.
 204   *
 205   * @since 1.0.0
 206   * @since 2.3.4 Add new parameter to get the user id the avatar was set for.
 207   *
 208   *                         specific activity
 209   *
 210   * @param int $user_id The user id the avatar was set for.
 211   * @return bool
 212   */
 213  function bp_xprofile_new_avatar_activity( $user_id = 0 ) {
 214  
 215      // Bail if activity component is not active.
 216      if ( ! bp_is_active( 'activity' ) ) {
 217          return false;
 218      }
 219  
 220      if ( empty( $user_id ) ) {
 221          $user_id = bp_displayed_user_id();
 222      }
 223  
 224      /**
 225       * Filters the user ID when a user has uploaded a new avatar.
 226       *
 227       * @since 1.5.0
 228       *
 229       * @param int $user_id ID of the user the avatar was set for.
 230       */
 231      $user_id = apply_filters( 'bp_xprofile_new_avatar_user_id', $user_id );
 232  
 233      // Add the activity.
 234      bp_activity_add( array(
 235          'user_id'   => $user_id,
 236          'component' => 'profile',
 237          'type'      => 'new_avatar'
 238      ) );
 239  }
 240  add_action( 'xprofile_avatar_uploaded', 'bp_xprofile_new_avatar_activity' );
 241  
 242  /**
 243   * Add an activity item when a user has updated his profile.
 244   *
 245   * @since 2.0.0
 246   *
 247   * @param int   $user_id    ID of the user who has updated his profile.
 248   * @param array $field_ids  IDs of the fields submitted.
 249   * @param bool  $errors     True if validation or saving errors occurred, otherwise false.
 250   * @param array $old_values Pre-save xprofile field values and visibility levels.
 251   * @param array $new_values Post-save xprofile field values and visibility levels.
 252   * @return bool True on success, false on failure.
 253   */
 254  function bp_xprofile_updated_profile_activity( $user_id, $field_ids = array(), $errors = false, $old_values = array(), $new_values = array() ) {
 255  
 256      // If there were errors, don't post.
 257      if ( ! empty( $errors ) ) {
 258          return false;
 259      }
 260  
 261      // Bail if activity component is not active.
 262      if ( ! bp_is_active( 'activity' ) ) {
 263          return false;
 264      }
 265  
 266      // Don't post if there have been no changes, or if the changes are
 267      // related solely to non-public fields.
 268      $public_changes = false;
 269      foreach ( $new_values as $field_id => $new_value ) {
 270          $old_value = isset( $old_values[ $field_id ] ) ? $old_values[ $field_id ] : '';
 271  
 272          // Don't register changes to private fields.
 273          if ( empty( $new_value['visibility'] ) || ( 'public' !== $new_value['visibility'] ) ) {
 274              continue;
 275          }
 276  
 277          // Don't register if there have been no changes.
 278          if ( $new_value === $old_value ) {
 279              continue;
 280          }
 281  
 282          // Looks like we have public changes - no need to keep checking.
 283          $public_changes = true;
 284          break;
 285      }
 286  
 287      // Bail if no public changes.
 288      if ( empty( $public_changes ) ) {
 289          return false;
 290      }
 291  
 292      // Throttle to one activity of this type per 2 hours.
 293      $existing = bp_activity_get( array(
 294          'max'    => 1,
 295          'filter' => array(
 296              'user_id' => $user_id,
 297              'object'  => buddypress()->profile->id,
 298              'action'  => 'updated_profile',
 299          ),
 300      ) );
 301  
 302      // Default throttle time is 2 hours. Filter to change (in seconds).
 303      if ( ! empty( $existing['activities'] ) ) {
 304  
 305          /**
 306           * Filters the throttle time, in seconds, used to prevent excessive activity posting.
 307           *
 308           * @since 2.0.0
 309           *
 310           * @param int $value Throttle time, in seconds.
 311           */
 312          $throttle_period = apply_filters( 'bp_xprofile_updated_profile_activity_throttle_time', HOUR_IN_SECONDS * 2 );
 313          $then            = strtotime( $existing['activities'][0]->date_recorded );
 314          $now             = bp_core_current_time( true, 'timestamp' );
 315  
 316          // Bail if throttled.
 317          if ( ( $now - $then ) < $throttle_period ) {
 318              return false;
 319          }
 320      }
 321  
 322      // If we've reached this point, assemble and post the activity item.
 323      $profile_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_profile_slug() );
 324  
 325      return (bool) xprofile_record_activity( array(
 326          'user_id'      => $user_id,
 327          'primary_link' => $profile_link,
 328          'component'    => buddypress()->profile->id,
 329          'type'         => 'updated_profile',
 330      ) );
 331  }
 332  add_action( 'xprofile_updated_profile', 'bp_xprofile_updated_profile_activity', 10, 5 );
 333  
 334  /**
 335   * Add filters for xprofile activity types to Show dropdowns.
 336   *
 337   * @since 2.0.0
 338   * @todo Mark as deprecated
 339   */
 340  function xprofile_activity_filter_options() {
 341      ?>
 342  
 343      <option value="updated_profile"><?php _e( 'Profile Updates', 'buddypress' ) ?></option>
 344  
 345      <?php
 346  }


Generated: Fri Sep 25 01:01:33 2020 Cross-referenced by PHPXref 0.7.1