|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustCoded\WP\Framework\Web; |
| 4 | + |
| 5 | +use JustCoded\WP\Framework\Objects\Singleton; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class RestController |
| 9 | + * |
| 10 | + * @package JustCoded\WP\Framework\Objects |
| 11 | + * |
| 12 | + * @method Rest_Controller instance() static |
| 13 | + */ |
| 14 | +abstract class Rest_Controller extends \WP_REST_Controller { |
| 15 | + use Singleton; |
| 16 | + |
| 17 | + /** |
| 18 | + * Basic namespace. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $namespace = 'ext/v2'; |
| 23 | + |
| 24 | + /** |
| 25 | + * Resource name. |
| 26 | + * Should be set inside child class. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $resource_name; |
| 31 | + |
| 32 | + /** |
| 33 | + * Custom routes. |
| 34 | + * |
| 35 | + * @var array |
| 36 | + */ |
| 37 | + protected $routes = []; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor |
| 41 | + * Check required parameter ROUTE and add WP action hook |
| 42 | + * |
| 43 | + * @throws \Exception Missing $resource_name property. |
| 44 | + */ |
| 45 | + protected function __construct() { |
| 46 | + if ( empty( $this->resource_name ) ) { |
| 47 | + throw new \Exception( static::class . '" init failed: missing resource_name property' ); |
| 48 | + } |
| 49 | + |
| 50 | + $this->init(); |
| 51 | + |
| 52 | + add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Add Route function. |
| 57 | + * |
| 58 | + * @param string $alias Name of a key in route array. |
| 59 | + * @param string $method HTTP method. |
| 60 | + * @param string $route Name of route. |
| 61 | + * @param array $callback Callback function. |
| 62 | + * @param array $args Args array. |
| 63 | + * @param array $permission_callback Permission callback function. |
| 64 | + */ |
| 65 | + protected function add_route( $alias, $method, $route, $callback, $args = [], $permission_callback = [] ) { |
| 66 | + $this->routes[ $alias ] = [ |
| 67 | + 'route' => $route, |
| 68 | + 'args' => [ |
| 69 | + [ |
| 70 | + 'methods' => $method, |
| 71 | + 'callback' => $callback, |
| 72 | + 'permission_callback' => $permission_callback, |
| 73 | + 'args' => $args, |
| 74 | + ], |
| 75 | + 'schema' => [ $this, 'get_public_item_schema' ], |
| 76 | + ], |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Register_endpoint |
| 82 | + */ |
| 83 | + public function register_routes() { |
| 84 | + foreach ( $this->routes as $route ) { |
| 85 | + register_rest_route( |
| 86 | + $this->namespace, |
| 87 | + $this->resource_name . $route['route'], |
| 88 | + $route['args'] |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Response |
| 95 | + * |
| 96 | + * @param mixed $data Data. |
| 97 | + * @param int $status HTTP status. |
| 98 | + * @param array $headers HTTP headers. |
| 99 | + * |
| 100 | + * @return \WP_REST_Response |
| 101 | + */ |
| 102 | + protected function response( $data, $status = 200, $headers = [] ) { |
| 103 | + return new \WP_REST_Response( $data, $status, $headers ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Update_acf_value |
| 108 | + * |
| 109 | + * @param int $post_id Post ID. |
| 110 | + * @param array $args Arguments. |
| 111 | + * |
| 112 | + * @return \WP_Error|bool |
| 113 | + */ |
| 114 | + protected function update_acf_fields( $post_id, $args ) { |
| 115 | + if ( empty( $args ) ) { |
| 116 | + return new \WP_Error( 'rest_employee_empty_args', __( 'Empty custom args.' ), array( 'status' => 500 ) ); |
| 117 | + } |
| 118 | + |
| 119 | + foreach ( $args as $selector => $value ) { |
| 120 | + update_field( $selector, $value, $post_id ); |
| 121 | + } |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Declaration of the 'init' action hook |
| 128 | + * should call $this->add_route( $method, $route, $alias, $callback, $args, $permission_callback ) inside |
| 129 | + * |
| 130 | + * @see Rest_Controller::add_route() Add route function. |
| 131 | + * |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + abstract public function init(); |
| 135 | + |
| 136 | + /** |
| 137 | + * Get permalink |
| 138 | + * |
| 139 | + * @param string $alias . |
| 140 | + * @param array $args . |
| 141 | + * |
| 142 | + * @return string |
| 143 | + */ |
| 144 | + public static function get_permalink( $alias, $args = array() ) { |
| 145 | + if ( is_array( $alias ) ) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + $instance = static::instance(); |
| 150 | + |
| 151 | + $data = []; |
| 152 | + $route = $instance->routes[ $alias ]['route']; |
| 153 | + $slug_convert = preg_replace( '/(\(\?\w+<(\w+)>(.*?)\))/i', '{$2}', $route ); |
| 154 | + $rest_url = rest_url( $instance->namespace . '/' . $instance->resource_name ); |
| 155 | + |
| 156 | + foreach ( $args as $key => $value ) { |
| 157 | + if ( false === strpos( $slug_convert, $key ) ) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + $data[ '{' . $key . '}' ] = $value; |
| 161 | + unset( $args[ $key ] ); |
| 162 | + } |
| 163 | + |
| 164 | + $route = strtr( $slug_convert, $data ); |
| 165 | + $rest_url = add_query_arg( [ $args ], $rest_url . $route ); |
| 166 | + |
| 167 | + return $rest_url; |
| 168 | + } |
| 169 | +} |
0 commit comments