BP_Media::save()

Save the media item to the database.

Description

Return

(WP_Error|bool) True on success.

Source

File: bp-media/classes/class-bp-media.php

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
public function save() {
 
    global $wpdb;
 
    $bp = buddypress();
 
    $this->id            = apply_filters_ref_array( 'bp_media_id_before_save',                array( $this->id,                &$this ) );
    $this->blog_id       = apply_filters_ref_array( 'bp_media_blog_id_before_save',           array( $this->blog_id,           &$this ) );
    $this->attachment_id = apply_filters_ref_array( 'bp_media_attachment_id_before_save',     array( $this->attachment_id,     &$this ) );
    $this->user_id       = apply_filters_ref_array( 'bp_media_user_id_before_save',           array( $this->user_id,           &$this ) );
    $this->title         = apply_filters_ref_array( 'bp_media_title_before_save',             array( $this->title,             &$this ) );
    $this->album_id      = apply_filters_ref_array( 'bp_media_album_id_before_save',          array( $this->album_id,          &$this ) );
    $this->activity_id   = apply_filters_ref_array( 'bp_media_activity_id_before_save',       array( $this->activity_id,       &$this ) );
    $this->group_id      = apply_filters_ref_array( 'bp_media_group_id_before_save',          array( $this->group_id,          &$this ) );
    $this->privacy       = apply_filters_ref_array( 'bp_media_privacy_before_save',           array( $this->privacy,           &$this ) );
    $this->menu_order    = apply_filters_ref_array( 'bp_media_menu_order_before_save',        array( $this->menu_order,        &$this ) );
    $this->date_created  = apply_filters_ref_array( 'bp_media_date_created_before_save',      array( $this->date_created,      &$this ) );
 
    /**
     * Fires before the current media item gets saved.
     *
     * Please use this hook to filter the properties above. Each part will be passed in.
     *
     * @since BuddyBoss 1.0.0
     *
     * @param BP_Media $this Current instance of the media item being saved. Passed by reference.
     */
    do_action_ref_array( 'bp_media_before_save', array( &$this ) );
 
    if ( 'wp_error' === $this->error_type && $this->errors->get_error_code() ) {
        return $this->errors;
    }
 
    if ( empty( $this->attachment_id )
    //|| empty( $this->activity_id ) //todo: when forums media is saving, it should have activity id assigned if settings enabled need to check this
    ) {
        if ( 'bool' === $this->error_type ) {
            return false;
        } else {
            if ( empty( $this->activity_id ) ) {
                $this->errors->add( 'bp_media_missing_activity' );
            } else {
                $this->errors->add( 'bp_media_missing_attachment' );
            }
 
            return $this->errors;
        }
    }
 
    // If we have an existing ID, update the media item, otherwise insert it.
    if ( ! empty( $this->id ) ) {
        $q = $wpdb->prepare( "UPDATE {$bp->media->table_name} SET blog_id = %d, attachment_id = %d, user_id = %d, title = %s, album_id = %d, activity_id = %d, group_id = %d, privacy = %s, menu_order = %d, date_created = %s WHERE id = %d", $this->blog_id, $this->attachment_id, $this->user_id, $this->title, $this->album_id, $this->activity_id, $this->group_id, $this->privacy, $this->menu_order, $this->date_created, $this->id );
    } else {
        $q = $wpdb->prepare( "INSERT INTO {$bp->media->table_name} ( blog_id, attachment_id, user_id, title, album_id, activity_id, group_id, privacy, menu_order, date_created ) VALUES ( %d, %d, %d, %s, %d, %d, %d, %s, %d, %s )", $this->blog_id, $this->attachment_id, $this->user_id, $this->title, $this->album_id, $this->activity_id, $this->group_id, $this->privacy, $this->menu_order, $this->date_created );
    }
 
    if ( false === $wpdb->query( $q ) ) {
        return false;
    }
 
    // If this is a new media item, set the $id property.
    if ( empty( $this->id ) ) {
        $this->id = $wpdb->insert_id;
    }
 
    /**
     * Fires after an media item has been saved to the database.
     *
     * @since BuddyBoss 1.0.0
     *
     * @param BP_Media $this Current instance of media item being saved. Passed by reference.
     */
    do_action_ref_array( 'bp_media_after_save', array( &$this ) );
 
    return true;
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.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.