Changeset 8125 for trunk/bp-activity/bp-activity-functions.php
- Timestamp:
- 03/13/2014 06:34:06 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-activity/bp-activity-functions.php
r8076 r8125 257 257 258 258 /** 259 * Set the current action for a given activity stream location. 260 * 261 * @since BuddyPress (1.1) 262 * 263 * @global object $bp BuddyPress global settings 264 * @uses apply_filters() To call the 'bp_activity_set_action' hook 259 * Register an activity 'type' and its action description/callback. 260 * 261 * Activity actions are strings used to describe items in the activity stream, 262 * such as 'Joe became a registered member' or 'Bill and Susie are now 263 * friends'. Each activity type (such as 'new_member' or 'friendship_created') 264 * used by a component should be registered using this function. 265 * 266 * While it's possible to post items to the activity stream whose types are 267 * not registered using bp_activity_set_action(), it is not recommended; 268 * unregistered types will not be displayed properly in the activity admin 269 * panel, and dynamic action generation (which is essential for multilingual 270 * sites, etc) will not work. 271 * 272 * @since BuddyPress (1.1.0) 265 273 * 266 274 * @param string $component_id The unique string ID of the component. 267 * @param string $key The action key. 268 * @param string $value The action value. 275 * @param string $type The action type. 276 * @param string $description The action description. 277 * @param callable $format_callback Callback for formatting the action string. 269 278 * @return bool False if any param is empty, otherwise true. 270 279 */ 271 function bp_activity_set_action( $component_id, $ key, $value ) {272 global $bp;280 function bp_activity_set_action( $component_id, $type, $description, $format_callback = false ) { 281 $bp = buddypress(); 273 282 274 283 // Return false if any of the above values are not set 275 if ( empty( $component_id ) || empty( $key ) || empty( $value ) ) 276 return false; 284 if ( empty( $component_id ) || empty( $type ) || empty( $description ) ) { 285 return false; 286 } 277 287 278 288 // Set activity action 279 if ( ! isset( $bp->activity->actions ) || !is_object( $bp->activity->actions ) ) {289 if ( ! isset( $bp->activity->actions ) || ! is_object( $bp->activity->actions ) ) { 280 290 $bp->activity->actions = new stdClass; 281 291 } 282 292 283 if ( !isset( $bp->activity->actions->{$component_id} ) || !is_object( $bp->activity->actions->{$component_id} ) ) { 293 // Verify callback 294 if ( ! is_callable( $format_callback ) ) { 295 $format_callback = ''; 296 } 297 298 if ( ! isset( $bp->activity->actions->{$component_id} ) || ! is_object( $bp->activity->actions->{$component_id} ) ) { 284 299 $bp->activity->actions->{$component_id} = new stdClass; 285 300 } 286 301 287 $bp->activity->actions->{$component_id}->{$key} = apply_filters( 'bp_activity_set_action', array( 288 'key' => $key, 289 'value' => $value 290 ), $component_id, $key, $value ); 302 $bp->activity->actions->{$component_id}->{$type} = apply_filters( 'bp_activity_set_action', array( 303 'key' => $type, 304 'value' => $description, 305 'format_callback' => $format_callback, 306 ), $component_id, $type, $description, $format_callback ); 291 307 292 308 return true; … … 858 874 global $bp; 859 875 860 bp_activity_set_action( $bp->activity->id, 'activity_update', __( 'Posted a status update', 'buddypress' ) ); 861 bp_activity_set_action( $bp->activity->id, 'activity_comment', __( 'Replied to a status update', 'buddypress' ) ); 876 bp_activity_set_action( 877 $bp->activity->id, 878 'activity_update', 879 __( 'Posted a status update', 'buddypress' ), 880 'bp_activity_format_activity_action_activity_update' 881 ); 882 883 bp_activity_set_action( 884 $bp->activity->id, 885 'activity_comment', 886 __( 'Replied to a status update', 'buddypress' ), 887 'bp_activity_format_activity_action_activity_comment' 888 ); 862 889 863 890 do_action( 'bp_activity_register_activity_actions' ); … … 867 894 } 868 895 add_action( 'bp_register_activity_actions', 'bp_activity_register_activity_actions' ); 896 897 /** 898 * Generate an activity action string for an activity item. 899 * 900 * @param object $activity Activity data object. 901 * @return string|bool Returns false if no callback is found, otherwise returns 902 * the formatted action string. 903 */ 904 function bp_activity_generate_action_string( $activity ) { 905 // Check for valid input 906 if ( empty( $activity->component ) || empty( $activity->type ) ) { 907 return false; 908 } 909 910 // Check for registered format callback 911 if ( empty( buddypress()->activity->actions->{$activity->component}->{$activity->type}['format_callback'] ) ) { 912 return false; 913 } 914 915 return call_user_func( buddypress()->activity->actions->{$activity->component}->{$activity->type}['format_callback'], $activity ); 916 } 917 918 /** 919 * Format 'activity_update' activity actions. 920 * 921 * @since BuddyPress (2.0.0) 922 * 923 * @param object $activity Activity data object. 924 * @return string 925 */ 926 function bp_activity_format_activity_action_activity_update( $activity ) { 927 $action = sprintf( __( '%s posted an update', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) ); 928 return apply_filters( 'bp_activity_new_update_action', $action, $activity ); 929 } 930 931 /** 932 * Format 'activity_comment' activity actions. 933 * 934 * @since BuddyPress (2.0.0) 935 * 936 * @param object $activity Activity data object. 937 * @return string 938 */ 939 function bp_activity_format_activity_action_activity_comment( $activity ) { 940 $action = sprintf( __( '%s posted a new activity comment', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) ); 941 return apply_filters( 'bp_activity_comment_action', $action, $activity ); 942 } 869 943 870 944 /****************************************************************************** … … 1032 1106 * false to create a new item. Default: false. 1033 1107 * @type string $action Optional. The activity action/description, typically 1034 * something like "Joe posted an update". 1108 * something like "Joe posted an update". Values passed to this param 1109 * will be stored in the database and used as a fallback for when the 1110 * activity item's format_callback cannot be found (eg, when the 1111 * component is disabled). As long as you have registered a 1112 * format_callback for your $type, it is unnecessary to include this 1113 * argument - BP will generate it automatically. 1114 * See {@link bp_activity_set_action()}. 1035 1115 * @type string $content Optional. The content of the activity item. 1036 1116 * @type string $component The unique name of the component associated with … … 1088 1168 $activity->component = $component; 1089 1169 $activity->type = $type; 1090 $activity->action = $action;1091 1170 $activity->content = $content; 1092 1171 $activity->primary_link = $primary_link; … … 1096 1175 $activity->hide_sitewide = $hide_sitewide; 1097 1176 $activity->is_spam = $is_spam; 1177 $activity->action = ! empty( $action ) ? $action : bp_activity_generate_action_string( $activity ); 1098 1178 1099 1179 if ( !$activity->save() ) … … 1151 1231 // Record this on the user's profile 1152 1232 $from_user_link = bp_core_get_userlink( $user_id ); 1153 $activity_action = sprintf( __( '%s posted an update', 'buddypress' ), $from_user_link );1154 1233 $activity_content = $content; 1155 1234 $primary_link = bp_core_get_userlink( $user_id, false, true ); … … 1158 1237 $activity_id = bp_activity_add( array( 1159 1238 'user_id' => $user_id, 1160 'action' => apply_filters( 'bp_activity_new_update_action', $activity_action ),1161 1239 'content' => apply_filters( 'bp_activity_new_update_content', $activity_content ), 1162 1240 'primary_link' => apply_filters( 'bp_activity_new_update_primary_link', $primary_link ), 1163 1241 'component' => $bp->activity->id, 1164 'type' => 'activity_update' 1242 'type' => 'activity_update', 1165 1243 ) ); 1166 1244 … … 1231 1309 $comment_id = bp_activity_add( array( 1232 1310 'id' => $id, 1233 'action' => apply_filters( 'bp_activity_comment_action', sprintf( __( '%s posted a new activity comment', 'buddypress' ), bp_core_get_userlink( $user_id ) ) ),1234 1311 'content' => apply_filters( 'bp_activity_comment_content', $content ), 1235 1312 'component' => buddypress()->activity->id,
Note: See TracChangeset
for help on using the changeset viewer.