Last active
January 24, 2017 01:13
-
-
Save LinzardMac/b8af908beb323907e8525119aeb815cb to your computer and use it in GitHub Desktop.
Find base64 images embedded in the post_content, download the image, sideload the image, and replace the base64 with the image file path and update the post_content
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
global $wpdb; | |
$requested_posts = $wpdb->get_results( "SELECT * FROM `wp_posts` WHERE `wp_posts`.`post_status` = 'publish' AND `wp_posts`.`post_type` = 'post' AND post_content LIKE '%base64%' LIMIT 9999999"); | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
); | |
$updated = array(); | |
$unique_related_imgReplace = array(); | |
// get all posts in the features post type | |
$new_query = array(); | |
$file_returned_array = array(); | |
foreach ( $requested_posts as $post) { | |
$count = 0; | |
update_post_meta($post->ID, '_backedup_post_content', $post->post_content); | |
$callback = function($matches) use (&$post, &$count){ | |
$code = $matches[1]; | |
$count++; | |
$decoded = base64_decode($code) ; | |
$filename = $post->post_name.'_img_'.$count.'.jpg'; | |
$hashed_filename = md5( $filename . microtime() ) . '_' . $filename; | |
$upload_dir = wp_upload_dir(); | |
$upload_path = $upload_dir['basedir'].'/tmp/'; | |
$image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded ); | |
$file_info[$post->ID]['filename'] = $filename; | |
$file_info[$post->ID]['upload_dir'] = $upload_dir; | |
$file_info[$post->ID]['hashed'] = $hashed_filename; | |
$file_info[$post->ID]['image_upload'] = $image_upload; | |
$file_info[$post->ID]['upload_path']= $upload_path; | |
$file_array = array(); | |
$file_array['error'] = 0; | |
$file_array['name'] = basename($filename); | |
$file_array['tmp_name'] = $upload_path.$hashed_filename; | |
$file_array['type'] = 'image/jpg'; | |
$file_array['size'] = filesize( $upload_path . $hashed_filename ); | |
//HANDLE UPLOADED FILE | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
require_once(ABSPATH . '/wp-admin/includes/media.php'); | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
$attachment_data = array( | |
'post_title' => basename($filename), | |
); | |
$file_return = media_handle_sideload( $file_array, $post->ID, null, $attachment_data ); | |
$file_returned_array[$post->ID][] = $file_return; | |
//if( $file_return == 0 ){ wp_die(var_dump($file_return)); } | |
$attachment_url = wp_get_attachment_url( $file_return ); | |
return $attachment_url; | |
}; | |
$replaced = preg_replace_callback('/data[^,]+(?<=base64),([^")]*)/', $callback, $post->post_content); | |
$my_post = array( | |
'ID' => $post->ID, | |
'post_content' => $replaced, | |
); | |
wp_update_post( $my_post ); | |
} // foreach features | |
$resp = array( | |
'success' => true, | |
$file_returned_array | |
); | |
wp_sent_json($resp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment