PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
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 / controller.php

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

166 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Calotes\Base;
4
5 /**
6 * This class use for:
7 * 1. Register admin page.
8 * 2. Register sub page.
9 * 3. Help to queue scripts & output script data for frontend.
10 * 4. Render frontend view.
11 *
12 * Class Controller
13 *
14 * @package Calotes\Base
15 */
16 class Controller extends Component {
17 protected $slug;
18 protected $layout = null;
19
20 /**
21 * A helper to quickly create a page or subpage.
22 *
23 * @param string $title
24 * @param string $slug
25 * @param callable $callback
26 * @param null|string $icon
27 * @param null|string $parent_slug
28 * @param string $menu_title
29 */
30 public function register_page( $title, $slug, $callback, $parent_slug = null, $icon = null, $menu_title = '' ) {
31 $hook = is_multisite() ? 'network_admin_menu' : 'admin_menu';
32 $menu_title = '' !== $menu_title ? $menu_title : $title;
33 $function = function () use ( $title, $slug, $callback, $hook, $parent_slug, $icon, $menu_title ) {
34 $cap = is_multisite() ? 'manage_network_options' : 'manage_options';
35 if ( null === $parent_slug ) {
36 add_menu_page( $title, $menu_title, $cap, $slug, $callback, $icon );
37 } else {
38 add_submenu_page( $parent_slug, $title, $menu_title, $cap, $slug, $callback );
39 }
40 };
41
42 add_action( $hook, $function );
43 }
44
45 /**
46 * @param string|array $view_file
47 * @param array $params
48 * @param bool $echo
49 *
50 * @return bool|string
51 */
52 public function render( $view_file, $params = array(), $echo = true ) {
53 $stop_further = $this->check_has_server_error();
54
55 if ( $stop_further ) {
56 return false;
57 }
58
59 $base_path = $this->get_base_path();
60 $view = new View( $base_path . 'view' );
61 // Assign controller to this.
62 if ( ! isset( $params['controller'] ) ) {
63 $params['controller'] = $this;
64 }
65 if ( is_array( $view_file ) ) {
66 $content = '';
67 foreach ( $view_file as $vf ) {
68 $content .= $view->render( $vf, $params );
69 }
70 } else {
71 $content = $view->render( $view_file, $params );
72 }
73
74 if ( ! empty( $this->layout ) ) {
75 $template = new View( $base_path . 'view' . DIRECTORY_SEPARATOR . 'layouts' );
76 $content = $template->render(
77 $this->layout,
78 array_merge(
79 $params,
80 array(
81 'controller' => $this,
82 'contents' => $content,
83 )
84 )
85 );
86 }
87 if ( false === $echo ) {
88 return $content;
89 }
90
91 echo $content;
92 }
93
94 /**
95 * This will guess the called class path, and return the base.
96 *
97 * @return bool|string
98 */
99 private function get_base_path() {
100 $reflector = new \ReflectionClass( get_called_class() );
101 $base_path = dirname( dirname( $reflector->getFileName() ) );
102
103 if ( is_dir( $base_path . DIRECTORY_SEPARATOR . 'controller' )
104 && is_dir( $base_path . DIRECTORY_SEPARATOR . 'view' )
105 ) {
106 return $base_path . DIRECTORY_SEPARATOR;
107 }
108
109 return false;
110 }
111
112 /**
113 * @param $view_file
114 * @param array $params
115 * @param bool $echo
116 *
117 * @return bool|string
118 */
119 public function render_partial( $view_file, $params = array(), $echo = true ) {
120 $base_path = $this->get_base_path();
121
122 if ( ! isset( $params['controller'] ) ) {
123 $params['controller'] = $this;
124 }
125
126 $view = new View( $base_path . 'view' );
127 if ( is_array( $view_file ) ) {
128 $content = '';
129 foreach ( $view_file as $vf ) {
130 $content .= $view->render( $vf, $params );
131 }
132 } else {
133 $content = $view->render( $view_file, $params );
134 }
135 if ( true === $echo ) {
136 echo $content;
137 }
138
139 return $content;
140 }
141
142 /**
143 * Check for has server not found error.
144 *
145 * @return bool
146 */
147 private function check_has_server_error() {
148 global $defender_server_not_supported;
149
150 if ( is_wp_error( $defender_server_not_supported ) ) {
151 $html = '<div class="sui-wrap"><div class="sui-notice sui-notice-info">';
152 $html .= '<div class="sui-notice-content">';
153 $html .= '<div class="sui-notice-message">';
154 $html .= '<p>' . $defender_server_not_supported->get_error_message() . '</p>';
155 $html .= '</div></div></div></div>';
156
157 echo $html;
158
159 return true;
160 }
161
162 return false;
163 }
164
165 }
166