Skip to content

Commit 60a9907

Browse files
authored
Merge pull request #40 from justcoded/feature/rest-integration
add wp rest api integration
2 parents 056fa20 + 38a9241 commit 60a9907

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

framework/Theme.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ protected function __construct() {
130130
$this->register_post_types();
131131
$this->register_taxonomies();
132132
$this->init_views_templates();
133+
$this->register_api_endpoints();
133134

134135
/**
135136
* Pretty standard theme hooks
@@ -404,6 +405,17 @@ public function register_post_types() {
404405
public function register_taxonomies() {
405406
}
406407

408+
/**
409+
* Register API Endpoints
410+
* Usage:
411+
* new \namespace\App\Endpoints\MyEndpoint();
412+
*
413+
* Each endpoint register it's own action hook
414+
*/
415+
public function register_api_endpoints() {
416+
417+
}
418+
407419
/**
408420
* Adds loading of custom features provided by 3d-party plugins.
409421
*/

framework/Web/Rest_Controller.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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

Comments
 (0)