/core/activity/sub-activity-class-singlepost.php
PHP | 380 lines | 175 code | 116 blank | 89 comment | 22 complexity | 185128220de604c993ede12fffe207ca MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
- <?php
- /**
- * BP-MEDIA ACTIVITY STREAM SINGLEPOST CLASS
- * Handles single-item posts in the activity stream
- *
- * @version 0.1.9
- * @since 0.1.9
- * @package BP-Media
- * @subpackage Activity Stream
- * @license GPL v2.0
- * @link http://code.google.com/p/buddypress-media/
- *
- * ========================================================================================================
- */
- class BPM_Activity_SinglePost {
- /**
- * Renders a single-item activity stream post
- *
- * @version 0.1.9
- * @since 0.1.9
- * @param int $activity_post_id | ID of the activity stream post
- * @param int $album_id | ID of the album that the media item belongs to
- * @param int $media_id | ID of media item
- * @return string | Text string of post contents
- */
- public static function renderPost($activity_post_id, $album_id, $media_id ){
- global $bp, $wpdb;
- if( empty($activity_post_id) || empty($album_id) || empty($media_id) ){
- return false;
- }
- // Retrieve the media item to add to the activity stream post
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = array();
- $args['id'] = $media_id;
- $media_items = BPM_Media::query_media($args);
- unset($args);
- $media_item = $media_items[0];
- if($debug){
- echo "Fetch Full Media Items \n";
- var_dump($media_items);
- }
- // Generate the html block containing the linked media item thumbnail
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = BPM_Cache::fill_resize_args(array( 'size'=>$bp->bpa->options['media_cache_images_activitySingleDisplaySize'] ));
- $cache_image = bp_media_get_preview_image_data( $args, $media_item );
- unset($args);
- $item_href = bp_core_get_user_domain($media_item->owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
- $album_id .'/'. $bp->bpa->options['media_slug_single'] .'/'. $media_item->id;
- $content = '<div class="bpa-activity-wrap" id="default">';
- $content .= '<div class="bpa-activity-singleimage">';
- $content .= '<a class="bpa-activity-link" ';
- $content .= 'title="' . $media_item->title . '" ';
- $content .= 'href="' . $item_href . '">' . '<img class="bpa-activity-thumb" ';
- $content .= 'height="' . $cache_image['file_h'] . '" ';
- $content .= 'width="' . $cache_image['file_w'] . '" ';
- $content .= 'alt="' . $media_item->description . '" ';
- $content .= 'src="' . $cache_image['url'] . '"></a>';
- $content .= '<div class="bpa-activity-imagecaption">';
- $content .= $media_item->description;
- $content .= '</div>';
- $content .= '</div>';
-
- if($bp->bpa->options['core_favorites_activityShow'] == true) {
- $content .= BPM_Activity_Favorite::renderTags($activity_post_id);
- }
- $content .= '</div>';
- return $content;
- }
- /**
- * Adds a single media item post to a user's activity stream.
- *
- * BP-Media doesn't post single media items to the activity stream until somebody comments on the
- * item. Otherwise, the activity stream would be flooded with hundreds of posts every time members
- * did a batch upload.
- *
- * Because of the way the BP activity stream works, members can't comment on something unless it's
- * already posted in the activity stream. To get around this limitation, when somebody comments on
- * an item for the first time, this function posts just that one item to the activity stream, so we
- * can attach a comment to it.
- *
- * @version 0.1.9
- * @since 0.1.9
- * @param int $owner_id | ID of the album's owner
- * @param int $album_id | ID of the album that the media item belongs to
- * @param int $media_id | ID of media item
- * @return bool | True on success
- */
- public static function createPost($album_id, $media_id ){
- global $bp, $wpdb;
- // Should we push user activity stream posts to global activity stream
- $post_global = $bp->bpa->options['core_comments_globalStreamEnable'];
- // Retrieve the album that the activity stream post is based on
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = array();
- $args['album_id'] = $album_id;
- $args['owner_id'] = false;
- $album = BPM_Album::query_albums($args);
- $album = $album[0];
- unset($args);
- if($debug){
- echo "Fetch Album \n";
- var_dump($album);
- }
- // Retrieve the media item to add to the activity stream post
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = array();
- $args['id'] = $media_id;
- $args['owner_id'] = false;
- $media_items = BPM_Media::query_media($args);
- unset($args);
- $media_item = $media_items[0];
- if($debug){
- echo "Fetch Full Media Items \n";
- var_dump($media_items);
- }
- $item_href = bp_core_get_user_domain($media_item->owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
- $album_id .'/'. $bp->bpa->options['media_slug_single'] .'/'. $media_item->id;
- $album_href = bp_core_get_user_domain($album->owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
- $album_id .'/';
- $action = sprintf( __( '%s - %s from the album %s', "bp-media" ), bp_core_get_userlink($media_item->owner_id), '<a href="'. $item_href .'">'.$media_item->title.'</a>',
- '<a href="'. $album_href .'">'.$album->title.'</a>' );
- // If the admin has disabled posts to the global activity stream and the user has set the album's privacy level
- // to public (priv level = 0), increase the privacy level to 1 so it is hidden in the global activity stream
- if(($post_global == false) && ($album->privacy < 2)){
- $post_privacy = 1;
- } else {
- $post_privacy = $album->privacy;
- }
- // Generate the activity stream post array
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = array();
- $args['id'] = null;
- $args['action'] = $action;
- $args['component'] = 'bp_album';
- $args['type'] = 'single_item';
- $args['primary_link'] = null;
- $args['user_id'] = $media_item->owner_id;
- $args['item_id'] = $album_id;
- $args['secondary_item_id'] = $media_item->id;
- $args['hide_sitewide'] = $post_privacy;
- if($debug){
- echo "Full Post Content \n";
- var_dump($args);
- }
- $activity_id = bp_activity_add($args);
- return $activity_id;
- }
- /**
- * Rebuilds the contents of an existing single-item activity stream post.
- *
- * @version 0.1.9
- * @since 0.1.9
- * @param int $owner_id | ID of the album's owner
- * @param int $album_id | ID of the album the media item belongs to
- * @param int $activity_post_id | ID of the activity stream post
- * @param bool $reset_timestamp | If set to true, the timestamp for the post will be reset to the current time
- * @return bool | True on success
- */
- static function updatePost($owner_id, $media_id, $album_id, $activity_post_id, $reset_timestamp = false){
- global $bp, $wpdb;
- // Should we push user activity stream posts to global activity stream
- $post_global = $bp->bpa->options['core_comments_globalStreamEnable'];
- // Retrieve the album that the activity stream post is based on
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = array();
- $args['album_id'] = $album_id;
- $args['owner_id'] = $owner_id;
- $album = BPM_Album::query_albums($args);
- $album = $album[0];
- unset($args);
- if($debug){
- echo "updateSinglePost() Fetch Album \n";
- var_dump($album);
- }
- // Retrieve the media item that the activity stream post is based on
- /////////////////////////////////////////////////////////////////////////////////////////
- $args = array();
- $args['id'] = $media_id;
- $args['owner_id'] = $owner_id;
- $media_items = BPM_Media::query_media($args);
- unset($args);
- $media_item = $media_items[0];
- if($debug){
- echo "updateSinglePost() Fetch Full Media Items \n";
- var_dump($media_items);
- }
- $item_href = bp_core_get_user_domain($owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
- $album_id .'/'. $bp->bpa->options['media_slug_single'] .'/'. $media_item->id;
- $album_href = bp_core_get_user_domain($owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
- $album_id .'/';
- $action = sprintf( __( '%s - %s from the album %s', "bp-media" ), bp_core_get_userlink($owner_id), '<a href="'. $item_href .'">'.$media_item->title.'</a>',
- '<a href="'. $album_href .'">'.$album->title.'</a>' );
- // Update the activity stream post
- // /////////////////////////////////////////////////////////////////////////////////////////////////////////
- // If the admin has disabled posts to the global activity stream and the user has set the album's privacy level
- // to public (priv level = 0), increase the privacy level to 1 so it is hidden in the global activity stream
- if(($post_global == false) && ($album->privacy < 2)){
- $post_privacy = 1;
- } else {
- $post_privacy = $album->privacy;
- }
- if(!$reset_timestamp){
- // Since bp_activity_add() resets the timestamp on posts, we cannot use this function for updating activity
- // stream items. Otherwise, it would bring old posts back to the top of the activity stream when we delete
- // media items from them. We could get around it by querying the old post and retrieving its timestamp, but
- // that would require an extra query. Its better to do an update query and use the SQL server properly
- $sql = $wpdb->prepare("
- UPDATE {$bp->activity->table_name}
- SET action = %s,
- item_id = %d,
- hide_sitewide = %d
- WHERE id = %d",
- $action, $album_id, $post_privacy,
- $activity_post_id
- );
- // clear cache
- wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
- }
- else {
- $date_recorded = gmdate( "Y-m-d H:i:s" );
- $sql = $wpdb->prepare("
- UPDATE {$bp->activity->table_name}
- SET action = %s,
- item_id = %d,
- date_recorded = %s,
- hide_sitewide = %d
- WHERE id = %d",
- $action, $album_id, $date_recorded, $post_privacy,
- $activity_post_id
- );
- // clear cache
- wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
- }
- $result = $wpdb->query($sql);
- if($debug){
- echo "updateSinglePost()SQL:\n";
- var_dump($sql);
- echo "updateSinglePost() Result:\n";
- var_dump($result);
- }
- return $result;
- }
-
- /**
- * Checks if an activity post is a BP-Media single-item post
- *
- * @version 0.1.9
- * @since 0.1.9
- * @param int $activity_id | ID of the activity item to test
- * @return bool | True on success, false on failure
- */
- public static function isSingleItem($activity_post_id){
- global $bp, $wpdb;
- $sql = $wpdb->prepare("
- SELECT COUNT(id)
- FROM {$bp->activity->table_name}
- WHERE id = %d
- AND component = 'bp_album'
- AND type = 'single_item'",
- $activity_post_id
- );
- $item_count = (int)$wpdb->get_var($sql);
- return $item_count;
- }
- }
- ?>