| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 4 |
if (!headers_sent()) { |
| 5 |
header('HTTP/1.1 403 Forbidden'); |
| 6 |
} |
| 7 |
die("Protected By WebTotem!"); |
| 8 |
} |
| 9 |
|
| 10 |
use Twig\Error\LoaderError; |
| 11 |
use Twig\Error\RuntimeError; |
| 12 |
use Twig\Error\SyntaxError; |
| 13 |
use \Twig\Loader\FilesystemLoader; |
| 14 |
use \Twig\Environment; |
| 15 |
|
| 16 |
/** |
| 17 |
* Read, parse and handle everything related with the templates. |
| 18 |
*/ |
| 19 |
class WebTotemTemplate { |
| 20 |
|
| 21 |
protected $loader; |
| 22 |
protected $page_nonce; |
| 23 |
protected $images_path; |
| 24 |
protected $menu_url; |
| 25 |
|
| 26 |
function __construct() { |
| 27 |
if (class_exists('\Twig\Loader\FilesystemLoader')) { |
| 28 |
$this->loader = new FilesystemLoader( WEBTOTEM_PLUGIN_PATH . '/includes/templates/'); |
| 29 |
} |
| 30 |
$this->page_nonce = wp_create_nonce('wtotem_page_nonce'); |
| 31 |
$this->images_path = WebTotem::getImagePath(''); |
| 32 |
$this->menu_url = WebTotem::adminURL('admin.php?page=wtotem'); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Rendering a template using twig and filling in data. |
| 37 |
* |
| 38 |
* @param string $template |
| 39 |
* @param array $variables |
| 40 |
* |
| 41 |
* @return bool|string |
| 42 |
* @throws LoaderError |
| 43 |
* @throws RuntimeError |
| 44 |
* @throws SyntaxError |
| 45 |
*/ |
| 46 |
public function twigRender( $template, $variables = []) { |
| 47 |
|
| 48 |
$twig = new Environment($this->loader); |
| 49 |
|
| 50 |
if(!file_exists(WEBTOTEM_PLUGIN_PATH . '/includes/templates/' . $template)) { |
| 51 |
WebTotemOption::setNotification('error', __('There is no template: ', 'wtotem') . $template); |
| 52 |
return FALSE; |
| 53 |
} |
| 54 |
|
| 55 |
// Default values of some variables |
| 56 |
$variables['images_path'] = $this->images_path; |
| 57 |
$variables['days'] = (isset($variables['days'])) ? $variables['days'] : 7; |
| 58 |
$variables['page_nonce'] = $this->page_nonce; |
| 59 |
$variables['menu_url'] = $this->menu_url; |
| 60 |
|
| 61 |
if( WebTotem::isMultiSite() ){ |
| 62 |
$variables['is_multisite'] = WebTotem::isMultiSite(); |
| 63 |
$variables['is_super_admin'] = is_super_admin(); |
| 64 |
$variables['hid'] = (WebTotemRequest::get('hid')) ? '&hid=' . WebTotemRequest::get('hid') : ''; |
| 65 |
} |
| 66 |
|
| 67 |
$twig->addFilter(new \Twig\TwigFilter('trans', array( $this, 'translate' ))); |
| 68 |
$twig->addFunction(new \Twig\TwigFunction('plural', array( $this, 'plural' ))); |
| 69 |
$twig->addFilter(new \Twig\TwigFilter('t', array( $this, 'translate' ))); |
| 70 |
|
| 71 |
return $twig->render($template, $variables); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Page rendering based on array data. |
| 76 |
* |
| 77 |
* @param $params |
| 78 |
* |
| 79 |
* @return bool|string |
| 80 |
* @throws LoaderError |
| 81 |
* @throws RuntimeError |
| 82 |
* @throws SyntaxError |
| 83 |
*/ |
| 84 |
public function arrayRender($params) { |
| 85 |
|
| 86 |
$render = ''; |
| 87 |
if(is_array($params)){ |
| 88 |
|
| 89 |
if(array_key_exists('template', $params)){ |
| 90 |
$template = $params['template'] . '.html.twig'; |
| 91 |
$variables = (isset($params['variables'])) ? $params['variables'] : []; |
| 92 |
|
| 93 |
$render = $this->twigRender($template, $variables) ?: ''; |
| 94 |
} else { |
| 95 |
foreach ($params as $param){ |
| 96 |
$template = $param['template'] . '.html.twig'; |
| 97 |
$variables = (isset($param['variables'])) ? $param['variables'] : []; |
| 98 |
|
| 99 |
$render .= $this->twigRender($template, $variables) ?: ''; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
return $render; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Generate a page based on a basic template and content. |
| 110 |
* |
| 111 |
* @param $page_content |
| 112 |
* |
| 113 |
* @return bool|string |
| 114 |
* @throws LoaderError |
| 115 |
* @throws RuntimeError |
| 116 |
* @throws SyntaxError |
| 117 |
*/ |
| 118 |
public function baseTemplate($page_content) { |
| 119 |
|
| 120 |
if(WebTotemRequest::get('hid')){ |
| 121 |
WebTotemOption::setSessionOptions(['host_id' => WebTotemRequest::get('hid')]); |
| 122 |
} |
| 123 |
|
| 124 |
$variables['menu_url'] = WebTotem::adminURL('admin.php?page=wtotem'); |
| 125 |
|
| 126 |
$page = str_replace(['wtotem', '_'], '', WebTotemRequest::get('page')); |
| 127 |
$page = $page ?: 'dashboard'; |
| 128 |
$variables['is_active'][$page] = 'wtotem_nav__link_active'; |
| 129 |
$variables['page'] = $page; |
| 130 |
|
| 131 |
$variables['theme_mode'] = WebTotem::getThemeMode(); |
| 132 |
$variables['notifications'] = WebTotem::getNotifications(); |
| 133 |
$variables['current_year'] = date('Y'); |
| 134 |
$variables['content'] = $page_content; |
| 135 |
|
| 136 |
return $this->twigRender('layout.html.twig', $variables); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* String translation. |
| 142 |
* |
| 143 |
* @param $string |
| 144 |
* @param array $params |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public static function translate($string, array $params = []) { |
| 149 |
|
| 150 |
global $locale; |
| 151 |
|
| 152 |
$string = ('en_US' !== $locale) ? translate($string, 'wtotem') : $string; |
| 153 |
|
| 154 |
if($params){ |
| 155 |
foreach ($params as $key => $value){ |
| 156 |
$string = str_replace($key, $value, $string); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return (string) $string; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param array $params [single, plural, number] |
| 165 |
* |
| 166 |
* usage example |
| 167 |
* {{ plural({'single' : '%s month', 'plural' : '%s months', 'number' : 1}) }} |
| 168 |
* |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public static function plural( array $params): string { |
| 172 |
|
| 173 |
$string = _n( $params['single'], $params['plural'], $params['number'],'wtotem' ); |
| 174 |
$string = str_replace('%s', $params['number'], $string); |
| 175 |
|
| 176 |
return (string) $string; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get HTML without Twig |
| 181 |
* |
| 182 |
* @return string|bool |
| 183 |
*/ |
| 184 |
public function getHtml($template) { |
| 185 |
$templatePath = WEBTOTEM_PLUGIN_PATH . '/includes/templates/' . $template . '.html.twig'; |
| 186 |
if(!file_exists($templatePath)) { |
| 187 |
return FALSE; |
| 188 |
} |
| 189 |
|
| 190 |
ob_start(); |
| 191 |
include $templatePath; |
| 192 |
return ob_get_clean(); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|