Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include '../../../wp-load.php';
- global $post;
- global $wpdb;
- $dir = plugin_dir_path( __FILE__ );
- $posts = $wpdb->prefix . "posts";
- $postmeta = $wpdb->prefix . "postmeta";
- $published_posts = wp_count_posts()->publish;
- echo "Total Post: $published_posts </br>";
- $getPosts = "SELECT * FROM $posts WHERE post_type = 'post' AND post_title != 'Auto Draft'";
- $postresults = $wpdb->get_results($getPosts);
- $i = 0;
- foreach( $postresults as $postresult ) {
- $imageId = $postresult->ID;
- $postTitle = $postresult->post_title;
- $imagesPath = $postresult->guid;
- $postContent = $postresult->post_content;
- //Check if post has attachment if not create the attachment and featured image from the first url in content.
- if (has_post_thumbnail( $imageId )) {
- $i++;
- }
- else{
- $string = $postContent;
- preg_match('!http://.+\.(?:jpe?g|png|gif)!Ui', $string, $match);
- $c = str_replace("wp-content/themes/prologue/phpThumb/phpThumb.php?src=../../../../", "", $match[0]);//Actual URL to the image in the uploads folder minus phpThumb
- 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.
- $newextension = substr(strrchr($c,'.'),1);
- $eachFileFolder = preg_replace( '#\S+uploads\/(\S+)#', '$1', $c );//break the string up around the "/" character in $mystring
- $neweachFileFolder = preg_replace('#[^/]*$#', '', $eachFileFolder);
- $wp_upload_dir = wp_upload_dir();//get upload dir
- $old = $wp_upload_dir['basedir']."/".$eachFileFolder;
- $new = $wp_upload_dir['basedir']."/".$neweachFileFolder.$imageId.".".$newextension;
- rename($old, $new);
- var_dump($old);
- var_dump($new);
- $newimage = preg_replace('#[^/]*$#', '', $c);
- $filename = $newimage.$imageId.".".$newextension;
- }
- else{
- // $filename should be the path to a file in the upload directory.
- $filename = $c;
- $wp_upload_dir = wp_upload_dir();//get upload dir
- var_dump($c);
- }
- //Need to end if else here... if the image has spaces
- // The ID of the post this attachment is for.
- $imageIds = $imageId;
- // Check the type of tile. We'll use this as the 'post_mime_type'.
- $filetype = wp_check_filetype( basename( $filename ), null );
- // Get the path to the upload directory.
- /*$wp_upload_dir = wp_upload_dir();*/
- /* var_dump($wp_upload_dir['baseurl']);*/
- // Prepare an array of post data for the attachment.
- $attachment = array(
- 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
- 'post_mime_type' => $filetype['type'],
- 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
- // Generate the metadata for the attachment, and update the database record.
- /* if (false !== $attachment['post_mime_type']) {
- $attach_id = wp_insert_attachment( $attachment, $filename, $imageIds );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- update_post_meta($imageIds, '_thumbnail_id', $attach_id);
- }*/
- //Update post content with new post content
- /* $string = preg_replace('/<a\s[^>]*?href="(.*)"><img\s[^>]*?src="([^\"]+)"[^>]*><\/a>/', '', $postContent, 1);
- $my_post = array(
- 'ID' => $imageId,
- 'post_content' => $string
- );
- // Update the post into the database
- wp_update_post( $my_post );
- echo "Processing post $postTitle, Added image for post $imageId to the library, file name $filename </br>";*/
- $i++;
- }
- //Emd Else
- //End Update post content with new post content
- }
- outputProgress($i, $published_posts);
- function outputProgress($i, $published_posts) {
- echo "<span style='position: absolute;z-index:$current;background:#FFF;'>" . round($i / $published_posts * 100) . "% </span>";
- myFlush();
- sleep(1);
- }
- /**
- * Flush output buffer
- */
- function myFlush() {
- echo(str_repeat(' ', 256));
- if (@ob_get_contents()) {
- @ob_end_flush();
- }
- flush();
- }
- /* echo "Processed $i </br>";
- $percent = ($i * 100) / $published_posts;
- echo $percent;*/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement