Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
37.50% covered (danger)
37.50%
6 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Playground_Clean_Up
37.50% covered (danger)
37.50%
6 / 16
50.00% covered (danger)
50.00%
1 / 2
23.62
0.00% covered (danger)
0.00%
0 / 1
 remove_tmp_files
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 remove_folder
16.67% covered (danger)
16.67%
2 / 12
0.00% covered (danger)
0.00%
0 / 1
26.83
1<?php
2/**
3 * Playground_Clean_Up file.
4 *
5 * @package wpcomsh
6 */
7
8namespace Imports;
9
10/**
11 * Class Playground_Clean_Up
12 *
13 * This class perform Playground files clean up.
14 */
15class Playground_Clean_Up {
16
17    /**
18     * Clean up the backup.
19     *
20     * @param string $zip_or_tar_file_path The path to the ZIP or TAR file to be imported.
21     * @param string $destination_path The path where the backup will be imported.
22     *
23     * @return boolean|\WP_Error True on success, or a WP_Error on failure.
24     */
25    public static function remove_tmp_files( string $zip_or_tar_file_path, string $destination_path ) {
26        if ( file_exists( $zip_or_tar_file_path ) ) {
27            // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
28            unlink( $zip_or_tar_file_path );
29        }
30
31        self::remove_folder( $destination_path );
32
33        return true;
34    }
35    /**
36     * Remove a folder.
37     *
38     * @param string $folder_path The folder path.
39     *
40     * @return bool
41     */
42    public static function remove_folder( string $folder_path ): bool {
43        if ( ! is_dir( $folder_path ) ) {
44            return false;
45        }
46
47        $files = scandir( $folder_path );
48
49        foreach ( $files as $file ) {
50            if ( $file === '.' || $file === '..' ) {
51                continue;
52            }
53
54            $name = $folder_path . '/' . $file;
55
56            if ( is_dir( $name ) ) {
57                self::remove_folder( $name );
58            } else {
59                // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
60                unlink( $name );
61            }
62        }
63
64        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir
65        rmdir( $folder_path );
66
67        return true;
68    }
69}