Class: Middleman::Cli::WordPress

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/middleman-wordpress/commands/wordpress.rb

Constant Summary collapse

MIDDLEMAN_LOCAL_DATA_FOLDER =

Path where Middleman expects the local data to be stored

'data'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Tell Thor to exit with a nonzero exit code on failure

Returns:

  • (Boolean)


27
28
29
# File 'lib/middleman-wordpress/commands/wordpress.rb', line 27

def self.exit_on_failure?
  true
end

.source_rootObject

TODO:

option to rebuild site if data changes

method_option “rebuild”, aliases: “-r”, desc: “Rebuilds the site if there were changes to the imported data”



22
23
24
# File 'lib/middleman-wordpress/commands/wordpress.rb', line 22

def self.source_root
  ENV['MM_ROOT']
end

Instance Method Details

#wordpressObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/middleman-wordpress/commands/wordpress.rb', line 31

def wordpress
  ::Middleman::Application.server.inst

  # Create data directory if it does not exist
  Dir.mkdir('data') unless File.exists?('data')

  # Remove all WordPress files
  FileUtils.rm_rf(Dir.glob('data/wordpress_*'))

  # Instantiate the client
  @api = WP::API[MiddlemanWordPress.options.uri]

  # Build-up posts
  posts = []
  posts.concat fetch_pages_collection
  posts.concat fetch_posts_collection(:posts)

  MiddlemanWordPress.options.custom_post_types.each do |post_type|
    posts.concat fetch_posts_collection(post_type)
  end

  # Strip out headers; keep attributes
  posts.map!{|post| post.attributes}

  # Derive all the post types
  post_types = []
  posts.each{|post| post_types << post['type']}
  post_types.uniq!

  # Save the posts out to disc in collections by post type
  post_types.each do |post_type|
    collection_name = post_type.pluralize
    collection      = posts.select{|post| post['type'] == post_type}
    extension       = "json"

    File.open("data/wordpress_#{collection_name}.#{extension}", "w") do |f|
      f.write(collection.to_json)
    end
  end
end