Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. /*
  2. WARNING: Try and only run this once, and only after the Download Monitor upgrade as been run.
  3. Also, I cannot guarantee that this will not break everything, so please use with caution!
  4. */
  5. function ebd_upgrade_from_legacy()
  6. {
  7. global $wpdb;
  8.  
  9. $content_items = $wpdb->get_results( "SELECT content_id FROM `" . $wpdb->prefix . DLM_LU_Content_Queue::TABLE . "`" );
  10.  
  11. $id_map = array();
  12.  
  13. foreach ( $content_items as $item )
  14. {
  15. $post = get_post( $item->content_id );
  16.  
  17. // content
  18. $content = $post->post_content;
  19.  
  20. // generate new content
  21. $regex = "`\[email-download ([^\]]*)(?:download_id=([\"|']{0,1})([0-9]+)(?:[\"|']{0,1}))([^\]]*)\]`";
  22. $new_content = preg_replace_callback( $regex, function ( $m ) use ( $id_map, $wpdb ) {
  23. // check map, if not in map, fetch
  24. if ( ! isset( $id_map[ $m[3] ] ) ) {
  25. $new_id = absint( $wpdb->get_var( $wpdb->prepare( "SELECT `new_id` FROM `" . $wpdb->prefix . DLM_LU_Download_Queue::TABLE . "` WHERE `legacy_id` = %d ", absint( $m[3] ) ) ) );
  26.  
  27. if ( ! $new_id )
  28. return $m[0];
  29.  
  30. $id_map[ $m[3] ] = $new_id;
  31. }
  32. return "[email-download " . $m[1] . "download_id=" . $m[2] . $id_map[ $m[3] ] . $m[2] . $m[4] . "]";
  33. }, $content );
  34.  
  35. if ( $content !== $new_content )
  36. $wpdb->update( $wpdb->posts, array( 'post_content' => $new_content ), array( 'ID' => $post->ID ), array( '%s' ), array( '%d' ) );
  37. }
  38. }
  39. if ( $_GET['ebd_upgrade'] == 'upgrade' )
  40. ebd_upgrade_from_legacy();
Add Comment
Please, Sign In to add comment