| 1 |
<?php |
| 2 |
|
| 3 |
/* * class |
| 4 |
* Description of AbstractController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @position: 1 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
|
| 13 |
abstract class AbstractController |
| 14 |
{ |
| 15 |
|
| 16 |
public const NONCE = 'ali2woo_nonce'; |
| 17 |
public const AJAX_NONCE_ACTION = 'ajax_nonce'; |
| 18 |
public const PAGE_NONCE_ACTION = 'page_nonce'; |
| 19 |
private array $model = []; |
| 20 |
private array $views = []; |
| 21 |
private string $views_path; |
| 22 |
|
| 23 |
public function __construct($views_path='') { |
| 24 |
if(!$views_path){ |
| 25 |
$views_path = A2WL()->plugin_path() . '/view/'; |
| 26 |
} |
| 27 |
|
| 28 |
$this->views_path = $views_path; |
| 29 |
} |
| 30 |
|
| 31 |
protected function set_views_path($views_path) { |
| 32 |
$this->views_path = $views_path; |
| 33 |
if (substr($this->views_path, -1) !== '/') { |
| 34 |
$this->views_path = $this->views_path . '/'; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
protected function model_put($name, $value) { |
| 39 |
$this->model[$name] = $value; |
| 40 |
} |
| 41 |
|
| 42 |
protected function include_view($view, $action_hook = false) { |
| 43 |
$this->views = is_array($view) ? $view : array($view); |
| 44 |
|
| 45 |
if ($action_hook) { |
| 46 |
add_action($action_hook, array($this, 'show_view')); |
| 47 |
} else { |
| 48 |
$this->show_view(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function show_view() { |
| 53 |
extract($this->model); |
| 54 |
|
| 55 |
foreach ($this->views as $v) { |
| 56 |
$view_action = str_replace(".php", "", str_replace(array('\\', '/'), '_', $v)); |
| 57 |
|
| 58 |
do_action('a2wl_before_'.$view_action); |
| 59 |
|
| 60 |
$theme_view_path = get_template_directory() . sprintf('/%s/', A2WL()->plugin_slug); |
| 61 |
|
| 62 |
if (file_exists( $theme_view_path . $v) && is_file($theme_view_path . $v)){ |
| 63 |
include($theme_view_path . $v); |
| 64 |
} else if (file_exists($this->views_path . $v) && is_file($this->views_path . $v)) { |
| 65 |
include($this->views_path . $v ); |
| 66 |
} |
| 67 |
|
| 68 |
do_action('a2wl_after_'.$view_action); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
protected function getErrorTextNoPermissions(): string |
| 73 |
{ |
| 74 |
return esc_html__('You do not have sufficient permissions to access this page.', 'ali2woo'); |
| 75 |
} |
| 76 |
|
| 77 |
public function verifyNonceAjax(): void |
| 78 |
{ |
| 79 |
$nonce = $_REQUEST[self::NONCE] ?? null; |
| 80 |
if (!wp_verify_nonce($nonce, self::AJAX_NONCE_ACTION)) { |
| 81 |
$message = esc_html__('Security check. Please refresh the page and try again.', 'ali2woo'); |
| 82 |
$result = ResultBuilder::buildError($message); |
| 83 |
|
| 84 |
echo wp_json_encode($result); |
| 85 |
wp_die(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|