ozh

Untitled

ozh
Aug 14th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. <?php
  2. /**********************************************
  3.  * Search & Replace for Marvin Shaker         *
  4.  * Place this script in your WordPress root   *
  5.  * and load in your web browser. Then wait.   *
  6.  **********************************************/
  7.  
  8. /***** CUSTOMIZE HERE MAYBE **/
  9.  
  10. // Batch size: how many post to fix in one pass
  11. // If your server is slow, reduce the number
  12. $marv_chunk = 50;
  13.  
  14.  
  15. /***** PROBABLY DONT EDIT THIS **/
  16.  
  17. // search pattern
  18. $marv_search = '/\[tab:(.+)?\]/';
  19.  
  20. // replacement pattern
  21. $marv_replace = '<!--nextpage--><!--marvin:\\1-->';
  22.  
  23. // Don't replace first occurence found in post, remove only
  24. $marv_remove_first = true;
  25.  
  26.  
  27. /***** DON'T EDIT ANYTHING PAST THIS LINE **/
  28.  
  29. // Load the WordPress Environment
  30. require('./wp-load.php');
  31. echo "<pre>\n";
  32.  
  33. // start offset
  34. $marv_start = ( isset( $_GET['start'] ) ? $_GET['start'] : 1 );
  35.  
  36. // total updated
  37. $marv_total = ( isset( $_GET['total'] ) ? $_GET['total'] : 0 );
  38.  
  39. if( $marv_total )
  40.     echo "So far, $marv_total posts updated...\n\n";
  41.  
  42. // Setup post query
  43. query_posts( array(
  44.     'posts_per_page' => $marv_chunk,
  45.     'paged' => $marv_start
  46.     )
  47. );
  48.  
  49.  
  50. // Loop through posts if any
  51. if ( have_posts() ) {
  52.  
  53.     while ( have_posts() ) : the_post();
  54.        
  55.         $post     = marv_trim( get_the_content() );   // post content
  56.         $new_post = marv_search_and_replace( $post ); // post content, fixed
  57.        
  58.         // Post needs to be updated
  59.         if( $post != $new_post ) {
  60.        
  61.             // Set new post content
  62.             $updated_post = array(
  63.                 'ID' => $id,
  64.                 'post_content' => $new_post,
  65.             );
  66.            
  67.             // Update post
  68.             if( wp_update_post( $updated_post ) ) {
  69.                 echo "Post #$id updated\n";            
  70.                 // Update counter
  71.                 $marv_total++;
  72.                
  73.             // log if there was an error
  74.             } else {
  75.                 error_log( "Error: could not update post $id\n", 3, dirname( __FILE__ ).'/marv-error.log' );
  76.             }
  77.  
  78.         // Post doesn't need to be updated
  79.         } else {
  80.             echo "Post #$id skipped\n";
  81.         }
  82.        
  83.     endwhile;
  84.  
  85.     // Load next batch of posts
  86.     marv_next_page( );
  87.  
  88. } else {
  89.  
  90.     echo "All done !! $marv_total posts were updated.";
  91.  
  92. }
  93.  
  94. function marv_trim( $string ) {
  95.     return trim( $string, "\r\n\t " );
  96. }
  97.  
  98. function marv_search_and_replace( $content ) {
  99.     global $marv_search, $marv_replace, $marv_remove_first;
  100.    
  101.     if( $marv_remove_first )
  102.         $content = preg_replace( $marv_search, '' , $content, 1 );
  103.        
  104.     $content = preg_replace( $marv_search, $marv_replace , $content );
  105.    
  106.     return marv_trim( $content );
  107. }
  108.  
  109. function marv_next_page( ) {
  110.     global $marv_start, $marv_chunk, $marv_total;
  111.     $marv_start += $marv_chunk;
  112.    
  113.     $url = basename( __FILE__ )."?start=$marv_start&total=$marv_total";
  114.  
  115.     echo <<<SCRIPT
  116.    
  117.     <a href="$url">Next page</a> loading, please wait...
  118.     <script type="text/javascript">
  119.         function letsgo() {
  120.             window.location="$url";
  121.         }
  122.         setTimeout( letsgo, 1500 );
  123.     </script>
  124. SCRIPT;
  125. }
Add Comment
Please, Sign In to add comment