ACF to REST API - Version 2.2.0

Version Description

get specific field via endpoints

Download this release

Release Info

Developer airesvsg
Plugin Icon 128x128 ACF to REST API
Version 2.2.0
Comparing to
See all releases

Code changes from version 2.1.1 to 2.2.0

class-acf-to-rest-api.php CHANGED
@@ -1,10 +1,10 @@
1
  <?php
2
  /**
3
  * Plugin Name: ACF to REST API
4
- * Description: Edit, Get and Puts ACF fields in WordPress REST API.
5
  * Author: Aires Gonçalves
6
  * Author URI: http://github.com/airesvsg
7
- * Version: 2.1.1
8
  * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
  */
10
 
@@ -16,7 +16,7 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
16
 
17
  class ACF_To_REST_API {
18
 
19
- const VERSION = '2.1.1';
20
 
21
  public static function init() {
22
  self::includes();
1
  <?php
2
  /**
3
  * Plugin Name: ACF to REST API
4
+ * Description: Exposes Advanced Custom Fields Endpoints in the WP REST API v2 ( WP-API | WordPress REST API )
5
  * Author: Aires Gonçalves
6
  * Author URI: http://github.com/airesvsg
7
+ * Version: 2.2.0
8
  * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
  */
10
 
16
 
17
  class ACF_To_REST_API {
18
 
19
+ const VERSION = '2.2.0';
20
 
21
  public static function init() {
22
  self::includes();
lib/endpoints/class-acf-to-rest-api-controller.php CHANGED
@@ -25,7 +25,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
25
  }
26
 
27
  public function register_routes() {
28
- register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
29
  array(
30
  'methods' => WP_REST_Server::READABLE,
31
  'callback' => array( $this, 'get_item' ),
@@ -105,14 +105,19 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
105
  $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type );
106
 
107
  if ( is_string( $key ) && ! empty( $key ) ) {
108
- $data = $request->get_param( $key );
109
-
 
110
  $this->format_id( $request );
111
 
112
  if ( $this->id && is_array( $data ) ) {
113
  $fields = $this->get_field_objects( $this->id );
114
 
115
  if ( is_array( $fields ) && ! empty( $fields ) ) {
 
 
 
 
116
  $item = array(
117
  'id' => $this->id,
118
  'fields' => $fields,
@@ -182,9 +187,14 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
182
  }
183
 
184
  protected function get_fields( $request, $response = null, $object = null ) {
185
- $data = array();
186
- $swap = $response instanceof WP_REST_Response;
 
187
 
 
 
 
 
188
  if ( $swap ) {
189
  $data = $response->get_data();
190
  }
@@ -200,7 +210,11 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
200
  $this->format_id( $object );
201
 
202
  if ( $this->id ) {
203
- $data['acf'] = get_fields( $this->id );
 
 
 
 
204
  } else {
205
  $data['acf'] = array();
206
  }
@@ -221,8 +235,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
221
  $fields = array();
222
  $fields_tmp = array();
223
 
224
- if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
225
-
226
  $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
227
 
228
  if ( is_array( $field_groups ) && ! empty( $field_groups ) ) {
@@ -235,9 +248,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
235
  }
236
  }
237
  }
238
-
239
  } else {
240
-
241
  if ( strpos( $id, 'user_' ) !== false ) {
242
  $filter = array( 'ef_user' => str_replace( 'user_', '', $id ) );
243
  } elseif ( strpos( $id, 'taxonomy_' ) !== false ) {
@@ -256,7 +267,6 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
256
  }
257
  }
258
  }
259
-
260
  }
261
 
262
  if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) {
25
  }
26
 
27
  public function register_routes() {
28
+ register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/?(?P<field>[\w\-\_]+)?', array(
29
  array(
30
  'methods' => WP_REST_Server::READABLE,
31
  'callback' => array( $this, 'get_item' ),
105
  $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type );
106
 
107
  if ( is_string( $key ) && ! empty( $key ) ) {
108
+ $data = $request->get_param( $key );
109
+ $field = $request->get_param( 'field' );
110
+
111
  $this->format_id( $request );
112
 
113
  if ( $this->id && is_array( $data ) ) {
114
  $fields = $this->get_field_objects( $this->id );
115
 
116
  if ( is_array( $fields ) && ! empty( $fields ) ) {
117
+ if ( $field && isset( $data[$field] ) ) {
118
+ $data = array( $field => $data[$field] );
119
+ }
120
+
121
  $item = array(
122
  'id' => $this->id,
123
  'fields' => $fields,
187
  }
188
 
189
  protected function get_fields( $request, $response = null, $object = null ) {
190
+ $data = array();
191
+ $field = null;
192
+ $swap = $response instanceof WP_REST_Response;
193
 
194
+ if ( $request instanceof WP_REST_Request ) {
195
+ $field = $request->get_param( 'field' );
196
+ }
197
+
198
  if ( $swap ) {
199
  $data = $response->get_data();
200
  }
210
  $this->format_id( $object );
211
 
212
  if ( $this->id ) {
213
+ if ( $field ) {
214
+ $data = array( $field => get_field( $field, $this->id ) );
215
+ } else {
216
+ $data['acf'] = get_fields( $this->id );
217
+ }
218
  } else {
219
  $data['acf'] = array();
220
  }
235
  $fields = array();
236
  $fields_tmp = array();
237
 
238
+ if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
 
239
  $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
240
 
241
  if ( is_array( $field_groups ) && ! empty( $field_groups ) ) {
248
  }
249
  }
250
  }
 
251
  } else {
 
252
  if ( strpos( $id, 'user_' ) !== false ) {
253
  $filter = array( 'ef_user' => str_replace( 'user_', '', $id ) );
254
  } elseif ( strpos( $id, 'taxonomy_' ) !== false ) {
267
  }
268
  }
269
  }
 
270
  }
271
 
272
  if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) {
lib/endpoints/class-acf-to-rest-api-option-controller.php CHANGED
@@ -12,7 +12,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Option_Controller' ) ) {
12
  }
13
 
14
  public function register_routes() {
15
- register_rest_route( $this->namespace, '/' . $this->rest_base, array(
16
  array(
17
  'methods' => WP_REST_Server::READABLE,
18
  'callback' => array( $this, 'get_item' ),
@@ -24,45 +24,6 @@ if ( ! class_exists( 'ACF_To_REST_API_Option_Controller' ) ) {
24
  'permission_callback' => array( $this, 'update_item_permissions_check' ),
25
  ),
26
  ) );
27
-
28
- register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<name>[\w\-\_]+)', array(
29
- array(
30
- 'methods' => WP_REST_Server::READABLE,
31
- 'callback' => array( $this, 'get_item' ),
32
- 'permission_callback' => array( $this, 'get_item_permissions_check' ),
33
- ),
34
- array(
35
- 'methods' => WP_REST_Server::EDITABLE,
36
- 'callback' => array( $this, 'update_item' ),
37
- 'permission_callback' => array( $this, 'update_item_permissions_check' ),
38
- ),
39
- ) );
40
- }
41
-
42
- public function prepare_item_for_database( $request ) {
43
- $item = parent::prepare_item_for_database( $request );
44
-
45
- if ( $item && $request instanceof WP_REST_Request ) {
46
- $name = $request->get_param( 'name' );
47
- if ( $name && array_key_exists( $name, $item['data'] ) ) {
48
- $item['data'] = array( $name => $item['data'][$name] );
49
- }
50
- }
51
-
52
- return $item;
53
- }
54
-
55
- public function get_fields( $request, $response = null, $object = null ) {
56
- if ( $request instanceof WP_REST_Request ) {
57
- $name = $request->get_param( 'name' );
58
- if ( $name ) {
59
- $value = get_field( $name, $this->type );
60
- $data = array( $name => $value );
61
- return apply_filters( 'acf/rest_api/' . $this->type . '/get_fields', $data, $request, $response, $object );
62
- }
63
- }
64
-
65
- return parent::get_fields( $request, $response, $object );
66
  }
67
  }
68
  }
12
  }
13
 
14
  public function register_routes() {
15
+ register_rest_route( $this->namespace, '/' . $this->rest_base . '/?(?P<field>[\w\-\_]+)?', array(
16
  array(
17
  'methods' => WP_REST_Server::READABLE,
18
  'callback' => array( $this, 'get_item' ),
24
  'permission_callback' => array( $this, 'update_item_permissions_check' ),
25
  ),
26
  ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
28
  }
29
  }
lib/endpoints/class-acf-to-rest-api-term-controller.php CHANGED
@@ -7,7 +7,7 @@ if ( ! defined( 'ABSPATH' ) ) {
7
  if ( ! class_exists( 'ACF_To_REST_API_Term_Controller' ) ) {
8
  class ACF_To_REST_API_Term_Controller extends ACF_To_REST_API_Controller {
9
  public function register_routes() {
10
- register_rest_route( $this->namespace, '/' . $this->type . '/(?P<taxonomy>[\w\-\_]+)/(?P<id>[\d]+)', array(
11
  array(
12
  'methods' => WP_REST_Server::READABLE,
13
  'callback' => array( $this, 'get_item' ),
7
  if ( ! class_exists( 'ACF_To_REST_API_Term_Controller' ) ) {
8
  class ACF_To_REST_API_Term_Controller extends ACF_To_REST_API_Controller {
9
  public function register_routes() {
10
+ register_rest_route( $this->namespace, '/' . $this->type . '/(?P<taxonomy>[\w\-\_]+)/(?P<id>[\d]+)/?(?P<field>[\w\-\_]+)?', array(
11
  array(
12
  'methods' => WP_REST_Server::READABLE,
13
  'callback' => array( $this, 'get_item' ),
readme.md CHANGED
@@ -1,6 +1,8 @@
1
  ACF to REST API
2
  ====
3
- Edit, Get and Puts [ACF](https://wordpress.org/plugins/advanced-custom-fields/) data into [WordPress REST API ( WP-API )](https://wordpress.org/plugins/rest-api/)
 
 
4
 
5
  - [Installation](#installation)
6
  - [Endpoints](#endpoints)
@@ -19,14 +21,21 @@ Endpoints
19
  | Endpoint | READABLE | EDITABLE |
20
  |----------|:--------:|:--------:|
21
  | /wp-json/acf/v2/post/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
22
  | /wp-json/acf/v2/page/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
23
  | /wp-json/acf/v2/user/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
24
  | /wp-json/acf/v2/term/**{taxonomy}**/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
25
  | /wp-json/acf/v2/comment/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
26
  | /wp-json/acf/v2/media/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
27
  | /wp-json/acf/v2/**{post-type}**/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
 
28
  | /wp-json/acf/v2/options | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
29
- | /wp-json/acf/v2/options/**{name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
30
 
31
  Filters
32
  ====
1
  ACF to REST API
2
  ====
3
+ Exposes [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-fields/) Endpoints in the [WP REST API v2](https://wordpress.org/plugins/rest-api/) ( WP-API | WordPress REST API )
4
+
5
+ https://wordpress.org/plugins/acf-to-rest-api/
6
 
7
  - [Installation](#installation)
8
  - [Endpoints](#endpoints)
21
  | Endpoint | READABLE | EDITABLE |
22
  |----------|:--------:|:--------:|
23
  | /wp-json/acf/v2/post/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
24
+ | /wp-json/acf/v2/post/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
25
  | /wp-json/acf/v2/page/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
26
+ | /wp-json/acf/v2/page/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
27
  | /wp-json/acf/v2/user/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
28
+ | /wp-json/acf/v2/user/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
29
  | /wp-json/acf/v2/term/**{taxonomy}**/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
30
+ | /wp-json/acf/v2/term/**{taxonomy}**/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
31
  | /wp-json/acf/v2/comment/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
32
+ | /wp-json/acf/v2/comment/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
33
  | /wp-json/acf/v2/media/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
34
+ | /wp-json/acf/v2/media/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
35
  | /wp-json/acf/v2/**{post-type}**/**{id}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
36
+ | /wp-json/acf/v2/**{post-type}**/**{id}**/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
37
  | /wp-json/acf/v2/options | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
38
+ | /wp-json/acf/v2/options/**{field_name}** | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) |
39
 
40
  Filters
41
  ====
readme.txt CHANGED
@@ -1,20 +1,19 @@
1
  === ACF to REST API ===
2
  Contributors: airesvsg
 
3
  Tags: acf, api, rest, wp-api, wp-rest-api, json, wp, wordpress, wp-rest-api
4
  Requires at least: 4.3
5
  Tested up to: 4.5
6
- Stable tag: 2.1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
- Edit, Get and Puts ACF data into the WordPress REST API ( WP-API | WP REST API ).
11
 
12
  == Description ==
13
- Edit, Get and Puts [ACF](https://wordpress.org/plugins/advanced-custom-fields/) data into the [WordPress REST API](https://wordpress.org/plugins/rest-api/) ( WP-API | WP REST API ).
14
 
15
- **See details on GitHub**
16
-
17
- http://github.com/airesvsg/acf-to-rest-api
18
 
19
  == Installation ==
20
  1. Copy the `acf-to-rest-api` folder into your `wp-content/plugins` folder
@@ -22,11 +21,15 @@ http://github.com/airesvsg/acf-to-rest-api
22
 
23
  == Changelog ==
24
 
 
 
 
25
  = 2.1.1 =
26
  bugfix term endpoint
27
 
28
  = 2.1.0 =
29
  adding rest base support
 
30
  adding new filter acf/rest_api/default_rest_base
31
 
32
  = 2.0.7 =
@@ -52,6 +55,7 @@ Bugfix strict standards
52
 
53
  = 2.0.0 =
54
  New version of the plugin ACF to WP REST API
 
55
  Changing name ACF to WP REST API > ACF to REST API
56
 
57
  == Upgrade Notice ==
1
  === ACF to REST API ===
2
  Contributors: airesvsg
3
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=airesvsg%40gmail%2ecom&lc=BR&item_name=Aires%20Goncalves&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest
4
  Tags: acf, api, rest, wp-api, wp-rest-api, json, wp, wordpress, wp-rest-api
5
  Requires at least: 4.3
6
  Tested up to: 4.5
7
+ Stable tag: 2.2.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
+ Exposes Advanced Custom Fields Endpoints in the WP REST API v2 ( WP-API | WordPress REST API )
12
 
13
  == Description ==
14
+ Exposes [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-fields/) Endpoints in the [WP REST API v2](https://wordpress.org/plugins/rest-api/) ( WP-API | WordPress REST API )
15
 
16
+ **See details on GitHub:** http://github.com/airesvsg/acf-to-rest-api
 
 
17
 
18
  == Installation ==
19
  1. Copy the `acf-to-rest-api` folder into your `wp-content/plugins` folder
21
 
22
  == Changelog ==
23
 
24
+ = 2.2.0 =
25
+ get specific field via endpoints
26
+
27
  = 2.1.1 =
28
  bugfix term endpoint
29
 
30
  = 2.1.0 =
31
  adding rest base support
32
+
33
  adding new filter acf/rest_api/default_rest_base
34
 
35
  = 2.0.7 =
55
 
56
  = 2.0.0 =
57
  New version of the plugin ACF to WP REST API
58
+
59
  Changing name ACF to WP REST API > ACF to REST API
60
 
61
  == Upgrade Notice ==