[ 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          // Append forum filters in site wide activity streams
 147          add_action( 'bp_activity_filter_options',        array( $this, 'activity_filter_options'   ), 10    );
 148  
 149          // Append forum filters in single member activity streams
 150          add_action( 'bp_member_activity_filter_options', array( $this, 'activity_filter_options'   ), 10    );
 151  
 152          // Append forum filters in single group activity streams
 153          add_action( 'bp_group_activity_filter_options',  array( $this, 'activity_filter_options'   ), 10    );
 154      }
 155  
 156      /**
 157       * Setup the filters
 158       *
 159       * @since 2.0.0 bbPress (r3395)
 160       *
 161       * @access private
 162       */
 163  	private function setup_filters() {
 164  
 165          // Obey BuddyPress commenting rules
 166          add_filter( 'bp_activity_can_comment',   array( $this, 'activity_can_comment'   )        );
 167  
 168          // Link directly to the topic or reply
 169          add_filter( 'bp_activity_get_permalink', array( $this, 'activity_get_permalink' ), 10, 2 );
 170      }
 171  
 172      /**
 173       * Allow the variables, actions, and filters to be modified by third party
 174       * plugins and themes.
 175       *
 176       * @since 2.1.0 bbPress (r3902)
 177       */
 178  	private function fully_loaded() {
 179          do_action_ref_array( 'bbp_buddypress_activity_loaded', array( $this ) );
 180      }
 181  
 182      /** Methods ***************************************************************/
 183  
 184      /**
 185       * Register our activity actions with BuddyPress
 186       *
 187       * @since 2.0.0 bbPress (r3395)
 188       */
 189  	public function register_activity_actions() {
 190  
 191          // Sitewide topic
 192          bp_activity_set_action(
 193              $this->component,
 194              $this->topic_create,
 195              esc_html__( 'New forum topic', 'bbpress' ),
 196              'bbp_format_activity_action_new_topic',
 197              esc_html__( 'Topics', 'bbpress' ),
 198              array( 'activity', 'member', 'member_groups', 'group' )
 199          );
 200  
 201          // Sitewide reply
 202          bp_activity_set_action(
 203              $this->component,
 204              $this->reply_create,
 205              esc_html__( 'New forum reply', 'bbpress' ),
 206              'bbp_format_activity_action_new_reply',
 207              esc_html__( 'Replies', 'bbpress' ),
 208              array( 'activity', 'member', 'member_groups', 'group' )
 209          );
 210  
 211          // Group forums
 212          /**
 213           * An upstream improvement to BuddyPress is required to make these work,
 214           * but they are included here to make our lives easier later.
 215          if ( bp_is_active( 'groups' ) ) {
 216  
 217              // Topic
 218              bp_activity_set_action(
 219                  buddypress()->groups->id,
 220                  $this->topic_create,
 221                  esc_html__( 'New forum topic', 'bbpress' ),
 222                  'bbp_format_activity_action_new_topic',
 223                  esc_html__( 'Topics', 'bbpress' ),
 224                  array( 'activity', 'member', 'member_groups', 'group' )
 225              );
 226  
 227              // Reply
 228              bp_activity_set_action(
 229                  buddypress()->groups->id,
 230                  $this->reply_create,
 231                  esc_html__( 'New forum reply', 'bbpress' ),
 232                  'bbp_format_activity_action_new_reply',
 233                  esc_html__( 'Replies', 'bbpress' ),
 234                  array( 'activity', 'member', 'member_groups', 'group' )
 235              );
 236          }
 237          */
 238      }
 239  
 240      /**
 241       * Wrapper for recoding bbPress actions to the BuddyPress activity stream
 242       *
 243       * @since 2.0.0 bbPress (r3395)
 244       *
 245       * @param  array $args Array of arguments for bp_activity_add()
 246       *
 247       * @return int   Activity ID if successful, false if not
 248       */
 249  	private function record_activity( $args = array() ) {
 250  
 251          // Default activity args
 252          $activity = bbp_parse_args( $args, array(
 253              'id'                => null,
 254              'user_id'           => bbp_get_current_user_id(),
 255              'type'              => '',
 256              'action'            => '',
 257              'item_id'           => '',
 258              'secondary_item_id' => '',
 259              'content'           => '',
 260              'primary_link'      => '',
 261              'component'         => $this->component,
 262              'recorded_time'     => bp_core_current_time(),
 263              'hide_sitewide'     => false
 264          ), 'record_activity' );
 265  
 266          // Add the activity
 267          return bp_activity_add( $activity );
 268      }
 269  
 270      /**
 271       * Wrapper for deleting bbPress actions from BuddyPress activity stream
 272       *
 273       * @since 2.0.0 bbPress (r3395)
 274       *
 275       * @param  array $args Array of arguments for bp_activity_add()
 276       *
 277       * @return int   Activity ID if successful, false if not
 278       */
 279  	public function delete_activity( $args = array() ) {
 280  
 281          // Default activity args
 282          $activity = bbp_parse_args( $args, array(
 283              'item_id'           => false,
 284              'component'         => $this->component,
 285              'type'              => false,
 286              'user_id'           => false,
 287              'secondary_item_id' => false
 288          ), 'delete_activity' );
 289  
 290          // Delete the activity
 291          bp_activity_delete_by_item_id( $activity );
 292      }
 293  
 294      /**
 295       * Check for an existing activity stream entry for a given post_id
 296       *
 297       * @param int $post_id ID of the topic or reply
 298       * @return int if an activity id is verified, false if not
 299       */
 300  	private static function get_activity_id( $post_id = 0 ) {
 301  
 302          // Try to get the activity ID of the post
 303          $activity_id = (int) get_post_meta( $post_id, '_bbp_activity_id', true );
 304  
 305          // Bail if no activity ID is in post meta
 306          if ( empty( $activity_id ) ) {
 307              return null;
 308          }
 309  
 310          // Get the activity stream item, bail if it doesn't exist
 311          $existing = new BP_Activity_Activity( $activity_id );
 312          if ( empty( $existing->component ) ) {
 313              return null;
 314          }
 315  
 316          // Return the activity ID since we've verified the connection
 317          return $activity_id;
 318      }
 319  
 320      /**
 321       * Maybe disable activity stream comments on select actions
 322       *
 323       * @since 2.0.0 bbPress (r3399)
 324       *
 325       * @global BP_Activity_Template $activities_template
 326       * @param boolean $can_comment
 327       * @return boolean
 328       */
 329  	public function activity_can_comment( $can_comment = true ) {
 330          global $activities_template;
 331  
 332          // Already forced off, so comply
 333          if ( false === $can_comment ) {
 334              return $can_comment;
 335          }
 336  
 337          // Check if blog & forum activity stream commenting is off
 338          if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
 339  
 340              // Get the current action name
 341              $action_name = bp_get_activity_action_name();
 342  
 343              // Setup the array of possibly disabled actions
 344              $disabled_actions = array(
 345                  $this->topic_create,
 346                  $this->reply_create
 347              );
 348  
 349              // Check if this activity stream action is disabled
 350              if ( in_array( $action_name, $disabled_actions, true ) ) {
 351                  $can_comment = false;
 352              }
 353          }
 354  
 355          return $can_comment;
 356      }
 357  
 358      /**
 359       * Maybe link directly to topics and replies in activity stream entries
 360       *
 361       * @since 2.0.0 bbPress (r3399)
 362       *
 363       * @param string $link
 364       * @param mixed $activity_object
 365       * @return string The link to the activity stream item
 366       */
 367  	public function activity_get_permalink( $link = '', $activity_object = false ) {
 368  
 369          // Setup the array of actions to link directly to
 370          $disabled_actions = array(
 371              $this->topic_create,
 372              $this->reply_create
 373          );
 374  
 375          // Check if this activity stream action is directly linked
 376          if ( in_array( $activity_object->type, $disabled_actions, true ) ) {
 377              $link = $activity_object->primary_link;
 378          }
 379  
 380          return $link;
 381      }
 382  
 383      /**
 384       * Append forum options to activity filter select box
 385       *
 386       * @since 2.1.0 bbPress (r3653)
 387       */
 388  	function activity_filter_options() {
 389      ?>
 390  
 391          <option value="<?php echo $this->topic_create; ?>"><?php esc_html_e( 'Topics',  'bbpress' ); ?></option>
 392          <option value="<?php echo $this->reply_create; ?>"><?php esc_html_e( 'Replies', 'bbpress' ); ?></option>
 393  
 394      <?php
 395      }
 396  
 397      /** Topics ****************************************************************/
 398  
 399      /**
 400       * Record an activity stream entry when a topic is created or updated
 401       *
 402       * @since 2.0.0 bbPress (r3395)
 403       *
 404       * @param int $topic_id
 405       * @param int $forum_id
 406       * @param array $anonymous_data
 407       * @param int $topic_author_id
 408       * @return Bail early if topic is by anonymous user
 409       */
 410  	public function topic_create( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $topic_author_id = 0 ) {
 411  
 412          // Bail early if topic is by anonymous user
 413          if ( ! empty( $anonymous_data ) ) {
 414              return;
 415          }
 416  
 417          // Bail if site is private
 418          if ( ! bbp_is_site_public() ) {
 419              return;
 420          }
 421  
 422          // Validate activity data
 423          $user_id  = bbp_get_user_id( $topic_author_id );
 424          $topic_id = bbp_get_topic_id( $topic_id );
 425          $forum_id = bbp_get_forum_id( $forum_id );
 426  
 427          // Bail if user is not active
 428          if ( bbp_is_user_inactive( $user_id ) ) {
 429              return;
 430          }
 431  
 432          // Bail if topic is not published
 433          if ( ! bbp_is_topic_published( $topic_id ) ) {
 434              return;
 435          }
 436  
 437          // User link for topic author
 438          $user_link = bbp_get_user_profile_link( $user_id  );
 439  
 440          // Topic
 441          $topic_permalink = bbp_get_topic_permalink( $topic_id );
 442          $topic_title     = get_post_field( 'post_title',   $topic_id, 'raw' );
 443          $topic_content   = get_post_field( 'post_content', $topic_id, 'raw' );
 444          $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
 445  
 446          // Forum
 447          $forum_permalink = bbp_get_forum_permalink( $forum_id );
 448          $forum_title     = get_post_field( 'post_title', $forum_id, 'raw' );
 449          $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
 450  
 451          // Activity action & text
 452          $activity_text    = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
 453          $activity_action  = apply_filters( 'bbp_activity_topic_create',         $activity_text, $user_id,   $topic_id,   $forum_id );
 454          $activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', $topic_content                                     );
 455  
 456          // Compile and record the activity stream results
 457          $activity_id = $this->record_activity( array(
 458              'id'                => $this->get_activity_id( $topic_id ),
 459              'user_id'           => $user_id,
 460              'action'            => $activity_action,
 461              'content'           => $activity_content,
 462              'primary_link'      => $topic_permalink,
 463              'type'              => $this->topic_create,
 464              'item_id'           => $topic_id,
 465              'secondary_item_id' => $forum_id,
 466              'recorded_time'     => get_post_time( 'Y-m-d H:i:s', true, $topic_id ),
 467              'hide_sitewide'     => ! bbp_is_forum_public( $forum_id, false )
 468          ) );
 469  
 470          // Add the activity entry ID as a meta value to the topic
 471          if ( ! empty( $activity_id ) ) {
 472              update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
 473          }
 474      }
 475  
 476      /**
 477       * Delete the activity stream entry when a topic is spammed, trashed, or deleted
 478       *
 479       * @param int $topic_id
 480       */
 481  	public function topic_delete( $topic_id = 0 ) {
 482  
 483          // Get activity ID, bail if it doesn't exist
 484          $activity_id = $this->get_activity_id( $topic_id );
 485          if ( ! empty( $activity_id ) ) {
 486              return bp_activity_delete( array( 'id' => $activity_id ) );
 487          }
 488  
 489          return false;
 490      }
 491  
 492      /**
 493       * Update the activity stream entry when a topic status changes
 494       *
 495       * @param int $topic_id
 496       * @param obj $post
 497       * @return Bail early if not a topic, or topic is by anonymous user
 498       */
 499  	public function topic_update( $topic_id = 0, $post = null ) {
 500  
 501          // Bail early if not a topic
 502          if ( get_post_type( $post ) !== bbp_get_topic_post_type() ) {
 503              return;
 504          }
 505  
 506          // Bail early if revisions are off
 507          if ( ! bbp_allow_revisions() || ! post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
 508              return;
 509          }
 510  
 511          $topic_id = bbp_get_topic_id( $topic_id );
 512  
 513          // Bail early if topic is by anonymous user
 514          if ( bbp_is_topic_anonymous( $topic_id ) ) {
 515              return;
 516          }
 517  
 518          // Action based on new status
 519          if ( bbp_is_topic_public( $post->ID ) ) {
 520  
 521              // Validate topic data
 522              $forum_id        = bbp_get_topic_forum_id( $topic_id );
 523              $topic_author_id = bbp_get_topic_author_id( $topic_id );
 524  
 525              $this->topic_create( $topic_id, $forum_id, array(), $topic_author_id );
 526          } else {
 527              $this->topic_delete( $topic_id );
 528          }
 529      }
 530  
 531      /** Replies ***************************************************************/
 532  
 533      /**
 534       * Record an activity stream entry when a reply is created
 535       *
 536       * @since 2.0.0 bbPress (r3395)
 537       *
 538       * @param int $topic_id
 539       * @param int $forum_id
 540       * @param array $anonymous_data
 541       * @param int $topic_author_id
 542       * @return Bail early if topic is by anonymous user
 543       */
 544  	public function reply_create( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $reply_author_id = 0 ) {
 545  
 546          // Do not log activity of anonymous users
 547          if ( ! empty( $anonymous_data ) ) {
 548              return;
 549          }
 550  
 551          // Bail if site is private
 552          if ( ! bbp_is_site_public() ) {
 553              return;
 554          }
 555  
 556          // Validate activity data
 557          $user_id  = bbp_get_user_id( $reply_author_id );
 558          $reply_id = bbp_get_reply_id( $reply_id );
 559          $topic_id = bbp_get_topic_id( $topic_id );
 560          $forum_id = bbp_get_forum_id( $forum_id );
 561  
 562          // Bail if user is not active
 563          if ( bbp_is_user_inactive( $user_id ) ) {
 564              return;
 565          }
 566  
 567          // Bail if reply is not published
 568          if ( ! bbp_is_reply_published( $reply_id ) ) {
 569              return;
 570          }
 571  
 572          // Setup links for activity stream
 573          $user_link = bbp_get_user_profile_link( $user_id  );
 574  
 575          // Reply
 576          $reply_url     = bbp_get_reply_url( $reply_id );
 577          $reply_content = get_post_field( 'post_content', $reply_id, 'raw' );
 578  
 579          // Topic
 580          $topic_permalink = bbp_get_topic_permalink( $topic_id );
 581          $topic_title     = get_post_field( 'post_title', $topic_id, 'raw' );
 582          $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
 583  
 584          // Forum
 585          $forum_permalink = bbp_get_forum_permalink( $forum_id );
 586          $forum_title     = get_post_field( 'post_title', $forum_id, 'raw' );
 587          $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
 588  
 589          // Activity action & text
 590          $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 );
 591          $activity_action  = apply_filters( 'bbp_activity_reply_create',         $activity_text, $user_id, $reply_id,  $topic_id );
 592          $activity_content = apply_filters( 'bbp_activity_reply_create_excerpt', $reply_content                                  );
 593  
 594          // Compile and record the activity stream results
 595          $activity_id = $this->record_activity( array(
 596              'id'                => $this->get_activity_id( $reply_id ),
 597              'user_id'           => $user_id,
 598              'action'            => $activity_action,
 599              'content'           => $activity_content,
 600              'primary_link'      => $reply_url,
 601              'type'              => $this->reply_create,
 602              'item_id'           => $reply_id,
 603              'secondary_item_id' => $topic_id,
 604              'recorded_time'     => get_post_time( 'Y-m-d H:i:s', true, $reply_id ),
 605              'hide_sitewide'     => ! bbp_is_forum_public( $forum_id, false )
 606          ) );
 607  
 608          // Add the activity entry ID as a meta value to the reply
 609          if ( ! empty( $activity_id ) ) {
 610              update_post_meta( $reply_id, '_bbp_activity_id', $activity_id );
 611          }
 612      }
 613  
 614      /**
 615       * Delete the activity stream entry when a reply is spammed, trashed, or deleted
 616       *
 617       * @param int $reply_id
 618       */
 619  	public function reply_delete( $reply_id ) {
 620  
 621          // Get activity ID, bail if it doesn't exist
 622          $activity_id = $this->get_activity_id( $reply_id );
 623          if ( ! empty( $activity_id ) ) {
 624              return bp_activity_delete( array( 'id' => $activity_id ) );
 625          }
 626  
 627          return false;
 628      }
 629  
 630      /**
 631       * Update the activity stream entry when a reply status changes
 632       *
 633       * @param int $reply_id
 634       * @param obj $post
 635       * @return Bail early if not a reply, or reply is by anonymous user
 636       */
 637  	public function reply_update( $reply_id, $post ) {
 638  
 639          // Bail early if not a reply
 640          if ( get_post_type( $post ) !== bbp_get_reply_post_type() ) {
 641              return;
 642          }
 643  
 644          // Bail early if revisions are off
 645          if ( ! bbp_allow_revisions() || ! post_type_supports( bbp_get_reply_post_type(), 'revisions' ) ) {
 646              return;
 647          }
 648  
 649          $reply_id = bbp_get_reply_id( $reply_id );
 650  
 651          // Bail early if reply is by anonymous user
 652          if ( bbp_is_reply_anonymous( $reply_id ) ) {
 653              return;
 654          }
 655  
 656          // Action based on new status
 657          if ( bbp_get_public_status_id() === $post->post_status ) {
 658  
 659              // Validate reply data
 660              $topic_id        = bbp_get_reply_topic_id( $reply_id );
 661              $forum_id        = bbp_get_reply_forum_id( $reply_id );
 662              $reply_author_id = bbp_get_reply_author_id( $reply_id );
 663  
 664              $this->reply_create( $reply_id, $topic_id, $forum_id, array(), $reply_author_id );
 665          } else {
 666              $this->reply_delete( $reply_id );
 667          }
 668      }
 669  }
 670  endif;


Generated: Wed Jul 24 01:01:31 2019 Cross-referenced by PHPXref 0.7.1