Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleanup whitespace and line endings, fixes #3

  • Loading branch information...
commit 72ba85e0ec6d193a045da89fa3dfa2ef5f21c89b 1 parent de519d2
Ben Balter authored

Showing 1 changed file with 278 additions and 278 deletions. Show diff stats Hide diff stats

  1. +278 278 jekyll-export.php
556 jekyll-export.php
@@ -25,399 +25,399 @@
25 25
26 26 class Jekyll_Export {
27 27
28   - private $zip_folder = 'jekyll-export/'; //folder zip file extracts to
  28 + private $zip_folder = 'jekyll-export/'; //folder zip file extracts to
29 29
30   - public $rename_options = array( 'site', 'blog' ); //strings to strip from option keys on export
  30 + public $rename_options = array( 'site', 'blog' ); //strings to strip from option keys on export
31 31
32   - public $options = array( //array of wp_options value to convert to _config.yml
33   - 'name',
34   - 'description',
35   - 'url'
36   - );
  32 + public $options = array( //array of wp_options value to convert to _config.yml
  33 + 'name',
  34 + 'description',
  35 + 'url'
  36 + );
37 37
38   - /**
39   - * Hook into WP Core
40   - */
41   - function __construct() {
  38 + /**
  39 + * Hook into WP Core
  40 + */
  41 + function __construct() {
42 42
43   - add_action( 'admin_menu', array( &$this, 'register_menu' ) );
44   - add_action( 'current_screen', array( &$this, 'callback' ) );
  43 + add_action( 'admin_menu', array( &$this, 'register_menu' ) );
  44 + add_action( 'current_screen', array( &$this, 'callback' ) );
45 45
46   - }
  46 + }
47 47
48   - /**
49   - * Listens for page callback, intercepts and runs export
50   - */
51   - function callback() {
  48 + /**
  49 + * Listens for page callback, intercepts and runs export
  50 + */
  51 + function callback() {
52 52
53   - if ( get_current_screen()->id != 'export' )
54   - return;
  53 + if ( get_current_screen()->id != 'export' )
  54 + return;
55 55
56   - if ( !isset( $_GET['type'] ) || $_GET['type'] != 'jekyll' )
57   - return;
  56 + if ( !isset( $_GET['type'] ) || $_GET['type'] != 'jekyll' )
  57 + return;
58 58
59   - if ( !current_user_can( 'manage_options' ) )
60   - return;
  59 + if ( !current_user_can( 'manage_options' ) )
  60 + return;
61 61
62   - $this->export();
63   - exit();
  62 + $this->export();
  63 + exit();
64 64
65   - }
  65 + }
66 66
67 67
68   - /**
69   - * Add menu option to tools list
70   - */
71   - function register_menu() {
  68 + /**
  69 + * Add menu option to tools list
  70 + */
  71 + function register_menu() {
72 72
73   - add_management_page( __( 'Export to Jekyll', 'jekyll-export' ), __( 'Export to Jekyll', 'jekyll-export' ), 'manage_options', 'export.php?type=jekyll' );
  73 + add_management_page( __( 'Export to Jekyll', 'jekyll-export' ), __( 'Export to Jekyll', 'jekyll-export' ), 'manage_options', 'export.php?type=jekyll' );
74 74
75   - }
  75 + }
76 76
77 77
78   - /**
79   - * Get an array of all post and page IDs
80   - * Note: We don't use core's get_posts as it doesn't scale as well on large sites
81   - */
82   - function get_posts() {
  78 + /**
  79 + * Get an array of all post and page IDs
  80 + * Note: We don't use core's get_posts as it doesn't scale as well on large sites
  81 + */
  82 + function get_posts() {
83 83
84   - global $wpdb;
85   - return $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ('post', 'page' )" );
  84 + global $wpdb;
  85 + return $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ('post', 'page' )" );
86 86
87   - }
  87 + }
88 88
89 89
90   - /**
91   - * Convert a posts meta data (both post_meta and the fields in wp_posts) to key value pairs for export
92   - */
93   - function convert_meta( $post ) {
  90 + /**
  91 + * Convert a posts meta data (both post_meta and the fields in wp_posts) to key value pairs for export
  92 + */
  93 + function convert_meta( $post ) {
94 94
95   - $output = array(
96   - 'title' => get_the_title( $post ),
97   - 'author' => get_userdata( $post->post_author )->display_name,
98   - 'excerpt' => $post->post_excerpt,
99   - 'layout' => get_post_type( $post ),
100   - );
  95 + $output = array(
  96 + 'title' => get_the_title( $post ),
  97 + 'author' => get_userdata( $post->post_author )->display_name,
  98 + 'excerpt' => $post->post_excerpt,
  99 + 'layout' => get_post_type( $post ),
  100 + );
101 101
102   - //preserve exact permalink, since Jekyll doesn't support redirection
103   - if ( 'page' != $post->post_type ) {
104   - $output[ 'permalink' ] = str_replace( home_url(), '', get_permalink( $post ) );
105   - }
  102 + //preserve exact permalink, since Jekyll doesn't support redirection
  103 + if ( 'page' != $post->post_type ) {
  104 + $output[ 'permalink' ] = str_replace( home_url(), '', get_permalink( $post ) );
  105 + }
106 106
107   - //convert traditional post_meta values, hide hidden values
108   - foreach ( get_post_custom( $post ) as $key => $value ) {
  107 + //convert traditional post_meta values, hide hidden values
  108 + foreach ( get_post_custom( $post ) as $key => $value ) {
109 109
110   - if ( substr( $key, 0, 1 ) == '_' )
111   - continue;
  110 + if ( substr( $key, 0, 1 ) == '_' )
  111 + continue;
112 112
113   - $output[ $key ] = $value;
  113 + $output[ $key ] = $value;
114 114
115   - }
  115 + }
116 116
117   - return $output;
118   - }
  117 + return $output;
  118 + }
119 119
120 120
121   - /**
122   - * Convert post taxonomies for export
123   - */
124   - function convert_terms( $post ) {
  121 + /**
  122 + * Convert post taxonomies for export
  123 + */
  124 + function convert_terms( $post ) {
125 125
126   - $output = array();
127   - foreach ( get_taxonomies( array( 'object_type' => array( get_post_type( $post ) ) ) ) as $tax ) {
  126 + $output = array();
  127 + foreach ( get_taxonomies( array( 'object_type' => array( get_post_type( $post ) ) ) ) as $tax ) {
128 128
129   - $terms = wp_get_post_terms( $post, $tax );
  129 + $terms = wp_get_post_terms( $post, $tax );
130 130
131   - //convert tax name for Jekyll
132   - switch ( $tax ) {
133   - case 'post_tag':
134   - $tax = 'tags';
135   - break;
136   - case 'category':
137   - $tax = 'categories';
138   - break;
139   - }
  131 + //convert tax name for Jekyll
  132 + switch ( $tax ) {
  133 + case 'post_tag':
  134 + $tax = 'tags';
  135 + break;
  136 + case 'category':
  137 + $tax = 'categories';
  138 + break;
  139 + }
140 140
141   - if ( $tax == 'post_format' ) {
142   - $output['format'] = get_post_format( $post );
143   - } else {
144   - $output[ $tax ] = wp_list_pluck( $terms, 'name' );
145   - }
146   - }
  141 + if ( $tax == 'post_format' ) {
  142 + $output['format'] = get_post_format( $post );
  143 + } else {
  144 + $output[ $tax ] = wp_list_pluck( $terms, 'name' );
  145 + }
  146 + }
147 147
148   - return $output;
149   - }
  148 + return $output;
  149 + }
150 150
151   - /**
152   - * Convert the main post content to Markdown.
153   - */
154   - function convert_content( $post ) {
155   - $md = new Markdownify_Extra;
  151 + /**
  152 + * Convert the main post content to Markdown.
  153 + */
  154 + function convert_content( $post ) {
  155 + $md = new Markdownify_Extra;
156 156
157   - return $md->parseString( apply_filters( 'the_content', $post->post_content ) );
158   - }
  157 + return $md->parseString( apply_filters( 'the_content', $post->post_content ) );
  158 + }
159 159
160   - /**
161   - * Loop through and convert all posts to MD files with YAML headers
162   - */
163   - function convert_posts() {
164   - global $post;
  160 + /**
  161 + * Loop through and convert all posts to MD files with YAML headers
  162 + */
  163 + function convert_posts() {
  164 + global $post;
165 165
166   - foreach ( $this->get_posts() as $postID ) {
167   - $post = get_post( $postID );
168   - setup_postdata( $post );
  166 + foreach ( $this->get_posts() as $postID ) {
  167 + $post = get_post( $postID );
  168 + setup_postdata( $post );
169 169
170   - $meta = array_merge( $this->convert_meta( $post ), $this->convert_terms( $postID ) );
  170 + $meta = array_merge( $this->convert_meta( $post ), $this->convert_terms( $postID ) );
171 171
172   - // remove falsy values, which just add clutter
173   - foreach ( $meta as $key => $value ) {
174   - if ( !is_numeric( $value ) && !$value )
175   - unset( $meta[ $key ] );
176   - }
  172 + // remove falsy values, which just add clutter
  173 + foreach ( $meta as $key => $value ) {
  174 + if ( !is_numeric( $value ) && !$value )
  175 + unset( $meta[ $key ] );
  176 + }
177 177
178   - // Jekyll doesn't like word-wrapped permalinks
179   - $output = Spyc::YAMLDump( $meta, false, 80 );
  178 + // Jekyll doesn't like word-wrapped permalinks
  179 + $output = Spyc::YAMLDump( $meta, false, 80 );
180 180
181   - $output .= "---\n";
182   - $output .= $this->convert_content( $post );
183   - $this->write( $output, $post );
184   - }
  181 + $output .= "---\n";
  182 + $output .= $this->convert_content( $post );
  183 + $this->write( $output, $post );
  184 + }
185 185
186   - }
  186 + }
187 187
188   - /**
189   - * Main function, bootstraps, converts, and cleans up
190   - */
191   - function export() {
192   - define( 'DOING_JEKYLL_EXPORT', true );
  188 + /**
  189 + * Main function, bootstraps, converts, and cleans up
  190 + */
  191 + function export() {
  192 + define( 'DOING_JEKYLL_EXPORT', true );
193 193
194   - if ( !class_exists( 'spyc' ) )
195   - require_once dirname( __FILE__ ) . '/includes/spyc.php';
  194 + if ( !class_exists( 'spyc' ) )
  195 + require_once dirname( __FILE__ ) . '/includes/spyc.php';
196 196
197   - if ( !class_exists( 'Markdownify_Extra' ) )
198   - require_once dirname( __FILE__ ) . '/includes/markdownify/markdownify_extra.php';
  197 + if ( !class_exists( 'Markdownify_Extra' ) )
  198 + require_once dirname( __FILE__ ) . '/includes/markdownify/markdownify_extra.php';
199 199
200   - $this->dir = sys_get_temp_dir() . '/wp-jekyll-' . md5( time() ) . '/';
201   - $this->zip = sys_get_temp_dir() . '/wp-jekyll.zip';
202   - mkdir( $this->dir );
203   - mkdir( $this->dir . '_posts/' );
  200 + $this->dir = sys_get_temp_dir() . '/wp-jekyll-' . md5( time() ) . '/';
  201 + $this->zip = sys_get_temp_dir() . '/wp-jekyll.zip';
  202 + mkdir( $this->dir );
  203 + mkdir( $this->dir . '_posts/' );
204 204
205   - $this->convert_options();
206   - $this->convert_posts();
207   - $this->convert_uploads();
208   - $this->zip();
209   - $this->send();
210   - $this->cleanup();
  205 + $this->convert_options();
  206 + $this->convert_posts();
  207 + $this->convert_uploads();
  208 + $this->zip();
  209 + $this->send();
  210 + $this->cleanup();
211 211
212   - }
  212 + }
213 213
214 214
215   - /**
216   - * Convert options table to _config.yml file
217   - */
218   - function convert_options() {
  215 + /**
  216 + * Convert options table to _config.yml file
  217 + */
  218 + function convert_options() {
219 219
220   - $options = wp_load_alloptions();
221   - foreach ( $options as $key => &$option ) {
  220 + $options = wp_load_alloptions();
  221 + foreach ( $options as $key => &$option ) {
222 222
223   - if ( substr( $key, 0, 1 ) == '_' )
224   - unset( $options[$key] );
  223 + if ( substr( $key, 0, 1 ) == '_' )
  224 + unset( $options[$key] );
225 225
226   - //strip site and blog from key names, since it will become site. when in Jekyll
227   - foreach ( $this->rename_options as $rename ) {
  226 + //strip site and blog from key names, since it will become site. when in Jekyll
  227 + foreach ( $this->rename_options as $rename ) {
228 228
229   - $len = strlen( $rename );
230   - if ( substr( $key, 0, $len ) != $rename )
231   - continue;
  229 + $len = strlen( $rename );
  230 + if ( substr( $key, 0, $len ) != $rename )
  231 + continue;
232 232
233   - $this->rename_key( $options, $key, substr( $key, $len ) );
  233 + $this->rename_key( $options, $key, substr( $key, $len ) );
234 234
235   - }
  235 + }
236 236
237   - $option = maybe_unserialize( $option );
  237 + $option = maybe_unserialize( $option );
238 238
239   - }
  239 + }
240 240
241   - foreach ( $options as $key => $value ) {
  241 + foreach ( $options as $key => $value ) {
242 242
243   - if ( !in_array( $key, $this->options ) )
244   - unset( $options[ $key ] );
  243 + if ( !in_array( $key, $this->options ) )
  244 + unset( $options[ $key ] );
245 245
246   - }
  246 + }
247 247
248   - $output = Spyc::YAMLDump( $options );
  248 + $output = Spyc::YAMLDump( $options );
249 249
250   - //strip starting "---"
251   - $output = substr( $output, 4 );
  250 + //strip starting "---"
  251 + $output = substr( $output, 4 );
252 252
253   - file_put_contents( $this->dir . '_config.yml', $output );
  253 + file_put_contents( $this->dir . '_config.yml', $output );
254 254
255   - }
  255 + }
256 256
257 257
258   - /**
259   - * Write file to temp dir
260   - */
261   - function write( $output, $post ) {
  258 + /**
  259 + * Write file to temp dir
  260 + */
  261 + function write( $output, $post ) {
262 262
263   - if ( get_post_type( $post ) == 'page' ) {
264   - mkdir( $this->dir . $post->post_name );
265   - $filename = $post->post_name . '/index.md';
266   - } else {
267   - $filename = '_posts/' . date( 'Y-m-d', strtotime( $post->post_date ) ) . '-' . $post->post_name . '.md';
268   - }
  263 + if ( get_post_type( $post ) == 'page' ) {
  264 + mkdir( $this->dir . $post->post_name );
  265 + $filename = $post->post_name . '/index.md';
  266 + } else {
  267 + $filename = '_posts/' . date( 'Y-m-d', strtotime( $post->post_date ) ) . '-' . $post->post_name . '.md';
  268 + }
269 269
270   - file_put_contents( $this->dir . $filename, $output );
  270 + file_put_contents( $this->dir . $filename, $output );
271 271
272   - }
  272 + }
273 273
274 274
275   - /**
276   - * Zip temp dir
277   - */
278   - function zip() {
  275 + /**
  276 + * Zip temp dir
  277 + */
  278 + function zip() {
279 279
280   - //create zip
281   - $zip = new ZipArchive();
282   - $zip->open( $this->zip, ZIPARCHIVE::CREATE );
283   - $this->_zip( $this->dir, $zip );
284   - $zip->close();
  280 + //create zip
  281 + $zip = new ZipArchive();
  282 + $zip->open( $this->zip, ZIPARCHIVE::CREATE );
  283 + $this->_zip( $this->dir, $zip );
  284 + $zip->close();
285 285
286   - }
  286 + }
287 287
288 288
289   - /**
290   - * Helper function to add a file to the zip
291   - */
292   - function _zip( $dir, &$zip ) {
  289 + /**
  290 + * Helper function to add a file to the zip
  291 + */
  292 + function _zip( $dir, &$zip ) {
293 293
294   - //loop through all files in directory
295   - foreach ( glob( trailingslashit( $dir ) . '*' ) as $path ) {
  294 + //loop through all files in directory
  295 + foreach ( glob( trailingslashit( $dir ) . '*' ) as $path ) {
296 296
297   - if ( is_dir( $path ) ) {
298   - $this->_zip( $path, $zip );
299   - continue;
300   - }
  297 + if ( is_dir( $path ) ) {
  298 + $this->_zip( $path, $zip );
  299 + continue;
  300 + }
301 301
302   - //make path within zip relative to zip base, not server root
303   - $local_path = '/' . str_replace( $this->dir, $this->zip_folder, $path );
  302 + //make path within zip relative to zip base, not server root
  303 + $local_path = '/' . str_replace( $this->dir, $this->zip_folder, $path );
304 304
305   - //add file
306   - $zip->addFile( realpath( $path ), $local_path );
  305 + //add file
  306 + $zip->addFile( realpath( $path ), $local_path );
307 307
308   - }
  308 + }
309 309
310   - }
  310 + }
311 311
312 312
313   - /**
314   - * Send headers and zip file to user
315   - */
316   - function send() {
  313 + /**
  314 + * Send headers and zip file to user
  315 + */
  316 + function send() {
317 317
318   - //send headers
319   - header( 'Content-Type: application/zip' );
320   - header( "Content-Disposition: attachment; filename=jekyll-export.zip" );
321   - header( 'Content-Length: ' . filesize( $this->zip ) );
  318 + //send headers
  319 + header( 'Content-Type: application/zip' );
  320 + header( "Content-Disposition: attachment; filename=jekyll-export.zip" );
  321 + header( 'Content-Length: ' . filesize( $this->zip ) );
322 322
323   - //read file
324   - readfile( $this->zip );
  323 + //read file
  324 + readfile( $this->zip );
325 325
326   - }
  326 + }
327 327
328 328
329   - /**
330   - * Clear temp files
331   - */
332   - function cleanup( ) {
  329 + /**
  330 + * Clear temp files
  331 + */
  332 + function cleanup( ) {
333 333
334   - $this->rmdir_recursive( $this->dir );
335   - unlink( $this->zip );
  334 + $this->rmdir_recursive( $this->dir );
  335 + unlink( $this->zip );
336 336
337   - }
  337 + }
338 338
339 339
340   - /**
341   - * Rename an assoc. array's key without changing the order
342   - */
343   - function rename_key( &$array, $from, $to ) {
  340 + /**
  341 + * Rename an assoc. array's key without changing the order
  342 + */
  343 + function rename_key( &$array, $from, $to ) {
344 344
345   - $keys = array_keys( $array );
346   - $index = array_search( $from, $keys );
  345 + $keys = array_keys( $array );
  346 + $index = array_search( $from, $keys );
347 347
348   - if ( $index === false )
349   - return;
  348 + if ( $index === false )
  349 + return;
350 350
351   - $keys[ $index ] = $to;
352   - $array = array_combine( $keys, $array );
  351 + $keys[ $index ] = $to;
  352 + $array = array_combine( $keys, $array );
353 353
354 354
355   - }
  355 + }
356 356
357   - function rmdir_recursive( $dir ) {
  357 + function rmdir_recursive( $dir ) {
358 358
359   - foreach( glob($dir . '/*' ) as $file ) {
360   - if( is_dir( $file ) )
361   - $this->rmdir_recursive( $file );
362   - else
363   - unlink( $file );
364   - }
  359 + foreach( glob($dir . '/*' ) as $file ) {
  360 + if( is_dir( $file ) )
  361 + $this->rmdir_recursive( $file );
  362 + else
  363 + unlink( $file );
  364 + }
365 365
366   - rmdir( $dir );
  366 + rmdir( $dir );
367 367
368   - }
  368 + }
369 369
370   - function convert_uploads() {
  370 + function convert_uploads() {
371 371
372   - $upload_dir = wp_upload_dir();
373   - $this->copy_recursive( $upload_dir['basedir'], $this->dir . str_replace( trailingslashit( get_home_url() ), '', $upload_dir['baseurl'] ) );
  372 + $upload_dir = wp_upload_dir();
  373 + $this->copy_recursive( $upload_dir['basedir'], $this->dir . str_replace( trailingslashit( get_home_url() ), '', $upload_dir['baseurl'] ) );
374 374
375   - }
  375 + }
376 376
377   - /**
378   - * Copy a file, or recursively copy a folder and its contents
379   - *
380   - * @author Aidan Lister <aidan@php.net>
381   - * @version 1.0.1
382   - * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
383   - * @param string $source Source path
384   - * @param string $dest Destination path
385   - * @return bool Returns TRUE on success, FALSE on failure
386   - */
387   - function copy_recursive($source, $dest) {
  377 + /**
  378 + * Copy a file, or recursively copy a folder and its contents
  379 + *
  380 + * @author Aidan Lister <aidan@php.net>
  381 + * @version 1.0.1
  382 + * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
  383 + * @param string $source Source path
  384 + * @param string $dest Destination path
  385 + * @return bool Returns TRUE on success, FALSE on failure
  386 + */
  387 + function copy_recursive($source, $dest) {
388 388
389   - // Check for symlinks
390   - if ( is_link( $source ) ) {
391   - return symlink( readlink( $source ), $dest );
392   - }
  389 + // Check for symlinks
  390 + if ( is_link( $source ) ) {
  391 + return symlink( readlink( $source ), $dest );
  392 + }
393 393
394   - // Simple copy for a file
395   - if ( is_file( $source ) ) {
396   - return copy( $source, $dest );
397   - }
  394 + // Simple copy for a file
  395 + if ( is_file( $source ) ) {
  396 + return copy( $source, $dest );
  397 + }
398 398
399   - // Make destination directory
400   - if ( !is_dir($dest) ) {
401   - mkdir($dest, null, true );
402   - }
  399 + // Make destination directory
  400 + if ( !is_dir($dest) ) {
  401 + mkdir($dest, null, true );
  402 + }
403 403
404   - // Loop through the folder
405   - $dir = dir($source);
406   - while (false !== $entry = $dir->read()) {
407   - // Skip pointers
408   - if ($entry == '.' || $entry == '..') {
409   - continue;
410   - }
  404 + // Loop through the folder
  405 + $dir = dir($source);
  406 + while (false !== $entry = $dir->read()) {
  407 + // Skip pointers
  408 + if ($entry == '.' || $entry == '..') {
  409 + continue;
  410 + }
411 411
412   - // Deep copy directories
413   - $this->copy_recursive("$source/$entry", "$dest/$entry");
414   - }
  412 + // Deep copy directories
  413 + $this->copy_recursive("$source/$entry", "$dest/$entry");
  414 + }
415 415
416   - // Clean up
417   - $dir->close();
418   - return true;
  416 + // Clean up
  417 + $dir->close();
  418 + return true;
419 419
420   - }
  420 + }
421 421
422 422 }
423 423

0 comments on commit 72ba85e

Please sign in to comment.
Something went wrong with that request. Please try again.