├── .gitignore ├── Gemfile ├── LICENSE.md ├── README.md ├── Rakefile ├── features └── support │ └── env.rb ├── lib ├── middleman-wordpress.rb ├── middleman-wordpress │ ├── commands │ │ └── wordpress.rb │ ├── core.rb │ └── version.rb └── middleman_extension.rb └── middleman-wordpress.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore bundler lock file 2 | /Gemfile.lock 3 | 4 | # Ignore pkg folder 5 | /pkg 6 | 7 | # Ignore built gem 8 | *.gem 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # If you do not have OpenSSL installed, update 2 | # the following line to use "http://" instead 3 | source 'https://rubygems.org' 4 | 5 | # Specify your gem's dependencies in middleman-wordpress.gemspec 6 | gemspec 7 | 8 | group :development do 9 | gem 'rake' 10 | gem 'rdoc' 11 | gem 'yard' 12 | end 13 | 14 | group :test do 15 | gem 'cucumber' 16 | gem 'fivemat' 17 | gem 'aruba' 18 | gem 'rspec' 19 | end 20 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Wise & Hammer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # middleman-wordpress 2 | 3 | An extension for Middleman that pulls content from WordPress. 4 | 5 | ## About 6 | 7 | This extension pulls content from WordPress via the WP REST API plugin for WordPress. 8 | 9 | ## Installation 10 | 11 | ``` 12 | gem install middleman-wordpress 13 | ``` 14 | 15 | ## Configuration 16 | 17 | in `config.rb` 18 | 19 | ```ruby 20 | activate :wordpress do |wp| 21 | wp.uri = 'example.com' 22 | wp.custom_post_types = [:events, :resources, :other_things] 23 | end 24 | ``` 25 | 26 | ## Import Data 27 | 28 | To import data, run: 29 | 30 | ``` 31 | middleman wordpress 32 | ``` 33 | 34 | ## License 35 | 36 | Copyright (c) 2015 Wise & Hammer. Middleman-WordPress is free software and 37 | may be redistributed under the terms specified in the [license](LICENSE.md). 38 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | 4 | require 'cucumber/rake/task' 5 | 6 | Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t| 7 | t.cucumber_opts = "--color --tags ~@wip --strict --format #{ENV['CUCUMBER_FORMAT'] || 'Fivemat'}" 8 | end 9 | 10 | require 'rake/clean' 11 | 12 | task test: ['cucumber'] 13 | 14 | task default: :test 15 | -------------------------------------------------------------------------------- /features/support/env.rb: -------------------------------------------------------------------------------- 1 | PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__))) 2 | require 'middleman-core' 3 | require 'middleman-core/step_definitions' 4 | require File.join(PROJECT_ROOT_PATH, 'lib', 'middleman-wordpress') 5 | -------------------------------------------------------------------------------- /lib/middleman-wordpress.rb: -------------------------------------------------------------------------------- 1 | # Require core library 2 | require 'middleman-core' 3 | 4 | require 'middleman-wordpress/version' 5 | require 'middleman-wordpress/core' 6 | require 'middleman-wordpress/commands/wordpress' 7 | 8 | ::Middleman::Extensions.register(:wordpress, MiddlemanWordPress::Core) 9 | -------------------------------------------------------------------------------- /lib/middleman-wordpress/commands/wordpress.rb: -------------------------------------------------------------------------------- 1 | require 'json' 2 | require 'fileutils' 3 | require 'middleman-core/cli' 4 | require 'wp/api' 5 | 6 | module Middleman 7 | module Cli 8 | class WordPress < Thor 9 | include Thor::Actions 10 | 11 | # Path where Middleman expects the local data to be stored 12 | MIDDLEMAN_LOCAL_DATA_FOLDER = 'data' 13 | 14 | check_unknown_options! 15 | 16 | namespace :wordpress 17 | desc 'wordpress', 'Import data from WordPress' 18 | 19 | # @todo option to rebuild site if data changes 20 | # method_option "rebuild", aliases: "-r", desc: "Rebuilds the site if there were changes to the imported data" 21 | 22 | def self.source_root 23 | ENV['MM_ROOT'] 24 | end 25 | 26 | # Tell Thor to exit with a nonzero exit code on failure 27 | def self.exit_on_failure? 28 | true 29 | end 30 | 31 | def wordpress 32 | ::Middleman::Application.server.inst 33 | 34 | # Create data directory if it does not exist 35 | Dir.mkdir('data') unless File.exists?('data') 36 | 37 | # Remove all WordPress files 38 | FileUtils.rm_rf(Dir.glob('data/wordpress_*')) 39 | 40 | # Instantiate the client 41 | @api = WP::API[MiddlemanWordPress.options.uri] 42 | 43 | # Build-up posts 44 | posts = [] 45 | posts.concat fetch_pages_collection 46 | posts.concat fetch_posts_collection(:posts) 47 | 48 | MiddlemanWordPress.options.custom_post_types.each do |post_type| 49 | posts.concat fetch_posts_collection(post_type) 50 | end 51 | 52 | # Strip out headers; keep attributes 53 | posts.map!{|post| post.attributes} 54 | 55 | # Derive all the post types 56 | post_types = [] 57 | posts.each{|post| post_types << post['type']} 58 | post_types.uniq! 59 | 60 | # Save the posts out to disc in collections by post type 61 | post_types.each do |post_type| 62 | collection_name = post_type.pluralize 63 | collection = posts.select{|post| post['type'] == post_type} 64 | extension = "json" 65 | 66 | File.open("data/wordpress_#{collection_name}.#{extension}", "w") do |f| 67 | f.write(collection.to_json) 68 | end 69 | end 70 | end 71 | 72 | protected 73 | 74 | def fetch_pages_collection 75 | pages = [] 76 | page = 1 77 | limit = 10 78 | 79 | tmp_pages = fetch_pages(page, limit) 80 | while !tmp_pages.empty? 81 | pages.concat tmp_pages 82 | page = page + 1 83 | tmp_pages = fetch_pages(page, limit) 84 | end 85 | 86 | pages 87 | end 88 | 89 | def fetch_posts_collection(type) 90 | type = type.to_s.singularize 91 | posts = [] 92 | page = 1 93 | limit = 10 94 | 95 | tmp_posts = fetch_posts(type, page, limit) 96 | while !tmp_posts.empty? 97 | posts.concat tmp_posts 98 | page = page + 1 99 | tmp_posts = fetch_posts(type, page, limit) 100 | end 101 | 102 | posts 103 | end 104 | 105 | def fetch_pages(page, limit) 106 | begin 107 | pages = @api.pages(page: page, posts_per_page: limit) 108 | rescue WP::API::ResourceNotFoundError => e 109 | # Who cares? We've reached the end of the list 110 | pages = [] 111 | end 112 | 113 | pages 114 | end 115 | 116 | def fetch_posts(type, page, limit) 117 | begin 118 | posts = @api.posts(type: type, page: page, posts_per_page: limit) 119 | rescue WP::API::ResourceNotFoundError => e 120 | # Who cares? We've reached the end of the list 121 | posts = [] 122 | end 123 | 124 | posts 125 | end 126 | end 127 | end 128 | end 129 | -------------------------------------------------------------------------------- /lib/middleman-wordpress/core.rb: -------------------------------------------------------------------------------- 1 | module MiddlemanWordPress 2 | class << self 3 | attr_reader :options 4 | end 5 | 6 | class Core < Middleman::Extension 7 | option :uri, nil, "The WordPress API uri" 8 | option :custom_post_types, [], "Custom post types" 9 | 10 | def initialize(app, options_hash={}, &block) 11 | super 12 | 13 | MiddlemanWordPress.instance_variable_set('@options', options) 14 | end 15 | 16 | helpers do 17 | Dir["data/wordpress_*"].each do |file| 18 | define_method(file.gsub("data/wordpress_", "")) do 19 | file = File.read(file) 20 | return JSON.parse(file) 21 | end 22 | end 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/middleman-wordpress/version.rb: -------------------------------------------------------------------------------- 1 | module MiddlemanWordPress 2 | VERSION = "0.0.1" 3 | end 4 | -------------------------------------------------------------------------------- /lib/middleman_extension.rb: -------------------------------------------------------------------------------- 1 | require 'middleman-wordpress' 2 | -------------------------------------------------------------------------------- /middleman-wordpress.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | 4 | require 'middleman-wordpress/version' 5 | 6 | Gem::Specification.new do |s| 7 | s.name = "middleman-wordpress" 8 | s.version = MiddlemanWordPress::VERSION 9 | s.license = 'MIT' 10 | s.platform = Gem::Platform::RUBY 11 | s.authors = ["Nickolas Kenyeres"] 12 | s.email = ["nkenyeres@gmail.com"] 13 | s.homepage = "https://github.com/knicklabs/middleman-wordpress" 14 | s.summary = %q{An extension for Middleman that pulls content from WordPress via WP REST API} 15 | s.description = %q{An extension for Middleman that enables the building of static websites using WordPress-managed content. This extension pulls content from WordPress via the WP REST API plugin for WordPress.} 16 | 17 | s.files = `git ls-files`.split("\n") 18 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 19 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 20 | s.require_paths = ["lib"] 21 | 22 | # The version of middleman-core your extension depends on 23 | s.add_runtime_dependency("middleman-core", [">= 3.3.12"]) 24 | s.add_runtime_dependency("wp-api", [">= 0.1.3"]) 25 | 26 | # Additional dependencies 27 | # s.add_runtime_dependency("gem-name", "gem-version") 28 | end 29 | --------------------------------------------------------------------------------