| 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 |
// Custimize the text. |
| 94 |
add_filter( |
| 95 |
'wpmudev_hub_connector_localize_text_vars', |
| 96 |
array( 'WP_Defender\Component\Hub_Connector', 'customize_text_vars' ), |
| 97 |
10, |
| 98 |
2 |
| 99 |
); |
| 100 |
do_action( 'wpmudev_hub_connector_ui', Hub_Connector::PLUGIN_IDENTIFIER ); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$base_path = $this->get_base_path(); |
| 105 |
$view = new View( $base_path . 'view' ); |
| 106 |
// Assign controller to this. |
| 107 |
if ( ! isset( $params['controller'] ) ) { |
| 108 |
$params['controller'] = $this; |
| 109 |
} |
| 110 |
if ( is_array( $view_file ) ) { |
| 111 |
$content = ''; |
| 112 |
foreach ( $view_file as $vf ) { |
| 113 |
$content .= $view->render( $vf, $params ); |
| 114 |
} |
| 115 |
} else { |
| 116 |
$content = $view->render( $view_file, $params ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( null !== $this->layout && '' !== $this->layout ) { |
| 120 |
$template = new View( $base_path . 'view' . DIRECTORY_SEPARATOR . 'layouts' ); |
| 121 |
$content = $template->render( |
| 122 |
$this->layout, |
| 123 |
array_merge( |
| 124 |
$params, |
| 125 |
array( |
| 126 |
'controller' => $this, |
| 127 |
'contents' => $content, |
| 128 |
) |
| 129 |
) |
| 130 |
); |
| 131 |
} |
| 132 |
if ( false === $output ) { |
| 133 |
return $content; |
| 134 |
} |
| 135 |
|
| 136 |
echo wp_kses_post( $content ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* This will guess the called class path, and return the base. |
| 141 |
* |
| 142 |
* @return bool|string |
| 143 |
*/ |
| 144 |
private function get_base_path() { |
| 145 |
$reflector = new ReflectionClass( get_called_class() ); |
| 146 |
$base_path = dirname( $reflector->getFileName(), 2 ); |
| 147 |
|
| 148 |
if ( is_dir( $base_path . DIRECTORY_SEPARATOR . 'controller' ) |
| 149 |
&& is_dir( $base_path . DIRECTORY_SEPARATOR . 'view' ) |
| 150 |
) { |
| 151 |
return $base_path . DIRECTORY_SEPARATOR; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Render a partial view file. |
| 159 |
* |
| 160 |
* @param mixed $view_file The view file to render. |
| 161 |
* @param array $params Optional. The parameters to pass to the view file. Default is an empty array. |
| 162 |
* @param bool $output Optional. Whether to output the rendered content or return it. Default is true. |
| 163 |
* |
| 164 |
* @return string The rendered content. |
| 165 |
*/ |
| 166 |
public function render_partial( $view_file, $params = array(), $output = true ) { |
| 167 |
$base_path = $this->get_base_path(); |
| 168 |
|
| 169 |
if ( ! isset( $params['controller'] ) ) { |
| 170 |
$params['controller'] = $this; |
| 171 |
} |
| 172 |
|
| 173 |
$view = new View( $base_path . 'view' ); |
| 174 |
if ( is_array( $view_file ) ) { |
| 175 |
$content = ''; |
| 176 |
foreach ( $view_file as $vf ) { |
| 177 |
$content .= $view->render( $vf, $params ); |
| 178 |
} |
| 179 |
} else { |
| 180 |
$content = $view->render( $view_file, $params ); |
| 181 |
} |
| 182 |
if ( true === $output ) { |
| 183 |
/** |
| 184 |
* Ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 185 |
* Why? |
| 186 |
* Because $content has scripts that will be broken if we escape it. |
| 187 |
*/ |
| 188 |
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 189 |
} |
| 190 |
|
| 191 |
return $content; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Check for has server not found error. |
| 196 |
* |
| 197 |
* @return bool |
| 198 |
*/ |
| 199 |
private function check_has_server_error(): bool { |
| 200 |
global $defender_server_not_supported; |
| 201 |
|
| 202 |
if ( is_wp_error( $defender_server_not_supported ) ) { |
| 203 |
$html = '<div class="sui-wrap"><div class="sui-notice sui-notice-info">'; |
| 204 |
$html .= '<div class="sui-notice-content">'; |
| 205 |
$html .= '<div class="sui-notice-message">'; |
| 206 |
$html .= '<p>' . $defender_server_not_supported->get_error_message() . '</p>'; |
| 207 |
$html .= '</div></div></div></div>'; |
| 208 |
|
| 209 |
echo wp_kses_post( $html ); |
| 210 |
|
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
|