Sha256: 8007c077273deedb760063f7dce7a48cf485971be8a302e2abf2eab32569e94d

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module WPMove
  class Commands
    def initialize(args)
      first_arg = args[0]
      second_arg = args[1]

      WPMove::Main.test

      if first_arg == 'rename'
        WPMove::Main.rename(second_arg)
      elsif first_arg == 'current'
        puts WPMove::Main.wp_current_url
      else
        # help
      end
    end
  end

  class Main
    require 'FileUtils'

    def self.test
      # test if WP installation
    end

    def self.rename(name)
      php = """
        global $wpdb;

        $oldurl = get_option('siteurl');
        $newurl = '#{name}';

        $wpdb->query(\"UPDATE \".$wpdb->prefix.\"options SET option_value = replace(option_value, '\".$oldurl.\"', '\".$newurl.\"') WHERE option_name = 'home' OR option_name = 'siteurl'\");
        $wpdb->query(\"UPDATE \".$wpdb->prefix.\"posts SET guid = replace(guid, '\".$oldurl.\"', '\".$newurl.\"')\");
        $wpdb->query(\"UPDATE \".$wpdb->prefix.\"posts SET post_content = replace(post_content, '\".$oldurl.\"', '\".$newurl.\"')\");
      """
      self.exec_php(php)
      puts "Renamed to: #{name}"
    end

    def self.wp_current_url
      Tools.exec_php("bloginfo('siteurl');")
    end

    def self.exec_php(blob)
      f = File.open('_from_ruby.php', 'w')
      f.write("<?php ")
      f.write("require 'wp-load.php';")
      f.write(blob)
      f.write('?>')
      f.write('')
      f.close
      result = `php _from_ruby.php`
      FileUtils.rm_f '_from_ruby.php'

      return result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wp-move-0.1.0 lib/wp-move.rb