PluginProbe
Export Themes / trunk
Export Themes vtrunk
3.0.1 trunk 1.0 1.5 2.0 2.1 2.2 3.0
wp-clone-template / main.php

main.php in Export Themes trunk, at main.php

309 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Export Themes
4 Plugin URI: https://milardovich.com.ar/wordpress/
5 Description: A simple plugin to export templates in a .zip file and then install them from the same package in other servers.
6 Version: 3.0.1
7 Requires at least: 6.0
8 Requires PHP: 7.4
9 Author: Sergio Milardovich
10 Author URI: https://milardovich.com.ar/
11 Text Domain: wp-clone-template
12 License: GPLv2 or later
13 License URI: https://www.gnu.org/licenses/gpl-2.0.html
14 */
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 define( 'WPCT_PATH', plugin_dir_path( __FILE__ ) );
21
22 /**
23 * Capability required to export a theme.
24 *
25 * An export is the theme's full PHP source, so this is deliberately stricter
26 * than the manage_options check used up to 2.12.
27 */
28 function wpct_capability() {
29 return apply_filters( 'wpct_capability', 'install_themes' );
30 }
31
32 /*
33 * ---------------------------------------------------------------------------
34 * Admin screen
35 * ---------------------------------------------------------------------------
36 */
37
38 add_action( 'admin_menu', 'wpct_admin_menu' );
39 function wpct_admin_menu() {
40 $hook = add_theme_page(
41 __( 'Export Themes', 'wp-clone-template' ),
42 __( 'Export', 'wp-clone-template' ),
43 wpct_capability(),
44 'clone_template',
45 'wpct_render_page'
46 );
47
48 if ( $hook ) {
49 // Runs before any admin markup, so the download can send its own headers.
50 add_action( 'load-' . $hook, 'wpct_maybe_export' );
51 }
52 }
53
54 function wpct_render_page() {
55 include_once WPCT_PATH . 'views/export.php';
56 }
57
58 /**
59 * List of exportable themes, keyed by stylesheet directory.
60 */
61 function wpct_get_themes() {
62 $themes = array();
63
64 foreach ( wp_get_themes() as $stylesheet => $theme ) {
65 $themes[ $stylesheet ] = $theme->display( 'Name' );
66 }
67
68 natcasesort( $themes );
69
70 return $themes;
71 }
72
73 function wpct_show_error( $message ) {
74 add_settings_error( 'wpct', 'wpct_error', $message, 'error' );
75 }
76
77 /*
78 * ---------------------------------------------------------------------------
79 * Export
80 * ---------------------------------------------------------------------------
81 */
82
83 /**
84 * Handle the export request, if this page load is one.
85 */
86 function wpct_maybe_export() {
87 if ( ! isset( $_POST['export_template'] ) ) {
88 return;
89 }
90
91 if ( ! current_user_can( wpct_capability() ) ) {
92 wp_die( esc_html__( 'You are not allowed to export themes.', 'wp-clone-template' ), 403 );
93 }
94
95 // 2.12 accepted this POST from anywhere, with no nonce at all.
96 check_admin_referer( 'wpct_export' );
97
98 $requested = isset( $_POST['Templates'] ) ? sanitize_text_field( wp_unslash( $_POST['Templates'] ) ) : '';
99
100 // Never trust the value as a path: it must be one of the installed themes.
101 // 2.12 pasted it straight into a filesystem path, so "../../.." walked out
102 // of the themes directory.
103 $themes = wpct_get_themes();
104 if ( '' === $requested || ! isset( $themes[ $requested ] ) ) {
105 wpct_show_error( __( 'That theme does not exist.', 'wp-clone-template' ) );
106 return;
107 }
108
109 $theme = wp_get_theme( $requested );
110 if ( ! $theme->exists() ) {
111 wpct_show_error( __( 'That theme does not exist.', 'wp-clone-template' ) );
112 return;
113 }
114
115 $archive = wpct_build_archive( $theme );
116
117 if ( is_wp_error( $archive ) ) {
118 wpct_show_error( $archive->get_error_message() );
119 return;
120 }
121
122 wpct_send_archive( $archive, $theme->get_stylesheet() );
123 }
124
125 /**
126 * Files that never belong in a distributable theme package.
127 */
128 function wpct_should_skip( $relative_path ) {
129 $skip = false;
130
131 foreach ( array( '.git', '.svn', '.hg', 'node_modules', '.DS_Store' ) as $needle ) {
132 if ( $relative_path === $needle
133 || 0 === strpos( $relative_path, $needle . '/' )
134 || false !== strpos( $relative_path, '/' . $needle . '/' )
135 || substr( $relative_path, - ( strlen( $needle ) + 1 ) ) === '/' . $needle ) {
136 $skip = true;
137 break;
138 }
139 }
140
141 /**
142 * Filters whether a file is left out of the export.
143 *
144 * @param bool $skip
145 * @param string $relative_path Path relative to the theme directory.
146 */
147 return apply_filters( 'wpct_should_skip_file', $skip, $relative_path );
148 }
149
150 /**
151 * Zip the theme into a temporary file outside the web root.
152 *
153 * Up to 2.12 the archive was written into the plugin's own directory and the
154 * browser was redirected to it, which left every exported theme downloadable by
155 * anyone who knew the URL.
156 *
157 * @return string|WP_Error Absolute path to the archive.
158 */
159 function wpct_build_archive( $theme ) {
160 $source = untrailingslashit( $theme->get_stylesheet_directory() );
161
162 if ( ! is_dir( $source ) || ! is_readable( $source ) ) {
163 return new WP_Error( 'wpct_unreadable', __( 'The theme directory cannot be read.', 'wp-clone-template' ) );
164 }
165
166 $stylesheet = $theme->get_stylesheet();
167 $destination = trailingslashit( get_temp_dir() ) . wp_unique_filename( get_temp_dir(), $stylesheet . '.zip' );
168
169 $files = wpct_collect_files( $source );
170 if ( empty( $files ) ) {
171 return new WP_Error( 'wpct_empty', __( 'The theme directory is empty.', 'wp-clone-template' ) );
172 }
173
174 if ( class_exists( 'ZipArchive' ) ) {
175 $result = wpct_zip_with_ziparchive( $destination, $stylesheet, $files );
176 } else {
177 $result = wpct_zip_with_pclzip( $destination, $stylesheet, $source, $files );
178 }
179
180 if ( is_wp_error( $result ) ) {
181 if ( file_exists( $destination ) ) {
182 unlink( $destination );
183 }
184 return $result;
185 }
186
187 return $destination;
188 }
189
190 /**
191 * Absolute paths of every file to include, keyed by their path inside the zip.
192 */
193 function wpct_collect_files( $source ) {
194 $files = array();
195
196 $iterator = new RecursiveIteratorIterator(
197 new RecursiveDirectoryIterator( $source, FilesystemIterator::SKIP_DOTS ),
198 RecursiveIteratorIterator::SELF_FIRST
199 );
200
201 foreach ( $iterator as $item ) {
202 $absolute = $item->getPathname();
203 $relative = ltrim( str_replace( $source, '', $absolute ), '/\\' );
204 $relative = str_replace( '\\', '/', $relative );
205
206 if ( '' === $relative || wpct_should_skip( $relative ) ) {
207 continue;
208 }
209
210 if ( $item->isFile() && $item->isReadable() ) {
211 $files[ $relative ] = $absolute;
212 }
213 }
214
215 return $files;
216 }
217
218 function wpct_zip_with_ziparchive( $destination, $stylesheet, $files ) {
219 $zip = new ZipArchive();
220
221 if ( true !== $zip->open( $destination, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
222 return new WP_Error( 'wpct_zip_open', __( 'The zip file could not be created.', 'wp-clone-template' ) );
223 }
224
225 $zip->addEmptyDir( $stylesheet );
226
227 foreach ( $files as $relative => $absolute ) {
228 $zip->addFile( $absolute, $stylesheet . '/' . $relative );
229 }
230
231 if ( ! $zip->close() ) {
232 return new WP_Error( 'wpct_zip_write', __( 'The zip file could not be written.', 'wp-clone-template' ) );
233 }
234
235 return true;
236 }
237
238 /**
239 * Fallback for the rare install without the zip extension.
240 *
241 * WordPress ships its own maintained copy of PclZip; the one bundled with this
242 * plugin until 2.12 still used a PHP 4 constructor and died under PHP 8.
243 */
244 function wpct_zip_with_pclzip( $destination, $stylesheet, $source, $files ) {
245 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
246
247 $zip = new PclZip( $destination );
248
249 $added = $zip->add(
250 array_values( $files ),
251 PCLZIP_OPT_REMOVE_PATH,
252 dirname( $source ),
253 PCLZIP_OPT_ADD_PATH,
254 ''
255 );
256
257 if ( 0 === $added ) {
258 return new WP_Error( 'wpct_zip_write', $zip->errorInfo( true ) );
259 }
260
261 return true;
262 }
263
264 /**
265 * Stream the archive to the browser and delete it.
266 */
267 function wpct_send_archive( $path, $stylesheet ) {
268 nocache_headers();
269 header( 'Content-Type: application/zip' );
270 header( 'Content-Disposition: attachment; filename="' . $stylesheet . '.zip"' );
271 header( 'Content-Length: ' . filesize( $path ) );
272 header( 'X-Content-Type-Options: nosniff' );
273
274 // Nothing else may end up inside the zip.
275 while ( ob_get_level() ) {
276 ob_end_clean();
277 }
278
279 readfile( $path );
280 unlink( $path );
281
282 exit;
283 }
284
285 /*
286 * ---------------------------------------------------------------------------
287 * Activation
288 * ---------------------------------------------------------------------------
289 */
290
291 register_activation_hook( __FILE__, 'wpct_activate' );
292 function wpct_activate() {
293 // Versions up to 2.12 left exported themes in a world-readable directory
294 // inside the plugin. Clean it up on upgrade.
295 $legacy = WPCT_PATH . 'templates';
296
297 if ( ! is_dir( $legacy ) ) {
298 return;
299 }
300
301 foreach ( (array) glob( $legacy . '/*.zip' ) as $file ) {
302 if ( is_file( $file ) ) {
303 unlink( $file );
304 }
305 }
306
307 @rmdir( $legacy );
308 }
309