Skip to content

Commit

Permalink
Merge branch 'master' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Apr 16, 2024
2 parents dfa35f3 + 370ff21 commit a2ddbd8
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 120 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: CI
on:
push:
branches:
- master
- alpha
- beta

Expand Down
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "x-wp/ajax-router",
"description": "Simplifies the use of WordPress hooks by allowing you to use simple dependency injection.",
"description": "Simplifies the use of WordPress AJAX by providing NestJS inspired Controller design.",
"authors": [
{
"name": "Sibin Grasic",
Expand All @@ -12,10 +12,10 @@
"require": {
"php": ">= 8.1",
"automattic/jetpack-constants": "^2.0",
"oblak/wp-hook-di": "^1.2",
"oblak/wp-polyfills": "^1.0",
"sunrise/http-message": "^3",
"sunrise/http-router": "^2.16"
"sunrise/http-router": "^2.16",
"x-wp/hook-invoker": "^1.0@alpha"
},
"require-dev": {
"oblak/wordpress-coding-standard": "^1"
Expand All @@ -31,6 +31,8 @@
"src/Utils/xwp-ajax-fns.php"
]
},
"minimum-stability": "alpha",
"prefer-stable": true,
"config": {
"classmap-authoritative": true,
"optimize-autoloader": true,
Expand Down
175 changes: 118 additions & 57 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 13 additions & 28 deletions src/Ajax_Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

namespace XWP;

use Oblak\WP\Decorators\Action;
use Oblak\WP\Decorators\Filter;
use Oblak\WP\Traits\Accessible_Hook_Methods;
use Oblak\WP\Traits\Hook_Processor_Trait;
use Oblak\WP\Traits\Singleton;
use XWP\Contracts\Hook\Accessible_Hook_Methods;
use XWP\Hook\Decorators\Action;
use XWP\Hook\Decorators\Filter;
use XWP\Hook\Invoker;

/**
* Ajax server.
*/
class Ajax_Server {
use Singleton;
use Accessible_Hook_Methods;
use Hook_Processor_Trait;

/**
* The base for the ajax requests.
Expand All @@ -33,29 +32,15 @@ class Ajax_Server {
*/
protected function __construct() {
$this->base = \get_option( 'xwp_ajax_slug', 'wp-ajax' );
$this->init( 'plugins_loaded', PHP_INT_MAX );
}

/**
* Runs the registered hooks for the plugin.
*/
public function run_hooks() {
\xwp_invoke_hooked_methods( $this );
}

/**
* Get the dependencies for the module.
*
* @return array
*/
protected function get_dependencies(): array {
return array( HTTP\Request_Dispatcher::class );
Invoker::instance()
->load_handler( $this )
->register_handler( HTTP\Request_Dispatcher::class );
}

/**
* Add the admin settings for the module.
*/
#[Action( 'admin_init' )]
#[Action( tag:'admin_init', context: Action::CTX_ADMIN )]
protected function add_admin_settings() {
\add_settings_field( 'xwp_ajax_slug', 'XWP AJAX base', $this->field( ... ), 'permalink', 'optional' );
}
Expand All @@ -78,7 +63,7 @@ class="regular-text code"
/**
* Save the admin settings for the module.
*/
#[Action( 'admin_init' )]
#[Action( tag: 'init', context: Action::CTX_ADMIN )]
protected function save_admin_settings() {
if ( ! isset( $_POST['permalink_structure'], $_POST['xwp_ajax_slug'] ) || ! \is_admin() ) {
return;
Expand All @@ -96,7 +81,7 @@ protected function save_admin_settings() {
* @param array $vars The existing query vars.
* @return array
*/
#[Filter( 'query_vars' )]
#[Filter( tag: 'query_vars' )]
protected function add_query_vars( array $vars ): array {
return \array_merge( $vars, array( 'xwp_ajax' ) );
}
Expand All @@ -107,7 +92,7 @@ protected function add_query_vars( array $vars ): array {
* @param array $rules The existing rewrite rules.
* @return array The modified rewrite rules.
*/
#[Filter( 'rewrite_rules_array', 120 )]
#[Filter( tag: 'rewrite_rules_array', priority: 120 )]
protected function add_rewrites( array $rules ) {
$addon_rules = array(
$this->base . '/([^/]+)/([^/]+)/?(.*)?' => 'index.php?xwp_ajax=1',
Expand All @@ -121,8 +106,8 @@ protected function add_rewrites( array $rules ) {
/**
* Add the ajax vars to the head.
*/
#[Action( 'wp_head', PHP_INT_MAX )]
#[Action( 'admin_head', PHP_INT_MAX )]
#[Action( tag: 'wp_head', priority: PHP_INT_MAX, context: Action::CTX_FRONTEND )]
#[Action( tag: 'admin_head', priority: PHP_INT_MAX, context: Action::CTX_ADMIN )]
protected function add_ajax_vars() {
$add_vars = \apply_filters( 'xwp_ajax_vars', true );
if ( ! \has_filter( 'xwp_ajax_controllers' ) || ! $add_vars ) {
Expand Down
11 changes: 6 additions & 5 deletions src/Decorator/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sunrise\Http\Router\RouteCollector;
use XWP\Decorator\HTTP\Field;
use XWP\Decorator\HTTP\Method;
use XWP\Hook\Reflection;
use XWP\HTTP\Response_Handler;
use XWP\Interfaces\Response_Handler as RHI;
use XWP\Interfaces\Response_Modifier;
Expand Down Expand Up @@ -103,7 +104,7 @@ protected function load_middlewares( ?\Reflector $r = null ): array {

$mw = \array_map(
static fn( $dt ) => new $dt(
...\xwp_get_hook_decorators(
...Reflection::get_decorators(
$r ?? $this->reflector,
$dt::DECORATOR_CLASS,
) ?: array(),
Expand All @@ -121,18 +122,18 @@ protected function load_middlewares( ?\Reflector $r = null ): array {
*/
public function get_routes(): \Generator {
foreach ( $this->reflector->getMethods() as $method ) {
$req_method = \current( \xwp_get_hook_decorators( $method, Method::class ) );

$req_method = Reflection::get_decorator( $method, Method::class );
$path = '' === $req_method->path ? '' : "/{$req_method->path}";
yield array(
'attributes' => array(
'@Method' => $method,
'@Type' => $req_method->type ?? false,
),
'methods' => $req_method->method->format(),
'requestHandler' => $this->handle( ... ),
'middlewares' => $this->get_middlewares( $method ),
'middlewares' => $this->load_middlewares( $method ),
'name' => "{$this->reflector->getName()}::{$method->getName()}",
'path' => "/{$req_method->path}",
'path' => $path,
);
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Decorator/HTTP/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace XWP\Decorator\HTTP;

use XWP\Enums\Request_Method;
use XWP\Interfaces\Response_Type;

#[\Attribute( \Attribute::TARGET_METHOD )]
class Delete extends Method {
public function __construct(
string $path,
string|Response_Type|null $type = null,
) {
parent::__construct( method: Request_Method::DELETE, path: $path, type: $type );
}
}
Loading

0 comments on commit a2ddbd8

Please sign in to comment.