Skip to content

Instantly share code, notes, and snippets.

@ScreamingDev
Created November 3, 2014 14:11
Show Gist options
  • Save ScreamingDev/3036f9d3ada59b852c00 to your computer and use it in GitHub Desktop.
Save ScreamingDev/3036f9d3ada59b852c00 to your computer and use it in GitHub Desktop.
Import images in the uploads folder to the WordPress Media
<?php
define( 'DOING_AJAX', true );
define( 'WP_ADMIN', true );
define( 'WP_IMPORTING', true );
$err = array();
/** Load WordPress Bootstrap */
require_once '../../../../wp-load.php';
require_once '../../../../wp-admin/includes/image.php';
//Handle an individual file import.
function handle_import_file( $file, $post_id = 0, $import_date = 'file' ) {
set_time_limit( 120 );
// Initially, Base it on the -current- time.
$time = current_time( 'mysql', 1 );
// Next, If it's post to base the upload off:
if ( 'post' == $import_date && $post_id > 0 ) {
$post = get_post( $post_id );
if ( $post && substr( $post->post_date_gmt, 0, 4 ) > 0 ) {
$time = $post->post_date_gmt;
}
} elseif ( 'file' == $import_date ) {
$time = gmdate( 'Y-m-d H:i:s', filemtime( $file ) );
}
// A writable uploads dir will pass this test. Again, there's no point overriding this one.
if ( ! ( ( $uploads = wp_upload_dir( $time ) )
&& false === $uploads['error'] )
) {
return new WP_Error( 'upload_error', $uploads['error'] );
}
$wp_filetype = wp_check_filetype( $file, null );
extract( $wp_filetype );
//Is the file allready in the uploads folder?
if ( preg_match( '|^' . preg_quote( str_replace( '\\',
'/',
$uploads['basedir'] ) ) . '(.*)$|i',
$file,
$mat ) ) {
$filename = basename( $file );
$new_file = $file;
$url = $uploads['baseurl'] . $mat[1];
$attachment = get_posts( array(
'post_type' => 'attachment',
'meta_key' => '_wp_attached_file',
'meta_value' => ltrim( $mat[1], '/' )
) );
if ( ! empty( $attachment ) ) {
return new WP_Error( 'file_exists',
__( 'Sorry, That file already exists in the WordPress media library.',
'add-from-server' ) );
}
//Ok, Its in the uploads folder, But NOT in WordPress's media library.
if ( 'file' == $import_date ) {
$time = filemtime( $file );
if ( preg_match( "|(\d+)/(\d+)|",
$mat[1],
$datemat ) ) { //So lets set the date of the import to the date folder its in, IF its in a date folder.
$hour = $min = $sec = 0;
$day = 1;
$year = $datemat[1];
$month = $datemat[2];
// If the files datetime is set, and it's in the same region of upload directory, set the minute details to that too, else, override it.
if ( $time && date( 'Y-m', $time ) == "$year-$month" ) {
list( $hour, $min, $sec, $day ) = explode( ';',
date( 'H;i;s;j', $time ) );
}
$time = mktime( $hour, $min, $sec, $month, $day, $year );
}
$time = gmdate( 'Y-m-d H:i:s', $time );
// A new time has been found! Get the new uploads folder:
// A writable uploads dir will pass this test. Again, there's no point overriding this one.
if ( ! ( ( $uploads = wp_upload_dir( $time ) )
&& false === $uploads['error'] )
) {
return new WP_Error( 'upload_error', $uploads['error'] );
}
$url = $uploads['baseurl'] . $mat[1];
}
} else {
$filename = wp_unique_filename( $uploads['path'], basename( $file ) );
// copy the file to the uploads dir
$new_file = $uploads['path'] . '/' . $filename;
if ( false === copy( $file, $new_file ) ) {
return new WP_Error( 'upload_error',
sprintf( __( 'The selected file could not be copied to %s.',
'add-from-server' ),
$uploads['path'] ) );
}
// Set correct file permissions
$stat = stat( dirname( $new_file ) );
$perms = $stat['mode'] & 0000666;
chmod( $new_file, $perms );
// Compute the URL
$url = $uploads['url'] . '/' . $filename;
if ( 'file' == $import_date ) {
$time = gmdate( 'Y-m-d H:i:s', filemtime( $file ) );
}
}
//Apply upload filters
$return = apply_filters( 'wp_handle_upload',
array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
$new_file = $return['file'];
$url = $return['url'];
$type = $return['type'];
$title = preg_replace( '!\.[^.]+$!', '', basename( $file ) );
$content = '';
// use image exif/iptc data for title and caption defaults if possible
if ( $image_meta = wp_read_image_metadata( $new_file ) ) {
if ( '' != trim( $image_meta['title'] ) ) {
$title = trim( $image_meta['title'] );
}
if ( '' != trim( $image_meta['caption'] ) ) {
$content = trim( $image_meta['caption'] );
}
}
if ( $time ) {
$post_date_gmt = $time;
$post_date = $time;
} else {
$post_date = current_time( 'mysql' );
$post_date_gmt = current_time( 'mysql', 1 );
}
// Construct the attachment array
$attachment = array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_name' => $title,
'post_content' => $content,
'post_date' => $post_date,
'post_date_gmt' => $post_date_gmt
);
$attachment = apply_filters( 'afs-import_details',
$attachment,
$file,
$post_id,
$import_date );
//Win32 fix:
$new_file = str_replace( strtolower( str_replace( '\\',
'/',
$uploads['basedir'] ) ),
$uploads['basedir'],
$new_file );
// Save the data
$id = wp_insert_attachment( $attachment, $new_file, $post_id );
if ( ! is_wp_error( $id ) ) {
$data = wp_generate_attachment_metadata( $id, $new_file );
wp_update_attachment_metadata( $id, $data );
}
//update_post_meta( $id, '_wp_attached_file', $uploads['subdir'] . '/' . $filename );
return $id;
}
$wp_upload_dir = wp_upload_dir();
$files = glob( $wp_upload_dir['basedir'] . '/*' );
/** @var \wpdb $wpdb */
global $wpdb;
foreach ( $files as $file ) {
if ( false === exif_imagetype( $file ) ) {
printf(
__( 'Not an image: %s' ) . PHP_EOL,
basename( $file )
);
continue;
}
if (preg_match('/.*\-(\d*x\d*)\.\w{3,4}$/', basename($file))) {
// printf(
// __( 'Found thumbnail. Next!' ) . PHP_EOL,
// basename( $file )
// );
// silent next on thumbnail
continue;
}
printf(
__( 'Importing %s ...' ) . PHP_EOL,
basename( $file )
);
$col = $wpdb->get_col(
$wpdb->prepare( "
SELECT *
FROM wp_posts
WHERE post_type = 'attachment'
AND guid = %s
",
$wp_upload_dir['baseurl'] . '/' . basename( $file )
)
);
if ( $col ) {
printf(
__( 'Already given: %s' ) . PHP_EOL,
basename( $file )
);
continue;
}
handle_import_file($file);
printf(
__( 'Imported %s !' ) . PHP_EOL,
basename( $file )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment