| 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 WCPOS\WooCommercePOS\Services\Permission_Rules; |
| 11 |
use WP_REST_Request; |
| 12 |
|
| 13 |
/** |
| 14 |
* Gives every resource the same install/run/finally/unwind discipline. |
| 15 |
*/ |
| 16 |
abstract class Scoped_Proxy_Behavior implements Proxy_Behavior { |
| 17 |
/** Permission collection, when this resource has a POS-tier read grant. |
| 18 |
* |
| 19 |
* @var string|null |
| 20 |
*/ |
| 21 |
protected $permission_collection; |
| 22 |
|
| 23 |
/** |
| 24 |
* Forward every param untouched. |
| 25 |
* |
| 26 |
* @param array $params Query parameters to forward. |
| 27 |
* @param WP_REST_Request $request Original proxy request. |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public function forwarded_params( array $params, WP_REST_Request $request ): array { |
| 32 |
return $params; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Install this resource's hooks, run the forward, and always unwind. |
| 37 |
* |
| 38 |
* @param callable $forward Forward operation. |
| 39 |
* |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
public function around( callable $forward ) { |
| 43 |
$bindings = $this->install(); |
| 44 |
if ( $this->permission_collection ) { |
| 45 |
Permission_Rules::install_wc_filter( $this->permission_collection ); |
| 46 |
} |
| 47 |
|
| 48 |
try { |
| 49 |
return $this->run( $forward ); |
| 50 |
} finally { |
| 51 |
if ( $this->permission_collection ) { |
| 52 |
Permission_Rules::uninstall_wc_filter(); |
| 53 |
} |
| 54 |
foreach ( array_reverse( $bindings ) as $binding ) { |
| 55 |
remove_filter( $binding[0], $binding[1], $binding[2] ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Return the response data unshaped. |
| 62 |
* |
| 63 |
* @param mixed $data Response data. |
| 64 |
* |
| 65 |
* @return mixed |
| 66 |
*/ |
| 67 |
public function post_process( $data ) { |
| 68 |
return $data; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Install hooks and return their exact removal tuples. |
| 73 |
* |
| 74 |
* @return array<int, array{0: string, 1: callable, 2: int}> |
| 75 |
*/ |
| 76 |
protected function install(): array { |
| 77 |
return array(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Run the forward, optionally through a resource plan. |
| 82 |
* |
| 83 |
* @param callable $forward Forward operation. |
| 84 |
* |
| 85 |
* @return mixed |
| 86 |
*/ |
| 87 |
protected function run( callable $forward ) { |
| 88 |
return $forward(); |
| 89 |
} |
| 90 |
} |
| 91 |
|