controller.php
147 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Common logic for all shortcodes plugin implements |
| 4 | * |
| 5 | * @author Maksym Tsypliakov <maksym.tsypliakov@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 | * Warning messages |
| 20 | * @var WP_Error |
| 21 | */ |
| 22 | protected $warnings; |
| 23 | /** |
| 24 | * Associative array of data which will be automatically available as variables when template is rendered |
| 25 | * @var array |
| 26 | */ |
| 27 | public $data = array(); |
| 28 | /** |
| 29 | * Constructor |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->input = new PMXI_Input(); |
| 33 | $this->input->addFilter('trim'); |
| 34 | |
| 35 | $this->errors = new WP_Error(); |
| 36 | $this->warnings = new WP_Error(); |
| 37 | |
| 38 | $this->init(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Method to put controller initialization logic to |
| 43 | */ |
| 44 | protected function init() {} |
| 45 | |
| 46 | /** |
| 47 | * Checks wether protocol is HTTPS and redirects user to secure connection if not |
| 48 | */ |
| 49 | protected function force_ssl() { |
| 50 | if (force_ssl_admin() && ! is_ssl()) { |
| 51 | if ( 0 === strpos(esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'] ?? '')), 'http') ) { |
| 52 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 53 | wp_redirect(preg_replace('|^http://|', 'https://', esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'] ?? '')))); die(); |
| 54 | } else { |
| 55 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 56 | wp_redirect('https://' . sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'] ?? '')) . esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'] ?? ''))); die(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Method returning resolved template content |
| 63 | * |
| 64 | * @param string[optional] $viewPath Template path to render |
| 65 | */ |
| 66 | protected function render($viewPath = null) { |
| 67 | |
| 68 | if ( ! get_current_user_id() or ! current_user_can( PMXI_Plugin::$capabilities )) { |
| 69 | // This nonce is not valid. |
| 70 | die( 'Security check' ); |
| 71 | } else { |
| 72 | // assume template file name depending on calling function |
| 73 | if (is_null($viewPath)) { |
| 74 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 75 | $trace = debug_backtrace(); |
| 76 | $viewPath = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower($trace[1]['class']))) . '/' . $trace[1]['function']; |
| 77 | } |
| 78 | // append file extension if not specified |
| 79 | if ( ! preg_match('%\.php$%', $viewPath)) { |
| 80 | $viewPath .= '.php'; |
| 81 | } |
| 82 | $filePath = PMXI_Plugin::ROOT_DIR . '/views/' . $viewPath; |
| 83 | if (is_file($filePath)) { |
| 84 | extract($this->data); |
| 85 | include $filePath; |
| 86 | } else { |
| 87 | throw new Exception(esc_html("Requested template file $filePath is not found.")); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Display list of errors |
| 94 | * |
| 95 | * @param string|array|WP_Error[optional] $msgs |
| 96 | */ |
| 97 | protected function error($msgs = NULL) { |
| 98 | if (is_null($msgs)) { |
| 99 | $msgs = $this->errors; |
| 100 | } |
| 101 | if (is_wp_error($msgs)) { |
| 102 | unset($msgs->errors['root-element-validation']); |
| 103 | unset($msgs->errors['upload-validation']); |
| 104 | unset($msgs->errors['delete-missing-validation']); |
| 105 | $msgs = $msgs->get_error_messages(); |
| 106 | } |
| 107 | if ( ! is_array($msgs)) { |
| 108 | $msgs = array($msgs); |
| 109 | } |
| 110 | $this->data['errors'] = $msgs; |
| 111 | |
| 112 | $viewPathRel = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower(get_class($this)))) . '/error.php'; |
| 113 | if (is_file(PMXI_Plugin::ROOT_DIR . '/views/' . $viewPathRel)) { // if calling controller class has specific error view |
| 114 | $this->render($viewPathRel); |
| 115 | } else { // render default error view |
| 116 | $this->render('controller/error.php'); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Display list of warnings |
| 122 | * |
| 123 | * @param string|array|WP_Error[optional] $msgs |
| 124 | */ |
| 125 | protected function warning($msgs = NULL) { |
| 126 | if (is_null($msgs)) { |
| 127 | $msgs = $this->warnings; |
| 128 | } |
| 129 | if (is_wp_error($msgs)) { |
| 130 | unset($msgs->errors['root-element-validation']); |
| 131 | $msgs = $msgs->get_error_messages(); |
| 132 | } |
| 133 | if ( ! is_array($msgs)) { |
| 134 | $msgs = array($msgs); |
| 135 | } |
| 136 | $this->data['warnings'] = $msgs; |
| 137 | |
| 138 | $viewPathRel = str_replace('_', '/', preg_replace('%^' . preg_quote(PMXI_Plugin::PREFIX, '%') . '%', '', strtolower(get_class($this)))) . '/warning.php'; |
| 139 | if (is_file(PMXI_Plugin::ROOT_DIR . '/views/' . $viewPathRel)) { // if calling controller class has specific error view |
| 140 | $this->render($viewPathRel); |
| 141 | } else { // render default error view |
| 142 | $this->render('controller/warning.php'); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | } |
| 147 |