| 1 |
<?php |
| 2 |
|
| 3 |
namespace JetBackup\Notification; |
| 4 |
|
| 5 |
use JetBackup\Data\ArrayData; |
| 6 |
use JetBackup\Exception\NotificationException; |
| 7 |
use JetBackup\Factory; |
| 8 |
use JetBackup\JetBackup; |
| 9 |
use JetBackup\Wordpress\Wordpress; |
| 10 |
|
| 11 |
if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 12 |
|
| 13 |
class Notification extends ArrayData { |
| 14 |
|
| 15 |
const TEMPLATES_PATH = JetBackup::TEMPLATES_PATH . JetBackup::SEP . 'emails'; |
| 16 |
|
| 17 |
private array $_vars=[]; |
| 18 |
|
| 19 |
public static function message():Notification { |
| 20 |
return (new Notification()); |
| 21 |
} |
| 22 |
|
| 23 |
public function addParam($key, $value):Notification { $this->set($key, $value); return $this; } |
| 24 |
|
| 25 |
/** |
| 26 |
* @param $subject |
| 27 |
* @param $message_name |
| 28 |
* |
| 29 |
* @return void |
| 30 |
* @throws NotificationException |
| 31 |
*/ |
| 32 |
public function send($subject, $message_name) { |
| 33 |
|
| 34 |
$recipient = Factory::getSettingsNotifications()->getAlternateEmail() ?: Wordpress::getBlogInfo('admin_email'); |
| 35 |
|
| 36 |
$message_file = self::TEMPLATES_PATH . JetBackup::SEP . $message_name . '.tpl'; |
| 37 |
if(!file_exists($message_file)) throw new NotificationException('Message file does not exist'); |
| 38 |
|
| 39 |
$main_file = self::TEMPLATES_PATH . JetBackup::SEP . 'main.tpl'; |
| 40 |
if(!file_exists($main_file)) throw new NotificationException('Main file does not exist'); |
| 41 |
|
| 42 |
$message = $this->_parse(file_get_contents($main_file), [ |
| 43 |
'content' => $this->_parse(file_get_contents($message_file), $this->getData()), |
| 44 |
'year' => date('Y') |
| 45 |
]); |
| 46 |
|
| 47 |
Email::send($recipient, $subject, $message); |
| 48 |
} |
| 49 |
|
| 50 |
private function _parse($content, $vars) { |
| 51 |
|
| 52 |
$content = preg_replace("#{else}#", "<?php else: ?>", $content); |
| 53 |
$content = preg_replace( "#{/if}#", "<?php endif; ?>", $content); |
| 54 |
$content = preg_replace( "#{/foreach}#", "<?php endforeach; ?>", $content); |
| 55 |
|
| 56 |
$content = preg_replace_callback("#{(if|elseif|foreach)\s+([^}]+)}#", function($matches) { |
| 57 |
$condition = preg_replace_callback("#\\$([a-zA-Z0-9_]+)#", function($matches) { |
| 58 |
return $this->_buildVar($matches[1]); |
| 59 |
}, $matches[2]); |
| 60 |
|
| 61 |
return "<?php $matches[1]($condition): ?>"; |
| 62 |
|
| 63 |
}, $content); |
| 64 |
|
| 65 |
$content = preg_replace_callback("#{\\$([^}]+)}#", function($matches) { |
| 66 |
return "<?php echo " . $this->_buildVar($matches[1]) . "; ?>"; |
| 67 |
}, $content); |
| 68 |
|
| 69 |
$this->_vars = $vars; |
| 70 |
|
| 71 |
ob_start(); |
| 72 |
eval('?>' . $content); |
| 73 |
return ob_get_clean(); |
| 74 |
} |
| 75 |
|
| 76 |
private function _buildVar($var) { |
| 77 |
$parts = explode(".", $var); |
| 78 |
$output = "\$this->_vars"; |
| 79 |
foreach ($parts as $part) $output .= "['$part']"; |
| 80 |
return $output; |
| 81 |
} |
| 82 |
} |