Advertisement
bowenac

ajax file

May 28th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.53 KB | None | 0 0
  1. <?php
  2. include '../../../wp-load.php';
  3. global $post;
  4. global $wpdb;
  5. $dir = plugin_dir_path( __FILE__ );
  6. $posts = $wpdb->prefix . "posts";
  7. $postmeta = $wpdb->prefix . "postmeta";
  8. $published_posts = wp_count_posts()->publish;
  9. echo "Total Post: $published_posts </br>";
  10. $getPosts = "SELECT * FROM $posts WHERE post_type = 'post' AND post_title != 'Auto Draft'";
  11. $postresults = $wpdb->get_results($getPosts);
  12. $i = 0;
  13. foreach( $postresults as $postresult ) {
  14.     $imageId = $postresult->ID;
  15.     $postTitle = $postresult->post_title;  
  16.     $imagesPath = $postresult->guid;
  17.     $postContent = $postresult->post_content;
  18.     //Check if post has attachment if not create the attachment and featured image from the first url in content.
  19.     if (has_post_thumbnail( $imageId )) {
  20.         $i++;
  21.     }
  22.     else{
  23.     $string = $postContent;
  24.     preg_match('!http://.+\.(?:jpe?g|png|gif)!Ui', $string, $match);
  25.     $c = str_replace("wp-content/themes/prologue/phpThumb/phpThumb.php?src=../../../../", "",  $match[0]);//Actual URL to the image in the uploads folder minus phpThumb
  26.  
  27.         if ( preg_match('/\s/', $c) ){//if else to check if the basename has spaces as we don't need to rename the file if it does not to save resources.
  28.         $newextension = substr(strrchr($c,'.'),1);
  29.         $eachFileFolder = preg_replace( '#\S+uploads\/(\S+)#', '$1', $c );//break the string up around the "/" character in $mystring
  30.         $neweachFileFolder = preg_replace('#[^/]*$#', '', $eachFileFolder);
  31.  
  32.         $wp_upload_dir = wp_upload_dir();//get upload dir
  33.         $old = $wp_upload_dir['basedir']."/".$eachFileFolder;
  34.         $new = $wp_upload_dir['basedir']."/".$neweachFileFolder.$imageId.".".$newextension;
  35.  
  36.         rename($old, $new);
  37.         var_dump($old);
  38.         var_dump($new);
  39.  
  40.         $newimage = preg_replace('#[^/]*$#', '', $c);
  41.         $filename = $newimage.$imageId.".".$newextension;
  42.         }
  43.         else{
  44.         // $filename should be the path to a file in the upload directory.
  45.         $filename = $c;
  46.         $wp_upload_dir = wp_upload_dir();//get upload dir
  47.         var_dump($c);
  48.         }
  49.         //Need to end if else here... if the image has spaces
  50.  
  51.     // The ID of the post this attachment is for.
  52.     $imageIds = $imageId;
  53.     // Check the type of tile. We'll use this as the 'post_mime_type'.
  54.     $filetype = wp_check_filetype( basename( $filename ), null );
  55.     // Get the path to the upload directory.
  56.     /*$wp_upload_dir = wp_upload_dir();*/
  57. /*    var_dump($wp_upload_dir['baseurl']);*/
  58.     // Prepare an array of post data for the attachment.
  59.     $attachment = array(
  60.         'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
  61.         'post_mime_type' => $filetype['type'],
  62.         'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
  63.         'post_content'   => '',
  64.         'post_status'    => 'inherit'
  65.     );
  66.     // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
  67.     require_once( ABSPATH . 'wp-admin/includes/image.php' );
  68.     // Generate the metadata for the attachment, and update the database record.
  69.  
  70. /*    if (false !== $attachment['post_mime_type']) {
  71.     $attach_id = wp_insert_attachment( $attachment, $filename, $imageIds );    
  72.     $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  73.     wp_update_attachment_metadata( $attach_id, $attach_data );
  74.     update_post_meta($imageIds, '_thumbnail_id', $attach_id);
  75.     }*/
  76.  
  77.     //Update post content with new post content
  78.  
  79.  
  80. /*    $string = preg_replace('/<a\s[^>]*?href="(.*)"><img\s[^>]*?src="([^\"]+)"[^>]*><\/a>/', '', $postContent, 1);
  81.     $my_post = array(
  82.       'ID'           => $imageId,
  83.       'post_content' => $string
  84.     );
  85.     // Update the post into the database
  86.       wp_update_post( $my_post );
  87.       echo "Processing post $postTitle, Added image for post $imageId to the library, file name $filename </br>";*/
  88.       $i++;
  89.     }    
  90.     //Emd Else
  91.     //End Update post content with new post content
  92. }
  93.  
  94. outputProgress($i, $published_posts);
  95. function outputProgress($i, $published_posts) {
  96.     echo "<span style='position: absolute;z-index:$current;background:#FFF;'>" . round($i / $published_posts * 100) . "% </span>";
  97.     myFlush();
  98.     sleep(1);
  99. }
  100.  
  101. /**
  102.  * Flush output buffer
  103.  */
  104. function myFlush() {
  105.     echo(str_repeat(' ', 256));
  106.     if (@ob_get_contents()) {
  107.         @ob_end_flush();
  108.     }
  109.     flush();
  110. }
  111.  
  112. /*    echo "Processed $i </br>";
  113.     $percent = ($i * 100) / $published_posts;
  114.     echo $percent;*/
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement