Module: WpWrapper::Modules::Themes

Included in:
Client
Defined in:
lib/wp_wrapper/modules/themes.rb

Instance Method Summary collapse

Instance Method Details

#activate_random_theme(ignore_themes: []) ⇒ Object



5
6
7
8
9
10
# File 'lib/wp_wrapper/modules/themes.rb', line 5

def activate_random_theme(ignore_themes: [])
  theme_links               =   get_theme_links(ignore_themes: ignore_themes)
  random_url                =   (theme_links && theme_links.any?) ? theme_links.sample : nil

  perform_activation(random_url) if random_url
end

#activate_theme(theme_identifier = 'twentytwelve') ⇒ Object



12
13
14
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
# File 'lib/wp_wrapper/modules/themes.rb', line 12

def activate_theme(theme_identifier = 'twentytwelve')
  success                   =   false
  theme_links               =   get_theme_links
  
  if theme_links && theme_links.any?
    activation_link         =   nil
    regex                   =   Regexp.new("stylesheet=#{theme_identifier}", Regexp::IGNORECASE)

    theme_links.each do |link|
      href                  =   link["href"]
  
      if regex.match(href)
        activation_link     =   href
        break
      end
    end

    success                 =   perform_activation(activation_link)
    
    if success
      puts "[WpWrapper::Modules::Themes] - #{Time.now}: Url: #{self.url}. Theme '#{theme_identifier}' has been activated!"
    else
      puts "[WpWrapper::Modules::Themes] - #{Time.now}: Url: #{self.url}. Couldn't find the theme #{theme_identifier}'s activation-link."
    end
  end

  return success
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wp_wrapper/modules/themes.rb', line 53

def get_theme_links(ignore_themes: [])
  theme_links         =   []
  
  if 
    themes_page       =   self.mechanize_client.open_url(get_url(:themes))
    links             =   themes_page ? themes_page.parser.css("div.themes div.theme div.theme-actions a.activate") : []
    
    links.each do |link|
      href            =   link["href"]
      
      if ignore_themes && ignore_themes.any?
        ignore_themes.each do |ignore_theme|
          regex       =   ignore_theme.is_a?(Regexp) ? ignore_theme : Regexp.new("stylesheet=#{ignore_theme}", Regexp::IGNORECASE)
    
          if !regex.match(href)
            theme_links << href
            break
          end
        end
      else
        theme_links << href
      end
    end if links && links.any?
  end
  
  puts "[WpWrapper::Modules::Themes] - #{Time.now}: Found a total of #{theme_links.size} theme activation links."
  
  return theme_links
end

#perform_activation(url) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wp_wrapper/modules/themes.rb', line 41

def perform_activation(url)
  success             =   false
  
  if url && url.present?
    puts "[WpWrapper::Modules::Themes] - #{Time.now}: Will activate theme with url #{url}."
    self.mechanize_client.open_url(url)
    success           =   true
  end
  
  return success
end