set :cul_allowed_upload_types_version, 'v0.5.0' # can be overridden by an app's deploy.rb file set :cul_allowed_upload_types_plugin_name, 'cul-allowed-upload-types' set :cul_allowed_upload_types_downloaded_plugin_path, ->{current_path.join(fetch(:cul_allowed_upload_types_plugin_name))} # can be overridden by an app's deploy.rb file

namespace :cul do

namespace :wp do

  set :maintenance_file_path, ->{
    require_cap_variables!([:wp_docroot])
    File.join(fetch(:wp_docroot), '.maintenance')
  }

  desc "Enables maintenance mode for the WordPress site in the deploy environment"
  task :enable_maintenance_mode do
    on roles(:web) do
      within fetch(:wp_docroot) do
        # Set maintenance $upgrading value to current time.
        # Note that WordPress will ignore maintenance mode file
        # after 10 minutes have passed after the maintenance time
        # we set in the file.
        execute :echo, "'<?php $upgrading = #{Time.now.to_i};'", '>', fetch(:maintenance_file_path)
      end
    end
    puts color_text("Maintenance mode enabled!")
  end

  desc "Disable maintenance mode for the WordPress site in the deploy environment"
  task :disable_maintenance_mode do
    on roles(:web) do
      within fetch(:wp_docroot) do
        if test("[ -f #{fetch(:maintenance_file_path)} ]")
          execute :rm, fetch(:maintenance_file_path)
        else
          puts "No maintenance file found, so there's nothing to delete."
        end
      end
    end
    puts color_text("Maintenance mode disabled!")
  end

  desc "Runs a search and replace operation on the tables in a WordPress installation"
  task :searchreplace do

    set :search_string, ask("search string")
    set :replacement_string, ask("replacement string")

    unless enter_y_to_continue(color_text("This will replace all occurrences of \"#{fetch(:search_string)}\" with \"#{fetch(:replacement_string)}\"."))
      puts 'Search and replace cancelled because "y" was not entered.'
      next
    end

    on roles(:web) do
      within fetch(:wp_docroot) do
        puts 'Running search and replace. This may take a while for large databases...'
        start_time = Time.now

        if fetch(:multisite, false)
          puts "Since this is a multisite, you'll need to specify the original multisite instance domain to continue:"
          set :multisite_url, ask('original multisite instance domain (e.g. blogs.cul.columbia.edu)')

          execute :wp, "--url=#{fetch(:multisite_url)}", 'search-replace', "'#{fetch(:search_string)}'", "'#{fetch(:replacement_string)}'", '--all-tables', '--skip-columns=guid'
        else
          execute :wp, 'search-replace', "'#{fetch(:search_string)}'", "'#{fetch(:replacement_string)}'", '--skip-columns=guid'
        end

        puts "Search and replace complete (took #{(Time.now - start_time).to_s} seconds)"
      end
    end
  end

  def self.require_dest_wp_domain_if_multisite!
    if(fetch(:multisite))
      puts "Since this is a multisite, you'll need to specify the DESTINATION instance domain to continue:"
      set :dest_multisite_domain, ask('destination multisite instance domain (e.g. blogs-dev.cul.columbia.edu)')
      require_cap_variables!([:dest_multisite_domain])
    end
  end

  def self.require_src_wp_domain_if_multisite!
    if(fetch(:multisite))
      puts "Since this is a multisite, you'll need to specify the SOURCE instance domain to continue:"
      set :src_multisite_domain, ask('source multisite instance domain (e.g. blogs.cul.columbia.edu)')
      require_cap_variables!([:src_multisite_domain])
    end
  end

end

end