| 1 |
<?php |
| 2 |
|
| 3 |
namespace Premmerce\Premmerce\WordpressSDK\FileManager; |
| 4 |
|
| 5 |
class FileManager |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var string |
| 9 |
*/ |
| 10 |
private $mainFile; |
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
private $pluginDirectory; |
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $pluginName; |
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $pluginUrl; |
| 23 |
/** |
| 24 |
* PluginManager constructor. |
| 25 |
* |
| 26 |
* @param string $mainFile |
| 27 |
*/ |
| 28 |
public function __construct($mainFile) |
| 29 |
{ |
| 30 |
$this->mainFile = $mainFile; |
| 31 |
$this->pluginDirectory = plugin_dir_path($this->mainFile); |
| 32 |
$this->pluginName = basename($this->pluginDirectory); |
| 33 |
$this->pluginUrl = plugin_dir_url($this->getMainFile()); |
| 34 |
} |
| 35 |
/** |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function getPluginDirectory() |
| 39 |
{ |
| 40 |
return $this->pluginDirectory; |
| 41 |
} |
| 42 |
/** |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public function getPluginName() |
| 46 |
{ |
| 47 |
return $this->pluginName; |
| 48 |
} |
| 49 |
/** |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function getMainFile() |
| 53 |
{ |
| 54 |
return $this->mainFile; |
| 55 |
} |
| 56 |
/** |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public function getPluginUrl() |
| 60 |
{ |
| 61 |
return $this->pluginUrl; |
| 62 |
} |
| 63 |
/** |
| 64 |
* @param string $template |
| 65 |
* @param array $variables |
| 66 |
*/ |
| 67 |
public function includeTemplate($template, array $variables = []) |
| 68 |
{ |
| 69 |
if ($templateFullPath = $this->locateTemplate($template)) { |
| 70 |
extract($variables); |
| 71 |
include $templateFullPath; |
| 72 |
} |
| 73 |
} |
| 74 |
/** |
| 75 |
* @param string $template |
| 76 |
* @param array $variables |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function renderTemplate($template, array $variables = []) |
| 81 |
{ |
| 82 |
ob_start(); |
| 83 |
$this->includeTemplate($template, $variables); |
| 84 |
$content = ob_get_contents(); |
| 85 |
ob_end_clean(); |
| 86 |
return $content; |
| 87 |
} |
| 88 |
/** |
| 89 |
* @param string $file |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
public function locateAsset($file) |
| 94 |
{ |
| 95 |
return $this->pluginUrl . 'assets/' . $file; |
| 96 |
} |
| 97 |
/** |
| 98 |
* @param string $template |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function locateTemplate($template) |
| 103 |
{ |
| 104 |
$frontendTemplate = $template; |
| 105 |
if (strpos($template, 'frontend/') === 0) { |
| 106 |
$frontendTemplate = str_replace('frontend/', '/', $template); |
| 107 |
} |
| 108 |
if ($file = locate_template($this->pluginName . '/' . $frontendTemplate)) { |
| 109 |
return $file; |
| 110 |
} |
| 111 |
return $this->pluginDirectory . 'views/' . $template; |
| 112 |
} |
| 113 |
} |
| 114 |
|