深入学习BuddyPress之bp_activity和bp_activity_meta表

BuddyPress作为Wordpress的重要社交插件,提供了用户系统,动态,群组,通知,消息等社交软件的基础功能。其中动态功能属于社交软件中必不可少的组成部分。

在BuddyPress相关数据表中,bp_activity和bp_activity_meta两张表负责了动态功能的数据存储。BuddyPress和WordPress一样,都采用了主表(bp_activity-BuddyPress,posts-WordPress),辅助表(bp_activity_meta-BuddyPress,postmeta-WordPress)的结构,bp_activity存储动态内容的相关信息,wp_activity_meta存储与动态内容相关联的一些信息。

我们先来看看两张表的字段(这里我们只介绍主要的字段)。

bp_activity的字段有action,component,content,is_spam,item_id

  • action字段,其内容是某某用户“发布了更新”,或者“更改了他们的资料图片”,“发布了一条新的动态评论”.
  • component字段, 这个是存储的是动态是在什么模块下产生的,如果是在动态activity下发布,则存储component,如果是用户更改资料,则存储members.
  • content,存储动态内容。
  • is_spam字段,是否为垃圾内容,默认为0,当设定为垃圾动态时,为1
  • item_id字段,当动态有评论时,存储父动态的id.默认为0

bp_activity_meta的字段比较少,与postmeta表一样,activity_id,meta_key,meta_value。这里不做具体介绍,从字面意思就可以看出,meta_key存键,meta_value为键值。比如动态中的收藏功能的meta_key为favorite_count。

在BuddyPress中添加新动态的功能函数是bp_activity_add()

用法:

$activity_id = bp_activity_add( $args );

参数:

$args An array that describes the activity item that you’re creating. Possible values:

  • 'id'
    (optional) Pass a numerical id to update an existing activity item
  • 'action'
    An HTML string summarizing the activity item, which is used by the template when displaying the activity item. Usually contains links to related users, groups, etc. E.g., '<a href="http://example.com/members/bill">Bill</a> created the group <a href="http://example.com/groups/tmnt">TMNT</a>'
  • 'content'
    (optional) The string content of the activity item. In the case of a blog post, for example, you might use an excerpt from the post content. In some cases, it’s appropriate for activity items not to have any content for this field – e.g., when a user creates a new group.
  • 'component'
    A string denoting the unique “component” that this activity item is associated with. BuddyPress components are ‘groups’, ‘members’, etc. If you are writing a BuddyPress plugin, you might consider having a single component name for all activity in your plugin.
  • 'type'
    A string denoting the specific kind of activity being created. This should be more specific than 'component'. For example, when creating a group, BuddyPress posts an activity item with the component ‘groups’ and the type ‘created_group’. 'type' is used for filtering, as in the “New Groups”, “New Blog Posts”, etc dropdown on Activity directories.
  • 'primary_link'
    (optional) The primary URL associated with the activity item. BuddyPress uses this value when creating activity RSS feeds, to tell the feed reader where to find the original item. If you omit this value, the fallback value is the permalink page for the single activity item.
  • 'user_id'
    (optional) The unique numeric id of the user associated with the activity item. In BuddyPress, the primary use of this value is to filter which items appear on the Activity tab of an individual member’s profile. If the value is omitted, it will default to the id of the currently logged-in user. Note that you can pass 0 for items that may be associated with no user at all.
  • 'item_id'
    (optional) The numeric ID of the primary item associated with this activity item. For example, when recording the creation of a group, BuddyPress sets the 'item_id' to the numeric id of the newly created group. This value can be used for filtering.
  • 'secondary_item_id'
    (optional) A second numeric ID for further filtering. For example, when recording a new blog comment, BuddyPress sets the 'item_id' to the ID of the blog, and the 'secondary_item_id' to the ID of the comment.
  • 'recorded_time'
    (optional) The time the activity is recorded. Should be in GMT, MySQL format. (In PHP, date( 'Y-m-d H:i:s', $time ).) Defaults to the current time.
  • 'hide_sitewide'
    (optional) 'hide_sitewide' is used in a few different ways:
    • To prevent duplicate entries when viewing the sitewide activity stream. For instance, when a new friendship is established, two activity items are created: one with 'user_id' set to the user who sent the request and 'item_id' set to recipient, and the other one vice versa. We need both items so that both users see the item on their own profiles, but it’s not desirable to see both of them on the sitewide stream, so the second one is marked 'hide_sitewide' => true.
    • To prevent activity items from hidden or private groups from appearing in sitewide activity streams. That is, when an activity item is posted to a non-public group, 'hide_sitewide' is set to false. Then, when viewing the group’s activity stream (which is accessible only to members), BP includes 'hide_sitewide' items; in contrast, the sitewide stream excludes these items.
    Thus, 'hide_sitewide' is used simultaneously for preventing duplicates and for privacy. Please be sure you understand the way that 'hide_sitewide' works before attempting to use it for the purposes of privacy in your own plugins.
  • 'is_spam'
    (optional) Set to true to mark an item as spam.
返回值

New database row activity ID

Source File

bp_activity_add() is located in bp-activity/bp-activity-functions.php

发表回复