BP_REST_Media_Endpoint::bp_rest_create_media( array $args )
Create the Media IDs from Upload IDs.
Description
Parameters
- $args
-
(Required) Key value array of query var to query value.
Return
(array|WP_Error)
Source
File: bp-media/classes/class-bp-rest-media-endpoint.php
public function bp_rest_create_media( $args ) { $media_privacy = ( ! empty( $args['privacy'] ) ? $args['privacy'] : 'public' ); $upload_ids = ( ! empty( $args['upload_ids'] ) ? $args['upload_ids'] : '' ); $activity_id = ( ! empty( $args['activity_id'] ) ? $args['activity_id'] : false ); $content = ( ! empty( $args['content'] ) ? $args['content'] : false ); $user_id = ( ! empty( $args['user_id'] ) ? $args['user_id'] : get_current_user_id() ); $id = ( ! empty( $args['id'] ) ? $args['id'] : '' ); $group_id = ( ! empty( $args['group_id'] ) ? $args['group_id'] : false ); $album_id = ( ! empty( $args['album_id'] ) ? $args['album_id'] : false ); // Override the privacy if album ID is given. if ( ! empty( $args['album_id'] ) ) { $albums = bp_album_get_specific( array( 'album_ids' => array( $args['album_id'] ) ) ); if ( ! empty( $albums['albums'] ) ) { $album = array_pop( $albums['albums'] ); $media_privacy = $album->privacy; } } // Update Media. if ( ! empty( $id ) ) { $wp_attachment_id = $args['attachment_id']; $wp_attachment_url = wp_get_attachment_url( $wp_attachment_id ); // when the file found to be empty it's means it's not a valid attachment. if ( empty( $wp_attachment_url ) ) { return; } $media_activity_id = $activity_id; // extract the nice title name. $title = get_the_title( $wp_attachment_id ); $media_id = bp_media_add( array( 'id' => $id, 'attachment_id' => $wp_attachment_id, 'title' => $title, 'activity_id' => $media_activity_id, 'album_id' => ( ! empty( $args['album_id'] ) ? $args['album_id'] : false ), 'group_id' => ( ! empty( $args['group_id'] ) ? $args['group_id'] : false ), 'privacy' => $media_privacy, 'user_id' => $user_id, 'error_type' => 'wp_error', ) ); if ( is_int( $media_id ) ) { // save media is saved in attachment. update_post_meta( $wp_attachment_id, 'bp_media_saved', true ); // save media meta for activity. if ( ! empty( $media_activity_id ) ) { update_post_meta( $wp_attachment_id, 'bp_media_activity_id', $media_activity_id ); } $created_media_ids[] = $media_id; } } // created Medias. if ( ! empty( $upload_ids ) ) { $valid_upload_ids = array(); foreach ( $upload_ids as $wp_attachment_id ) { $wp_attachment_url = wp_get_attachment_url( $wp_attachment_id ); // when the file found to be empty it's means it's not a valid attachment. if ( empty( $wp_attachment_url ) ) { continue; } $valid_upload_ids[] = $wp_attachment_id; } if ( ! empty( $valid_upload_ids ) ) { foreach ( $valid_upload_ids as $wp_attachment_id ) { $media_activity_id = false; // make an activity for the media. if ( bp_is_active( 'activity' ) ) { if ( empty( $activity_id ) ) { if ( bp_is_active( 'groups' ) && ! empty( $group_id ) && $group_id > 0 ) { $activity_id = groups_post_update( array( 'content' => $content, 'group_id' => $group_id, 'privacy' => 'public' ) ); } else { $activity_id = bp_activity_post_update( array( 'content' => $content ) ); } } if ( count( $valid_upload_ids ) > 1 ) { if ( bp_is_active( 'groups' ) && ! empty( $group_id ) && $group_id > 0 ) { $media_activity_id = groups_post_update( array( 'group_id' => $group_id, 'hide_sitewide' => true, 'privacy' => 'media', ) ); } else { $media_activity_id = bp_activity_post_update( array( 'hide_sitewide' => true, 'privacy' => 'media', ) ); } if ( $media_activity_id ) { // update activity meta. bp_activity_update_meta( $media_activity_id, 'bp_media_activity', '1' ); } } else { $media_activity_id = $activity_id; } } // extract the nice title name. $title = get_the_title( $wp_attachment_id ); $media_id = bp_media_add( array( 'attachment_id' => $wp_attachment_id, 'title' => $title, 'activity_id' => $media_activity_id, 'album_id' => ( ! empty( $args['album_id'] ) ? $args['album_id'] : false ), 'group_id' => ( ! empty( $args['group_id'] ) ? $args['group_id'] : false ), 'privacy' => $media_privacy, 'user_id' => $user_id, 'error_type' => 'wp_error', ) ); if ( is_int( $media_id ) ) { // save media is saved in attachment. update_post_meta( $wp_attachment_id, 'bp_media_saved', true ); // save media meta for activity. if ( ! empty( $activity_id ) ) { update_post_meta( $wp_attachment_id, 'bp_media_parent_activity_id', $activity_id ); if ( $media_activity_id !== $activity_id ) { update_post_meta( $wp_attachment_id, 'bp_media_activity_id', $media_activity_id ); } } $created_media_ids[] = $media_id; } } } } if ( empty( $created_media_ids ) ) { return new WP_Error( 'bp_rest_media_creation_error', __( 'Error creating media, please try again.', 'buddyboss' ), array( 'status' => 400, ) ); } // Link all uploaded media to main activity. if ( ! empty( $activity_id ) && empty( $id ) ) { $created_media_ids_joined = implode( ',', $created_media_ids ); bp_activity_update_meta( $activity_id, 'bp_media_ids', $created_media_ids_joined ); $main_activity = new BP_Activity_Activity( $activity_id ); if ( ! empty( $main_activity ) && empty( $group_id ) ) { $main_activity->privacy = $media_privacy; $main_activity->save(); } } return $created_media_ids; }
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.