| 1 |
<?php |
| 2 |
/** |
| 3 |
* Scoped catalog proxy behavior base. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\API\V2\Proxy |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\V2\Proxy; |
| 9 |
|
| 10 |
use WP_REST_Request; |
| 11 |
|
| 12 |
/** |
| 13 |
* Gives every resource the same install/run/finally/unwind discipline. |
| 14 |
*/ |
| 15 |
abstract class Scoped_Proxy_Behavior implements Proxy_Behavior { |
| 16 |
/** |
| 17 |
* Forward every param untouched. |
| 18 |
* |
| 19 |
* @param array $params Query parameters to forward. |
| 20 |
* @param WP_REST_Request $request Original proxy request. |
| 21 |
* |
| 22 |
* @return array |
| 23 |
*/ |
| 24 |
public function forwarded_params( array $params, WP_REST_Request $request ): array { |
| 25 |
return $params; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Install this resource's hooks, run the forward, and always unwind. |
| 30 |
* |
| 31 |
* @param callable $forward Forward operation. |
| 32 |
* |
| 33 |
* @return mixed |
| 34 |
*/ |
| 35 |
public function around( callable $forward ) { |
| 36 |
$bindings = $this->install(); |
| 37 |
|
| 38 |
try { |
| 39 |
return $this->run( $forward ); |
| 40 |
} finally { |
| 41 |
foreach ( array_reverse( $bindings ) as $binding ) { |
| 42 |
remove_filter( $binding[0], $binding[1], $binding[2] ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Return the response data unshaped. |
| 49 |
* |
| 50 |
* @param mixed $data Response data. |
| 51 |
* |
| 52 |
* @return mixed |
| 53 |
*/ |
| 54 |
public function post_process( $data ) { |
| 55 |
return $data; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Install hooks and return their exact removal tuples. |
| 60 |
* |
| 61 |
* @return array<int, array{0: string, 1: callable, 2: int}> |
| 62 |
*/ |
| 63 |
protected function install(): array { |
| 64 |
return array(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Run the forward, optionally through a resource plan. |
| 69 |
* |
| 70 |
* @param callable $forward Forward operation. |
| 71 |
* |
| 72 |
* @return mixed |
| 73 |
*/ |
| 74 |
protected function run( callable $forward ) { |
| 75 |
return $forward(); |
| 76 |
} |
| 77 |
} |
| 78 |
|