1. Go to this page and download the library: Download codezone/router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace CodeZone\Router\Middleware;
use CodeZone\Router\Illuminate\Http\Request;
use WP_HTTP_Response;
class LoggedIn implements Middleware {
public function handle( Request $request, WP_HTTP_Response $response, $next ) {
if ( ! is_user_logged_in() ) {
$response->set_status( 302 );
$response->set_data( wp_login_url( $request->getUri() ) );
}
return $next( $request, $response );
}
}
use CodeZone\Router\Middleware\DispatchController;
use CodeZone\Router\Middleware\HandleErrors;
use CodeZone\Router\Middleware\HandleRedirects;
use CodeZone\Router\Middleware\Render;
use CodeZone\Router\Middleware\Route;
use CodeZone\Router\Middleware\SetHeaders;
use CodeZone\Router\Middleware\Stack;
$middleware = [
Route::class,
DispatchController::class,
SetHeaders::class,
HandleRedirects::class,
HandleErrors::class,
Render::class,
];
$stack = container()->makeWith(Stack::class, $middleware);
$stack->run();
add_filter(Router::class . '\\' . 'conditions_factory', function(Condition|null $instanceOrNull, array $attributes = []) {
//Check if this is our named condition
if ($name !== 'can') {
return $instanceOrNull;
}
$className = $attributes['className'] ?? null;
$name = $attributes['name'] ?? null;
$signature = $attributes['signature'] ?? null;
//The signature is the part of the route name after the ":". We need to break it into an array.
$params = explode(',', $signature);
return container->makeWith(HasCap::class, ['params' => $params]);
});
add_filter(Router::class . '\\' . 'conditions_factory', function(Condition|null $instanceOrNull, array $attributes = []) {
//Check if this is our named condition
if ($name !== 'can') {
return $instanceOrNull;
}
$className = $attributes['className'] ?? null;
$name = $attributes['name'] ?? null;
$signature = $attributes['signature'] ?? null;
//The signature is the part of the route name after the ":". We need to break it into an array.
$params = explode(',', $signature);
return container->makeWith(HasCap::class, ['params' => $params]);
});
add_filter(Router::class . '\\' . 'middleware_factory', function(Middleware|null $instanceOrNull, array $attributes = []) {
//Check if this is our named middleware
if ($name !== 'can') {
return $instanceOrNull;
}
$className = $attributes['className'] ?? null;
$name = $attributes['name'] ?? null;
$signature = $attributes['signature'] ?? null;
//The signature is the part of the route name after the ":". We need to break it into an array.
$params = explode(',', $signature);
return container->makeWith(UserHasCap::class, ['params' => $params]);
});