PluginProbe
WebTotem Security / 2.4.6
WebTotem Security v2.4.6
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.6, at lib/Template.php

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