ACF to REST API - Version 3.0.1-beta

Version Description

fix default params bug

Download this release

Release Info

Developer airesvsg
Plugin Icon 128x128 ACF to REST API
Version 3.0.1-beta
Comparing to
See all releases

Code changes from version 2.2.1 to 3.0.1-beta

class-acf-to-rest-api.php CHANGED
@@ -1,10 +1,10 @@
1
  <?php
2
  /**
3
  * Plugin Name: ACF to REST API
4
- * Description: Exposes Advanced Custom Fields Endpoints in the WP REST API v2
5
  * Author: Aires Gonçalves
6
  * Author URI: http://github.com/airesvsg
7
- * Version: 2.2.1
8
  * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
  */
10
 
@@ -16,29 +16,67 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
16
 
17
  class ACF_To_REST_API {
18
 
19
- const VERSION = '2.2.1';
 
 
 
 
 
 
20
 
21
  public static function init() {
22
  self::includes();
23
  self::hooks();
24
  }
25
 
 
 
 
 
 
 
 
 
 
 
26
  private static function includes() {
 
 
 
 
 
 
27
  if ( self::is_plugin_active( 'all' ) ) {
28
- require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-controller.php';
29
- require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-option-controller.php';
30
- require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-term-controller.php';
31
- require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-attachment-controller.php';
 
 
 
 
 
 
 
 
 
 
32
  }
 
33
  }
34
 
35
  private static function hooks() {
36
  add_action( 'init', array( __CLASS__, 'load_plugin_textdomain' ) );
 
37
  if ( self::is_plugin_active( 'all' ) ) {
38
  add_action( 'rest_api_init', array( __CLASS__, 'create_rest_routes' ), 10 );
 
 
 
39
  } else {
40
  add_action( 'admin_notices', array( __CLASS__, 'missing_notice' ) );
41
  }
 
42
  }
43
 
44
  public static function load_plugin_textdomain() {
@@ -47,32 +85,7 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
47
  }
48
 
49
  public static function create_rest_routes() {
50
- $default = array( 'user', 'comment', 'term', 'option' );
51
- $types = get_post_types( array( 'show_in_rest' => true ) );
52
-
53
- if ( $types && isset( $types['attachment'] ) ) {
54
- unset( $types['attachment'] );
55
- $default[] = 'media';
56
- }
57
-
58
- $types = apply_filters( 'acf/rest_api/types', array_merge( $types, array_combine( $default, $default ) ) );
59
-
60
- if ( is_array( $types ) && count( $types ) > 0 ) {
61
- foreach( $types as $type ) {
62
- if ( 'term' == $type ) {
63
- $controller = new ACF_To_REST_API_Term_Controller( $type );
64
- } elseif ( 'media' == $type ) {
65
- $controller = new ACF_To_REST_API_Attachment_Controller( $type );
66
- } elseif ( 'option' == $type ) {
67
- $controller = new ACF_To_REST_API_Option_Controller( $type );
68
- } else {
69
- $controller = new ACF_To_REST_API_Controller( $type );
70
- }
71
-
72
- $controller->register_routes();
73
- $controller->register_hooks();
74
- }
75
- }
76
  }
77
 
78
  public static function is_plugin_active( $plugin ) {
@@ -89,7 +102,7 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
89
 
90
  public static function is_plugin_installed( $plugin ) {
91
  if ( ! function_exists( 'get_plugins' ) ) {
92
- include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
93
  }
94
 
95
  $paths = false;
@@ -103,10 +116,10 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
103
  $plugins = get_plugins();
104
  if ( is_array( $plugins ) && count( $plugins ) > 0 ) {
105
  foreach ( $paths as $path ) {
106
- if ( isset( $plugins[$path] ) && ! empty( $plugins[$path] ) ) {
107
  return $path;
108
  }
109
- }
110
  }
111
  }
112
 
@@ -114,16 +127,10 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
114
  }
115
 
116
  public static function missing_notice() {
117
- if ( ! self::is_plugin_active( 'rest-api' ) ) {
118
- include dirname( __FILE__ ) . '/includes/admin/views/html-notice-missing-rest-api.php';
119
- }
120
-
121
- if ( ! self::is_plugin_active( 'acf' ) ) {
122
- include dirname( __FILE__ ) . '/includes/admin/views/html-notice-missing-acf.php';
123
- }
124
  }
125
  }
126
 
127
  add_action( 'plugins_loaded', array( 'ACF_To_REST_API', 'init' ) );
128
 
129
- }
1
  <?php
2
  /**
3
  * Plugin Name: ACF to REST API
4
+ * Description: Exposes Advanced Custom Fields Endpoints in the WordPress REST API
5
  * Author: Aires Gonçalves
6
  * Author URI: http://github.com/airesvsg
7
+ * Version: 3.0.1-beta
8
  * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
  */
10
 
16
 
17
  class ACF_To_REST_API {
18
 
19
+ const VERSION = '3.0.1-beta';
20
+
21
+ private static $old_request_version = 2;
22
+ private static $default_request_version = 3;
23
+ private static $request_version;
24
+
25
+ private static $instance = null;
26
 
27
  public static function init() {
28
  self::includes();
29
  self::hooks();
30
  }
31
 
32
+ protected static function instance() {
33
+ if ( is_null( self::$instance ) ) {
34
+ $class = 'ACF_To_REST_API_V' . self::handle_request_version();
35
+ if ( class_exists( $class ) ) {
36
+ self::$instance = new $class;
37
+ }
38
+ }
39
+ return self::$instance;
40
+ }
41
+
42
  private static function includes() {
43
+ if ( self::$old_request_version == self::handle_request_version() ) {
44
+ require_once dirname( __FILE__ ) . '/legacy/v2/class-acf-to-rest-api-v2.php';
45
+ } else {
46
+ require_once dirname( __FILE__ ) . '/v3/class-acf-to-rest-api-v3.php';
47
+ }
48
+
49
  if ( self::is_plugin_active( 'all' ) ) {
50
+ if ( is_admin() ) {
51
+ require_once dirname( __FILE__ ) . '/shared/lib/class-acf-to-rest-api-settings.php';
52
+ }
53
+ self::instance()->includes();
54
+ }
55
+ }
56
+
57
+ public static function handle_request_version() {
58
+ if ( is_null( self::$request_version ) ) {
59
+ if ( defined( 'ACF_TO_REST_API_REQUEST_VERSION' ) ) {
60
+ self::$request_version = (int) ACF_TO_REST_API_REQUEST_VERSION;
61
+ } else {
62
+ self::$request_version = (int) get_option( 'acf_to_rest_api_request_version', self::$default_request_version );
63
+ }
64
  }
65
+ return self::$request_version;
66
  }
67
 
68
  private static function hooks() {
69
  add_action( 'init', array( __CLASS__, 'load_plugin_textdomain' ) );
70
+
71
  if ( self::is_plugin_active( 'all' ) ) {
72
  add_action( 'rest_api_init', array( __CLASS__, 'create_rest_routes' ), 10 );
73
+ if ( self::$default_request_version == self::handle_request_version() ) {
74
+ ACF_To_REST_API_ACF_Field_Settings::hooks();
75
+ }
76
  } else {
77
  add_action( 'admin_notices', array( __CLASS__, 'missing_notice' ) );
78
  }
79
+
80
  }
81
 
82
  public static function load_plugin_textdomain() {
85
  }
86
 
87
  public static function create_rest_routes() {
88
+ self::instance()->create_rest_routes();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  }
90
 
91
  public static function is_plugin_active( $plugin ) {
102
 
103
  public static function is_plugin_installed( $plugin ) {
104
  if ( ! function_exists( 'get_plugins' ) ) {
105
+ include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
106
  }
107
 
108
  $paths = false;
116
  $plugins = get_plugins();
117
  if ( is_array( $plugins ) && count( $plugins ) > 0 ) {
118
  foreach ( $paths as $path ) {
119
+ if ( isset( $plugins[ $path ] ) && ! empty( $plugins[ $path ] ) ) {
120
  return $path;
121
  }
122
+ }
123
  }
124
  }
125
 
127
  }
128
 
129
  public static function missing_notice() {
130
+ self::instance()->missing_notice();
 
 
 
 
 
 
131
  }
132
  }
133
 
134
  add_action( 'plugins_loaded', array( 'ACF_To_REST_API', 'init' ) );
135
 
136
+ }
composer.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "airesvsg/acf-to-rest-api",
3
+ "description": "Exposes Advanced Custom Fields Endpoints in the WordPress REST API",
4
+ "type": "wordpress-plugin",
5
+ "version": "3.0.1-beta",
6
+ "keywords": ["wordpress", "wp", "rest-api", "acf", "wp-api", "json", "wordpres-plugin", "fields"],
7
+ "homepage": "https://github.com/airesvsg/acf-to-rest-api",
8
+ "license": "GPLv2.0+",
9
+ "authors": [
10
+ {
11
+ "name": "Aires Gonçalves",
12
+ "email": "airesvsg@gmail.com",
13
+ "homepage": "http://airesgoncalves.com",
14
+ "role": "Developer"
15
+ }
16
+ ],
17
+ "support": {
18
+ "email": "airesvsg@gmail.com",
19
+ "issues": "https://github.com/airesvsg/acf-to-rest-api/issues"
20
+ },
21
+ "require": {
22
+ "php": ">=5.3.2",
23
+ "composer/installers": "~1.0"
24
+ }
25
+ }
languages/pt_BR.mo CHANGED
Binary file
legacy/v2/class-acf-to-rest-api-v2.php ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_V2' ) ) {
8
+
9
+ class ACF_To_REST_API_V2 {
10
+
11
+ public static function includes() {
12
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-controller.php';
13
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-option-controller.php';
14
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-term-controller.php';
15
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-attachment-controller.php';
16
+ }
17
+
18
+ public static function create_rest_routes() {
19
+ $default = array( 'user', 'comment', 'term', 'option' );
20
+ $types = get_post_types( array( 'show_in_rest' => true ) );
21
+ if ( $types && isset( $types['attachment'] ) ) {
22
+ unset( $types['attachment'] );
23
+ $default[] = 'media';
24
+ }
25
+ $types = apply_filters( 'acf/rest_api/types', array_merge( $types, array_combine( $default, $default ) ) );
26
+ if ( is_array( $types ) && count( $types ) > 0 ) {
27
+ foreach ( $types as $type ) {
28
+ if ( 'term' == $type ) {
29
+ $controller = new ACF_To_REST_API_Term_Controller( $type );
30
+ } elseif ( 'media' == $type ) {
31
+ $controller = new ACF_To_REST_API_Attachment_Controller( $type );
32
+ } elseif ( 'option' == $type ) {
33
+ $controller = new ACF_To_REST_API_Option_Controller( $type );
34
+ } else {
35
+ $controller = new ACF_To_REST_API_Controller( $type );
36
+ }
37
+ $controller->register_routes();
38
+ $controller->register_hooks();
39
+ }
40
+ }
41
+ }
42
+
43
+ public static function missing_notice() {
44
+ if ( ! ACF_To_REST_API::is_plugin_active( 'rest-api' ) ) {
45
+ include dirname( __FILE__ ) . '/../../shared/includes/admin/views/html-notice-missing-rest-api.php';
46
+ }
47
+ if ( ! ACF_To_REST_API::is_plugin_active( 'acf' ) ) {
48
+ include dirname( __FILE__ ) . '/../../shared/includes/admin/views/html-notice-missing-acf.php';
49
+ }
50
+ }
51
+
52
+ }
53
+ }
{lib → legacy/v2/lib}/endpoints/class-acf-to-rest-api-attachment-controller.php RENAMED
File without changes
{lib → legacy/v2/lib}/endpoints/class-acf-to-rest-api-controller.php RENAMED
@@ -41,11 +41,11 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
41
 
42
  protected function get_rest_base( $type ) {
43
  global $wp_post_types;
44
-
45
  $default = apply_filters( 'acf/rest_api/default_rest_base', ! in_array( $type, array( 'post', 'page' ) ), $type );
46
 
47
- if ( $default && isset( $wp_post_types[$type] ) && isset( $wp_post_types[$type]->rest_base ) ) {
48
- return $wp_post_types[$type]->rest_base;
49
  }
50
 
51
  return $type;
@@ -58,11 +58,11 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
58
  public function get_item_permissions_check( $request ) {
59
  return apply_filters( 'acf/rest_api/item_permissions/get', true, $request, $this->type );
60
  }
61
-
62
  public function rest_prepare( $response, $post, $request ) {
63
  return $this->get_fields( $request, $response, $post );
64
  }
65
-
66
  public function update_item_permissions_check( $request ) {
67
  return apply_filters( 'acf/rest_api/item_permissions/update', current_user_can( 'edit_posts' ), $request, $this->type );
68
  }
@@ -72,8 +72,8 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
72
 
73
  if ( is_array( $item ) && count( $item ) > 0 ) {
74
  foreach ( $item['data'] as $key => $value ) {
75
- if ( isset( $item['fields'][$key]['key'] ) ) {
76
- $field = $item['fields'][$key];
77
  if ( function_exists( 'acf_update_value' ) ) {
78
  acf_update_value( $value, $item['id'], $field );
79
  } elseif ( function_exists( 'update_field' ) ) {
@@ -86,7 +86,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
86
 
87
  return new WP_REST_Response( $this->get_fields( $request ), 200 );
88
  }
89
-
90
  return new WP_Error( 'cant_update_item', __( 'Cannot update item', 'acf-to-rest-api' ), array( 'status' => 500 ) );
91
  }
92
 
@@ -114,8 +114,8 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
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(
@@ -142,7 +142,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
142
  $this->id = $object['ID'];
143
  }
144
  } elseif ( is_object( $object ) ) {
145
- if( $object instanceof WP_REST_Response ) {
146
  return $this->get_id( $object->get_data() );
147
  } elseif ( $object instanceof WP_REST_Request ) {
148
  $this->id = $object->get_param( 'id' );
@@ -162,8 +162,8 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
162
 
163
  protected function format_id( $object ) {
164
  $this->get_id( $object );
165
-
166
- switch( $this->type ) {
167
  case 'comment' :
168
  $this->id = 'comment_' . $this->id;
169
  break;
@@ -220,7 +220,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
220
  } else {
221
  $data['acf'] = array();
222
  }
223
-
224
  if ( $swap ) {
225
  $response->data = $data;
226
  $data = $response;
@@ -237,14 +237,14 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
237
  $fields = array();
238
  $fields_tmp = array();
239
 
240
- if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
241
  $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
242
 
243
  if ( is_array( $field_groups ) && ! empty( $field_groups ) ) {
244
  foreach ( $field_groups as $field_group ) {
245
  $field_group_fields = acf_get_fields( $field_group );
246
  if ( is_array( $field_group_fields ) && ! empty( $field_group_fields ) ) {
247
- foreach( array_keys( $field_group_fields ) as $i ) {
248
  $fields_tmp[] = acf_extract_var( $field_group_fields, $i );
249
  }
250
  }
@@ -263,7 +263,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
263
  $acfs = apply_filters( 'acf/get_field_groups', array() );
264
 
265
  if ( is_array( $acfs ) && ! empty( $acfs ) && is_array( $field_groups ) && ! empty( $field_groups ) ) {
266
- foreach( $acfs as $acf ) {
267
  if ( in_array( $acf['id'], $field_groups ) ) {
268
  $fields_tmp = array_merge( $fields_tmp, apply_filters( 'acf/field_group/get_fields', array(), $acf['id'] ) );
269
  }
@@ -272,9 +272,9 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
272
  }
273
 
274
  if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) {
275
- foreach( $fields_tmp as $field ) {
276
  if ( is_array( $field ) && isset( $field['name'] ) ) {
277
- $fields[$field['name']] = $field;
278
  }
279
  }
280
  }
@@ -283,4 +283,4 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
283
  }
284
 
285
  }
286
- }
41
 
42
  protected function get_rest_base( $type ) {
43
  global $wp_post_types;
44
+
45
  $default = apply_filters( 'acf/rest_api/default_rest_base', ! in_array( $type, array( 'post', 'page' ) ), $type );
46
 
47
+ if ( $default && isset( $wp_post_types[ $type ] ) && isset( $wp_post_types[ $type ]->rest_base ) ) {
48
+ return $wp_post_types[ $type ]->rest_base;
49
  }
50
 
51
  return $type;
58
  public function get_item_permissions_check( $request ) {
59
  return apply_filters( 'acf/rest_api/item_permissions/get', true, $request, $this->type );
60
  }
61
+
62
  public function rest_prepare( $response, $post, $request ) {
63
  return $this->get_fields( $request, $response, $post );
64
  }
65
+
66
  public function update_item_permissions_check( $request ) {
67
  return apply_filters( 'acf/rest_api/item_permissions/update', current_user_can( 'edit_posts' ), $request, $this->type );
68
  }
72
 
73
  if ( is_array( $item ) && count( $item ) > 0 ) {
74
  foreach ( $item['data'] as $key => $value ) {
75
+ if ( isset( $item['fields'][ $key ]['key'] ) ) {
76
+ $field = $item['fields'][ $key ];
77
  if ( function_exists( 'acf_update_value' ) ) {
78
  acf_update_value( $value, $item['id'], $field );
79
  } elseif ( function_exists( 'update_field' ) ) {
86
 
87
  return new WP_REST_Response( $this->get_fields( $request ), 200 );
88
  }
89
+
90
  return new WP_Error( 'cant_update_item', __( 'Cannot update item', 'acf-to-rest-api' ), array( 'status' => 500 ) );
91
  }
92
 
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(
142
  $this->id = $object['ID'];
143
  }
144
  } elseif ( is_object( $object ) ) {
145
+ if ( $object instanceof WP_REST_Response ) {
146
  return $this->get_id( $object->get_data() );
147
  } elseif ( $object instanceof WP_REST_Request ) {
148
  $this->id = $object->get_param( 'id' );
162
 
163
  protected function format_id( $object ) {
164
  $this->get_id( $object );
165
+
166
+ switch ( $this->type ) {
167
  case 'comment' :
168
  $this->id = 'comment_' . $this->id;
169
  break;
220
  } else {
221
  $data['acf'] = array();
222
  }
223
+
224
  if ( $swap ) {
225
  $response->data = $data;
226
  $data = $response;
237
  $fields = array();
238
  $fields_tmp = array();
239
 
240
+ if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
241
  $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
242
 
243
  if ( is_array( $field_groups ) && ! empty( $field_groups ) ) {
244
  foreach ( $field_groups as $field_group ) {
245
  $field_group_fields = acf_get_fields( $field_group );
246
  if ( is_array( $field_group_fields ) && ! empty( $field_group_fields ) ) {
247
+ foreach ( array_keys( $field_group_fields ) as $i ) {
248
  $fields_tmp[] = acf_extract_var( $field_group_fields, $i );
249
  }
250
  }
263
  $acfs = apply_filters( 'acf/get_field_groups', array() );
264
 
265
  if ( is_array( $acfs ) && ! empty( $acfs ) && is_array( $field_groups ) && ! empty( $field_groups ) ) {
266
+ foreach ( $acfs as $acf ) {
267
  if ( in_array( $acf['id'], $field_groups ) ) {
268
  $fields_tmp = array_merge( $fields_tmp, apply_filters( 'acf/field_group/get_fields', array(), $acf['id'] ) );
269
  }
272
  }
273
 
274
  if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) {
275
+ foreach ( $fields_tmp as $field ) {
276
  if ( is_array( $field ) && isset( $field['name'] ) ) {
277
+ $fields[ $field['name'] ] = $field;
278
  }
279
  }
280
  }
283
  }
284
 
285
  }
286
+ }
{lib → legacy/v2/lib}/endpoints/class-acf-to-rest-api-option-controller.php RENAMED
@@ -10,7 +10,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Option_Controller' ) ) {
10
  parent::__construct( $type );
11
  $this->rest_base = 'options';
12
  }
13
-
14
  public function register_routes() {
15
  register_rest_route( $this->namespace, '/' . $this->rest_base . '/?(?P<field>[\w\-\_]+)?', array(
16
  array(
10
  parent::__construct( $type );
11
  $this->rest_base = 'options';
12
  }
13
+
14
  public function register_routes() {
15
  register_rest_route( $this->namespace, '/' . $this->rest_base . '/?(?P<field>[\w\-\_]+)?', array(
16
  array(
{lib → legacy/v2/lib}/endpoints/class-acf-to-rest-api-term-controller.php RENAMED
@@ -31,14 +31,14 @@ if ( ! class_exists( 'ACF_To_REST_API_Term_Controller' ) ) {
31
 
32
  protected function get_rest_base( $request ) {
33
  global $wp_taxonomies;
34
-
35
  $taxonomy = false;
36
  if ( $request instanceof WP_REST_Request ) {
37
- $taxonomy = $request->get_param( 'taxonomy' );
38
  }
39
 
40
  if ( $taxonomy && ! array_key_exists( $taxonomy, $wp_taxonomies ) ) {
41
- foreach( $wp_taxonomies as $tax_key => $tax_value ) {
42
  if ( isset( $tax_value->rest_base ) && $taxonomy == $tax_value->rest_base ) {
43
  $request->set_param( 'taxonomy', $tax_key );
44
  return $tax_key;
@@ -50,11 +50,9 @@ if ( ! class_exists( 'ACF_To_REST_API_Term_Controller' ) ) {
50
  }
51
 
52
  protected function show( $object ) {
53
- global $wp_taxonomies;
54
-
55
  $taxonomy = $this->get_rest_base( $object );
56
-
57
- return $taxonomy && isset( $wp_taxonomies[$taxonomy]->show_in_rest ) && $wp_taxonomies[$taxonomy]->show_in_rest;
58
  }
59
  }
60
  }
31
 
32
  protected function get_rest_base( $request ) {
33
  global $wp_taxonomies;
34
+
35
  $taxonomy = false;
36
  if ( $request instanceof WP_REST_Request ) {
37
+ $taxonomy = $request->get_param( 'taxonomy' );
38
  }
39
 
40
  if ( $taxonomy && ! array_key_exists( $taxonomy, $wp_taxonomies ) ) {
41
+ foreach ( $wp_taxonomies as $tax_key => $tax_value ) {
42
  if ( isset( $tax_value->rest_base ) && $taxonomy == $tax_value->rest_base ) {
43
  $request->set_param( 'taxonomy', $tax_key );
44
  return $tax_key;
50
  }
51
 
52
  protected function show( $object ) {
53
+ global $wp_taxonomies;
 
54
  $taxonomy = $this->get_rest_base( $object );
55
+ return $taxonomy && isset( $wp_taxonomies[ $taxonomy ]->show_in_rest ) && $wp_taxonomies[ $taxonomy ]->show_in_rest;
 
56
  }
57
  }
58
  }
readme.md CHANGED
@@ -1,14 +1,17 @@
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/)
4
 
5
  https://wordpress.org/plugins/acf-to-rest-api/
6
 
7
  - [Installation](#installation)
8
  - [Endpoints](#endpoints)
9
  - [Filters](#filters)
10
- - [Editing the fields](#editing-the-fields)
11
- - [Example](#example)
 
 
 
12
  - [Cache](#cache)
13
 
14
  Installation
@@ -21,65 +24,109 @@ Endpoints
21
 
22
  | Endpoint | READABLE | EDITABLE |
23
  |----------|:--------:|:--------:|
24
- | /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) |
25
- | /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) |
26
- | /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) |
27
- | /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) |
28
- | /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) |
29
- | /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) |
30
- | /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) |
31
- | /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) |
32
- | /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) |
33
- | /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) |
34
- | /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) |
35
- | /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) |
36
- | /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) |
37
- | /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) |
38
- | /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) |
39
- | /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) |
 
 
 
 
 
 
 
40
 
41
  Filters
42
  ====
43
  | Filter | Argument(s) |
44
  |-----------|-----------|
45
- | acf/rest_api/types | array **$types** |
46
- | acf/rest_api/type | string **$type** |
47
- | acf/rest_api/id | mixed ( string, integer, boolean ) **$id** |
48
  | acf/rest_api/key | string **$key**<br>WP_REST_Request **$request**<br>string **$type** |
49
  | acf/rest_api/item_permissions/get | boolean **$permission**<br>WP_REST_Request **$request**<br>string **$type** |
50
  | acf/rest_api/item_permissions/update | boolean **$permission**<br>WP_REST_Request **$request**<br>string **$type** |
51
- | acf/rest_api/default_rest_base | boolean **$default**<br>string **$type** |
52
  | acf/rest_api/**{type}**/prepare_item | mixed ( array, boolean ) **$item**<br>WP_REST_Request **$request** |
53
- | acf/rest_api/**{type}**/get_fields | mixed ( array, WP_REST_Request ) **$data**<br>mixed ( WP_REST_Request, NULL ) **$request**<br>mixed ( WP_REST_Response, NULL ) **$response**<br>mixed ( WP_Post, WP_Term, WP_User, NULL ) **$object** |
 
 
 
 
 
 
 
 
 
54
 
55
- If you do not want edit/show the fields of posts. So, you must use the filter `acf/rest_api/types`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  ```PHP
58
- add_filter( 'acf/rest_api/types', function( $types ) {
59
- if ( array_key_exists( 'post', $types ) ) {
60
- unset( $types['post'] );
61
- }
62
 
63
- return $types;
64
- } );
 
 
 
 
 
 
 
 
 
 
65
  ```
66
 
 
 
 
 
67
  Editing the fields
68
  ====
69
  The fields should be sent into the key `fields`.
70
 
71
- ![Field Name](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/field-name.jpg)
72
 
73
- **Action:** http://localhost/wp-json/acf/v2/post/1
74
 
75
  ```HTML
76
- <form action="http://localhost/wp-json/acf/v2/post/1" method="POST">
77
- <?php
78
- // http://v2.wp-api.org/guide/authentication
79
- wp_nonce_field( 'wp_rest' );
80
- ?>
81
- <label>Site: <input type="text" name="fields[site]"></label>
82
- <button type="submit">Save</button>
83
  </form>
84
  ```
85
 
@@ -87,14 +134,14 @@ The fields should be sent into the key `fields`.
87
 
88
  ```HTML
89
  <form action="http://localhost/wp-json/wp/v2/posts/1" method="POST">
90
- <?php
91
- // http://v2.wp-api.org/guide/authentication
92
- wp_nonce_field( 'wp_rest' );
93
- ?>
94
- <label>Title: <input type="text" name="title"></label>
95
- <h3>ACF</h3>
96
- <label>Site: <input type="text" name="fields[site]"></label>
97
- <button type="submit">Save</button>
98
  </form>
99
  ```
100
 
@@ -102,31 +149,35 @@ Use the filter `acf/rest_api/key` to change the key `fields`.
102
 
103
  ```PHP
104
  add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
105
- return 'acf_fields';
106
  }, 10, 3 );
107
  ```
108
 
109
  Now, the fields should be sent into the key `acf_fields`
110
 
111
  ```HTML
112
- <form action="http://localhost/wp-json/acf/v2/post/1" method="POST">
113
- <?php
114
- // http://v2.wp-api.org/guide/authentication
115
- wp_nonce_field( 'wp_rest' );
116
- ?>
117
- <label>Site: <input type="text" name="acf_fields[site]"></label>
118
- <button type="submit">Save</button>
119
  </form>
120
  ```
121
 
122
- Example
123
  ====
124
  Sample theme to edit the ACF Fields.
125
 
126
  https://github.com/airesvsg/acf-to-rest-api-example
127
 
 
 
 
 
128
  Cache
129
  ====
130
  Enable caching for WordPress REST API and increase speed of your application.
131
 
132
- https://github.com/airesvsg/wp-rest-api-cache
1
  ACF to REST API
2
  ====
3
+ Exposes [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-fields/) Endpoints in the [WordPress REST API](https://developer.wordpress.org/rest-api/)
4
 
5
  https://wordpress.org/plugins/acf-to-rest-api/
6
 
7
  - [Installation](#installation)
8
  - [Endpoints](#endpoints)
9
  - [Filters](#filters)
10
+ - [Deprecated Filters ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png)](#deprecated-filters)
11
+ - [Request API Version ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png)](#request-api-version)
12
+ - [Field Settings ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png)](#field-settings)
13
+ - [Editing the Fields](#editing-the-fields)
14
+ - [Examples](#examples)
15
  - [Cache](#cache)
16
 
17
  Installation
24
 
25
  | Endpoint | READABLE | EDITABLE |
26
  |----------|:--------:|:--------:|
27
+ | /wp-json/acf/v3/posts ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
28
+ | /wp-json/acf/v3/posts/**{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) |
29
+ | /wp-json/acf/v3/posts/**{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) |
30
+ | /wp-json/acf/v3/pages ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
31
+ | /wp-json/acf/v3/pages/**{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/v3/pages/**{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/v3/users ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
34
+ | /wp-json/acf/v3/users/**{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) |
35
+ | /wp-json/acf/v3/users/**{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) |
36
+ | /wp-json/acf/v3/**{taxonomy}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
37
+ | /wp-json/acf/v3/**{taxonomy}**/**{id}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![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/v3/**{taxonomy}**/**{id}**/**{field-name}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![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
+ | /wp-json/acf/v3/comments ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
40
+ | /wp-json/acf/v3/comments/**{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) |
41
+ | /wp-json/acf/v3/comments/**{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) |
42
+ | /wp-json/acf/v3/media ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
43
+ | /wp-json/acf/v3/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) |
44
+ | /wp-json/acf/v3/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) |
45
+ | /wp-json/acf/v3/**{post-type}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![yes](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/yes.png) | ![no](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/no.png) |
46
+ | /wp-json/acf/v3/**{post-type}**/**{id}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![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) |
47
+ | /wp-json/acf/v3/**{post-type}**/**{id}**/**{field-name}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![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) |
48
+ | /wp-json/acf/v3/options/**{id}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![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) |
49
+ | /wp-json/acf/v3/options/**{id}**/**{field-name}** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | ![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) |
50
 
51
  Filters
52
  ====
53
  | Filter | Argument(s) |
54
  |-----------|-----------|
55
+ | acf/rest_api/id | mixed ( string, integer, boolean ) **$id**<br>string **$type** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png)<br>string **$controller** ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) |
 
 
56
  | acf/rest_api/key | string **$key**<br>WP_REST_Request **$request**<br>string **$type** |
57
  | acf/rest_api/item_permissions/get | boolean **$permission**<br>WP_REST_Request **$request**<br>string **$type** |
58
  | acf/rest_api/item_permissions/update | boolean **$permission**<br>WP_REST_Request **$request**<br>string **$type** |
 
59
  | acf/rest_api/**{type}**/prepare_item | mixed ( array, boolean ) **$item**<br>WP_REST_Request **$request** |
60
+ | acf/rest_api/**{type}**/get_fields | mixed ( array, WP_REST_Request ) **$data**<br>mixed ( WP_REST_Request, NULL ) **$request** |
61
+ | acf/rest_api/field_settings/show_in_rest ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | boolean **$show** |
62
+ | acf/rest_api/field_settings/edit_in_rest ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png) | boolean **$edit** |
63
+
64
+ Basic example of how to use the filters, in this case I will set a new permission to get the fields
65
+ ```PHP
66
+ add_filter( 'acf/rest_api/item_permissions/get', function( $permission ) {
67
+ return current_user_can( 'edit_posts' );
68
+ } );
69
+ ```
70
 
71
+ Deprecated filters
72
+ ====
73
+ | Filter | Argument(s) |
74
+ |-----------|-----------|
75
+ | acf/rest_api/type | string **$type** |
76
+ | acf/rest_api/types | array **$types** |
77
+ | acf/rest_api/default_rest_base | boolean **$default**<br>string **$type** |
78
+
79
+ Request API version
80
+ ====
81
+ See below how to select the Request API Version.
82
+
83
+ 1. Open the plugins page;
84
+ 2. Click the settings link under the pluing name ( `ACF to REST API` );
85
+ 3. Select your version in the `ACF to REST API` session;
86
+ 4. Click in the button Save changes.
87
+
88
+ ![Choose request API version](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/request-api-version-v3.jpg)
89
+
90
+ The other alternative is to define the constant `ACF_TO_REST_API_REQUEST_VERSION` in your `wp-config.php`
91
 
92
  ```PHP
93
+ define( 'ACF_TO_REST_API_REQUEST_VERSION', 2 );
94
+ ```
 
 
95
 
96
+ Field Settings
97
+ ====
98
+ In this version is possible to configure the field options via admin.
99
+
100
+ The options are enabled using the filters below, by default theses options are disabled.
101
+
102
+ ```PHP
103
+ // Enable the option show in rest
104
+ add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' );
105
+
106
+ // Enable the option edit in rest
107
+ add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' );
108
  ```
109
 
110
+ After you activate the filters, all your fields should show these options:
111
+ ![Choose request API version](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/field-settings-v3.jpg)
112
+
113
+
114
  Editing the fields
115
  ====
116
  The fields should be sent into the key `fields`.
117
 
118
+ ![Field Name](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/field-name-v3.jpg)
119
 
120
+ **Action:** http://localhost/wp-json/acf/v3/posts/1
121
 
122
  ```HTML
123
+ <form action="http://localhost/wp-json/acf/v3/posts/1" method="POST">
124
+ <?php
125
+ // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
126
+ wp_nonce_field( 'wp_rest' );
127
+ ?>
128
+ <label>Site: <input type="text" name="fields[site]"></label>
129
+ <button type="submit">Save</button>
130
  </form>
131
  ```
132
 
134
 
135
  ```HTML
136
  <form action="http://localhost/wp-json/wp/v2/posts/1" method="POST">
137
+ <?php
138
+ // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
139
+ wp_nonce_field( 'wp_rest' );
140
+ ?>
141
+ <label>Title: <input type="text" name="title"></label>
142
+ <h3>ACF</h3>
143
+ <label>Site: <input type="text" name="fields[site]"></label>
144
+ <button type="submit">Save</button>
145
  </form>
146
  ```
147
 
149
 
150
  ```PHP
151
  add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
152
+ return 'acf_fields';
153
  }, 10, 3 );
154
  ```
155
 
156
  Now, the fields should be sent into the key `acf_fields`
157
 
158
  ```HTML
159
+ <form action="http://localhost/wp-json/acf/v3/posts/1" method="POST">
160
+ <?php
161
+ // https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
162
+ wp_nonce_field( 'wp_rest' );
163
+ ?>
164
+ <label>Site: <input type="text" name="acf_fields[site]"></label>
165
+ <button type="submit">Save</button>
166
  </form>
167
  ```
168
 
169
+ Examples
170
  ====
171
  Sample theme to edit the ACF Fields.
172
 
173
  https://github.com/airesvsg/acf-to-rest-api-example
174
 
175
+ To-do list ![new](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/new-v3.1.png)
176
+
177
+ https://github.com/airesvsg/to-do-list-acf-to-rest-api
178
+
179
  Cache
180
  ====
181
  Enable caching for WordPress REST API and increase speed of your application.
182
 
183
+ https://github.com/airesvsg/wp-rest-api-cache
readme.txt CHANGED
@@ -1,17 +1,17 @@
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.7
7
- Stable tag: 2.2.1
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
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/)
15
 
16
  **See details on GitHub:** http://github.com/airesvsg/acf-to-rest-api
17
 
@@ -21,6 +21,18 @@ Exposes [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-f
21
 
22
  == Changelog ==
23
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  = 2.2.1 =
25
  bugfix options page id
26
 
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, wordpress-rest-api
5
+ Requires at least: 4.6
6
+ Tested up to: 4.7.2
7
+ Stable tag: 3.0.1-beta
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 WordPress REST API
12
 
13
  == Description ==
14
+ Exposes [Advanced Custom Fields](https://wordpress.org/plugins/advanced-custom-fields/) Endpoints in the [WordPress REST API](https://developer.wordpress.org/rest-api/)
15
 
16
  **See details on GitHub:** http://github.com/airesvsg/acf-to-rest-api
17
 
21
 
22
  == Changelog ==
23
 
24
+ = 3.0.1-beta =
25
+ fix default params bug
26
+
27
+ = 3.0.0-beta =
28
+ more readable endpoints ( https://github.com/airesvsg/acf-to-rest-api/issues/46 ) - Thanks to Imaginet
29
+ change return when acf filed key is empty ( https://github.com/airesvsg/acf-to-rest-api/issues/48 ) - Thanks to Joris Verbogt
30
+ multiple custom options pages ( https://github.com/airesvsg/acf-to-rest-api/issues/85 ) - Thanks to Alex Patton
31
+ bugfix ACF key in taxonomy ( https://github.com/airesvsg/acf-to-rest-api/issues/43 ) - Thanks to Cesar Denis
32
+ depreacted filter acf/rest_api/type
33
+ depreacted filter acf/rest_api/types
34
+ depreacted filter acf/rest_api/default_rest_base
35
+
36
  = 2.2.1 =
37
  bugfix options page id
38
 
{includes → shared/includes}/admin/views/html-notice-missing-acf.php RENAMED
@@ -17,7 +17,7 @@ if ( current_user_can( 'install_plugins' ) ) {
17
  }
18
  } else {
19
  $target = true;
20
- $url = 'http://wordpress.org/plugins/advanced-custom-fields/';
21
  }
22
 
23
  ?>
@@ -25,4 +25,4 @@ if ( current_user_can( 'install_plugins' ) ) {
25
  <div class="notice error is-dismissible">
26
  <p><strong><?php esc_html_e( 'ACF to REST API', 'act-to-rest-api' ); ?></strong> <?php esc_html_e( 'depends on the last version of Advanced Custom Fields to work!', 'acf-to-rest-api' ); ?></p>
27
  <p><a href="<?php echo esc_url( $url ); ?>" class="button button-primary"<?php if ( $target ) : ?> target="_blank"<?php endif; ?>><?php esc_html_e( $action . ' Advanced Custom Fields', 'acf-to-rest-api' ); ?></a></p>
28
- </div>
17
  }
18
  } else {
19
  $target = true;
20
+ $url = 'http://wordpress.org/plugins/advanced-custom-fields/';
21
  }
22
 
23
  ?>
25
  <div class="notice error is-dismissible">
26
  <p><strong><?php esc_html_e( 'ACF to REST API', 'act-to-rest-api' ); ?></strong> <?php esc_html_e( 'depends on the last version of Advanced Custom Fields to work!', 'acf-to-rest-api' ); ?></p>
27
  <p><a href="<?php echo esc_url( $url ); ?>" class="button button-primary"<?php if ( $target ) : ?> target="_blank"<?php endif; ?>><?php esc_html_e( $action . ' Advanced Custom Fields', 'acf-to-rest-api' ); ?></a></p>
28
+ </div>
{includes → shared/includes}/admin/views/html-notice-missing-rest-api.php RENAMED
@@ -17,7 +17,7 @@ if ( current_user_can( 'install_plugins' ) ) {
17
  }
18
  } else {
19
  $target = true;
20
- $url = 'http://wordpress.org/plugins/rest-api/';
21
  }
22
 
23
  ?>
@@ -25,4 +25,4 @@ if ( current_user_can( 'install_plugins' ) ) {
25
  <div class="notice error is-dismissible">
26
  <p><strong><?php esc_html_e( 'ACF to REST API', 'act-to-rest-api' ); ?></strong> <?php esc_html_e( 'depends on the last version of WordPress REST API to work!', 'acf-to-rest-api' ); ?></p>
27
  <p><a href="<?php echo esc_url( $url ); ?>" class="button button-primary"<?php if ( $target ) : ?> target="_blank"<?php endif; ?>><?php esc_html_e( $action . ' WordPress REST API', 'acf-to-rest-api' ); ?></a></p>
28
- </div>
17
  }
18
  } else {
19
  $target = true;
20
+ $url = 'http://wordpress.org/plugins/rest-api/';
21
  }
22
 
23
  ?>
25
  <div class="notice error is-dismissible">
26
  <p><strong><?php esc_html_e( 'ACF to REST API', 'act-to-rest-api' ); ?></strong> <?php esc_html_e( 'depends on the last version of WordPress REST API to work!', 'acf-to-rest-api' ); ?></p>
27
  <p><a href="<?php echo esc_url( $url ); ?>" class="button button-primary"<?php if ( $target ) : ?> target="_blank"<?php endif; ?>><?php esc_html_e( $action . ' WordPress REST API', 'acf-to-rest-api' ); ?></a></p>
28
+ </div>
shared/includes/admin/views/html-settings-field.php ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ ?>
8
+
9
+ <div id="acf-to-rest-api-settings">
10
+ <code><?php echo esc_url( site_url( 'wp-json/acf/' ) ); ?></code>
11
+ <select name="acf_to_rest_api_settings[request_version]">
12
+ <option value="2"<?php selected( 2, $request_version ); ?>>v2</option>
13
+ <option value="3"<?php selected( 3, $request_version ); ?>>v3</option>
14
+ </select>
15
+ <p><a href="<?php echo esc_url( self::$donation_url ); ?>" target="_blank"><?php esc_html_e( 'Click here', 'acf-to-rest-api' ); ?></a> <?php esc_html_e( 'to make a donation and help to improve the plugin.', 'acf-to-rest-api' ); ?></p>
16
+ </div>
shared/includes/admin/views/html-settings-section.php ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
+ exit;
4
+ }
5
+
6
+ ?>
7
+
8
+ <p><?php esc_html_e( 'Choose your request api version', 'acf-to-rest-api' ); ?></p>
shared/lib/class-acf-to-rest-api-settings.php ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Settings' ) ) {
8
+
9
+ class ACF_To_REST_API_Settings {
10
+
11
+ private static $donation_url = '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';
12
+ private static $github_url = 'http://github.com/airesvsg/acf-to-rest-api';
13
+
14
+ public static function init() {
15
+ self::hooks();
16
+ self::save();
17
+ }
18
+
19
+ private static function hooks() {
20
+ if ( ACF_To_REST_API::is_plugin_active( 'all' ) ) {
21
+ if ( current_user_can( 'manage_options' ) && ! defined( 'ACF_TO_REST_API_REQUEST_VERSION' ) ) {
22
+ add_action( 'admin_init', array( __CLASS__, 'acf_admin_setting' ) );
23
+ add_filter( 'plugin_action_links_acf-to-rest-api/class-acf-to-rest-api.php', array( __CLASS__, 'plugin_action_links' ) );
24
+ }
25
+
26
+ add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 3 );
27
+ }
28
+ }
29
+
30
+ public static function add_settings_section() {
31
+ include_once dirname( __FILE__ ) . '/../includes/admin/views/html-settings-section.php';
32
+ }
33
+
34
+ public static function add_settings_field( $args ) {
35
+ $request_version = ACF_To_REST_API::handle_request_version();
36
+
37
+ include_once dirname( __FILE__ ) . '/../includes/admin/views/html-settings-field.php';
38
+ }
39
+
40
+ public static function plugin_action_links( $actions ) {
41
+ if ( ! empty( $actions ) ) {
42
+ $new_actions = array(
43
+ 'acf_to_rest_api_settings' => sprintf( '<a href="%s">%s</a>', admin_url( 'options-permalink.php#acf-to-rest-api-settings' ), esc_html__( 'Settings', 'acf-to-rest-api' ) ),
44
+ );
45
+
46
+ $new_actions += $actions;
47
+
48
+ return $new_actions;
49
+ }
50
+
51
+ return $actions;
52
+ }
53
+
54
+ public static function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) {
55
+ if ( isset( $plugin_data['slug'] ) && 'acf-to-rest-api' == $plugin_data['slug'] ) {
56
+ $plugin_meta['acf-to-rest-api-github'] = sprintf( '<a href="%s" target="_blank">%s</a>', self::$github_url, esc_html__( 'Fork me on GitHub' ) );
57
+ $plugin_meta['acf_to_rest_api_donation'] = sprintf( '<a href="%s" target="_blank">%s</a>', self::$donation_url, esc_html__( 'Make a donation', 'acf-to-rest-api' ) );
58
+ }
59
+
60
+ return $plugin_meta;
61
+ }
62
+
63
+ public static function acf_admin_setting() {
64
+ add_settings_section(
65
+ 'acf_to_rest_api_settings_section',
66
+ __( 'ACF to REST API', 'acf-to-rest-api' ),
67
+ array( __CLASS__, 'add_settings_section' ),
68
+ 'permalink'
69
+ );
70
+
71
+ add_settings_field(
72
+ 'acf_to_rest_api_request_version',
73
+ __( 'Request Version', 'acf-to-rest-api' ),
74
+ array( __CLASS__, 'add_settings_field' ),
75
+ 'permalink',
76
+ 'acf_to_rest_api_settings_section'
77
+ );
78
+ }
79
+
80
+ private static function save() {
81
+ if ( ! is_admin() ) {
82
+ return;
83
+ }
84
+
85
+ if ( isset( $_POST['acf_to_rest_api_settings'] ) ) {
86
+ $settings = $_POST['acf_to_rest_api_settings'];
87
+ if ( array_key_exists( 'request_version', $settings ) ) {
88
+ update_option( 'acf_to_rest_api_request_version', absint( $settings['request_version'] ) );
89
+ }
90
+ }
91
+ }
92
+
93
+ }
94
+
95
+ ACF_To_REST_API_Settings::init();
96
+
97
+ }
v3/class-acf-to-rest-api-v3.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_V3' ) ) {
8
+ class ACF_To_REST_API_V3 {
9
+ public static function includes() {
10
+ require_once dirname( __FILE__ ) . '/lib/class-acf-to-rest-api-acf-api.php';
11
+ require_once dirname( __FILE__ ) . '/lib/class-acf-to-rest-api-acf-field-settings.php';
12
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-controller.php';
13
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-posts-controller.php';
14
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-terms-controller.php';
15
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-comments-controller.php';
16
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-attachments-controller.php';
17
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-options-controller.php';
18
+ require_once dirname( __FILE__ ) . '/lib/endpoints/class-acf-to-rest-api-users-controller.php';
19
+ }
20
+
21
+ public static function create_rest_routes() {
22
+ foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
23
+ if ( 'attachment' == $post_type->name ) {
24
+ $controller = new ACF_To_REST_API_Attachments_Controller( $post_type );
25
+ } else {
26
+ $controller = new ACF_To_REST_API_Posts_Controller( $post_type );
27
+ }
28
+ $controller->register();
29
+ }
30
+
31
+ foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) as $taxonomy ) {
32
+ $controller = new ACF_To_REST_API_Terms_Controller( $taxonomy );
33
+ $controller->register();
34
+ }
35
+
36
+ $controller = new ACF_To_REST_API_Comments_Controller;
37
+ $controller->register();
38
+
39
+ $controller = new ACF_To_REST_API_Options_Controller;
40
+ $controller->register();
41
+
42
+ $controller = new ACF_To_REST_API_Users_Controller;
43
+ $controller->register();
44
+ }
45
+
46
+ public static function missing_notice() {
47
+ if ( ! ACF_To_REST_API::is_plugin_active( 'rest-api' ) ) {
48
+ include dirname( __FILE__ ) . '/../shared/includes/admin/views/html-notice-missing-rest-api.php';
49
+ }
50
+
51
+ if ( ! ACF_To_REST_API::is_plugin_active( 'acf' ) ) {
52
+ include dirname( __FILE__ ) . '/../shared/includes/admin/views/html-notice-missing-acf.php';
53
+ }
54
+ }
55
+ }
56
+ }
v3/lib/class-acf-to-rest-api-acf-api.php ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_ACF_API' ) ) {
8
+ class ACF_To_REST_API_ACF_API {
9
+ protected $id = null;
10
+ protected $type = null;
11
+ protected $controller = null;
12
+ protected $field_objects = null;
13
+
14
+ public function __construct( $type, $controller = null ) {
15
+ $this->type = $type;
16
+ $this->controller = $controller;
17
+ }
18
+
19
+ protected function format_id( $object ) {
20
+ if ( $this->id ) {
21
+ switch ( $this->type ) {
22
+ case 'comment' :
23
+ $this->id = 'comment_' . $this->id;
24
+ break;
25
+ case 'user' :
26
+ $this->id = 'user_' . $this->id;
27
+ break;
28
+ default :
29
+ if ( 'ACF_To_REST_API_Terms_Controller' == $this->controller ) {
30
+ $this->id = $this->type . '_' . $this->id;
31
+ }
32
+ break;
33
+ }
34
+ }
35
+
36
+ $this->id = apply_filters( 'acf/rest_api/id', $this->id, $this->type, $this->controller );
37
+
38
+ return $this->id;
39
+ }
40
+
41
+ public function get_id( $obj ) {
42
+ $this->id = false;
43
+
44
+ if ( is_numeric( $obj ) ) {
45
+ $this->id = $obj;
46
+ } elseif ( is_array( $obj ) && isset( $obj['id'] ) ) {
47
+ $this->id = $obj['id'];
48
+ } elseif ( is_object( $obj ) ) {
49
+ if ( $obj instanceof WP_REST_Response ) {
50
+ $data = $obj->get_data();
51
+ if ( isset( $data['id'] ) ) {
52
+ $this->id = $data['id'];
53
+ }
54
+ } elseif ( $obj instanceof WP_REST_Request ) {
55
+ $this->id = $obj->get_param( 'id' );
56
+ } elseif ( isset( $obj->ID ) ) {
57
+ $this->id = $obj->ID;
58
+ } elseif ( isset( $obj->comment_ID ) ) {
59
+ $this->id = $obj->comment_ID;
60
+ } elseif ( isset( $obj->term_id ) ) {
61
+ $this->id = $obj->term_id;
62
+ }
63
+ }
64
+
65
+ if ( 'option' == $this->type ) {
66
+ $this->id = sanitize_title( $this->id );
67
+ } else {
68
+ $this->id = absint( $this->id );
69
+ }
70
+
71
+ return $this->format_id( $this->id );
72
+ }
73
+
74
+ public function get_fields( $request ) {
75
+ $data = array();
76
+ $field = null;
77
+
78
+ if ( $request instanceof WP_REST_Request ) {
79
+ $field = $request->get_param( 'field' );
80
+ }
81
+
82
+ if ( $this->get_id( $request ) ) {
83
+ if ( $field ) {
84
+ $data[ $field ] = get_field( $field, $this->id );
85
+ } else {
86
+ $fields = get_fields( $this->id );
87
+ if ( ! $fields ) {
88
+ $fields = array();
89
+ }
90
+ $data['acf'] = $fields;
91
+ }
92
+
93
+ if ( apply_filters( 'acf/rest_api/field_settings/show_in_rest', false ) && $this->get_field_objects( $this->id ) ) {
94
+ if ( $field ) {
95
+ $this->show_in_rest( $data, $field, $this->field_objects );
96
+ } else {
97
+ foreach ( array_keys( $data['acf'] ) as $key ) {
98
+ $this->show_in_rest( $data['acf'], $key, $this->field_objects );
99
+ }
100
+ }
101
+ }
102
+ } else {
103
+ $data['acf'] = array();
104
+ }
105
+
106
+ return apply_filters( 'acf/rest_api/' . $this->type . '/get_fields', $data, $request );
107
+ }
108
+
109
+ public function get_field_objects( $id ) {
110
+ if ( empty( $id ) ) {
111
+ return false;
112
+ }
113
+
114
+ $this->field_objects = false;
115
+ $fields_tmp = array();
116
+
117
+ if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
118
+ $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
119
+
120
+ if ( is_array( $field_groups ) && ! empty( $field_groups ) ) {
121
+ foreach ( $field_groups as $field_group ) {
122
+ $field_group_fields = acf_get_fields( $field_group );
123
+ if ( is_array( $field_group_fields ) && ! empty( $field_group_fields ) ) {
124
+ foreach ( array_keys( $field_group_fields ) as $i ) {
125
+ $fields_tmp[] = acf_extract_var( $field_group_fields, $i );
126
+ }
127
+ }
128
+ }
129
+ }
130
+ } else {
131
+ if ( strpos( $id, 'user_' ) !== false ) {
132
+ $filter = array( 'ef_user' => str_replace( 'user_', '', $id ) );
133
+ } elseif ( strpos( $id, 'taxonomy_' ) !== false ) {
134
+ $filter = array( 'ef_taxonomy' => str_replace( 'taxonomy_', '', $id ) );
135
+ } else {
136
+ $filter = array( 'post_id' => $id );
137
+ }
138
+
139
+ $field_groups = apply_filters( 'acf/location/match_field_groups', array(), $filter );
140
+ $acfs = apply_filters( 'acf/get_field_groups', array() );
141
+
142
+ if ( is_array( $acfs ) && ! empty( $acfs ) && is_array( $field_groups ) && ! empty( $field_groups ) ) {
143
+ foreach ( $acfs as $acf ) {
144
+ if ( in_array( $acf['id'], $field_groups ) ) {
145
+ $fields_tmp = array_merge( $fields_tmp, apply_filters( 'acf/field_group/get_fields', array(), $acf['id'] ) );
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) {
152
+ $this->field_objects = array();
153
+ foreach ( $fields_tmp as $field ) {
154
+ if ( is_array( $field ) && isset( $field['name'] ) ) {
155
+ $this->field_objects[ $field['name'] ] = $field;
156
+ }
157
+ }
158
+ }
159
+
160
+ return $this->field_objects;
161
+ }
162
+
163
+ public function show_in_rest( &$data, $field, $field_objects ) {
164
+ if ( ! array_key_exists( $field, $field_objects ) || ! isset( $field_objects[ $field ]['show_in_rest'] ) || ! $field_objects[ $field ]['show_in_rest'] ) {
165
+ unset( $data[ $field ] );
166
+ }
167
+ }
168
+
169
+ public function edit_in_rest( $field ) {
170
+ return ! ( apply_filters( 'acf/rest_api/field_settings/edit_in_rest', false ) && isset( $field['edit_in_rest'] ) && ! $field['edit_in_rest'] );
171
+ }
172
+ }
173
+ }
v3/lib/class-acf-to-rest-api-acf-field-settings.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_ACF_Field_Settings' ) ) {
8
+ class ACF_To_REST_API_ACF_Field_Settings {
9
+ private function __construct() {}
10
+
11
+ public static function hooks() {
12
+ if ( function_exists( 'acf_render_field_setting' ) ) {
13
+ add_action( 'acf/render_field_settings', array( __CLASS__, 'render_field_settings' ) );
14
+ } else {
15
+ add_action( 'acf/create_field_options', array( __CLASS__, 'render_field_settings' ) );
16
+ }
17
+ }
18
+
19
+ public static function render_field_settings( $field ) {
20
+ if ( apply_filters( 'acf/rest_api/field_settings/show_in_rest', false ) ) {
21
+ self::show_in_rest( $field );
22
+ }
23
+
24
+ if ( apply_filters( 'acf/rest_api/field_settings/edit_in_rest', false ) ) {
25
+ self::edit_in_rest( $field );
26
+ }
27
+ }
28
+
29
+ private static function edit_in_rest( $field ) {
30
+ if ( function_exists( 'acf_render_field_setting' ) ) {
31
+ acf_render_field_setting( $field, array(
32
+ 'label' => __( 'Edit in REST API?', 'acf-to-rest-api' ),
33
+ 'instructions' => '',
34
+ 'type' => 'true_false',
35
+ 'name' => 'edit_in_rest',
36
+ 'ui' => 1,
37
+ 'class' => 'field-edit_in_rest',
38
+ 'default_value' => 1,
39
+ ), true );
40
+ } else { ?>
41
+ <tr>
42
+ <td class="label">
43
+ <label><?php esc_html_e( 'Edit in REST API?', 'acf-to-rest-api' ); ?></label>
44
+ </td>
45
+ <td>
46
+ <?php
47
+ if ( ! isset( $field['edit_in_rest'] ) ) {
48
+ $field['edit_in_rest'] = 1;
49
+ }
50
+ do_action( 'acf/create_field', array(
51
+ 'type' => 'radio',
52
+ 'name' => 'fields[' . $field['name'] . '][edit_in_rest]',
53
+ 'value' => $field['edit_in_rest'],
54
+ 'layout' => 'horizontal',
55
+ 'choices' => array(
56
+ 1 => __( 'Yes', 'acf-to-rest-api' ),
57
+ 0 => __( 'No', 'acf-to-rest-api' ),
58
+ ),
59
+ ) ); ?>
60
+ </td>
61
+ </tr>
62
+ <?php
63
+ }
64
+ }
65
+
66
+ private static function show_in_rest( $field ) {
67
+ if ( function_exists( 'acf_render_field_setting' ) ) {
68
+ acf_render_field_setting( $field, array(
69
+ 'label' => __( 'Show in REST API?', 'acf-to-rest-api' ),
70
+ 'instructions' => '',
71
+ 'type' => 'true_false',
72
+ 'name' => 'show_in_rest',
73
+ 'ui' => 1,
74
+ 'class' => 'field-show_in_rest',
75
+ 'default_value' => 1,
76
+ ), true );
77
+ } else { ?>
78
+ <tr>
79
+ <td class="label">
80
+ <label><?php esc_html_e( 'Show in REST API?', 'acf-to-rest-api' ); ?></label>
81
+ </td>
82
+ <td>
83
+ <?php
84
+ if ( ! isset( $field['show_in_rest'] ) ) {
85
+ $field['show_in_rest'] = 1;
86
+ }
87
+ do_action( 'acf/create_field', array(
88
+ 'type' => 'radio',
89
+ 'name' => 'fields[' . $field['name'] . '][show_in_rest]',
90
+ 'value' => $field['show_in_rest'],
91
+ 'layout' => 'horizontal',
92
+ 'choices' => array(
93
+ 1 => __( 'Yes', 'acf-to-rest-api' ),
94
+ 0 => __( 'No', 'acf-to-rest-api' ),
95
+ ),
96
+ ) ); ?>
97
+ </td>
98
+ </tr>
99
+ <?php
100
+ }
101
+ }
102
+ }
103
+ }
v3/lib/endpoints/class-acf-to-rest-api-attachments-controller.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Attachments_Controller' ) ) {
8
+ class ACF_To_REST_API_Attachments_Controller extends ACF_To_REST_API_Controller {
9
+ public function __construct( $type ) {
10
+ $this->type = $type->name;
11
+ $this->rest_base = ! empty( $type->rest_base ) ? $type->rest_base : $type->name;
12
+ parent::__construct( $type );
13
+ }
14
+
15
+ public function get_items( $request ) {
16
+ $this->controller = new WP_REST_Attachments_Controller( $this->type );
17
+ return parent::get_items( $request );
18
+ }
19
+ }
20
+ }
v3/lib/endpoints/class-acf-to-rest-api-comments-controller.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Comments_Controller' ) ) {
8
+ class ACF_To_REST_API_Comments_Controller extends ACF_To_REST_API_Controller {
9
+ public function __construct() {
10
+ $this->type = 'comment';
11
+ $this->rest_base = 'comments';
12
+ parent::__construct();
13
+ }
14
+
15
+ public function get_items( $request ) {
16
+ $this->controller = new WP_REST_Comments_Controller;
17
+ return parent::get_items( $request );
18
+ }
19
+ }
20
+ }
v3/lib/endpoints/class-acf-to-rest-api-controller.php ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
8
+ class ACF_To_REST_API_Controller extends WP_REST_Controller {
9
+ protected $acf = null;
10
+ protected $type = null;
11
+ protected $controller = null;
12
+
13
+ protected static $default_params = array(
14
+ 'page' => 1,
15
+ 'per_page' => 10,
16
+ 'orderby' => 'id',
17
+ );
18
+
19
+ public function __construct( $type = null ) {
20
+ $this->namespace = 'acf/v3';
21
+ $this->acf = new ACF_To_REST_API_ACF_API( $this->type, get_class( $this ) );
22
+ }
23
+
24
+ public function register_hooks() {
25
+ add_action( 'rest_insert_' . $this->type, array( $this, 'rest_insert' ), 10, 3 );
26
+ }
27
+
28
+ public function register_routes() {
29
+ register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/?(?P<field>[\w\-\_]+)?', array(
30
+ array(
31
+ 'methods' => WP_REST_Server::READABLE,
32
+ 'callback' => array( $this, 'get_item' ),
33
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
34
+ ),
35
+ array(
36
+ 'methods' => WP_REST_Server::EDITABLE,
37
+ 'callback' => array( $this, 'update_item' ),
38
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
39
+ ),
40
+ ) );
41
+
42
+ register_rest_route( $this->namespace, '/' . $this->rest_base, array(
43
+ array(
44
+ 'methods' => WP_REST_Server::READABLE,
45
+ 'callback' => array( $this, 'get_items' ),
46
+ 'permission_callback' => array( $this, 'get_items_permissions_check' ),
47
+ ),
48
+ ) );
49
+ }
50
+
51
+ public function register_field() {
52
+ register_rest_field( $this->type, 'acf', array(
53
+ 'get_callback' => function( $data ) {
54
+ $fields = $this->acf->get_fields( $data );
55
+ return $fields['acf'];
56
+ },
57
+ 'schema' => array(
58
+ 'description' => __( 'Expose advanced custom fields.', 'acf-to-rest-api' ),
59
+ 'type' => 'object',
60
+ ),
61
+ ) );
62
+ }
63
+
64
+ public function register() {
65
+ $this->register_routes();
66
+ $this->register_hooks();
67
+ $this->register_field();
68
+ }
69
+
70
+ public function get_item( $request ) {
71
+ $fields = $this->acf->get_fields( $request );
72
+ return rest_ensure_response( $fields );
73
+ }
74
+
75
+ public function get_item_permissions_check( $request ) {
76
+ return apply_filters( 'acf/rest_api/item_permissions/get', true, $request, $this->type );
77
+ }
78
+
79
+ public function get_items( $request ) {
80
+ if ( ! method_exists( $this->controller, 'get_items' ) ) {
81
+ return new WP_Error( 'cant_get_items', __( 'Cannot get items', 'acf-to-rest-api' ), array( 'status' => 500 ) );
82
+ }
83
+
84
+ $this->set_default_parameters( $request );
85
+ $data = $this->controller->get_items( $request )->get_data();
86
+
87
+ $response = array();
88
+ if ( ! empty( $data ) ) {
89
+ foreach ( $data as $v ) {
90
+ if ( isset( $v['acf'] ) ) {
91
+ $response[] = array(
92
+ 'id' => $v['id'],
93
+ 'acf' => $v['acf'],
94
+ );
95
+ }
96
+ }
97
+ }
98
+
99
+ return apply_filters( 'acf/rest_api/' . $this->type . '/get_items', rest_ensure_response( $response ), rest_ensure_request( $request ) );
100
+ }
101
+
102
+ public function get_items_permissions_check( $request ) {
103
+ return apply_filters( 'acf/rest_api/items_permissions/get', true, $request, $this->type );
104
+ }
105
+
106
+ public function update_item_permissions_check( $request ) {
107
+ return apply_filters( 'acf/rest_api/item_permissions/update', current_user_can( 'edit_posts' ), $request, $this->type );
108
+ }
109
+
110
+ public function update_item( $request ) {
111
+ $item = $this->prepare_item_for_database( $request );
112
+ if ( is_array( $item ) && count( $item ) > 0 ) {
113
+ foreach ( $item['data'] as $key => $value ) {
114
+ if ( isset( $item['fields'][ $key ]['key'] ) ) {
115
+ $field = $item['fields'][ $key ];
116
+ $edit = $this->acf->edit_in_rest( $field );
117
+ if ( $edit ) {
118
+ if ( function_exists( 'acf_update_value' ) ) {
119
+ acf_update_value( $value, $item['id'], $field );
120
+ } elseif ( function_exists( 'update_field' ) ) {
121
+ update_field( $field['key'], $value, $item['id'] );
122
+ } else {
123
+ do_action( 'acf/update_value', $value, $item['id'], $field );
124
+ }
125
+ }
126
+ }
127
+ }
128
+
129
+ return new WP_REST_Response( $this->acf->get_fields( $request ), 200 );
130
+ }
131
+
132
+ return new WP_Error( 'cant_update_item', __( 'Cannot update item', 'acf-to-rest-api' ), array( 'status' => 500 ) );
133
+ }
134
+
135
+ public function rest_insert( $object, $request, $creating ) {
136
+ if ( $request instanceof WP_REST_Request ) {
137
+ $id = $this->acf->get_id( $object );
138
+ if ( ! $id ) {
139
+ $id = $this->acf->get_id( $request );
140
+ }
141
+ $request->set_param( 'id', $id );
142
+ }
143
+
144
+ return $this->update_item( $request );
145
+ }
146
+
147
+ public function prepare_item_for_database( $request ) {
148
+ $item = false;
149
+ if ( $request instanceof WP_REST_Request ) {
150
+ $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type );
151
+ if ( is_string( $key ) && ! empty( $key ) ) {
152
+ $data = $request->get_param( $key );
153
+ $field = $request->get_param( 'field' );
154
+ $id = $this->acf->get_id( $request );
155
+ if ( $id && is_array( $data ) ) {
156
+ $fields = $this->acf->get_field_objects( $id );
157
+ if ( is_array( $fields ) && ! empty( $fields ) ) {
158
+ if ( $field && isset( $data[ $field ] ) ) {
159
+ $data = array( $field => $data[ $field ] );
160
+ }
161
+ $item = array(
162
+ 'id' => $id,
163
+ 'fields' => $fields,
164
+ 'data' => $data,
165
+ );
166
+ }
167
+ }
168
+ }
169
+ }
170
+ return apply_filters( 'acf/rest_api/' . $this->type . '/prepare_item', $item, $request );
171
+ }
172
+
173
+ protected function set_default_parameters( &$request ) {
174
+ if ( $request instanceof WP_REST_Request ) {
175
+ $params = $request->get_params();
176
+ foreach ( self::$default_params as $k => $v ) {
177
+ if ( ! isset( $params[ $k ] ) ) {
178
+ $request->set_param( $k, $v );
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
v3/lib/endpoints/class-acf-to-rest-api-options-controller.php ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Options_Controller' ) ) {
8
+ class ACF_To_REST_API_Options_Controller extends ACF_To_REST_API_Controller {
9
+ public function __construct() {
10
+ $this->type = 'option';
11
+ $this->rest_base = 'options';
12
+ parent::__construct();
13
+ }
14
+
15
+ public function register_routes() {
16
+ register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\w\-\_]+)/?(?P<field>[\w\-\_]+)?', array(
17
+ array(
18
+ 'methods' => WP_REST_Server::READABLE,
19
+ 'callback' => array( $this, 'get_item' ),
20
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
21
+ ),
22
+ array(
23
+ 'methods' => WP_REST_Server::EDITABLE,
24
+ 'callback' => array( $this, 'update_item' ),
25
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
26
+ ),
27
+ ) );
28
+ }
29
+
30
+ }
31
+ }
v3/lib/endpoints/class-acf-to-rest-api-posts-controller.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Posts_Controller' ) ) {
8
+ class ACF_To_REST_API_Posts_Controller extends ACF_To_REST_API_Controller {
9
+ public function __construct( $type ) {
10
+ $this->type = $type->name;
11
+ $this->rest_base = ! empty( $type->rest_base ) ? $type->rest_base : $type->name;
12
+ parent::__construct( $type );
13
+ }
14
+
15
+ public function get_items( $request ) {
16
+ $this->controller = new WP_REST_Posts_Controller( $this->type );
17
+ return parent::get_items( $request );
18
+ }
19
+ }
20
+ }
v3/lib/endpoints/class-acf-to-rest-api-terms-controller.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Terms_Controller' ) ) {
8
+ class ACF_To_REST_API_Terms_Controller extends ACF_To_REST_API_Controller {
9
+ public function __construct( $type ) {
10
+ $this->type = $type->name;
11
+ $this->rest_base = ! empty( $type->rest_base ) ? $type->rest_base : $type->name;
12
+ parent::__construct( $type );
13
+ }
14
+
15
+ public function get_items( $request ) {
16
+ $this->controller = new WP_REST_Terms_Controller( $this->type );
17
+ return parent::get_items( $request );
18
+ }
19
+ }
20
+ }
v3/lib/endpoints/class-acf-to-rest-api-users-controller.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Users_Controller' ) ) {
8
+ class ACF_To_REST_API_Users_Controller extends ACF_To_REST_API_Controller {
9
+ public function __construct() {
10
+ $this->type = 'user';
11
+ $this->rest_base = 'users';
12
+ parent::__construct();
13
+ }
14
+
15
+ public function get_items( $request ) {
16
+ $this->controller = new WP_REST_Users_Controller;
17
+ return parent::get_items( $request );
18
+ }
19
+ }
20
+ }