1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: WP Twitter Importer |
---|
4 | Plugin URI: http://wordpress.org/extend/plugins/wp-twitter-importer/ |
---|
5 | Description: |
---|
6 | Author: wordpressdotorg |
---|
7 | Author URI: http://wordpress.org/ |
---|
8 | Version: 0.1 |
---|
9 | Text Domain: wp-twitter-importer |
---|
10 | License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | */ |
---|
12 | |
---|
13 | if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) |
---|
14 | return; |
---|
15 | |
---|
16 | // Load Importer API |
---|
17 | require_once ABSPATH . 'wp-admin/includes/import.php'; |
---|
18 | |
---|
19 | if ( ! class_exists( 'WP_Importer' ) ) { |
---|
20 | $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; |
---|
21 | if ( file_exists( $class_wp_importer ) ) |
---|
22 | require $class_wp_importer; |
---|
23 | } |
---|
24 | |
---|
25 | class WP_Twitter_Import extends WP_Importer { |
---|
26 | |
---|
27 | /** |
---|
28 | * Display import page title |
---|
29 | */ |
---|
30 | function header() { |
---|
31 | echo '<div class="wrap">'; |
---|
32 | screen_icon(); |
---|
33 | echo '<h2>' . __( 'Import Twitter', 'wp-twitter-importer' ) . '</h2>'; |
---|
34 | |
---|
35 | $updates = get_plugin_updates(); |
---|
36 | $basename = plugin_basename( __FILE__ ); |
---|
37 | if ( isset( $updates[$basename] ) ) { |
---|
38 | $update = $updates[$basename]; |
---|
39 | echo '<div class="error"><p><strong>'; |
---|
40 | printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wp-twitter-importer' ), $update->update->new_version ); |
---|
41 | echo '</strong></p></div>'; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Close div.wrap |
---|
47 | */ |
---|
48 | function footer() { |
---|
49 | echo '</div>'; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Display introductory text and file upload form |
---|
54 | */ |
---|
55 | function greet() { |
---|
56 | echo '<div class="narrow">'; |
---|
57 | echo '<p>'.__( 'Howdy! Upload your Twitter archive data file and we’ll import the status updates into this site.', 'wp-twitter-importer' ).'</p>'; |
---|
58 | echo '<p>'.__( 'Choose a Twitter archive data (.zip) file to upload, then click Upload file and import.', 'wp-twitter-importer' ).'</p>'; |
---|
59 | wp_import_upload_form( 'admin.php?import=twitter&step=1' ); |
---|
60 | echo '</div>'; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Registered callback function for the WordPress Importer |
---|
65 | * |
---|
66 | * Manages the three separate stages of the WXR import process |
---|
67 | */ |
---|
68 | function dispatch() { |
---|
69 | $this->header(); |
---|
70 | |
---|
71 | $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; |
---|
72 | switch ( $step ) { |
---|
73 | case 0: |
---|
74 | $this->greet(); |
---|
75 | break; |
---|
76 | case 1: |
---|
77 | //check_admin_referer( 'import-upload' ); |
---|
78 | $working_dir = $this->handle_upload(); |
---|
79 | $this->parse_files( $working_dir ); |
---|
80 | break; |
---|
81 | } |
---|
82 | |
---|
83 | $this->footer(); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Handles the zip upload and initial parsing of the file |
---|
88 | * |
---|
89 | * @return bool False if error uploading or invalid file, true otherwise |
---|
90 | */ |
---|
91 | function handle_upload() { |
---|
92 | global $wp_filesystem; |
---|
93 | |
---|
94 | if ( ! isset( $_FILES['import'] ) ) { |
---|
95 | $file['error'] = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ); |
---|
96 | return $file; |
---|
97 | } |
---|
98 | |
---|
99 | $url = 'admin.php?import=twitter'; |
---|
100 | |
---|
101 | if ( false === ($credentials = request_filesystem_credentials($url, '', false, ABSPATH)) ) |
---|
102 | return false; |
---|
103 | |
---|
104 | if ( ! WP_Filesystem($credentials, ABSPATH) ) { |
---|
105 | request_filesystem_credentials($url, '', true, ABSPATH); //Failed to connect, Error and request again |
---|
106 | return false; |
---|
107 | } |
---|
108 | |
---|
109 | $import_folder = $wp_filesystem->wp_content_dir() . 'import/'; |
---|
110 | |
---|
111 | //Clean up contents of upgrade directory beforehand. |
---|
112 | $import_files = $wp_filesystem->dirlist( $import_folder ); |
---|
113 | if ( ! empty( $import_files ) ) { |
---|
114 | foreach ( $import_files as $file ) |
---|
115 | $wp_filesystem->delete( $import_folder . $file['name'], true ); |
---|
116 | } |
---|
117 | |
---|
118 | //We need a working directory |
---|
119 | $working_dir = $import_folder . basename( $_FILES['import']['name'], '.zip' ); |
---|
120 | |
---|
121 | // Clean up working directory |
---|
122 | if ( $wp_filesystem->is_dir( $working_dir ) ) |
---|
123 | $wp_filesystem->delete( $working_dir, true ); |
---|
124 | |
---|
125 | // Unzip package to working directory |
---|
126 | $result = unzip_file( $_FILES['import']['tmp_name'], $working_dir ); //TODO optimizations, Copy when Move/Rename would suffice? |
---|
127 | |
---|
128 | // Once extracted, delete the import file. |
---|
129 | unlink( $_FILES['import']['tmp_name'] ); |
---|
130 | |
---|
131 | if ( is_wp_error($result) ) { |
---|
132 | $wp_filesystem->delete($working_dir, true); |
---|
133 | if ( 'incompatible_archive' == $result->get_error_code() ) { |
---|
134 | return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() ); |
---|
135 | } |
---|
136 | return $result; |
---|
137 | } |
---|
138 | |
---|
139 | return $working_dir; |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | public function parse_files( $working_dir ) { |
---|
144 | global $wp_filesystem; |
---|
145 | $tweets_dir = $working_dir . '/data/js/tweets/'; |
---|
146 | |
---|
147 | $tweet_files = $wp_filesystem->dirlist( $tweets_dir ); |
---|
148 | $tweets = array(); |
---|
149 | if ( ! empty( $tweet_files ) ) { |
---|
150 | foreach ( $tweet_files as $file ) { |
---|
151 | $tweet_json = file_get_contents( $tweets_dir . $file['name'] ); |
---|
152 | $tweet_json = preg_replace( '/^[^\[]*/', '', $tweet_json ); |
---|
153 | $tweet_json = json_decode( $tweet_json ); |
---|
154 | if ( is_array( $tweet_json ) ) |
---|
155 | $tweets = array_merge( $tweet_json, $tweets ); |
---|
156 | } |
---|
157 | } |
---|
158 | echo '<pre>', var_dump( $tweets ), '</pre>'; |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | function wp_twitter_importer_init() { |
---|
164 | load_plugin_textdomain( 'wp-twitter-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
---|
165 | |
---|
166 | /** |
---|
167 | * WordPress Importer object for registering the import callback |
---|
168 | * @global WP_Twittter_Import $wp_twitter_import |
---|
169 | */ |
---|
170 | $GLOBALS['wp_twitter_import'] = new WP_Twitter_Import(); |
---|
171 | register_importer( 'twitter', 'Twitter', __( 'Import your twitter status udpates.', 'wp-twitter-importer' ), array( $GLOBALS['wp_twitter_import'], 'dispatch' ) ); |
---|
172 | } |
---|
173 | add_action( 'admin_init', 'wp_twitter_importer_init' ); |
---|