forked from benbalter/wordpress-to-jekyll-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjekyll-exporter.php
417 lines (301 loc) · 9.85 KB
/
jekyll-exporter.php
1
2
3
4
5
6
7
8
9
10
11
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/*
Plugin Name: WordPress to Jekyll Exporter
Description: Exports WordPress posts, pages, and options as YAML files parsable by Jekyll
Version: 2.0.1
Author: Ben Balter
Author URI: http://ben.balter.com
License: GPLv3 or Later
Copyright 2012-2014 Benjamin J. Balter (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
wp_die("Jekyll Export requires PHP 5.3 or later");
}
require_once dirname( __FILE__ ) . "/lib/cli.php";
require_once dirname( __FILE__ ) . "/vendor/autoload.php";
use Alchemy\Zippy\Zippy;
class Jekyll_Export {
private $zip_folder = 'jekyll-export/'; //folder zip file extracts to
public $rename_options = array( 'site', 'blog' ); //strings to strip from option keys on export
public $options = array( //array of wp_options value to convert to _config.yml
'name',
'description',
'url'
);
/**
* Hook into WP Core
*/
function __construct() {
add_action( 'admin_menu', array( &$this, 'register_menu' ) );
add_action( 'current_screen', array( &$this, 'callback' ) );
}
/**
* Listens for page callback, intercepts and runs export
*/
function callback() {
if ( get_current_screen()->id != 'export' )
return;
if ( !isset( $_GET['type'] ) || $_GET['type'] != 'jekyll' )
return;
if ( !current_user_can( 'manage_options' ) )
return;
$this->export();
exit();
}
/**
* Add menu option to tools list
*/
function register_menu() {
add_management_page( __( 'Export to Jekyll', 'jekyll-export' ), __( 'Export to Jekyll', 'jekyll-export' ), 'manage_options', 'export.php?type=jekyll' );
}
/**
* Get an array of all post and page IDs
* Note: We don't use core's get_posts as it doesn't scale as well on large sites
*/
function get_posts() {
global $wpdb;
return $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ('post', 'page' )" );
}
/**
* Convert a posts meta data (both post_meta and the fields in wp_posts) to key value pairs for export
*/
function convert_meta( $post ) {
$output = array(
'id' => $post->ID,
'title' => get_the_title( $post ),
'author' => get_userdata( $post->post_author )->display_name,
'excerpt' => $post->post_excerpt,
'layout' => get_post_type( $post ),
'guid' => $post->guid
);
//preserve exact permalink, since Jekyll doesn't support redirection
if ( 'page' != $post->post_type ) {
$output[ 'permalink' ] = str_replace( home_url(), '', get_permalink( $post ) );
}
//convert traditional post_meta values, hide hidden values
foreach ( get_post_custom( $post->ID ) as $key => $value ) {
if ( substr( $key, 0, 1 ) == '_' )
continue;
$output[ $key ] = $value;
}
return $output;
}
/**
* Convert post taxonomies for export
*/
function convert_terms( $post ) {
$output = array();
foreach ( get_taxonomies( array( 'object_type' => array( get_post_type( $post ) ) ) ) as $tax ) {
$terms = wp_get_post_terms( $post, $tax );
//convert tax name for Jekyll
switch ( $tax ) {
case 'post_tag':
$tax = 'tags';
break;
case 'category':
$tax = 'categories';
break;
}
if ( $tax == 'post_format' ) {
$output['format'] = get_post_format( $post );
} else {
$output[ $tax ] = wp_list_pluck( $terms, 'name' );
}
}
return $output;
}
/**
* Convert the main post content to Markdown.
*/
function convert_content( $post ) {
$content = apply_filters( 'the_content', $post->post_content );
$converter = new Markdownify\ConverterExtra;
$markdown = $converter->parseString( $content );
if ( false !== strpos( $markdown, '[]: ' ) ) {
// faulty links; return plain HTML
return $content;
}
return $markdown;
}
/**
* Loop through and convert all posts to MD files with YAML headers
*/
function convert_posts() {
global $post;
foreach ( $this->get_posts() as $postID ) {
$post = get_post( $postID );
setup_postdata( $post );
$meta = array_merge( $this->convert_meta( $post ), $this->convert_terms( $postID ) );
// remove falsy values, which just add clutter
foreach ( $meta as $key => $value ) {
if ( !is_numeric( $value ) && !$value )
unset( $meta[ $key ] );
}
// Jekyll doesn't like word-wrapped permalinks
$output = Spyc::YAMLDump( $meta, false, 0 );
$output .= "---\n";
$output .= $this->convert_content( $post );
$this->write( $output, $post );
}
}
function filesystem_method_filter() {
return 'direct';
}
function init_temp_dir() {
global $wp_filesystem;
add_filter( 'filesystem_method', array( &$this, 'filesystem_method_filter' ) );
WP_Filesystem();
$temp_dir = get_temp_dir();
$this->dir = $temp_dir . 'wp-jekyll-' . md5( time() ) . '/';
$this->zip = $temp_dir . 'wp-jekyll.zip';
$wp_filesystem->mkdir( $this->dir );
$wp_filesystem->mkdir( $this->dir . '_posts/' );
$wp_filesystem->mkdir( $this->dir . 'wp-content/' );
}
/**
* Main function, bootstraps, converts, and cleans up
*/
function export() {
$this->init_temp_dir();
$this->convert_options();
$this->convert_posts();
$this->convert_uploads();
$this->zip();
$this->send();
$this->cleanup();
}
/**
* Convert options table to _config.yml file
*/
function convert_options() {
global $wp_filesystem;
$options = wp_load_alloptions();
foreach ( $options as $key => &$option ) {
if ( substr( $key, 0, 1 ) == '_' )
unset( $options[$key] );
//strip site and blog from key names, since it will become site. when in Jekyll
foreach ( $this->rename_options as $rename ) {
$len = strlen( $rename );
if ( substr( $key, 0, $len ) != $rename )
continue;
$this->rename_key( $options, $key, substr( $key, $len ) );
}
$option = maybe_unserialize( $option );
}
foreach ( $options as $key => $value ) {
if ( !in_array( $key, $this->options ) )
unset( $options[ $key ] );
}
$output = Spyc::YAMLDump( $options );
//strip starting "---"
$output = substr( $output, 4 );
$wp_filesystem->put_contents( $this->dir . '_config.yml', $output );
}
/**
* Write file to temp dir
*/
function write( $output, $post ) {
global $wp_filesystem;
if ( get_post_type( $post ) == 'page' ) {
$wp_filesystem->mkdir( $this->dir . $post->post_name );
$filename = $post->post_name . '/index.md';
} else {
$filename = '_posts/' . date( 'Y-m-d', strtotime( $post->post_date ) ) . '-' . $post->post_name . '.md';
}
$wp_filesystem->put_contents( $this->dir . $filename, $output );
}
/**
* Zip temp dir
*/
function zip() {
$zippy = Zippy::load();
$zippy->create($this->zip, array("./" => $this->dir), true);
}
/**
* Send headers and zip file to user
*/
function send() {
//send headers
@header( 'Content-Type: application/zip' );
@header( "Content-Disposition: attachment; filename=jekyll-export.zip" );
@header( 'Content-Length: ' . filesize( $this->zip ) );
//read file
flush();
readfile( $this->zip );
}
/**
* Clear temp files
*/
function cleanup( ) {
global $wp_filesystem;
$wp_filesystem->delete( $this->dir, true );
$wp_filesystem->delete( $this->zip );
}
/**
* Rename an assoc. array's key without changing the order
*/
function rename_key( &$array, $from, $to ) {
$keys = array_keys( $array );
$index = array_search( $from, $keys );
if ( $index === false )
return;
$keys[ $index ] = $to;
$array = array_combine( $keys, $array );
}
function convert_uploads() {
$upload_dir = wp_upload_dir();
$source = $upload_dir['basedir'];
$dest = $this->dir . str_replace( trailingslashit( get_site_url() ), '', $upload_dir['baseurl'] );
$this->copy_recursive( $source, $dest );
}
/**
* Copy a file, or recursively copy a folder and its contents
*
* @author Aidan Lister <[email protected]>
* @version 1.0.1
* @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
* @param string $source Source path
* @param string $dest Destination path
* @return bool Returns TRUE on success, FALSE on failure
*/
function copy_recursive($source, $dest) {
global $wp_filesystem;
// Check for symlinks
if ( is_link( $source ) ) {
return symlink( readlink( $source ), $dest );
}
// Simple copy for a file
if ( is_file( $source ) ) {
return $wp_filesystem->copy( $source, $dest );
}
// Make destination directory
if ( !is_dir($dest) ) {
$wp_filesystem->mkdir( $dest );
}
// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Deep copy directories
$this->copy_recursive("$source/$entry", "$dest/$entry");
}
// Clean up
$dir->close();
return true;
}
}
global $jekyll_export;
$jekyll_export = new Jekyll_Export();