/** * Add an activity item. * * @since BuddyPress (1.1.0) * * @uses wp_parse_args() * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity} * @uses BP_Activity_Activity::rebuild_activity_comment_tree() {@link BP_Activity_Activity} * @uses wp_cache_delete() * @uses do_action() To call the 'bp_activity_add' hook * * @param array $args { * An array of arguments. * @type int|bool $id Pass an activity ID to update an existing item, or * false to create a new item. Default: false. * @type string $action Optional. The activity action/description, typically * something like "Joe posted an update". Values passed to this param * will be stored in the database and used as a fallback for when the * activity item's format_callback cannot be found (eg, when the * component is disabled). As long as you have registered a * format_callback for your $type, it is unnecessary to include this * argument - BP will generate it automatically. * See {@link bp_activity_set_action()}. * @type string $content Optional. The content of the activity item. * @type string $component The unique name of the component associated with * the activity item - 'groups', 'profile', etc. * @type string $type The specific activity type, used for directory * filtering. 'new_blog_post', 'activity_update', etc. * @type string $primary_link Optional. The URL for this item, as used in * RSS feeds. Defaults to the URL for this activity item's permalink page. * @type int|bool $user_id Optional. The ID of the user associated with the * activity item. May be set to false or 0 if the item is not related * to any user. Default: the ID of the currently logged-in user. * @type int $item_id Optional. The ID of the associated item. * @type int $secondary_item_id Optional. The ID of a secondary associated * item. * @type string $date_recorded Optional. The GMT time, in Y-m-d h:i:s format, * when the item was recorded. Defaults to the current time. * @type bool $hide_sitewide Should the item be hidden on sitewide streams? * Default: false. * @type bool $is_spam Should the item be marked as spam? Default: false. * } * @return int|bool The ID of the activity on success. False on error. */ function bp_activity_add($args = '') { $r = bp_parse_args($args, array('id' => false, 'action' => '', 'content' => '', 'component' => false, 'type' => false, 'primary_link' => '', 'user_id' => bp_loggedin_user_id(), 'item_id' => false, 'secondary_item_id' => false, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => false, 'is_spam' => false)); // Make sure we are backwards compatible if (empty($r['component']) && !empty($r['component_name'])) { $r['component'] = $r['component_name']; } if (empty($r['type']) && !empty($r['component_action'])) { $r['type'] = $r['component_action']; } // Setup activity to be added $activity = new BP_Activity_Activity($r['id']); $activity->user_id = $r['user_id']; $activity->component = $r['component']; $activity->type = $r['type']; $activity->content = $r['content']; $activity->primary_link = $r['primary_link']; $activity->item_id = $r['item_id']; $activity->secondary_item_id = $r['secondary_item_id']; $activity->date_recorded = $r['recorded_time']; $activity->hide_sitewide = $r['hide_sitewide']; $activity->is_spam = $r['is_spam']; $activity->action = !empty($r['action']) ? $r['action'] : bp_activity_generate_action_string($activity); if (!$activity->save()) { return false; } // If this is an activity comment, rebuild the tree if ('activity_comment' === $activity->type) { // Also clear the comment cache for the parent activity ID wp_cache_delete($activity->item_id, 'bp_activity_comments'); BP_Activity_Activity::rebuild_activity_comment_tree($activity->item_id); } wp_cache_delete('bp_activity_sitewide_front', 'bp'); do_action('bp_activity_add', $r); return $activity->id; }
/** * Generate action strings for the activities located in BP_Activity_Activity::get(). * * If no string can be dynamically generated for a given item * (typically because the activity type has not been properly * registered), the static 'action' value pulled from the database will * be left in place. * * @since 2.0.0 * * @param array $activities Array of activities. * @return array */ protected static function generate_action_strings($activities) { foreach ($activities as $key => $activity) { $generated_action = bp_activity_generate_action_string($activity); if (false !== $generated_action) { $activity->action = $generated_action; } $activities[$key] = $activity; } return $activities; }
/** * 3/ Gets the activity before it is saved and adjusts his arguments to match our need * * @package WP Idea Stream * @subpackage buddypress/activity * * @since 2.0.0 * * @param BP_Activity_Activity $activity the activity object before being saved * @uses apply_filters() call 'wp_idea_stream_buddypress_pre_adjust_activity' to early override the activity object * call 'wp_idea_stream_buddypress_adjust_activity' to override the activity object * @uses bp_activity_set_action() to define the post type activity action if not set * @uses bp_activity_generate_action_string() to generate the action strings using the 'action_callback' * of our activity actions * @return BP_Activity_Activity the activity to be saved */ public function adjust_activity_args($activity = null) { // Bail if not an idea if (empty($this->secondary_item_id) || $this->secondary_item_id != $activity->secondary_item_id) { return; } $bp = buddypress(); $bp_activity_actions = new stdClass(); $bp_activity_actions = $bp->activity->actions; /** * Used internally to override ideas posted within a group * * @param BP_Activity_Activity $activity the activity object * @param WP_Post $this->idea the idea object */ $activity = apply_filters('wp_idea_stream_buddypress_pre_adjust_activity', $activity, $this->idea); // Define the corresponding index $activity_type = $activity->type; // override the activity type if (!empty($this->activity_actions[$activity_type])) { $activity->type = $this->activity_actions[$activity_type]->type; } if (empty($bp->activity->actions->{$activity->component}->{$activity->type}['format_callback'])) { bp_activity_set_action($this->activity_actions[$activity_type]->component, $this->activity_actions[$activity_type]->type, $this->activity_actions[$activity_type]->admin_caption, $this->activity_actions[$activity_type]->action_callback); } // Regenerate the action string $activity->action = bp_activity_generate_action_string($activity); /** * Make sure the visibility status respect the idea post status * So far BuddyPress : * - is not generating an activity for private posts * - is generating an activity for comments made on a private post */ if ('publish' != $this->idea->post_status) { $activity->hide_sitewide = true; } /** * @param BP_Activity_Activity $activity the activity object */ $activity = apply_filters('wp_idea_stream_buddypress_adjust_activity', $activity); // Reset BuddyPress activity actions $bp->activity->actions = $bp_activity_actions; }
/** * Add an activity item. * * @since 1.1.0 * @since 2.6.0 Added 'error_type' parameter to $args. * * @param array|string $args { * An array of arguments. * @type int|bool $id Pass an activity ID to update an existing item, or * false to create a new item. Default: false. * @type string $action Optional. The activity action/description, typically * something like "Joe posted an update". Values passed to this param * will be stored in the database and used as a fallback for when the * activity item's format_callback cannot be found (eg, when the * component is disabled). As long as you have registered a * format_callback for your $type, it is unnecessary to include this * argument - BP will generate it automatically. * See {@link bp_activity_set_action()}. * @type string $content Optional. The content of the activity item. * @type string $component The unique name of the component associated with * the activity item - 'groups', 'profile', etc. * @type string $type The specific activity type, used for directory * filtering. 'new_blog_post', 'activity_update', etc. * @type string $primary_link Optional. The URL for this item, as used in * RSS feeds. Defaults to the URL for this activity * item's permalink page. * @type int|bool $user_id Optional. The ID of the user associated with the activity * item. May be set to false or 0 if the item is not related * to any user. Default: the ID of the currently logged-in user. * @type int $item_id Optional. The ID of the associated item. * @type int $secondary_item_id Optional. The ID of a secondary associated item. * @type string $date_recorded Optional. The GMT time, in Y-m-d h:i:s format, when * the item was recorded. Defaults to the current time. * @type bool $hide_sitewide Should the item be hidden on sitewide streams? * Default: false. * @type bool $is_spam Should the item be marked as spam? Default: false. * @type string $error_type Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'. * } * @return int|bool The ID of the activity on success. False on error. */ function bp_activity_add($args = '') { $r = bp_parse_args($args, array('id' => false, 'action' => '', 'content' => '', 'component' => false, 'type' => false, 'primary_link' => '', 'user_id' => bp_loggedin_user_id(), 'item_id' => false, 'secondary_item_id' => false, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => false, 'is_spam' => false, 'error_type' => 'bool'), 'activity_add'); // Make sure we are backwards compatible. if (empty($r['component']) && !empty($r['component_name'])) { $r['component'] = $r['component_name']; } if (empty($r['type']) && !empty($r['component_action'])) { $r['type'] = $r['component_action']; } // Setup activity to be added. $activity = new BP_Activity_Activity($r['id']); $activity->user_id = $r['user_id']; $activity->component = $r['component']; $activity->type = $r['type']; $activity->content = $r['content']; $activity->primary_link = $r['primary_link']; $activity->item_id = $r['item_id']; $activity->secondary_item_id = $r['secondary_item_id']; $activity->date_recorded = $r['recorded_time']; $activity->hide_sitewide = $r['hide_sitewide']; $activity->is_spam = $r['is_spam']; $activity->error_type = $r['error_type']; $activity->action = !empty($r['action']) ? $r['action'] : bp_activity_generate_action_string($activity); $save = $activity->save(); if ('wp_error' === $r['error_type'] && is_wp_error($save)) { return $save; } elseif ('bool' === $r['error_type'] && false === $save) { return false; } // If this is an activity comment, rebuild the tree. if ('activity_comment' === $activity->type) { // Also clear the comment cache for the parent activity ID. wp_cache_delete($activity->item_id, 'bp_activity_comments'); BP_Activity_Activity::rebuild_activity_comment_tree($activity->item_id); } wp_cache_delete('bp_activity_sitewide_front', 'bp'); /** * Fires at the end of the execution of adding a new activity item, before returning the new activity item ID. * * @since 1.1.0 * * @param array $r Array of parsed arguments for the activity item being added. */ do_action('bp_activity_add', $r); return $activity->id; }
/** * Add an activity item. * * @since BuddyPress (1.1.0) * * @uses wp_parse_args() * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity} * @uses BP_Activity_Activity::rebuild_activity_comment_tree() {@link BP_Activity_Activity} * @uses wp_cache_delete() * @uses do_action() To call the 'bp_activity_add' hook * * @param array $args { * An array of arguments. * @type int|bool $id Pass an activity ID to update an existing item, or * false to create a new item. Default: false. * @type string $action Optional. The activity action/description, typically * something like "Joe posted an update". Values passed to this param * will be stored in the database and used as a fallback for when the * activity item's format_callback cannot be found (eg, when the * component is disabled). As long as you have registered a * format_callback for your $type, it is unnecessary to include this * argument - BP will generate it automatically. * See {@link bp_activity_set_action()}. * @type string $content Optional. The content of the activity item. * @type string $component The unique name of the component associated with * the activity item - 'groups', 'profile', etc. * @type string $type The specific activity type, used for directory * filtering. 'new_blog_post', 'activity_update', etc. * @type string $primary_link Optional. The URL for this item, as used in * RSS feeds. Defaults to the URL for this activity item's permalink page. * @type int|bool $user_id Optional. The ID of the user associated with the * activity item. May be set to false or 0 if the item is not related * to any user. Default: the ID of the currently logged-in user. * @type int $item_id Optional. The ID of the associated item. * @type int $secondary_item_id Optional. The ID of a secondary associated * item. * @type string $date_recorded Optional. The GMT time, in Y-m-d h:i:s format, * when the item was recorded. Defaults to the current time. * @type bool $hide_sitewide Should the item be hidden on sitewide streams? * Default: false. * @type bool $is_spam Should the item be marked as spam? Default: false. * } * @return int|bool The ID of the activity on success. False on error. */ function bp_activity_add( $args = '' ) { $r = bp_parse_args( $args, array( 'id' => false, // Pass an existing activity ID to update an existing entry. 'action' => '', // The activity action - e.g. "Jon Doe posted an update" 'content' => '', // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!" 'component' => false, // The name/ID of the component e.g. groups, profile, mycomponent 'type' => false, // The activity type e.g. activity_update, profile_updated 'primary_link' => '', // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink) 'user_id' => bp_loggedin_user_id(), // Optional: The user to record the activity for, can be false if this activity is not for a user. 'item_id' => false, // Optional: The ID of the specific item being recorded, e.g. a blog_id 'secondary_item_id' => false, // Optional: A second ID used to further filter e.g. a comment_id 'recorded_time' => bp_core_current_time(), // The GMT time that this activity was recorded 'hide_sitewide' => false, // Should this be hidden on the sitewide activity stream? 'is_spam' => false, // Is this activity item to be marked as spam? ) ); // Make sure we are backwards compatible if ( empty( $r['component'] ) && !empty( $r['component_name'] ) ) { $r['component'] = $r['component_name']; } if ( empty( $r['type'] ) && !empty( $r['component_action'] ) ) { $r['type'] = $r['component_action']; } // Setup activity to be added $activity = new BP_Activity_Activity( $r['id'] ); $activity->user_id = $r['user_id']; $activity->component = $r['component']; $activity->type = $r['type']; $activity->content = $r['content']; $activity->primary_link = $r['primary_link']; $activity->item_id = $r['item_id']; $activity->secondary_item_id = $r['secondary_item_id']; $activity->date_recorded = $r['recorded_time']; $activity->hide_sitewide = $r['hide_sitewide']; $activity->is_spam = $r['is_spam']; $activity->action = ! empty( $r['action'] ) ? $r['action'] : bp_activity_generate_action_string( $activity ); if ( ! $activity->save() ) { return false; } // If this is an activity comment, rebuild the tree if ( 'activity_comment' === $activity->type ) { // Also clear the comment cache for the parent activity ID wp_cache_delete( $activity->item_id, 'bp_activity_comments' ); BP_Activity_Activity::rebuild_activity_comment_tree( $activity->item_id ); } wp_cache_delete( 'bp_activity_sitewide_front', 'bp' ); /** * Fires at the end of the execution of adding a new activity item, before returning the new activity item ID. * * @since BuddyPress (1.1.0) * * @param array $r Array of parsed arguments for the activity item being added. */ do_action( 'bp_activity_add', $r ); return $activity->id; }
/** * Add an activity item. * * @since BuddyPress (1.1) * * @uses wp_parse_args() * @uses BP_Activity_Activity::save() {@link BP_Activity_Activity} * @uses BP_Activity_Activity::rebuild_activity_comment_tree() {@link BP_Activity_Activity} * @uses wp_cache_delete() * @uses do_action() To call the 'bp_activity_add' hook * * @param array $args { * An array of arguments. * @type int|bool $id Pass an activity ID to update an existing item, or * false to create a new item. Default: false. * @type string $action Optional. The activity action/description, typically * something like "Joe posted an update". Values passed to this param * will be stored in the database and used as a fallback for when the * activity item's format_callback cannot be found (eg, when the * component is disabled). As long as you have registered a * format_callback for your $type, it is unnecessary to include this * argument - BP will generate it automatically. * See {@link bp_activity_set_action()}. * @type string $content Optional. The content of the activity item. * @type string $component The unique name of the component associated with * the activity item - 'groups', 'profile', etc. * @type string $type The specific activity type, used for directory * filtering. 'new_blog_post', 'activity_update', etc. * @type string $primary_link Optional. The URL for this item, as used in * RSS feeds. Defaults to the URL for this activity item's permalink page. * @type int|bool $user_id Optional. The ID of the user associated with the * activity item. May be set to false or 0 if the item is not related * to any user. Default: the ID of the currently logged-in user. * @type int $item_id Optional. The ID of the associated item. * @type int $secondary_item_id Optional. The ID of a secondary associated * item. * @type string $date_recorded Optional. The GMT time, in Y-m-d h:i:s format, * when the item was recorded. Defaults to the current time. * @type bool $hide_sitewide Should the item be hidden on sitewide streams? * Default: false. * @type bool $is_spam Should the item be marked as spam? Default: false. * } * @return int|bool The ID of the activity on success. False on error. */ function bp_activity_add($args = '') { $defaults = array('id' => false, 'action' => '', 'content' => '', 'component' => false, 'type' => false, 'primary_link' => '', 'user_id' => bp_loggedin_user_id(), 'item_id' => false, 'secondary_item_id' => false, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => false, 'is_spam' => false); $params = wp_parse_args($args, $defaults); extract($params, EXTR_SKIP); // Make sure we are backwards compatible if (empty($component) && !empty($component_name)) { $component = $component_name; } if (empty($type) && !empty($component_action)) { $type = $component_action; } // Setup activity to be added $activity = new BP_Activity_Activity($id); $activity->user_id = $user_id; $activity->component = $component; $activity->type = $type; $activity->content = $content; $activity->primary_link = $primary_link; $activity->item_id = $item_id; $activity->secondary_item_id = $secondary_item_id; $activity->date_recorded = $recorded_time; $activity->hide_sitewide = $hide_sitewide; $activity->is_spam = $is_spam; $activity->action = !empty($action) ? $action : bp_activity_generate_action_string($activity); if (!$activity->save()) { return false; } // If this is an activity comment, rebuild the tree if ('activity_comment' == $activity->type) { BP_Activity_Activity::rebuild_activity_comment_tree($activity->item_id); } wp_cache_delete('bp_activity_sitewide_front', 'bp'); do_action('bp_activity_add', $params); return $activity->id; }