| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
class JavaScriptWrapper { |
| 6 |
|
| 7 |
private $_scriptFileContent; |
| 8 |
private $_currentScript; |
| 9 |
private $_fileName; |
| 10 |
private $_params; |
| 11 |
private $_plugin_directory; |
| 12 |
|
| 13 |
/** |
| 14 |
* JavaScriptWrapper constructor. |
| 15 |
* |
| 16 |
* @param $_fileName "should be a file from the js folder" |
| 17 |
* @param $_params "key/value array with parameters to be replaced in the script" |
| 18 |
*/ |
| 19 |
public function __construct( $_fileName, $_params ) { |
| 20 |
|
| 21 |
$this->_plugin_directory = plugin_dir_path(__FILE__); |
| 22 |
$this->_fileName = $_fileName; |
| 23 |
$this->_params = $_params; |
| 24 |
|
| 25 |
$this->loadScriptFromFile($this->_fileName, $this->_params); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
function loadScriptFromFile($fileName, $params){ |
| 30 |
$this->_scriptFileContent = file_get_contents($this->_plugin_directory.'js/'.$fileName); |
| 31 |
$this->_currentScript = str_replace(array_keys($params),array_values($params),$this->_scriptFileContent); |
| 32 |
} |
| 33 |
|
| 34 |
function appendScript($jscript){ |
| 35 |
$this->_currentScript .= $jscript; |
| 36 |
} |
| 37 |
|
| 38 |
function __toString() { |
| 39 |
return $this->_currentScript; |
| 40 |
} |
| 41 |
|
| 42 |
function getScriptMarkupString(){ |
| 43 |
return "<script type='text/javascript'>".$this."</script>"; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
} |