~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-admin/includes/import.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * WordPress Administration Importer API.
 
4
 *
 
5
 * @package WordPress
 
6
 * @subpackage Administration
 
7
 */
 
8
 
 
9
/**
 
10
 * Retrieve list of importers.
 
11
 *
 
12
 * @since 2.0.0
 
13
 *
 
14
 * @return array
 
15
 */
 
16
function get_importers() {
 
17
        global $wp_importers;
 
18
        if ( is_array( $wp_importers ) ) {
 
19
                uasort( $wp_importers, '_usort_by_first_member' );
 
20
        }
 
21
        return $wp_importers;
 
22
}
 
23
 
 
24
/**
 
25
 * Sorts a multidimensional array by first member of each top level member
 
26
 *
 
27
 * Used by uasort() as a callback, should not be used directly.
 
28
 *
 
29
 * @since 2.9.0
 
30
 * @access private
 
31
 *
 
32
 * @param array $a
 
33
 * @param array $b
 
34
 * @return int
 
35
 */
 
36
function _usort_by_first_member( $a, $b ) {
 
37
        return strnatcasecmp( $a[0], $b[0] );
 
38
}
 
39
 
 
40
/**
 
41
 * Register importer for WordPress.
 
42
 *
 
43
 * @since 2.0.0
 
44
 *
 
45
 * @param string $id Importer tag. Used to uniquely identify importer.
 
46
 * @param string $name Importer name and title.
 
47
 * @param string $description Importer description.
 
48
 * @param callback $callback Callback to run.
 
49
 * @return WP_Error Returns WP_Error when $callback is WP_Error.
 
50
 */
 
51
function register_importer( $id, $name, $description, $callback ) {
 
52
        global $wp_importers;
 
53
        if ( is_wp_error( $callback ) )
 
54
                return $callback;
 
55
        $wp_importers[$id] = array ( $name, $description, $callback );
 
56
}
 
57
 
 
58
/**
 
59
 * Cleanup importer.
 
60
 *
 
61
 * Removes attachment based on ID.
 
62
 *
 
63
 * @since 2.0.0
 
64
 *
 
65
 * @param string $id Importer ID.
 
66
 */
 
67
function wp_import_cleanup( $id ) {
 
68
        wp_delete_attachment( $id );
 
69
}
 
70
 
 
71
/**
 
72
 * Handle importer uploading and add attachment.
 
73
 *
 
74
 * @since 2.0.0
 
75
 *
 
76
 * @return array Uploaded file's details on success, error message on failure
 
77
 */
 
78
function wp_import_handle_upload() {
 
79
        if ( !isset($_FILES['import']) ) {
 
80
                $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.' );
 
81
                return $file;
 
82
        }
 
83
 
 
84
        $overrides = array( 'test_form' => false, 'test_type' => false );
 
85
        $_FILES['import']['name'] .= '.txt';
 
86
        $file = wp_handle_upload( $_FILES['import'], $overrides );
 
87
 
 
88
        if ( isset( $file['error'] ) )
 
89
                return $file;
 
90
 
 
91
        $url = $file['url'];
 
92
        $type = $file['type'];
 
93
        $file = $file['file'];
 
94
        $filename = basename( $file );
 
95
 
 
96
        // Construct the object array
 
97
        $object = array( 'post_title' => $filename,
 
98
                'post_content' => $url,
 
99
                'post_mime_type' => $type,
 
100
                'guid' => $url,
 
101
                'context' => 'import',
 
102
                'post_status' => 'private'
 
103
        );
 
104
 
 
105
        // Save the data
 
106
        $id = wp_insert_attachment( $object, $file );
 
107
 
 
108
        /*
 
109
         * Schedule a cleanup for one day from now in case of failed
 
110
         * import or missing wp_import_cleanup() call.
 
111
         */
 
112
        wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
 
113
 
 
114
        return array( 'file' => $file, 'id' => $id );
 
115
}
 
116
 
 
117
/**
 
118
 * Returns a list from WordPress.org of popular importer plugins.
 
119
 *
 
120
 * @since 3.5.0
 
121
 *
 
122
 * @return array Importers with metadata for each.
 
123
 */
 
124
function wp_get_popular_importers() {
 
125
        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
 
126
 
 
127
        $locale = get_locale();
 
128
        $popular_importers = get_site_transient( 'popular_importers_' . $locale );
 
129
 
 
130
        if ( ! $popular_importers ) {
 
131
                $url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' );
 
132
                $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
 
133
                $response = wp_remote_get( $url, $options );
 
134
                $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
 
135
 
 
136
                if ( is_array( $popular_importers ) )
 
137
                        set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS );
 
138
                else
 
139
                        $popular_importers = false;
 
140
        }
 
141
 
 
142
        if ( is_array( $popular_importers ) ) {
 
143
                // If the data was received as translated, return it as-is.
 
144
                if ( $popular_importers['translated'] )
 
145
                        return $popular_importers['importers'];
 
146
 
 
147
                foreach ( $popular_importers['importers'] as &$importer ) {
 
148
                        $importer['description'] = translate( $importer['description'] );
 
149
                        if ( $importer['name'] != 'WordPress' )
 
150
                                $importer['name'] = translate( $importer['name'] );
 
151
                }
 
152
                return $popular_importers['importers'];
 
153
        }
 
154
 
 
155
        return array(
 
156
                // slug => name, description, plugin slug, and register_importer() slug
 
157
                'blogger' => array(
 
158
                        'name' => __( 'Blogger' ),
 
159
                        'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ),
 
160
                        'plugin-slug' => 'blogger-importer',
 
161
                        'importer-id' => 'blogger',
 
162
                ),
 
163
                'wpcat2tag' => array(
 
164
                        'name' => __( 'Categories and Tags Converter' ),
 
165
                        'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ),
 
166
                        'plugin-slug' => 'wpcat2tag-importer',
 
167
                        'importer-id' => 'wp-cat2tag',
 
168
                ),
 
169
                'livejournal' => array(
 
170
                        'name' => __( 'LiveJournal' ),
 
171
                        'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ),
 
172
                        'plugin-slug' => 'livejournal-importer',
 
173
                        'importer-id' => 'livejournal',
 
174
                ),
 
175
                'movabletype' => array(
 
176
                        'name' => __( 'Movable Type and TypePad' ),
 
177
                        'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ),
 
178
                        'plugin-slug' => 'movabletype-importer',
 
179
                        'importer-id' => 'mt',
 
180
                ),
 
181
                'opml' => array(
 
182
                        'name' => __( 'Blogroll' ),
 
183
                        'description' => __( 'Install the blogroll importer to import links in OPML format.' ),
 
184
                        'plugin-slug' => 'opml-importer',
 
185
                        'importer-id' => 'opml',
 
186
                ),
 
187
                'rss' => array(
 
188
                        'name' => __( 'RSS' ),
 
189
                        'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ),
 
190
                        'plugin-slug' => 'rss-importer',
 
191
                        'importer-id' => 'rss',
 
192
                ),
 
193
                'tumblr' => array(
 
194
                        'name' => __( 'Tumblr' ),
 
195
                        'description' => __( 'Install the Tumblr importer to import posts &amp; media from Tumblr using their API.' ),
 
196
                        'plugin-slug' => 'tumblr-importer',
 
197
                        'importer-id' => 'tumblr',
 
198
                ),
 
199
                'wordpress' => array(
 
200
                        'name' => 'WordPress',
 
201
                        'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
 
202
                        'plugin-slug' => 'wordpress-importer',
 
203
                        'importer-id' => 'wordpress',
 
204
                ),
 
205
        );
 
206
}