bp_document_rename_file( int $document_id, int $attachment_document_id, string $title = '', $backend = false )
This function will rename the document name.
Description
Parameters
- $document_id
-
(Required)
- $attachment_document_id
-
(Required)
- $title
-
(Optional)
Default value: ''
Return
(bool|int)
Source
File: bp-document/bp-document-functions.php
function bp_document_rename_file( $document_id = 0, $attachment_document_id = 0, $title = '', $backend = false ) {
global $wpdb, $bp;
if ( 0 === $document_id && '' === $title ) {
return false;
}
if ( (int) $document_id > 0 ) {
$has_access = bp_document_user_can_edit( $document_id );
if ( ! $has_access ) {
return false;
}
}
$file_name = $title;
$new_filename = $title;
// Variables.
$post = get_post( $attachment_document_id );
$file_parts = bp_document_get_file_parts( $attachment_document_id );
$old_filename = $file_parts['filename'];
$new_filename_unsanitized = $new_filename;
// sanitizing file name (using sanitize_title because sanitize_file_name doesn't remove accents).
$new_filename = sanitize_file_name( $new_filename );
$file_abs_path = get_attached_file( $post->ID );
$file_abs_dir = dirname( $file_abs_path );
$new_file_abs_path = preg_replace( '~[^/]+$~', $new_filename . '.' . $file_parts['extension'], $file_abs_path );
$file_abs_path = get_attached_file( $post->ID );
$file_abs_dir = dirname( $file_abs_path );
$new_file_abs_path = preg_replace( '~[^/]+$~', $new_filename . '.' . $file_parts['extension'], $file_abs_path );
$file_rel_path = get_post_meta( $post->ID, '_wp_attached_file', 1 );
$new_file_rel_path = preg_replace( '~[^/]+$~', $new_filename . '.' . $file_parts['extension'], $file_rel_path );
$uploads_path = wp_upload_dir();
$uploads_path = $uploads_path['basedir'];
// attachment miniatures.
$searches = bp_document_get_attachment_urls( $attachment_document_id );
// Validations.
if ( ! $post ) {
return __( 'Post with ID ' . $attachment_document_id . ' does not exist!', 'buddyboss' );
}
if ( $post && $post->post_type != 'attachment' ) {
return __( 'Post with ID ' . $attachment_document_id . ' is not an attachment!', 'buddyboss' );
}
if ( ! $new_filename ) {
return __( 'The document name is empty!', 'buddyboss' );
}
if ( $new_filename != sanitize_file_name( $new_filename ) ) {
return __( 'Bad characters or invalid document name!', 'buddyboss' );
}
if ( file_exists( $new_file_abs_path ) ) {
return __( 'A file with that name already exists in the containing folder!', 'buddyboss' );
}
if ( ! is_writable( $file_abs_dir ) ) {
return __( 'The document containing directory is not writable!', 'buddyboss' );
}
// Change the attachment post.
// $post_changes = array();
// $post_changes['ID'] = $post->ID;
// $post_changes['guid'] = preg_replace( '~[^/]+$~', $new_filename . '.' . $file_parts['extension'], $post->guid );
// $post_changes['post_title'] = ( true ) ? bp_document_filename_to_title( $new_filename_unsanitized ) : $post->post_title;
// $post_changes['post_name'] = wp_unique_post_slug( $new_filename, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
// wp_update_post( $post_changes );
// unset( $post_changes );
$my_post = array(
'ID' => $post->ID,
'post_title' => bp_document_filename_to_title( $new_filename_unsanitized ),
// 'post_name' => $new_filename,
'guid' => preg_replace( '~[^/]+$~', $new_filename . '.' . $file_parts['extension'], $post->guid ),
);
$post_id = wp_update_post( $my_post );
// Change attachment post metas & rename files.
foreach ( get_intermediate_image_sizes() as $size ) {
$size_data = image_get_intermediate_size( $attachment_document_id, $size );
@unlink( $uploads_path . DIRECTORY_SEPARATOR . $size_data['path'] );
}
if ( ! @rename( $file_abs_path, $new_file_abs_path ) ) {
return __( 'File renaming error!', 'buddyboss' );
}
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';
}
update_post_meta( $attachment_document_id, '_wp_attached_file', $new_file_rel_path );
wp_update_attachment_metadata( $attachment_document_id, wp_generate_attachment_metadata( $attachment_document_id, $new_file_abs_path ) );
// Replace the old with the new media link in the content of all posts and metas.
$replaces = bp_document_get_attachment_urls( $attachment_document_id );
$i = 0;
$post_types = get_post_types();
unset( $post_types['attachment'] );
while ( $posts = get_posts(
array(
'post_type' => $post_types,
'post_status' => 'any',
'numberposts' => 100,
'offset' => $i * 100,
)
) ) {
foreach ( $posts as $post ) {
// Updating post content if necessary.
$new_post = array( 'ID' => $post->ID );
$new_post['post_content'] = str_replace( '\\', '\\\\', $post->post_content );
$new_post['post_content'] = str_replace( $searches, $replaces, $new_post['post_content'] );
if ( $new_post['post_content'] != $post->post_content ) {
wp_update_post( $new_post );
}
// Updating post metas if necessary.
$metas = get_post_meta( $post->ID );
foreach ( $metas as $key => $meta ) {
$meta[0] = bp_document_unserialize_deep( $meta[0] );
$new_meta = bp_document_replace_media_urls( $meta[0], $searches, $replaces );
if ( $new_meta != $meta[0] ) {
update_post_meta( $post->ID, $key, $new_meta, $meta[0] );
}
}
}
$i++;
}
// Updating options if necessary.
$options = bp_document_get_all_options();
foreach ( $options as $option ) {
$option['value'] = bp_document_unserialize_deep( $option['value'] );
$new_option = bp_document_replace_media_urls( $option['value'], $searches, $replaces );
if ( $new_option != $option['value'] ) {
update_option( $option['name'], $new_option );
}
}
$query = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET title = %s, date_modified = %s WHERE id = %d AND attachment_id = %d", $new_filename, bp_core_current_time(), $document_id, $attachment_document_id );
$query = $wpdb->query( $query ); // db call ok; no-cache ok;
bp_document_update_meta( $document_id, 'file_name', $new_filename );
if ( false === $query ) {
return false;
}
$response = apply_filters(
'bp_document_rename_file',
array(
'document_id' => $document_id,
'attachment_document_id' => $attachment_document_id,
'title' => $new_filename,
)
);
@unlink( $file_abs_path );
return $response;
}
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.