]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-theme-upgrader.php
WordPress 4.6.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-theme-upgrader.php
1 <?php
2 /**
3  * Upgrade API: Theme_Upgrader class
4  *
5  * @package WordPress
6  * @subpackage Upgrader
7  * @since 4.6.0
8  */
9
10 /**
11  * Core class used for upgrading/installing themes.
12  *
13  * It is designed to upgrade/install themes from a local zip, remote zip URL,
14  * or uploaded zip file.
15  *
16  * @since 2.8.0
17  * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
18  *
19  * @see WP_Upgrader
20  */
21 class Theme_Upgrader extends WP_Upgrader {
22
23         /**
24          * Result of the theme upgrade offer.
25          *
26          * @since 2.8.0
27          * @access public
28          * @var array|WP_Error $result
29          * @see WP_Upgrader::$result
30          */
31         public $result;
32
33         /**
34          * Whether multiple themes are being upgraded/installed in bulk.
35          *
36          * @since 2.9.0
37          * @access public
38          * @var bool $bulk
39          */
40         public $bulk = false;
41
42         /**
43          * Initialize the upgrade strings.
44          *
45          * @since 2.8.0
46          * @access public
47          */
48         public function upgrade_strings() {
49                 $this->strings['up_to_date'] = __('The theme is at the latest version.');
50                 $this->strings['no_package'] = __('Update package not available.');
51                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
52                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
53                 $this->strings['remove_old'] = __('Removing the old version of the theme&#8230;');
54                 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
55                 $this->strings['process_failed'] = __('Theme update failed.');
56                 $this->strings['process_success'] = __('Theme updated successfully.');
57         }
58
59         /**
60          * Initialize the install strings.
61          *
62          * @since 2.8.0
63          * @access public
64          */
65         public function install_strings() {
66                 $this->strings['no_package'] = __('Install package not available.');
67                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
68                 $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
69                 $this->strings['installing_package'] = __('Installing the theme&#8230;');
70                 $this->strings['no_files'] = __('The theme contains no files.');
71                 $this->strings['process_failed'] = __('Theme install failed.');
72                 $this->strings['process_success'] = __('Theme installed successfully.');
73                 /* translators: 1: theme name, 2: version */
74                 $this->strings['process_success_specific'] = __('Successfully installed the theme <strong>%1$s %2$s</strong>.');
75                 $this->strings['parent_theme_search'] = __('This theme requires a parent theme. Checking if it is installed&#8230;');
76                 /* translators: 1: theme name, 2: version */
77                 $this->strings['parent_theme_prepare_install'] = __('Preparing to install <strong>%1$s %2$s</strong>&#8230;');
78                 /* translators: 1: theme name, 2: version */
79                 $this->strings['parent_theme_currently_installed'] = __('The parent theme, <strong>%1$s %2$s</strong>, is currently installed.');
80                 /* translators: 1: theme name, 2: version */
81                 $this->strings['parent_theme_install_success'] = __('Successfully installed the parent theme, <strong>%1$s %2$s</strong>.');
82                 $this->strings['parent_theme_not_found'] = __('<strong>The parent theme could not be found.</strong> You will need to install the parent theme, <strong>%s</strong>, before you can use this child theme.');
83         }
84
85         /**
86          * Check if a child theme is being installed and we need to install its parent.
87          *
88          * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::install().
89          *
90          * @since 3.4.0
91          * @access public
92          *
93          * @param bool  $install_result
94          * @param array $hook_extra
95          * @param array $child_result
96          * @return type
97          */
98         public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
99                 // Check to see if we need to install a parent theme
100                 $theme_info = $this->theme_info();
101
102                 if ( ! $theme_info->parent() )
103                         return $install_result;
104
105                 $this->skin->feedback( 'parent_theme_search' );
106
107                 if ( ! $theme_info->parent()->errors() ) {
108                         $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display('Name'), $theme_info->parent()->display('Version') );
109                         // We already have the theme, fall through.
110                         return $install_result;
111                 }
112
113                 // We don't have the parent theme, let's install it.
114                 $api = themes_api('theme_information', array('slug' => $theme_info->get('Template'), 'fields' => array('sections' => false, 'tags' => false) ) ); //Save on a bit of bandwidth.
115
116                 if ( ! $api || is_wp_error($api) ) {
117                         $this->skin->feedback( 'parent_theme_not_found', $theme_info->get('Template') );
118                         // Don't show activate or preview actions after install
119                         add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
120                         return $install_result;
121                 }
122
123                 // Backup required data we're going to override:
124                 $child_api = $this->skin->api;
125                 $child_success_message = $this->strings['process_success'];
126
127                 // Override them
128                 $this->skin->api = $api;
129                 $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];//, $api->name, $api->version);
130
131                 $this->skin->feedback('parent_theme_prepare_install', $api->name, $api->version);
132
133                 add_filter('install_theme_complete_actions', '__return_false', 999); // Don't show any actions after installing the theme.
134
135                 // Install the parent theme
136                 $parent_result = $this->run( array(
137                         'package' => $api->download_link,
138                         'destination' => get_theme_root(),
139                         'clear_destination' => false, //Do not overwrite files.
140                         'clear_working' => true
141                 ) );
142
143                 if ( is_wp_error($parent_result) )
144                         add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
145
146                 // Start cleaning up after the parents installation
147                 remove_filter('install_theme_complete_actions', '__return_false', 999);
148
149                 // Reset child's result and data
150                 $this->result = $child_result;
151                 $this->skin->api = $child_api;
152                 $this->strings['process_success'] = $child_success_message;
153
154                 return $install_result;
155         }
156
157         /**
158          * Don't display the activate and preview actions to the user.
159          *
160          * Hooked to the {@see 'install_theme_complete_actions'} filter by
161          * Theme_Upgrader::check_parent_theme_filter() when installing
162          * a child theme and installing the parent theme fails.
163          *
164          * @since 3.4.0
165          * @access public
166          *
167          * @param array $actions Preview actions.
168          * @return array
169          */
170         public function hide_activate_preview_actions( $actions ) {
171                 unset($actions['activate'], $actions['preview']);
172                 return $actions;
173         }
174
175         /**
176          * Install a theme package.
177          *
178          * @since 2.8.0
179          * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
180          * @access public
181          *
182          * @param string $package The full local path or URI of the package.
183          * @param array  $args {
184          *     Optional. Other arguments for installing a theme package. Default empty array.
185          *
186          *     @type bool $clear_update_cache Whether to clear the updates cache if successful.
187          *                                    Default true.
188          * }
189          *
190          * @return bool|WP_Error True if the install was successful, false or a WP_Error object otherwise.
191          */
192         public function install( $package, $args = array() ) {
193
194                 $defaults = array(
195                         'clear_update_cache' => true,
196                 );
197                 $parsed_args = wp_parse_args( $args, $defaults );
198
199                 $this->init();
200                 $this->install_strings();
201
202                 add_filter('upgrader_source_selection', array($this, 'check_package') );
203                 add_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'), 10, 3);
204                 // Clear cache so wp_update_themes() knows about the new theme.
205                 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
206
207                 $this->run( array(
208                         'package' => $package,
209                         'destination' => get_theme_root(),
210                         'clear_destination' => false, //Do not overwrite files.
211                         'clear_working' => true,
212                         'hook_extra' => array(
213                                 'type' => 'theme',
214                                 'action' => 'install',
215                         ),
216                 ) );
217
218                 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
219                 remove_filter('upgrader_source_selection', array($this, 'check_package') );
220                 remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
221
222                 if ( ! $this->result || is_wp_error($this->result) )
223                         return $this->result;
224
225                 // Refresh the Theme Update information
226                 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
227
228                 return true;
229         }
230
231         /**
232          * Upgrade a theme.
233          *
234          * @since 2.8.0
235          * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
236          * @access public
237          *
238          * @param string $theme The theme slug.
239          * @param array  $args {
240          *     Optional. Other arguments for upgrading a theme. Default empty array.
241          *
242          *     @type bool $clear_update_cache Whether to clear the update cache if successful.
243          *                                    Default true.
244          * }
245          * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
246          */
247         public function upgrade( $theme, $args = array() ) {
248
249                 $defaults = array(
250                         'clear_update_cache' => true,
251                 );
252                 $parsed_args = wp_parse_args( $args, $defaults );
253
254                 $this->init();
255                 $this->upgrade_strings();
256
257                 // Is an update available?
258                 $current = get_site_transient( 'update_themes' );
259                 if ( !isset( $current->response[ $theme ] ) ) {
260                         $this->skin->before();
261                         $this->skin->set_result(false);
262                         $this->skin->error( 'up_to_date' );
263                         $this->skin->after();
264                         return false;
265                 }
266
267                 $r = $current->response[ $theme ];
268
269                 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
270                 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
271                 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
272                 // Clear cache so wp_update_themes() knows about the new theme.
273                 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
274
275                 $this->run( array(
276                         'package' => $r['package'],
277                         'destination' => get_theme_root( $theme ),
278                         'clear_destination' => true,
279                         'clear_working' => true,
280                         'hook_extra' => array(
281                                 'theme' => $theme,
282                                 'type' => 'theme',
283                                 'action' => 'update',
284                         ),
285                 ) );
286
287                 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
288                 remove_filter('upgrader_pre_install', array($this, 'current_before'));
289                 remove_filter('upgrader_post_install', array($this, 'current_after'));
290                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
291
292                 if ( ! $this->result || is_wp_error($this->result) )
293                         return $this->result;
294
295                 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
296
297                 return true;
298         }
299
300         /**
301          * Upgrade several themes at once.
302          *
303          * @since 3.0.0
304          * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
305          * @access public
306          *
307          * @param array $themes The theme slugs.
308          * @param array $args {
309          *     Optional. Other arguments for upgrading several themes at once. Default empty array.
310          *
311          *     @type bool $clear_update_cache Whether to clear the update cache if successful.
312          *                                    Default true.
313          * }
314          * @return array[]|false An array of results, or false if unable to connect to the filesystem.
315          */
316         public function bulk_upgrade( $themes, $args = array() ) {
317
318                 $defaults = array(
319                         'clear_update_cache' => true,
320                 );
321                 $parsed_args = wp_parse_args( $args, $defaults );
322
323                 $this->init();
324                 $this->bulk = true;
325                 $this->upgrade_strings();
326
327                 $current = get_site_transient( 'update_themes' );
328
329                 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
330                 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
331                 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
332                 // Clear cache so wp_update_themes() knows about the new theme.
333                 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
334
335                 $this->skin->header();
336
337                 // Connect to the Filesystem first.
338                 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
339                 if ( ! $res ) {
340                         $this->skin->footer();
341                         return false;
342                 }
343
344                 $this->skin->bulk_header();
345
346                 // Only start maintenance mode if:
347                 // - running Multisite and there are one or more themes specified, OR
348                 // - a theme with an update available is currently in use.
349                 // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
350                 $maintenance = ( is_multisite() && ! empty( $themes ) );
351                 foreach ( $themes as $theme )
352                         $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
353                 if ( $maintenance )
354                         $this->maintenance_mode(true);
355
356                 $results = array();
357
358                 $this->update_count = count($themes);
359                 $this->update_current = 0;
360                 foreach ( $themes as $theme ) {
361                         $this->update_current++;
362
363                         $this->skin->theme_info = $this->theme_info($theme);
364
365                         if ( !isset( $current->response[ $theme ] ) ) {
366                                 $this->skin->set_result(true);
367                                 $this->skin->before();
368                                 $this->skin->feedback( 'up_to_date' );
369                                 $this->skin->after();
370                                 $results[$theme] = true;
371                                 continue;
372                         }
373
374                         // Get the URL to the zip file
375                         $r = $current->response[ $theme ];
376
377                         $result = $this->run( array(
378                                 'package' => $r['package'],
379                                 'destination' => get_theme_root( $theme ),
380                                 'clear_destination' => true,
381                                 'clear_working' => true,
382                                 'is_multi' => true,
383                                 'hook_extra' => array(
384                                         'theme' => $theme
385                                 ),
386                         ) );
387
388                         $results[$theme] = $this->result;
389
390                         // Prevent credentials auth screen from displaying multiple times
391                         if ( false === $result )
392                                 break;
393                 } //end foreach $plugins
394
395                 $this->maintenance_mode(false);
396
397                 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
398                 do_action( 'upgrader_process_complete', $this, array(
399                         'action' => 'update',
400                         'type' => 'theme',
401                         'bulk' => true,
402                         'themes' => $themes,
403                 ) );
404
405                 $this->skin->bulk_footer();
406
407                 $this->skin->footer();
408
409                 // Cleanup our hooks, in case something else does a upgrade on this connection.
410                 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
411                 remove_filter('upgrader_pre_install', array($this, 'current_before'));
412                 remove_filter('upgrader_post_install', array($this, 'current_after'));
413                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
414
415                 // Refresh the Theme Update information
416                 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
417
418                 return $results;
419         }
420
421         /**
422          * Check that the package source contains a valid theme.
423          *
424          * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
425          * It will return an error if the theme doesn't have style.css or index.php
426          * files.
427          *
428          * @since 3.3.0
429          * @access public
430          *
431          * @global WP_Filesystem_Base $wp_filesystem Subclass
432          *
433          * @param string $source The full path to the package source.
434          * @return string|WP_Error The source or a WP_Error.
435          */
436         public function check_package( $source ) {
437                 global $wp_filesystem;
438
439                 if ( is_wp_error($source) )
440                         return $source;
441
442                 // Check the folder contains a valid theme
443                 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
444                 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
445                         return $source;
446
447                 // A proper archive should have a style.css file in the single subdirectory
448                 if ( ! file_exists( $working_directory . 'style.css' ) ) {
449                         return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'],
450                                 /* translators: %s: style.css */
451                                 sprintf( __( 'The theme is missing the %s stylesheet.' ),
452                                         '<code>style.css</code>'
453                                 )
454                         );
455                 }
456
457                 $info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
458
459                 if ( empty( $info['Name'] ) ) {
460                         return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'],
461                                 /* translators: %s: style.css */
462                                 sprintf( __( 'The %s stylesheet doesn&#8217;t contain a valid theme header.' ),
463                                         '<code>style.css</code>'
464                                 )
465                         );
466                 }
467
468                 // If it's not a child theme, it must have at least an index.php to be legit.
469                 if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
470                         return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'],
471                                 /* translators: %s: index.php */
472                                 sprintf( __( 'The theme is missing the %s file.' ),
473                                         '<code>index.php</code>'
474                                 )
475                         );
476                 }
477
478                 return $source;
479         }
480
481         /**
482          * Turn on maintenance mode before attempting to upgrade the current theme.
483          *
484          * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
485          * Theme_Upgrader::bulk_upgrade().
486          *
487          * @since 2.8.0
488          * @access public
489          *
490          * @param bool|WP_Error  $return
491          * @param array          $theme
492          * @return bool|WP_Error
493          */
494         public function current_before($return, $theme) {
495                 if ( is_wp_error($return) )
496                         return $return;
497
498                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
499
500                 if ( $theme != get_stylesheet() ) //If not current
501                         return $return;
502                 //Change to maintenance mode now.
503                 if ( ! $this->bulk )
504                         $this->maintenance_mode(true);
505
506                 return $return;
507         }
508
509         /**
510          * Turn off maintenance mode after upgrading the current theme.
511          *
512          * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
513          * and Theme_Upgrader::bulk_upgrade().
514          *
515          * @since 2.8.0
516          * @access public
517          *
518          * @param bool|WP_Error  $return
519          * @param array          $theme
520          * @return bool|WP_Error
521          */
522         public function current_after($return, $theme) {
523                 if ( is_wp_error($return) )
524                         return $return;
525
526                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
527
528                 if ( $theme != get_stylesheet() ) // If not current
529                         return $return;
530
531                 // Ensure stylesheet name hasn't changed after the upgrade:
532                 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
533                         wp_clean_themes_cache();
534                         $stylesheet = $this->result['destination_name'];
535                         switch_theme( $stylesheet );
536                 }
537
538                 //Time to remove maintenance mode
539                 if ( ! $this->bulk )
540                         $this->maintenance_mode(false);
541                 return $return;
542         }
543
544         /**
545          * Delete the old theme during an upgrade.
546          *
547          * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
548          * and Theme_Upgrader::bulk_upgrade().
549          *
550          * @since 2.8.0
551          * @access public
552          *
553          * @global WP_Filesystem_Base $wp_filesystem Subclass
554          *
555          * @param bool   $removed
556          * @param string $local_destination
557          * @param string $remote_destination
558          * @param array  $theme
559          * @return bool
560          */
561         public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
562                 global $wp_filesystem;
563
564                 if ( is_wp_error( $removed ) )
565                         return $removed; // Pass errors through.
566
567                 if ( ! isset( $theme['theme'] ) )
568                         return $removed;
569
570                 $theme = $theme['theme'];
571                 $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
572                 if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
573                         if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
574                                 return false;
575                 }
576
577                 return true;
578         }
579
580         /**
581          * Get the WP_Theme object for a theme.
582          *
583          * @since 2.8.0
584          * @since 3.0.0 The `$theme` argument was added.
585          * @access public
586          *
587          * @param string $theme The directory name of the theme. This is optional, and if not supplied,
588          *                      the directory name from the last result will be used.
589          * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
590          *                        and the last result isn't set.
591          */
592         public function theme_info($theme = null) {
593
594                 if ( empty($theme) ) {
595                         if ( !empty($this->result['destination_name']) )
596                                 $theme = $this->result['destination_name'];
597                         else
598                                 return false;
599                 }
600                 return wp_get_theme( $theme );
601         }
602
603 }