Skip to content

Instantly share code, notes, and snippets.

@gdarko
Created December 26, 2018 09:43
Show Gist options
  • Save gdarko/feee282c09b3549e17f8d995f3332f30 to your computer and use it in GitHub Desktop.
Save gdarko/feee282c09b3549e17f8d995f3332f30 to your computer and use it in GitHub Desktop.
Import images to media library from specific directory.
<?php
/**
* Class DG_Image_Import
* @author Darko Gjorgjijoski <dg@darkog.com>
* @license GPLv2
* @copyright 2019
*
* Handles import of images from a specific subfolder of your root site dir (eg public_html/your_data_dir) to WordPress media library.
* Has basic lock/unlock functionality to avoid colisions.
*
*/
class DG_Image_Import {
const ROOT_DIR = ABSPATH;
const DATA_DIR = 'your_data_dir';
/**
* Scan directory for files
* @return array
*/
public function scan_dir() {
$data_dir = self::get_data_dir() . DIRECTORY_SEPARATOR;
return glob( "{$data_dir}*.{jpg,png,gif}", GLOB_BRACE );
}
/**
* Handles import of all the files.
* @return bool|int (false or total imported)
* @throws Exception
*/
public function import() {
if ( $this->is_locked() ) {
throw new Exception( 'Already working...' );
}
$files = $this->scan_dir();
if ( ! empty( $files ) && count( $files ) > 0 ) {
@set_time_limit(0);
$total_imported = 0;
$this->lock();
foreach ( $files as $file ) {
if ( $this->handle_file_import( $file ) ) {
$total_imported ++;
}
}
$this->unlock();
return $total_imported;
}
return 0;
}
/**
* Handles import of single file
*
* @param $file
*
* @return bool
*/
public function handle_file_import( $file ) {
$filename = basename( $file );
$upload_file = wp_upload_bits( $filename, null, file_get_contents( $file ) );
if ( ! $upload_file['error'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'] );
if ( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
$this->remove_file($file);
return true;
}
}
return false;
}
public function remove_file($file) {
unlink($file);
}
/**
* Is the dir locked?
* @return bool
*/
public function is_locked() {
return file_exists( $this->get_lock_file_path() );
}
/**
* Lock the dir to avoid collisions
* @return bool
*/
public function lock() {
return touch( $this->get_lock_file_path() );
}
/**
* Unlock the dir
* @return bool
*/
public function unlock() {
return unlink( $this->get_lock_file_path() );
}
/**
* Return the root dir
* @return string
*/
public static function get_root_dir() {
return self::ROOT_DIR;
}
/**
* Return the data dir
* @return string
*/
public static function get_data_dir() {
return self::get_root_dir() . self::DATA_DIR;
}
/**
* Get lock file path
* @return string
*/
public static function get_lock_file_path() {
return self::get_data_dir() . DIRECTORY_SEPARATOR . '.locked';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment