| 1 | <?php |
| 2 | /** |
| 3 | * The Bulk handlers for the WordPress Upgrader |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Upgrader |
| 7 | * @since 4.4.1 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Abstract base class for Bulk Upgrades |
| 12 | * |
| 13 | * @since 4.4.1 |
| 14 | */ |
| 15 | abstract class WP_Bulk_Upgrader_Composite { |
| 16 | /** |
| 17 | * @var WP_Upgrader |
| 18 | */ |
| 19 | protected $upgrader; |
| 20 | |
| 21 | /** |
| 22 | * @var array Elements to upgrade |
| 23 | */ |
| 24 | protected $elements = array(); |
| 25 | |
| 26 | /** |
| 27 | * WP_Bulk_Upgrader_Composite constructor. |
| 28 | * |
| 29 | * @since 4.4.1 |
| 30 | * |
| 31 | * @param WP_Upgrader $upgrader Upgrader to report back to |
| 32 | * @param array $elements List of elements to be bulked. |
| 33 | * @param string $elementClass Class name to wrap elements in. Extended of WP_Bulk_Upgrader_Element. |
| 34 | */ |
| 35 | public function __construct( WP_Upgrader $upgrader, array $elements = array(), $elementClass ) { |
| 36 | if ( ! is_string( $elementClass ) ) { |
| 37 | throw new InvalidArgumentException( 'Expected element class to be string, got ' . gettype( $elementClass ) ); |
| 38 | } |
| 39 | |
| 40 | if ( ! class_exists( $elementClass ) ) { |
| 41 | throw new InvalidArgumentException( sprintf( 'Element class "%s" does not exist.', $elementClass ) ); |
| 42 | } |
| 43 | |
| 44 | $this->upgrader = $upgrader; |
| 45 | |
| 46 | if ( array() !== $elements ) { |
| 47 | foreach ( $elements as $element ) { |
| 48 | $this->elements[] = new $elementClass( $element, $upgrader ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | $this->upgrader->update_count = count( $this->elements ); |
| 53 | $this->upgrader->update_current = 0; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Runs the bulk upgrade |
| 58 | * |
| 59 | * @since 4.4.1 |
| 60 | * @access public |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function run() { |
| 65 | $this->upgrader->clean_upgrade_folder(); |
| 66 | |
| 67 | $this->register_on_skin(); |
| 68 | |
| 69 | $this->check_up_to_date(); |
| 70 | |
| 71 | # Work |
| 72 | $this->download(); |
| 73 | $this->unpack(); |
| 74 | $this->install(); |
| 75 | # Done |
| 76 | |
| 77 | $this->upgrader->maintenance_mode( false ); |
| 78 | $this->upgrader->update_current = false; |
| 79 | |
| 80 | return $this->get_results(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Shorthand retrieval of upgrader skin |
| 85 | * |
| 86 | * @since 4.4.1 |
| 87 | * @access protected |
| 88 | * |
| 89 | * @return null|WP_Upgrader_Skin |
| 90 | */ |
| 91 | protected function get_skin() { |
| 92 | return $this->upgrader->get_skin(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Set the current element as active on the upgrader |
| 97 | * |
| 98 | * @since 4.4.1 |
| 99 | * @access protected |
| 100 | * |
| 101 | * @param $element |
| 102 | */ |
| 103 | protected function current_element( $element ) { |
| 104 | $index = array_search( $element, $this->elements ); |
| 105 | |
| 106 | if ( false !== $index ) { |
| 107 | $this->upgrader->update_current = $index + 1; |
| 108 | } else { |
| 109 | $this->upgrader->update_current = false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * When the element is already up to date |
| 115 | * |
| 116 | * @since 4.4.1 |
| 117 | * @access protected |
| 118 | * |
| 119 | * @param $element |
| 120 | */ |
| 121 | protected function handle_up_to_date( $element ) { |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Optionally let the elements be sorted before installing |
| 126 | * |
| 127 | * @since 4.4.1 |
| 128 | * @acces protected |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | protected function sort_install_elements() { |
| 133 | return $this->elements; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Register elements on the skin |
| 138 | * |
| 139 | * @since 4.4.1 |
| 140 | * @access protected |
| 141 | */ |
| 142 | protected function register_on_skin() { |
| 143 | foreach ( $this->elements as $element ) { |
| 144 | $this->current_element( $element ); |
| 145 | |
| 146 | $skin = $this->get_skin(); |
| 147 | $skin->before(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check if any of the elements is already up to date |
| 153 | * |
| 154 | * @since 4.4.1 |
| 155 | * @access protected |
| 156 | */ |
| 157 | protected function check_up_to_date() { |
| 158 | /** @var $element WP_Bulk_Upgrader_Element */ |
| 159 | foreach ( $this->elements as $element ) { |
| 160 | if ( $element->is_up_to_date() ) { |
| 161 | $this->current_element( $element ); |
| 162 | |
| 163 | $this->handle_up_to_date( $element ); |
| 164 | |
| 165 | $skin = $this->get_skin(); |
| 166 | |
| 167 | $skin->feedback( 'up_to_date' ); |
| 168 | $skin->after(); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Download all the elements |
| 175 | * |
| 176 | * @since 4.4.1 |
| 177 | * @access protected |
| 178 | */ |
| 179 | protected function download() { |
| 180 | /** @var $element WP_Bulk_Upgrader_Element */ |
| 181 | foreach ( $this->elements as $element ) { |
| 182 | if ( $element->is_up_to_date() ) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | $this->current_element( $element ); |
| 187 | |
| 188 | $result = $element->download(); |
| 189 | if ( is_wp_error( $result ) ) { |
| 190 | $this->show_error( $result, $element ); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Unpack downloaded elements |
| 197 | * |
| 198 | * @since 4.4.1 |
| 199 | * @access protected |
| 200 | */ |
| 201 | protected function unpack() { |
| 202 | /** @var $element WP_Bulk_Upgrader_Element */ |
| 203 | foreach ( $this->elements as $element ) { |
| 204 | if ( $element->is_up_to_date() ) { |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | $this->current_element( $element ); |
| 209 | |
| 210 | $result = $element->unpack(); |
| 211 | if ( is_wp_error( $result ) ) { |
| 212 | $this->show_error( $result, $element ); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Install unpacked elements |
| 219 | * |
| 220 | * @since 4.4.1 |
| 221 | * @access protected |
| 222 | */ |
| 223 | protected function install() { |
| 224 | $elements = $this->sort_install_elements(); |
| 225 | |
| 226 | /** @var $element WP_Bulk_Upgrader_Element */ |
| 227 | foreach ( $elements as $element ) { |
| 228 | if ( $element->is_up_to_date() ) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | $this->current_element( $element ); |
| 233 | |
| 234 | $result = $element->install(); |
| 235 | |
| 236 | $skin = $this->get_skin(); |
| 237 | $skin->set_result( $result ); |
| 238 | if ( is_wp_error( $result ) ) { |
| 239 | $skin->error( $result ); |
| 240 | $skin->feedback( 'process_failed' ); |
| 241 | } else { |
| 242 | // Install succeeded. |
| 243 | $skin->feedback( 'process_success' ); |
| 244 | } |
| 245 | $skin->after(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get the results of the upgrade process |
| 251 | * |
| 252 | * @since 4.4.1 |
| 253 | * @access protected |
| 254 | * |
| 255 | * @return array |
| 256 | */ |
| 257 | protected function get_results() { |
| 258 | $results = array(); |
| 259 | |
| 260 | /** @var $element WP_Bulk_Upgrader_Element */ |
| 261 | foreach ( $this->elements as $element ) { |
| 262 | $index = $element->get_result_index(); |
| 263 | if ( false === $index ) { |
| 264 | $results[] = $element->get_result(); |
| 265 | } else { |
| 266 | $results[ $index ] = $element->get_result(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return $results; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Pass error to skin |
| 275 | * |
| 276 | * @since 4.4.1 |
| 277 | * @access protected |
| 278 | * |
| 279 | * @param $error |
| 280 | * @param $element |
| 281 | */ |
| 282 | protected function show_error( $error, $element ) { |
| 283 | $this->current_element( $element ); |
| 284 | |
| 285 | $skin = $this->get_skin(); |
| 286 | $skin->error( $error ); |
| 287 | $skin->after(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Abstract base class for Bulk Upgrade Elements |
| 293 | * |
| 294 | * @since 4.4.1 |
| 295 | */ |
| 296 | abstract class WP_Bulk_Upgrader_Element { |
| 297 | /** |
| 298 | * @var bool is this element already up-to-date |
| 299 | */ |
| 300 | protected $up_to_date = false; |
| 301 | /** |
| 302 | * @var bool has the package been downloaded |
| 303 | */ |
| 304 | protected $downloaded = false; |
| 305 | /** |
| 306 | * @var bool was the package unpacked properly |
| 307 | */ |
| 308 | protected $unpacked = false; |
| 309 | /** |
| 310 | * @var bool has the package been installed |
| 311 | */ |
| 312 | protected $installed = false; |
| 313 | |
| 314 | /** |
| 315 | * @var mixed identifier for this element (source) |
| 316 | */ |
| 317 | protected $name; |
| 318 | /** |
| 319 | * @var WP_Upgrader upgrader to talk back to |
| 320 | */ |
| 321 | protected $upgrader; |
| 322 | |
| 323 | /** |
| 324 | * @var array options of current element, used throughout the process |
| 325 | */ |
| 326 | protected $options; |
| 327 | /** |
| 328 | * @var mixed info about the current element, used by the upgrader |
| 329 | */ |
| 330 | protected $info; |
| 331 | /** |
| 332 | * @var string name of the download |
| 333 | */ |
| 334 | protected $download; |
| 335 | /** |
| 336 | * @var string working dir for the unpacked package |
| 337 | */ |
| 338 | protected $working_dir; |
| 339 | |
| 340 | /** |
| 341 | * @var mixed final result of installation (via upgrader) |
| 342 | */ |
| 343 | protected $result; |
| 344 | |
| 345 | /** |
| 346 | * WP_Bulk_Upgrader_Element constructor. |
| 347 | * |
| 348 | * @since 4.4.1 |
| 349 | * @access public |
| 350 | * |
| 351 | * @param mixed $name |
| 352 | * @param WP_Upgrader $upgrader |
| 353 | */ |
| 354 | public function __construct( $name, WP_Upgrader $upgrader ) { |
| 355 | $this->name = $name; |
| 356 | $this->upgrader = $upgrader; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Get the original identifier data |
| 361 | * |
| 362 | * @since 4.4.1 |
| 363 | * @access public |
| 364 | * |
| 365 | * @return mixed |
| 366 | */ |
| 367 | public function get_name() { |
| 368 | return $this->name; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get the cummulated result of the upgrade |
| 373 | * |
| 374 | * @since 4.4.1 |
| 375 | * @access public |
| 376 | * |
| 377 | * @return bool |
| 378 | */ |
| 379 | public function get_result() { |
| 380 | return isset( $this->result ) ? $this->result : $this->installed; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * If the result needs to be named, it can be overwriten here |
| 385 | * |
| 386 | * @since 4.4.1 |
| 387 | * @access public |
| 388 | * |
| 389 | * @return bool |
| 390 | */ |
| 391 | public function get_result_index() { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Has been downloaded correctly |
| 397 | * |
| 398 | * @since 4.4.1 |
| 399 | * @access public |
| 400 | * |
| 401 | * @return bool |
| 402 | */ |
| 403 | public function downloaded() { |
| 404 | return $this->downloaded; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Has been unpacked succesfully |
| 409 | * |
| 410 | * @since 4.4.1 |
| 411 | * @access public |
| 412 | * |
| 413 | * @return bool |
| 414 | */ |
| 415 | public function unpacked() { |
| 416 | return $this->unpacked; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Has been installed succesfully |
| 421 | * |
| 422 | * @since 4.4.1 |
| 423 | * @access public |
| 424 | * |
| 425 | * @return bool |
| 426 | */ |
| 427 | public function installed() { |
| 428 | return $this->installed; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Is it already up-to-date |
| 433 | * |
| 434 | * @since 4.4.1 |
| 435 | * @access public |
| 436 | * |
| 437 | * @return bool |
| 438 | */ |
| 439 | public function is_up_to_date() { |
| 440 | return $this->up_to_date; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Get element info |
| 445 | * |
| 446 | * @since 4.4.1 |
| 447 | * @access public |
| 448 | * @return mixed |
| 449 | */ |
| 450 | public function get_info() { |
| 451 | return $this->info; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Optionally manage maintenance after install |
| 456 | * |
| 457 | * @since 4.4.1 |
| 458 | * @access protected |
| 459 | * |
| 460 | * @param $maintenance |
| 461 | */ |
| 462 | protected function post_install( $maintenance ) { |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Apply defaults and filter to options |
| 467 | * |
| 468 | * @since 4.4.1 |
| 469 | * @access protected |
| 470 | * |
| 471 | * @param $options |
| 472 | */ |
| 473 | protected function set_options( $options ) { |
| 474 | $defaults = array( |
| 475 | 'package' => '', |
| 476 | // Please always pass this. |
| 477 | 'destination' => '', |
| 478 | // And this |
| 479 | 'clear_destination' => false, |
| 480 | 'abort_if_destination_exists' => true, |
| 481 | // Abort if the Destination directory exists, Pass clear_destination as false please |
| 482 | 'clear_working' => true, |
| 483 | 'is_multi' => false, |
| 484 | 'hook_extra' => array() |
| 485 | // Pass any extra $hook_extra args here, this will be passed to any hooked filters. |
| 486 | ); |
| 487 | |
| 488 | $options = wp_parse_args( $options, $defaults ); |
| 489 | |
| 490 | /** This filter is documented in wp-admin/includes/class-wp-upgrader.php */ |
| 491 | $this->options = apply_filters( 'upgrader_package_options', $options ); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Does element need maintenance to be enabled |
| 496 | * |
| 497 | * @since 4.4.1 |
| 498 | * @access protected |
| 499 | * |
| 500 | * @return bool |
| 501 | */ |
| 502 | protected function need_maintenance() { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Download this element |
| 508 | * |
| 509 | * @since 4.4.1 |
| 510 | * @access public |
| 511 | * |
| 512 | * @return bool|string|WP_Error |
| 513 | */ |
| 514 | public function download() { |
| 515 | if ( $this->is_up_to_date() ) { |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // Connect to the Filesystem first. |
| 520 | $result = $this->upgrader->fs_connect( array( WP_CONTENT_DIR, $this->options['destination'] ) ); |
| 521 | // Mainly for non-connected filesystem. |
| 522 | if ( ! $result || is_wp_error( $result ) ) { |
| 523 | return $result; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Download the package (Note, This just returns the filename |
| 528 | * of the file if the package is a local file) |
| 529 | */ |
| 530 | $result = $this->upgrader->download_package( $this->options['package'] ); |
| 531 | if ( ! is_wp_error( $result ) ) { |
| 532 | $this->downloaded = true; |
| 533 | $this->download = $result; |
| 534 | } |
| 535 | |
| 536 | return $result; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Unpack this element |
| 541 | * |
| 542 | * @since 4.4.1 |
| 543 | * @access public |
| 544 | * |
| 545 | * @return bool|string|WP_Error |
| 546 | */ |
| 547 | public function unpack() { |
| 548 | if ( ! $this->downloaded() ) { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | // Do not delete a "local" file |
| 553 | $delete_package = ( $this->download !== $this->options['package'] ); |
| 554 | |
| 555 | // Unzips the file into a temporary directory. |
| 556 | $result = $this->upgrader->unpack_package( $this->download, $delete_package ); |
| 557 | if ( ! is_wp_error( $result ) ) { |
| 558 | $this->unpacked = true; |
| 559 | $this->working_dir = $result; |
| 560 | } |
| 561 | |
| 562 | return $result; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Install this element |
| 567 | * |
| 568 | * Enable maintenance mode if required |
| 569 | * |
| 570 | * @since 4.4.1 |
| 571 | * @access public |
| 572 | * |
| 573 | * @return array|bool|WP_Error |
| 574 | */ |
| 575 | public function install() { |
| 576 | if ( ! $this->unpacked() ) { |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | $maintenance = $this->need_maintenance(); |
| 581 | $this->upgrader->maintenance_mode( $maintenance ); |
| 582 | |
| 583 | // With the given options, this installs it to the destination directory. |
| 584 | $result = $this->upgrader->install_package( array( |
| 585 | 'source' => $this->working_dir, |
| 586 | 'destination' => $this->options['destination'], |
| 587 | 'clear_destination' => $this->options['clear_destination'], |
| 588 | 'abort_if_destination_exists' => $this->options['abort_if_destination_exists'], |
| 589 | 'clear_working' => $this->options['clear_working'], |
| 590 | 'hook_extra' => $this->options['hook_extra'] |
| 591 | ) ); |
| 592 | |
| 593 | $this->post_install( $maintenance ); |
| 594 | |
| 595 | /* |
| 596 | * This does not feel right. |
| 597 | */ |
| 598 | $this->result = $this->upgrader->result; |
| 599 | |
| 600 | return $result; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Abstract base class for Bulk Upgrade Elements that are formatted as Plugin/Theme |
| 606 | * |
| 607 | * @since 4.4.1 |
| 608 | */ |
| 609 | abstract class WP_Bulk_Upgrader_Element_Plugin_Format extends WP_Bulk_Upgrader_Element { |
| 610 | /** |
| 611 | * @var object cache for current element type information |
| 612 | */ |
| 613 | protected static $current; |
| 614 | |
| 615 | /** |
| 616 | * @var object current element response information from repository |
| 617 | */ |
| 618 | protected $response; |
| 619 | |
| 620 | /** |
| 621 | * Implement logic to tell if this element is active |
| 622 | * |
| 623 | * @since 4.4.1 |
| 624 | * @access public |
| 625 | * |
| 626 | * @return mixed |
| 627 | */ |
| 628 | abstract public function is_active(); |
| 629 | |
| 630 | /** |
| 631 | * Implement logic to get current info on all elements |
| 632 | * |
| 633 | * @since 4.4.1 |
| 634 | * @access protected |
| 635 | * |
| 636 | * @return mixed |
| 637 | */ |
| 638 | abstract protected function get_current(); |
| 639 | |
| 640 | /** |
| 641 | * Results are based on plugin name |
| 642 | * |
| 643 | * @return mixed |
| 644 | */ |
| 645 | public function get_result_index() { |
| 646 | return $this->name; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Get plugin repository response |
| 651 | * |
| 652 | * @since 4.4.1 |
| 653 | * @access protected |
| 654 | * |
| 655 | * @return bool|object Response from the respository as stdClass |
| 656 | */ |
| 657 | protected function get_response() { |
| 658 | $current = $this->get_current(); |
| 659 | |
| 660 | if ( is_object( $current ) && is_array( $current->response ) ) { |
| 661 | return $this->response = isset( $current->response[ $this->name ] ) ? $current->response[ $this->name ] : false; |
| 662 | } |
| 663 | |
| 664 | return false; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Plugin Bulk Upgrader implementation |
| 670 | * |
| 671 | * @since 4.4.1 |
| 672 | */ |
| 673 | class WP_Plugin_Bulk_Upgrader extends WP_Bulk_Upgrader_Composite { |
| 674 | /** |
| 675 | * WP_Plugin_Bulk_Upgrader constructor. |
| 676 | * |
| 677 | * @since 4.4.1 |
| 678 | * @access public |
| 679 | * |
| 680 | * @param Plugin_Upgrader $upgrader |
| 681 | * @param array $elements |
| 682 | */ |
| 683 | public function __construct( Plugin_Upgrader $upgrader, array $elements = array() ) { |
| 684 | parent::__construct( $upgrader, $elements, 'WP_Bulk_Upgrader_Plugin_Element' ); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Communicate plugin info to skin |
| 689 | * |
| 690 | * @since 4.4.1 |
| 691 | * @access protected |
| 692 | * |
| 693 | * @param WP_Bulk_Upgrader_Element $element |
| 694 | */ |
| 695 | protected function current_element( WP_Bulk_Upgrader_Element $element ) { |
| 696 | parent::current_element( $element ); |
| 697 | |
| 698 | $skin = $this->get_skin(); |
| 699 | if ( $skin instanceof Bulk_Plugin_Upgrader_Skin ) { |
| 700 | $skin->plugin_info = $element->get_info(); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Communicate result to skin |
| 706 | * |
| 707 | * @since 4.4.1 |
| 708 | * @access public |
| 709 | * |
| 710 | * @param WP_Bulk_Upgrader_Element $element |
| 711 | */ |
| 712 | public function handle_up_to_date( WP_Bulk_Upgrader_Element $element ) { |
| 713 | $skin = $this->get_skin(); |
| 714 | if ( $skin instanceof WP_Upgrader_Skin ) { |
| 715 | $skin->set_result( 'up_to_date' ); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Sort plugin so active ones are bundled at the end |
| 721 | * |
| 722 | * @since 4.4.1 |
| 723 | * @access protected |
| 724 | * |
| 725 | * @return array |
| 726 | */ |
| 727 | protected function sort_install_elements() { |
| 728 | $sorted = $this->elements; |
| 729 | usort( $sorted, array( $this, 'sort_elements_active_on_bottom' ) ); |
| 730 | |
| 731 | return $sorted; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Sort plugins |
| 736 | * |
| 737 | * @since 4.4.1 |
| 738 | * @access private |
| 739 | * |
| 740 | * @param WP_Bulk_Upgrader_Element_Plugin_Format $a |
| 741 | * @param WP_Bulk_Upgrader_Element_Plugin_Format $b |
| 742 | * |
| 743 | * @return int 0, -1 or 1 |
| 744 | */ |
| 745 | private function sort_elements_active_on_bottom( |
| 746 | WP_Bulk_Upgrader_Element_Plugin_Format $a, |
| 747 | WP_Bulk_Upgrader_Element_Plugin_Format $b |
| 748 | ) { |
| 749 | if ( $a->is_active() === $b->is_active() ) { |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | // Active plugins last: |
| 754 | return ( $a->is_active() && ! $b->is_active() ) ? 1 : - 1; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Plugin implementation of the Bulk Upgrade Element |
| 760 | * |
| 761 | * @since 4.4.1 |
| 762 | */ |
| 763 | class WP_Bulk_Upgrader_Plugin_Element extends WP_Bulk_Upgrader_Element_Plugin_Format { |
| 764 | /** |
| 765 | * WP_Bulk_Upgrader_Plugin_Element constructor. |
| 766 | * |
| 767 | * @since 4.4.1 |
| 768 | * @access public |
| 769 | * |
| 770 | * @param string $plugin |
| 771 | * @param Plugin_Upgrader $upgrader |
| 772 | */ |
| 773 | public function __construct( $plugin, Plugin_Upgrader $upgrader ) { |
| 774 | parent::__construct( $plugin, $upgrader ); |
| 775 | |
| 776 | $this->info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true ); |
| 777 | |
| 778 | $response = $this->get_response(); |
| 779 | if ( false === $response ) { |
| 780 | $this->up_to_date = true; |
| 781 | |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | $options = array( |
| 786 | 'package' => $response->package, |
| 787 | 'destination' => WP_PLUGIN_DIR, |
| 788 | 'clear_destination' => true, |
| 789 | 'clear_working' => true, |
| 790 | 'is_multi' => true, |
| 791 | 'hook_extra' => array( |
| 792 | 'plugin' => $plugin |
| 793 | ) |
| 794 | ); |
| 795 | |
| 796 | $this->set_options( $options ); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Is this plugin activated |
| 801 | * |
| 802 | * @since 4.4.1 |
| 803 | * @access public |
| 804 | * |
| 805 | * @return bool |
| 806 | */ |
| 807 | public function is_active() { |
| 808 | return is_plugin_active( $this->name ); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Get info from transient update_plugins |
| 813 | * |
| 814 | * @since 4.4.1 |
| 815 | * @access protected |
| 816 | * |
| 817 | * @return mixed |
| 818 | */ |
| 819 | protected function get_current() { |
| 820 | if ( ! isset( self::$current ) ) { |
| 821 | self::$current = get_site_transient( 'update_plugins' ); |
| 822 | } |
| 823 | |
| 824 | return self::$current; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Do we need maintenance mode to install |
| 829 | * |
| 830 | * @since 4.4.1 |
| 831 | * @access protected |
| 832 | * |
| 833 | * @return bool |
| 834 | */ |
| 835 | protected function need_maintenance() { |
| 836 | return is_multisite() || $this->is_active(); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Theme Bulk Upgrader implementation |
| 842 | * |
| 843 | * @since 4.4.1 |
| 844 | */ |
| 845 | class WP_Theme_Bulk_Upgrader extends WP_Bulk_Upgrader_Composite { |
| 846 | /** |
| 847 | * WP_Theme_Bulk_Upgrader constructor. |
| 848 | * |
| 849 | * @since 4.4.1 |
| 850 | * @access public |
| 851 | * |
| 852 | * @param Theme_Upgrader $upgrader |
| 853 | * @param array $elements |
| 854 | */ |
| 855 | public function __construct( Theme_Upgrader $upgrader, array $elements = array() ) { |
| 856 | parent::__construct( $upgrader, $elements, 'WP_Bulk_Upgrader_Theme_Element' ); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Communicate theme info to skin |
| 861 | * |
| 862 | * @since 4.4.1 |
| 863 | * @access protected |
| 864 | * |
| 865 | * @param WP_Bulk_Upgrader_Element $element |
| 866 | */ |
| 867 | protected function current_element( WP_Bulk_Upgrader_Element $element ) { |
| 868 | parent::current_element( $element ); |
| 869 | |
| 870 | $skin = $this->get_skin(); |
| 871 | if ( $skin instanceof Bulk_Theme_Upgrader_Skin ) { |
| 872 | $skin->theme_info = $element->get_info(); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * Communicate result to skin |
| 878 | * |
| 879 | * @since 4.4.1 |
| 880 | * @access public |
| 881 | * |
| 882 | * @param $element |
| 883 | */ |
| 884 | public function handle_up_to_date( $element ) { |
| 885 | $this->get_skin()->set_result( true ); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * Theme implementation of the Bulk Upgrade Element |
| 891 | * |
| 892 | * @since 4.4.1 |
| 893 | */ |
| 894 | class WP_Bulk_Upgrader_Theme_Element extends WP_Bulk_Upgrader_Element_Plugin_Format { |
| 895 | /** |
| 896 | * WP_Bulk_Upgrader_Theme_Element constructor. |
| 897 | * |
| 898 | * @since 4.4.1 |
| 899 | * @access public |
| 900 | * |
| 901 | * @param string $theme |
| 902 | * @param Theme_Upgrader $upgrader |
| 903 | */ |
| 904 | public function __construct( $theme, Theme_Upgrader $upgrader ) { |
| 905 | parent::__construct( $theme, $upgrader ); |
| 906 | |
| 907 | $this->info = $upgrader->theme_info( $theme ); |
| 908 | |
| 909 | $response = $this->get_response(); |
| 910 | if ( false === $response ) { |
| 911 | $this->up_to_date = true; |
| 912 | |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | $options = array( |
| 917 | 'package' => $this->response['package'], |
| 918 | 'destination' => get_theme_root( $theme ), |
| 919 | 'clear_destination' => true, |
| 920 | 'clear_working' => true, |
| 921 | 'is_multi' => true, |
| 922 | 'hook_extra' => array( |
| 923 | 'theme' => $theme |
| 924 | ) |
| 925 | ); |
| 926 | |
| 927 | $this->set_options( $options ); |
| 928 | } |
| 929 | |
| 930 | |
| 931 | /** |
| 932 | * Get info from update_themes |
| 933 | * |
| 934 | * @since 4.4.1 |
| 935 | * @access protected |
| 936 | * |
| 937 | * @return mixed |
| 938 | */ |
| 939 | protected function get_current() { |
| 940 | // @todo should this be cached or not? |
| 941 | if ( ! isset( self::$current ) ) { |
| 942 | self::$current = get_site_transient( 'update_themes' ); |
| 943 | } |
| 944 | |
| 945 | return self::$current; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Is this theme active |
| 950 | * |
| 951 | * @since 4.4.1 |
| 952 | * @access public |
| 953 | * |
| 954 | * @return bool |
| 955 | */ |
| 956 | public function is_active() { |
| 957 | return $this->name == get_stylesheet() || $this->name == get_template(); |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * Do we need maintenace mode to install |
| 962 | * |
| 963 | * @since 4.4.1 |
| 964 | * @access protected |
| 965 | * |
| 966 | * @return bool |
| 967 | */ |
| 968 | protected function need_maintenance() { |
| 969 | return is_multisite() || $this->is_active(); |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * Disable maintenance mode if it was active during install |
| 974 | * |
| 975 | * There can be only 1 active theme, so disable maintenance after installing |
| 976 | * |
| 977 | * @since 4.4.1 |
| 978 | * @access protected |
| 979 | * |
| 980 | * @param $maintenance |
| 981 | */ |
| 982 | protected function post_install( $maintenance ) { |
| 983 | if ( $maintenance ) { |
| 984 | $this->upgrader->maintenance_mode( false ); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Language Bulk Upgrader implementation |
| 991 | * |
| 992 | * @since 4.4.1 |
| 993 | */ |
| 994 | class WP_Language_Bulk_Upgrader extends WP_Bulk_Upgrader_Composite { |
| 995 | /** |
| 996 | * WP_Language_Bulk_Upgrader constructor. |
| 997 | * |
| 998 | * @since 4.4.1 |
| 999 | * @access public |
| 1000 | * |
| 1001 | * @param Language_Pack_Upgrader $upgrader |
| 1002 | * @param array $elements |
| 1003 | */ |
| 1004 | public function __construct( Language_Pack_Upgrader $upgrader, array $elements = array() ) { |
| 1005 | parent::__construct( $upgrader, $elements, 'WP_Bulk_Upgrader_Language_Element' ); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Communicate language info to skin |
| 1010 | * |
| 1011 | * @since 4.4.1 |
| 1012 | * @access protected |
| 1013 | * |
| 1014 | * @param WP_Bulk_Upgrader_Element $element |
| 1015 | */ |
| 1016 | protected function current_element( WP_Bulk_Upgrader_Element $element ) { |
| 1017 | parent::current_element( $element ); |
| 1018 | |
| 1019 | $skin = $this->get_skin(); |
| 1020 | if ( $skin instanceof Language_Pack_Upgrader_Skin ) { |
| 1021 | $skin->language_update = $element->get_name(); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Language implementation of the Bulk Upgrade Element |
| 1028 | * |
| 1029 | * @since 4.4.1 |
| 1030 | */ |
| 1031 | class WP_Bulk_Upgrader_Language_Element extends WP_Bulk_Upgrader_Element { |
| 1032 | /** |
| 1033 | * WP_Bulk_Upgrader_Language_Element constructor. |
| 1034 | * |
| 1035 | * @since 4.4.1 |
| 1036 | * @access public |
| 1037 | * |
| 1038 | * @param mixed $language_update |
| 1039 | * @param Language_Pack_Upgrader $upgrader |
| 1040 | */ |
| 1041 | public function __construct( $language_update, Language_Pack_Upgrader $upgrader ) { |
| 1042 | parent::__construct( $language_update, $upgrader ); |
| 1043 | |
| 1044 | $destination = WP_LANG_DIR; |
| 1045 | if ( 'plugin' == $language_update->type ) { |
| 1046 | $destination .= '/plugins'; |
| 1047 | } elseif ( 'theme' == $language_update->type ) { |
| 1048 | $destination .= '/themes'; |
| 1049 | } |
| 1050 | |
| 1051 | $options = array( |
| 1052 | 'package' => $language_update->package, |
| 1053 | 'destination' => $destination, |
| 1054 | 'clear_destination' => false, |
| 1055 | 'abort_if_destination_exists' => false, // We expect the destination to exist. |
| 1056 | 'clear_working' => true, |
| 1057 | 'is_multi' => true, |
| 1058 | 'hook_extra' => array( |
| 1059 | 'language_update_type' => $language_update->type, |
| 1060 | 'language_update' => $language_update, |
| 1061 | ) |
| 1062 | ); |
| 1063 | |
| 1064 | $this->set_options( $options ); |
| 1065 | } |
| 1066 | } |
| 1067 | No newline at end of file |