Skip to content

Instantly share code, notes, and snippets.

@VR51
Created October 29, 2021 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VR51/b6581a1e68c3bc32cf7d1c056d839c8c to your computer and use it in GitHub Desktop.
Save VR51/b6581a1e68c3bc32cf7d1c056d839c8c to your computer and use it in GitHub Desktop.
Schedule the regular import of files into a WordPress site's wp-content/uploads/ directory
<?php
/**
* Import Files from External Domains v1.0.0
*
* This script is written for WordPress sites.
* This script can be adapted for use with non WordPress sites.
* The code in this script should be copied and pasted into a theme's funcitons.php file or into a functionality plugin
* though it can be used in other ways too.
* This script does not add files to the WordPress Media Library.
*
* Read the comments. You must configure the script or it won't do anything useful.
*
* Files are downloaded to wp-content/uploads/$dirname/
* Currently configured as wp-content/uploads/importedfiles/
* Download location may be different in multisites to e.g. wp-content/uploads/sites/3/$dirname/
*
* Old copies of imported files are deleted before new copies are imported.
* They are deleted one-by-one, not all at the same time.
*
* Copyright Lee Hodson (@vr51) 2021
*
**/
function scheduled_file_import_vr51() {
// Configure location of files that are to be imported
// Add to this array your list of files to be imported. Any file type though must be consistent for all files.
// Warning: You're responsibile for ensuring the files do not pose a security risk
// Tip: Google Sheets adds 'gviz/tq?tqx=out:csv' to sheets exported as CSV files;
$importl = array(
'https://example.com/file1.csv',
'https://example.com/file2.csv',
'https://example.com/file3.csv' // no comma for the last file
);
// Configure the file type extension
// This is used by CURL when the file is saved
$ext = 'csv' // File extension
// Optional: Configure the name of directory within wp-uploads that imported files will be downloaded to.
$dirname = 'importedfiles'; // No leading or trailing forward slash. Enter directory name only.
// Sometimes required for $wp_filesystem to be accessed.
// Uncomment the next line if downloads do not happen. This error probably means $wp_filesystem is inaccessible
// require_once( ABSPATH . 'wp-admin/includes/file.php' ); // this line is needed because this is a functionality plugin
/**
* Download and store files that will be imported
*/
// Set the download directory
$upload_dir = wp_upload_dir();
$dir = trailingslashit( $upload_dir['basedir'] ) . $direname . '/';
global $wp_filesystem;
WP_Filesystem();
// Create $dir if it needs to be created
if( ! $wp_filesystem->is_dir( $dir ) ) {
$wp_filesystem->mkdir( $dir );
}
$site = get_site_url(); // Needed for multisite compatibility
// Fetch the $importl files
$n = 1;
foreach ($importl as $ifile) {
// Delete previously imported $file if one exists
unlink( $dir . $n . "import.csv" );
// Fetch new file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ifile);
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_REFERER, $site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$csv = curl_exec($ch);
curl_close($ch);
// Import the new file and set file permissions to 0644
$wp_filesystem->put_contents( $dir . $n . 'import.' . $ext, $csv, 0644 );
$n = $n+1;
}
}
// Set/Stop the cron event
// Comment out these 5 lines to stop the import cron event
add_action( 'scheduled_file_import_cron', 'scheduled_file_import_vr51' );
if ( ! wp_next_scheduled( 'scheduled_file_import_cron' ) ) {
// Configure the import time and frequency
wp_schedule_event( time(), 'daily', 'scheduled_file_import_cron' );
}
// Uncomment the next two lines to delete the scheduled import cron event.
// $timestamp = wp_next_scheduled( 'scheduled_file_import_cron' );
// wp_unschedule_event( $timestamp, 'scheduled_file_import_cron' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment