PluginProbe
WebTotem Security / 2.4.25
WebTotem Security v2.4.25
3.0.2 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 All 110 releases
wt-security / lib / Template.php

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

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