wp_wrapper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ module WpWrapper
2
+ module Modules
3
+ module Plugins
4
+ include ::WpWrapper::Modules::Plugins::Akismet
5
+ include ::WpWrapper::Modules::Plugins::Gocodes
6
+ include ::WpWrapper::Modules::Plugins::W3TotalCache
7
+ include ::WpWrapper::Modules::Plugins::WordpressSeo
8
+ include ::WpWrapper::Modules::Plugins::TrackingCode
9
+
10
+ def manage_plugins(plugin_identifiers, action = :activate)
11
+ plugin_identifiers = (plugin_identifiers.is_a?(Array)) ? plugin_identifiers : [plugin_identifiers.to_s]
12
+
13
+ plugin_identifiers.each do |plugin_identifier|
14
+ manage_plugin(plugin_identifier, action)
15
+ end
16
+ end
17
+
18
+ def manage_plugin(plugin_identifier, action = :activate)
19
+ success = false
20
+
21
+ if (login)
22
+ activation_link = nil
23
+ plugins_page = self.mechanize_client.open_url(get_url(:plugins))
24
+
25
+ if (plugins_page)
26
+ plugin_links = plugins_page.parser.css("table.plugins tbody tr td span.#{action} a")
27
+ regex = Regexp.new("plugin=#{plugin_identifier}", Regexp::IGNORECASE)
28
+
29
+ plugin_links.each do |link|
30
+ href = link["href"]
31
+
32
+ if (regex.match(href))
33
+ activation_link = href
34
+ break
35
+ end
36
+ end if (plugin_links && plugin_links.any?)
37
+
38
+ if (activation_link && activation_link.present?)
39
+ url = "#{get_url(:admin)}/#{activation_link}"
40
+ self.mechanize_client.open_url(url)
41
+ puts "#{Time.now}: Url: #{self.url}. Plugin '#{plugin_identifier}' has been #{action}d!"
42
+ success = true
43
+ else
44
+ puts "#{Time.now}: Url: #{self.url}. Couldn't find the plugin #{plugin_identifier}'s #{action}-link."
45
+ end
46
+
47
+ end
48
+ else
49
+ puts "#{Time.now}: Failed to login for url #{self.url}, will not proceed to #{action} plugins"
50
+ end
51
+
52
+ return success
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,66 @@
1
+ module WpWrapper
2
+ module Modules
3
+ module Profiles
4
+
5
+ def add_user(login, email, password, role = :editor, send_password = false)
6
+ options = {
7
+ :user_login => {:value => login, :type => :input},
8
+ :email => {:value => email, :type => :input},
9
+ :pass1 => {:value => password, :type => :input},
10
+ :pass2 => {:value => password, :type => :input},
11
+ :role => {:value => role, :type => :select},
12
+ :send_password => {:checked => send_password, :type => :checkbox},
13
+ }
14
+
15
+ url = get_url(:new_user, absolute: false, admin_prefix: '')
16
+
17
+ return set_options_and_submit(url, {name: "createuser"}, options)
18
+ end
19
+
20
+ def change_display_name(display_name)
21
+ options = {
22
+ :nickname => {:value => display_name, :type => :input}
23
+ }
24
+
25
+ update_profile(options)
26
+
27
+ options = {
28
+ :display_name => {:value => display_name, :type => :select}
29
+ }
30
+
31
+ update_profile(options)
32
+ end
33
+
34
+ def change_profile_password
35
+ options = {
36
+ :pass1 => {:value => new_password, :type => :input},
37
+ :pass2 => {:value => new_password, :type => :input}
38
+ }
39
+
40
+ return update_profile(options)
41
+ end
42
+
43
+ def update_profile(options = {})
44
+ url = get_url(:profile, absolute: false, admin_prefix: '')
45
+
46
+ return set_options_and_submit(url, {action: /profile\.php/i}, options)
47
+ end
48
+
49
+ # Deprec?
50
+ def change_profile_password(new_password)
51
+ if (login)
52
+ profile_page = self.mechanize_client.open_url(get_url(:profile))
53
+
54
+ if (profile_page)
55
+ profile_form = profile_page.form_with(:action => "#{self.url}/wp-admin/profile.php")
56
+ profile_form.field_with(:name => 'pass1').value = new_password
57
+ profile_form.field_with(:name => 'pass2').value = new_password
58
+
59
+ updated_page = profile_form.submit(profile_form.buttons.first)
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,65 @@
1
+ module WpWrapper
2
+ module Modules
3
+ module Setup
4
+
5
+ def setup(title: nil, email: nil)
6
+ success = false
7
+ step_1_page = self.mechanize_client.open_url(get_url(:home))
8
+
9
+ if step_1_page
10
+ step_1_form = step_1_page.form_with(action: '?step=1')
11
+
12
+ if step_1_form
13
+ step_2_page = step_1_form.submit
14
+ success = set_setup_form_options(step_2_page, title: title, email: email)
15
+ else
16
+ success = set_setup_form_options(step_1_page, title: title, email: email)
17
+ end
18
+ end
19
+
20
+ return success
21
+ end
22
+
23
+ def set_setup_form_options(setup_page, title: nil, email: nil)
24
+ success = false
25
+ setup_form = setup_page.form_with(action: 'install.php?step=2')
26
+
27
+ if (setup_form && title.present? && self.username.present? && self.password.present? && email.present?)
28
+ puts "#{Time.now}: Url: #{self.url}. Setting up site..."
29
+
30
+ setup_form.field_with(name: 'weblog_title').value = title
31
+ setup_form.field_with(name: 'user_name').value = self.username
32
+ setup_form.field_with(name: 'admin_password').value = self.password
33
+ setup_form.field_with(name: 'admin_password2').value = self.password
34
+ setup_form.field_with(name: 'admin_email').value = email
35
+
36
+ confirmation_page = setup_form.submit
37
+
38
+ puts "#{Time.now}: Url: #{self.url}. The WordPress-blog has now been installed!"
39
+ success = true
40
+ else
41
+ puts "#{Time.now}: Url: #{self.url}. The blog has already been setup or the registration form couldn't be found or some data is missing."
42
+ puts "#{Time.now}: Url: #{self.url}. Information supplied:\nTitle: #{title}.\nUsername: #{self.username}.\nPassword: #{self.password}.\nEmail: #{email}."
43
+ end
44
+
45
+ return success
46
+ end
47
+
48
+ def set_permalinks_options(options = {})
49
+ permalink_structure = options.fetch(:permalink_structure, '/%postname%/')
50
+ category_base = options.fetch(:category_base, 'kategori')
51
+ tag_base = options.fetch(:tag_base, 'etikett')
52
+
53
+ opts = {
54
+ :custom_selection => {:identifier => :id, :checked => true, :type => :radiobutton},
55
+ :permalink_structure => {:value => permalink_structure, :type => :input},
56
+ :category_base => {:value => category_base, :type => :input},
57
+ :tag_base => {:value => tag_base, :type => :input},
58
+ }
59
+
60
+ return set_options_and_submit("options-permalink.php", {:action => 'options-permalink.php'}, opts, :first, {:should_reset_radio_buttons => true})
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,71 @@
1
+ module WpWrapper
2
+ module Modules
3
+ module Themes
4
+
5
+ def activate_random_theme
6
+ theme_links = get_theme_links
7
+ random_link = (theme_links && theme_links.any?) ? theme_links.to_a.sample : nil
8
+
9
+ perform_activation(random_link["href"]) if (random_link)
10
+ end
11
+
12
+ def activate_theme(theme_identifier = 'twentytwelve')
13
+ success = false
14
+ theme_links = get_theme_links
15
+
16
+ if (theme_links && theme_links.any?)
17
+ activation_link = nil
18
+ regex = Regexp.new("stylesheet=#{theme_identifier}", Regexp::IGNORECASE)
19
+
20
+ theme_links.each do |link|
21
+ href = link["href"]
22
+
23
+ if (regex.match(href))
24
+ activation_link = href
25
+ break
26
+ end
27
+ end
28
+
29
+ success = perform_activation(activation_link)
30
+
31
+ if success
32
+ puts "[WpWrapper::Modules::Themes] - #{Time.now}: Url: #{self.url}. Theme '#{theme_identifier}' has been activated!"
33
+ else
34
+ puts "[WpWrapper::Modules::Themes] - #{Time.now}: Url: #{self.url}. Couldn't find the theme #{theme_identifier}'s activation-link."
35
+ end
36
+ end
37
+
38
+ return success
39
+ end
40
+
41
+ def perform_activation(url)
42
+ success = false
43
+
44
+ if (url && url.present?)
45
+ puts "[WpWrapper::Modules::Themes] - #{Time.now}: Will activate theme with url #{url}."
46
+ self.mechanize_client.open_url(url)
47
+ success = true
48
+ end
49
+
50
+ return success
51
+ end
52
+
53
+ def get_theme_links
54
+ theme_links = []
55
+
56
+ if (login)
57
+ themes_page = self.mechanize_client.open_url(get_url(:themes))
58
+
59
+ if (themes_page)
60
+ theme_links = themes_page.parser.css("div.themes div.theme div.theme-actions a.activate")
61
+ end
62
+ end
63
+
64
+ puts "[WpWrapper::Modules::Themes] - #{Time.now}: Found a total of #{theme_links.try(:size)} installed themes."
65
+
66
+ return theme_links
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,137 @@
1
+ module WpWrapper
2
+ module Modules
3
+
4
+ module Upgrade
5
+
6
+ def upgrade(type = :core)
7
+ success = self.send("upgrade_#{type}")
8
+
9
+ puts "#{Time.now}: Will upgrade #{type} for site #{self.url}."
10
+
11
+ if (success)
12
+ puts "#{Time.now}: Successfully upgraded #{type} for site #{self.url}."
13
+ end
14
+
15
+ return success
16
+ end
17
+
18
+ def upgrade_core(retries: 3)
19
+ success = false
20
+
21
+ if (login)
22
+ update_page = self.mechanize_client.open_url(get_url(:upgrade))
23
+
24
+ if (update_page)
25
+ response_header = update_page.parser.at_css('h3.response')
26
+ upgrade_form = update_page.forms_with(:name => 'upgrade').try(:first)
27
+
28
+ begin
29
+ if (response_header && upgrade_form)
30
+ puts "#{Time.now}: Url: #{self.url}. Upgrading WordPress..."
31
+ upgraded_page = upgrade_form.submit(upgrade_form.button_with(:name => 'upgrade'))
32
+
33
+ puts "#{Time.now}: Url: #{self.url}. WordPress was upgraded!"
34
+ success = true
35
+ else
36
+ puts "#{Time.now}: Url: #{self.url}. Will not upgrade WordPress, already at latest version."
37
+ success = true
38
+ end
39
+ rescue Exception => e
40
+ puts "#{Time.now}: Url: #{self.url}. An error occurred while trying to upgrade WordPress. Exception: #{e.class.name}. Message: #{e.message}. Stacktrace: #{e.backtrace.join("\n")}"
41
+ retries -= 1
42
+ retry if (retries > 0)
43
+ end
44
+ end
45
+ end
46
+
47
+ return success
48
+ end
49
+
50
+ def upgrade_plugins
51
+ return upgrade_themes_or_plugins(:plugins)
52
+ end
53
+
54
+ def upgrade_themes
55
+ return upgrade_themes_or_plugins(:themes)
56
+ end
57
+
58
+ def upgrade_themes_or_plugins(type = :plugins, retries: 3)
59
+ success = false
60
+ form_identifier = get_upgrade_form_identifier(type)
61
+
62
+ if (login)
63
+ update_page = self.mechanize_client.open_url(get_url(:upgrade))
64
+
65
+ if (update_page)
66
+ upgrade_form = update_page.form_with(:name => form_identifier)
67
+
68
+ if (upgrade_form)
69
+ puts "#{Time.now}: Url: #{self.url}. Upgrading #{type}..."
70
+
71
+ upgrade_form.checkboxes.each do |checkbox|
72
+ checkbox.checked = true
73
+ end
74
+
75
+ begin
76
+ upgraded_page = upgrade_form.submit(upgrade_form.buttons.first)
77
+ upgrade_url = upgraded_page.iframes.first.src
78
+
79
+ puts "Upgrade #{type} url: #{upgrade_url.inspect}"
80
+
81
+ if (upgrade_url)
82
+ self.mechanize_client.agent.get("#{get_url(:admin)}/#{upgrade_url}")
83
+ puts "#{Time.now}: Url: #{self.url}. #{type.to_s.capitalize} were upgraded!"
84
+ success = true
85
+ end
86
+ rescue Exception => e
87
+ retries -= 1
88
+ retry if (retries > 0)
89
+ end
90
+
91
+ else
92
+ puts "#{Time.now}: Url: #{self.url}. Will not upgrade any #{type}, they are all already at the latest version."
93
+ success = true
94
+ end
95
+ end
96
+ end
97
+
98
+ return success
99
+ end
100
+
101
+ def upgrade_database
102
+ success = false
103
+ response = self.http_client.retrieve_parsed_html(get_url(:admin)).parsed_body
104
+
105
+ puts "#{Time.now}: Url: #{self.url}. Will check if database upgrades needs to be performed..."
106
+
107
+ if (response)
108
+ should_upgrade = !(response.at_css('body').content =~ /Database Update Required/i).nil?
109
+
110
+ if (should_upgrade)
111
+ puts "#{Time.now}: Url: #{self.url}. Should Upgrade WordPress database"
112
+ upgrade_url = response.at_css("p.step a.button")['href']
113
+
114
+ if (upgrade_url.present?)
115
+ upgrade_url = "#{self.url}wp-admin/#{upgrade_url}" unless upgrade_url =~ /^http/i
116
+ upgrade_response = self.http_client.retrieve_parsed_html(upgrade_url).parsed_body
117
+ success = !(upgrade_response.at_css('body').content =~ /Update Complete/i).nil?
118
+
119
+ puts "#{Time.now}: Url: #{self.url}. Successfully updated WordPress database." if success
120
+ end
121
+ end
122
+ end
123
+
124
+ return success
125
+ end
126
+
127
+ def get_upgrade_form_identifier(type = :plugins)
128
+ return case type.to_sym
129
+ when :plugins then 'upgrade-plugins'
130
+ when :themes then 'upgrade-themes'
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,12 @@
1
+ require 'wp_wrapper'
2
+ require 'rails'
3
+
4
+ module WpWrapper
5
+ class Railtie < Rails::Railtie
6
+
7
+ #rake_tasks do
8
+ # Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each { |ext| load ext }
9
+ #end
10
+
11
+ end
12
+ end
data/lib/wp_wrapper.rb ADDED
@@ -0,0 +1,24 @@
1
+ module WpWrapper
2
+ VERSION = "0.0.2"
3
+
4
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/railtie') if defined?(Rails)
5
+
6
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/authorization')
7
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/setup')
8
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/upgrade')
9
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/themes')
10
+
11
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/plugins/akismet')
12
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/plugins/gocodes')
13
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/plugins/w3_total_cache')
14
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/plugins/wordpress_seo')
15
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/plugins/tracking_code')
16
+
17
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/plugins')
18
+
19
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/options')
20
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/profiles')
21
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/modules/api')
22
+
23
+ require File.join(File.dirname(__FILE__), 'wp_wrapper/client')
24
+ end
@@ -0,0 +1,43 @@
1
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
+
3
+ begin
4
+ require "rubygems"
5
+ require "bundler"
6
+
7
+ if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.5")
8
+ raise RuntimeError, "Your bundler version is too old." +
9
+ "Run `gem install bundler` to upgrade."
10
+ end
11
+
12
+ # Set up load paths for all bundled gems
13
+ Bundler.setup
14
+ rescue Bundler::GemNotFound
15
+ raise RuntimeError, "Bundler couldn't find some gems." +
16
+ "Did you run \`bundlee install\`?"
17
+ end
18
+
19
+ Bundler.require
20
+
21
+ require File.expand_path('../../lib/wp_wrapper', __FILE__)
22
+
23
+ RSpec.configure do |config|
24
+ config.mock_with :mocha
25
+ end
26
+
27
+ def load_config
28
+ config_path = File.exists?(File.expand_path('../wordpress.yml', __FILE__)) ? File.expand_path('../wordpress.yml', __FILE__) : File.expand_path('../wordpress.yml.example', __FILE__)
29
+ config = YAML.load_file(config_path)
30
+ config.symbolize_keys! if config.respond_to?(:symbolize_keys!)
31
+
32
+ return config
33
+ end
34
+
35
+ def init_admin_connection
36
+ config = load_config
37
+ WpWrapper::Client.new(config[:admin])
38
+ end
39
+
40
+ def init_invalid_connection
41
+ config = load_config
42
+ WpWrapper::Client.new(config[:invalid])
43
+ end
@@ -0,0 +1,8 @@
1
+ :admin:
2
+ :url: http://domain.com
3
+ :username: UsernameToLoginWith
4
+ :password: PasswordToLoginWith
5
+ :invalid:
6
+ :url: http://domain.com
7
+ :username: foofoofoo
8
+ :password: foobar
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Authentication using WpWrapper" do
4
+
5
+ before(:each) do
6
+ @client = init_admin_connection
7
+ end
8
+
9
+ it 'can successfully login given proper credentials' do
10
+ @client.login.should == true
11
+ end
12
+
13
+ it 'can\'t login with invalid credentials' do
14
+ invalid_client = init_invalid_connection
15
+ invalid_client.login.should == false
16
+ end
17
+
18
+ end
@@ -0,0 +1,51 @@
1
+ Gem::Specification.new do |s|
2
+ s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.3.5") if s.respond_to? :required_rubygems_version=
4
+
5
+ s.name = 'wp_wrapper'
6
+ s.version = '0.0.2'
7
+
8
+ s.homepage = "http://github.com/Agiley/wp_wrapper"
9
+ s.email = "sebastian@agiley.se"
10
+ s.authors = ["Sebastian Johnsson"]
11
+ s.description = "Wrapper to interact with WordPress using Mechanize"
12
+ s.summary = "Wrapper to interact with WordPress using Mechanize"
13
+
14
+ s.add_dependency "nokogiri", ">= 1.5.9"
15
+ s.add_dependency "http_utilities", ">= 1.0.1"
16
+
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'rspec'
19
+ s.add_development_dependency 'mocha'
20
+
21
+ # = MANIFEST =
22
+ s.files = %w[
23
+ Gemfile
24
+ LICENSE.txt
25
+ README.markdown
26
+ Rakefile
27
+ lib/wp_wrapper.rb
28
+ lib/wp_wrapper/client.rb
29
+ lib/wp_wrapper/modules/api.rb
30
+ lib/wp_wrapper/modules/authorization.rb
31
+ lib/wp_wrapper/modules/options.rb
32
+ lib/wp_wrapper/modules/plugins.rb
33
+ lib/wp_wrapper/modules/plugins/akismet.rb
34
+ lib/wp_wrapper/modules/plugins/gocodes.rb
35
+ lib/wp_wrapper/modules/plugins/tracking_code.rb
36
+ lib/wp_wrapper/modules/plugins/w3_total_cache.rb
37
+ lib/wp_wrapper/modules/plugins/wordpress_seo.rb
38
+ lib/wp_wrapper/modules/profiles.rb
39
+ lib/wp_wrapper/modules/setup.rb
40
+ lib/wp_wrapper/modules/themes.rb
41
+ lib/wp_wrapper/modules/upgrade.rb
42
+ lib/wp_wrapper/railtie.rb
43
+ spec/spec_helper.rb
44
+ spec/wordpress.yml.example
45
+ spec/wp_wrapper/authentication_spec.rb
46
+ wp_wrapper.gemspec
47
+ ]
48
+ # = MANIFEST =
49
+
50
+ s.test_files = s.files.select { |path| path =~ %r{^spec/*/.+\.rb} }
51
+ end