]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-theme-upgrader.php
WordPress 4.7.2
[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                 if ( $parsed_args['clear_update_cache'] ) {
205                         // Clear cache so wp_update_themes() knows about the new theme.
206                         add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
207                 }
208
209                 $this->run( array(
210                         'package' => $package,
211                         'destination' => get_theme_root(),
212                         'clear_destination' => false, //Do not overwrite files.
213                         'clear_working' => true,
214                         'hook_extra' => array(
215                                 'type' => 'theme',
216                                 'action' => 'install',
217                         ),
218                 ) );
219
220                 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
221                 remove_filter('upgrader_source_selection', array($this, 'check_package') );
222                 remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
223
224                 if ( ! $this->result || is_wp_error($this->result) )
225                         return $this->result;
226
227                 // Refresh the Theme Update information
228                 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
229
230                 return true;
231         }
232
233         /**
234          * Upgrade a theme.
235          *
236          * @since 2.8.0
237          * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
238          * @access public
239          *
240          * @param string $theme The theme slug.
241          * @param array  $args {
242          *     Optional. Other arguments for upgrading a theme. Default empty array.
243          *
244          *     @type bool $clear_update_cache Whether to clear the update cache if successful.
245          *                                    Default true.
246          * }
247          * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
248          */
249         public function upgrade( $theme, $args = array() ) {
250
251                 $defaults = array(
252                         'clear_update_cache' => true,
253                 );
254                 $parsed_args = wp_parse_args( $args, $defaults );
255
256                 $this->init();
257                 $this->upgrade_strings();
258
259                 // Is an update available?
260                 $current = get_site_transient( 'update_themes' );
261                 if ( !isset( $current->response[ $theme ] ) ) {
262                         $this->skin->before();
263                         $this->skin->set_result(false);
264                         $this->skin->error( 'up_to_date' );
265                         $this->skin->after();
266                         return false;
267                 }
268
269                 $r = $current->response[ $theme ];
270
271                 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
272                 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
273                 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
274                 if ( $parsed_args['clear_update_cache'] ) {
275                         // Clear cache so wp_update_themes() knows about the new theme.
276                         add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
277                 }
278
279                 $this->run( array(
280                         'package' => $r['package'],
281                         'destination' => get_theme_root( $theme ),
282                         'clear_destination' => true,
283                         'clear_working' => true,
284                         'hook_extra' => array(
285                                 'theme' => $theme,
286                                 'type' => 'theme',
287                                 'action' => 'update',
288                         ),
289                 ) );
290
291                 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
292                 remove_filter('upgrader_pre_install', array($this, 'current_before'));
293                 remove_filter('upgrader_post_install', array($this, 'current_after'));
294                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
295
296                 if ( ! $this->result || is_wp_error($this->result) )
297                         return $this->result;
298
299                 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
300
301                 return true;
302         }
303
304         /**
305          * Upgrade several themes at once.
306          *
307          * @since 3.0.0
308          * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
309          * @access public
310          *
311          * @param array $themes The theme slugs.
312          * @param array $args {
313          *     Optional. Other arguments for upgrading several themes at once. Default empty array.
314          *
315          *     @type bool $clear_update_cache Whether to clear the update cache if successful.
316          *                                    Default true.
317          * }
318          * @return array[]|false An array of results, or false if unable to connect to the filesystem.
319          */
320         public function bulk_upgrade( $themes, $args = array() ) {
321
322                 $defaults = array(
323                         'clear_update_cache' => true,
324                 );
325                 $parsed_args = wp_parse_args( $args, $defaults );
326
327                 $this->init();
328                 $this->bulk = true;
329                 $this->upgrade_strings();
330
331                 $current = get_site_transient( 'update_themes' );
332
333                 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
334                 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
335                 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
336
337                 $this->skin->header();
338
339                 // Connect to the Filesystem first.
340                 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
341                 if ( ! $res ) {
342                         $this->skin->footer();
343                         return false;
344                 }
345
346                 $this->skin->bulk_header();
347
348                 // Only start maintenance mode if:
349                 // - running Multisite and there are one or more themes specified, OR
350                 // - a theme with an update available is currently in use.
351                 // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
352                 $maintenance = ( is_multisite() && ! empty( $themes ) );
353                 foreach ( $themes as $theme )
354                         $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
355                 if ( $maintenance )
356                         $this->maintenance_mode(true);
357
358                 $results = array();
359
360                 $this->update_count = count($themes);
361                 $this->update_current = 0;
362                 foreach ( $themes as $theme ) {
363                         $this->update_current++;
364
365                         $this->skin->theme_info = $this->theme_info($theme);
366
367                         if ( !isset( $current->response[ $theme ] ) ) {
368                                 $this->skin->set_result(true);
369                                 $this->skin->before();
370                                 $this->skin->feedback( 'up_to_date' );
371                                 $this->skin->after();
372                                 $results[$theme] = true;
373                                 continue;
374                         }
375
376                         // Get the URL to the zip file
377                         $r = $current->response[ $theme ];
378
379                         $result = $this->run( array(
380                                 'package' => $r['package'],
381                                 'destination' => get_theme_root( $theme ),
382                                 'clear_destination' => true,
383                                 'clear_working' => true,
384                                 'is_multi' => true,
385                                 'hook_extra' => array(
386                                         'theme' => $theme
387                                 ),
388                         ) );
389
390                         $results[$theme] = $this->result;
391
392                         // Prevent credentials auth screen from displaying multiple times
393                         if ( false === $result )
394                                 break;
395                 } //end foreach $plugins
396
397                 $this->maintenance_mode(false);
398
399                 // Refresh the Theme Update information
400                 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
401
402                 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
403                 do_action( 'upgrader_process_complete', $this, array(
404                         'action' => 'update',
405                         'type' => 'theme',
406                         'bulk' => true,
407                         'themes' => $themes,
408                 ) );
409
410                 $this->skin->bulk_footer();
411
412                 $this->skin->footer();
413
414                 // Cleanup our hooks, in case something else does a upgrade on this connection.
415                 remove_filter('upgrader_pre_install', array($this, 'current_before'));
416                 remove_filter('upgrader_post_install', array($this, 'current_after'));
417                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
418
419                 return $results;
420         }
421
422         /**
423          * Check that the package source contains a valid theme.
424          *
425          * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
426          * It will return an error if the theme doesn't have style.css or index.php
427          * files.
428          *
429          * @since 3.3.0
430          * @access public
431          *
432          * @global WP_Filesystem_Base $wp_filesystem Subclass
433          *
434          * @param string $source The full path to the package source.
435          * @return string|WP_Error The source or a WP_Error.
436          */
437         public function check_package( $source ) {
438                 global $wp_filesystem;
439
440                 if ( is_wp_error($source) )
441                         return $source;
442
443                 // Check the folder contains a valid theme
444                 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
445                 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
446                         return $source;
447
448                 // A proper archive should have a style.css file in the single subdirectory
449                 if ( ! file_exists( $working_directory . 'style.css' ) ) {
450                         return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'],
451                                 /* translators: %s: style.css */
452                                 sprintf( __( 'The theme is missing the %s stylesheet.' ),
453                                         '<code>style.css</code>'
454                                 )
455                         );
456                 }
457
458                 $info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
459
460                 if ( empty( $info['Name'] ) ) {
461                         return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'],
462                                 /* translators: %s: style.css */
463                                 sprintf( __( 'The %s stylesheet doesn&#8217;t contain a valid theme header.' ),
464                                         '<code>style.css</code>'
465                                 )
466                         );
467                 }
468
469                 // If it's not a child theme, it must have at least an index.php to be legit.
470                 if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
471                         return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'],
472                                 /* translators: %s: index.php */
473                                 sprintf( __( 'The theme is missing the %s file.' ),
474                                         '<code>index.php</code>'
475                                 )
476                         );
477                 }
478
479                 return $source;
480         }
481
482         /**
483          * Turn on maintenance mode before attempting to upgrade the current theme.
484          *
485          * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
486          * Theme_Upgrader::bulk_upgrade().
487          *
488          * @since 2.8.0
489          * @access public
490          *
491          * @param bool|WP_Error  $return
492          * @param array          $theme
493          * @return bool|WP_Error
494          */
495         public function current_before($return, $theme) {
496                 if ( is_wp_error($return) )
497                         return $return;
498
499                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
500
501                 if ( $theme != get_stylesheet() ) //If not current
502                         return $return;
503                 //Change to maintenance mode now.
504                 if ( ! $this->bulk )
505                         $this->maintenance_mode(true);
506
507                 return $return;
508         }
509
510         /**
511          * Turn off maintenance mode after upgrading the current theme.
512          *
513          * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
514          * and Theme_Upgrader::bulk_upgrade().
515          *
516          * @since 2.8.0
517          * @access public
518          *
519          * @param bool|WP_Error  $return
520          * @param array          $theme
521          * @return bool|WP_Error
522          */
523         public function current_after($return, $theme) {
524                 if ( is_wp_error($return) )
525                         return $return;
526
527                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
528
529                 if ( $theme != get_stylesheet() ) // If not current
530                         return $return;
531
532                 // Ensure stylesheet name hasn't changed after the upgrade:
533                 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
534                         wp_clean_themes_cache();
535                         $stylesheet = $this->result['destination_name'];
536                         switch_theme( $stylesheet );
537                 }
538
539                 //Time to remove maintenance mode
540                 if ( ! $this->bulk )
541                         $this->maintenance_mode(false);
542                 return $return;
543         }
544
545         /**
546          * Delete the old theme during an upgrade.
547          *
548          * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
549          * and Theme_Upgrader::bulk_upgrade().
550          *
551          * @since 2.8.0
552          * @access public
553          *
554          * @global WP_Filesystem_Base $wp_filesystem Subclass
555          *
556          * @param bool   $removed
557          * @param string $local_destination
558          * @param string $remote_destination
559          * @param array  $theme
560          * @return bool
561          */
562         public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
563                 global $wp_filesystem;
564
565                 if ( is_wp_error( $removed ) )
566                         return $removed; // Pass errors through.
567
568                 if ( ! isset( $theme['theme'] ) )
569                         return $removed;
570
571                 $theme = $theme['theme'];
572                 $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
573                 if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
574                         if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
575                                 return false;
576                 }
577
578                 return true;
579         }
580
581         /**
582          * Get the WP_Theme object for a theme.
583          *
584          * @since 2.8.0
585          * @since 3.0.0 The `$theme` argument was added.
586          * @access public
587          *
588          * @param string $theme The directory name of the theme. This is optional, and if not supplied,
589          *                      the directory name from the last result will be used.
590          * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
591          *                        and the last result isn't set.
592          */
593         public function theme_info($theme = null) {
594
595                 if ( empty($theme) ) {
596                         if ( !empty($this->result['destination_name']) )
597                                 $theme = $this->result['destination_name'];
598                         else
599                                 return false;
600                 }
601                 return wp_get_theme( $theme );
602         }
603
604 }