PluginProbe
WebTotem Security / 2.4.26
WebTotem Security v2.4.26
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / lib / Template.php

Template.php in WebTotem Security 2.4.26, at lib/Template.php

219 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
22 protected $loader;
23 protected $page_nonce;
24 protected $images_path;
25 protected $menu_url;
26 protected $domain;
27
28 function __construct()
29 {
30 if (class_exists('\Twig\Loader\FilesystemLoader')) {
31 $this->loader = new FilesystemLoader(WEBTOTEM_PLUGIN_PATH . '/includes/templates/');
32 }
33 $this->page_nonce = wp_create_nonce('wtotem_page_nonce');
34 $this->images_path = WebTotem::getImagePath('');
35 $this->menu_url = WebTotem::adminURL('admin.php?page=wtotem');
36 $this->domain = WEBTOTEM_SITE_DOMAIN;
37 }
38
39 /**
40 * Rendering a template using twig and filling in data.
41 *
42 * @param string $template
43 * @param array $variables
44 *
45 * @return bool|string
46 * @throws LoaderError
47 * @throws RuntimeError
48 * @throws SyntaxError
49 */
50 public function twigRender($template, $variables = [])
51 {
52
53 $twig = new Environment($this->loader);
54
55 if (!file_exists(WEBTOTEM_PLUGIN_PATH . '/includes/templates/' . $template)) {
56 WebTotemOption::setNotification('error', __('There is no template: ', 'wtotem') . $template);
57 return FALSE;
58 }
59
60 // Default values of some variables
61 $variables['images_path'] = $this->images_path;
62 $variables['days'] = (isset($variables['days'])) ? $variables['days'] : 7;
63 $variables['page_nonce'] = $this->page_nonce;
64 $variables['menu_url'] = $this->menu_url;
65 $variables['domain'] = $this->domain;
66 $variables['user_email'] = WebTotem::getUserEmail();
67
68 if (WebTotem::isMultiSite()) {
69 $variables['is_multisite'] = WebTotem::isMultiSite();
70 $variables['is_super_admin'] = is_super_admin();
71 $variables['hid'] = (WebTotemRequest::get('hid')) ? '&hid=' . WebTotemRequest::get('hid') : '';
72 }
73
74 $twig->addFilter(new \Twig\TwigFilter('trans', array($this, 'translate')));
75 $twig->addFunction(new \Twig\TwigFunction('plural', array($this, 'plural')));
76 $twig->addFilter(new \Twig\TwigFilter('t', array($this, 'translate')));
77
78 return $twig->render($template, $variables);
79 }
80
81 /**
82 * Page rendering based on array data.
83 *
84 * @param $params
85 *
86 * @return bool|string
87 * @throws LoaderError
88 * @throws RuntimeError
89 * @throws SyntaxError
90 */
91 public function arrayRender($params)
92 {
93
94 $render = '';
95 if (is_array($params)) {
96
97 if (array_key_exists('template', $params)) {
98 $template = $params['template'] . '.html.twig';
99 $variables = (isset($params['variables'])) ? $params['variables'] : [];
100
101 $render = $this->twigRender($template, $variables) ?: '';
102 } else {
103 foreach ($params as $param) {
104 $template = $param['template'] . '.html.twig';
105 $variables = (isset($param['variables'])) ? $param['variables'] : [];
106
107 $render .= $this->twigRender($template, $variables) ?: '';
108 }
109 }
110
111 }
112
113 return $render;
114 }
115
116 /**
117 * Generate a page based on a basic template and content.
118 *
119 * @param $page_content
120 *
121 * @return bool|string
122 * @throws LoaderError
123 * @throws RuntimeError
124 * @throws SyntaxError
125 */
126 public function baseTemplate($page_content)
127 {
128
129 if (WebTotemRequest::get('hid')) {
130 WebTotemOption::setSessionOptions(['host_id' => WebTotemRequest::get('hid')]);
131 }
132
133 $variables['menu_url'] = WebTotem::adminURL('admin.php?page=wtotem');
134
135 $page = str_replace(['wtotem', '_'], '', WebTotemRequest::get('page'));
136 $page = $page ?: 'dashboard';
137 $variables['is_active'][$page] = 'wtotem_nav__link_active';
138 $variables['page'] = $page;
139
140 if ($page != 'activation') {
141 $user_feedback = WebTotemAPI::getFeedback();
142 if(is_array($user_feedback) and array_key_exists('score', $user_feedback)){
143 $variables['user_feedback'] = (bool)$user_feedback['score'];
144 } else {
145 $variables['user_feedback'] = true;
146 }
147 } else {
148 $variables['user_feedback'] = true;
149 }
150
151 $variables['theme_mode'] = WebTotem::getThemeMode();
152 $variables['notifications'] = WebTotem::getNotifications();
153 $variables['current_year'] = date('Y');
154 $variables['content'] = $page_content;
155
156 return $this->twigRender('layout.html.twig', $variables);
157 }
158
159
160 /**
161 * String translation.
162 *
163 * @param $string
164 * @param array $params
165 *
166 * @return string
167 */
168 public static function translate($string, array $params = [])
169 {
170
171 global $locale;
172
173 $string = ('en_US' !== $locale) ? translate($string, 'wtotem') : $string;
174
175 if ($params) {
176 foreach ($params as $key => $value) {
177 $string = str_replace($key, $value, $string);
178 }
179 }
180
181 return (string)$string;
182 }
183
184 /**
185 * @param array $params [single, plural, number]
186 *
187 * usage example
188 * {{ plural({'single' : '%s month', 'plural' : '%s months', 'number' : 1}) }}
189 *
190 * @return string
191 */
192 public static function plural(array $params): string
193 {
194
195 $string = _n($params['single'], $params['plural'], $params['number'], 'wtotem');
196 $string = str_replace('%s', $params['number'], $string);
197
198 return (string)$string;
199 }
200
201 /**
202 * Get HTML without Twig
203 *
204 * @return string|bool
205 */
206 public function getHtml($template)
207 {
208 $templatePath = WEBTOTEM_PLUGIN_PATH . '/includes/templates/' . $template . '.html.twig';
209 if (!file_exists($templatePath)) {
210 return FALSE;
211 }
212
213 ob_start();
214 include $templatePath;
215 return ob_get_clean();
216 }
217
218 }
219