Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- global $wpdb;
- while($bad_posts = $wpdb->get_results("Select * from wp_posts where post_content like '%;base64,/9j%'") AND count($bad_posts) > 0){
- $bad_post = $bad_posts[0];
- $bad_content = $bad_post->post_content;
- preg_match('~data:image/jpeg;base64,(/9j/.*/Z)~',$bad_content,$matches,PREG_OFFSET_CAPTURE);
- $media = image_upload('base64','',$matches[1][0]);
- $attach_id = wp_insert_attachment( $media, $media->file_name, $bad_post->ID );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $media->file_name );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- $bad_post->post_content = str_replace($matches[0][0], $media['guid'],$bad_post->post_content );
- // Insert the post into the database
- $result = wp_update_post( $bad_post );
- echo $result;
- }
- function image_upload($type,$name='',$data) {//returns attachment id or false
- $upload_dir = wp_upload_dir();
- $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
- switch ($type){
- case 'base64':
- $decoded = base64_decode($data) ;
- $filename = ($name == '')? 'img.png' : $name;
- $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
- $image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
- var_dump($hashed_filename, $image_upload);
- //HANDLE UPLOADED FILE
- $file = array();
- $file['error'] = '';
- $file['tmp_name'] = $upload_path . $hashed_filename;
- $file['name'] = $hashed_filename;
- $file['type'] = 'image/png';
- $file['size'] = filesize( $upload_path . $hashed_filename );
- // upload file to server
- // @new use $file instead of $image_upload
- $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
- $filename = $file_return['file'];
- $attachment = array(
- 'post_mime_type' => $file_return['type'],
- 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
- 'post_content' => '',
- 'post_status' => 'inherit',
- 'guid' => $upload_dir['url'] . '/' . basename($filename),
- 'file_name' => $filename
- );
- return $attachment;
- break;
- case 'url':
- break;
- default:
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement