<?php class Resolvable implements ArrayAccess, IteratorAggregate, Countable { protected $callable; protected $resolved = null; public function __construct( callable $closure ) { $this->callable = $closure; } public function __invoke( ...$args ) { if ( ! $this->resolved ) { $this->resolved = call_user_func_array( $this->callable, $args ); // Any normalisation can be done here. } return $this->resolved; } public function offsetExists( mixed $k ) : bool { $this->__invoke(); return isset( $this->resolved[ $k ] ); } public function &offsetGet( mixed $k ) : mixed { $this->__invoke(); return $this->resolved[ $k ]; } public function offsetSet( mixed $k, mixed $v ) : void { $this->__invoke(); $this->resolved[ $k ] = $v; } public function offsetUnset( mixed $k ) : void { $this->__invoke(); unset( $this->resolved[ $k ] ); } public function getIterator(): Traversable { $this->__invoke(); return new ArrayIterator( $this->resolved ); } public function count() : int { $this->__invoke(); return count( $this->resolved ); } } // $foo = function () { return 1; }; $endpoints = [ '/wp/v2/users' => fn () => [ [ 'method' => 'GET', 'callback' => fn () => true, 'args' => [ 'id' => [], ], ], [ 'method' => 'POST', 'callback' => fn () => true, 'args' => [ 'id' => [], ], ], ], ]; foreach ( $endpoints as $k => &$ep ) { if ( is_callable( $ep ) ) { $ep = new Resolvable( $ep ); } } // Old-style plugin, eg Really Simple SSL: if ( isset( $endpoints['/wp/v2/users'] ) ) { // Save the original endpoint $original_endpoint = $endpoints['/wp/v2/users']; // Override the GET callback $endpoints['/wp/v2/users'][0]['callback'] = function() { return 'Sorry, you are not allowed to access users without authentication.'; }; // Preserve the original args and permission callback $endpoints['/wp/v2/users'][0]['args'] = $original_endpoint[0]['args']; $endpoints['/wp/v2/users'][0]['permission_callback'] = '__return_true'; } var_dump( $endpoints ); var_dump( $endpoints['/wp/v2/users'][0]['callback']() );
You have javascript disabled. You will not be able to edit any code.