Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. <?php
  2. global $wpdb;
  3. while($bad_posts = $wpdb->get_results("Select * from wp_posts where post_content like '%;base64,/9j%'") AND count($bad_posts) > 0){
  4. $bad_post = $bad_posts[0];
  5.  
  6. $bad_content = $bad_post->post_content;
  7. preg_match('~data:image/jpeg;base64,(/9j/.*/Z)~',$bad_content,$matches,PREG_OFFSET_CAPTURE);
  8. $media = image_upload('base64','',$matches[1][0]);
  9.  
  10.  
  11. $attach_id = wp_insert_attachment( $media, $media->file_name, $bad_post->ID );
  12. $attach_data = wp_generate_attachment_metadata( $attach_id, $media->file_name );
  13. wp_update_attachment_metadata( $attach_id, $attach_data );
  14.  
  15. $bad_post->post_content = str_replace($matches[0][0], $media['guid'],$bad_post->post_content );
  16. // Insert the post into the database
  17. $result = wp_update_post( $bad_post );
  18.  
  19.  
  20. echo $result;
  21. }
  22.  
  23.  
  24.  
  25. function image_upload($type,$name='',$data) {//returns attachment id or false
  26.  
  27. $upload_dir = wp_upload_dir();
  28. $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
  29. switch ($type){
  30. case 'base64':
  31. $decoded = base64_decode($data) ;
  32.  
  33. $filename = ($name == '')? 'img.png' : $name;
  34. $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
  35.  
  36. $image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
  37. var_dump($hashed_filename, $image_upload);
  38.  
  39. //HANDLE UPLOADED FILE
  40. $file = array();
  41. $file['error'] = '';
  42. $file['tmp_name'] = $upload_path . $hashed_filename;
  43. $file['name'] = $hashed_filename;
  44. $file['type'] = 'image/png';
  45. $file['size'] = filesize( $upload_path . $hashed_filename );
  46.  
  47. // upload file to server
  48. // @new use $file instead of $image_upload
  49. $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
  50.  
  51. $filename = $file_return['file'];
  52. $attachment = array(
  53. 'post_mime_type' => $file_return['type'],
  54. 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
  55. 'post_content' => '',
  56. 'post_status' => 'inherit',
  57. 'guid' => $upload_dir['url'] . '/' . basename($filename),
  58. 'file_name' => $filename
  59. );
  60.  
  61. return $attachment;
  62.  
  63. break;
  64.  
  65.  
  66. case 'url':
  67. break;
  68.  
  69. default:
  70. return FALSE;
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement