Created
September 20, 2015 21:58
-
-
Save logoscreative/6e1cd69a5e59d1aadc49 to your computer and use it in GitHub Desktop.
Batch Set External Images in Content as Featured Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Batch Set External Images in Content as Featured Images | |
* | |
* Look for external images shoved into the content, upload them to the Media Library, and assign it as the featured post. | |
* | |
* @package batch_upload_featured | |
* @author Cliff Seal <cliff@evermo.re> | |
* @link http://evermo.re | |
* @copyright 2015 Logos Creative | |
* | |
* @wordpress-plugin | |
* Plugin Name: Batch Set External Images in Content as Featured Images | |
* Plugin URI: http://evermo.re | |
* Description: Look for external images shoved into the content, upload them to the Media Library, and assign it as the featured post. | |
* Version: 1.0.0 | |
* Author: Evermore <cliff@evermo.re> | |
* Author URI: http://evermo.re | |
*/ | |
// If this file is called directly, abort. | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
/** | |
* Plugin class. | |
* | |
* @package batch_upload_featured | |
* @author Cliff Seal <cliff@evermo.re> | |
*/ | |
class batch_upload_featured { | |
/** | |
* Plugin version, used for cache-busting of style and script file references. | |
* | |
* @since 1.0.0 | |
* | |
* @const string | |
*/ | |
const VERSION = '1.0.0'; | |
/** | |
* Instance of this class. | |
* | |
* @since 1.0.0 | |
* | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* Initialize the plugin by setting localization, filters, and administration functions. | |
* | |
* @since 1.0.0 | |
*/ | |
private function __construct() { | |
add_action( 'wp_footer', array( $this, 'batch_upload_featured_batch_media_migrate' ), 35); | |
add_action( 'wp_ajax_batch_upload_featured_do_migration_media', array( $this, 'batch_upload_featured_do_migration_media' ) ); | |
} | |
/** | |
* Return an instance of this class. | |
* | |
* @since 1.0.0 | |
* | |
* @return object A single instance of this class. | |
*/ | |
public static function get_instance() { | |
// If the single instance hasn't been set, set it now. | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
/** | |
* Add the Javascript call to the footer | |
* | |
* @since 1.0.0 | |
* | |
* @return object An instance of the form | |
*/ | |
public function batch_upload_featured_batch_media_migrate() { | |
if ( isset($_GET['batchthumbnail']) && $_GET['batchthumbnail'] === 'run' ) { | |
?> | |
<script> | |
jQuery(document).ready(function () { | |
batch_upload_featured_migration_media(); | |
}); | |
function batch_upload_featured_migration_media() { | |
return jQuery.ajax({ | |
url: "<?php echo admin_url( 'admin-ajax.php' ); ?>", | |
data: { | |
action: 'batch_upload_featured_do_migration_media', | |
time: '<?php echo time(); ?>' | |
}, | |
success: function (response) { | |
batch_upload_featured_migration_media(); | |
console.log(response); | |
}, | |
failure: function (response) { | |
console.log('fail?'); | |
console.log(response); | |
}, | |
cache: false | |
}); | |
} | |
</script> | |
<?php | |
} | |
} | |
public function batch_upload_featured_do_migration_media() { | |
require_once(ABSPATH . 'wp-admin/includes/media.php'); | |
require_once(ABSPATH . 'wp-admin/includes/file.php'); | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
$offset = (int) get_option( 'batch_upload_featured_media_offset_counter', 0 ); | |
if ( !$offset ) { | |
$offset = 0; | |
} | |
$count_posts = wp_count_posts(); | |
$highest = $count_posts->publish; | |
if( $offset > $highest ) { | |
echo 'VICTORY!' . $offset . ' ' . $highest; | |
status_header( 404 ); | |
die(); | |
} | |
$args = array( | |
'posts_per_page' => 5, | |
'offset' => $offset | |
); | |
$allposts = get_posts($args); | |
foreach ( $allposts as $thispost ) { | |
if ( !has_post_thumbnail($thispost->ID) ) { | |
preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $thispost->post_content, $thispostsimages ); | |
if ( $thispostsimages[1] ) { | |
foreach ( $thispostsimages[1] as $oneimagetag ) { | |
$fullsizeimage = preg_replace('/-[0-9]{1,4}x[0-9]{1,4}/i', '', $oneimagetag); | |
if ( file_exists($fullsizeimage) ) { | |
$oneimagetag = $fullsizeimage; | |
} | |
$image = media_sideload_image($oneimagetag, $thispost->ID); | |
if ( !is_wp_error($image) ) { | |
$attachedimages = get_attached_media( 'image', $thispost->ID ); | |
if ( $attachedimages ) { | |
$i = 0; | |
foreach ( $attachedimages as $attachedimage ) { | |
if ( $i === 0 ) { | |
$postthumbnail = set_post_thumbnail( $thispost->ID, $attachedimage->ID ); | |
if ( $postthumbnail ) { | |
echo 'Changed post thumbnail on post ' . $postthumbnail . '. '; | |
} else { | |
echo 'Did not change post thumbnail on post ' . $thispost->ID . '. '; | |
} | |
} | |
$i++; | |
} | |
} | |
} | |
} | |
} | |
} | |
$offset++; | |
} | |
update_option( 'batch_upload_featured_media_offset_counter', $offset ); | |
status_header( 200 ); | |
die(); | |
} | |
} | |
batch_upload_featured::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment