/** * Maps the before_delete_post action to mpp_before_gallery_delete action * It also does the cleanup of attachments as wp_delete_post() does not delete attachments * * @param int $gallery_id post id * @return unknown */ public function map_before_delete_post_action($gallery_id) { //is this called or a valid gallery? if (!$gallery_id || !mpp_is_valid_gallery($gallery_id)) { return; } $gallery = mpp_get_gallery($gallery_id); //we are certain that it is called for gallery //fire the MediaPress gallery delete action if ($this->is_queued($gallery_id)) { //this action is already being executed for the current gallery, no need to do that again return; } $this->add_item($gallery_id, 'gallery'); do_action('mpp_before_gallery_delete', $gallery_id); //after that action, we delete all attachment global $wpdb; //1// delete all media $storage_manager = null; $media_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d", $gallery_id)); //we need the storage manager to notify it that it do do any final cleanup after the gallery delete //should we keep a reference to the storage manager for each gallery? what will happen for a gallery that contains local/remote media? //for the time being we are not keeping a reference but this method is doing exactly the same and I am not sure which approach will be fbetter for futuer if (!empty($media_ids)) { $mid = $media_ids[0]; $storage_manager = mpp_get_storage_manager($mid); } //delete all media foreach ($media_ids as $media_id) { wp_delete_attachment($media_id); //delete all media } if (mediapress()->is_bp_active()) { //delete all gallery activity mpp_gallery_delete_activity($gallery_id); //delete all associated activity meta //mpp_gallery_delete_activity_meta( $gallery_id ); } //Delete wall gallery meta mpp_delete_wall_gallery_id(array('component' => $gallery->component, 'component_id' => $gallery->component_id, 'gallery_id' => $gallery->id, 'media_type' => $gallery->type)); //do any final cleanup, deletegate to the storage manager if ($storage_manager) { $storage_manager->delete_gallery($gallery); } return; }
/** * Delete a Gallery * @global type $bp * @param type $gallery_id * @return boolean */ function mpp_delete_gallery($gallery_id, $delete_posts = true) { global $bp, $wpdb; if (!$gallery_id) { return false; } $gallery = mpp_get_gallery($gallery_id); //1// delete all media $media_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d", $gallery_id)); foreach ($media_ids as $media_id) { mpp_delete_media($media_id); //delete all media } //delete all gallery activity mpp_gallery_delete_activity($gallery_id); //delete all associated activity meta mpp_gallery_delete_activity_meta($gallery_id); //Delete wall gallery meta mpp_delete_wall_gallery_id(array('component' => $gallery->component, 'component_id' => $gallery->component_id, 'gallery_id' => $gallery->id, 'media_type' => $gallery->type)); wp_delete_post($gallery_id, true); //completely delete post do_action('mpp_gallery_deleted', $gallery_id); return true; }