0){
$pl_error['upload_error'] = __('There was some error uploading the file ! Please try again.');
pagelayer_media_replace_theme();
return;
}
$filedata = wp_check_filetype_and_ext($_FILES['userfile']['tmp_name'], $_FILES['userfile']['name']);
if ($filedata['ext'] == false){
$pl_error['ext_error'] = __('The File type could not be determined. Please upload a permitted file type.');
pagelayer_media_replace_theme();
return;
}
$result = pagelayer_replace_attachment($_FILES['userfile']['tmp_name'], $post_id, $err);
if(empty($result)){
$pl_error['replace_error'] = $err;
pagelayer_media_replace_theme();
return;
}
$redirect_success = admin_url('post.php');
$redirect_success = add_query_arg(array(
'action' => 'edit',
'post' => $post_id,
), $redirect_success);
echo '';
}
// Show the theme
pagelayer_media_replace_theme();
}
// Theme of the page
function pagelayer_media_replace_theme(){
global $pl_error;
pagelayer_report_error($pl_error);echo '
';
$id = (int) $_GET['id'];
?>
0){
chmod( $targetFile, $permissions ); // restore permissions
}
$updated = update_attached_file($post_id, $targetFile);
$target_url = wp_get_attachment_url($post_id);
// Run the filter, so other plugins can hook if needed.
$filtered = apply_filters( 'wp_handle_upload', array(
'file' => $targetFile,
'url' => $target_url,
'type' => $fileMime,
), 'sideload');
// Check if file changed during filter. Set changed to attached file meta properly.
if (isset($filtered['file']) && $filtered['file'] != $targetFile ){
update_attached_file($post_id, $filtered['file']);
}
$metadata = wp_generate_attachment_metadata($post_id, $targetFile);
wp_update_attachment_metadata($post_id, $metadata);
return true;
}
function pagelayer_remove_attahment_files($post_id){
$meta = wp_get_attachment_metadata( $post_id );
if (function_exists('wp_get_original_image_path')){ // WP 5.3+
$fullfilepath = wp_get_original_image_path($post_id);
}else{
$fullFilePath = trim(get_attached_file($post_id, apply_filters( 'pagelayer_unfiltered_get_attached_file', true )));
}
$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
$file = $fullFilePath;
$result = wp_delete_attachment_files($post_id, $meta, $backup_sizes, $file );
// If attached file is not the same path as file, this indicates a -scaled images is in play.
$attached_file = get_attached_file($post_id);
if ($file !== $attached_file && file_exists($attached_file)){
@unlink($attached_file);
}
}