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


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1