-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade.php
More file actions
125 lines (104 loc) · 3.18 KB
/
upgrade.php
File metadata and controls
125 lines (104 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
// CONFIG
$githubUser = 'devyarn-subhajit';
$repoName = 'wp-git';
$branch = 'main';
// $githubToken = 'ghp_xxxxxxxxxxxxxxxxxxxxxxx'; // for private repos
// Files/folders to skip (preserve these)
$skipList = [
'.env',
'db',
'upgrade.php',
'tmp_update',
'update.zip'
];
echo "=== WP-GIT UPGRADE START ===\n";
echo "GitHub Repo: $githubUser/$repoName ($branch)\n";
// TEMP PATHS
$tmpZip = __DIR__ . '/update.zip';
$tmpDir = __DIR__ . '/tmp_update';
// Step 1: Download ZIP
echo "\n[1/5] Downloading update from GitHub...\n";
$zipUrl = "https://api.github.com/repos/$githubUser/$repoName/zipball/$branch";
$opts = [
"http" => [
"header" => "User-Agent: PHP\r\n"
// For private repos: "Authorization: token $githubToken\r\n"
]
];
$context = stream_context_create($opts);
$data = @file_get_contents($zipUrl, false, $context);
if (!$data || !file_put_contents($tmpZip, $data)) {
exit("❌ Failed to download update ZIP.\n");
}
echo "✅ ZIP downloaded.\n";
// Step 2: Extract ZIP
echo "\n[2/5] Extracting update ZIP...\n";
$zip = new ZipArchive;
if ($zip->open($tmpZip) === TRUE) {
if (!is_dir($tmpDir)) mkdir($tmpDir);
$zip->extractTo($tmpDir);
$zip->close();
echo "✅ ZIP extracted.\n";
} else {
exit("❌ Failed to open ZIP file.\n");
}
// Step 3: Find extracted root folder
echo "\n[3/5] Locating extracted root folder...\n";
$extractedFolders = glob($tmpDir . '/*', GLOB_ONLYDIR);
if (!$extractedFolders || !isset($extractedFolders[0])) {
rrmdir($tmpDir);
unlink($tmpZip);
exit("❌ No folder found in extracted ZIP.\n");
}
$rootExtractedFolder = $extractedFolders[0];
echo "✅ Found extracted folder.\n";
// Step 4: Clean local directory except skip list
echo "\n[4/5] Cleaning local directory...\n";
clean_directory(__DIR__, $skipList);
echo "✅ Clean complete.\n";
// Step 5: Copy files from GitHub
echo "\n[5/5] Copying new files from GitHub...\n";
sync_directories($rootExtractedFolder, __DIR__, $skipList);
echo "✅ Files copied.\n";
// Cleanup
rrmdir($tmpDir);
unlink($tmpZip);
echo "\n✅ Update complete!\n=== WP-GIT UPGRADE END ===\n";
// === FUNCTIONS ===
function clean_directory($dir, $skipList)
{
foreach (array_diff(scandir($dir), ['.', '..']) as $item) {
$path = "$dir/$item";
if (in_array($item, $skipList)) continue;
is_dir($path) ? rrmdir($path) : unlink($path);
}
}
function sync_directories($src, $dst, $skipList = [])
{
if (!is_dir($src)) return;
if (!is_dir($dst)) @mkdir($dst, 0777, true);
$dir = opendir($src);
if (!$dir) return;
while (false !== ($file = readdir($dir))) {
if ($file == '.' || $file == '..') continue;
$srcPath = "$src/$file";
$dstPath = "$dst/$file";
if (in_array($file, $skipList)) continue;
if (is_dir($srcPath)) {
sync_directories($srcPath, $dstPath, $skipList);
} else {
copy($srcPath, $dstPath);
}
}
closedir($dir);
}
function rrmdir($dir)
{
if (!is_dir($dir)) return;
foreach (array_diff(scandir($dir), ['.', '..']) as $file) {
$path = "$dir/$file";
is_dir($path) ? rrmdir($path) : @unlink($path);
}
rmdir($dir);
}