| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * E2Pdf View Helper | |
| 5 | - * | |
| 6 | - * @copyright Copyright 2017 https://e2pdf.com | |
| 7 | - * @license GPLv3 | |
| 8 | - * @version 1 | |
| 9 | - * @link https://e2pdf.com | |
| 10 | - * @since 0.00.01 | |
| 4 | + * File: /helper/e2pdf-view.php | |
| 5 | + * | |
| 6 | + * @package E2Pdf | |
| 7 | + * @license GPLv3 | |
| 8 | + * @link https://e2pdf.com | |
| 11 | 9 | */ |
| 12 | 10 | if (!defined('ABSPATH')) { |
| 13 | 11 | die('Access denied.'); |
| 14 | 12 | } |
| @@ -38,9 +36,9 @@ | ||
| 38 | 36 | } |
| 39 | 37 | |
| 40 | 38 | public function __construct() { |
| 41 | 39 | $this->view = new stdClass(); |
| 42 | - $this->uri = home_url(add_query_arg(null, null)); | |
| 40 | + $this->uri = home_url(add_query_arg([], false)); | |
| 43 | 41 | $this->get = new Helper_E2pdf_Get($this->uri); |
| 44 | 42 | $this->post = new Helper_E2pdf_Post(); |
| 45 | 43 | $this->files = new Helper_E2pdf_Files(); |
| 46 | 44 | $this->page = $this->get->get_page(); |
| @@ -48,58 +46,45 @@ | ||
| 48 | 46 | $this->notification = Model_E2pdf_Notification::instance(); |
| 49 | 47 | $this->view_dir = $this->helper->get('plugin_dir') . 'classes/view/'; |
| 50 | 48 | } |
| 51 | 49 | |
| 52 | - /** | |
| 53 | - * Get Tpl Value | |
| 54 | - * @param string $key - Tpl key | |
| 55 | - * @return mixed - Tpl value | |
| 56 | - */ | |
| 50 | + // render | |
| 57 | 51 | public function render($type, $element, $args = array()) { |
| 58 | 52 | $this->tpl_args = new Helper_E2pdf_Tplargs($args); |
| 59 | 53 | $this->tpl = $this->view_dir . $type . '/' . $element . '.php'; |
| 60 | 54 | if (file_exists($this->tpl)) { |
| 61 | - include($this->tpl); | |
| 55 | + include $this->tpl; | |
| 62 | 56 | } |
| 63 | 57 | } |
| 64 | 58 | |
| 65 | - /** | |
| 66 | - * Get Tpl Value | |
| 67 | - * @param string $key - Tpl key | |
| 68 | - * @return mixed - Tpl value | |
| 69 | - */ | |
| 59 | + // render metabox | |
| 70 | 60 | public function render_metabox($post, $metabox) { |
| 71 | 61 | $this->render('metaboxes', $metabox['args']['tpl']); |
| 72 | 62 | } |
| 73 | 63 | |
| 74 | - /** | |
| 75 | - * Get Tpl Value | |
| 76 | - * @param string $key - Tpl key | |
| 77 | - * @return mixed - Tpl value | |
| 78 | - */ | |
| 64 | + // render page | |
| 79 | 65 | public function render_page() { |
| 80 | 66 | $this->tpl_page = $this->view_dir . 'page-' . $this->page . '.php'; |
| 81 | 67 | $controller = $this->page_to_controller($this->page); |
| 82 | 68 | if (class_exists($controller)) { |
| 83 | 69 | $this->controller = new $controller(); |
| 70 | + $method = 'index_action'; | |
| 84 | 71 | if ($this->get->get('action')) { |
| 85 | 72 | $method = $this->get->get('action') . '_action'; |
| 86 | - } else { | |
| 87 | - $method = 'index_action'; | |
| 88 | 73 | } |
| 89 | 74 | if (method_exists($this->controller, $method)) { |
| 90 | 75 | $this->controller->$method(); |
| 91 | 76 | $this->view = $this->controller->view; |
| 77 | + if (file_exists($this->tpl_page)) { | |
| 78 | + include $this->tpl_page; | |
| 79 | + } | |
| 92 | 80 | } else { |
| 93 | 81 | $this->handle_404(); |
| 94 | 82 | } |
| 95 | 83 | } |
| 96 | - | |
| 97 | - if (file_exists($this->tpl_page)) { | |
| 98 | - include($this->tpl_page); | |
| 99 | - } | |
| 100 | 84 | } |
| 101 | 85 | |
| 86 | + // render frontend page | |
| 102 | 87 | public function render_frontend_page() { |
| 103 | 88 | if ($this->page == 'e2pdf-download' || get_query_var('e2pdf')) { |
| 104 | 89 | $this->tpl_page = $this->view_dir . '/frontend/page-e2pdf-download.php'; |
| 105 | 90 | $controller = $this->page_to_controller('e2pdf-download', true); |
| @@ -104,23 +89,21 @@ | ||
| 104 | 89 | $this->tpl_page = $this->view_dir . '/frontend/page-e2pdf-download.php'; |
| 105 | 90 | $controller = $this->page_to_controller('e2pdf-download', true); |
| 106 | 91 | if (class_exists($controller)) { |
| 107 | 92 | $this->controller = new $controller(); |
| 93 | + $method = 'index_action'; | |
| 108 | 94 | if ($this->get->get('action')) { |
| 109 | 95 | $method = $this->get->get('action') . '_action'; |
| 110 | - } else { | |
| 111 | - $method = 'index_action'; | |
| 112 | 96 | } |
| 113 | 97 | if (method_exists($this->controller, $method)) { |
| 114 | 98 | $this->controller->$method(); |
| 99 | + if (file_exists($this->tpl_page)) { | |
| 100 | + include $this->tpl_page; | |
| 101 | + } | |
| 115 | 102 | } else { |
| 116 | 103 | $this->handle_404(); |
| 117 | 104 | } |
| 118 | 105 | } |
| 119 | - | |
| 120 | - if (file_exists($this->tpl_page)) { | |
| 121 | - include($this->tpl_page); | |
| 122 | - } | |
| 123 | 106 | } elseif ($this->page == 'e2pdf-activation') { |
| 124 | 107 | if (get_transient('e2pdf_activation_key')) { |
| 125 | 108 | $activation_key = get_transient('e2pdf_activation_key'); |
| 126 | 109 | if ($this->get->get('key') == $activation_key) { |
| @@ -134,8 +117,9 @@ | ||
| 134 | 117 | } |
| 135 | 118 | } |
| 136 | 119 | } |
| 137 | 120 | |
| 121 | + // rpc | |
| 138 | 122 | public function rpc() { |
| 139 | 123 | if (ob_get_length() > 0) { |
| 140 | 124 | while (@ob_end_clean()); |
| 141 | 125 | } |
| @@ -142,23 +126,22 @@ | ||
| 142 | 126 | $rpc = new Model_E2pdf_Rpc($this->helper->load('server')->get('REQUEST_URI')); |
| 143 | 127 | $controller = $this->page_to_controller('e2pdf-rpc-' . $rpc->get('version'), true); |
| 144 | 128 | if (class_exists($controller)) { |
| 145 | 129 | $this->controller = new $controller(); |
| 146 | - $method = $rpc->get('method'); | |
| 147 | - if ($method && method_exists($this->controller, $method)) { | |
| 130 | + $method = 'index_action'; | |
| 131 | + if ($rpc->get('method')) { | |
| 132 | + $method = $rpc->get('method') . '_action'; | |
| 133 | + } | |
| 134 | + if (method_exists($this->controller, $method)) { | |
| 148 | 135 | $this->controller->$method($rpc); |
| 149 | 136 | } |
| 150 | 137 | } |
| 151 | 138 | } |
| 152 | 139 | |
| 153 | - /** | |
| 154 | - * Get Tpl Value | |
| 155 | - * @param string $key - Tpl key | |
| 156 | - * @return mixed - Tpl value | |
| 157 | - */ | |
| 140 | + // page to controller | |
| 158 | 141 | public function page_to_controller($page, $frontend = false) { |
| 159 | 142 | $controller = array( |
| 160 | - 'Controller' | |
| 143 | + 'Controller', | |
| 161 | 144 | ); |
| 162 | 145 | if ($frontend) { |
| 163 | 146 | $controller[] = 'Frontend'; |
| 164 | 147 | } |
| @@ -166,12 +149,9 @@ | ||
| 166 | 149 | $controller = array_map('ucfirst', $controller); |
| 167 | 150 | return implode("_", $controller); |
| 168 | 151 | } |
| 169 | 152 | |
| 170 | - /** | |
| 171 | - * Redirect to page | |
| 172 | - * @param string $location - Full url where to redirect | |
| 173 | - */ | |
| 153 | + // redirect | |
| 174 | 154 | public function redirect($location) { |
| 175 | 155 | if (headers_sent()) { |
| 176 | 156 | die("<script>window.location='{$location}';</script>"); |
| 177 | 157 | } else { |
| @@ -179,30 +159,19 @@ | ||
| 179 | 159 | exit(); |
| 180 | 160 | } |
| 181 | 161 | } |
| 182 | 162 | |
| 183 | - /** | |
| 184 | - * Add notification | |
| 185 | - * @param string $type - Type of notification error|update | |
| 186 | - * @param string $text - Text of notification | |
| 187 | - */ | |
| 163 | + // add notification | |
| 188 | 164 | public function add_notification($type, $text) { |
| 189 | 165 | return $this->notification->add_notification($type, $text); |
| 190 | 166 | } |
| 191 | 167 | |
| 192 | - /** | |
| 193 | - * Get notifications | |
| 194 | - * @return array - List of notifications | |
| 195 | - */ | |
| 168 | + // get notifications | |
| 196 | 169 | public function get_notifications($notification_id = '') { |
| 197 | 170 | return $this->notification->get_notifications($notification_id); |
| 198 | 171 | } |
| 199 | 172 | |
| 200 | - /** | |
| 201 | - * Force Json response | |
| 202 | - * @param array $data - Array of data | |
| 203 | - * @return json | |
| 204 | - */ | |
| 173 | + // json response | |
| 205 | 174 | public function json_response($data = array()) { |
| 206 | 175 | @header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 207 | 176 | if (function_exists('wp_json_encode')) { |
| 208 | 177 | echo wp_json_encode($data, JSON_FORCE_OBJECT); |
| @@ -211,8 +180,9 @@ | ||
| 211 | 180 | } |
| 212 | 181 | wp_die(); |
| 213 | 182 | } |
| 214 | 183 | |
| 184 | + // json response ajax | |
| 215 | 185 | public function json_response_ajax($data = array(), $status = 200) { |
| 216 | 186 | if (ob_get_length() > 0) { |
| 217 | 187 | while (@ob_end_clean()); |
| 218 | 188 | } |
| @@ -226,11 +196,9 @@ | ||
| 226 | 196 | } |
| 227 | 197 | exit; |
| 228 | 198 | } |
| 229 | 199 | |
| 230 | - /** | |
| 231 | - * Force close browser tab from PHP | |
| 232 | - */ | |
| 200 | + // close tab response | |
| 233 | 201 | public function close_tab_response() { |
| 234 | 202 | echo " |
| 235 | 203 | <script> |
| 236 | 204 | self.close(); |
| @@ -237,16 +205,9 @@ | ||
| 237 | 205 | </script> |
| 238 | 206 | "; |
| 239 | 207 | } |
| 240 | 208 | |
| 241 | - /** | |
| 242 | - * Force Download response | |
| 243 | - * @param string $format - Format of file | |
| 244 | - * @param string $file - Base64 Encoded File | |
| 245 | - * @param string $name - Name of file when download | |
| 246 | - * @param string $disposition - Disposition of file inline|attachment | |
| 247 | - * @return string | |
| 248 | - */ | |
| 209 | + // download response | |
| 249 | 210 | public function download_response($format, $file, $name = '', $disposition = '', $fpassthru = false, $preview = false) { |
| 250 | 211 | |
| 251 | 212 | $content_length = true; |
| 252 | 213 | /** |
| @@ -422,15 +383,9 @@ | ||
| 422 | 383 | echo $file; |
| 423 | 384 | } |
| 424 | 385 | } |
| 425 | 386 | |
| 426 | - /** | |
| 427 | - * Check if exists in array | |
| 428 | - * @param string $value - Array Key | |
| 429 | - * @param mixed $compare | |
| 430 | - * @param array $data - Array of data | |
| 431 | - * @return bool | |
| 432 | - */ | |
| 387 | + // exist | |
| 433 | 388 | public function exist($value, $compare = false, $data = array()) { |
| 434 | 389 | if (isset($data[$value]) && $value === $compare) { |
| 435 | 390 | return true; |
| 436 | 391 | } else { |
| @@ -437,12 +392,9 @@ | ||
| 437 | 392 | return false; |
| 438 | 393 | } |
| 439 | 394 | } |
| 440 | 395 | |
| 441 | - /** | |
| 442 | - * Force Error | |
| 443 | - * @param string $code - Error Code | |
| 444 | - */ | |
| 396 | + // handle 404 | |
| 445 | 397 | public function handle_404() { |
| 446 | 398 | global $wp_query; |
| 447 | 399 | $wp_query->set_404(); |
| 448 | 400 | status_header(404); |
| @@ -451,22 +403,14 @@ | ||
| 451 | 403 | wp_die('404'); |
| 452 | 404 | } |
| 453 | 405 | } |
| 454 | 406 | |
| 455 | - /** | |
| 456 | - * Set var available to view template | |
| 457 | - * @param string $key - Key of value | |
| 458 | - * @param mixed $value - Value | |
| 459 | - */ | |
| 407 | + // view | |
| 460 | 408 | public function view($key, $value) { |
| 461 | 409 | $this->view->$key = $value; |
| 462 | 410 | } |
| 463 | 411 | |
| 464 | - /** | |
| 465 | - * Check if Array/Object Empty | |
| 466 | - * @param mixed $data - Object/Array to check | |
| 467 | - * @return bool | |
| 468 | - */ | |
| 412 | + // is empty | |
| 469 | 413 | public function is_empty($data) { |
| 470 | 414 | if (is_object($data) || is_array($data)) { |
| 471 | 415 | |
| 472 | 416 | if (is_object($data)) { |
| @@ -479,8 +423,9 @@ | ||
| 479 | 423 | |
| 480 | 424 | return true; |
| 481 | 425 | } |
| 482 | 426 | |
| 427 | + // message | |
| 483 | 428 | public function message($key) { |
| 484 | 429 | $message = ''; |
| 485 | 430 | switch ($key) { |
| 486 | 431 | case 'wp_verify_nonce_error': |