Class: Shellpress::Plugin

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

Constant Summary collapse

ORDER =
0

Instance Method Summary collapse

Methods inherited from Thor

banner, #help

Instance Method Details

#activate(name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/shellpress/plugin.rb', line 72

def activate(name)
  network = options[:network_wide]
  base = "wp-content/plugins"
  php = "php -r \"include 'wp-load.php';"
  php << "require_once(ABSPATH . 'wp-admin/includes/plugin.php');"
  if File.exists?("#{base}/#{name}/#{name}.php")
    if network
      php << "activate_plugin('#{name}/#{name}.php', '', true);\""
    else
      php << "activate_plugin('#{name}/#{name}.php');\""
    end
  elsif File.exists?("#{base}/#{name}.php")
    if network
      php << "activate_plugin('#{name}.php', '', true);\""
    else
      php << "activate_plugin('#{name}.php');\""
    end
  else
    abort "Error: Invalid plugin #{name}"
  end
  run php
end

#deactivate(name) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/shellpress/plugin.rb', line 121

def deactivate(name)
  base = "wp-content/plugins"
  php = "php -r \"include 'wp-load.php';"
  php << "require_once(ABSPATH . 'wp-admin/includes/plugin.php');"
  php << "require_once(ABSPATH . 'wp-admin/includes/file.php');"
  if File.exists?("#{base}/#{name}/#{name}.php")
    php << "deactivate_plugins(array('#{name}/#{name}.php'));\""
  elsif File.exists?("#{base}/#{name}.php")
    php << "deactivate_plugins(array('#{name}.php'));\""
  else
    abort "Error: Invalid plugin #{name}"
  end
  run php
end

#delete(name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/shellpress/plugin.rb', line 100

def delete(name)
  base = "wp-content/plugins"
  php = "php -r \"include 'wp-load.php';"
  php << "require_once(ABSPATH . 'wp-admin/includes/plugin.php');"
  php << "require_once(ABSPATH . 'wp-admin/includes/file.php');"
  if File.exists?("#{base}/#{name}/#{name}.php")
    php << "delete_plugins(array('#{name}/#{name}.php'));\""
  elsif File.exists?("#{base}/#{name}.php")
    php << "delete_plugins(array('#{name}.php'));\""
  else
    abort "Error: Invalid plugin #{name}"
  end
  run php
end

#download(url) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/shellpress/plugin.rb', line 56

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

#install(plugin) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shellpress/plugin.rb', line 15

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

  if plugin =~ URI::regexp
    invoke :download, [plugin]
    filename = File.basename(URI.parse(plugin).path)
    name = filename.split(".").first
    invoke :activate, [name]
  else
    if version
      begin
        response = open("http://svn.wp-plugins.org/#{plugin}/tags/#{version}/readme.txt")
        url = "http://downloads.wordpress.org/plugin/#{plugin}.#{version}.zip"
      rescue
        abort "Error: Invalid plugin #{plugin}"
      end
    else
      begin
        response = open("http://svn.wp-plugins.org/#{plugin}/trunk/readme.txt").read
        stable = response.match(/Stable tag: (.*)\n/)[1]
        if stable == "trunk"
          url = "http://downloads.wordpress.org/plugin/#{plugin}.zip"
        elsif stable =~ /[\d\.]+/
          url = "http://downloads.wordpress.org/plugin/#{plugin}.#{stable}.zip"
        else
          abort "Error: Invalid plugin #{plugin} or invalid readme.txt"
        end
      rescue
        abort "Error: Invalid plugin #{plugin}"
      end
    end
    invoke :download, [url]
    invoke :activate, [plugin]
  end
end