LLMS_Admin_Media_Protection_Attachment_Settings::move_attachment_to_protected_dir( $attachment_id )
Move an existing media attachment over to the protected folder.
Parameters Parameters
- $attachment_id
-
(Required)
Return Return
(bool)
Source Source
File: includes/admin/class-llms-admin-media-protection-attachment-settings.php
function move_attachment_to_protected_dir( $attachment_id ) {
// Get attachment metadata.
$metadata = wp_get_attachment_metadata( $attachment_id );
$file = get_attached_file( $attachment_id );
// Get the protected upload directory.
$protector = new LLMS_Media_Protector();
// We could check that the file is in the protected folder, but currently there's no "unprotect" method.
if ( $protector->is_media_protected( $attachment_id ) ) {
return false;
}
$protected_dir = $protector->get_upload_basedir();
global $wp_filesystem;
if ( empty( $wp_filesystem ) ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
$new_file = str_replace( wp_upload_dir()['basedir'], wp_upload_dir()['basedir'] . untrailingslashit( $protected_dir ), $file );
if ( ! $wp_filesystem->is_dir( dirname( $new_file ) ) ) {
wp_mkdir_p( dirname( $new_file ) );
}
if ( $wp_filesystem->move( $file, $new_file ) ) {
// Move thumbnails if they exist.
if ( ! empty( $metadata['sizes'] ) ) {
$base_dir = dirname( $file );
$new_base_dir = dirname( $new_file );
// Multiple registered sizes can share the same physical file.
$moved_files = array();
foreach ( $metadata['sizes'] as $size => $size_info ) {
if ( in_array( $size_info['file'], $moved_files, true ) ) {
continue;
}
$old_thumb = $base_dir . '/' . $size_info['file'];
$new_thumb = $new_base_dir . '/' . $size_info['file'];
if ( ! $wp_filesystem->exists( $old_thumb ) ) {
error_log( 'Registered metadata thumbnail file does not exist. Skipping. ' . $old_thumb );
continue;
}
if ( ! $wp_filesystem->move( $old_thumb, $new_thumb ) ) {
error_log( 'Unable to move protected file. Thumbnail moving failed: ' . $old_thumb . ' to ' . $new_thumb );
// Move the file back along with any thumbnails we already moved.
$wp_filesystem->move( $new_file, $file );
foreach ( $moved_files as $moved_file ) {
$old_thumb = $base_dir . '/' . $moved_file;
$new_thumb = $new_base_dir . '/' . $moved_file;
if ( $wp_filesystem->exists( $new_thumb ) ) {
$wp_filesystem->move( $new_thumb, $old_thumb );
}
}
return false;
}
$moved_files[] = $size_info['file'];
}
}
// Update attachment location in database.
update_attached_file( $attachment_id, $new_file );
// This only exists with images it seems.
if ( array_key_exists( 'file', $metadata ) ) {
$metadata['file'] = ltrim( $protected_dir, '/' ) . $metadata['file'];
wp_update_attachment_metadata( $attachment_id, $metadata );
}
$protector->add_authorization_meta_to_media_post( $attachment_id );
return true;
}
error_log( 'Unable to move protected file, check permissions on the protected directory or existing file with the same name: ' . $file );
return false;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 9.0.0 | Introduced. |