├── .gitignore ├── cosmicjs-wp-export.zip ├── README.md └── cosmicjs-export.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /cosmicjs-wp-export.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmicjs/cosmicjs-wp-export/master/cosmicjs-wp-export.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://cosmicjs.com/) 2 | === 3 | This plugin helps you export the content and files from your WordPress site into a JSON file. This file can then be uploaded into Cosmic JS. 4 | ## To export 5 | 1. Download the ```cosmicjs-wp-export.zip``` file in this repo. 6 | 2. Go to the Plugins section in the backend of your WordPress install. 7 | 3. Click Add new. 8 | 4. Click Upload Plugin. 9 | 5. Click choose file. Choose the downloaded ```cosmicjs-wp-export.zip``` file included in this repo. 10 | 6. Activate plugin. 11 | 7. Find a new link in under Settings in your WP sidebar nav titled "Cosmic JS Export". Click it. 12 | 8. Select the post types that you would like to export. 13 | 9. Submit. This will download a JSON formatted file with your all of your WordPress posts and media. 14 | 10. Go to your Cosmic JS account at https://cosmicjs.com 15 | 11. Add a new bucket or select a current bucket 16 | 12. Click the Import link in the Settings nav. 17 | 13. Upload the WordPress export JSON file. This will upload all of your posts that were selected as well as all upload all media from your WP site to your bucket on Cosmic JS. 18 | 14. Enjoy the freedom of content in the cloud. 19 | -------------------------------------------------------------------------------- /cosmicjs-export.php: -------------------------------------------------------------------------------- 1 | bucket = new stdClass(); 36 | global $wpdb; 37 | 38 | // Get object types 39 | $sql = "SELECT * FROM wp_posts WHERE post_type IN ('$types') AND post_status = 'publish' GROUP by post_type"; 40 | $wp_posts = $wpdb->get_results($sql); 41 | $cosmic->bucket->object_types = Array(); 42 | $num = 0; 43 | foreach($wp_posts as $post){ 44 | $type = $post->post_type; 45 | $type_obj = get_post_type_object( $type ); 46 | $cosmic->bucket->object_types[$num] = new stdClass(); 47 | if($type_obj){ 48 | $cosmic->bucket->object_types[$num]->title = $type_obj->labels->name; 49 | $cosmic->bucket->object_types[$num]->slug = $type_obj->name; 50 | $cosmic->bucket->object_types[$num]->singular = $type_obj->labels->singular_name; 51 | } else { 52 | $cosmic->bucket->object_types[$num]->title = ucfirst($type); 53 | $cosmic->bucket->object_types[$num]->slug = $type; 54 | $cosmic->bucket->object_types[$num]->singular = $type; 55 | } 56 | $num++; 57 | } 58 | 59 | // Get objects 60 | $sql = "SELECT * FROM wp_posts WHERE post_type IN ('$types') AND post_status = 'publish'"; 61 | $wp_posts = $wpdb->get_results($sql); 62 | $num_objects = 0; 63 | $cosmic->bucket->media = Array(); 64 | foreach($wp_posts as $key => $post){ 65 | $cosmic->bucket->objects[$num_objects] = new stdClass(); 66 | $cosmic->bucket->objects[$num_objects]->title = $post->post_title; 67 | $cosmic->bucket->objects[$num_objects]->content = $post->post_content; 68 | $cosmic->bucket->objects[$num_objects]->type_slug = $post->post_type; 69 | $cosmic->bucket->objects[$num_objects]->slug = $post->post_name; 70 | $num_objects++; 71 | } 72 | 73 | // Grab media 74 | $sql = "SELECT * FROM wp_posts WHERE post_type = 'attachment'"; 75 | $wp_media = $wpdb->get_results($sql); 76 | foreach($wp_media as $key => $item){ 77 | 78 | $ext = AppUtil::fileExt($item->post_mime_type); 79 | $name = $item->post_title . $ext; 80 | $og_name = $item->post_title . $ext; 81 | $cosmic->bucket->media[$key] = new stdClass(); 82 | $cosmic->bucket->media[$key]->name = $name; 83 | $cosmic->bucket->media[$key]->original_name = $og_name; 84 | $cosmic->bucket->media[$key]->type = $item->post_mime_type; 85 | $cosmic->bucket->media[$key]->location = str_replace("/" . $name, "", $item->guid); 86 | } 87 | 88 | $site_name = get_bloginfo('site'); 89 | $export_slug = AppUtil::makeSlug($site_name); 90 | 91 | header('Content-Type: application/json'); 92 | header('Content-Disposition: attachment; filename="' . $export_slug . '.json"'); 93 | echo json_encode($cosmic, JSON_PRETTY_PRINT); 94 | die(); 95 | } 96 | 97 | /* Cosmic JS export 98 | ==================================== */ 99 | class AppUtil 100 | { 101 | public static function fileExt($contentType) 102 | { 103 | $map = array( 104 | 'application/pdf' => '.pdf', 105 | 'application/zip' => '.zip', 106 | 'image/gif' => '.gif', 107 | 'image/jpeg' => '.jpg', 108 | 'image/png' => '.png', 109 | 'text/css' => '.css', 110 | 'text/html' => '.html', 111 | 'text/javascript' => '.js', 112 | 'text/plain' => '.txt', 113 | 'text/xml' => '.xml', 114 | ); 115 | if (isset($map[$contentType])) 116 | { 117 | return $map[$contentType]; 118 | } 119 | 120 | // HACKISH CATCH ALL (WHICH IN MY CASE IS 121 | // PREFERRED OVER THROWING AN EXCEPTION) 122 | $pieces = explode('/', $contentType); 123 | return '.' . array_pop($pieces); 124 | } 125 | 126 | public static function makeSlug($str){ 127 | $slug = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $str)); 128 | return $slug; 129 | } 130 | } 131 | 132 | // Testing 133 | 134 | // ini_set('display_errors',1); 135 | // error_reporting(E_ALL); 136 | 137 | function cosmicjs_export_output() { 138 | 139 | ?> 140 |