SGExtension.php
5 years ago
SGExtensionAdapterWordpress.php
5 years ago
SGIExtensionAdapter.php
5 years ago
SGExtensionAdapterWordpress.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | require_once(dirname(__FILE__).'/SGIExtensionAdapter.php'); |
| 4 | |
| 5 | class SGExtensionAdapterWordpress implements SGIExtensionAdapter |
| 6 | { |
| 7 | public function isExtensionActive($extension) |
| 8 | { |
| 9 | return is_plugin_active($extension."/".$extension.".php"); |
| 10 | } |
| 11 | |
| 12 | public function isExtensionAvailable($extension) |
| 13 | { |
| 14 | return file_exists(SG_EXTENSIONS_FOLDER_PATH.$extension); |
| 15 | } |
| 16 | |
| 17 | public function isExtensionAlreadyInPluginsFolder($extension) |
| 18 | { |
| 19 | return file_exists(WP_PLUGIN_DIR."/".$extension); |
| 20 | } |
| 21 | |
| 22 | public function installExtension($extension) |
| 23 | { |
| 24 | if (!$this->isExtensionAlreadyInPluginsFolder($extension)) { |
| 25 | return $this->copyExtensionFilesToPLuginsFolder($extension); |
| 26 | } |
| 27 | |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | public function activateExtension($extension) |
| 32 | { |
| 33 | SGConfig::set($extension, 1); |
| 34 | if (!$this->isExtensionActive($extension)) { |
| 35 | activate_plugin($extension."/".$extension.".php"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public function copyExtensionFilesToPLuginsFolder($extension) |
| 40 | { |
| 41 | $extensionsDirectoryPath = SG_EXTENSIONS_FOLDER_PATH.$extension; |
| 42 | if (!$this->isExtensionAvailable($extension)) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | $pluginsDirectoryPath = WP_PLUGIN_DIR."/".$extension; |
| 47 | @mkdir($pluginsDirectoryPath, 0755, true); |
| 48 | |
| 49 | $it = new RecursiveIteratorIterator( |
| 50 | new RecursiveDirectoryIterator($extensionsDirectoryPath, RecursiveDirectoryIterator::SKIP_DOTS), |
| 51 | RecursiveIteratorIterator::SELF_FIRST, |
| 52 | RecursiveIteratorIterator::CATCH_GET_CHILD |
| 53 | ); |
| 54 | |
| 55 | foreach ($it as $path => $fileInfo) { |
| 56 | $filename = $fileInfo->getFilename(); |
| 57 | if ($filename == '.DS_Store') { |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | $relativePath = substr($path, strlen($extensionsDirectoryPath)); |
| 62 | |
| 63 | if (substr($relativePath, 0, 4) == '.git') { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if ($fileInfo->isDir()) { |
| 68 | @mkdir($pluginsDirectoryPath.$relativePath, 0755, true); |
| 69 | } |
| 70 | else { |
| 71 | if (!@copy($path, $pluginsDirectoryPath.$relativePath)) { |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 |