controller.php
102 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Common logic for all shortcodes plugin implements |
| 4 | * |
| 5 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 | */ |
| 7 | abstract class PMXI_Controller { |
| 8 | /** |
| 9 | * Input class instance to retrieve parameters submitted during page request |
| 10 | * @var PMXI_Input |
| 11 | */ |
| 12 | protected $input; |
| 13 | /** |
| 14 | * Error messages |
| 15 | * @var WP_Error |
| 16 | */ |
| 17 | protected $errors; |
| 18 | /** |
| 19 | * Associative array of data which will be automatically available as variables when template is rendered |
| 20 | * @var array |
| 21 | */ |
| 22 | public $data = array(); |
| 23 | /** |
| 24 | * Constructor |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | $this->input = new PMXI_Input(); |
| 28 | $this->input->addFilter('trim'); |
| 29 | |
| 30 | $this->errors = new WP_Error(); |
| 31 | |
| 32 | $this->init(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Method to put controller initialization logic to |
| 37 | */ |
| 38 | protected function init() {} |
| 39 | |
| 40 | /** |
| 41 | * Checks wether protocol is HTTPS and redirects user to secure connection if not |
| 42 | */ |
| 43 | protected function force_ssl() { |
| 44 | if (force_ssl_admin() && ! is_ssl()) { |
| 45 | if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) { |
| 46 | wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI'])); die(); |
| 47 | } else { |
| 48 | wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); die(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Method returning resolved template content |
| 55 | * |
| 56 | * @param string[optional] $viewPath Template path to render |
| 57 | */ |
| 58 | protected function render($viewPath = null) { |
| 59 | // assume template file name depending on calling function |
| 60 | if (is_null($viewPath)) { |
| 61 | $trace = debug_backtrace(); |
| 62 | $viewPath = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower($trace[1]['class']))) . '/' . $trace[1]['function']; |
| 63 | } |
| 64 | // append file extension if not specified |
| 65 | if ( ! preg_match('%\.php$%', $viewPath)) { |
| 66 | $viewPath .= '.php'; |
| 67 | } |
| 68 | $filePath = PMXI_Plugin::ROOT_DIR . '/views/' . $viewPath; |
| 69 | if (is_file($filePath)) { |
| 70 | extract($this->data); |
| 71 | include $filePath; |
| 72 | } else { |
| 73 | throw new Exception("Requested template file $filePath is not found."); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Display list of errors |
| 79 | * |
| 80 | * @param string|array|WP_Error[optional] $msgs |
| 81 | */ |
| 82 | protected function error($msgs = NULL) { |
| 83 | if (is_null($msgs)) { |
| 84 | $msgs = $this->errors; |
| 85 | } |
| 86 | if (is_wp_error($msgs)) { |
| 87 | $msgs = $msgs->get_error_messages(); |
| 88 | } |
| 89 | if ( ! is_array($msgs)) { |
| 90 | $msgs = array($msgs); |
| 91 | } |
| 92 | $this->data['errors'] = $msgs; |
| 93 | |
| 94 | $viewPathRel = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower(get_class($this)))) . '/error.php'; |
| 95 | if (is_file(PMXI_Plugin::ROOT_DIR . '/views/' . $viewPathRel)) { // if calling controller class has specific error view |
| 96 | $this->render($viewPathRel); |
| 97 | } else { // render default error view |
| 98 | $this->render('controller/error.php'); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } |