| 390 | /** |
| 391 | * Clears existing translations where this item is going to be installed into. |
| 392 | * |
| 393 | * @since 5.1.0 |
| 394 | * |
| 395 | * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
| 396 | * |
| 397 | * @param string $remote_destination The location on the remote filesystem to be cleared. |
| 398 | * @return bool|WP_Error True upon success, WP_Error on failure. |
| 399 | */ |
| 400 | public function clear_destination( $remote_destination ) { |
| 401 | global $wp_filesystem; |
| 402 | |
| 403 | $language_update = $this->skin->language_update; |
| 404 | $language_directory = WP_LANG_DIR . '/'; // Local path for use with glob(). |
| 405 | |
| 406 | if ( 'core' === $language_update->type ) { |
| 407 | $files = array( |
| 408 | $remote_destination . $language_update->language . '.po', |
| 409 | $remote_destination . $language_update->language . '.mo', |
| 410 | $remote_destination . 'admin-' . $language_update->language . '.po', |
| 411 | $remote_destination . 'admin-' . $language_update->language . '.mo', |
| 412 | $remote_destination . 'admin-network-' . $language_update->language . '.po', |
| 413 | $remote_destination . 'admin-network-' . $language_update->language . '.mo', |
| 414 | $remote_destination . 'continents-cities-' . $language_update->language . '.po', |
| 415 | $remote_destination . 'continents-cities-' . $language_update->language . '.mo', |
| 416 | ); |
| 417 | |
| 418 | $json_translation_files = glob( $language_directory . $language_update->language . '-*.json' ); |
| 419 | if ( $json_translation_files ) { |
| 420 | foreach ( $json_translation_files as $json_translation_file ) { |
| 421 | $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file ); |
| 422 | } |
| 423 | } |
| 424 | } else { |
| 425 | $files = array( |
| 426 | $remote_destination . $language_update->slug . '-' . $language_update->language . '.po', |
| 427 | $remote_destination . $language_update->slug . '-' . $language_update->language . '.mo', |
| 428 | ); |
| 429 | |
| 430 | $language_directory = $language_directory . $language_update->type . 's/'; |
| 431 | $json_translation_files = glob( $language_directory . $language_update->slug . '-' . $language_update->language . '-*.json' ); |
| 432 | if ( $json_translation_files ) { |
| 433 | foreach ( $json_translation_files as $json_translation_file ) { |
| 434 | $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file ); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | $files = array_filter( $files, array( $wp_filesystem, 'exists' ) ); |
| 440 | |
| 441 | // No files to delete. |
| 442 | if ( ! $files ) { |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | // Check all files are writable before attempting to clear the destination. |
| 447 | $unwritable_files = array(); |
| 448 | |
| 449 | // Check writability. |
| 450 | foreach ( $files as $file ) { |
| 451 | if ( ! $wp_filesystem->is_writable( $file ) ) { |
| 452 | // Attempt to alter permissions to allow writes and try again. |
| 453 | $wp_filesystem->chmod( $file, FS_CHMOD_FILE ); |
| 454 | if ( ! $wp_filesystem->is_writable( $file ) ) { |
| 455 | $unwritable_files[] = $file; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if ( ! empty( $unwritable_files ) ) { |
| 461 | return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) ); |
| 462 | } |
| 463 | |
| 464 | foreach ( $files as $file ) { |
| 465 | if ( ! $wp_filesystem->delete( $file ) ) { |
| 466 | return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] ); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return true; |
| 471 | } |