[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/extend/buddypress/ -> activity.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress BuddyPress Activity Class
   5   *
   6   * @package bbPress
   7   * @subpackage BuddyPress
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  if ( ! class_exists( 'BBP_BuddyPress_Activity' ) ) :
  14  /**
  15   * Loads BuddyPress Activity extension
  16   *
  17   * @since 2.0.0 bbPress (r3395)
  18   *
  19   * @package bbPress
  20   * @subpackage BuddyPress
  21   */
  22  class BBP_BuddyPress_Activity {
  23  
  24      /** Variables *************************************************************/
  25  
  26      /**
  27       * The name of the BuddyPress component, used in activity streams
  28       *
  29       * @var string
  30       */
  31      private $component = '';
  32  
  33      /**
  34       * Forum Create Activity Action
  35       *
  36       * @var string
  37       */
  38      private $forum_create = '';
  39  
  40      /**
  41       * Topic Create Activity Action
  42       *
  43       * @var string
  44       */
  45      private $topic_create = '';
  46  
  47      /**
  48       * Topic Close Activity Action
  49       *
  50       * @var string
  51       */
  52      private $topic_close = '';
  53  
  54      /**
  55       * Topic Edit Activity Action
  56       *
  57       * @var string
  58       */
  59      private $topic_edit = '';
  60  
  61      /**
  62       * Topic Open Activity Action
  63       *
  64       * @var string
  65       */
  66      private $topic_open = '';
  67  
  68      /**
  69       * Reply Create Activity Action
  70       *
  71       * @var string
  72       */
  73      private $reply_create = '';
  74  
  75      /**
  76       * Reply Edit Activity Action
  77       *
  78       * @var string
  79       */
  80      private $reply_edit = '';
  81  
  82      /** Setup Methods *********************************************************/
  83  
  84      /**
  85       * The bbPress BuddyPress Activity loader
  86       *
  87       * @since 2.0.0 bbPress (r3395)
  88       */
  89  	public function __construct() {
  90          $this->setup_globals();
  91          $this->setup_actions();
  92          $this->setup_filters();
  93          $this->fully_loaded();
  94      }
  95  
  96      /**
  97       * Extension variables
  98       *
  99       * @since 2.0.0 bbPress (r3395)
 100       *
 101       * @access private
 102       */
 103  	private function setup_globals() {
 104  
 105          // The name of the BuddyPress component, used in activity streams
 106          $this->component    = 'bbpress';
 107  
 108          // Forums
 109          $this->forum_create = 'bbp_forum_create';
 110  
 111          // Topics
 112          $this->topic_create = 'bbp_topic_create';
 113          $this->topic_edit   = 'bbp_topic_edit';
 114          $this->topic_close  = 'bbp_topic_close';
 115          $this->topic_open   = 'bbp_topic_open';
 116  
 117          // Replies
 118          $this->reply_create = 'bbp_reply_create';
 119          $this->reply_edit   = 'bbp_reply_edit';
 120      }
 121  
 122      /**
 123       * Setup the actions
 124       *
 125       * @since 2.0.0 bbPress (r3395)
 126       *
 127       * @access private
 128       */
 129  	private function setup_actions() {
 130  
 131          // Register the activity stream actions
 132          add_action( 'bp_register_activity_actions',      array( $this, 'register_activity_actions' )        );
 133  
 134          // Hook into topic and reply creation
 135          add_action( 'bbp_new_topic',                     array( $this, 'topic_create'              ), 10, 4 );
 136          add_action( 'bbp_new_reply',                     array( $this, 'reply_create'              ), 10, 5 );
 137  
 138          // Hook into topic and reply status changes
 139          add_action( 'edit_post',                         array( $this, 'topic_update'              ), 10, 2 );
 140          add_action( 'edit_post',                         array( $this, 'reply_update'              ), 10, 2 );
 141  
 142          // Hook into topic and reply deletion
 143          add_action( 'bbp_delete_topic',                  array( $this, 'topic_delete'              ), 10, 1 );
 144          add_action( 'bbp_delete_reply',                  array( $this, 'reply_delete'              ), 10, 1 );
 145      }
 146  
 147      /**
 148       * Setup the filters
 149       *
 150       * @since 2.0.0 bbPress (r3395)
 151       *
 152       * @access private
 153       */
 154  	private function setup_filters() {
 155  
 156          // Obey BuddyPress commenting rules
 157          add_filter( 'bp_activity_can_comment',   array( $this, 'activity_can_comment'   )        );
 158  
 159          // Link directly to the topic or reply
 160          add_filter( 'bp_activity_get_permalink', array( $this, 'activity_get_permalink' ), 10, 2 );
 161      }
 162  
 163      /**
 164       * Allow the variables, actions, and filters to be modified by third party
 165       * plugins and themes.
 166       *
 167       * @since 2.1.0 bbPress (r3902)
 168       */
 169  	private function fully_loaded() {
 170          do_action_ref_array( 'bbp_buddypress_activity_loaded', array( $this ) );
 171      }
 172  
 173      /** Methods ***************************************************************/
 174  
 175      /**
 176       * Register our activity actions with BuddyPress
 177       *
 178       * @since 2.0.0 bbPress (r3395)
 179       */
 180  	public function register_activity_actions() {
 181  
 182          // Sitewide topic
 183          bp_activity_set_action(
 184              $this->component,
 185              $this->topic_create,
 186              esc_html__( 'New forum topic', 'bbpress' ),
 187              'bbp_format_activity_action_new_topic',
 188              esc_html__( 'Topics', 'bbpress' ),
 189              array( 'activity', 'member', 'member_groups', 'group' )
 190          );
 191  
 192          // Sitewide reply
 193          bp_activity_set_action(
 194              $this->component,
 195              $this->reply_create,
 196              esc_html__( 'New forum reply', 'bbpress' ),
 197              'bbp_format_activity_action_new_reply',
 198              esc_html__( 'Replies', 'bbpress' ),
 199              array( 'activity', 'member', 'member_groups', 'group' )
 200          );
 201      }
 202  
 203      /**
 204       * Wrapper for recoding bbPress actions to the BuddyPress activity stream
 205       *
 206       * @since 2.0.0 bbPress (r3395)
 207       *
 208       * @param  array $args Array of arguments for bp_activity_add()
 209       *
 210       * @return int   Activity ID if successful, false if not
 211       */
 212  	private function record_activity( $args = array() ) {
 213  
 214          // Default activity args
 215          $activity = bbp_parse_args( $args, array(
 216              'id'                => null,
 217              'user_id'           => bbp_get_current_user_id(),
 218              'type'              => '',
 219              'action'            => '',
 220              'item_id'           => '',
 221              'secondary_item_id' => '',
 222              'content'           => '',
 223              'primary_link'      => '',
 224              'component'         => $this->component,
 225              'recorded_time'     => bp_core_current_time(),
 226              'hide_sitewide'     => false
 227          ), 'record_activity' );
 228  
 229          // Add the activity
 230          return bp_activity_add( $activity );
 231      }
 232  
 233      /**
 234       * Wrapper for deleting bbPress actions from BuddyPress activity stream
 235       *
 236       * @since 2.0.0 bbPress (r3395)
 237       *
 238       * @param  array $args Array of arguments for bp_activity_add()
 239       *
 240       * @return int   Activity ID if successful, false if not
 241       */
 242  	public function delete_activity( $args = array() ) {
 243  
 244          // Default activity args
 245          $activity = bbp_parse_args( $args, array(
 246              'item_id'           => false,
 247              'component'         => $this->component,
 248              'type'              => false,
 249              'user_id'           => false,
 250              'secondary_item_id' => false
 251          ), 'delete_activity' );
 252  
 253          // Delete the activity
 254          bp_activity_delete_by_item_id( $activity );
 255      }
 256  
 257      /**
 258       * Check for an existing activity stream entry for a given post_id
 259       *
 260       * @param int $post_id ID of the topic or reply
 261       * @return int if an activity id is verified, false if not
 262       */
 263  	private static function get_activity_id( $post_id = 0 ) {
 264  
 265          // Try to get the activity ID of the post
 266          $activity_id = (int) get_post_meta( $post_id, '_bbp_activity_id', true );
 267  
 268          // Bail if no activity ID is in post meta
 269          if ( empty( $activity_id ) ) {
 270              return null;
 271          }
 272  
 273          // Get the activity stream item, bail if it doesn't exist
 274          $existing = new BP_Activity_Activity( $activity_id );
 275          if ( empty( $existing->component ) ) {
 276              return null;
 277          }
 278  
 279          // Return the activity ID since we've verified the connection
 280          return $activity_id;
 281      }
 282  
 283      /**
 284       * Maybe disable activity stream comments on select actions
 285       *
 286       * @since 2.0.0 bbPress (r3399)
 287       *
 288       * @global BP_Activity_Template $activities_template
 289       * @param boolean $can_comment
 290       * @return boolean
 291       */
 292  	public function activity_can_comment( $can_comment = true ) {
 293          global $activities_template;
 294  
 295          // Already forced off, so comply
 296          if ( false === $can_comment ) {
 297              return $can_comment;
 298          }
 299  
 300          // Check if blog & forum activity stream commenting is off
 301          if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
 302  
 303              // Get the current action name
 304              $action_name = bp_get_activity_action_name();
 305  
 306              // Setup the array of possibly disabled actions
 307              $disabled_actions = array(
 308                  $this->topic_create,
 309                  $this->reply_create
 310              );
 311  
 312              // Check if this activity stream action is disabled
 313              if ( in_array( $action_name, $disabled_actions, true ) ) {
 314                  $can_comment = false;
 315              }
 316          }
 317  
 318          return $can_comment;
 319      }
 320  
 321      /**
 322       * Maybe link directly to topics and replies in activity stream entries
 323       *
 324       * @since 2.0.0 bbPress (r3399)
 325       *
 326       * @param string $link
 327       * @param mixed $activity_object
 328       * @return string The link to the activity stream item
 329       */
 330  	public function activity_get_permalink( $link = '', $activity_object = false ) {
 331  
 332          // Setup the array of actions to link directly to
 333          $disabled_actions = array(
 334              $this->topic_create,
 335              $this->reply_create
 336          );
 337  
 338          // Check if this activity stream action is directly linked
 339          if ( in_array( $activity_object->type, $disabled_actions, true ) ) {
 340              $link = $activity_object->primary_link;
 341          }
 342  
 343          return $link;
 344      }
 345  
 346      /** Topics ****************************************************************/
 347  
 348      /**
 349       * Record an activity stream entry when a topic is created or updated
 350       *
 351       * @since 2.0.0 bbPress (r3395)
 352       *
 353       * @param int $topic_id
 354       * @param int $forum_id
 355       * @param array $anonymous_data
 356       * @param int $topic_author_id
 357       * @return Bail early if topic is by anonymous user
 358       */
 359  	public function topic_create( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $topic_author_id = 0 ) {
 360  
 361          // Bail early if topic is by anonymous user
 362          if ( ! empty( $anonymous_data ) ) {
 363              return;
 364          }
 365  
 366          // Bail if site is private
 367          if ( ! bbp_is_site_public() ) {
 368              return;
 369          }
 370  
 371          // Validate activity data
 372          $user_id  = bbp_get_user_id( $topic_author_id );
 373          $topic_id = bbp_get_topic_id( $topic_id );
 374          $forum_id = bbp_get_forum_id( $forum_id );
 375  
 376          // Bail if user is not active
 377          if ( bbp_is_user_inactive( $user_id ) ) {
 378              return;
 379          }
 380  
 381          // Bail if topic is not published
 382          if ( ! bbp_is_topic_published( $topic_id ) ) {
 383              return;
 384          }
 385  
 386          // User link for topic author
 387          $user_link = bbp_get_user_profile_link( $user_id );
 388  
 389          // Topic
 390          $topic_permalink = bbp_get_topic_permalink( $topic_id );
 391          $topic_title     = get_post_field( 'post_title',   $topic_id, 'raw' );
 392          $topic_content   = get_post_field( 'post_content', $topic_id, 'raw' );
 393          $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
 394  
 395          // Forum
 396          $forum_permalink = bbp_get_forum_permalink( $forum_id );
 397          $forum_title     = get_post_field( 'post_title', $forum_id, 'raw' );
 398          $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
 399  
 400          // Activity action & text
 401          $activity_text    = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
 402          $activity_action  = apply_filters( 'bbp_activity_topic_create',         $activity_text, $user_id,   $topic_id,   $forum_id );
 403          $activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', $topic_content                                     );
 404  
 405          // Compile and record the activity stream results
 406          $activity_id = $this->record_activity( array(
 407              'id'                => $this->get_activity_id( $topic_id ),
 408              'user_id'           => $user_id,
 409              'action'            => $activity_action,
 410              'content'           => $activity_content,
 411              'primary_link'      => $topic_permalink,
 412              'type'              => $this->topic_create,
 413              'item_id'           => $topic_id,
 414              'secondary_item_id' => $forum_id,
 415              'recorded_time'     => get_post_time( 'Y-m-d H:i:s', true, $topic_id ),
 416              'hide_sitewide'     => ! bbp_is_forum_public( $forum_id, false )
 417          ) );
 418  
 419          // Add the activity entry ID as a meta value to the topic
 420          if ( ! empty( $activity_id ) ) {
 421              update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
 422          }
 423      }
 424  
 425      /**
 426       * Delete the activity stream entry when a topic is spammed, trashed, or deleted
 427       *
 428       * @param int $topic_id
 429       */
 430  	public function topic_delete( $topic_id = 0 ) {
 431  
 432          // Get activity ID, bail if it doesn't exist
 433          $activity_id = $this->get_activity_id( $topic_id );
 434          if ( ! empty( $activity_id ) ) {
 435              return bp_activity_delete( array( 'id' => $activity_id ) );
 436          }
 437  
 438          return false;
 439      }
 440  
 441      /**
 442       * Update the activity stream entry when a topic status changes
 443       *
 444       * @param int $topic_id
 445       * @param obj $post
 446       * @return Bail early if not a topic, or topic is by anonymous user
 447       */
 448  	public function topic_update( $topic_id = 0, $post = null ) {
 449  
 450          // Bail early if not a topic
 451          if ( get_post_type( $post ) !== bbp_get_topic_post_type() ) {
 452              return;
 453          }
 454  
 455          // Bail early if revisions are off
 456          if ( ! bbp_allow_revisions() || ! post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
 457              return;
 458          }
 459  
 460          $topic_id = bbp_get_topic_id( $topic_id );
 461  
 462          // Bail early if topic is by anonymous user
 463          if ( bbp_is_topic_anonymous( $topic_id ) ) {
 464              return;
 465          }
 466  
 467          // Action based on new status
 468          if ( bbp_is_topic_public( $post->ID ) ) {
 469  
 470              // Validate topic data
 471              $forum_id        = bbp_get_topic_forum_id( $topic_id );
 472              $topic_author_id = bbp_get_topic_author_id( $topic_id );
 473  
 474              $this->topic_create( $topic_id, $forum_id, array(), $topic_author_id );
 475          } else {
 476              $this->topic_delete( $topic_id );
 477          }
 478      }
 479  
 480      /** Replies ***************************************************************/
 481  
 482      /**
 483       * Record an activity stream entry when a reply is created
 484       *
 485       * @since 2.0.0 bbPress (r3395)
 486       *
 487       * @param int $topic_id
 488       * @param int $forum_id
 489       * @param array $anonymous_data
 490       * @param int $topic_author_id
 491       * @return Bail early if topic is by anonymous user
 492       */
 493  	public function reply_create( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $reply_author_id = 0 ) {
 494  
 495          // Do not log activity of anonymous users
 496          if ( ! empty( $anonymous_data ) ) {
 497              return;
 498          }
 499  
 500          // Bail if site is private
 501          if ( ! bbp_is_site_public() ) {
 502              return;
 503          }
 504  
 505          // Validate activity data
 506          $user_id  = bbp_get_user_id( $reply_author_id );
 507          $reply_id = bbp_get_reply_id( $reply_id );
 508          $topic_id = bbp_get_topic_id( $topic_id );
 509          $forum_id = bbp_get_forum_id( $forum_id );
 510  
 511          // Bail if user is not active
 512          if ( bbp_is_user_inactive( $user_id ) ) {
 513              return;
 514          }
 515  
 516          // Bail if reply is not published
 517          if ( ! bbp_is_reply_published( $reply_id ) ) {
 518              return;
 519          }
 520  
 521          // Setup links for activity stream
 522          $user_link = bbp_get_user_profile_link( $user_id );
 523  
 524          // Reply
 525          $reply_url     = bbp_get_reply_url( $reply_id );
 526          $reply_content = get_post_field( 'post_content', $reply_id, 'raw' );
 527  
 528          // Topic
 529          $topic_permalink = bbp_get_topic_permalink( $topic_id );
 530          $topic_title     = get_post_field( 'post_title', $topic_id, 'raw' );
 531          $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
 532  
 533          // Forum
 534          $forum_permalink = bbp_get_forum_permalink( $forum_id );
 535          $forum_title     = get_post_field( 'post_title', $forum_id, 'raw' );
 536          $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
 537  
 538          // Activity action & text
 539          $activity_text    = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
 540          $activity_action  = apply_filters( 'bbp_activity_reply_create',         $activity_text, $user_id, $reply_id,  $topic_id );
 541          $activity_content = apply_filters( 'bbp_activity_reply_create_excerpt', $reply_content                                  );
 542  
 543          // Compile and record the activity stream results
 544          $activity_id = $this->record_activity( array(
 545              'id'                => $this->get_activity_id( $reply_id ),
 546              'user_id'           => $user_id,
 547              'action'            => $activity_action,
 548              'content'           => $activity_content,
 549              'primary_link'      => $reply_url,
 550              'type'              => $this->reply_create,
 551              'item_id'           => $reply_id,
 552              'secondary_item_id' => $topic_id,
 553              'recorded_time'     => get_post_time( 'Y-m-d H:i:s', true, $reply_id ),
 554              'hide_sitewide'     => ! bbp_is_forum_public( $forum_id, false )
 555          ) );
 556  
 557          // Add the activity entry ID as a meta value to the reply
 558          if ( ! empty( $activity_id ) ) {
 559              update_post_meta( $reply_id, '_bbp_activity_id', $activity_id );
 560          }
 561      }
 562  
 563      /**
 564       * Delete the activity stream entry when a reply is spammed, trashed, or deleted
 565       *
 566       * @param int $reply_id
 567       */
 568  	public function reply_delete( $reply_id ) {
 569  
 570          // Get activity ID, bail if it doesn't exist
 571          $activity_id = $this->get_activity_id( $reply_id );
 572          if ( ! empty( $activity_id ) ) {
 573              return bp_activity_delete( array( 'id' => $activity_id ) );
 574          }
 575  
 576          return false;
 577      }
 578  
 579      /**
 580       * Update the activity stream entry when a reply status changes
 581       *
 582       * @param int $reply_id
 583       * @param obj $post
 584       * @return Bail early if not a reply, or reply is by anonymous user
 585       */
 586  	public function reply_update( $reply_id, $post ) {
 587  
 588          // Bail early if not a reply
 589          if ( get_post_type( $post ) !== bbp_get_reply_post_type() ) {
 590              return;
 591          }
 592  
 593          // Bail early if revisions are off
 594          if ( ! bbp_allow_revisions() || ! post_type_supports( bbp_get_reply_post_type(), 'revisions' ) ) {
 595              return;
 596          }
 597  
 598          $reply_id = bbp_get_reply_id( $reply_id );
 599  
 600          // Bail early if reply is by anonymous user
 601          if ( bbp_is_reply_anonymous( $reply_id ) ) {
 602              return;
 603          }
 604  
 605          // Action based on new status
 606          if ( bbp_get_public_status_id() === $post->post_status ) {
 607  
 608              // Validate reply data
 609              $topic_id        = bbp_get_reply_topic_id( $reply_id );
 610              $forum_id        = bbp_get_reply_forum_id( $reply_id );
 611              $reply_author_id = bbp_get_reply_author_id( $reply_id );
 612  
 613              $this->reply_create( $reply_id, $topic_id, $forum_id, array(), $reply_author_id );
 614          } else {
 615              $this->reply_delete( $reply_id );
 616          }
 617      }
 618  }
 619  endif;


Generated: Tue Apr 16 01:01:01 2024 Cross-referenced by PHPXref 0.7.1