Class: Shellpress::Theme

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/shellpress/theme.rb

Constant Summary collapse

ORDER =
1

Instance Method Summary collapse

Methods inherited from Thor

banner, #help

Instance Method Details

#delete(theme) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/shellpress/theme.rb', line 87

def delete(theme)
  path = "wp-content/themes/#{theme}"

  if !File.exists?(path)
    abort "Error: Invalid theme #{theme}"
  end

  force = options[:force]
  if force
    FileUtils.rm_rf path
  else
    confirm = ask "Delete #{path}? [Yn]"
    if confirm == "Y"
      FileUtils.rm_rf path
    end
  end
end

#download(url) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/shellpress/theme.rb', line 72

def download(url)
  zip = File.basename(URI.parse(url).path)
  theme = zip.split(".").first
  run "wget #{url}", :verbose => false
  run "unzip #{zip}", :verbose => false
  remove_file "#{zip}", :verbose => false
  run "mv #{theme} wp-content/themes/"
end

#install(theme) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shellpress/theme.rb', line 42

def install(theme)
  version = options[:version]

  if theme =~ URI::regexp
    invoke :download, [theme]
    filename = File.basename(URI.parse(theme).path)
    name = filename.split(".").first
    invoke :switch, [name]
  else
    if version
      url = "http://wordpress.org/extend/themes/download/#{theme}.#{version}.zip"
    else
      begin
        response = open("http://themes.svn.wordpress.org/#{theme}/").read
        version = response.match(%r|<a href="([\d+.]+)/">(.*)/</a></li>\n </ul>|)[1]
        url = "http://wordpress.org/extend/themes/download/#{theme}.#{version}.zip"
      rescue
        abort "Error: Invalid theme #{theme}"
      end
    end
    invoke :download, [url]
    invoke :switch, [theme]
  end
end

#switch(theme) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shellpress/theme.rb', line 14

def switch(theme)
  base = "wp-content/themes/#{theme}"

  if !File.exists?(base)
    abort "Error: Invalid theme #{theme}"
  end

  path = File.join(base, "style.css")
  file = File.open(path, "rb").read
  if file =~ /Template: (.*)\n/
    parent = $1
  end
  php = "php -r \"include 'wp-load.php';"
  if parent
    php << "switch_theme('#{parent}', '#{theme}');\""
  else
    php << "switch_theme('#{theme}', '#{theme}');\""
  end
  run php
end