Advertisement
SimeonGriggs

WP Migrate DB Pro - Mapped Domains/Subdirectories

Sep 17th, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.62 KB | None | 0 0
  1. function migration_complete( $migration_type, $connection_url ) {
  2.  
  3.     // Set Development and Production sites to use use correct domains
  4.     // We want:
  5.     // example.dev + example.dev/subdirectory domains on Development
  6.     // example.com.au + subdirectory.com.au mapped domains on Production
  7.     // Use at your own risk :D
  8.  
  9.  
  10.     // Code below assumes you're using https:// and .com.au, but you can change that here
  11.     $default_ssl = 'https://';
  12.     $default_tld = '.com.au';
  13.  
  14.  
  15.     // Let's begin
  16.     global $wpdb;
  17.  
  18.     // Select just the first blog, to get the root URL
  19.     $blog_root = $wpdb->get_results(
  20.         $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", 1 )
  21.     );
  22.  
  23.     // And set its domain as the root for all blogs
  24.     foreach ( $blog_root as $root ) {
  25.         $root_domain = $root->domain;
  26.     }
  27.  
  28.  
  29.     // Because this function fires on BOTH Development and Production databases
  30.     // We need to perform different actions on different databases whether pushing or pulling
  31.  
  32.     // First check if this database's #1 site domain ends in .dev
  33.     // And we'll assume it's Development
  34.     if (preg_match('/.dev$/', $root_domain)) {
  35.  
  36.         // Then select all blogs, other than the root blog #1
  37.         $blogs = $wpdb->get_results(
  38.             $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id != %d", 1 )
  39.         );
  40.  
  41.         // For each of these, turn the domain into the path...
  42.         foreach ( $blogs as $blog ) {
  43.  
  44.             // ...if it doesn't have a path, it could be the subdirectory is already set on this DB
  45.             if ($blog->path == '/') {
  46.  
  47.             // Remove the TLD, and add slashes either side
  48.                 $dev_path = str_replace($default_tld, "", $blog->domain);
  49.                 $dev_path_slashed = '/'. $dev_path . '/';
  50.  
  51.                 // Update the domain to use the root domain
  52.                 $wpdb->update(
  53.                     $wpdb->blogs,
  54.                     array( 'domain' => $root_domain ),
  55.                     array( 'blog_id' => $blog->blog_id )
  56.                 );
  57.  
  58.                 // Update the path to use the slashed version of the production domain
  59.                 $wpdb->update(
  60.                     $wpdb->blogs,
  61.                     array( 'path' => $dev_path_slashed ),
  62.                     array( 'blog_id' => $blog->blog_id )
  63.                 );
  64.  
  65.                 // We have to also update the site options with the complete dev URL
  66.                 $dev_url = $default_ssl . $root_domain . $dev_path_slashed;
  67.                 update_blog_option ($blog->blog_id, 'siteurl', $dev_url);
  68.                 update_blog_option ($blog->blog_id, 'home', $dev_url);
  69.             }
  70.         }
  71.  
  72.     } else {
  73.  
  74.         // If the database's #1 site does NOT end in .dev
  75.         // We'll assume it's Production
  76.         // And set the mapped domains accordingly
  77.  
  78.         // Select all blogs, other than the root blog #1
  79.         $blogs = $wpdb->get_results(
  80.             $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id != %d", 1 )
  81.         );
  82.  
  83.         // For each of these, turn the path into the domain...
  84.         foreach ( $blogs as $blog ) {
  85.  
  86.             // ...if it has a path, it could be the domain is already set on this DB and this has no subdirectory
  87.             if ($blog->path != '/') {
  88.  
  89.                 $production_domain_raw = str_replace("/", "", $blog->path);
  90.                 $production_domain = $production_domain_raw . $default_tld;
  91.  
  92.                 // Update the domain to use the production domain
  93.                 $wpdb->update(
  94.                     $wpdb->blogs,
  95.                     array( 'domain' => $production_domain ),
  96.                     array( 'blog_id' => $blog->blog_id )
  97.                 );
  98.  
  99.                 // Update the path to just use a forward slash
  100.                 $wpdb->update(
  101.                     $wpdb->blogs,
  102.                     array( 'path' => '/' ),
  103.                     array( 'blog_id' => $blog->blog_id )
  104.                 );
  105.  
  106.                 // We have to also update the site options with the complete dev URL
  107.                 $dev_url = $default_ssl . $production_domain . '/';
  108.                 update_blog_option ($blog->blog_id, 'siteurl', $dev_url);
  109.                 update_blog_option ($blog->blog_id, 'home', $dev_url);
  110.             }
  111.         }
  112.  
  113.     } // End does domain end in .dev?
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement