Skip to:
Content

BuddyPress.org

Ticket #5669: 5669.patch

File 5669.patch, 19.1 KB (added by imath, 11 years ago)
  • src/bp-activity/bp-activity-classes.php

    diff --git src/bp-activity/bp-activity-classes.php src/bp-activity/bp-activity-classes.php
    index f1e6054..543a58d 100644
    class BP_Activity_Feed { 
    16801680</rss><?php
    16811681        }
    16821682}
     1683
     1684/**
     1685 * Activity Post Types tracking class
     1686 *
     1687 * @since BuddyPress (2.1.0)
     1688 */
     1689class BP_Activity_Tracking {
     1690
     1691        /** Properties ************************************************************/
     1692
     1693        /**
     1694         * post types to track.
     1695         *
     1696         * @var array
     1697         */
     1698        public $post_types = array();
     1699
     1700        /**
     1701         * Start.
     1702         */
     1703        public static function start() {
     1704                $bp = buddypress();
     1705
     1706                if ( empty( $bp->activity->tracker ) ) {
     1707                        $bp->activity->tracker = new self;
     1708                }
     1709
     1710                return $bp->activity->tracker;
     1711        }
     1712
     1713        /**
     1714         * Constructor method.
     1715         */
     1716        public function __construct() {
     1717                $this->setup_hooks();
     1718        }
     1719
     1720        /**
     1721         * Hooks
     1722         */
     1723        private function setup_hooks() {
     1724                // Register activity actions
     1725                add_action( 'bp_register_activity_actions', array( $this, 'register_activity_actions' ) );
     1726
     1727                // Manage tracking
     1728                add_action( 'transition_post_status', array( $this, 'track' ),  10, 3 );
     1729                add_action( 'delete_post',            array( $this, 'delete' ), 10, 1 );
     1730        }
     1731
     1732        /**
     1733         * Define activity actions.
     1734         */
     1735        public function register_activity_actions() {
     1736                $bp = buddypress();
     1737
     1738                $active_components = array_keys( $bp->active_components );
     1739
     1740                foreach ( $active_components as $component ) {
     1741                        if ( ! empty( $bp->{$component}->tracking ) ) {
     1742                                $post_types = array_keys( $bp->{$component}->tracking );
     1743                               
     1744                                foreach( $post_types as $post_type ) {
     1745
     1746                                        $activity_args = $bp->{$component}->tracking[ $post_type ];
     1747
     1748                                        if ( empty( $activity_args->track ) || empty( $activity_args->type ) || empty( $activity_args->description ) ) {
     1749                                                continue;
     1750                                        }
     1751
     1752                                        // for later use
     1753                                        $this->post_types[ $post_type ] = $component;
     1754
     1755                                        if ( empty( $activity_args->label ) ) {
     1756                                                $activity_args->label = $activity_args->description;
     1757                                        }
     1758
     1759                                        bp_activity_set_action(
     1760                                                $component,
     1761                                                $activity_args->type,
     1762                                                $activity_args->description,
     1763                                                $activity_args->format_callback,
     1764                                                $activity_args->label,
     1765                                                $activity_args->context
     1766                                        );
     1767
     1768                                        do_action( "bp_activity_tracking_{$component}_{$post_type}", $activity_args );
     1769                                }
     1770                        }
     1771                }
     1772
     1773                $this->post_types = apply_filters( 'bp_activity_tracking_post_types', $this->post_types );
     1774        }
     1775
     1776        /**
     1777         * Track post types
     1778         */
     1779        public function track( $new_status = '', $old_status = '', $post = null ) {
     1780                $bp = buddypress();
     1781                $tracked = array_keys( $this->post_types );
     1782
     1783                if ( empty( $post->post_type ) || ! in_array( $post->post_type, $tracked ) ) {
     1784                        return;
     1785                }
     1786
     1787                $component = $this->post_types[ $post->post_type ];
     1788
     1789                if ( empty( $component ) || empty( $bp->{$component}->tracking[ $post->post_type ] ) ) {
     1790                        return;
     1791                } else {
     1792                        $tracking_args = apply_filters( "bp_activity_tracking_{$component}_args", $bp->{$component}->tracking[ $post->post_type ] );
     1793                }
     1794
     1795                if ( empty( $tracking_args->track ) ) {
     1796                        return;
     1797                }
     1798
     1799                // This is an edit
     1800                if ( $new_status === $old_status ) {
     1801                        if ( $new_status == 'publish' ) {
     1802
     1803                                if ( ! empty( $tracking_args->update_callback ) && is_callable( $tracking_args->update_callback ) ) {
     1804                                        call_user_func_array( $tracking_args->update_callback, array( 'post' => $post ) );
     1805                                }
     1806                                return;
     1807                        }
     1808                }
     1809
     1810                // Publishing a previously unpublished post
     1811                if ( 'publish' === $new_status ) {
     1812                        // Untrashing the post
     1813                        if ( 'trash' == $old_status ) {
     1814                                if ( ! empty( $tracking_args->untrash_callback ) && is_callable( $tracking_args->untrash_callback ) ) {
     1815                                        call_user_func_array( $tracking_args->untrash_callback, array( 'post_id' => $post->ID, 'post' => $post ) );
     1816                                }
     1817                                return;
     1818                        }
     1819
     1820                        if ( ! empty( $tracking_args->publish_callback ) && is_callable( $tracking_args->publish_callback ) ) {
     1821                                call_user_func_array( $tracking_args->publish_callback, array( 'post_id' => $post->ID, 'post' => $post ) );
     1822                        } else {
     1823                                // Define default activity args
     1824                                $activity_args = array(
     1825                                        'action'            => '',
     1826                                        'content'           => bp_create_excerpt( $post->post_content ),
     1827                                        'component'         => $component,
     1828                                        'type'              => 'new_item',
     1829                                        'user_id'           => $post->post_author,
     1830                                        'item_id'           => get_current_blog_id(),
     1831                                        'secondary_item_id' => $post->ID,
     1832                                );
     1833
     1834                                if ( ! empty( $tracking_args->type ) ) {
     1835                                        $activity_args['type'] = $tracking_args->type;
     1836                                }
     1837                               
     1838                                // Default action if format callback is not set
     1839                                if ( empty( $tracking_args->format_callback ) ) {
     1840                                        $blog_id = $activity_args['item_id'];
     1841
     1842                                        // Record this in activity streams
     1843                                        $post_permalink = add_query_arg(
     1844                                                'p',
     1845                                                $post_id,
     1846                                                trailingslashit( get_home_url( $blog_id ) )
     1847                                        );
     1848
     1849                                        if ( is_multisite() ){
     1850                                                $activity_args['action']  = sprintf( __( '%1$s wrote a new item, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
     1851                                        } else {
     1852                                                $activity_args['action']  = sprintf( __( '%1$s wrote a new item, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
     1853                                        }
     1854                                }
     1855                               
     1856                                // Add the activity
     1857                                bp_activity_add( $activity_args );
     1858                        }
     1859
     1860                // Unpublishing a previously published post
     1861                } else if ( 'publish' === $old_status ) {
     1862                        if ( ! empty( $tracking_args->unpublish_callback ) && is_callable( $tracking_args->unpublish_callback ) ) {
     1863                                call_user_func_array( $tracking_args->unpublish_callback, array( 'post' => $post ) );
     1864                        } else {
     1865                                // Define default activity args
     1866                                $activity_args = array(
     1867                                        'component'         => $component,
     1868                                        'type'              => 'new_item',
     1869                                        'user_id'           => $post->post_author,
     1870                                        'item_id'           => get_current_blog_id(),
     1871                                        'secondary_item_id' => $post->ID,
     1872                                );
     1873
     1874                                if ( ! empty( $tracking_args->type ) ) {
     1875                                        $activity_args['type'] = $tracking_args->type;
     1876                                }
     1877
     1878                                // Delete the activity
     1879                                bp_activity_delete( $activity_args );
     1880                        }
     1881                }
     1882        }
     1883
     1884        /**
     1885         * Remove the activity when the tracked post type is deleted
     1886         */
     1887        public function delete( $post_id = 0 ) {
     1888                $bp = buddypress();
     1889                $tracked = array_keys( $this->post_types );
     1890
     1891                if ( empty( $post_id ) ) {
     1892                        return;
     1893                }
     1894
     1895                if ( empty( $tracked ) ) {
     1896                        return;
     1897                }
     1898
     1899                $post_type = get_post_type( $post_id );
     1900
     1901                if ( ! in_array( $post_type, $tracked ) ) {
     1902                        return;
     1903                }
     1904
     1905                $component = $this->post_types[ $post_type ];
     1906
     1907                if ( empty( $component ) || empty( $bp->{$component}->tracking[ $post_type ] ) ) {
     1908                        return;
     1909                } else {
     1910                        $tracking_args = apply_filters( "bp_activity_tracking_{$component}_args", $bp->{$component}->tracking[ $post_type ] );
     1911                }
     1912
     1913                if ( empty( $tracking_args->track ) ) {
     1914                        return;
     1915                }
     1916
     1917                if ( ! empty( $tracking_args->remove_callback ) && is_callable( $tracking_args->remove_callback ) ) {
     1918                        call_user_func_array( $tracking_args->remove_callback, array( 'post_id' => $post_id ) );
     1919                } else {
     1920                        $activity_args = array(
     1921                                'type'              => 'new_item',
     1922                                'component'         => $component,
     1923                                'item_id'           => get_current_blog_id(),
     1924                                'secondary_item_id' => $post_id,
     1925                        );
     1926
     1927                        if ( ! empty( $tracking_args->type ) ) {
     1928                                $activity_args['type'] = $tracking_args->type;
     1929                        }
     1930
     1931                        bp_activity_delete( $activity_args );
     1932                }
     1933        }
     1934}
     1935add_action( 'bp_init', array( 'BP_Activity_Tracking', 'start' ), 7 );
  • src/bp-blogs/bp-blogs-activity.php

    diff --git src/bp-blogs/bp-blogs-activity.php src/bp-blogs/bp-blogs-activity.php
    index 19f567b..aae2bd4 100644
     
    1111if ( !defined( 'ABSPATH' ) ) exit;
    1212
    1313/**
    14  * Register activity actions for the blogs component.
     14 * Register new blog activity action for the blogs component.
    1515 *
    1616 * @since BuddyPress (1.0.0)
    1717 *
    if ( !defined( 'ABSPATH' ) ) exit; 
    1919 *
    2020 * @return bool|null Returns false if activity component is not active.
    2121 */
    22 function bp_blogs_register_activity_actions() {
     22function bp_blogs_register_activity_newblog_action() {
    2323        global $bp;
    2424
    2525        // Bail if activity is not active
    function bp_blogs_register_activity_actions() { 
    3636                        __( 'New Sites', 'buddypress' )
    3737                );
    3838        }
     39}
     40add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_newblog_action' );
    3941
     42/**
     43 * Register activity actions for the blogs component.
     44 *
     45 * @since BuddyPress (2.1.0)
     46 *
     47 * @uses bp_activity_set_action()
     48 * @uses buddypress()
     49 *
     50 * @return bool|null Returns false if activity component is not active.
     51 */
     52function bp_blogs_register_activity_actions() {
    4053        bp_activity_set_action(
    41                 $bp->blogs->id,
    42                 'new_blog_post',
    43                 __( 'New post published', 'buddypress' ),
    44                 'bp_blogs_format_activity_action_new_blog_post',
    45                 __( 'Posts', 'buddypress' ),
    46                 array( 'activity', 'member' )
    47         );
    48 
    49         bp_activity_set_action(
    50                 $bp->blogs->id,
     54                buddypress()->blogs->id,
    5155                'new_blog_comment',
    5256                __( 'New post comment posted', 'buddypress' ),
    5357                'bp_blogs_format_activity_action_new_blog_comment',
    function bp_blogs_register_activity_actions() { 
    5761
    5862        do_action( 'bp_blogs_register_activity_actions' );
    5963}
    60 add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_actions' );
     64add_action( 'bp_activity_tracking_blogs_post', 'bp_blogs_register_activity_actions' );
    6165
    6266/**
    6367 * Format 'new_blog' activity actions.
  • src/bp-blogs/bp-blogs-filters.php

    diff --git src/bp-blogs/bp-blogs-filters.php src/bp-blogs/bp-blogs-filters.php
    index 07ad0e3..c3d8b69 100644
    function bp_blogs_creation_location( $url ) { 
    3636        return apply_filters( 'bp_blogs_creation_location', trailingslashit( bp_get_root_domain() . '/' . bp_get_blogs_root_slug() . '/create', $url ) );
    3737}
    3838add_filter( 'wp_signup_location', 'bp_blogs_creation_location' );
     39
     40/**
     41 * Ensure post types tracking is respecting the blog's tracking settings.
     42 *
     43 * @since BuddyPress (2.1.0)
     44 *
     45 * @param object $args The tracking args.
     46 * @return object $args The tracking args.
     47 */
     48function bp_blogs_filter_tracking_args( $args = null ) {
     49        if ( empty( $args->track ) )
     50                return $args;
     51
     52        $blog_id = get_current_blog_id();
     53        $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
     54
     55        if ( ( empty( $is_blog_public ) && is_multisite() ) || ! bp_blogs_is_blog_trackable( $blog_id ) ) {
     56                $args->track = false;
     57        }
     58
     59        return $args;
     60}
     61add_filter( 'bp_activity_tracking_blogs_args', 'bp_blogs_filter_tracking_args', 10, 1 );
  • src/bp-blogs/bp-blogs-functions.php

    diff --git src/bp-blogs/bp-blogs-functions.php src/bp-blogs/bp-blogs-functions.php
    index 384693a..b03fcd9 100644
    add_action( 'update_option_thread_comments_depth', 'bp_blogs_update_option_threa 
    359359 *
    360360 * @since BuddyPress (2.0.0)
    361361 *
    362  * @todo Support untrashing better
     362 * @todo deprecated
    363363 *
    364364 * @param string $new_status New status for the post.
    365365 * @param string $old_status Old status for the post.
    function bp_blogs_catch_transition_post_status( $new_status, $old_status, $post 
    396396                ) );
    397397        }
    398398}
    399 add_action( 'transition_post_status', 'bp_blogs_catch_transition_post_status', 10, 3 );
    400399
    401400/**
    402401 * Record a new blog post in the BuddyPress activity stream.
    function bp_blogs_record_post( $post_id, $post, $user_id = 0 ) { 
    413412        $post_id = (int) $post_id;
    414413        $blog_id = (int) $wpdb->blogid;
    415414
    416         // If blog is not trackable, do not record the activity.
    417         if ( ! bp_blogs_is_blog_trackable( $blog_id, $user_id ) )
    418                 return false;
    419 
    420415        if ( !$user_id )
    421416                $user_id = (int) $post->post_author;
    422417
    function bp_blogs_record_post( $post_id, $post, $user_id = 0 ) { 
    436431        if ( !in_array( $post->post_type, apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ) ) )
    437432                return false;
    438433
    439         $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
    440 
    441434        if ( 'publish' == $post->post_status && empty( $post->post_password ) ) {
    442                 if ( $is_blog_public || !is_multisite() ) {
    443 
    444                         // Record this in activity streams
    445                         $post_permalink = add_query_arg(
    446                                 'p',
    447                                 $post_id,
    448                                 trailingslashit( get_home_url( $blog_id ) )
    449                         );
    450 
    451                         if ( is_multisite() )
    452                                 $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
    453                         else
    454                                 $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
    455 
    456                         // Make sure there's not an existing entry for this post (prevent bumping)
    457                         if ( bp_is_active( 'activity' ) ) {
    458                                 $existing = bp_activity_get( array(
    459                                         'filter' => array(
    460                                                 'action'       => 'new_blog_post',
    461                                                 'primary_id'   => $blog_id,
    462                                                 'secondary_id' => $post_id,
    463                                         )
    464                                 ) );
    465 
    466                                 if ( !empty( $existing['activities'] ) ) {
    467                                         return;
    468                                 }
     435                // Record this in activity streams
     436                $post_permalink = add_query_arg(
     437                        'p',
     438                        $post_id,
     439                        trailingslashit( get_home_url( $blog_id ) )
     440                );
     441
     442                if ( is_multisite() )
     443                        $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
     444                else
     445                        $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
     446
     447                // Make sure there's not an existing entry for this post (prevent bumping)
     448                if ( bp_is_active( 'activity' ) ) {
     449                        $existing = bp_activity_get( array(
     450                                'filter' => array(
     451                                        'action'       => 'new_blog_post',
     452                                        'primary_id'   => $blog_id,
     453                                        'secondary_id' => $post_id,
     454                                )
     455                        ) );
     456
     457                        if ( !empty( $existing['activities'] ) ) {
     458                                return;
    469459                        }
     460                }
    470461
    471                         $activity_content = $post->post_content;
     462                $activity_content = $post->post_content;
    472463
    473                         bp_blogs_record_activity( array(
    474                                 'user_id'           => (int) $post->post_author,
    475                                 'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
    476                                 'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink,   $post_id               ),
    477                                 'type'              => 'new_blog_post',
    478                                 'item_id'           => $blog_id,
    479                                 'secondary_item_id' => $post_id,
    480                                 'recorded_time'     => $post->post_date_gmt,
    481                         ));
    482                 }
     464                bp_blogs_record_activity( array(
     465                        'user_id'           => (int) $post->post_author,
     466                        'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
     467                        'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink,   $post_id               ),
     468                        'type'              => 'new_blog_post',
     469                        'item_id'           => $blog_id,
     470                        'secondary_item_id' => $post_id,
     471                        'recorded_time'     => $post->post_date_gmt,
     472                ));
    483473
    484474                // Update the blogs last activity
    485475                bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
  • src/bp-blogs/bp-blogs-loader.php

    diff --git src/bp-blogs/bp-blogs-loader.php src/bp-blogs/bp-blogs-loader.php
    index 89cf2a1..44d3f73 100644
    class BP_Blogs_Component extends BP_Component { 
    241241
    242242                parent::setup_title();
    243243        }
     244        /**
     245         * Register the post types
     246         *
     247         * @since BuddyPress (2.1.0)
     248         */
     249        public function register_post_types( $post_types = array() ) {
     250                $post_types = apply_filters( 'bp_blogs_register_post_types', array(
     251                        'post' => array(
     252                                'buddypress' => array(
     253                                        'type'             => 'new_blog_post',
     254                                        'description'      => __( 'New post published', 'buddypress' ),
     255                                        'format_callback'  => 'bp_blogs_format_activity_action_new_blog_post',
     256                                        'label'            => __( 'Posts', 'buddypress' ),
     257                                        'context'          => array( 'activity', 'member' ),
     258                                        'track'            => true,
     259                                        'publish_callback' => 'bp_blogs_record_post',
     260                                        'update_callback'  => 'bp_blogs_update_post',
     261                                        'remove_callback'  => 'bp_blogs_remove_post'
     262                                )
     263                        )
     264                ) );
     265
     266                parent::register_post_types( $post_types );
     267        }
    244268}
    245269
    246270/**
  • src/bp-core/bp-core-actions.php

    diff --git src/bp-core/bp-core-actions.php src/bp-core/bp-core-actions.php
    index df61f5a..c2991c0 100644
    add_action( 'bp_loaded', 'bp_register_theme_directory', 14 ); 
    6464 * The load order helps to execute code at the correct time.
    6565 *                                                   v---Load order
    6666 */
     67add_action( 'bp_init', 'bp_register_post_types',     1  );
    6768add_action( 'bp_init', 'bp_core_set_uri_globals',    2  );
    6869add_action( 'bp_init', 'bp_setup_globals',           4  );
    6970add_action( 'bp_init', 'bp_setup_nav',               6  );
  • src/bp-core/bp-core-component.php

    diff --git src/bp-core/bp-core-component.php src/bp-core/bp-core-component.php
    index c16c17c..88bb814 100644
    class BP_Component { 
    532532         *
    533533         * @uses do_action() Calls 'bp_{@link bp_Component::name}_register_post_types'.
    534534         */
    535         public function register_post_types() {
     535        public function register_post_types( $post_types = array() ) {
     536
     537                if ( ! empty( $post_types ) ) {
     538                        $this->tracking = array();
     539
     540                        foreach ( $post_types as $post_type => $args ) {
     541
     542                                if ( ! empty( $args['buddypress'] ) ) {
     543                                        $activity_args = wp_parse_args( $args['buddypress'], array(
     544                                                'type'            => '',
     545                                                'description'     => '',
     546                                                'format_callback' => '',
     547                                                'label'           => '',
     548                                                'context'         => array(),
     549                                                'track'           => false
     550                                        ) );
     551                                        $this->tracking[ $post_type ] = (object) $activity_args;
     552                                        unset( $args['buddypress'] );
     553                                }
     554
     555                                if ( ! post_type_exists( $post_type ) && ! empty( $args ) && bp_is_root_blog() ) {
     556                                        register_post_type( $post_type, $args );
     557                                }
     558                        }
     559                }
     560
    536561                do_action( 'bp_' . $this->id . '_register_post_types' );
    537562        }
    538563
  • src/bp-core/bp-core-dependency.php

    diff --git src/bp-core/bp-core-dependency.php src/bp-core/bp-core-dependency.php
    index eaf839c..1228b07 100644
    function bp_setup_title() { 
    6161}
    6262
    6363/**
     64 * Fire the 'bp_setup_components' action, where plugins should initialize components.
     65 */
     66function bp_register_post_types() {
     67        do_action( 'bp_register_post_types' );
     68}
     69
     70/**
    6471 * Fire the 'bp_register_widgets' action, where plugins should register widgets.
    6572 */
    6673function bp_setup_widgets() {