| 1 |
<?php namespace TierPricingTable\Core; |
| 2 |
|
| 3 |
use function \extract as allowedExtract; |
| 4 |
|
| 5 |
class FileManager { |
| 6 |
|
| 7 |
/** |
| 8 |
* Main file |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
private $mainFile; |
| 13 |
|
| 14 |
/** |
| 15 |
* Directory of the plugin |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $pluginDirectory; |
| 20 |
|
| 21 |
/** |
| 22 |
* Plugin name |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $pluginName; |
| 27 |
|
| 28 |
/** |
| 29 |
* Plugin url |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $pluginUrl; |
| 34 |
|
| 35 |
/** |
| 36 |
* Theme directory |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $themeDirectory; |
| 41 |
|
| 42 |
/** |
| 43 |
* PluginManager constructor. |
| 44 |
* |
| 45 |
* @param string $mainFile |
| 46 |
* @param string|null $themeDirectory |
| 47 |
*/ |
| 48 |
public function __construct( $mainFile, $themeDirectory = null ) { |
| 49 |
$this->mainFile = $mainFile; |
| 50 |
$this->pluginDirectory = plugin_dir_path( $this->mainFile ); |
| 51 |
$this->pluginName = basename( $this->pluginDirectory ); |
| 52 |
$this->themeDirectory = $themeDirectory ? $themeDirectory : $this->pluginName; |
| 53 |
$this->pluginUrl = plugin_dir_url( $this->getMainFile() ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get the plugin directory |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function getPluginDirectory() { |
| 62 |
return $this->pluginDirectory; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Return name of the plugin |
| 67 |
* |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function getPluginName() { |
| 71 |
return $this->pluginName; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get the main file |
| 76 |
* |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function getMainFile() { |
| 80 |
return $this->mainFile; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get the plugin url |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function getPluginUrl() { |
| 89 |
return $this->pluginUrl; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Include template |
| 94 |
* |
| 95 |
* @param string $__template |
| 96 |
* @param array $__variables |
| 97 |
*/ |
| 98 |
public function includeTemplate( $__template, array $__variables = array(), $__default_path = '' ) { |
| 99 |
$__template = $this->locateTemplate( $__template, $__default_path ); |
| 100 |
|
| 101 |
if ( $__template ) { |
| 102 |
allowedExtract( $__variables ); |
| 103 |
|
| 104 |
do_action( 'tiered_pricing_table/template/before_render', $__template, $__variables ); |
| 105 |
// nosemgrep: audit.php.lang.security.file.inclusion-arg |
| 106 |
include( $__template ); |
| 107 |
do_action( 'tiered_pricing_table/template/after_render', $__template, $__variables ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Render template |
| 113 |
* |
| 114 |
* @param string $template |
| 115 |
* @param array $variables |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
public function renderTemplate( $template, array $variables = array(), $default_path = '' ) { |
| 120 |
ob_start(); |
| 121 |
$this->includeTemplate( $template, $variables, $default_path ); |
| 122 |
$content = ob_get_contents(); |
| 123 |
ob_end_clean(); |
| 124 |
|
| 125 |
return $content; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Locate assets |
| 130 |
* |
| 131 |
* @param string $file |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function locateAsset( $file ) { |
| 136 |
return $this->pluginUrl . 'assets/' . $file; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Locate assets |
| 141 |
* |
| 142 |
* @param string $file |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function locateJSAsset( $file ) { |
| 147 |
|
| 148 |
$file = str_replace( '.js', '', $file ); |
| 149 |
|
| 150 |
$js = '.js'; |
| 151 |
|
| 152 |
if ( defined( 'TIERED_PRICING_PRODUCTION' ) ) { |
| 153 |
$js = '.min.js'; |
| 154 |
} |
| 155 |
|
| 156 |
return apply_filters( 'tiered_pricing_table/assets/js/url', $this->pluginUrl . 'assets/' . $file . $js, $file ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Locate CSS assets |
| 161 |
* |
| 162 |
* @param string $file |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
public function locateCSSAsset( $file ) { |
| 167 |
|
| 168 |
$file = str_replace( '.css', '', $file ); |
| 169 |
|
| 170 |
$css = '.css'; |
| 171 |
|
| 172 |
if ( defined( 'TIERED_PRICING_PRODUCTION' ) ) { |
| 173 |
$css = '.min.css'; |
| 174 |
} |
| 175 |
|
| 176 |
return apply_filters( 'tiered_pricing_table/assets/css/url', $this->pluginUrl . 'assets/' . $file . $css, $file ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Locate template |
| 181 |
* |
| 182 |
* @param string $template |
| 183 |
* |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
public function locateTemplate( $template, $default_path = '' ) { |
| 187 |
|
| 188 |
if ( $default_path ) { |
| 189 |
$file = trailingslashit( $default_path ) . $template; |
| 190 |
} else { |
| 191 |
$file = $this->pluginDirectory . 'views/' . $template; |
| 192 |
} |
| 193 |
|
| 194 |
if ( strpos( $template, 'frontend/' ) === 0 ) { |
| 195 |
|
| 196 |
$frontendTemplate = str_replace( 'frontend/', '', $template ); |
| 197 |
$frontendFile = locate_template( $this->themeDirectory . '/' . $frontendTemplate ); |
| 198 |
|
| 199 |
if ( $frontendFile ) { |
| 200 |
$file = $frontendFile; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return apply_filters( 'tiered_pricing_table/template/location', $file, $template ); |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|