[ 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      bp_activity_set_action(
  25          buddypress()->profile->id,
  26          'updated_profile',
  27          __( 'Updated Profile', 'buddypress' ),
  28          'bp_xprofile_format_activity_action_updated_profile',
  29          __( 'Profile Updates', 'buddypress' ),
  30          array( 'activity' )
  31      );
  32  
  33      /**
  34       * Fires after the registration of default activity actions for Extended Profile component.
  35       *
  36       * @since 1.1.0
  37       */
  38      do_action( 'xprofile_register_activity_actions' );
  39  }
  40  add_action( 'bp_register_activity_actions', 'xprofile_register_activity_actions' );
  41  
  42  /**
  43   * Format 'updated_profile' activity actions.
  44   *
  45   * @since 2.0.0
  46   *
  47   * @param string $action   Static activity action.
  48   * @param object $activity Activity object.
  49   * @return string
  50   */
  51  function bp_xprofile_format_activity_action_updated_profile( $action, $activity ) {
  52  
  53      // Note for translators: The natural phrasing in English, "Joe updated
  54      // his profile", requires that we know Joe's gender, which we don't. If
  55      // your language doesn't have this restriction, feel free to use a more
  56      // natural translation.
  57      $profile_link = trailingslashit( bp_core_get_user_domain( $activity->user_id ) . bp_get_profile_slug() );
  58  
  59      /* translators: %s: user profile link */
  60      $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>' );
  61  
  62      /**
  63       * Filters the formatted 'updated_profile' activity stream action.
  64       *
  65       * @since 2.0.0
  66       *
  67       * @param string $action   Formatted action for activity stream.
  68       * @param object $activity Activity object.
  69       */
  70      return apply_filters( 'bp_xprofile_format_activity_action_updated_profile', $action, $activity );
  71  }
  72  
  73  /**
  74   * Records activity for the logged in user within the profile component so that
  75   * it will show in the users activity stream (if installed).
  76   *
  77   * @since 1.0.0
  78   *
  79   * @param array|string $args String containing all variables used after bp_parse_args() call.
  80   * @return WP_Error|bool|int
  81   */
  82  function xprofile_record_activity( $args = '' ) {
  83  
  84      // Bail if activity component is not active.
  85      if ( ! bp_is_active( 'activity' ) ) {
  86          return false;
  87      }
  88  
  89      // Parse the arguments.
  90      $r = bp_parse_args( $args, array(
  91          'user_id'           => bp_loggedin_user_id(),
  92          'action'            => '',
  93          'content'           => '',
  94          'primary_link'      => '',
  95          'component'         => buddypress()->profile->id,
  96          'type'              => false,
  97          'item_id'           => false,
  98          'secondary_item_id' => false,
  99          'recorded_time'     => bp_core_current_time(),
 100          'hide_sitewide'     => false
 101      ) );
 102  
 103      return bp_activity_add( $r );
 104  }
 105  
 106  /**
 107   * Deletes activity for a user within the profile component so that it will be
 108   * removed from the users activity stream and sitewide stream (if installed).
 109   *
 110   * @since 1.0.0
 111   *
 112   *                            for a specific activity.
 113   *
 114   * @param array|string $args Containing all variables used after bp_parse_args() call.
 115   * @return bool
 116   */
 117  function xprofile_delete_activity( $args = '' ) {
 118  
 119      // Bail if activity component is not active.
 120      if ( ! bp_is_active( 'activity' ) ) {
 121          return false;
 122      }
 123  
 124      // Parse the arguments.
 125      $r = bp_parse_args( $args, array(
 126          'component' => buddypress()->profile->id
 127      ), 'xprofile_delete_activity' );
 128  
 129      // Delete the activity item.
 130      bp_activity_delete_by_item_id( $r );
 131  }
 132  
 133  /**
 134   * Register an activity action for the Extended Profiles component.
 135   *
 136   * @since 1.0.0
 137   *
 138   * @param string $key Key.
 139   * @param string $value Value.
 140   * @return bool True if success, false on failure.
 141   */
 142  function xprofile_register_activity_action( $key, $value ) {
 143  
 144      // Bail if activity component is not active.
 145      if ( ! bp_is_active( 'activity' ) ) {
 146          return false;
 147      }
 148  
 149      /**
 150       * Filters the return value of bp_activity_set_action.
 151       *
 152       * @since 1.1.0
 153       *
 154       * @param bool   $value Whether or not an action was successfully registered.
 155       * @param string $key   Key used for the registered action.
 156       * @param string $value Value used for the registered action.
 157       */
 158      return apply_filters( 'xprofile_register_activity_action', bp_activity_set_action( buddypress()->profile->id, $key, $value ), $key, $value );
 159  }
 160  
 161  /**
 162   * Add an activity item when a user has updated his profile.
 163   *
 164   * @since 2.0.0
 165   *
 166   * @param int   $user_id    ID of the user who has updated his profile.
 167   * @param array $field_ids  IDs of the fields submitted.
 168   * @param bool  $errors     True if validation or saving errors occurred, otherwise false.
 169   * @param array $old_values Pre-save xprofile field values and visibility levels.
 170   * @param array $new_values Post-save xprofile field values and visibility levels.
 171   * @return bool True on success, false on failure.
 172   */
 173  function bp_xprofile_updated_profile_activity( $user_id, $field_ids = array(), $errors = false, $old_values = array(), $new_values = array() ) {
 174  
 175      // If there were errors, don't post.
 176      if ( ! empty( $errors ) ) {
 177          return false;
 178      }
 179  
 180      // Bail if activity component is not active.
 181      if ( ! bp_is_active( 'activity' ) ) {
 182          return false;
 183      }
 184  
 185      // Don't post if there have been no changes, or if the changes are
 186      // related solely to non-public fields.
 187      $public_changes = false;
 188      foreach ( $new_values as $field_id => $new_value ) {
 189          $old_value = isset( $old_values[ $field_id ] ) ? $old_values[ $field_id ] : '';
 190  
 191          // Don't register changes to private fields.
 192          if ( empty( $new_value['visibility'] ) || ( 'public' !== $new_value['visibility'] ) ) {
 193              continue;
 194          }
 195  
 196          // Don't register if there have been no changes.
 197          if ( $new_value === $old_value ) {
 198              continue;
 199          }
 200  
 201          // Looks like we have public changes - no need to keep checking.
 202          $public_changes = true;
 203          break;
 204      }
 205  
 206      // Bail if no public changes.
 207      if ( empty( $public_changes ) ) {
 208          return false;
 209      }
 210  
 211      // Throttle to one activity of this type per 2 hours.
 212      $existing = bp_activity_get( array(
 213          'max'    => 1,
 214          'filter' => array(
 215              'user_id' => $user_id,
 216              'object'  => buddypress()->profile->id,
 217              'action'  => 'updated_profile',
 218          ),
 219      ) );
 220  
 221      // Default throttle time is 2 hours. Filter to change (in seconds).
 222      if ( ! empty( $existing['activities'] ) ) {
 223  
 224          /**
 225           * Filters the throttle time, in seconds, used to prevent excessive activity posting.
 226           *
 227           * @since 2.0.0
 228           *
 229           * @param int $value Throttle time, in seconds.
 230           */
 231          $throttle_period = apply_filters( 'bp_xprofile_updated_profile_activity_throttle_time', HOUR_IN_SECONDS * 2 );
 232          $then            = strtotime( $existing['activities'][0]->date_recorded );
 233          $now             = bp_core_current_time( true, 'timestamp' );
 234  
 235          // Bail if throttled.
 236          if ( ( $now - $then ) < $throttle_period ) {
 237              return false;
 238          }
 239      }
 240  
 241      // If we've reached this point, assemble and post the activity item.
 242      $profile_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_profile_slug() );
 243  
 244      return (bool) xprofile_record_activity( array(
 245          'user_id'      => $user_id,
 246          'primary_link' => $profile_link,
 247          'component'    => buddypress()->profile->id,
 248          'type'         => 'updated_profile',
 249      ) );
 250  }
 251  add_action( 'xprofile_updated_profile', 'bp_xprofile_updated_profile_activity', 10, 5 );
 252  
 253  /**
 254   * Add filters for xprofile activity types to Show dropdowns.
 255   *
 256   * @since 2.0.0
 257   * @todo Mark as deprecated
 258   */
 259  function xprofile_activity_filter_options() {
 260      ?>
 261  
 262      <option value="updated_profile"><?php _e( 'Profile Updates', 'buddypress' ) ?></option>
 263  
 264      <?php
 265  }


Generated: Wed Apr 21 01:01:43 2021 Cross-referenced by PHPXref 0.7.1