So, habe den Code überarbeitet:
PHP
function _move_file_dir_meta_field_content( $post ) {
$up_dirs = wp_upload_dir(null,false);
print_r($up_dirs);
$cur_url = trim($post->guid);
$cur_baseurl = substr($cur_url, 0, strlen(trim($up_dirs['baseurl'])));
if (strcmp(trim($up_dirs['baseurl']),$cur_baseurl)!=0) {
echo '<p>Base-URL abweichend, Datei kann nicht verschoben werden!</p>';
return;
}
$cur_relname = substr($cur_url, strlen(trim($up_dirs['baseurl'])));
while ($cur_relname[0]=='/') $cur_relname = substr($cur_relname, 1);
$cur_metaname = get_metadata('post', $post->ID, '_wp_attached_file', true);
if(strcmp($cur_relname,$cur_metaname)!=0) {
echo '<p>Metadaten und GUID inkonsistent, Datei kann nicht verschoben werden!</p>';
return;
}
$cur_meta = get_metadata('post', $post->ID, '_wp_attachment_metadata', true);
if(is_array($cur_meta) && isset($cur_meta['file'])) {
if(strcmp($cur_relname,$cur_meta['file'])!=0) {
echo '<p>Metadaten inkonsistent, Datei kann nicht verschoben werden!</p>';
return;
}
echo '<input type="hidden" id="move-uploaded-file-dir-attachmeta" name="move-uploaded-file-dir-attachmeta" value="1" />';
}
else {
echo '<input type="hidden" id="move-uploaded-file-dir-attachmeta" name="move-uploaded-file-dir-attachmeta" value="0" />';
}
$cur_filename = $cur_relname;
$cur_relpath = '/';
if(strrpos($cur_relname,'/')!==false) {
$splitpos = strrpos($cur_relname,'/');
$cur_relpath .= substr($cur_relname,0,$splitpos+1);
$cur_filename = substr($cur_relname,$splitpos+1);
}
$dir_options = array($cur_relpath, '/');
$yearmonth = '/'.substr($post->post_date,0,4).'/'.substr($post->post_date,5,2).'/';
if (is_dir($up_dirs['basedir'].$yearmonth)) array_push($dir_options, $yearmonth);
$dirlist = scandir($up_dirs['basedir']);
foreach ($dirlist as &$dir) {
if(!is_numeric($dir) && $dir[0]!='.' && is_dir($up_dirs['basedir'].'/'.$dir)) {
array_push($dir_options, '/'.$dir.'/');
}
}
$dir_options = array_unique($dir_options);
wp_nonce_field( basename( __FILE__ ), 'move-uploaded-file-dir_nonce' );
echo '<ul>';
$i=0;
foreach ($dir_options as &$dir) {
echo '<label><input id="move-uploaded-file-dir-'.$i.'" type="radio" name="move-uploaded-file-dir" value="'.$i.'-'.$dir.'"'.(($i==0) ? ' checked="checked"': '').'>'.$dir.'</label><br />';
$i++;
}
echo '</ul>';
}
function add_move_file_dir_meta_field() {
add_meta_box( '_move_file_dir', 'Verzeichnis', '_move_file_dir_meta_field_content', 'attachment', 'side', 'default' );
}
function move_file_dir_process($post_id) {
if(!isset($_POST['move-uploaded-file-dir_nonce']) || !wp_verify_nonce( $_POST['move-uploaded-file-dir_nonce'], basename( __FILE__ )))
return $post_id;
if(isset($_POST['move-uploaded-file-dir']) && trim($_POST['move-uploaded-file-dir'])[0]!='0') {
// file should be moved
$newdir = trim($_POST['move-uploaded-file-dir']);
$newdir = substr($newdir,strpos($newdir,'/'));
$up_dirs = wp_upload_dir(null,false);
$post = get_post($post_id);
$cur_url = trim($post->guid);
$cur_baseurl = substr($cur_url, 0, strlen(trim($up_dirs['baseurl'])));
if (strcmp(trim($up_dirs['baseurl']),$cur_baseurl)!=0) return $post_id;
$cur_relname = substr($cur_url, strlen(trim($up_dirs['baseurl'])));
while ($cur_relname[0]=='/') $cur_relname = substr($cur_relname, 1);
$cur_filename = $cur_relname;
$cur_relpath = '/';
if(strrpos($cur_relname,'/')!==false) {
$splitpos = strrpos($cur_relname,'/');
$cur_relpath .= substr($cur_relname,0,$splitpos+1);
$cur_filename = substr($cur_relname,$splitpos+1);
}
if (file_exists($up_dirs['basedir'].$newdir.$cur_filename)) return $post_id;
$cur_metaname = get_metadata('post', $post->ID, '_wp_attached_file', true);
if(strcmp($cur_relname,$cur_metaname)!=0) return $post_id;
if (!isset($_POST['move-uploaded-file-dir-attachmeta'])) return $post_id;
if($_POST['move-uploaded-file-dir-attachmeta']=='1') {
$cur_meta = get_metadata('post', $post_id, '_wp_attachment_metadata', true);
if (!is_array($cur_meta) || !isset($cur_meta['file']) || strcmp($cur_relname,$cur_meta['file'])!=0) return $post_id;
if(isset($cur_meta['sizes']) && is_array($cur_meta['sizes'])) {
foreach ($cur_meta['sizes'] as &$thumb) {
$thumb_filename = (strrpos($thumb['file'],'/')!==false) ? substr($thumb['file'],strrpos($thumb['file'],'/')) : $thumb['file'];
if (file_exists($up_dirs['basedir'].$newdir.$thumb_filename)) return $post_id;
}
}
}
if(rename($up_dirs['basedir'].$cur_relpath.$cur_filename, $up_dirs['basedir'].$newdir.$cur_filename)) {
global $wpdb;
if($wpdb->update($wpdb->posts, ['guid' => $up_dirs['baseurl'].$newdir.$cur_filename], ['ID' => $post_id])!=1)
{
rename($up_dirs['basedir'].$newdir.$cur_filename, $up_dirs['basedir'].$cur_relpath.$cur_filename); // rename back
}
else {
$newname = $newdir.$cur_filename;
while ($newname[0]=='/') $newname = substr($newname, 1);
update_metadata('post', $post_id, '_wp_attached_file', $newname);
// now move thumbnails
if($_POST['move-uploaded-file-dir-attachmeta']=='1') {
$cur_meta['file'] = $newname;
if(isset($cur_meta['sizes']) && is_array($cur_meta['sizes'])) {
foreach ($cur_meta['sizes'] as &$thumb) {
$thumb_filename = (strrpos($thumb['file'],'/')!==false) ? substr($thumb['file'],strrpos($thumb['file'],'/')) : $thumb['file'];
$newname = $newdir.$thumb_filename;
while ($newname[0]=='/') $newname = substr($newname, 1);
if(rename($up_dirs['basedir'].'/'.$thumb['file'], $up_dirs['basedir'].'/'.$newname)) {
$thumb['file'] = $newname;
}
}
}
update_metadata('post', $post_id, '_wp_attachment_metadata', $cur_meta);
}
}
}
}
return $post_id;
}
add_action('add_meta_boxes', 'add_move_file_dir_meta_field');
add_action('edit_attachment', 'move_file_dir_process', 10, 1 );
Alles anzeigen
Es werden nun auch die Metadaten "_wp_attached_file" und "_wp_attachment_metadata" aktualisiert, die Thumbnails werden jetzt nicht gesucht sondern aus den Metadaten gelesen und verschoben (und die Metadaten aktualisiert). Damit sollten z.B. JXL-Dateien mitverschoben werden wenn sie als Thumbnail mit aufgeführt sind. Es gibt vorher auch eine Prüfung, ob irgendeine der Dateien schon vorhanden ist, wenn ja wird nichts verschoben.