Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function migration_complete( $migration_type, $connection_url ) {
- // Set Development and Production sites to use use correct domains
- // We want:
- // example.dev + example.dev/subdirectory domains on Development
- // example.com.au + subdirectory.com.au mapped domains on Production
- // Use at your own risk :D
- // Code below assumes you're using https:// and .com.au, but you can change that here
- $default_ssl = 'https://';
- $default_tld = '.com.au';
- // Let's begin
- global $wpdb;
- // Select just the first blog, to get the root URL
- $blog_root = $wpdb->get_results(
- $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", 1 )
- );
- // And set its domain as the root for all blogs
- foreach ( $blog_root as $root ) {
- $root_domain = $root->domain;
- }
- // Because this function fires on BOTH Development and Production databases
- // We need to perform different actions on different databases whether pushing or pulling
- // First check if this database's #1 site domain ends in .dev
- // And we'll assume it's Development
- if (preg_match('/.dev$/', $root_domain)) {
- // Then select all blogs, other than the root blog #1
- $blogs = $wpdb->get_results(
- $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id != %d", 1 )
- );
- // For each of these, turn the domain into the path...
- foreach ( $blogs as $blog ) {
- // ...if it doesn't have a path, it could be the subdirectory is already set on this DB
- if ($blog->path == '/') {
- // Remove the TLD, and add slashes either side
- $dev_path = str_replace($default_tld, "", $blog->domain);
- $dev_path_slashed = '/'. $dev_path . '/';
- // Update the domain to use the root domain
- $wpdb->update(
- $wpdb->blogs,
- array( 'domain' => $root_domain ),
- array( 'blog_id' => $blog->blog_id )
- );
- // Update the path to use the slashed version of the production domain
- $wpdb->update(
- $wpdb->blogs,
- array( 'path' => $dev_path_slashed ),
- array( 'blog_id' => $blog->blog_id )
- );
- // We have to also update the site options with the complete dev URL
- $dev_url = $default_ssl . $root_domain . $dev_path_slashed;
- update_blog_option ($blog->blog_id, 'siteurl', $dev_url);
- update_blog_option ($blog->blog_id, 'home', $dev_url);
- }
- }
- } else {
- // If the database's #1 site does NOT end in .dev
- // We'll assume it's Production
- // And set the mapped domains accordingly
- // Select all blogs, other than the root blog #1
- $blogs = $wpdb->get_results(
- $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id != %d", 1 )
- );
- // For each of these, turn the path into the domain...
- foreach ( $blogs as $blog ) {
- // ...if it has a path, it could be the domain is already set on this DB and this has no subdirectory
- if ($blog->path != '/') {
- $production_domain_raw = str_replace("/", "", $blog->path);
- $production_domain = $production_domain_raw . $default_tld;
- // Update the domain to use the production domain
- $wpdb->update(
- $wpdb->blogs,
- array( 'domain' => $production_domain ),
- array( 'blog_id' => $blog->blog_id )
- );
- // Update the path to just use a forward slash
- $wpdb->update(
- $wpdb->blogs,
- array( 'path' => '/' ),
- array( 'blog_id' => $blog->blog_id )
- );
- // We have to also update the site options with the complete dev URL
- $dev_url = $default_ssl . $production_domain . '/';
- update_blog_option ($blog->blog_id, 'siteurl', $dev_url);
- update_blog_option ($blog->blog_id, 'home', $dev_url);
- }
- }
- } // End does domain end in .dev?
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement