| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJTPHPCodeEvaluator { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
private $block = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
private $output = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @param mixed $code |
| 29 |
* @param stdClass Block object! |
| 30 |
* @return CJTPHPCodeEvaluator |
| 31 |
*/ |
| 32 |
public function __construct(& $block) { |
| 33 |
// Hold Block code! |
| 34 |
$this->block = $block; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* put your comment there... |
| 39 |
* |
| 40 |
* @param mixed $stack |
| 41 |
*/ |
| 42 |
public function exec($stack = array()) { |
| 43 |
$block =& $this->block; |
| 44 |
$code =& $block->code; |
| 45 |
// Make all stack variables available to the local scope. |
| 46 |
extract($stack); |
| 47 |
// Evaluate PHP codes! |
| 48 |
ob_start(); |
| 49 |
// Evaluate PHP code and save the result! |
| 50 |
$beforeEvalError = error_get_last(); |
| 51 |
$unusedResult = eval("?>{$code}"); |
| 52 |
$afterEvalError = error_get_last(); |
| 53 |
$evalOBuffer = ob_get_clean(); |
| 54 |
$showError = ini_get('display_errors') && WP_DEBUG; |
| 55 |
$isError = $afterEvalError && ($beforeEvalError != $afterEvalError); |
| 56 |
// Handling errors! |
| 57 |
if ($isError && $showError) { |
| 58 |
$this->output = 'CJT PHP Code Error detected for the following block: <br><br>'; |
| 59 |
$this->output .= "Name: {$block->name}<br>"; |
| 60 |
$this->output .= "ID: #{$block->id}<br><br>"; |
| 61 |
if ($evalOBuffer) { |
| 62 |
$this->output .= "PHP Error message:<br>"; |
| 63 |
$this->output .= $evalOBuffer; |
| 64 |
} |
| 65 |
$this->output .= "PHP Error tech Details:<br>"; |
| 66 |
$this->output .= print_r($afterEvalError, true); |
| 67 |
} |
| 68 |
else { // Get evaludated code result! |
| 69 |
$this->output .= $evalOBuffer; |
| 70 |
} |
| 71 |
return $this; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* put your comment there... |
| 76 |
* |
| 77 |
*/ |
| 78 |
public function getBlock() { |
| 79 |
return $this->block; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* put your comment there... |
| 84 |
* |
| 85 |
* @param mixed $code |
| 86 |
*/ |
| 87 |
public static function getInstance($code) { |
| 88 |
return new CJTPHPCodeEvaluator($code); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* put your comment there... |
| 93 |
* |
| 94 |
*/ |
| 95 |
public function getOutput() { |
| 96 |
return $this->output; |
| 97 |
} |
| 98 |
} // End class. |