BP_Document::save()
Save the document item to the database.
Description
Return
(WP_Error|bool) True on success.
Source
File: bp-document/classes/class-bp-document.php
public function save() {
global $wpdb;
$bp = buddypress();
$this->id = apply_filters_ref_array( 'bp_document_id_before_save', array( $this->id, &$this ) );
$this->blog_id = apply_filters_ref_array( 'bp_document_blog_id_before_save', array( $this->blog_id, &$this ) );
$this->attachment_id = apply_filters_ref_array( 'bp_document_attachment_id_before_save', array( $this->attachment_id, &$this ) );
$this->user_id = apply_filters_ref_array( 'bp_document_user_id_before_save', array( $this->user_id, &$this ) );
$this->title = apply_filters_ref_array( 'bp_document_title_before_save', array( $this->title, &$this ) );
$this->folder_id = apply_filters_ref_array( 'bp_document_folder_id_before_save', array( $this->folder_id, &$this ) );
$this->activity_id = apply_filters_ref_array( 'bp_document_activity_id_before_save', array( $this->activity_id, &$this ) );
$this->group_id = apply_filters_ref_array( 'bp_document_group_id_before_save', array( $this->group_id, &$this ) );
$this->privacy = apply_filters_ref_array( 'bp_document_privacy_before_save', array( $this->privacy, &$this ) );
$this->menu_order = apply_filters_ref_array( 'bp_document_menu_order_before_save', array( $this->menu_order, &$this ) );
$this->date_created = apply_filters_ref_array( 'bp_document_date_created_before_save', array( $this->date_created, &$this ) );
$this->date_modified = apply_filters_ref_array( 'bp_document_date_modified_before_save', array( $this->date_modified, &$this ) );
/**
* Fires before the current document item gets saved.
* Please use this hook to filter the properties above. Each part will be passed in.
*
* @param BP_Document $this Current instance of the document item being saved. Passed by reference.
*
* @since BuddyBoss 1.4.0
*/
do_action_ref_array( 'bp_document_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 document 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_document_missing_activity' );
} else {
$this->errors->add( 'bp_document_missing_attachment' );
}
return $this->errors;
}
}
// Generate PDF file preview image.
$attachment_id = $this->attachment_id;
$pdf_preview = false;
$is_pdf = false;
$is_preview_generated = get_post_meta( $attachment_id, 'document_preview_generated', true );
$preview_attachment_id = (int) get_post_meta( $attachment_id, 'document_preview_attachment_id', true );
if ( empty( $is_preview_generated ) ) {
$extension = bp_document_extension( $attachment_id );
$preview_attachment_id = 0;
$file = get_attached_file( $attachment_id );
$upload_dir = wp_upload_dir();
if ( 'pdf' === $extension ) {
$is_pdf = true;
$output_file = wp_get_attachment_image_url( $attachment_id, 'full' );
$output_file_src = bp_document_scaled_image_path( $attachment_id );
if ( '' !== $output_file && '' !== basename( $output_file ) && strstr( $output_file, 'bb_documents/' ) ) {
add_filter( 'upload_dir', 'bp_document_upload_dir_script' );
$upload_dir = $upload_dir['basedir'];
// Create temp folder.
$upload_dir = $upload_dir . '/preview-image-folder-' . time();
$preview_folder = $upload_dir;
// If folder not exists then create.
if ( ! is_dir( $upload_dir ) ) {
// Create temp folder.
wp_mkdir_p( $upload_dir );
chmod( $upload_dir, 0777 );
// Create given main parent folder.
$preview_folder = $upload_dir;
wp_mkdir_p( $preview_folder );
$file_name = basename( $output_file );
$extension_pos = strrpos($file_name, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($file_name, 0, $extension_pos) . '_thumb' . substr($file_name, $extension_pos);
copy( $output_file_src, $preview_folder . '/' . $thumb );
}
$files = scandir( $preview_folder );
$firstFile = $preview_folder . '/' . $files[2];
bp_document_chmod_r( $preview_folder );
$image_data = file_get_contents( $firstFile );
$filename = basename( $output_file );
$upload_dir = wp_upload_dir();
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents( $file, $image_data );
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit',
);
$preview_attachment_id = wp_insert_attachment( $attachment, $file );
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once ABSPATH . 'wp-admin' . '/includes/image.php';
require_once ABSPATH . 'wp-admin' . '/includes/file.php';
require_once ABSPATH . 'wp-admin' . '/includes/media.php';
}
$attach_data = wp_generate_attachment_metadata( $preview_attachment_id, $file );
wp_update_attachment_metadata( $preview_attachment_id, $attach_data );
update_post_meta( $attachment_id, 'document_preview_generated', 'yes' );
update_post_meta( $attachment_id, 'document_preview_attachment_id', $preview_attachment_id );
$pdf_preview = true;
BP_Document::bp_document_remove_temp_directory( $preview_folder );
remove_filter( 'upload_dir', 'bp_document_upload_dir_script' );
}
} else if ( 'css' === $extension || 'txt' === $extension || 'js' === $extension || 'html' === $extension || 'htm' === $extension || 'csv' === $extension ) {
$absolute_path = get_attached_file( $attachment_id );
if ( '' !== $absolute_path && '' !== basename( $absolute_path ) && strstr( $absolute_path, 'bb_documents/' ) ) {
$upload_dir = $upload_dir['basedir'];
// Create temp folder.
$upload_dir = $upload_dir . '/preview-image-folder-' . time();
$preview_folder = $upload_dir;
// If folder not exists then create.
if ( ! is_dir( $upload_dir ) ) {
// Create temp folder.
wp_mkdir_p( $upload_dir );
chmod( $upload_dir, 0777 );
// Create given main parent folder.
$preview_folder = $upload_dir;
wp_mkdir_p( $preview_folder );
$file_name = basename( $absolute_path );
$extension_pos = strrpos($file_name, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($file_name, 0, $extension_pos) . '_thumb' . substr($file_name, $extension_pos);
copy( $absolute_path, $preview_folder . '/' . $thumb );
}
$files = scandir( $preview_folder );
$firstFile = $preview_folder . '/' . $files[2];
bp_document_chmod_r( $preview_folder );
$image_data = file_get_contents( $firstFile );
$words = 10000;
$mirror_text = strlen($image_data) > $words ? substr($image_data,0,$words).'...' : $image_data;
update_post_meta( $attachment_id, 'document_preview_mirror_text', $mirror_text );
BP_Document::bp_document_remove_temp_directory( $preview_folder );
}
}
}
// If we have an existing ID, update the document item, otherwise insert it.
if ( ! empty( $this->id ) ) {
$q = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET blog_id = %d, attachment_id = %d, user_id = %d, title = %s, folder_id = %d, activity_id = %d, group_id = %d, privacy = %s, menu_order = %d, date_modified = %s WHERE id = %d", $this->blog_id, $this->attachment_id, $this->user_id, $this->title, $this->folder_id, $this->activity_id, $this->group_id, $this->privacy, $this->menu_order, $this->date_modified, $this->id );
} else {
$q = $wpdb->prepare( "INSERT INTO {$bp->document->table_name} ( blog_id, attachment_id, user_id, title, folder_id, activity_id, group_id, privacy, menu_order, date_created, date_modified ) VALUES ( %d, %d, %d, %s, %d, %d, %d, %s, %d, %s, %s )", $this->blog_id, $this->attachment_id, $this->user_id, $this->title, $this->folder_id, $this->activity_id, $this->group_id, $this->privacy, $this->menu_order, $this->date_created, $this->date_modified );
}
if ( false === $wpdb->query( $q ) ) {
return false;
}
// If this is a new document item, set the $id property.
if ( empty( $this->id ) ) {
$this->id = $wpdb->insert_id;
}
if ( $preview_attachment_id ) {
bp_document_update_meta( $this->id, 'preview_attachment_id', $preview_attachment_id );
}
if ( ! $pdf_preview && $is_pdf ) {
add_filter( 'wp_image_editors', array( $this, 'bp_document_wp_image_editors' ) );
self::bp_document_pdf_previews( array( $this->attachment_id ), true, $this->id );
remove_filter( 'wp_image_editors', array( $this, 'bp_document_wp_image_editors' ) );
}
// Update folder modified date.
$folder = (int) $this->folder_id;
if ( $folder > 0 ) {
bp_document_update_folder_modified_date( $folder );
}
/**
* Fires after an document item has been saved to the database.
*
* @param BP_Document $this Current instance of document item being saved. Passed by reference.
*
* @since BuddyBoss 1.4.0
*/
do_action_ref_array( 'bp_document_after_save', array( &$this ) );
return true;
}
Changelog
| Version | Description |
|---|---|
| BuddyBoss 1.4.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.