| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Controllers\Admin; |
| 4 |
|
| 5 |
use SureCartCore\Responses\RedirectResponse; |
| 6 |
|
| 7 |
abstract class AdminController { |
| 8 |
/** |
| 9 |
* Preload API Request Paths |
| 10 |
* |
| 11 |
* @param array $preload_paths The preload paths. |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public function preloadPaths( $preload_paths ) { |
| 16 |
wp_add_inline_script( |
| 17 |
'wp-api-fetch', |
| 18 |
sprintf( |
| 19 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 20 |
wp_json_encode( |
| 21 |
array_reduce( |
| 22 |
$preload_paths, |
| 23 |
'rest_preload_api_request', |
| 24 |
array() |
| 25 |
) |
| 26 |
) |
| 27 |
), |
| 28 |
'after' |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* The header. |
| 34 |
* |
| 35 |
* @param array $args The arguments. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function withHeader( $args ) { |
| 40 |
add_action( |
| 41 |
'in_admin_header', |
| 42 |
function () use ( $args ) { |
| 43 |
return \SureCart::render( |
| 44 |
'layouts/partials/admin-header', |
| 45 |
[ |
| 46 |
'breadcrumbs' => $args['breadcrumbs'] ?? [], |
| 47 |
'suffix' => $args['suffix'] ?? '', |
| 48 |
'claim_url' => ! \SureCart::account()->claimed ? \SureCart::routeUrl( 'account.claim' ) : '', |
| 49 |
'claim_expired' => \SureCart::account()->claim_expired ?? false, |
| 50 |
'report_url' => $args['report_url'] ?? '', |
| 51 |
] |
| 52 |
); |
| 53 |
} |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add notices. |
| 59 |
* |
| 60 |
* @param array $items The notices. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function withNotices( $items ) { |
| 65 |
add_action( |
| 66 |
'admin_notices', |
| 67 |
function () use ( $items ) { |
| 68 |
foreach ( $items as $key => $item ) { |
| 69 |
if ( (bool) ( $_REQUEST[ $key ] ?? false ) ) { |
| 70 |
?> |
| 71 |
<div class="notice notice-success is-dismissible"> |
| 72 |
<p><?php echo esc_html( $item ); ?></p> |
| 73 |
</div> |
| 74 |
<?php |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Redirect back to the previous page. |
| 83 |
* |
| 84 |
* @param @param \SureCartCore\Requests\RequestInterface $request Request. |
| 85 |
* |
| 86 |
* @return \SureCartCore\Responses\RedirectResponse |
| 87 |
*/ |
| 88 |
public function redirectBack( $request ) { |
| 89 |
return ( new RedirectResponse( $request ) )->back(); |
| 90 |
} |
| 91 |
} |
| 92 |
|