Ticket #6482: 6482.06.patch
File 6482.06.patch, 130.7 KB (added by , 9 years ago) |
---|
-
src/bp-activity/bp-activity-actions.php
diff --git src/bp-activity/bp-activity-actions.php src/bp-activity/bp-activity-actions.php index 7d17133..8109ae7 100644
function bp_activity_catch_transition_post_type_status( $new_status, $old_status 831 831 } 832 832 } 833 833 add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 ); 834 835 /** 836 * When a post type comment status transition occurs, update the relevant activity's status. 837 * 838 * @since 2.5.0 839 * 840 * @param string $new_status New comment status. 841 * @param string $old_status Previous comment status. 842 * @param WP_Comment $comment Comment data. 843 */ 844 function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) { 845 $post_type = get_post_type( $comment->comment_post_ID ); 846 if ( ! $post_type ) { 847 return; 848 } 849 850 // Get the post type tracking args. 851 $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type ); 852 853 // Bail if the activity type does not exist 854 if ( empty( $activity_post_object->comments_tracking->action_id ) ) { 855 return false; 856 857 // Set the $activity_comment_object 858 } else { 859 $activity_comment_object = $activity_post_object->comments_tracking; 860 } 861 862 // Init an empty activity ID 863 $activity_id = 0; 864 865 /** 866 * Activity currently doesn't have any concept of a trash, or an unapproved/approved state. 867 * 868 * If a blog comment transitions to a "delete" or "hold" status, delete the activity item. 869 * If a blog comment transitions to trashed, or spammed, mark the activity as spam. 870 * If a blog comment transitions to approved (and the activity exists), mark the activity as ham. 871 * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam. 872 * Otherwise, record the comment into the activity stream. 873 */ 874 875 // This clause handles delete/hold. 876 if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) { 877 return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object ); 878 879 // These clauses handle trash, spam, and un-spams. 880 } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) { 881 $action = 'spam_activity'; 882 } elseif ( 'approved' == $new_status ) { 883 $action = 'ham_activity'; 884 } 885 886 // Get the activity 887 if ( bp_disable_blogforum_comments() ) { 888 $activity_id = bp_activity_get_activity_id( array( 889 'component' => $activity_comment_object->component_id, 890 'item_id' => get_current_blog_id(), 891 'secondary_item_id' => $comment->comment_ID, 892 'type' => $activity_comment_object->action_id, 893 ) ); 894 } else { 895 $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ); 896 } 897 898 /** 899 * Leave a chance to plugins to manage activity comments differently. 900 * 901 * @since 2.5.0 902 * 903 * @param bool $value True to override BuddyPress management. 904 * @param string $post_type The post type name. 905 * @param int $activity_id The post type activity (0 if not found). 906 * @param string $new_status The new status of the post type comment. 907 * @param string $old_status The old status of the post type comment. 908 * @param WP_Comment $comment Comment data. 909 */ 910 if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) { 911 return false; 912 } 913 914 // Check activity item exists 915 if ( empty( $activity_id ) ) { 916 // If no activity exists, but the comment has been approved, record it into the activity table. 917 if ( 'approved' == $new_status ) { 918 return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object ); 919 } 920 921 return; 922 } 923 924 // Create an activity object 925 $activity = new BP_Activity_Activity( $activity_id ); 926 if ( empty( $activity->component ) ) { 927 return; 928 } 929 930 // Spam/ham the activity if it's not already in that state 931 if ( 'spam_activity' === $action && ! $activity->is_spam ) { 932 bp_activity_mark_as_spam( $activity ); 933 } elseif ( 'ham_activity' == $action) { 934 bp_activity_mark_as_ham( $activity ); 935 } 936 937 // Add "new_post_type_comment" to the whitelisted activity types, so that the activity's Akismet history is generated 938 $post_type_comment_action = $activity_comment_object->action_id; 939 $comment_akismet_history = create_function( '$t', '$t[] = $post_type_comment_action; return $t;' ); 940 add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history ); 941 942 // Make sure the activity change won't edit the comment if sync is on 943 remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 ); 944 945 // Save the updated activity 946 $activity->save(); 947 948 // Restore the action 949 add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 ); 950 951 // Remove the "new_blog_comment" activity type whitelist so we don't break anything 952 remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history ); 953 } 954 add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 ); -
src/bp-activity/bp-activity-admin.php
diff --git src/bp-activity/bp-activity-admin.php src/bp-activity/bp-activity-admin.php index 49c9cad..9cf7e07 100644
class BP_Activity_List_Table extends WP_List_Table { 1110 1110 1111 1111 // Option defaults. 1112 1112 $filter = array(); 1113 $filter_query = false; 1113 1114 $include_id = false; 1114 1115 $search_terms = false; 1115 1116 $sort = 'DESC'; … … class BP_Activity_List_Table extends WP_List_Table { 1136 1137 }*/ 1137 1138 1138 1139 // Filter. 1139 if ( ! empty( $_REQUEST['activity_type'] ) )1140 if ( ! empty( $_REQUEST['activity_type'] ) ) { 1140 1141 $filter = array( 'action' => $_REQUEST['activity_type'] ); 1141 1142 1143 /** 1144 * Filter here to override the filter with a filter query 1145 * 1146 * @since 2.5.0 1147 * 1148 * @param array $filter 1149 */ 1150 $has_filter_query = apply_filters( 'bp_activity_list_table_filter_activity_type_items', $filter ); 1151 1152 if ( ! empty( $has_filter_query['filter_query'] ) ) { 1153 // Reset the filter 1154 $filter = array(); 1155 1156 // And use the filter query instead 1157 $filter_query = $has_filter_query['filter_query']; 1158 } 1159 } 1160 1142 1161 // Are we doing a search? 1143 1162 if ( !empty( $_REQUEST['s'] ) ) 1144 1163 $search_terms = $_REQUEST['s']; … … class BP_Activity_List_Table extends WP_List_Table { 1165 1184 'page' => $page, 1166 1185 'per_page' => $per_page, 1167 1186 'search_terms' => $search_terms, 1187 'filter_query' => $filter_query, 1168 1188 'show_hidden' => true, 1169 1189 // 'sort' => $sort, 1170 1190 'spam' => $spam, … … class BP_Activity_List_Table extends WP_List_Table { 1768 1788 * functions from working as intended. 1769 1789 * 1770 1790 * @since 2.0.0 1791 * @since 2.5.0 Include Post type activities types 1771 1792 * 1772 1793 * @param array $item An array version of the BP_Activity_Activity object. 1773 1794 * @return bool $can_comment 1774 1795 */ 1775 protected function can_comment( $item ) { 1776 $can_comment = true; 1777 1778 if ( $this->disable_blogforum_comments ) { 1779 switch ( $item['type'] ) { 1780 case 'new_blog_post' : 1781 case 'new_blog_comment' : 1782 case 'new_forum_topic' : 1783 case 'new_forum_post' : 1784 $can_comment = false; 1785 break; 1786 } 1796 protected function can_comment( $item ) { 1797 $can_comment = bp_activity_type_supports( $item['type'], 'comment-reply' ); 1787 1798 1788 // Activity comments supported. 1789 } else { 1790 // Activity comment. 1791 if ( 'activity_comment' == $item['type'] ) { 1792 // Blogs. 1793 if ( bp_is_active( 'blogs' ) ) { 1794 // Grab the parent activity entry. 1795 $parent_activity = new BP_Activity_Activity( $item['item_id'] ); 1796 1797 // Fetch blog post comment depth and if the blog post's comments are open. 1798 bp_blogs_setup_activity_loop_globals( $parent_activity ); 1799 1800 // Check if the activity item can be replied to. 1801 if ( false === bp_blogs_can_comment_reply( true, $item ) ) { 1802 $can_comment = false; 1803 } 1804 } 1799 if ( ! $this->disable_blogforum_comments && bp_is_active( 'blogs' ) ) { 1800 $parent_activity = false; 1801 1802 if ( bp_activity_type_supports( $item['type'], 'post-type-comment-tracking' ) ) { 1803 $parent_activity = (object) $item; 1804 } elseif ( 'activity_comment' === $item['type'] ) { 1805 $parent_activity = new BP_Activity_Activity( $item['item_id'] ); 1806 } 1805 1807 1806 // Blog post. 1807 } elseif ( 'new_blog_post' == $item['type'] ) { 1808 if ( bp_is_active( 'blogs' ) ) { 1809 bp_blogs_setup_activity_loop_globals( (object) $item ); 1808 if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) { 1809 // Fetch blog post comment depth and if the blog post's comments are open. 1810 bp_blogs_setup_activity_loop_globals( $parent_activity ); 1810 1811 1811 if ( empty( buddypress()->blogs->allow_comments[$item['id']] ) ) { 1812 $can_comment = false; 1813 } 1814 } 1812 $can_comment = bp_blogs_can_comment_reply( true, $item ); 1815 1813 } 1816 1814 } 1817 1815 … … class BP_Activity_List_Table extends WP_List_Table { 1819 1817 * Filters if an activity item can be commented on or not. 1820 1818 * 1821 1819 * @since 2.0.0 1820 * @since 2.5.0 Add a second parameter to include the activity item into the filter. 1822 1821 * 1823 * @param bool $can_comment Whether an activity item can be commented on or not. 1822 * @param bool $can_comment Whether an activity item can be commented on or not. 1823 * @param array $item An array version of the BP_Activity_Activity object. 1824 1824 */ 1825 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment );1825 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment, $item ); 1826 1826 } 1827 1827 1828 1828 /** -
src/bp-activity/bp-activity-functions.php
diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php index b5e8a93..75464fb 100644
function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array 428 428 return false; 429 429 } 430 430 431 $activity_labels = array( 432 /* Post labels */ 433 'bp_activity_admin_filter', 434 'bp_activity_front_filter', 435 'bp_activity_new_post', 436 'bp_activity_new_post_ms', 437 /* Comment labels */ 438 'bp_activity_comments_admin_filter', 439 'bp_activity_comments_front_filter', 440 'bp_activity_new_comment', 441 'bp_activity_new_comment_ms' 442 ); 443 431 444 // Labels are loaded into the post type object. 432 foreach ( array( 'bp_activity_admin_filter', 'bp_activity_front_filter', 'bp_activity_new_post', 'bp_activity_new_post_ms' )as $label_type ) {445 foreach ( $activity_labels as $label_type ) { 433 446 if ( ! empty( $args[ $label_type ] ) ) { 434 447 $wp_post_types[ $post_type ]->labels->{$label_type} = $args[ $label_type ]; 435 448 unset( $args[ $label_type ] ); … … function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array 446 459 * Get tracking arguments for a specific post type. 447 460 * 448 461 * @since 2.2.0 462 * @since 2.5.0 Add post type comments tracking args 449 463 * 450 464 * @param string $post_type Name of the post type. 451 465 * @return object The tracking arguments of the post type. … … function bp_activity_get_post_type_tracking_args( $post_type ) { 455 469 return false; 456 470 } 457 471 458 $post_type_object = get_post_type_object( $post_type ); 472 $post_type_object = get_post_type_object( $post_type ); 473 $post_type_support_comments = post_type_supports( $post_type, 'comments' ); 459 474 460 475 $post_type_activity = array( 461 'component_id' => buddypress()->activity->id, 462 'action_id' => 'new_' . $post_type, 463 'format_callback' => 'bp_activity_format_activity_action_custom_post_type_post', 464 'front_filter' => $post_type_object->labels->name, 465 'contexts' => array( 'activity' ), 466 'position' => 0, 467 'singular' => strtolower( $post_type_object->labels->singular_name ), 468 'activity_comment' => ! post_type_supports( $post_type, 'comments' ), 476 'component_id' => buddypress()->activity->id, 477 'action_id' => 'new_' . $post_type, 478 'format_callback' => 'bp_activity_format_activity_action_custom_post_type_post', 479 'front_filter' => $post_type_object->labels->name, 480 'contexts' => array( 'activity' ), 481 'position' => 0, 482 'singular' => strtolower( $post_type_object->labels->singular_name ), 483 'activity_comment' => ! $post_type_support_comments, 484 'comment_action_id' => false, 485 'comment_format_callback' => 'bp_activity_format_activity_action_custom_post_type_comment', 469 486 ); 470 487 471 488 if ( ! empty( $post_type_object->bp_activity ) ) { … … function bp_activity_get_post_type_tracking_args( $post_type ) { 498 515 $post_type_activity->new_post_type_action_ms = $post_type_object->labels->bp_activity_new_post_ms; 499 516 } 500 517 518 // If the post type supports comments and has a comment action id, build the comments tracking args 519 if ( $post_type_support_comments && ! empty( $post_type_activity->comment_action_id ) ) { 520 // Init a new container for the activity type for comments 521 $post_type_activity->comments_tracking = new stdClass(); 522 523 // Build the activity type for comments 524 $post_type_activity->comments_tracking->component_id = $post_type_activity->component_id; 525 $post_type_activity->comments_tracking->action_id = $post_type_activity->comment_action_id; 526 527 // Try to get the comments admin filter from the post type labels. 528 if ( ! empty( $post_type_object->labels->bp_activity_comments_admin_filter ) ) { 529 $post_type_activity->comments_tracking->admin_filter = $post_type_object->labels->bp_activity_comments_admin_filter; 530 531 // Fall back to a generic name. 532 } else { 533 $post_type_activity->comments_tracking->admin_filter = _x( 'New item comment posted', 'Post Type generic comments activity admin filter', 'buddypress' ); 534 } 535 536 $post_type_activity->comments_tracking->format_callback = $post_type_activity->comment_format_callback; 537 538 // Check for the comments front filter in the post type labels. 539 if ( ! empty( $post_type_object->labels->bp_activity_comments_front_filter ) ) { 540 $post_type_activity->comments_tracking->front_filter = $post_type_object->labels->bp_activity_comments_front_filter; 541 542 // Fall back to a generic name. 543 } else { 544 $post_type_activity->comments_tracking->front_filter = sprintf( __( '%s comments', 'buddypress' ), $post_type_object->labels->singular_name ); 545 } 546 547 $post_type_activity->comments_tracking->contexts = $post_type_activity->contexts; 548 $post_type_activity->comments_tracking->position = (int) $post_type_activity->position + 1; 549 550 // Try to get the action for new post type comment action on non-multisite installations. 551 if ( ! empty( $post_type_object->labels->bp_activity_new_comment ) ) { 552 $post_type_activity->comments_tracking->new_post_type_comment_action = $post_type_object->labels->bp_activity_new_comment; 553 } 554 555 // Try to get the action for new post type comment action on multisite installations. 556 if ( ! empty( $post_type_object->labels->bp_activity_new_comment_ms ) ) { 557 $post_type_activity->comments_tracking->new_post_type_comment_action_ms = $post_type_object->labels->bp_activity_new_comment_ms; 558 } 559 } 560 561 // Finally make sure we'll be able to find the post type this activity type is associated to. 562 $post_type_activity->post_type = $post_type; 563 501 564 /** 502 565 * Filters tracking arguments for a specific post type. 503 566 * … … function bp_activity_get_post_type_tracking_args( $post_type ) { 513 576 * Get tracking arguments for all post types. 514 577 * 515 578 * @since 2.2.0 579 * @since 2.5.0 Include post type comments tracking args if needed 516 580 * 517 581 * @return array List of post types with their tracking arguments. 518 582 */ … … function bp_activity_get_post_types_tracking_args() { 526 590 $track_post_type = bp_activity_get_post_type_tracking_args( $post_type ); 527 591 528 592 if ( ! empty( $track_post_type ) ) { 593 // Set the post type comments tracking args 594 if ( ! empty( $track_post_type->comments_tracking->action_id ) ) { 595 // Used to check support for comment tracking by activity type (new_post_type_comment) 596 $track_post_type->comments_tracking->comments_tracking = true; 597 598 // Used to be able to find the post type this activity type is associated to. 599 $track_post_type->comments_tracking->post_type = $post_type; 600 601 $post_types_tracking_args[ $track_post_type->comments_tracking->action_id ] = $track_post_type->comments_tracking; 602 603 // Used to check support for comment tracking by activity type (new_post_type) 604 $track_post_type->comments_tracking = true; 605 } 606 529 607 $post_types_tracking_args[ $track_post_type->action_id ] = $track_post_type; 530 608 } 531 609 … … function bp_activity_get_post_types_tracking_args() { 543 621 } 544 622 545 623 /** 624 * Check if the *Post Type* activity supports a specific feature. 625 * 626 * @since 2.5.0 627 * 628 * @param string $activity_type The activity type to check. 629 * @param string $feature The feature to check. Currently supports: 630 * 'post-type-comment-tracking', 'post-type-comment-reply' & 'comment-reply'. 631 * See inline doc for more info. 632 * @return bool 633 */ 634 function bp_activity_type_supports( $activity_type = '', $feature = '' ) { 635 $retval = false; 636 637 $bp = buddypress(); 638 639 switch ( $feature ) { 640 /** 641 * Does this activity type support comment tracking? 642 * 643 * eg. 'new_blog_post' and 'new_blog_comment' will both return true. 644 */ 645 case 'post-type-comment-tracking' : 646 // Set the activity track global if not set yet 647 if ( empty( $bp->activity->track ) ) { 648 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 649 } 650 651 if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) ) { 652 $retval = true; 653 } 654 break; 655 656 /** 657 * Is this a parent activity type that support post comments? 658 * 659 * eg. 'new_blog_post' will return true; 'new_blog_comment' will return false. 660 */ 661 case 'post-type-comment-reply' : 662 // Set the activity track global if not set yet. 663 if ( empty( $bp->activity->track ) ) { 664 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 665 } 666 667 if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) && ! empty( $bp->activity->track[ $activity_type ]->comment_action_id ) ) { 668 $retval = true; 669 } 670 break; 671 672 /** 673 * Does this activity type support comment & reply? 674 */ 675 case 'comment-reply' : 676 // Set the activity track global if not set yet. 677 if ( empty( $bp->activity->track ) ) { 678 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 679 } 680 681 // Post Type activities 682 if ( ! empty( $bp->activity->track[ $activity_type ] ) ) { 683 if ( isset( $bp->activity->track[ $activity_type ]->activity_comment ) ) { 684 $retval = $bp->activity->track[ $activity_type ]->activity_comment; 685 } 686 687 // Eventually override with comment synchronization feature. 688 if ( isset( $bp->activity->track[ $activity_type ]->comments_tracking ) ) { 689 $retval = $bp->activity->track[ $activity_type ]->comments_tracking && ! bp_disable_blogforum_comments(); 690 } 691 692 // Retired Forums component 693 } elseif ( 'new_forum_topic' === $activity_type || 'new_forum_post' === $activity_type ) { 694 $retval = ! bp_disable_blogforum_comments(); 695 696 // By Default, all other activity types are supporting comments. 697 } else { 698 $retval = true; 699 } 700 break; 701 } 702 703 return $retval; 704 } 705 706 /** 707 * Get a specific tracking argument for a given activity type 708 * 709 * @since 2.5.0 710 * 711 * @param string $activity_type the activity type. 712 * @param string $arg the key of the tracking argument. 713 * @return mixed the value of the tracking arg, false if not found. 714 */ 715 function bp_activity_post_type_get_tracking_arg( $activity_type, $arg = '' ) { 716 if ( empty( $activity_type ) || empty( $arg ) ) { 717 return false; 718 } 719 720 $bp = buddypress(); 721 722 // Set the activity track global if not set yet 723 if ( empty( $bp->activity->track ) ) { 724 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 725 } 726 727 if ( isset( $bp->activity->track[ $activity_type ]->{$arg} ) ) { 728 return $bp->activity->track[ $activity_type ]->{$arg}; 729 } else { 730 return false; 731 } 732 } 733 734 /** 546 735 * Get all components' activity actions, sorted by their position attribute. 547 736 * 548 737 * @since 2.2.0 … … function bp_activity_format_activity_action_custom_post_type_post( $action, $act 1405 1594 return apply_filters( 'bp_activity_custom_post_type_post_action', $action, $activity ); 1406 1595 } 1407 1596 1597 /** 1598 * Format activity action strings for custom post types comments. 1599 * 1600 * @since 2.5.0 1601 * 1602 * @param string $action Static activity action. 1603 * @param object $activity Activity data object. 1604 * 1605 * @return string 1606 */ 1607 function bp_activity_format_activity_action_custom_post_type_comment( $action, $activity ) { 1608 $bp = buddypress(); 1609 1610 // Fetch all the tracked post types once. 1611 if ( empty( $bp->activity->track ) ) { 1612 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 1613 } 1614 1615 if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) { 1616 return $action; 1617 } 1618 1619 $user_link = bp_core_get_userlink( $activity->user_id ); 1620 1621 if ( is_multisite() ) { 1622 $blog_link = '<a href="' . esc_url( get_home_url( $activity->item_id ) ) . '">' . get_blog_option( $activity->item_id, 'blogname' ) . '</a>'; 1623 1624 if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms ) ) { 1625 $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms, $user_link, $activity->primary_link, $blog_link ); 1626 } else { 1627 $action = sprintf( _x( '%1$s commented on the <a href="%2$s">item</a>, on the site %3$s', 'Activity Custom Post Type comment action', 'buddypress' ), $user_link, $activity->primary_link, $blog_link ); 1628 } 1629 } else { 1630 if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action ) ) { 1631 $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action, $user_link, $activity->primary_link ); 1632 } else { 1633 $action = sprintf( _x( '%1$s commented on the <a href="%2$s">item</a>', 'Activity Custom Post Type post comment action', 'buddypress' ), $user_link, $activity->primary_link ); 1634 } 1635 } 1636 1637 /** 1638 * Filters the formatted custom post type activity comment action string. 1639 * 1640 * @since 2.5.0 1641 * 1642 * @param string $action Activity action string value. 1643 * @param BP_Activity_Activity $activity Activity item object. 1644 */ 1645 return apply_filters( 'bp_activity_custom_post_type_comment_action', $action, $activity ); 1646 } 1647 1408 1648 /* 1409 1649 * Business functions are where all the magic happens in BuddyPress. They will 1410 1650 * handle the actual saving or manipulation of information. Usually they will … … function bp_activity_post_type_update( $post = null ) { 1993 2233 * Fires after the updating of an activity item for a custom post type entry. 1994 2234 * 1995 2235 * @since 2.2.0 2236 * @since 2.5.0 Add the post type tracking args parameter 1996 2237 * 1997 * @param WP_Post $post Post object. 1998 * @param BP_Activity_Activity $activity Activity object. 2238 * @param WP_Post $post Post object. 2239 * @param BP_Activity_Activity $activity Activity object. 2240 * @param object $activity_post_object The post type tracking args object. 1999 2241 */ 2000 do_action( 'bp_activity_post_type_updated', $post, $activity );2242 do_action( 'bp_activity_post_type_updated', $post, $activity, $activity_post_object ); 2001 2243 2002 2244 return $updated; 2003 2245 } … … function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) { 2053 2295 } 2054 2296 2055 2297 /** 2298 * Create an activity item for a newly posted post type comment. 2299 * 2300 * @since 2.5.0 2301 * 2302 * @param int $comment_id ID of the comment. 2303 * @param bool $is_approved Whether the comment is approved or not. 2304 * @param object $activity_post_object the post type tracking args object. 2305 * 2306 * @return int|bool The ID of the activity on success. False on error. 2307 */ 2308 function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) { 2309 // Get the users comment 2310 $post_type_comment = get_comment( $comment_id ); 2311 2312 // Don't record activity if the comment hasn't been approved 2313 if ( empty( $is_approved ) ) { 2314 return false; 2315 } 2316 2317 // Don't record activity if no email address has been included 2318 if ( empty( $post_type_comment->comment_author_email ) ) { 2319 return false; 2320 } 2321 2322 // Don't record activity if the comment has already been marked as spam 2323 if ( 'spam' === $is_approved ) { 2324 return false; 2325 } 2326 2327 // Get the user by the comment author email. 2328 $user = get_user_by( 'email', $post_type_comment->comment_author_email ); 2329 2330 // If user isn't registered, don't record activity 2331 if ( empty( $user ) ) { 2332 return false; 2333 } 2334 2335 // Get the user_id 2336 $user_id = (int) $user->ID; 2337 2338 // Get blog and post data 2339 $blog_id = get_current_blog_id(); 2340 2341 // Get the post 2342 $post_type_comment->post = get_post( $post_type_comment->comment_post_ID ); 2343 2344 if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) { 2345 return false; 2346 } 2347 2348 /** 2349 * Filters whether to publish activities about the comment regarding the post status 2350 * 2351 * @since 2.5.0 2352 * 2353 * @param bool true to bail, false otherwise. 2354 */ 2355 $is_post_status_not_allowed = (bool) apply_filters( 'bp_activity_post_type_is_post_status_allowed', 'publish' !== $post_type_comment->post->post_status || ! empty( $post_type_comment->post->post_password ) ); 2356 2357 // If this is a password protected post, or not a public post don't record the comment 2358 if ( $is_post_status_not_allowed ) { 2359 return false; 2360 } 2361 2362 // Set post type 2363 $post_type = $post_type_comment->post->post_type; 2364 2365 if ( empty( $activity_post_object ) ) { 2366 // Get the post type tracking args. 2367 $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type ); 2368 2369 // Bail if the activity type does not exist 2370 if ( empty( $activity_post_object->comments_tracking->action_id ) ) { 2371 return false; 2372 } 2373 } 2374 2375 // Set the $activity_comment_object 2376 $activity_comment_object = $activity_post_object->comments_tracking; 2377 2378 /** 2379 * Filters whether or not to post the activity about the comment. 2380 * 2381 * This is a variable filter, dependent on the post type, 2382 * that lets components or plugins bail early if needed. 2383 * 2384 * @since 2.5.0 2385 * 2386 * @param bool $value Whether or not to continue. 2387 * @param int $blog_id ID of the current site. 2388 * @param int $post_id ID of the current post being commented. 2389 * @param int $user_id ID of the current user. 2390 * @param int $comment_id ID of the current comment being posted. 2391 */ 2392 if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) { 2393 return false; 2394 } 2395 2396 // Is this an update ? 2397 $activity_id = bp_activity_get_activity_id( array( 2398 'user_id' => $user_id, 2399 'component' => $activity_comment_object->component_id, 2400 'type' => $activity_comment_object->action_id, 2401 'item_id' => $blog_id, 2402 'secondary_item_id' => $comment_id, 2403 ) ); 2404 2405 // Record this in activity streams. 2406 $comment_link = get_comment_link( $post_type_comment->comment_ID ); 2407 2408 // Backward compatibility filters for the 'blogs' component. 2409 if ( 'blogs' == $activity_comment_object->component_id ) { 2410 $activity_content = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content', array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) ); 2411 $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) ); 2412 } else { 2413 $activity_content = $post_type_comment->comment_content; 2414 $activity_primary_link = $comment_link; 2415 } 2416 2417 $activity_args = array( 2418 'id' => $activity_id, 2419 'user_id' => $user_id, 2420 'content' => $activity_content, 2421 'primary_link' => $activity_primary_link, 2422 'component' => $activity_comment_object->component_id, 2423 'recorded_time' => $post_type_comment->comment_date_gmt, 2424 ); 2425 2426 if ( bp_disable_blogforum_comments() ) { 2427 $blog_url = get_home_url( $blog_id ); 2428 $post_url = add_query_arg( 2429 'p', 2430 $post_type_comment->post->ID, 2431 trailingslashit( $blog_url ) 2432 ); 2433 2434 $activity_args['type'] = $activity_comment_object->action_id; 2435 $activity_args['item_id'] = $blog_id; 2436 $activity_args['secondary_item_id'] = $post_type_comment->comment_ID; 2437 2438 if ( ! empty( $activity_args['content'] ) ) { 2439 // Create the excerpt. 2440 $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args ); 2441 2442 // Backward compatibility filter for blog comments. 2443 if ( 'blogs' == $activity_post_object->component_id ) { 2444 $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type ); 2445 } else { 2446 $activity_args['content'] = $activity_summary; 2447 } 2448 } 2449 2450 // Set up the action by using the format functions. 2451 $action_args = array_merge( $activity_args, array( 2452 'post_title' => $post_type_comment->post->post_title, 2453 'post_url' => $post_url, 2454 'blog_url' => $blog_url, 2455 'blog_name' => get_blog_option( $blog_id, 'blogname' ), 2456 ) ); 2457 2458 $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) ); 2459 2460 // Make sure the action is set. 2461 if ( empty( $activity_args['action'] ) ) { 2462 return; 2463 } else { 2464 // Backward compatibility filter for the blogs component. 2465 if ( 'blogs' === $activity_post_object->component_id ) { 2466 $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] ); 2467 } 2468 } 2469 2470 $activity_id = bp_activity_add( $activity_args ); 2471 } 2472 2473 /** 2474 * Fires after the publishing of an activity item for a newly published post type post. 2475 * 2476 * @since 2.5.0 2477 * 2478 * @param int $activity_id ID of the newly published activity item. 2479 * @param WP_Comment $post_type_comment Comment object. 2480 * @param array $activity_args Array of activity arguments. 2481 * @param object $activity_post_object the post type tracking args object. 2482 */ 2483 do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) ); 2484 2485 return $activity_id; 2486 } 2487 add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 ); 2488 add_action( 'edit_comment', 'bp_activity_post_type_comment', 10 ); 2489 2490 /** 2491 * Remove an activity item when a comment about a post type is deleted. 2492 * 2493 * @since 2.5.0 2494 * 2495 * @param int $comment_id ID of the comment. 2496 * @param object $activity_post_object The post type tracking args object. 2497 * 2498 * @return bool True on success. False on error. 2499 */ 2500 function bp_activity_post_type_remove_comment( $comment_id = 0, $activity_post_object = null ) { 2501 if ( empty( $activity_post_object ) ) { 2502 $comment = get_comment( $comment_id ); 2503 if ( ! $comment ) { 2504 return; 2505 } 2506 2507 $post_type = get_post_type( $comment->comment_post_ID ); 2508 if ( ! $post_type ) { 2509 return; 2510 } 2511 2512 // Get the post type tracking args. 2513 $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type ); 2514 2515 // Bail if the activity type does not exist 2516 if ( empty( $activity_post_object->comments_tracking->action_id ) ) { 2517 return false; 2518 } 2519 } 2520 2521 // Set the $activity_comment_object 2522 $activity_comment_object = $activity_post_object->comments_tracking; 2523 2524 if ( empty( $activity_comment_object->action_id ) ) { 2525 return false; 2526 } 2527 2528 $deleted = false; 2529 2530 if ( bp_disable_blogforum_comments() ) { 2531 $deleted = bp_activity_delete_by_item_id( array( 2532 'item_id' => get_current_blog_id(), 2533 'secondary_item_id' => $comment_id, 2534 'component' => $activity_comment_object->component_id, 2535 'type' => $activity_comment_object->action_id, 2536 'user_id' => false, 2537 ) ); 2538 } 2539 2540 /** 2541 * Fires after the custom post type comment activity was removed. 2542 * 2543 * @since 2.5.0 2544 * 2545 * @param bool $deleted True if the activity was deleted false otherwise 2546 * @param WP_Comment $comment Comment object. 2547 * @param object $activity_post_object The post type tracking args object. 2548 * @param string $value The post type comment activity type. 2549 */ 2550 do_action( 'bp_activity_post_type_remove_comment', $deleted, $comment_id, $activity_post_object, $activity_comment_object->action_id ); 2551 2552 return $deleted; 2553 } 2554 add_action( 'delete_comment', 'bp_activity_post_type_remove_comment', 10, 1 ); 2555 2556 /** 2056 2557 * Add an activity comment. 2057 2558 * 2058 2559 * @since 1.2.0 2560 * @since 2.5.0 Add a new possible parameter $skip_notification for the array of arguments. 2561 * Add the $primary_link parameter for the array of arguments. 2059 2562 * 2060 2563 * @uses wp_parse_args() 2061 2564 * @uses bp_activity_add() … … function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) { 2065 2568 * @uses do_action() To call the 'bp_activity_comment_posted' hook. 2066 2569 * 2067 2570 * @param array|string $args { 2068 * @type int $id Optional. Pass an ID to update an existing comment. 2069 * @type string $content The content of the comment. 2070 * @type int $user_id Optional. The ID of the user making the comment. 2071 * Defaults to the ID of the logged-in user. 2072 * @type int $activity_id The ID of the "root" activity item, ie the oldest 2073 * ancestor of the comment. 2074 * @type int $parent_id Optional. The ID of the parent activity item, ie the item to 2075 * which the comment is an immediate reply. If not provided, 2076 * this value defaults to the $activity_id. 2571 * @type int $id Optional. Pass an ID to update an existing comment. 2572 * @type string $content The content of the comment. 2573 * @type int $user_id Optional. The ID of the user making the comment. 2574 * Defaults to the ID of the logged-in user. 2575 * @type int $activity_id The ID of the "root" activity item, ie the oldest 2576 * ancestor of the comment. 2577 * @type int $parent_id Optional. The ID of the parent activity item, ie the item to 2578 * which the comment is an immediate reply. If not provided, 2579 * this value defaults to the $activity_id. 2580 * @type string $primary_link Optional. the primary link for the comment. 2581 * Defaults to an empty string. 2582 * @type bool $skip_notification Optional. false to send a comment notification, false otherwise. 2583 * Defaults to false. 2077 2584 * } 2078 2585 * @return int|bool The ID of the comment on success, otherwise false. 2079 2586 */ … … function bp_activity_new_comment( $args = '' ) { 2087 2594 } 2088 2595 2089 2596 $r = wp_parse_args( $args, array( 2090 'id' => false, 2091 'content' => false, 2092 'user_id' => bp_loggedin_user_id(), 2093 'activity_id' => false, // ID of the root activity item. 2094 'parent_id' => false // ID of a parent comment (optional). 2597 'id' => false, 2598 'content' => false, 2599 'user_id' => bp_loggedin_user_id(), 2600 'activity_id' => false, // ID of the root activity item. 2601 'parent_id' => false, // ID of a parent comment (optional). 2602 'primary_link' => '', 2603 'skip_notification' => false, 2095 2604 ) ); 2096 2605 2097 2606 // Bail if missing necessary data. … … function bp_activity_new_comment( $args = '' ) { 2138 2647 'content' => $comment_content, 2139 2648 'component' => buddypress()->activity->id, 2140 2649 'type' => 'activity_comment', 2650 'primary_link' => $r['primary_link'], 2141 2651 'user_id' => $r['user_id'], 2142 2652 'item_id' => $activity_id, 2143 2653 'secondary_item_id' => $r['parent_id'], … … function bp_activity_new_comment( $args = '' ) { 2156 2666 } 2157 2667 wp_cache_delete( $activity_id, 'bp_activity' ); 2158 2668 2159 /** 2160 * Fires near the end of an activity comment posting, before the returning of the comment ID. 2161 * 2162 * @since 1.2.0 2163 * 2164 * @param int $comment_id ID of the newly posted activity comment. 2165 * @param array $r Array of parsed comment arguments. 2166 * @param BP_Activity_Activity $activity Activity item being commented on. 2167 */ 2168 do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity ); 2669 if ( empty( $r[ 'skip_notification' ] ) ) { 2670 /** 2671 * Fires near the end of an activity comment posting, before the returning of the comment ID. 2672 * Sends a notification to the user @see bp_activity_new_comment_notification_helper(). 2673 * 2674 * @since 1.2.0 2675 * 2676 * @param int $comment_id ID of the newly posted activity comment. 2677 * @param array $r Array of parsed comment arguments. 2678 * @param int $activity ID of the activity item being commented on. 2679 */ 2680 do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity ); 2681 } else { 2682 /** 2683 * Fires near the end of an activity comment posting, before the returning of the comment ID. 2684 * without sending a notification to the user 2685 * 2686 * @since 2.5.0 2687 * 2688 * @param int $comment_id ID of the newly posted activity comment. 2689 * @param array $r Array of parsed comment arguments. 2690 * @param int $activity ID of the activity item being commented on. 2691 */ 2692 do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity ); 2693 } 2169 2694 2170 2695 if ( empty( $comment_id ) ) { 2171 2696 $errors->add( 'comment_failed', $feedback ); … … function bp_activity_delete( $args = '' ) { 2434 2959 * @return bool True on success, false on failure. 2435 2960 */ 2436 2961 function bp_activity_delete_comment( $activity_id, $comment_id ) { 2962 $deleted = false; 2437 2963 2438 2964 /** 2439 2965 * Filters whether BuddyPress should delete an activity comment or not. … … function bp_activity_delete_comment( $activity_id, $comment_id ) { 2442 2968 * handle the deletion of child comments differently. Make sure you return false. 2443 2969 * 2444 2970 * @since 1.2.0 2971 * @since 2.5.0 Add the deleted parameter (passed by reference) 2445 2972 * 2446 2973 * @param bool $value Whether BuddyPress should continue or not. 2447 2974 * @param int $activity_id ID of the root activity item being deleted. 2448 2975 * @param int $comment_id ID of the comment being deleted. 2449 2976 */ 2450 if ( ! apply_filters ( 'bp_activity_delete_comment_pre', true, $activity_id, $comment_id) ) {2451 return false;2977 if ( ! apply_filters_ref_array( 'bp_activity_delete_comment_pre', array( true, $activity_id, $comment_id, &$deleted ) ) ) { 2978 return $deleted; 2452 2979 } 2453 2980 2454 2981 // Delete any children of this comment. … … function bp_activity_delete_comment( $activity_id, $comment_id ) { 2457 2984 // Delete the actual comment. 2458 2985 if ( ! bp_activity_delete( array( 'id' => $comment_id, 'type' => 'activity_comment' ) ) ) { 2459 2986 return false; 2987 } else { 2988 $deleted = true; 2460 2989 } 2461 2990 2462 2991 // Purge comment cache for the root activity update. … … function bp_activity_delete_comment( $activity_id, $comment_id ) { 2475 3004 */ 2476 3005 do_action( 'bp_activity_delete_comment', $activity_id, $comment_id ); 2477 3006 2478 return true;3007 return $deleted; 2479 3008 } 2480 3009 2481 3010 /** -
src/bp-activity/bp-activity-template.php
diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php index 113d480..f3ab841 100644
function bp_activity_can_comment() { 3425 3425 global $activities_template; 3426 3426 $bp = buddypress(); 3427 3427 3428 // Assume activity can be commented on. 3429 $can_comment = true; 3430 3431 // Determine ability to comment based on activity action name. 3432 $activity_action = bp_get_activity_action_name(); 3433 3434 $turn_off = 0; 3435 if ( ! empty( $activities_template->disable_blogforum_replies ) ) { 3436 $turn_off = 1; 3437 } 3428 // Determine ability to comment based on activity type name. 3429 $activity_type = bp_get_activity_type(); 3438 3430 3439 $maybe_turn_off = array_fill_keys( array( 3440 'new_blog_post', 3441 'new_blog_comment', 3442 'new_forum_topic', 3443 'new_forum_post', 3444 ), $turn_off ); 3431 // Get the 'comment-reply' support for the current activity type. 3432 $can_comment = bp_activity_type_supports( $activity_type, 'comment-reply' ); 3445 3433 3446 $maybe_turn_off['activity_comment'] = 1; 3447 3448 // Fetch all the tracked post types once. 3449 if ( empty( $bp->activity->track ) ) { 3450 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 3434 // Neutralize activity_comment. 3435 if ( 'activity_comment' === $activity_type ) { 3436 $can_comment = false; 3451 3437 } 3452 3438 3453 foreach ( $bp->activity->track as $action => $tracking_args ) {3454 if ( empty( $tracking_args->activity_comment ) ) {3455 $maybe_turn_off[ $action ] = $turn_off;3456 }3457 }3458 3459 $can_comment = empty( $maybe_turn_off[ $activity_action ] );3460 3461 3439 /** 3462 3440 * Filters whether a comment can be made on an activity item. 3463 3441 * 3464 3442 * @since 1.5.0 3443 * @since 2.5.0 Use $activity_type instead of $activity_name for the second parameter. 3465 3444 * 3466 3445 * @param bool $can_comment Status on if activity can be commented on. 3467 * @param string $activity_ action Current activity actionbeing checked on.3446 * @param string $activity_type Current activity type being checked on. 3468 3447 */ 3469 return apply_filters( 'bp_activity_can_comment', $can_comment, $activity_ action);3448 return apply_filters( 'bp_activity_can_comment', $can_comment, $activity_type ); 3470 3449 } 3471 3450 3472 3451 /** -
src/bp-blogs/bp-blogs-activity.php
diff --git src/bp-blogs/bp-blogs-activity.php src/bp-blogs/bp-blogs-activity.php index af93731..b822f50 100644
defined( 'ABSPATH' ) || exit; 18 18 * @return bool|null Returns false if activity component is not active. 19 19 */ 20 20 function bp_blogs_register_activity_actions() { 21 $bp = buddypress();22 23 // Bail if activity is not active.24 if ( ! bp_is_active( 'activity' ) ) {25 return false;26 }27 28 21 if ( is_multisite() ) { 29 22 bp_activity_set_action( 30 $bp->blogs->id,23 buddypress()->blogs->id, 31 24 'new_blog', 32 25 __( 'New site created', 'buddypress' ), 33 26 'bp_blogs_format_activity_action_new_blog', … … function bp_blogs_register_activity_actions() { 37 30 ); 38 31 } 39 32 40 // Only add the comment type if the 'post' post type is trackable.41 if ( post_type_supports( 'post', 'buddypress-activity' ) ) {42 bp_activity_set_action(43 $bp->blogs->id,44 'new_blog_comment',45 __( 'New post comment posted', 'buddypress' ),46 'bp_blogs_format_activity_action_new_blog_comment',47 __( 'Comments', 'buddypress' ),48 array( 'activity', 'member' ),49 1050 );51 }52 53 33 /** 54 34 * Fires after the registry of the default blog component activity actions. 55 35 * … … function bp_blogs_register_activity_actions() { 60 40 add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_actions' ); 61 41 62 42 /** 43 * Set up the tracking arguments for the 'post' post type. 44 * 45 * @since 2.5.0 This was moved out of the BP_Blogs_Component class. 46 * 47 * @see bp_activity_get_post_type_tracking_args() for information on parameters. 48 * 49 * @param object|null $params Tracking arguments. 50 * @param string|int $post_type Post type to track. 51 * @return object 52 */ 53 function bp_blogs_register_post_tracking_args( $params = null, $post_type = 0 ) { 54 55 /** 56 * Filters the post types to track for the Blogs component. 57 * 58 * @since 1.5.0 59 * @deprecated 2.3.0 60 * 61 * Make sure plugins still using 'bp_blogs_record_post_post_types' 62 * to track their post types will generate new_blog_post activities 63 * See https://buddypress.trac.wordpress.org/ticket/6306 64 * 65 * @param array $value Array of post types to track. 66 */ 67 $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ); 68 $post_types_array = array_flip( $post_types ); 69 70 if ( ! isset( $post_types_array[ $post_type ] ) ) { 71 return $params; 72 } 73 74 // Set specific params for the 'post' post type. 75 $params->component_id = buddypress()->blogs->id; 76 $params->action_id = 'new_blog_post'; 77 $params->admin_filter = __( 'New post published', 'buddypress' ); 78 $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post'; 79 $params->front_filter = __( 'Posts', 'buddypress' ); 80 $params->contexts = array( 'activity', 'member' ); 81 $params->position = 5; 82 83 if ( post_type_supports( $post_type, 'comments' ) ) { 84 $params->comment_action_id = 'new_blog_comment'; 85 86 /** 87 * Filters the post types to track for the Blogs component. 88 * 89 * @since 1.5.0 90 * @deprecated 2.5.0 91 * 92 * Make sure plugins still using 'bp_blogs_record_comment_post_types' 93 * to track comment about their post types will generate new_blog_comment activities 94 * See https://buddypress.trac.wordpress.org/ticket/6306 95 * 96 * @param array $value Array of post types to track. 97 */ 98 $comment_post_types = apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) ); 99 $comment_post_types_array = array_flip( $comment_post_types ); 100 101 if ( isset( $comment_post_types_array[ $post_type ] ) ) { 102 $params->comments_tracking = new stdClass(); 103 $params->comments_tracking->component_id = buddypress()->blogs->id; 104 $params->comments_tracking->action_id = 'new_blog_comment'; 105 $params->comments_tracking->admin_filter = __( 'New post comment posted', 'buddypress' ); 106 $params->comments_tracking->format_callback = 'bp_blogs_format_activity_action_new_blog_comment'; 107 $params->comments_tracking->front_filter = __( 'Comments', 'buddypress' ); 108 $params->comments_tracking->contexts = array( 'activity', 'member' ); 109 $params->comments_tracking->position = 10; 110 } 111 } 112 113 return $params; 114 } 115 add_filter( 'bp_activity_get_post_type_tracking_args', 'bp_blogs_register_post_tracking_args', 10, 2 ); 116 117 /** 63 118 * Format 'new_blog' activity actions. 64 119 * 65 120 * @since 2.0.0 … … function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) { 218 273 * @return string Constructed activity action. 219 274 */ 220 275 function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) { 221 $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' ); 222 $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' ); 276 /** 277 * When the comment is published we are faking an activity object 278 * to which we add 4 properties : 279 * - the post url 280 * - the post title 281 * - the blog url 282 * - the blog name 283 * This is done to build the 'post link' part of the activity 284 * action string. 285 * NB: in this case the activity has not yet been created. 286 */ 287 288 $blog_url = false; 289 290 // Try to get the blog url from the activity object 291 if ( isset( $activity->blog_url ) ) { 292 $blog_url = $activity->blog_url; 293 } else { 294 $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' ); 295 } 296 297 $blog_name = false; 298 299 // Try to get the blog name from the activity object 300 if ( isset( $activity->blog_name ) ) { 301 $blog_name = $activity->blog_name; 302 } else { 303 $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' ); 304 } 223 305 224 306 if ( empty( $blog_url ) || empty( $blog_name ) ) { 225 307 $blog_url = get_home_url( $activity->item_id ); … … function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) 229 311 bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name ); 230 312 } 231 313 232 $post_url = bp_activity_get_meta( $activity->id, 'post_url' ); 233 $post_title = bp_activity_get_meta( $activity->id, 'post_title' ); 314 $post_url = false; 315 316 // Try to get the post url from the activity object 317 if ( isset( $activity->post_url ) ) { 318 $post_url = $activity->post_url; 319 320 /** 321 * The post_url property is not set, we need to build the url 322 * thanks to the post id which is also saved as the secondary 323 * item id property of the activity object. 324 */ 325 } elseif ( ! empty( $activity->id ) ) { 326 $post_url = bp_activity_get_meta( $activity->id, 'post_url' ); 327 } 328 329 $post_title = false; 330 331 // Should be the case when the comment has just been published 332 if ( isset( $activity->post_title ) ) { 333 $post_title = $activity->post_title; 334 335 // If activity already exists try to get the post title from activity meta 336 } elseif ( ! empty( $activity->id ) ) { 337 $post_title = bp_activity_get_meta( $activity->id, 'post_title' ); 338 } 234 339 235 340 // Should only be empty at the time of post creation. 236 341 if ( empty( $post_url ) || empty( $post_title ) ) { … … add_filter( 'bp_activity_prefetch_object_data', 'bp_blogs_prefetch_activity_obje 333 438 * @return int|bool On success, returns the activity ID. False on failure. 334 439 */ 335 440 function bp_blogs_record_activity( $args = '' ) { 336 337 // Bail if activity is not active.338 if ( ! bp_is_active( 'activity' ) ) {339 return false;340 }341 342 $bp = buddypress();343 344 441 $defaults = array( 345 442 'user_id' => bp_loggedin_user_id(), 346 443 'action' => '', 347 444 'content' => '', 348 445 'primary_link' => '', 349 'component' => $bp->blogs->id,446 'component' => buddypress()->blogs->id, 350 447 'type' => false, 351 448 'item_id' => false, 352 449 'secondary_item_id' => false, … … function bp_blogs_record_activity( $args = '' ) { 410 507 * @return bool True on success, false on failure. 411 508 */ 412 509 function bp_blogs_delete_activity( $args = '' ) { 413 414 // Bail if activity is not active.415 if ( ! bp_is_active( 'activity' ) ) {416 return false;417 }418 419 510 $r = bp_parse_args( $args, array( 420 511 'item_id' => false, 421 512 'component' => buddypress()->blogs->id, … … function bp_blogs_comments_open( $activity ) { 522 613 * 523 614 * Note: This is only a one-way sync - activity comments -> blog comment. 524 615 * 525 * For blog post -> activity comment, see {@link bp_ blogs_record_comment()}.616 * For blog post -> activity comment, see {@link bp_activity_post_type_comment()}. 526 617 * 527 618 * @since 2.0.0 619 * @since 2.5.0 Allow custom post types to sync their comments with activity ones 528 620 * 529 621 * @param int $comment_id The activity ID for the posted activity comment. 530 622 * @param array $params Parameters for the activity comment. 531 623 * @param object $parent_activity Parameters of the parent activity item (in this case, the blog post). 532 624 */ 533 625 function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_activity ) { 534 // If parent activity isn't a blog post, stop now!535 if ( $parent_activity->type != 'new_blog_post') {626 // if parent activity isn't a post type having the buddypress-activity support, stop now! 627 if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) { 536 628 return; 537 629 } 538 630 … … function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 548 640 $user = bp_core_get_core_userdata( $params['user_id'] ); 549 641 } 550 642 643 // Get associated post type and set default comment parent 644 $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ); 645 $comment_parent = 0; 646 551 647 // See if a parent WP comment ID exists. 552 if ( ! empty( $params['parent_id'] ) ) { 553 $comment_parent = bp_activity_get_meta( $params['parent_id'], 'bp_blogs_post_comment_id' ); 554 } else { 555 $comment_parent = 0; 648 if ( ! empty( $params['parent_id'] ) && ! empty( $post_type ) ) { 649 $comment_parent = bp_activity_get_meta( $params['parent_id'], "bp_blogs_{$post_type}_comment_id" ); 556 650 } 557 651 558 652 // Comment args. … … function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 565 659 'comment_type' => '', // Could be interesting to add 'buddypress' here... 566 660 'comment_parent' => (int) $comment_parent, 567 661 'user_id' => $params['user_id'], 568 569 // Commenting these out for now570 // 'comment_author_IP' => '127.0.0.1',571 // 'comment_agent' => '', .572 662 'comment_approved' => 1 573 663 ); 574 664 575 665 // Prevent separate activity entry being made. 576 remove_action( 'comment_post', 'bp_ blogs_record_comment', 10, 2 );666 remove_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 ); 577 667 578 668 // Handle multisite. 579 669 switch_to_blog( $parent_activity->item_id ); … … function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 589 679 add_comment_meta( $post_comment_id, 'bp_activity_comment_id', $comment_id ); 590 680 591 681 // Add meta to activity comment. 592 bp_activity_update_meta( $comment_id, 'bp_blogs_post_comment_id', $post_comment_id ); 682 if ( ! empty( $post_type ) ) { 683 bp_activity_update_meta( $comment_id, "bp_blogs_{$post_type}_comment_id", $post_comment_id ); 684 } 593 685 594 686 // Resave activity comment with WP comment permalink. 595 687 // … … function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 616 708 restore_current_blog(); 617 709 618 710 // Add the comment hook back. 619 add_action( 'comment_post', 'bp_ blogs_record_comment', 10, 2 );711 add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 ); 620 712 621 713 /** 622 714 * Fires after activity comments have been synced and posted as blog comments. … … add_action( 'bp_activity_comment_posted', 'bp_blogs_sync_add_from_activity_comme 640 732 * activity comment children before they are deleted. 641 733 * 642 734 * @since 2.0.0 735 * @since 2.5.0 Add the $delected parameter 643 736 * 644 737 * @param bool $retval Whether BuddyPress should continue or not. 645 738 * @param int $parent_activity_id The parent activity ID for the activity comment. 646 739 * @param int $activity_id The activity ID for the pending deleted activity comment. 740 * @param bool $deleted Whether the comment was deleted or not. 647 741 * @return bool 648 742 */ 649 function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id ) {743 function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id, &$deleted ) { 650 744 // Check if parent activity is a blog post. 651 745 $parent_activity = new BP_Activity_Activity( $parent_activity_id ); 652 if ( 'new_blog_post' != $parent_activity->type ) { 746 747 // if parent activity isn't a post type having the buddypress-activity support, stop now! 748 if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) { 653 749 return $retval; 654 750 } 655 751 … … function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_i 657 753 $activity = bp_activity_get( array( 658 754 'in' => $activity_id, 659 755 'display_comments' => 'stream', 756 'spam' => 'all', 660 757 ) ); 661 758 662 759 // Get all activity comment IDs for the pending deleted item. … … function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_i 677 774 // emulate bp_activity_delete_comment(). 678 775 BP_Activity_Activity::rebuild_activity_comment_tree( $parent_activity_id ); 679 776 777 // Avoid the error message although the comments were successfully deleted 778 $deleted = true; 779 680 780 // We're overriding the default bp_activity_delete_comment() functionality 681 781 // so we need to return false. 682 782 return false; 683 783 } 684 add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 3);784 add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 4 ); 685 785 686 786 /** 687 787 * Updates the blog comment when the associated activity comment is edited. … … add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activit 691 791 * @param BP_Activity_Activity $activity The activity object. 692 792 */ 693 793 function bp_blogs_sync_activity_edit_to_post_comment( BP_Activity_Activity $activity ) { 694 // Not an activity comment? stop now!695 if ( 'activity_comment' !== $activity->type ) {696 return;697 }698 699 794 // This is a new entry, so stop! 700 795 // We only want edits! 701 if ( empty( $activity->id ) ) {796 if ( empty( $activity->id ) || bp_disable_blogforum_comments() ) { 702 797 return; 703 798 } 704 799 705 // Prevent recursion.706 remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20);800 // fetch parent activity item 801 $parent_activity = new BP_Activity_Activity( $activity->item_id ); 707 802 708 // Try to see if a corresponding blog comment exists. 709 $post_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' ); 803 // if parent activity isn't a post type having the buddypress-activity support for comments, stop now! 804 if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) { 805 return; 806 } 710 807 711 if ( empty( $post_comment_id ) ) { 808 $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ); 809 810 // No associated post type for this activity comment, stop. 811 if ( ! $post_type ) { 712 812 return; 713 813 } 714 814 715 // Fetch parent activity item.716 $p arent_activity = new BP_Activity_Activity( $activity->item_id);815 // Try to see if a corresponding blog comment exists. 816 $post_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" ); 717 817 718 // Sanity check. 719 if ( 'new_blog_post' !== $parent_activity->type ) { 818 if ( empty( $post_comment_id ) ) { 720 819 return; 721 820 } 722 821 723 822 // Handle multisite. 724 823 switch_to_blog( $parent_activity->item_id ); 725 824 726 // Update the blog post comment. 727 wp_update_comment( array( 728 'comment_ID' => $post_comment_id, 729 'comment_content' => $activity->content 730 ) ); 825 // Get the comment status 826 $post_comment_status = wp_get_comment_status( $post_comment_id ); 827 $old_comment_status = $post_comment_status; 828 829 // No need to edit the activity, as it's the activity who's updating the comment 830 remove_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 ); 831 remove_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 ); 832 833 if ( 1 === (int) $activity->is_spam && 'spam' !== $post_comment_status ) { 834 wp_spam_comment( $post_comment_id ); 835 } elseif ( ! $activity->is_spam ) { 836 if ( 'spam' === $post_comment_status ) { 837 wp_unspam_comment( $post_comment_id ); 838 } elseif ( 'trash' === $post_comment_status ) { 839 wp_untrash_comment( $post_comment_id ); 840 } else { 841 // Update the blog post comment. 842 wp_update_comment( array( 843 'comment_ID' => $post_comment_id, 844 'comment_content' => $activity->content, 845 ) ); 846 } 847 } 848 849 // Restore actions 850 add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 ); 851 add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 ); 731 852 732 853 restore_current_blog(); 733 854 } … … add_action( 'trashed_post_comments', 'bp_blogs_remove_activity_meta_for_trashed_ 769 890 * powers the 'Comments' filter in the activity directory dropdown) includes 770 891 * both old-style and new-style activity comments. 771 892 * 772 * This implementation involves filtering the activity queries directly, and773 * should be considered a stopgap. The proper solution would involve enabling774 * multiple query condition clauses, connected by an OR, in the bp_has_activities()775 * API.776 *777 893 * @since 2.1.0 894 * @since 2.5.0 Used for any synced Post type comments, in wp-admin or front-end contexts. 778 895 * 779 896 * @param array $args Arguments passed from bp_parse_args() in bp_has_activities(). 780 897 * @return array $args … … function bp_blogs_new_blog_comment_query_backpat( $args ) { 783 900 global $wpdb; 784 901 $bp = buddypress(); 785 902 786 // Bail if this is not a 'new_blog_comment' query. 787 if ( 'new_blog_comment' !== $args['action'] ) { 903 // If activity comments are disabled for blog posts, stop now! 904 if ( bp_disable_blogforum_comments() ) { 905 return $args; 906 } 907 908 // Get the associated post type 909 $post_type = bp_activity_post_type_get_tracking_arg( $args['action'], 'post_type' ); 910 911 // Bail if this is not an activity associated with a post type 912 if ( empty( $post_type ) ) { 913 return $args; 914 } 915 916 // Bail if this is an activity about posts and not comments 917 if ( bp_activity_post_type_get_tracking_arg( $args['action'], 'comment_action_id' ) ) { 788 918 return $args; 789 919 } 790 920 791 921 // Comment synced ? 792 $activity_ids = $wpdb->get_col( $wpdb->prepare( "SELECT activity_id FROM {$bp->activity->table_name_meta} WHERE meta_key = %s", 'bp_blogs_post_comment_id') );922 $activity_ids = $wpdb->get_col( $wpdb->prepare( "SELECT activity_id FROM {$bp->activity->table_name_meta} WHERE meta_key = %s", "bp_blogs_{$post_type}_comment_id" ) ); 793 923 794 924 if ( empty( $activity_ids ) ) { 795 925 return $args; … … function bp_blogs_new_blog_comment_query_backpat( $args ) { 798 928 // Init the filter query. 799 929 $filter_query = array(); 800 930 801 if ( 'null' === $args['scope'] ) {931 if ( ! isset( $args['scope'] ) || 'null' === $args['scope'] ) { 802 932 $args['scope'] = ''; 803 933 } elseif ( 'just-me' === $args['scope'] ) { 804 934 $filter_query = array( … … function bp_blogs_new_blog_comment_query_backpat( $args ) { 831 961 832 962 // Finally reset the action. 833 963 $args['action'] = ''; 964 $args['type'] = ''; 834 965 835 966 // Return the original arguments. 836 967 return $args; 837 968 } 838 add_filter( 'bp_after_has_activities_parse_args', 'bp_blogs_new_blog_comment_query_backpat' ); 969 add_filter( 'bp_after_has_activities_parse_args', 'bp_blogs_new_blog_comment_query_backpat' ); 970 add_filter( 'bp_activity_list_table_filter_activity_type_items', 'bp_blogs_new_blog_comment_query_backpat' ); 839 971 840 972 /** 841 973 * Utility function to set up some variables for use in the activity loop. … … function bp_blogs_setup_activity_loop_globals( $activity ) { 857 989 return; 858 990 } 859 991 860 // Parent not a blog post? stop now!861 if ( 'new_blog_post' !== $activity->type) {992 // The activity type does not support comments or replies ? stop now! 993 if ( ! bp_activity_type_supports( $activity->type, 'post-type-comment-reply' ) ) { 862 994 return; 863 995 } 864 996 … … add_action( 'bp_before_activity_comment', 'bp_blogs_setup_comment_loop_globals_o 935 1067 * @return bool 936 1068 */ 937 1069 function bp_blogs_disable_activity_commenting( $retval ) { 1070 global $activities_template; 1071 938 1072 // If activity commenting is disabled, return current value. 939 if ( bp_disable_blogforum_comments() ) {1073 if ( bp_disable_blogforum_comments() || ! isset( $activities_template->in_the_loop ) ) { 940 1074 return $retval; 941 1075 } 942 1076 943 // Activity commenting is enabled for blog posts. 944 switch ( bp_get_activity_action_name() ) { 945 946 // We still have to disable activity commenting for 'new_blog_comment' items 947 // commenting should only be done on the parent 'new_blog_post' item. 948 case 'new_blog_comment' : 949 $retval = false; 950 951 break; 952 953 // Check if commenting is disabled for the WP blog post 954 // we should extrapolate this and automate this for plugins... or not. 955 case 'new_blog_post' : 956 global $activities_template; 1077 $type = bp_get_activity_type(); 957 1078 1079 // It's a post type supporting comment tracking. 1080 if ( bp_activity_type_supports( $type, 'post-type-comment-tracking' ) ) { 1081 // The activity type is supporting comments or replies 1082 if ( bp_activity_type_supports( $type, 'post-type-comment-reply' ) ) { 958 1083 // Setup some globals we'll need to reference later. 959 1084 bp_blogs_setup_activity_loop_globals( $activities_template->activity ); 960 1085 961 1086 // If comments are closed for the WP blog post, we should disable 962 1087 // activity comments for this activity entry. 963 if ( empty( buddypress()->blogs->allow_comments[ bp_get_activity_id()] ) ) {1088 if ( empty( buddypress()->blogs->allow_comments[ bp_get_activity_id() ] ) ) { 964 1089 $retval = false; 965 1090 } 966 967 break; 1091 // The activity type does not support comments or replies 1092 } else { 1093 $retval = false; 1094 } 968 1095 } 969 1096 970 1097 return $retval; … … function bp_blogs_disable_activity_commenting( $retval ) { 972 1099 add_filter( 'bp_activity_can_comment', 'bp_blogs_disable_activity_commenting' ); 973 1100 974 1101 /** 1102 * Limit the display of post type synced comments. 1103 * 1104 * @since 2.5.0 1105 * 1106 * When viewing the synced comments in stream mode, this prevents comments to 1107 * be displayed twice, and avoids a Javascript error as the form to add replies 1108 * is not available. 1109 * 1110 * @param int $retval The comment count for the activity. 1111 * @return int The comment count, or 0 to hide activity comment replies. 1112 */ 1113 function bp_blogs_post_type_comments_avoid_duplicates( $retval ) { 1114 /** 1115 * Only limit the display when Post type comments are synced with 1116 * activity comments. 1117 */ 1118 if ( bp_disable_blogforum_comments() ) { 1119 return $retval; 1120 } 1121 1122 if ( 'activity_comment' !== bp_get_activity_type() ) { 1123 return $retval; 1124 } 1125 1126 // Check the parent activity 1127 $parent_activity = new BP_Activity_Activity( bp_get_activity_item_id() ); 1128 1129 if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) { 1130 $retval = 0; 1131 } 1132 1133 return $retval; 1134 } 1135 add_filter( 'bp_activity_get_comment_count', 'bp_blogs_post_type_comments_avoid_duplicates' ); 1136 1137 /** 975 1138 * Check if an activity comment associated with a blog post can be replied to. 976 1139 * 977 1140 * By default, disables replying to activity comments if the corresponding WP … … function bp_blogs_activity_comment_single_permalink( $retval, $activity ) { 1057 1220 return $retval; 1058 1221 } 1059 1222 1060 $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' ); 1223 if ( bp_disable_blogforum_comments() ) { 1224 return $retval; 1225 } 1226 1227 $parent_activity = new BP_Activity_Activity( $activity->item_id ); 1061 1228 1062 if ( ! empty( $blog_comment_id) ) {1229 if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) { 1063 1230 $retval = $activity->primary_link; 1064 1231 } 1065 1232 … … function bp_blogs_activity_comment_single_action( $retval, $activity ) { 1083 1250 return $retval; 1084 1251 } 1085 1252 1086 $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' ); 1253 if ( bp_disable_blogforum_comments() ) { 1254 return $retval; 1255 } 1256 1257 $parent_activity = new BP_Activity_Activity( $activity->item_id ); 1258 1259 if ( ! isset( $parent_activity->type ) ) { 1260 return $retval; 1261 } 1262 1263 $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ); 1264 1265 if ( ! $post_type ) { 1266 return $retval; 1267 } 1268 1269 $blog_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" ); 1087 1270 1088 1271 if ( ! empty( $blog_comment_id ) ) { 1089 // Fetch the parent blog post activity item. 1090 $parent_blog_post_activity = new BP_Activity_Activity( $activity->item_id ); 1272 $bp = buddypress(); 1091 1273 1092 // Fake a 'new_blog_comment' activity object.1093 $ object = $activity;1274 // Check if a comment action id is set for the parent activity 1275 $comment_action_id = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'comment_action_id' ); 1094 1276 1095 // Override 'item_id' to use blog ID. 1096 $object->item_id = $parent_blog_post_activity->item_id; 1277 // Use the action string callback for the activity type 1278 if ( ! empty( $comment_action_id ) ) { 1279 // Fake a 'new_{post_type}_comment' by cloning the activity object. 1280 $object = clone $activity; 1097 1281 1098 // Override 'secondary_item_id' to use comment ID.1099 $object->secondary_item_id = $blog_comment_id;1282 // Set the type of the activity to be a comment about a post type 1283 $object->type = $comment_action_id; 1100 1284 1101 // Now format the activity action using the 'new_blog_comment' action callback. 1102 $retval = bp_blogs_format_activity_action_new_blog_comment( '', $object ); 1285 // Use the blog ID as the item_id. 1286 $object->item_id = $parent_activity->item_id; 1287 1288 // Use comment ID as the secondary_item_id. 1289 $object->secondary_item_id = $blog_comment_id; 1290 1291 // Get the format callback for this activity comment 1292 $format_callback = bp_activity_post_type_get_tracking_arg( $comment_action_id, 'format_callback' ); 1293 1294 // now format the activity action using the 'new_{post_type}_comment' action callback 1295 if ( is_callable( $format_callback ) ) { 1296 $retval = call_user_func_array( $format_callback, array( '', $object ) ); 1297 } 1298 } 1103 1299 } 1104 1300 1105 1301 return $retval; -
src/bp-blogs/bp-blogs-filters.php
diff --git src/bp-blogs/bp-blogs-filters.php src/bp-blogs/bp-blogs-filters.php index 5cb6a2f..bfd96f0 100644
function bp_blogs_comments_clauses_select_by_id( $retval ) { 61 61 } 62 62 63 63 /** 64 * Check whether the current post can be published.64 * Check whether the current activity about a post or a comment can be published. 65 65 * 66 66 * Abstracted from the deprecated `bp_blogs_record_post()`. 67 67 * … … function bp_blogs_post_pre_publish( $return = true, $blog_id = 0, $post_id = 0, 121 121 return $return; 122 122 } 123 123 add_filter( 'bp_activity_post_pre_publish', 'bp_blogs_post_pre_publish', 10, 4 ); 124 add_filter( 'bp_activity_post_pre_comment', 'bp_blogs_post_pre_publish', 10, 4 ); -
src/bp-blogs/bp-blogs-functions.php
diff --git src/bp-blogs/bp-blogs-functions.php src/bp-blogs/bp-blogs-functions.php index 02e3477..a608107 100644
add_action( 'bp_activity_post_type_published', 'bp_blogs_publish_post_activity_m 477 477 * Updates a blog post's activity meta entry during a post edit. 478 478 * 479 479 * @since 2.2.0 480 * @since 2.5.0 Add the post type tracking args object parameter 480 481 * 481 * @param WP_Post $post Post object. 482 * @param BP_Activity_Activity $activity Activity object. 482 * @param WP_Post $post Post object. 483 * @param BP_Activity_Activity $activity Activity object. 484 * @param object $activity_post_object The post type tracking args object. 483 485 */ 484 function bp_blogs_update_post_activity_meta( $post, $activity ) {485 if ( empty( $activity->id ) || 'post' != $post->post_type) {486 function bp_blogs_update_post_activity_meta( $post, $activity, $activity_post_object ) { 487 if ( empty( $activity->id ) || empty( $activity_post_object->action_id ) ) { 486 488 return; 487 489 } 488 490 … … function bp_blogs_update_post_activity_meta( $post, $activity ) { 491 493 if ( $post->post_title !== $existing_title ) { 492 494 bp_activity_update_meta( $activity->id, 'post_title', $post->post_title ); 493 495 494 // Now update activity meta for post comments... sigh. 495 add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 496 $comments = get_comments( array( 'post_id' => $post->ID ) ); 497 remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 498 499 if ( ! empty( $comments ) ) { 500 $activity_ids = array(); 501 $comment_ids = wp_list_pluck( $comments, 'comment_ID' ); 502 503 // Set up activity args. 504 $args = array( 505 'update_meta_cache' => false, 506 'show_hidden' => true, 507 'per_page' => 99999, 508 ); 509 510 // Query for old-style "new_blog_comment" activity items. 511 $args['filter'] = array( 512 'object' => buddypress()->blogs->id, 513 'action' => 'new_blog_comment', 514 'secondary_id' => implode( ',', $comment_ids ), 515 ); 516 517 $activities = bp_activity_get( $args ); 518 if ( ! empty( $activities['activities'] ) ) { 519 $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' ); 520 } 521 522 // Query for activity comments connected to a blog post. 523 unset( $args['filter'] ); 524 $args['meta_query'] = array( array( 525 'key' => 'bp_blogs_post_comment_id', 526 'value' => $comment_ids, 527 'compare' => 'IN', 528 ) ); 529 $args['type'] = 'activity_comment'; 530 $args['display_comments'] = 'stream'; 496 if ( ! empty( $activity_post_object->comments_tracking->action_id ) ) { 497 // Now update activity meta for post comments... sigh. 498 add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 499 $comments = get_comments( array( 'post_id' => $post->ID ) ); 500 remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 501 502 if ( ! empty( $comments ) ) { 503 $activity_ids = array(); 504 $comment_ids = wp_list_pluck( $comments, 'comment_ID' ); 505 506 // Set up activity args. 507 $args = array( 508 'update_meta_cache' => false, 509 'show_hidden' => true, 510 'per_page' => 99999, 511 ); 512 513 // Query for old-style "new_blog_comment" activity items. 514 $args['filter'] = array( 515 'object' => $activity_post_object->comments_tracking->component_id, 516 'action' => $activity_post_object->comments_tracking->action_id, 517 'secondary_id' => implode( ',', $comment_ids ), 518 ); 519 520 $activities = bp_activity_get( $args ); 521 if ( ! empty( $activities['activities'] ) ) { 522 $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' ); 523 } 531 524 532 $activities = bp_activity_get( $args ); 533 if ( ! empty( $activities['activities'] ) ) { 534 $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) ); 535 } 525 // Query for activity comments connected to a blog post. 526 unset( $args['filter'] ); 527 $args['meta_query'] = array( array( 528 'key' => 'bp_blogs_' . $post->post_type . '_comment_id', 529 'value' => $comment_ids, 530 'compare' => 'IN', 531 ) ); 532 $args['type'] = 'activity_comment'; 533 $args['display_comments'] = 'stream'; 534 535 $activities = bp_activity_get( $args ); 536 if ( ! empty( $activities['activities'] ) ) { 537 $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) ); 538 } 536 539 537 // Update activity meta for all found activity items. 538 if ( ! empty( $activity_ids ) ) { 539 foreach ( $activity_ids as $aid ) { 540 bp_activity_update_meta( $aid, 'post_title', $post->post_title ); 540 // Update activity meta for all found activity items. 541 if ( ! empty( $activity_ids ) ) { 542 foreach ( $activity_ids as $aid ) { 543 bp_activity_update_meta( $aid, 'post_title', $post->post_title ); 544 } 541 545 } 542 }543 546 544 unset( $activities, $activity_ids, $comment_ids, $comments ); 547 unset( $activities, $activity_ids, $comment_ids, $comments ); 548 } 545 549 } 546 550 } 547 551 … … function bp_blogs_update_post_activity_meta( $post, $activity ) { 552 556 bp_activity_delete_meta( $activity->id, 'post_comment_status' ); 553 557 } 554 558 } 555 add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 2);559 add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 3 ); 556 560 557 561 /** 558 * Record a new blog comment in the BuddyPress activity stream.562 * Update Activity and blogs meta and eventually sync comment with activity comment 559 563 * 560 * Only posts the item if blog is public and post is not password-protected.564 * @since 2.5.0 561 565 * 562 * @param int $comment_id ID of the comment being recorded. 563 * @param bool|string $is_approved Optional. The $is_approved value passed to 564 * the 'comment_post' action. Default: true. 565 * @return bool|object Returns false on failure, the comment object on success. 566 * @param int|bool $activity_id ID of recorded activity, or false if sync is active. 567 * @param WP_Comment $comment The comment object. 568 * @param array $activity_args Array of activity arguments. 569 * @param object $activity_post_object The post type tracking args object. 570 * @return int|bool Returns false if no activity, the activity id otherwise. 566 571 */ 567 function bp_blogs_record_comment( $comment_id, $is_approved = true ) { 568 // Bail if activity component is not active. 569 if ( ! bp_is_active( 'activity' ) ) { 570 return; 571 } 572 573 // Get the users comment. 574 $recorded_comment = get_comment( $comment_id ); 575 576 // Don't record activity if the comment hasn't been approved. 577 if ( empty( $is_approved ) ) 572 function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) { 573 if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) { 578 574 return false; 575 } 579 576 580 // Don't record activity if no email address has been included. 581 if ( empty( $recorded_comment->comment_author_email ) ) 582 return false; 583 584 // Don't record activity if the comment has already been marked as spam. 585 if ( 'spam' === $is_approved ) 586 return false; 587 588 // Get the user by the comment author email. 589 $user = get_user_by( 'email', $recorded_comment->comment_author_email ); 590 591 // If user isn't registered, don't record activity. 592 if ( empty( $user ) ) 593 return false; 594 595 // Get the user_id. 596 $user_id = (int) $user->ID; 597 598 // Get blog and post data. 577 // Set the current blog id. 599 578 $blog_id = get_current_blog_id(); 600 579 601 // If blog is not trackable, do not record the activity. 602 if ( ! bp_blogs_is_blog_trackable( $blog_id, $user_id ) ) 603 return false; 604 605 $recorded_comment->post = get_post( $recorded_comment->comment_post_ID ); 606 607 if ( empty( $recorded_comment->post ) || is_wp_error( $recorded_comment->post ) ) 608 return false; 609 610 // If this is a password protected post, don't record the comment. 611 if ( !empty( $recorded_comment->post->post_password ) ) 612 return false; 613 614 // Don't record activity if the comment's associated post isn't a WordPress Post. 615 if ( !in_array( $recorded_comment->post->post_type, apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) ) ) ) 616 return false; 617 618 $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) ); 619 620 // If blog is public allow activity to be posted. 621 if ( $is_blog_public ) { 622 623 // Get activity related links. 624 $post_permalink = get_permalink( $recorded_comment->comment_post_ID ); 625 $comment_link = get_comment_link( $recorded_comment->comment_ID ); 626 627 // Setup activity args. 628 $args = array(); 629 630 $args['user_id'] = $user_id; 631 $args['content'] = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content', array( $recorded_comment->comment_content, &$recorded_comment, $comment_link ) ); 632 $args['primary_link'] = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$recorded_comment ) ); 633 $args['recorded_time'] = $recorded_comment->comment_date_gmt; 634 635 // Setup some different activity args depending if activity commenting is 636 // enabled or not. 637 // if cannot comment, record separate activity entry 638 // this is the old way of doing things. 639 if ( bp_disable_blogforum_comments() ) { 640 $args['type'] = 'new_blog_comment'; 641 $args['item_id'] = $blog_id; 642 $args['secondary_item_id'] = $comment_id; 643 644 // Record the activity entry. 645 $activity_id = bp_blogs_record_activity( $args ); 580 // These activity metadatas are used to build the new_blog_comment action string 581 if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) { 582 // add some post info in activity meta 583 bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 584 bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 585 } 646 586 647 // Add some post info in activity meta. 648 bp_activity_update_meta( $activity_id, 'post_title', $recorded_comment->post->post_title ); 649 bp_activity_update_meta( $activity_id, 'post_url', add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) ); 587 // Sync comment - activity comment 588 if ( ! bp_disable_blogforum_comments() ) { 650 589 651 // Record comment as BP activity comment under the parent 'new_blog_post' 652 // activity item. 653 } else { 654 // This is a comment edit 655 // check to see if corresponding activity entry already exists. 656 if ( ! empty( $_REQUEST['action'] ) ) { 657 $existing_activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true ); 590 if ( ! empty( $_REQUEST['action'] ) ) { 591 $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ); 658 592 659 if ( ! empty( $existing_activity_id ) ) { 660 $args['id'] = $existing_activity_id; 661 } 593 if ( ! empty( $existing_activity_id ) ) { 594 $activity_args['id'] = $existing_activity_id; 662 595 } 596 } 597 598 if ( empty( $activity_post_object ) ) { 599 $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type ); 600 } 663 601 664 // Find the parent 'new_blog_post' activity entry. 602 if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) { 603 // find the parent 'new_post_type' activity entry 665 604 $parent_activity_id = bp_activity_get_activity_id( array( 666 'component' => 'blogs',667 'type' => 'new_blog_post',605 'component' => $activity_post_object->component_id, 606 'type' => $activity_post_object->action_id, 668 607 'item_id' => $blog_id, 669 'secondary_item_id' => $ recorded_comment->comment_post_ID608 'secondary_item_id' => $comment->comment_post_ID 670 609 ) ); 671 610 672 611 // Try to create a new activity item for the parent blog post. 673 612 if ( empty( $parent_activity_id ) ) { 674 $parent_activity_id = bp_activity_post_type_publish( $ recorded_comment->comment_post_ID, $recorded_comment->post );613 $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post ); 675 614 } 615 } 676 616 677 // We found the parent activity entry678 // so let's go ahead and reconfigure some activity args.679 680 // Set the 'item_id' with the parent activity entry ID.681 $args['item_id'] = $parent_activity_id;617 // we found the parent activity entry 618 // so let's go ahead and reconfigure some activity args 619 if ( ! empty( $parent_activity_id ) ) { 620 // set the parent activity entry ID 621 $activity_args['activity_id'] = $parent_activity_id; 682 622 683 // Now see if the WP parent comment has a BP activity ID.684 685 if ( ! empty( $recorded_comment->comment_parent ) ) {686 $comment_parent = get_comment_meta( $recorded_comment->comment_parent, 'bp_activity_comment_id', true );687 623 // now see if the WP parent comment has a BP activity ID 624 $comment_parent = 0; 625 if ( ! empty( $comment->comment_parent ) ) { 626 $comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true ); 627 } 688 628 689 690 // so set to 'new_blog_post' activity ID.691 692 693 629 // WP parent comment does not have a BP activity ID 630 // so set to 'new_' . post_type activity ID 631 if ( empty( $comment_parent ) ) { 632 $comment_parent = $parent_activity_id; 633 } 694 634 695 $args['secondary_item_id'] = $comment_parent; 696 $args['component'] = 'activity'; 697 $args['type'] = 'activity_comment'; 635 $activity_args['parent_id'] = $comment_parent; 636 $activity_args['skip_notification'] = true; 698 637 699 // Could not find corresponding parent activity entry700 // so wipe out $args array.701 702 $args = array();703 638 // could not find corresponding parent activity entry 639 // so wipe out $args array 640 } else { 641 $activity_args = array(); 642 } 704 643 705 // Record in activity streams. 706 if ( ! empty( $args ) ) { 707 // @todo should we use bp_activity_new_comment()? that function will also send 708 // an email to people in the activity comment thread. 709 // 710 // What if a site already has some comment email notification plugin setup? 711 // this is why I decided to go with bp_activity_add() to avoid any conflict 712 // with existing comment email notification plugins. 713 $comment_activity_id = bp_activity_add( $args ); 714 715 if ( empty( $args['id'] ) ) { 716 // Add meta to activity comment. 717 bp_activity_update_meta( $comment_activity_id, 'bp_blogs_post_comment_id', $comment_id ); 718 bp_activity_update_meta( $comment_activity_id, 'post_title', $recorded_comment->post->post_title ); 719 bp_activity_update_meta( $comment_activity_id, 'post_url', add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) ); 720 721 // Add meta to comment. 722 add_comment_meta( $comment_id, 'bp_activity_comment_id', $comment_activity_id ); 644 // Record in activity streams 645 if ( ! empty( $activity_args ) ) { 646 $activity_id = bp_activity_new_comment( $activity_args ); 647 648 if ( empty( $activity_args['id'] ) ) { 649 // The activity metadata to inform about the corresponding comment ID 650 bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID ); 651 652 // The comment metadata to inform about the corresponding activity ID 653 add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id ); 654 655 // These activity metadatas are used to build the new_blog_comment action string 656 if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 657 bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 658 bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 723 659 } 724 660 } 725 661 } 662 } 726 663 727 // Update the blogs last active date. 728 bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() ); 664 // Update the blogs last active date 665 bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() ); 666 667 if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 668 /** 669 * Fires after BuddyPress has recorded metadata about a published blog post comment. 670 * 671 * @since 2.5.0 672 * 673 * @param int $value Comment ID of the blog post comment being recorded. 674 * @param WP_Post $post WP_Comment object for the current blog post. 675 * @param string $value ID of the user associated with the current blog post comment. 676 */ 677 do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() ); 729 678 } 730 679 731 return $ recorded_comment;680 return $activity_id; 732 681 } 733 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 ); 734 add_action( 'edit_comment', 'bp_blogs_record_comment', 10 ); 682 add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 ); 735 683 736 684 /** 737 685 * Record a user's association with a blog. … … function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) { 984 932 add_action( 'delete_post', 'bp_blogs_remove_post' ); 985 933 986 934 /** 987 * Remove a blog comment activity itemfrom the activity stream.935 * Remove a synced activity comment from the activity stream. 988 936 * 989 * @param int $comment_id ID of the comment to be removed. 937 * @since 2.5.0 938 * 939 * @param bool $deleted True when a comment post type activity was successfully removed. 940 * @param int $comment_id ID of the comment to be removed. 941 * @param object $activity_post_object The post type tracking args object. 942 * @param string $activity_type The post type comment activity type. 943 * 944 * @return bool True on success. False on error. 990 945 */ 991 function bp_blogs_remove_comment( $comment_id ) { 992 global $wpdb; 993 994 // Activity comments are disabled for blog posts 995 // which means that individual activity items exist for blog comments. 996 if ( bp_disable_blogforum_comments() ) { 997 // Delete the individual activity stream item. 998 bp_blogs_delete_activity( array( 999 'item_id' => $wpdb->blogid, 1000 'secondary_item_id' => $comment_id, 1001 'type' => 'new_blog_comment' 1002 ) ); 1003 1004 // Activity comments are enabled for blog posts 1005 // remove the associated activity item. 1006 } else { 1007 // Get associated activity ID from comment meta. 946 function bp_blogs_post_type_remove_comment( $deleted, $comment_id, $activity_post_object, $activity_type = '' ) { 947 // Remove synced activity comments, if needed. 948 if ( ! bp_disable_blogforum_comments() ) { 949 // Get associated activity ID from comment meta 1008 950 $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true ); 1009 951 1010 // Delete the associated activity comment. 1011 // 1012 // Also removes child post comments and associated activity comments. 1013 if ( ! empty( $activity_id ) && bp_is_active( 'activity' ) ) { 1014 // Fetch the activity comments for the activity item. 952 /** 953 * Delete the associated activity comment & also remove 954 * child post comments and associated activity comments. 955 */ 956 if ( ! empty( $activity_id ) ) { 957 // fetch the activity comments for the activity item 1015 958 $activity = bp_activity_get( array( 1016 959 'in' => $activity_id, 1017 960 'display_comments' => 'stream', 1018 961 'spam' => 'all', 1019 962 ) ); 1020 963 1021 // Get all activity comment IDs for the pending deleted item.964 // get all activity comment IDs for the pending deleted item 1022 965 if ( ! empty( $activity['activities'] ) ) { 1023 966 $activity_ids = bp_activity_recurse_comments_activity_ids( $activity ); 1024 967 $activity_ids[] = $activity_id; 1025 968 1026 // Delete activity items.969 // delete activity items 1027 970 foreach ( $activity_ids as $activity_id ) { 1028 971 bp_activity_delete( array( 1029 972 'id' => $activity_id 1030 973 ) ); 1031 974 } 1032 975 1033 // Remove associated blog comments.976 // remove associated blog comments 1034 977 bp_blogs_remove_associated_blog_comments( $activity_ids ); 1035 978 1036 // Rebuild activity comment tree.979 // rebuild activity comment tree 1037 980 BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id ); 981 982 // Set the result 983 $deleted = true; 1038 984 } 1039 985 } 1040 986 } 1041 987 1042 /** 1043 * Fires after a blog comment activity item was removed from activity stream. 1044 * 1045 * @since 1.0.0 1046 * 1047 * @param int $blogid Item ID for the blog associated with the removed comment. 1048 * @param int $comment_id ID of the comment being removed. 1049 * @param int $value ID of the current logged in user. 1050 */ 1051 do_action( 'bp_blogs_remove_comment', $wpdb->blogid, $comment_id, bp_loggedin_user_id() ); 988 // Backcompat for comments about the 'post' post type. 989 if ( 'new_blog_comment' === $activity_type ) { 990 /** 991 * Fires after a blog comment activity item was removed from activity stream. 992 * 993 * @since 1.0.0 994 * 995 * @param int $value ID for the blog associated with the removed comment. 996 * @param int $comment_id ID of the comment being removed. 997 * @param int $value ID of the current logged in user. 998 */ 999 do_action( 'bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id() ); 1000 } 1001 1002 return $deleted; 1052 1003 } 1053 add_action( ' delete_comment', 'bp_blogs_remove_comment');1004 add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 ); 1054 1005 1055 1006 /** 1056 1007 * Removes blog comments that are associated with activity comments. 1057 1008 * 1058 1009 * @since 2.0.0 1059 1010 * 1060 * @see bp_blogs_remove_ comment()1011 * @see bp_blogs_remove_synced_comment() 1061 1012 * @see bp_blogs_sync_delete_from_activity_comment() 1062 1013 * 1063 1014 * @param array $activity_ids The activity IDs to check association with blog … … function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $for 1094 1045 } 1095 1046 1096 1047 /** 1097 * When a blog comment status transition occurs, update the relevant activity's status.1098 *1099 * @since 1.6.01100 *1101 * @param string $new_status New comment status.1102 * @param string $old_status Previous comment status.1103 * @param object $comment Comment data.1104 */1105 function bp_blogs_transition_activity_status( $new_status, $old_status, $comment ) {1106 1107 // Check the Activity component is active.1108 if ( ! bp_is_active( 'activity' ) )1109 return;1110 1111 /**1112 * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.1113 *1114 * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.1115 * If a blog comment transitions to trashed, or spammed, mark the activity as spam.1116 * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.1117 * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.1118 * Otherwise, record the comment into the activity stream.1119 */1120 1121 // This clause was moved in from bp_blogs_remove_comment() in BuddyPress 1.6. It handles delete/hold.1122 if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {1123 return bp_blogs_remove_comment( $comment->comment_ID );1124 1125 // These clauses handle trash, spam, and un-spams.1126 } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {1127 $action = 'spam_activity';1128 } elseif ( 'approved' == $new_status ) {1129 $action = 'ham_activity';1130 }1131 1132 // Get the activity.1133 if ( bp_disable_blogforum_comments() ) {1134 $activity_id = bp_activity_get_activity_id( array(1135 'component' => buddypress()->blogs->id,1136 'item_id' => get_current_blog_id(),1137 'secondary_item_id' => $comment->comment_ID,1138 'type' => 'new_blog_comment'1139 ) );1140 } else {1141 $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );1142 }1143 1144 // Check activity item exists.1145 if ( empty( $activity_id ) ) {1146 // If no activity exists, but the comment has been approved, record it into the activity table.1147 if ( 'approved' == $new_status ) {1148 return bp_blogs_record_comment( $comment->comment_ID, true );1149 }1150 1151 return;1152 }1153 1154 // Create an activity object.1155 $activity = new BP_Activity_Activity( $activity_id );1156 if ( empty( $activity->component ) )1157 return;1158 1159 // Spam/ham the activity if it's not already in that state.1160 if ( 'spam_activity' == $action && ! $activity->is_spam ) {1161 bp_activity_mark_as_spam( $activity );1162 } elseif ( 'ham_activity' == $action) {1163 bp_activity_mark_as_ham( $activity );1164 }1165 1166 // Add "new_blog_comment" to the whitelisted activity types, so that the activity's Akismet history is generated.1167 $comment_akismet_history = create_function( '$t', '$t[] = "new_blog_comment"; return $t;' );1168 add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );1169 1170 // Save the updated activity.1171 $activity->save();1172 1173 // Remove the "new_blog_comment" activity type whitelist so we don't break anything.1174 remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );1175 }1176 add_action( 'transition_comment_status', 'bp_blogs_transition_activity_status', 10, 3 );1177 1178 /**1179 1048 * Get the total number of blogs being tracked by BuddyPress. 1180 1049 * 1181 1050 * @return int $count Total blog count. -
src/bp-blogs/bp-blogs-loader.php
diff --git src/bp-blogs/bp-blogs-loader.php src/bp-blogs/bp-blogs-loader.php index 67d84db..274bc5f 100644
class BP_Blogs_Component extends BP_Component { 106 106 add_post_type_support( $post_type, 'buddypress-activity' ); 107 107 } 108 108 } 109 110 // Filter the generic track parameters for the 'post' post type.111 add_filter( 'bp_activity_get_post_type_tracking_args', array( $this, 'post_tracking_args' ), 10, 2 );112 109 } 113 110 114 111 /** … … class BP_Blogs_Component extends BP_Component { 128 125 'classes', 129 126 'template', 130 127 'filters', 131 'activity',132 128 'functions', 133 129 ); 134 130 131 if ( bp_is_active( 'activity' ) ) { 132 $includes[] = 'activity'; 133 } 134 135 135 if ( is_multisite() ) { 136 136 $includes[] = 'widgets'; 137 137 } … … class BP_Blogs_Component extends BP_Component { 300 300 301 301 parent::setup_cache_groups(); 302 302 } 303 304 /**305 * Set up the tracking arguments for the 'post' post type.306 *307 * @since 2.2.0308 *309 * @see bp_activity_get_post_type_tracking_args() for information on parameters.310 *311 * @param object|null $params Tracking arguments.312 * @param string|int $post_type Post type to track.313 * @return object314 */315 public function post_tracking_args( $params = null, $post_type = 0 ) {316 317 /**318 * Filters the post types to track for the Blogs component.319 *320 * @since 1.5.0321 * @deprecated 2.3.0322 *323 * Make sure plugins still using 'bp_blogs_record_post_post_types'324 * to track their post types will generate new_blog_post activities325 * See https://buddypress.trac.wordpress.org/ticket/6306326 *327 * @param array $value Array of post types to track.328 */329 $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );330 $post_types_array = array_flip( $post_types );331 332 if ( ! isset( $post_types_array[ $post_type ] ) ) {333 return $params;334 }335 336 // Set specific params for the 'post' post type.337 $params->component_id = $this->id;338 $params->action_id = 'new_blog_post';339 $params->admin_filter = __( 'New post published', 'buddypress' );340 $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post';341 $params->front_filter = __( 'Posts', 'buddypress' );342 $params->contexts = array( 'activity', 'member' );343 $params->position = 5;344 345 return $params;346 }347 303 } 348 304 349 305 /** -
src/bp-core/deprecated/1.6.php
diff --git src/bp-core/deprecated/1.6.php src/bp-core/deprecated/1.6.php index 626d31b..c5550b6 100644
function bp_core_is_user_spammer( $user_id = 0 ) { 84 84 85 85 /** 86 86 * @deprecated 1.6.0 87 * @deprecated No longer used; see bp_ blogs_transition_activity_status()87 * @deprecated No longer used; see bp_activity_transition_post_type_comment_status() 88 88 */ 89 89 function bp_blogs_manage_comment( $comment_id, $comment_status ) { 90 90 _deprecated_function( __FUNCTION__, '1.6', 'No longer used' ); -
src/bp-core/deprecated/2.5.php
diff --git src/bp-core/deprecated/2.5.php src/bp-core/deprecated/2.5.php index f441def..c3246df 100644
function bp_core_deprecated_email_actions( $email, $delivery_status ) { 883 883 } 884 884 } 885 885 add_action( 'bp_sent_email', 'bp_core_deprecated_email_actions', 20, 2 ); 886 887 /** 888 * When a blog comment status transition occurs, update the relevant activity's status. 889 * 890 * @since 1.6.0 891 * @deprecated 2.5.0 892 * 893 * @param string $new_status New comment status. 894 * @param string $old_status Previous comment status. 895 * @param object $comment Comment data. 896 */ 897 function bp_blogs_transition_activity_status( $new_status, $old_status, $comment ) { 898 _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_transition_post_type_comment_status()' ); 899 bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ); 900 } 901 902 /** 903 * Record a new blog comment in the BuddyPress activity stream. 904 * 905 * Only posts the item if blog is public and post is not password-protected. 906 * 907 * @deprecated 2.5.0 908 * 909 * @param int $comment_id ID of the comment being recorded. 910 * @param bool|string $is_approved Optional. The $is_approved value passed to 911 * the 'comment_post' action. Default: true. 912 * @return bool|object Returns false on failure, the comment object on success. 913 */ 914 function bp_blogs_record_comment( $comment_id, $is_approved = true ) { 915 _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_comment()' ); 916 bp_activity_post_type_comment( $comment_id, $is_approved ); 917 } 918 919 /** 920 * Remove a blog comment activity item from the activity stream. 921 * 922 * @deprecated 2.5.0 923 * 924 * @param int $comment_id ID of the comment to be removed. 925 */ 926 function bp_blogs_remove_comment( $comment_id ) { 927 _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_remove_comment()' ); 928 bp_activity_post_type_remove_comment( $comment_id ); 929 } 930 -
tests/phpunit/includes/testcase.php
diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php index a75009a..1e8bcb4 100644
class BP_UnitTestCase extends WP_UnitTestCase { 49 49 50 50 // Clean up after autocommits. 51 51 add_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) ); 52 53 // Make sure Activity actions are reset before each test 54 $this->reset_bp_activity_actions(); 55 56 // Make sure all Post types activities globals are reset before each test 57 $this->reset_bp_activity_post_types_globals(); 52 58 } 53 59 54 60 public function tearDown() { … … class BP_UnitTestCase extends WP_UnitTestCase { 97 103 parent::clean_up_global_scope(); 98 104 } 99 105 106 protected function reset_bp_activity_actions() { 107 buddypress()->activity->actions = new stdClass(); 108 109 /** 110 * Populate the global with default activity actions only 111 * before each test. 112 */ 113 do_action( 'bp_register_activity_actions' ); 114 } 115 116 protected function reset_bp_activity_post_types_globals() { 117 global $wp_post_types; 118 119 // Remove all remaining tracking arguments to each post type 120 foreach ( $wp_post_types as $post_type => $post_type_arg ) { 121 if ( post_type_supports( $post_type, 'buddypress-activity' ) ) { 122 remove_post_type_support( $post_type, 'buddypress-activity' ); 123 } 124 125 if ( isset( $post_type_arg->bp_activity ) ) { 126 unset( $post_type_arg->bp_activity ); 127 } 128 } 129 130 buddypress()->activity->track = array(); 131 } 132 100 133 function assertPreConditions() { 101 134 parent::assertPreConditions(); 102 135 -
tests/phpunit/testcases/activity/actions.php
diff --git tests/phpunit/testcases/activity/actions.php tests/phpunit/testcases/activity/actions.php index d1ae6df..5549607 100644
class BP_Tests_Activity_Actions extends BP_UnitTestCase { 9 9 * @group activity_tracking 10 10 */ 11 11 public function test_bp_activity_catch_transition_post_type_status_publish() { 12 $bp = buddypress();13 14 12 register_post_type( 'foo', array( 15 13 'label' => 'foo', 16 14 'public' => true, … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 28 26 $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' ); 29 27 30 28 _unregister_post_type( 'foo' ); 31 32 // Reset globals33 unset( $bp->activity->actions->activity->new_foo );34 $bp->activity->track = array();35 29 } 36 30 37 31 /** … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 39 33 * @group activity_tracking 40 34 */ 41 35 public function test_bp_activity_catch_transition_post_type_status_publish_to_publish() { 42 $bp = buddypress();43 44 36 register_post_type( 'foo', array( 45 37 'label' => 'foo', 46 38 'public' => true, … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 68 60 $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Updating a post type should not create a new activity' ); 69 61 70 62 _unregister_post_type( 'foo' ); 71 72 // Reset globals73 unset( $bp->activity->actions->activity->new_foo );74 $bp->activity->track = array();75 63 } 76 64 77 65 /** … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 79 67 * @group activity_tracking 80 68 */ 81 69 public function test_bp_activity_catch_transition_post_type_status_publish_password() { 82 $bp = buddypress();83 84 70 register_post_type( 'foo', array( 85 71 'label' => 'foo', 86 72 'public' => true, … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 106 92 $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Password protected post type should not have activity' ); 107 93 108 94 _unregister_post_type( 'foo' ); 109 110 // Reset globals111 unset( $bp->activity->actions->activity->new_foo );112 $bp->activity->track = array();113 95 } 114 96 115 97 /** … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 117 99 * @group activity_tracking 118 100 */ 119 101 public function test_bp_activity_catch_transition_post_type_status_publish_trash() { 120 $bp = buddypress();121 122 102 register_post_type( 'foo', array( 123 103 'label' => 'foo', 124 104 'public' => true, … … class BP_Tests_Activity_Actions extends BP_UnitTestCase { 141 121 $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Unpublished post type should not have activity' ); 142 122 143 123 _unregister_post_type( 'foo' ); 144 145 // Reset globals146 unset( $bp->activity->actions->activity->new_foo );147 $bp->activity->track = array();148 124 } 149 125 150 126 protected function activity_exists_for_post( $post_id, $action ) { -
tests/phpunit/testcases/activity/functions.php
diff --git tests/phpunit/testcases/activity/functions.php tests/phpunit/testcases/activity/functions.php index 8a3304f..273d926 100644
Bar!'; 763 763 return; 764 764 } 765 765 766 $bp = buddypress();767 768 766 register_post_type( 'foo', array( 769 767 'label' => 'foo', 770 768 'public' => true, … … Bar!'; 800 798 $this->assertSame( $expected, $a_obj->action ); 801 799 802 800 _unregister_post_type( 'foo' ); 803 804 // Reset globals805 unset( $bp->activity->actions->activity->new_foo );806 $bp->activity->track = array();807 801 } 808 802 809 803 /** … … Bar!'; 816 810 return; 817 811 } 818 812 819 $bp = buddypress();820 821 813 $b = $this->factory->blog->create(); 822 814 $u = $this->factory->user->create(); 823 815 … … Bar!'; 863 855 $expected = sprintf( '%s wrote a new %s, on the site %s', $user_link, $post_link, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' ); 864 856 865 857 $this->assertSame( $expected, $a_obj->action ); 866 867 // Reset globals868 unset( $bp->activity->actions->activity->new_foo );869 $bp->activity->track = array();870 858 } 871 859 872 860 /** … … Bar!'; 874 862 * @group bp_activity_get_actions 875 863 */ 876 864 public function test_bp_activity_get_actions_should_sort_by_position() { 877 $old_actions = bp_activity_get_actions();878 865 buddypress()->activity->actions = new stdClass; 879 866 880 867 register_post_type( 'foo5', array( … … Bar!'; 916 903 ); 917 904 $foo_actions = (array) $actions->foo; 918 905 $this->assertEquals( $expected, array_values( wp_list_pluck( $foo_actions, 'key' ) ) ); 919 920 buddypress()->activity->actions = $old_actions;921 906 } 922 907 923 908 /** … … Bar!'; 929 914 return; 930 915 } 931 916 932 $bp = buddypress();933 934 917 $labels = array( 935 918 'name' => 'bars', 936 919 'singular_name' => 'bar', … … Bar!'; 974 957 $this->assertSame( $expected, $a_obj->action ); 975 958 976 959 _unregister_post_type( 'foo' ); 977 978 // Reset globals979 unset( $bp->activity->actions->activity->foo_bar );980 $bp->activity->track = array();981 960 } 982 961 983 962 /** … … Bar!'; 990 969 return; 991 970 } 992 971 993 $bp = buddypress();994 $reset = $bp->activity->actions;995 996 972 $b = $this->factory->blog->create(); 997 973 $u = $this->factory->user->create(); 998 974 … … Bar!'; 1041 1017 $expected = sprintf( '%1$s shared a new <a href="%2$s">bar</a>, on the site %3$s', $user_link, $post_url, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' ); 1042 1018 1043 1019 $this->assertSame( $expected, $a_obj->action ); 1044 1045 // Reset globals1046 unset( $bp->activity->actions->activity->new_foo );1047 $bp->activity->track = array();1048 1020 } 1049 1021 1050 1022 /** … … Bar!'; 1081 1053 $this->assertSame( $bp->blogs->id, $a['activities'][0]->component ); 1082 1054 1083 1055 remove_post_type_support( 'page', 'buddypress-activity' ); 1084 1085 // Reset globals1086 unset( $bp->activity->actions->blogs->new_page );1087 $bp->activity->track = array();1088 1056 } 1089 1057 1090 1058 /** … … Bar!'; 1126 1094 } 1127 1095 1128 1096 /** 1097 * @group activity_action 1098 * @group bp_activity_format_activity_action_custom_post_type_post_ms 1099 * @group post_type_comment_activities 1100 */ 1101 public function test_bp_activity_format_activity_action_custom_post_type_comment() { 1102 if ( is_multisite() ) { 1103 $b = $this->factory->blog->create(); 1104 switch_to_blog( $b ); 1105 add_filter( 'comment_flood_filter', '__return_false' ); 1106 } else { 1107 $b = get_current_blog_id(); 1108 } 1109 1110 $u = $this->factory->user->create(); 1111 $userdata = get_userdata( $u ); 1112 1113 $labels = array( 1114 'name' => 'bars', 1115 'singular_name' => 'bar', 1116 'bp_activity_new_comment' => __( '%1$s commented on the <a href="%2$s">bar</a>', 'buddypress' ), 1117 'bp_activity_new_comment_ms' => __( '%1$s commented on the <a href="%2$s">bar</a>, on the site %3$s', 'buddypress' ), 1118 ); 1119 1120 register_post_type( 'foo', array( 1121 'labels' => $labels, 1122 'public' => true, 1123 'supports' => array( 'buddypress-activity', 'comments' ), 1124 'bp_activity' => array( 1125 'action_id' => 'new_bar', 1126 'comment_action_id' => 'new_bar_comment', 1127 ), 1128 ) ); 1129 1130 // Build the actions to fetch the tracking args 1131 bp_activity_get_actions(); 1132 1133 $p = $this->factory->post->create( array( 1134 'post_author' => $u, 1135 'post_type' => 'foo', 1136 ) ); 1137 1138 $c = wp_new_comment( array( 1139 'comment_post_ID' => $p, 1140 'comment_author' => $userdata->user_nicename, 1141 'comment_author_url' => 'http://buddypress.org', 1142 'comment_author_email' => $userdata->user_email, 1143 'comment_content' => 'this is a blog comment', 1144 'comment_type' => '', 1145 'comment_parent' => 0, 1146 'user_id' => $u, 1147 ) ); 1148 1149 $a = bp_activity_get_activity_id( array( 'type' => 'new_bar_comment' ) ); 1150 1151 $a_obj = new BP_Activity_Activity( $a ); 1152 1153 $user_link = bp_core_get_userlink( $u ); 1154 $comment_url = get_comment_link( $c ); 1155 1156 _unregister_post_type( 'foo' ); 1157 1158 if ( is_multisite() ) { 1159 $blog_url = get_blog_option( $a_obj->item_id, 'home' ); 1160 restore_current_blog(); 1161 remove_filter( 'comment_flood_filter', '__return_false' ); 1162 1163 $expected = sprintf( $labels['bp_activity_new_comment_ms'], $user_link, $comment_url, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' ); 1164 } else { 1165 $expected = sprintf( $labels['bp_activity_new_comment'], $user_link, $comment_url ); 1166 } 1167 1168 $this->assertSame( $expected, $a_obj->action ); 1169 } 1170 1171 /** 1129 1172 * @group bp_activity_new_comment 1130 1173 * @group cache 1131 1174 */ -
tests/phpunit/testcases/activity/template.php
diff --git tests/phpunit/testcases/activity/template.php tests/phpunit/testcases/activity/template.php index db33cda..090701a 100644
class BP_Tests_Activity_Template extends BP_UnitTestCase { 909 909 /** 910 910 * @group filter_query 911 911 * @group BP_Activity_Query 912 * @group post_type_comment_activities 912 913 */ 913 914 function test_bp_has_activities_with_filter_query_compare_regex() { 914 915 $u1 = $this->factory->user->create(); … … class BP_Tests_Activity_Template extends BP_UnitTestCase { 1299 1300 1300 1301 /** 1301 1302 * @group bp_has_activities 1303 * @group post_type_comment_activities 1302 1304 */ 1303 1305 public function test_bp_has_activities_with_type_new_blog_comments() { 1304 1306 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); … … class BP_Tests_Activity_Template extends BP_UnitTestCase { 1430 1432 } 1431 1433 1432 1434 /** 1435 * @group bp_activity_can_comment 1436 */ 1437 public function test_bp_activity_can_comment() { 1438 global $activities_template; 1439 $reset_activities_template = $activities_template; 1440 1441 $activities_template = new stdClass; 1442 $activities_template->disable_blogforum_replies = true; 1443 $activities_template->activity = (object) array( 'type' => 'activity_comment' ); 1444 1445 $this->assertFalse( bp_activity_can_comment(), 'bp_activity_can_comment() should return false if the activity type is activity_comment' ); 1446 1447 $types = array( 1448 'new_blog_post', 1449 'new_blog_comment', 1450 'new_forum_topic', 1451 'new_forum_post' 1452 ); 1453 1454 foreach ( $types as $type_false ) { 1455 $activities_template->activity->type = $type_false; 1456 $this->assertFalse( bp_activity_can_comment(), 'Comments about blog or forum posts/replies are disabled' ); 1457 } 1458 1459 $activities_template->disable_blogforum_replies = false; 1460 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 1461 1462 foreach ( $types as $type_true ) { 1463 $activities_template->activity->type = $type_true; 1464 $this->assertTrue( bp_activity_can_comment(), 'Comments about blog or forum posts/replies are enabled' ); 1465 } 1466 1467 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 1468 1469 // clean up! 1470 $activities_template = $reset_activities_template; 1471 } 1472 1473 /** 1474 * @group bp_activity_can_comment 1475 */ 1476 public function test_bp_activity_can_comment_post_type_activity() { 1477 global $activities_template; 1478 $bp = buddypress(); 1479 1480 $reset_activities_template = $activities_template; 1481 $reset_activity_track = $bp->activity->track; 1482 1483 $activities_template = new stdClass; 1484 $activities_template->disable_blogforum_replies = true; 1485 1486 register_post_type( 'foo', array( 1487 'label' => 'foo', 1488 'public' => true, 1489 'supports' => array( 'buddypress-activity' ), 1490 ) ); 1491 1492 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 1493 1494 $activities_template->activity = (object) array( 'type' => 'new_foo' ); 1495 1496 $this->assertTrue( bp_activity_can_comment(), 'If post type does not support comments, a post type activity can be commented' ); 1497 1498 add_post_type_support( 'foo', 'comments' ); 1499 1500 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 1501 1502 $this->assertFalse( bp_activity_can_comment(), 'If post type support comments, a post type activity cannot be commented' ); 1503 1504 $bp_activity_support = (array) $bp->activity->track['new_foo']; 1505 $bp_activity_support['activity_comment'] = true; 1506 1507 bp_activity_set_post_type_tracking_args( 'foo', $bp_activity_support ); 1508 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 1509 1510 $this->assertTrue( bp_activity_can_comment(), 'If post type supports activity comments, a post type activity can be commented' ); 1511 1512 // clean up! 1513 $activities_template = $reset_activities_template; 1514 $bp->activity->track = $reset_activity_track; 1515 } 1516 1517 /** 1433 1518 * @group bp_activity_has_more_items 1434 1519 */ 1435 1520 public function test_bp_activity_has_more_items_no_count_total_false() { -
tests/phpunit/testcases/blogs/activity.php
diff --git tests/phpunit/testcases/blogs/activity.php tests/phpunit/testcases/blogs/activity.php index 08941e2..d7595ef 100644
class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 156 156 /** 157 157 * @group activity_action 158 158 * @group bp_blogs_format_activity_action_new_blog_comment 159 * @group post_type_comment_activities 159 160 */ 160 161 public function test_bp_blogs_format_activity_action_new_blog_comment_ms_nonrootblog() { 161 162 if ( ! is_multisite() ) { … … class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 318 319 return; 319 320 } 320 321 321 $bp = buddypress(); 322 $activity_actions = $bp->activity->actions; 323 $bp->activity->actions = new stdClass(); 322 buddypress()->activity->actions = new stdClass(); 324 323 325 324 $u = $this->factory->user->create(); 326 325 $p = wp_insert_post( array( … … class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 346 345 $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link ); 347 346 348 347 $this->assertSame( $expected, $a_obj['activities'][0]->action ); 349 350 // Reset activity actions351 $bp->activity->actions = $activity_actions;352 $bp->activity->track = array();353 348 } 354 349 355 350 /** … … class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 361 356 return; 362 357 } 363 358 364 $bp = buddypress(); 365 $activity_actions = $bp->activity->actions; 366 $bp->activity->actions = new stdClass(); 359 buddypress()->activity->actions = new stdClass(); 367 360 368 361 $u = $this->factory->user->create(); 369 362 $p = wp_insert_post( array( … … class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 407 400 ) ); 408 401 409 402 $this->assertSame( $expected, $a_obj['activities'][0]->action ); 403 } 404 405 /** 406 * @group bp_blogs_sync_add_from_activity_comment 407 * @group post_type_comment_activities 408 */ 409 public function test_bp_blogs_sync_add_from_activity_comment() { 410 $old_user = get_current_user_id(); 411 $u = $this->factory->user->create(); 412 $this->set_current_user( $u ); 413 $userdata = get_userdata( $u ); 414 415 // let's use activity comments instead of single "new_blog_comment" activity items 416 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 417 418 // create the blog post 419 $post_id = $this->factory->post->create( array( 420 'post_status' => 'publish', 421 'post_type' => 'post', 422 'post_title' => 'Test activity comment to post comment', 423 ) ); 424 425 // grab the activity ID for the activity comment 426 $a1 = bp_activity_get_activity_id( array( 427 'type' => 'new_blog_post', 428 'component' => buddypress()->blogs->id, 429 'filter' => array( 430 'item_id' => get_current_blog_id(), 431 'secondary_item_id' => $post_id 432 ), 433 ) ); 434 435 $a2 = bp_activity_new_comment( array( 436 'content' => 'this content shoud be in a new post comment', 437 'user_id' => $u, 438 'activity_id' => $a1, 439 ) ); 440 441 $approved_comments = get_approved_comments( $post_id ); 442 $comment = reset( $approved_comments ); 443 444 $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' ); 445 $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' ); 446 447 // reset 448 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 449 450 $this->set_current_user( $old_user ); 451 } 452 453 /** 454 * @group bp_blogs_sync_delete_from_activity_comment 455 * @group post_type_comment_activities 456 */ 457 public function test_bp_blogs_sync_delete_from_activity_comment() { 458 $old_user = get_current_user_id(); 459 $u = $this->factory->user->create(); 460 $this->set_current_user( $u ); 461 $userdata = get_userdata( $u ); 462 463 // let's use activity comments instead of single "new_blog_comment" activity items 464 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 465 466 // create the blog post 467 $post_id = $this->factory->post->create( array( 468 'post_status' => 'publish', 469 'post_type' => 'post', 470 'post_title' => 'Test activity comment to post comment', 471 ) ); 472 473 // grab the activity ID for the activity comment 474 $a1 = bp_activity_get_activity_id( array( 475 'type' => 'new_blog_post', 476 'component' => buddypress()->blogs->id, 477 'filter' => array( 478 'item_id' => get_current_blog_id(), 479 'secondary_item_id' => $post_id 480 ), 481 ) ); 482 483 $a2 = bp_activity_new_comment( array( 484 'content' => 'the generated comment should be deleted once the activity comment is removed', 485 'user_id' => $u, 486 'activity_id' => $a1, 487 ) ); 488 489 bp_activity_delete_comment( $a1, $a2 ); 490 491 $post_comments = get_comments( array( 'post_id' => $post_id ) ); 492 493 $this->assertEmpty( $post_comments, 'A post comment should be deleted when the corresponding activity is' ); 494 495 // reset 496 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 497 498 $this->set_current_user( $old_user ); 499 } 500 501 /** 502 * @group bp_blogs_sync_activity_edit_to_post_comment 503 * @group post_type_comment_activities 504 */ 505 public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_unspam_activity_comment() { 506 $old_user = get_current_user_id(); 507 $u = $this->factory->user->create(); 508 $this->set_current_user( $u ); 509 $userdata = get_userdata( $u ); 510 511 // let's use activity comments instead of single "new_blog_comment" activity items 512 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 513 514 // create the blog post 515 $post_id = $this->factory->post->create( array( 516 'post_status' => 'publish', 517 'post_type' => 'post', 518 'post_title' => 'Test activity comment to post comment', 519 ) ); 520 521 // grab the activity ID for the activity comment 522 $a1 = bp_activity_get_activity_id( array( 523 'type' => 'new_blog_post', 524 'component' => buddypress()->blogs->id, 525 'filter' => array( 526 'item_id' => get_current_blog_id(), 527 'secondary_item_id' => $post_id 528 ), 529 ) ); 530 531 $a2 = bp_activity_new_comment( array( 532 'content' => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed', 533 'user_id' => $u, 534 'activity_id' => $a1, 535 ) ); 536 537 $activity = new BP_Activity_Activity( $a2 ); 538 539 bp_activity_mark_as_spam( $activity ); 540 $activity->save(); 541 542 $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) ); 543 544 $this->assertEmpty( $post_comments, 'A post comment should be spammed when the corresponding activity is spammed' ); 545 546 bp_activity_mark_as_ham( $activity ); 547 $activity->save(); 548 549 $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) ); 550 $comment = reset( $post_comments ); 551 552 $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' ); 553 $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' ); 554 555 // reset 556 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 557 558 $this->set_current_user( $old_user ); 559 } 560 561 /** 562 * @group bp_blogs_sync_activity_edit_to_post_comment 563 * @group post_type_comment_activities 564 */ 565 public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_activity_comment_unspam_post_comment() { 566 $old_user = get_current_user_id(); 567 $u = $this->factory->user->create(); 568 $this->set_current_user( $u ); 569 $userdata = get_userdata( $u ); 570 571 // let's use activity comments instead of single "new_blog_comment" activity items 572 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 573 574 // create the blog post 575 $post_id = $this->factory->post->create( array( 576 'post_status' => 'publish', 577 'post_type' => 'post', 578 'post_title' => 'Test activity comment to post comment', 579 ) ); 580 581 // grab the activity ID for the activity comment 582 $a1 = bp_activity_get_activity_id( array( 583 'type' => 'new_blog_post', 584 'component' => buddypress()->blogs->id, 585 'filter' => array( 586 'item_id' => get_current_blog_id(), 587 'secondary_item_id' => $post_id 588 ), 589 ) ); 590 591 $a2 = bp_activity_new_comment( array( 592 'content' => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed', 593 'user_id' => $u, 594 'activity_id' => $a1, 595 ) ); 596 597 $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ); 598 599 $activity = new BP_Activity_Activity( $a2 ); 600 601 bp_activity_mark_as_spam( $activity ); 602 $activity->save(); 603 604 wp_unspam_comment( $c ); 605 606 $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) ); 607 $comment = reset( $post_comments ); 608 609 $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' ); 610 $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' ); 611 612 // reset 613 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 614 615 $this->set_current_user( $old_user ); 616 } 617 618 /** 619 * @group bp_blogs_sync_activity_edit_to_post_comment 620 * @group post_type_comment_activities 621 * @group imath 622 */ 623 public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() { 624 $old_user = get_current_user_id(); 625 $u = $this->factory->user->create(); 626 $this->set_current_user( $u ); 627 $userdata = get_userdata( $u ); 628 629 // let's use activity comments instead of single "new_blog_comment" activity items 630 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 631 632 // create the blog post 633 $post_id = $this->factory->post->create( array( 634 'post_status' => 'publish', 635 'post_type' => 'post', 636 'post_title' => 'Test activity comment to post comment', 637 ) ); 638 639 // grab the activity ID for the activity comment 640 $a1 = bp_activity_get_activity_id( array( 641 'type' => 'new_blog_post', 642 'component' => buddypress()->blogs->id, 643 'filter' => array( 644 'item_id' => get_current_blog_id(), 645 'secondary_item_id' => $post_id 646 ), 647 ) ); 648 649 $a2 = bp_activity_new_comment( array( 650 'content' => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed', 651 'user_id' => $u, 652 'activity_id' => $a1, 653 ) ); 654 655 $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ); 656 657 wp_trash_comment( $c ); 658 659 $activity = new BP_Activity_Activity( $a2 ); 660 661 bp_activity_mark_as_ham( $activity ); 662 $activity->save(); 663 664 $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) ); 665 $comment = reset( $post_comments ); 666 667 $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' ); 668 $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' ); 669 670 // reset 671 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 410 672 411 // Reset activity actions 412 $bp->activity->actions = $activity_actions; 413 $bp->activity->track = array(); 673 $this->set_current_user( $old_user ); 414 674 } 415 675 416 676 /** -
tests/phpunit/testcases/blogs/filters.php
diff --git tests/phpunit/testcases/blogs/filters.php tests/phpunit/testcases/blogs/filters.php index a0fc961..3d2af7f 100644
class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 10 10 public function setUp() { 11 11 parent::setUp(); 12 12 13 $bp = buddypress();14 15 $this->activity_actions = $bp->activity->actions;16 $bp->activity->actions = new stdClass();17 18 13 $this->custom_post_types = array( 'using_old_filter' ); 19 14 20 15 register_post_type( 'using_old_filter', array( … … class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 35 30 _unregister_post_type( 'using_old_filter' ); 36 31 remove_filter( 'bp_blogs_record_post_post_types', array( $this, 'filter_post_types'), 10, 1 ); 37 32 remove_filter( 'bp_blogs_record_comment_post_types', array( $this, 'filter_post_types'), 10, 1 ); 38 39 // Reset activity actions40 $bp->activity->actions = $this->activity_actions;41 $bp->activity->track = array();42 33 } 43 34 44 35 /** … … class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 64 55 65 56 /** 66 57 * @goup bp_blogs_record_comment 58 * @group post_type_comment_activities 67 59 */ 68 60 public function test_bp_blogs_record_comment() { 69 61 $u = $this->factory->user->create(); … … class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 89 81 90 82 /** 91 83 * @goup bp_blogs_record_comment_sync_activity_comment 84 * @group post_type_comment_activities 92 85 */ 93 86 public function test_bp_blogs_record_comment_sync_activity_comment() { 94 87 $u = $this->factory->user->create(); -
tests/phpunit/testcases/blogs/functions.php
diff --git tests/phpunit/testcases/blogs/functions.php tests/phpunit/testcases/blogs/functions.php index a058db2..d7fad14 100644
class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 513 513 514 514 /** 515 515 * @group bp_blogs_catch_transition_post_status 516 * @group post_type_comment_activities 516 517 */ 517 518 public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() { 518 519 // save the current user and override logged-in user … … class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 596 597 597 598 /** 598 599 * @group bp_blogs_transition_activity_status 599 * @group bp_blogs_remove_comment 600 * @group bp_blogs_post_type_remove_comment 601 * @group post_type_comment_activities 600 602 */ 601 603 public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment() { 602 604 // save the current user and override logged-in user … … class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 658 660 } 659 661 660 662 /** 663 * @group bp_blogs_post_type_remove_comment 664 * @group post_type_comment_activities 665 */ 666 public function test_bp_blogs_post_type_remove_comment() { 667 $old_user = get_current_user_id(); 668 $u = $this->factory->user->create(); 669 $this->set_current_user( $u ); 670 $userdata = get_userdata( $u ); 671 672 // create the blog post 673 $p = $this->factory->post->create( array( 674 'post_status' => 'publish', 675 'post_type' => 'post', 676 'post_title' => 'First title', 677 ) ); 678 679 $c = wp_new_comment( array( 680 'comment_post_ID' => $p, 681 'comment_author' => $userdata->user_nicename, 682 'comment_author_url' => 'http://buddypress.org', 683 'comment_author_email' => $userdata->user_email, 684 'comment_content' => 'this comment will be removed', 685 'comment_type' => '', 686 'comment_parent' => 0, 687 'user_id' => $u, 688 ) ); 689 690 // An activity should exist 691 $a = bp_activity_get_activity_id( array( 692 'user_id' => $u, 693 'type' => 'new_blog_comment' 694 ) ); 695 696 // now permanently delete the comment 697 wp_delete_comment( $c, true ); 698 699 // The activity comment should no longer exist 700 $ac = bp_activity_get( array( 'in' => $a ) ); 701 $this->assertTrue( empty( $ac['activities'] ) ); 702 } 703 704 /** 661 705 * @group bp_blogs_catch_transition_post_status 662 706 */ 663 707 public function test_bp_blogs_is_blog_trackable_false_publish_post() { … … class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 714 758 /** 715 759 * @group bp_blogs_record_comment 716 760 * @group unique 761 * @group post_type_comment_activities 717 762 */ 718 763 public function test_bp_blogs_record_comment_no_duplicate_activity_comments() { 719 764 // save the current user and override logged-in user … … class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 777 822 778 823 /** 779 824 * @group bp_blogs_record_comment 825 * @group post_type_comment_activities 780 826 */ 781 827 public function test_bp_blogs_record_comment_should_record_parent_blog_post_activity_if_not_found() { 782 828 // Save the current user and override logged-in user … … class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 826 872 ), 827 873 ) ); 828 874 875 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 876 829 877 // Assert that activity item for blog post was created after adding a comment 830 878 $this->assertNotNull( $a1, 'Activity item was not created for existing blog post when recording post comment.' ); 831 879 832 880 $this->set_current_user( $old_user ); 833 881 } 834 882 883 /** 884 * @group bp_blogs_comment_sync_activity_comment 885 * @group post_type_comment_activities 886 */ 887 public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() { 888 if ( is_multisite() ) { 889 $b = $this->factory->blog->create(); 890 switch_to_blog( $b ); 891 add_filter( 'comment_flood_filter', '__return_false' ); 892 } else { 893 $b = get_current_blog_id(); 894 } 895 896 $u = $this->factory->user->create(); 897 $userdata = get_userdata( $u ); 898 899 $labels = array( 900 'name' => 'bars', 901 'singular_name' => 'bar', 902 ); 903 904 register_post_type( 'foo', array( 905 'labels' => $labels, 906 'public' => true, 907 'supports' => array( 'comments' ), 908 ) ); 909 910 add_post_type_support( 'foo', 'buddypress-activity' ); 911 912 bp_activity_set_post_type_tracking_args( 'foo', array( 913 'comment_action_id' => 'new_foo_comment', 914 ) ); 915 916 add_filter( 'bp_disable_blogforum_comments', '__return_false' ); 917 918 $p = $this->factory->post->create( array( 919 'post_author' => $u, 920 'post_type' => 'foo', 921 ) ); 922 923 $a1 = bp_activity_get_activity_id( array( 924 'type' => 'new_foo', 925 'filter' => array( 926 'item_id' => $b, 927 'secondary_item_id' => $p 928 ), 929 ) ); 930 931 $c = wp_new_comment( array( 932 'comment_post_ID' => $p, 933 'comment_author' => $userdata->user_nicename, 934 'comment_author_url' => 'http://buddypress.org', 935 'comment_author_email' => $userdata->user_email, 936 'comment_content' => 'this is a foo comment', 937 'comment_type' => '', 938 'comment_parent' => 0, 939 'user_id' => $u, 940 ) ); 941 942 $a2 = bp_activity_new_comment( array( 943 'content' => 'this should generate a new foo comment', 944 'user_id' => $u, 945 'activity_id' => $a1, 946 ) ); 947 948 $activity_args = array( 949 'type' => 'activity_comment', 950 'display_comments' => 'stream', 951 'meta_query' => array( array( 952 'key' => 'bp_blogs_foo_comment_id', 953 'compare' => 'exists', 954 ) ) 955 ); 956 957 $a = bp_activity_get( $activity_args ); 958 $aids = wp_list_pluck( $a['activities'], 'id' ); 959 $cids = wp_list_pluck( get_approved_comments( $p ), 'comment_ID' ); 960 961 foreach ( $aids as $aid ) { 962 $this->assertTrue( in_array( bp_activity_get_meta( $aid, 'bp_blogs_foo_comment_id' ), $cids ), 'The comment ID should be in the activity meta' ); 963 } 964 965 foreach ( $cids as $cid ) { 966 $this->assertTrue( in_array( get_comment_meta( $cid, 'bp_activity_comment_id', true ), $aids ), 'The activity ID should be in the comment meta' ); 967 } 968 969 _unregister_post_type( 'foo' ); 970 971 if ( is_multisite() ) { 972 restore_current_blog(); 973 remove_filter( 'comment_flood_filter', '__return_false' ); 974 } 975 976 remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); 977 } 978 835 979 public function count_activity_comment_saved() { 836 980 $this->activity_saved_comment_count += 1; 837 981 }