| 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 sub page |
| 22 |
* |
| 23 |
* @param $title |
| 24 |
* @param $slug |
| 25 |
* @param $callback |
| 26 |
* @param null $icon |
| 27 |
* @param null $parent_slug |
| 28 |
*/ |
| 29 |
public function register_page( $title, $slug, $callback, $parent_slug = null, $icon = null ) { |
| 30 |
$hook = is_multisite() ? 'network_admin_menu' : 'admin_menu'; |
| 31 |
$function = function () use ( $title, $slug, $callback, $hook, $parent_slug, $icon ) { |
| 32 |
$cap = is_multisite() ? 'manage_network_options' : 'manage_options'; |
| 33 |
if ( null === $parent_slug ) { |
| 34 |
add_menu_page( $title, $title, $cap, $slug, $callback, $icon ); |
| 35 |
} else { |
| 36 |
add_submenu_page( $parent_slug, $title, $title, $cap, $slug, $callback ); |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
add_action( $hook, $function ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @param $view_file |
| 45 |
* @param array $params |
| 46 |
* @param bool $echo |
| 47 |
* |
| 48 |
* @return bool|string |
| 49 |
*/ |
| 50 |
public function render( $view_file, $params = array(), $echo = true ) { |
| 51 |
$stop_further = $this->check_has_server_error(); |
| 52 |
|
| 53 |
if ( $stop_further ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
$base_path = $this->get_base_path(); |
| 58 |
$view = new View( $base_path . 'view' ); |
| 59 |
// assign controller to this |
| 60 |
if ( ! isset( $params['controller'] ) ) { |
| 61 |
$params['controller'] = $this; |
| 62 |
} |
| 63 |
if ( is_array( $view_file ) ) { |
| 64 |
$content = ''; |
| 65 |
foreach ( $view_file as $vf ) { |
| 66 |
$content .= $view->render( $vf, $params ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
$content = $view->render( $view_file, $params ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $this->layout ) ) { |
| 73 |
$template = new View( $base_path . 'view' . DIRECTORY_SEPARATOR . 'layouts' ); |
| 74 |
$content = $template->render( |
| 75 |
$this->layout, |
| 76 |
array_merge( |
| 77 |
$params, |
| 78 |
array( |
| 79 |
'controller' => $this, |
| 80 |
'contents' => $content, |
| 81 |
) |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
if ( false === $echo ) { |
| 86 |
return $content; |
| 87 |
} |
| 88 |
|
| 89 |
echo $content; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* This will guess the called class path, and return the base |
| 94 |
* |
| 95 |
* @return bool|string |
| 96 |
*/ |
| 97 |
private function get_base_path() { |
| 98 |
$reflector = new \ReflectionClass( get_called_class() ); |
| 99 |
$base_path = dirname( dirname( $reflector->getFileName() ) ); |
| 100 |
|
| 101 |
if ( is_dir( $base_path . DIRECTORY_SEPARATOR . 'controller' ) |
| 102 |
&& is_dir( $base_path . DIRECTORY_SEPARATOR . 'view' ) |
| 103 |
) { |
| 104 |
return $base_path . DIRECTORY_SEPARATOR; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @param $view_file |
| 112 |
* @param array $params |
| 113 |
* @param bool $echo |
| 114 |
* |
| 115 |
* @return bool|string |
| 116 |
*/ |
| 117 |
public function render_partial( $view_file, $params = array(), $echo = true ) { |
| 118 |
$base_path = $this->get_base_path(); |
| 119 |
|
| 120 |
if ( ! isset( $params['controller'] ) ) { |
| 121 |
$params['controller'] = $this; |
| 122 |
} |
| 123 |
|
| 124 |
$view = new View( $base_path . 'view' ); |
| 125 |
if ( is_array( $view_file ) ) { |
| 126 |
$content = ''; |
| 127 |
foreach ( $view_file as $vf ) { |
| 128 |
$content .= $view->render( $vf, $params ); |
| 129 |
} |
| 130 |
} else { |
| 131 |
$content = $view->render( $view_file, $params ); |
| 132 |
} |
| 133 |
if ( true === $echo ) { |
| 134 |
echo $content; |
| 135 |
} |
| 136 |
|
| 137 |
return $content; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Check for has server not found error. |
| 142 |
* |
| 143 |
* @return bool |
| 144 |
*/ |
| 145 |
private function check_has_server_error() { |
| 146 |
global $defender_server_not_supported; |
| 147 |
|
| 148 |
if ( is_wp_error( $defender_server_not_supported ) ) { |
| 149 |
$html = '<div class="sui-wrap"><div class="sui-notice sui-notice-info">'; |
| 150 |
$html .= '<div class="sui-notice-content">'; |
| 151 |
$html .= '<div class="sui-notice-message">'; |
| 152 |
$html .= '<p>' . $defender_server_not_supported->get_error_message() . '</p>'; |
| 153 |
$html .= '</div></div></div></div>'; |
| 154 |
|
| 155 |
echo $html; |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|