|
6 | 6 |
|
7 | 7 | class RepoInstaller
|
8 | 8 | {
|
9 |
| - private RepoManager $repoManager; |
| 9 | + private RepoManager $repoManager; |
10 | 10 |
|
11 |
| - public function __construct() |
12 |
| - { |
13 |
| - $this->repoManager = new RepoManager(); |
14 |
| - } |
| 11 | + public function __construct() |
| 12 | + { |
| 13 | + $this->repoManager = new RepoManager(); |
| 14 | + } |
15 | 15 |
|
16 |
| - public function downloadExtractRepo( string $repository ): void |
17 |
| - { |
18 |
| - $response = $this->repoManager->getRepoInfoByRepoName( $repository ); |
| 16 | + public function downloadExtractRepo(string $repository): void |
| 17 | + { |
| 18 | + $response = $this->repoManager->getRepoInfoByRepoName($repository); |
19 | 19 |
|
20 |
| - if ( ! isset( $response['zipball_url'] ) ) { |
21 |
| - wp_redirect( wp_get_referer() ); |
| 20 | + if (!isset($response['zipball_url'])) { |
| 21 | + wp_redirect(wp_get_referer()); |
22 | 22 |
|
23 |
| - return; |
24 |
| - } |
| 23 | + return; |
| 24 | + } |
25 | 25 |
|
26 |
| - $zip_url = $response['zipball_url']; |
27 |
| - $destination_path = WP_PLUGIN_DIR . '/' . $repository . '.zip'; |
| 26 | + $zip_url = $response['zipball_url']; |
| 27 | + $destination_path = WP_PLUGIN_DIR . '/' . $repository . '.zip'; |
28 | 28 |
|
29 |
| - if ( ! $this->downloadZipFile( $zip_url, $destination_path ) ) { |
30 |
| - wp_redirect( wp_get_referer() ); |
| 29 | + if (!$this->downloadZipFile($zip_url, $destination_path)) { |
| 30 | + wp_redirect(wp_get_referer()); |
31 | 31 |
|
32 |
| - return; |
33 |
| - } |
| 32 | + return; |
| 33 | + } |
34 | 34 |
|
35 |
| - $zip = new ZipArchive(); |
36 |
| - if ( $zip->open( $destination_path ) === true ) { |
37 |
| - $pluginDestination = WP_PLUGIN_DIR; |
| 35 | + $zip = new ZipArchive(); |
| 36 | + if ($zip->open($destination_path) === true) { |
| 37 | + $pluginDestination = WP_PLUGIN_DIR; |
38 | 38 |
|
39 |
| - $name = $zip->getNameIndex( 0 ); |
| 39 | + $name = $zip->getNameIndex(0); |
40 | 40 |
|
41 |
| - // extract the plugin |
42 |
| - $success = $zip->extractTo( $pluginDestination ); |
43 |
| - $zip->close(); |
| 41 | + // extract the plugin |
| 42 | + $success = $zip->extractTo($pluginDestination); |
| 43 | + $zip->close(); |
44 | 44 |
|
45 |
| - $pluginRepoPath = $pluginDestination . '/' . $repository; |
46 |
| - $pluginRepoGeneratedName = $pluginDestination . '/' . $name; |
| 45 | + $pluginRepoPath = $pluginDestination . '/' . $repository; |
| 46 | + $pluginRepoGeneratedName = $pluginDestination . '/' . $name; |
47 | 47 |
|
48 |
| - // if old repo data exists delete it |
49 |
| - if ( $success && is_dir( $pluginRepoPath ) ) { |
50 |
| - $deletedOldRepo = $this->delTree( $pluginRepoPath ); |
51 |
| - } |
| 48 | + // if old repo data exists delete it |
| 49 | + if ($success && is_dir($pluginRepoPath)) { |
| 50 | + $deletedOldRepo = $this->delTree($pluginRepoPath); |
| 51 | + } |
52 | 52 |
|
53 | 53 | // rename the plugin to the correct name
|
54 |
| - if ( is_dir( $pluginRepoGeneratedName ) ) { |
55 |
| - rename( $pluginRepoGeneratedName, $pluginRepoPath ); |
56 |
| - } |
57 |
| - |
58 |
| - // removes the zip file |
59 |
| - unlink( $destination_path ); |
60 |
| - |
61 |
| - // generate autoload files |
62 |
| - $this->composer_dump_autoload( $pluginRepoPath ); |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - |
67 |
| - private function downloadZipFile( $url, $filepath ): bool |
68 |
| - { |
69 |
| - $token = get_option( 'repo-key' ); |
70 |
| - |
71 |
| - $ch = curl_init( $url ); |
72 |
| - curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 ); |
73 |
| - curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763' ); |
74 |
| - curl_setopt( $ch, CURLOPT_HTTPHEADER, array( |
75 |
| - 'Content-Type: application/json', |
76 |
| - 'Authorization: Bearer ' . $token |
77 |
| - ) ); |
78 |
| - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
79 |
| - curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); |
80 |
| - curl_setopt( $ch, CURLOPT_AUTOREFERER, true ); |
81 |
| - curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); |
82 |
| - curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 ); |
83 |
| - |
84 |
| - $result = curl_exec( $ch ); |
85 |
| - $status_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); //get status code |
86 |
| - curl_close( $ch ); |
87 |
| - |
88 |
| - file_put_contents( $filepath, $result ); |
89 |
| - |
90 |
| - return ( filesize( $filepath ) > 0 ) ? true : false; |
91 |
| - } |
92 |
| - |
93 |
| - private function delTree( $dir ): bool |
94 |
| - { |
95 |
| - $files = array_diff( scandir( $dir ), array( '.', '..' ) ); |
96 |
| - foreach ( $files as $file ) { |
97 |
| - ( is_dir( "$dir/$file" ) ) ? $this->delTree( "$dir/$file" ) : unlink( "$dir/$file" ); |
98 |
| - } |
99 |
| - |
100 |
| - return rmdir( $dir ); |
101 |
| - |
102 |
| - } |
103 |
| - |
104 |
| - private function composer_dump_autoload( string $filePath ): void |
105 |
| - { |
106 |
| - exec( "cd $filePath && composer dump-autoload -o" ); |
107 |
| - } |
| 54 | + if (is_dir($pluginRepoGeneratedName)) { |
| 55 | + rename($pluginRepoGeneratedName, $pluginRepoPath); |
| 56 | + } |
| 57 | + |
| 58 | + // removes the zip file |
| 59 | + unlink($destination_path); |
| 60 | + |
| 61 | + // generate autoload files |
| 62 | + $this->composer_dump_autoload($pluginRepoPath); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + private function downloadZipFile($url, $filepath): bool |
| 68 | + { |
| 69 | + $token = get_option('repo-key'); |
| 70 | + |
| 71 | + $ch = curl_init($url); |
| 72 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
| 73 | + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763'); |
| 74 | + curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
| 75 | + 'Content-Type: application/json', |
| 76 | + 'Authorization: Bearer ' . $token |
| 77 | + ]); |
| 78 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 79 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 80 | + curl_setopt($ch, CURLOPT_AUTOREFERER, true); |
| 81 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 82 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 83 | + |
| 84 | + $result = curl_exec($ch); |
| 85 | + $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code |
| 86 | + curl_close($ch); |
| 87 | + |
| 88 | + file_put_contents($filepath, $result); |
| 89 | + |
| 90 | + return filesize($filepath) > 0; |
| 91 | + } |
| 92 | + |
| 93 | + private function delTree($dir): bool |
| 94 | + { |
| 95 | + $files = array_diff(scandir($dir), ['.', '..']); |
| 96 | + foreach ($files as $file) { |
| 97 | + (is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file"); |
| 98 | + } |
| 99 | + |
| 100 | + return rmdir($dir); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + private function composer_dump_autoload(string $filePath): void |
| 105 | + { |
| 106 | + exec("cd $filePath && composer dump-autoload -o"); |
| 107 | + } |
108 | 108 | }
|
0 commit comments