[ 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(
  91          $args,
  92          array(
  93              'user_id'           => bp_loggedin_user_id(),
  94              'action'            => '',
  95              'content'           => '',
  96              'primary_link'      => '',
  97              'component'         => buddypress()->profile->id,
  98              'type'              => false,
  99              'item_id'           => false,
 100              'secondary_item_id' => false,
 101              'recorded_time'     => bp_core_current_time(),
 102              'hide_sitewide'     => false,
 103          )
 104      );
 105  
 106      return bp_activity_add( $r );
 107  }
 108  
 109  /**
 110   * Deletes activity for a user within the profile component so that it will be
 111   * removed from the users activity stream and sitewide stream (if installed).
 112   *
 113   * @since 1.0.0
 114   *
 115   *                            for a specific activity.
 116   *
 117   * @param array|string $args Containing all variables used after bp_parse_args() call.
 118   * @return bool
 119   */
 120  function xprofile_delete_activity( $args = '' ) {
 121  
 122      // Bail if activity component is not active.
 123      if ( ! bp_is_active( 'activity' ) ) {
 124          return false;
 125      }
 126  
 127      // Parse the arguments.
 128      $r = bp_parse_args(
 129          $args,
 130          array(
 131              'component' => buddypress()->profile->id,
 132          ),
 133          'xprofile_delete_activity'
 134      );
 135  
 136      // Delete the activity item.
 137      bp_activity_delete_by_item_id( $r );
 138  }
 139  
 140  /**
 141   * Register an activity action for the Extended Profiles component.
 142   *
 143   * @since 1.0.0
 144   *
 145   * @param string $key Key.
 146   * @param string $value Value.
 147   * @return bool True if success, false on failure.
 148   */
 149  function xprofile_register_activity_action( $key, $value ) {
 150  
 151      // Bail if activity component is not active.
 152      if ( ! bp_is_active( 'activity' ) ) {
 153          return false;
 154      }
 155  
 156      /**
 157       * Filters the return value of bp_activity_set_action.
 158       *
 159       * @since 1.1.0
 160       *
 161       * @param bool   $value Whether or not an action was successfully registered.
 162       * @param string $key   Key used for the registered action.
 163       * @param string $value Value used for the registered action.
 164       */
 165      return apply_filters( 'xprofile_register_activity_action', bp_activity_set_action( buddypress()->profile->id, $key, $value ), $key, $value );
 166  }
 167  
 168  /**
 169   * Add an activity item when a user has updated his profile.
 170   *
 171   * @since 2.0.0
 172   *
 173   * @param int   $user_id    ID of the user who has updated his profile.
 174   * @param array $field_ids  IDs of the fields submitted.
 175   * @param bool  $errors     True if validation or saving errors occurred, otherwise false.
 176   * @param array $old_values Pre-save xprofile field values and visibility levels.
 177   * @param array $new_values Post-save xprofile field values and visibility levels.
 178   * @return bool True on success, false on failure.
 179   */
 180  function bp_xprofile_updated_profile_activity( $user_id, $field_ids = array(), $errors = false, $old_values = array(), $new_values = array() ) {
 181  
 182      // If there were errors, don't post.
 183      if ( ! empty( $errors ) ) {
 184          return false;
 185      }
 186  
 187      // Bail if activity component is not active.
 188      if ( ! bp_is_active( 'activity' ) ) {
 189          return false;
 190      }
 191  
 192      // Don't post if there have been no changes, or if the changes are
 193      // related solely to non-public fields.
 194      $public_changes = false;
 195      foreach ( $new_values as $field_id => $new_value ) {
 196          $old_value = isset( $old_values[ $field_id ] ) ? $old_values[ $field_id ] : '';
 197  
 198          // Don't register changes to private fields.
 199          if ( empty( $new_value['visibility'] ) || ( 'public' !== $new_value['visibility'] ) ) {
 200              continue;
 201          }
 202  
 203          // Don't register if there have been no changes.
 204          if ( $new_value === $old_value ) {
 205              continue;
 206          }
 207  
 208          // Looks like we have public changes - no need to keep checking.
 209          $public_changes = true;
 210          break;
 211      }
 212  
 213      // Bail if no public changes.
 214      if ( empty( $public_changes ) ) {
 215          return false;
 216      }
 217  
 218      // Throttle to one activity of this type per 2 hours.
 219      $existing = bp_activity_get( array(
 220          'max'    => 1,
 221          'filter' => array(
 222              'user_id' => $user_id,
 223              'object'  => buddypress()->profile->id,
 224              'action'  => 'updated_profile',
 225          ),
 226      ) );
 227  
 228      // Default throttle time is 2 hours. Filter to change (in seconds).
 229      if ( ! empty( $existing['activities'] ) ) {
 230  
 231          /**
 232           * Filters the throttle time, in seconds, used to prevent excessive activity posting.
 233           *
 234           * @since 2.0.0
 235           *
 236           * @param int $value Throttle time, in seconds.
 237           */
 238          $throttle_period = apply_filters( 'bp_xprofile_updated_profile_activity_throttle_time', HOUR_IN_SECONDS * 2 );
 239          $then            = strtotime( $existing['activities'][0]->date_recorded );
 240          $now             = bp_core_current_time( true, 'timestamp' );
 241  
 242          // Bail if throttled.
 243          if ( ( $now - $then ) < $throttle_period ) {
 244              return false;
 245          }
 246      }
 247  
 248      // If we've reached this point, assemble and post the activity item.
 249      $profile_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_profile_slug() );
 250  
 251      return (bool) xprofile_record_activity( array(
 252          'user_id'      => $user_id,
 253          'primary_link' => $profile_link,
 254          'component'    => buddypress()->profile->id,
 255          'type'         => 'updated_profile',
 256      ) );
 257  }
 258  add_action( 'xprofile_updated_profile', 'bp_xprofile_updated_profile_activity', 10, 5 );
 259  
 260  /**
 261   * Add filters for xprofile activity types to Show dropdowns.
 262   *
 263   * @since 2.0.0
 264   * @todo Mark as deprecated
 265   */
 266  function xprofile_activity_filter_options() {
 267      ?>
 268  
 269      <option value="updated_profile"><?php _e( 'Profile Updates', 'buddypress' ) ?></option>
 270  
 271      <?php
 272  }


Generated: Mon Oct 25 01:00:54 2021 Cross-referenced by PHPXref 0.7.1