| 1 |
<?php |
| 2 |
namespace SureCartBlocks\Controllers; |
| 3 |
|
| 4 |
use SureCart\Models\User; |
| 5 |
|
| 6 |
/** |
| 7 |
* Base controller for dashboard pages. |
| 8 |
*/ |
| 9 |
abstract class BaseController { |
| 10 |
/** |
| 11 |
* The middleware for this controller. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $middleware = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Block controller middleware. |
| 19 |
* |
| 20 |
* @param string[] $middleware Middleware. |
| 21 |
* @param string $action Action. |
| 22 |
* @param Closure $next Next. |
| 23 |
* |
| 24 |
* @return function |
| 25 |
*/ |
| 26 |
public function executeMiddleware( $middleware, $action = null, $next = null ) { |
| 27 |
$next = null === $next ? $action : $next; |
| 28 |
|
| 29 |
if ( empty( $this->middleware[ $action ] ) && null === $middleware ) { |
| 30 |
return $this->$next(); |
| 31 |
} |
| 32 |
|
| 33 |
$middleware = null === $middleware ? $this->middleware[ $action ] : $middleware; |
| 34 |
|
| 35 |
$top_middleware = array_shift( $middleware ); |
| 36 |
|
| 37 |
if ( null === $top_middleware ) { |
| 38 |
return $this->$next(); |
| 39 |
} |
| 40 |
|
| 41 |
$top_middleware_next = function () use ( $middleware, $action, $next ) { |
| 42 |
return $this->executeMiddleware( $middleware, $action, $next ); |
| 43 |
}; |
| 44 |
|
| 45 |
$instance = new $top_middleware(); |
| 46 |
|
| 47 |
return call_user_func_array( array( $instance, 'handle' ), array( $action, $top_middleware_next ) ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Handle the request. |
| 52 |
* |
| 53 |
* @param string $action The action. |
| 54 |
* |
| 55 |
* @return $this |
| 56 |
*/ |
| 57 |
public function handle( $action ) { |
| 58 |
return $this->executeMiddleware( null, $action ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get a query param. |
| 63 |
* |
| 64 |
* @param string $name The query param name. |
| 65 |
* @param mixed $fallback The fallback value. |
| 66 |
* |
| 67 |
* @return string|false |
| 68 |
*/ |
| 69 |
protected function getParam( $name, $fallback = false ) { |
| 70 |
return isset( $_GET[ $name ] ) ? sanitize_text_field( wp_unslash( $_GET[ $name ] ) ) : $fallback; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the current tab. |
| 75 |
* |
| 76 |
* @return string|false |
| 77 |
*/ |
| 78 |
protected function getTab() { |
| 79 |
return $this->getParam( 'tab' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get the current page. |
| 84 |
* |
| 85 |
* @return integer |
| 86 |
*/ |
| 87 |
protected function getPage() { |
| 88 |
return $this->getParam( 'page', 1 ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the current id. |
| 93 |
* |
| 94 |
* @return integer|false |
| 95 |
*/ |
| 96 |
protected function getId() { |
| 97 |
return $this->getParam( 'id' ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the users customer ids. |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
protected function customerIds() { |
| 106 |
return array_values( (array) User::current()->customerIds() ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Render not found view. |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
protected function notFound() { |
| 115 |
return '<sc-alert type="danger" open>' . esc_html__( 'Not found.', 'surecart' ) . '</sc-alert>'; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Render not found view. |
| 120 |
* |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
protected function noAccess() { |
| 124 |
return '<sc-alert type="danger" open>' . esc_html__( 'You do not have permission to do this.', 'surecart' ) . '</sc-alert>'; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Check if this is in live mode. |
| 129 |
* |
| 130 |
* @return boolean |
| 131 |
*/ |
| 132 |
protected function isLiveMode() { |
| 133 |
if ( 'false' === sanitize_text_field( wp_unslash( $_GET['live_mode'] ?? '' ) ) ) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
return true; |
| 137 |
} |
| 138 |
} |
| 139 |
|