PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / framework / base / class-controller.php

class-controller.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at framework/base/class-controller.php

218 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base controller.
4 *
5 * @package Calotes\Base
6 */
7
8 namespace Calotes\Base;
9
10 use ReflectionClass;
11 use WP_Defender\Component\Hub_Connector;
12
13 /**
14 * This class use for:
15 * 1. Register admin page.
16 * 2. Register sub-page.
17 * 3. Help to queue scripts & output script data for frontend.
18 * 4. Render frontend view.
19 */
20 class Controller extends Component {
21
22 /**
23 * The slug for the page.
24 *
25 * @var string
26 */
27 protected $slug;
28
29 /**
30 * The layout for the page.
31 *
32 * @var string|null
33 */
34 protected $layout = null;
35
36 /**
37 * Registers a page or subpage in the admin menu.
38 *
39 * @param string $title The title of the page.
40 * @param string $slug The slug for the page.
41 * @param callable $callback The callback function to handle the page content.
42 * @param string|null $parent_slug Optional. The slug of the parent page. Default is null.
43 * @param string|null $icon Optional. The icon for the menu item. Default is null.
44 * @param string $menu_title Optional. The title for the menu item. Default is an empty string.
45 */
46 public function register_page( $title, $slug, $callback, $parent_slug = null, $icon = null, $menu_title = '' ) {
47 $hook = is_multisite() ? 'network_admin_menu' : 'admin_menu';
48 $menu_title = '' !== $menu_title ? $menu_title : $title;
49 $function = function () use ( $title, $slug, $callback, $parent_slug, $icon, $menu_title ) {
50 $cap = is_multisite() ? 'manage_network_options' : 'manage_options';
51 if ( null === $parent_slug ) {
52 $page_hook = add_menu_page( $title, $menu_title, $cap, $slug, $callback, $icon );
53 } else {
54 $page_hook = add_submenu_page( $parent_slug, $title, $menu_title, $cap, $slug, $callback );
55 }
56 add_action( 'load-' . $page_hook, array( $this, 'trigger_load_action' ) );
57 };
58
59 add_action( $hook, $function );
60 }
61
62 /**
63 * Redirects to the Onboard page only on single sites or network admin.
64 */
65 public function trigger_load_action() {
66 $redirect_slug = 'wp-defender';
67 if (
68 $redirect_slug !== $this->slug
69 && true !== (bool) get_site_option( 'wp_defender_shown_activator' )
70 ) {
71 // Redirect to the Onboard page only on single sites or network admin.
72 wp_safe_redirect( network_admin_url( 'admin.php?page=' . $redirect_slug ) );
73 exit;
74 }
75 }
76
77 /**
78 * Render a view file.
79 *
80 * @param mixed $view_file The view file to render.
81 * @param array $params Optional. The parameters to pass to the view file. Default is an empty array.
82 * @param bool $output Optional. Whether to output the rendered content or return it. Default is true.
83 *
84 * @return void|bool|string If $output is false, the rendered content. Otherwise, true on success, false on failure.
85 */
86 public function render( $view_file, $params = array(), $output = true ) {
87 $stop_further = $this->check_has_server_error();
88
89 if ( $stop_further ) {
90 return false;
91 }
92 if ( Hub_Connector::should_render() ) {
93 wp_dequeue_script( 'def-iplockout' );
94 // Custimize the text.
95 add_filter(
96 'wpmudev_hub_connector_localize_text_vars',
97 array( 'WP_Defender\Component\Hub_Connector', 'customize_text_vars' ),
98 10,
99 2
100 );
101 do_action( 'wpmudev_hub_connector_ui', Hub_Connector::PLUGIN_IDENTIFIER );
102 return;
103 }
104
105 $base_path = $this->get_base_path();
106 $view = new View( $base_path . 'view' );
107 // Assign controller to this.
108 if ( ! isset( $params['controller'] ) ) {
109 $params['controller'] = $this;
110 }
111 if ( is_array( $view_file ) ) {
112 $content = '';
113 foreach ( $view_file as $vf ) {
114 $content .= $view->render( $vf, $params );
115 }
116 } else {
117 $content = $view->render( $view_file, $params );
118 }
119
120 if ( ! empty( $this->layout ) ) {
121 $template = new View( $base_path . 'view' . DIRECTORY_SEPARATOR . 'layouts' );
122 $content = $template->render(
123 $this->layout,
124 array_merge(
125 $params,
126 array(
127 'controller' => $this,
128 'contents' => $content,
129 )
130 )
131 );
132 }
133 if ( false === $output ) {
134 return $content;
135 }
136
137 echo wp_kses_post( $content );
138 }
139
140 /**
141 * This will guess the called class path, and return the base.
142 *
143 * @return bool|string
144 */
145 private function get_base_path() {
146 $reflector = new ReflectionClass( get_called_class() );
147 $base_path = dirname( $reflector->getFileName(), 2 );
148
149 if ( is_dir( $base_path . DIRECTORY_SEPARATOR . 'controller' )
150 && is_dir( $base_path . DIRECTORY_SEPARATOR . 'view' )
151 ) {
152 return $base_path . DIRECTORY_SEPARATOR;
153 }
154
155 return false;
156 }
157
158 /**
159 * Render a partial view file.
160 *
161 * @param mixed $view_file The view file to render.
162 * @param array $params Optional. The parameters to pass to the view file. Default is an empty array.
163 * @param bool $output Optional. Whether to output the rendered content or return it. Default is true.
164 *
165 * @return string The rendered content.
166 */
167 public function render_partial( $view_file, $params = array(), $output = true ) {
168 $base_path = $this->get_base_path();
169
170 if ( ! isset( $params['controller'] ) ) {
171 $params['controller'] = $this;
172 }
173
174 $view = new View( $base_path . 'view' );
175 if ( is_array( $view_file ) ) {
176 $content = '';
177 foreach ( $view_file as $vf ) {
178 $content .= $view->render( $vf, $params );
179 }
180 } else {
181 $content = $view->render( $view_file, $params );
182 }
183 if ( true === $output ) {
184 /**
185 * Ignore WordPress.Security.EscapeOutput.OutputNotEscaped
186 * Why?
187 * Because $content has scripts that will be broken if we escape it.
188 */
189 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
190 }
191
192 return $content;
193 }
194
195 /**
196 * Check for has server not found error.
197 *
198 * @return bool
199 */
200 private function check_has_server_error(): bool {
201 global $defender_server_not_supported;
202
203 if ( is_wp_error( $defender_server_not_supported ) ) {
204 $html = '<div class="sui-wrap"><div class="sui-notice sui-notice-info">';
205 $html .= '<div class="sui-notice-content">';
206 $html .= '<div class="sui-notice-message">';
207 $html .= '<p>' . $defender_server_not_supported->get_error_message() . '</p>';
208 $html .= '</div></div></div></div>';
209
210 echo wp_kses_post( $html );
211
212 return true;
213 }
214
215 return false;
216 }
217 }
218