Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Created October 29, 2013 11:03
Show Gist options
  • Select an option

  • Save wokamoto/7212611 to your computer and use it in GitHub Desktop.

Select an option

Save wokamoto/7212611 to your computer and use it in GitHub Desktop.
[WordPress] 年月別にディレクトリに保存していなかったファイルを年月別に保存しなおしちゃう奴 2行目、/path/to/wordpress/ を修正して使ってね
<?php
require_once('/path/to/wordpress/wp-load.php');
// file permision
$perms = 0655;
global $_wp_additional_image_sizes;
$siteurl = get_option( 'siteurl' );
$upload_path = get_option( 'upload_path' );
if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
$dir = trailingslashit( WP_CONTENT_DIR . '/uploads' );
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH
$dir = trailingslashit( path_join( ABSPATH, $upload_path ) );
} else {
$dir = trailingslashit( $upload_path );
}
if ( !$url = get_option( 'upload_url_path' ) ) {
if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
$url = trailingslashit( WP_CONTENT_URL . '/uploads' );
else
$url = trailingslashit( trailingslashit( $siteurl ) . $upload_path );
}
$time_start = microtime(true);
$attachments = $wpdb->get_results($wpdb->prepare(
"SELECT ID, guid, post_date FROM {$wpdb->posts} WHERE POST_TYPE = %s",
'attachment'));
foreach($attachments as $attachment) {
$attachment_url = wp_get_attachment_url($attachment->ID);
$attached_file = get_post_meta( $attachment->ID, '_wp_attached_file', true);
if ( !preg_match('#^20[0-9][0-9]/(0[1-9]|1[0-2])/[^/]+$#', $attached_file) && file_exists($dir.$attached_file) ) {
$file = $attached_file;
//$file_time = date('Y/m', filemtime($dir.$file));
$file_time = date('Y/m', strtotime($attachment->post_date));
$path_info = pathinfo($file);
$imagedata = wp_get_attachment_metadata( $attachment->ID );
echo "-- {$attachment->ID} : ({$file_time}) : {$dir}{$file} --\n";
if ( move_image( $dir, $url, $file_time, $file, $perms ) ) {
foreach ($_wp_additional_image_sizes as $size => $data) {
$image_size = image_get_intermediate_size( $attachment->ID, $size );
if( $image_size && isset($image_size['file']) && file_exists($dir.basename($image_size['file'])) ) {
$thumb = basename($image_size['file']);
move_image( $dir, $url, $file_time, $thumb, $perms );
}
}
$thumbs_list = scan_file( $dir, '{'.$path_info['filename'].'-*}', false);
foreach ( $thumbs_list as $thumb ) {
$thumb = basename($thumb);
move_image( $dir, $url, $file_time, $thumb, $perms );
}
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->posts} SET guid = %s WHERE ID = %d",
$url.$file_time.'/'.$file,
intval($attachment->ID)
));
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE post_id = %d and meta_key = %s",
$file_time.'/'.$file,
intval($attachment->ID),
'_wp_attached_file'
));
}
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf("--- %01.2f sec ---\n", $time);
function move_image( $dir, $url, $file_time, $file, $perms ) {
global $wpdb;
if ( !file_exists($dir.$file) )
return false;
$new_file = trailingslashit($file_time).$file;
if ( !file_exists($dir.$file_time) )
mkdir( $dir.$file_time, $perms, true);
rename( $dir.$file, $dir.$new_file );
chmod( $dir.$new_file, $perms );
$wpdb->query($wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_content = replace(post_content, %s, %s) where post_content like %s",
$url.$file,
$url.$new_file,
"%{$url}{$file}%"
));
return $new_file;
}
function scan_file($dir, $target, $recursive = false) {
$list = $tmp = array();
if ($recursive) {
foreach(glob($dir . '*/', GLOB_ONLYDIR) as $child_dir) {
if ($tmp = scan_file($child_dir, $target, $recursive)) {
$list = array_merge($list, $tmp);
}
}
}
foreach(glob($dir . $target, GLOB_BRACE) as $image) {
$list[] = $image;
}
return $list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment