ACF to REST API - Version 2.0.3

Version Description

error fixed when register a new post with acf fields

Download this release

Release Info

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

Version 2.0.3

class-acf-to-rest-api.php ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.0.3
8
+ * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
+ */
10
+
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
+ exit;
13
+ }
14
+
15
+ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
16
+
17
+ class ACF_To_REST_API {
18
+
19
+ const VERSION = '2.0.3';
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() {
45
+ $locale = apply_filters( 'plugin_locale', get_locale(), 'acf-to-rest-api' );
46
+ load_textdomain( 'acf-to-rest-api', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/languages/' . $locale . '.mo' );
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 ) {
79
+ if ( ! function_exists( 'is_plugin_active' ) ) {
80
+ include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
81
+ }
82
+
83
+ if ( 'rest-api' == $plugin ) {
84
+ return is_plugin_active( 'rest-api/plugin.php' );
85
+ } elseif ( 'acf' == $plugin ) {
86
+ return is_plugin_active( 'advanced-custom-fields/acf.php' ) || is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) || is_plugin_active( 'acf-pro/acf.php' );
87
+ } elseif ( 'all' == $plugin ) {
88
+ return self::is_plugin_active( 'rest-api' ) && self::is_plugin_active( 'acf' );
89
+ }
90
+
91
+ return false;
92
+ }
93
+
94
+ public static function is_plugin_installed( $plugin ) {
95
+ if ( ! function_exists( 'get_plugins' ) ) {
96
+ include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
97
+ }
98
+
99
+ $paths = false;
100
+ if ( 'rest-api' == $plugin ) {
101
+ $paths = array( 'rest-api/plugin.php' );
102
+ } elseif ( 'acf' == $plugin ) {
103
+ $paths = array( 'advanced-custom-fields-pro/acf.php', 'acf-pro/acf.php', 'advanced-custom-fields/acf.php' );
104
+ }
105
+
106
+ if ( $paths ) {
107
+ $plugins = get_plugins();
108
+ if ( $plugins ) {
109
+ foreach ( $paths as $path ) {
110
+ if ( ! empty( $plugins[$path] ) ) {
111
+ return $path;
112
+ }
113
+ }
114
+ }
115
+ }
116
+
117
+ return false;
118
+ }
119
+
120
+ public static function missing_notice() {
121
+ if ( ! self::is_plugin_active( 'rest-api' ) ) {
122
+ include dirname( __FILE__ ) . '/includes/admin/views/html-notice-missing-rest-api.php';
123
+ }
124
+
125
+ if ( ! self::is_plugin_active( 'acf' ) ) {
126
+ include dirname( __FILE__ ) . '/includes/admin/views/html-notice-missing-acf.php';
127
+ }
128
+ }
129
+ }
130
+
131
+ add_action( 'plugins_loaded', array( 'ACF_To_REST_API', 'init' ) );
132
+
133
+ }
includes/admin/views/html-notice-missing-acf.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ $is_installed = ACF_To_REST_API::is_plugin_installed( 'acf' );
8
+
9
+ $target = false;
10
+ $action = __( 'Install', 'acf-to-rest-api' );
11
+ if ( current_user_can( 'install_plugins' ) ) {
12
+ if ( $is_installed ) {
13
+ $action = __( 'Active', 'acf-to-rest-api' );
14
+ $url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . $is_installed . '&plugin_status=active' ), 'activate-plugin_' . $is_installed );
15
+ } else {
16
+ $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=advanced-custom-fields' ), 'install-plugin_advanced-custom-fields' );
17
+ }
18
+ } else {
19
+ $target = true;
20
+ $url = 'http://wordpress.org/plugins/advanced-custom-fields/';
21
+ }
22
+
23
+ ?>
24
+
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/admin/views/html-notice-missing-rest-api.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ $is_installed = ACF_To_REST_API::is_plugin_installed( 'rest-api' );
8
+
9
+ $target = false;
10
+ $action = __( 'Install', 'acf-to-rest-api' );
11
+ if ( current_user_can( 'install_plugins' ) ) {
12
+ if ( $is_installed ) {
13
+ $action = __( 'Active', 'acf-to-rest-api' );
14
+ $url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=rest-api/plugin.php&plugin_status=active' ), 'activate-plugin_rest-api/plugin.php' );
15
+ } else {
16
+ $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=rest-api' ), 'install-plugin_rest-api' );
17
+ }
18
+ } else {
19
+ $target = true;
20
+ $url = 'http://wordpress.org/plugins/rest-api/';
21
+ }
22
+
23
+ ?>
24
+
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>
languages/pt_BR.mo ADDED
Binary file
lib/endpoints/class-acf-to-rest-api-attachment-controller.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Attachment_Controller' ) ) {
8
+ class ACF_To_REST_API_Attachment_Controller extends ACF_To_REST_API_Controller {
9
+ public function register_hooks() {
10
+ $this->type = 'attachment';
11
+ parent::register_hooks();
12
+ }
13
+ }
14
+ }
lib/endpoints/class-acf-to-rest-api-controller.php ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ protected $type;
11
+
12
+ protected $id;
13
+
14
+ public function __construct( $type ) {
15
+ $this->type = apply_filters( 'acf/rest_api/type', $type );
16
+ }
17
+
18
+ public function register_hooks() {
19
+ if ( $this->type ) {
20
+ add_filter( "rest_prepare_{$this->type}", array( $this, 'rest_prepare' ), 10, 3 );
21
+ add_action( "rest_insert_{$this->type}", array( $this, 'rest_insert' ), 10, 3 );
22
+ }
23
+ }
24
+
25
+ public function register_routes() {
26
+ register_rest_route( 'acf/v2', "/{$this->type}/(?P<id>\d+)/?", array(
27
+ array(
28
+ 'methods' => WP_REST_Server::READABLE,
29
+ 'callback' => array( $this, 'get_item' ),
30
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
31
+ ),
32
+ array(
33
+ 'methods' => WP_REST_Server::EDITABLE,
34
+ 'callback' => array( $this, 'update_item' ),
35
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
36
+ ),
37
+ ) );
38
+ }
39
+
40
+ public function get_item( $request ) {
41
+ return $this->get_fields( $request );
42
+ }
43
+
44
+ public function get_item_permissions_check( $request ) {
45
+ return apply_filters( "acf/rest_api/item_permissions/get", true, $request, $this->type );
46
+ }
47
+
48
+ public function rest_prepare( $response, $post, $request ) {
49
+ return $this->get_fields( $request, $response, $post );
50
+ }
51
+
52
+ public function update_item_permissions_check( $request ) {
53
+ return apply_filters( "acf/rest_api/item_permissions/update", current_user_can( 'edit_posts' ), $request, $this->type );
54
+ }
55
+
56
+ public function update_item( $request ) {
57
+ $item = $this->prepare_item_for_database( $request );
58
+
59
+ if ( is_array( $item ) && count( $item ) > 0 ) {
60
+ foreach ( $item['data'] as $key => $value ) {
61
+ if ( isset( $item['fields'][$key]['key'] ) ) {
62
+ $field = $item['fields'][$key];
63
+ if ( function_exists( 'acf_update_value' ) ) {
64
+ acf_update_value( $value, $item['id'], $field );
65
+ } elseif ( function_exists( 'update_field' ) ) {
66
+ update_field( $field['key'], $value, $item['id'] );
67
+ } else {
68
+ do_action( 'acf/update_value', $value, $item['id'], $field );
69
+ }
70
+ }
71
+ }
72
+
73
+ return new WP_REST_Response( $this->get_fields( $request ), 200 );
74
+ }
75
+
76
+ return new WP_Error( 'cant_update_item', __( "Cannot update item", 'acf-to-rest-api' ), array( 'status' => 500 ) );
77
+ }
78
+
79
+ public function rest_insert( $object, $request, $creating ) {
80
+ if ( $request instanceof WP_REST_Request && ! $this->get_id( $request ) && $this->format_id( $object ) ) {
81
+ $request->set_param( 'id', $this->id );
82
+ }
83
+
84
+ return $this->update_item( $request );
85
+ }
86
+
87
+ public function prepare_item_for_database( $request ) {
88
+ $item = false;
89
+
90
+ if ( $request instanceof WP_REST_Request ) {
91
+ $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type );
92
+
93
+ if ( is_string( $key ) && ! empty( $key ) ) {
94
+ $data = $request->get_param( $key );
95
+
96
+ $this->format_id( $request );
97
+
98
+ if ( $this->id && is_array( $data ) ) {
99
+ $fields = $this->get_field_objects( $this->id );
100
+
101
+ if ( $fields ) {
102
+ $item = array(
103
+ 'id' => $this->id,
104
+ 'fields' => $fields,
105
+ 'data' => $data,
106
+ );
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ return apply_filters( "acf/rest_api/{$this->type}/prepare_item", $item, $request );
113
+ }
114
+
115
+ protected function get_id( $object ) {
116
+ $this->id = false;
117
+
118
+ if ( is_numeric( $object ) ) {
119
+ $this->id = $object;
120
+ } elseif ( is_array( $object ) ) {
121
+ $object = array_change_key_case( $object, CASE_UPPER );
122
+ if ( array_key_exists( 'ID', $object ) ) {
123
+ $this->id = $object['ID'];
124
+ }
125
+ } elseif ( is_object( $object ) ) {
126
+ if( $object instanceof WP_REST_Response ) {
127
+ return $this->get_id( $object->get_data() );
128
+ } elseif ( $object instanceof WP_REST_Request ) {
129
+ $this->id = $object->get_param( 'id' );
130
+ } elseif ( isset( $object->ID ) ) {
131
+ $this->id = $object->ID;
132
+ } elseif ( isset( $object->comment_ID ) ) {
133
+ $this->id = $object->comment_ID;
134
+ } elseif ( isset( $object->term_id ) ) {
135
+ $this->id = $object->term_id;
136
+ }
137
+ }
138
+
139
+ $this->id = absint( $this->id );
140
+
141
+ return $this->id;
142
+ }
143
+
144
+ protected function format_id( $object ) {
145
+ $this->get_id( $object );
146
+
147
+ switch( $this->type ) {
148
+ case 'comment' :
149
+ $this->id = 'comment_' . $this->id;
150
+ break;
151
+ case 'user' :
152
+ $this->id = 'user_' . $this->id;
153
+ break;
154
+ case 'term' :
155
+ if ( $object instanceof WP_Term ) {
156
+ $taxonomy = $object->taxonomy;
157
+ } elseif ( $object instanceof WP_REST_Request ) {
158
+ $taxonomy = $object->get_param( 'taxonomy' );
159
+ }
160
+ $this->id = $taxonomy . '_' . $this->id;
161
+ break;
162
+ case 'option' :
163
+ $this->id = 'option';
164
+ break;
165
+ }
166
+
167
+ return apply_filters( 'acf/rest_api/id', $this->id );
168
+ }
169
+
170
+ protected function get_fields( $request, $response = null, $object = null ) {
171
+ $data = array();
172
+ $swap = $response instanceof WP_REST_Response;
173
+
174
+ if ( $swap ) {
175
+ $data = $response->get_data();
176
+ }
177
+
178
+ if ( empty( $object ) ) {
179
+ if ( ! empty( $request ) ) {
180
+ $object = $request;
181
+ } elseif ( ! empty( $data ) ) {
182
+ $object = $response;
183
+ }
184
+ }
185
+
186
+ $this->format_id( $object );
187
+
188
+ if ( $this->id ) {
189
+ $data['acf'] = get_fields( $this->id );
190
+ } else {
191
+ $data['acf'] = array();
192
+ }
193
+
194
+ if ( $swap ) {
195
+ $response->data = $data;
196
+ $data = $response;
197
+ }
198
+
199
+ return apply_filters( "acf/rest_api/{$this->type}/get_fields", $data, $request, $response, $object );
200
+ }
201
+
202
+ protected function get_field_objects( $id ) {
203
+ if ( empty( $id ) ) {
204
+ return false;
205
+ }
206
+
207
+ $fields = get_field_objects( $id );
208
+
209
+ if ( ! $fields ) {
210
+ $fields = array();
211
+ $fields_tmp = array();
212
+
213
+ if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
214
+ $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
215
+
216
+ if ( ! empty( $field_groups ) ) {
217
+ foreach ( $field_groups as $field_group ) {
218
+ $field_group_fields = acf_get_fields( $field_group );
219
+ if ( ! empty( $field_group_fields ) ) {
220
+ foreach( array_keys( $field_group_fields ) as $i ) {
221
+ $fields_tmp[] = acf_extract_var( $field_group_fields, $i );
222
+ }
223
+ }
224
+ }
225
+ }
226
+ } else {
227
+ if ( strpos( $id, 'user_' ) !== false ) {
228
+ $filter = array( 'ef_user' => str_replace( 'user_', '', $id ) );
229
+ } elseif ( strpos( $id, 'taxonomy_' ) !== false ) {
230
+ $filter = array( 'ef_taxonomy' => str_replace( 'taxonomy_', '', $id ) );
231
+ } else {
232
+ $filter = array( 'post_id' => $id );
233
+ }
234
+
235
+ $field_groups = apply_filters( 'acf/location/match_field_groups', array(), $filter );
236
+ $acfs = apply_filters( 'acf/get_field_groups', array() );
237
+
238
+ if ( is_array( $acfs ) && is_array( $field_groups ) ) {
239
+ foreach( $acfs as $acf ) {
240
+ if ( in_array( $acf['id'], $field_groups ) ) {
241
+ $fields_tmp = array_merge( $fields_tmp, apply_filters( 'acf/field_group/get_fields', array(), $acf['id'] ) );
242
+ }
243
+ }
244
+ }
245
+ }
246
+
247
+ if ( is_array( $fields_tmp ) ) {
248
+ foreach( $fields_tmp as $field ) {
249
+ if ( isset( $field['name'] ) ) {
250
+ $fields[$field['name']] = $field;
251
+ }
252
+ }
253
+ }
254
+ }
255
+
256
+ return $fields;
257
+ }
258
+
259
+ }
260
+ }
lib/endpoints/class-acf-to-rest-api-option-controller.php ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'ACF_To_REST_API_Option_Controller' ) ) {
8
+ class ACF_To_REST_API_Option_Controller extends ACF_To_REST_API_Controller {
9
+ public function register_routes() {
10
+ register_rest_route( 'acf/v2', "/options/?", array(
11
+ array(
12
+ 'methods' => WP_REST_Server::READABLE,
13
+ 'callback' => array( $this, 'get_item' ),
14
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
15
+ ),
16
+ array(
17
+ 'methods' => WP_REST_Server::EDITABLE,
18
+ 'callback' => array( $this, 'update_item' ),
19
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
20
+ ),
21
+ ) );
22
+
23
+ register_rest_route( 'acf/v2', "/options/(?P<name>[\w\-\_]+)/?", array(
24
+ array(
25
+ 'methods' => WP_REST_Server::READABLE,
26
+ 'callback' => array( $this, 'get_item' ),
27
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
28
+ ),
29
+ array(
30
+ 'methods' => WP_REST_Server::EDITABLE,
31
+ 'callback' => array( $this, 'update_item' ),
32
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
33
+ ),
34
+ ) );
35
+ }
36
+
37
+ public function prepare_item_for_database( $request ) {
38
+ $item = parent::prepare_item_for_database( $request );
39
+
40
+ if ( $item && $request instanceof WP_REST_Request ) {
41
+ $name = $request->get_param( 'name' );
42
+ if ( $name && array_key_exists( $name, $item['data'] ) ) {
43
+ $item['data'] = array( $name => $item['data'][$name] );
44
+ }
45
+ }
46
+
47
+ return $item;
48
+ }
49
+
50
+ public function get_fields( $request, $response = null, $object = null ) {
51
+ if ( $request instanceof WP_REST_Request ) {
52
+ $name = $request->get_param( 'name' );
53
+ if ( $name ) {
54
+ $value = get_field( $name, $this->type );
55
+ $data = array( $name => $value );
56
+ return apply_filters( "acf/rest_api/{$this->type}/get_fields", $data, $request, $response, $object );
57
+ }
58
+ }
59
+
60
+ return parent::get_fields( $request, $response, $object );
61
+ }
62
+ }
63
+ }
lib/endpoints/class-acf-to-rest-api-term-controller.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
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( 'acf/v2', "/{$this->type}/(?P<taxonomy>[\w\-\_]+)/(?P<id>\d+)", array(
11
+ array(
12
+ 'methods' => WP_REST_Server::READABLE,
13
+ 'callback' => array( $this, 'get_item' ),
14
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
15
+ ),
16
+ array(
17
+ 'methods' => WP_REST_Server::EDITABLE,
18
+ 'callback' => array( $this, 'update_item' ),
19
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
20
+ ),
21
+ ) );
22
+ }
23
+
24
+ public function get_item( $request ) {
25
+ if ( self::show( $request ) ) {
26
+ return parent::get_item( $request );
27
+ }
28
+
29
+ return new WP_Error( 'rest_no_route', __( 'No route was found matching the URL and request method', 'acf-to-rest-api' ), array( 'status' => 404 ) );
30
+ }
31
+
32
+ protected static function show( $object ) {
33
+ global $wp_taxonomies;
34
+
35
+ if ( $object instanceof WP_REST_Request ) {
36
+ $taxonomy = $object->get_param( 'taxonomy' );
37
+ } else {
38
+ $taxonomy = false;
39
+ }
40
+
41
+ return $taxonomy && isset( $wp_taxonomies[$taxonomy]->show_in_rest ) && $wp_taxonomies[$taxonomy]->show_in_rest;
42
+ }
43
+ }
44
+ }
readme.md ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
6
+ ====
7
+ 1. Copy the `acf-to-rest-api` folder into your `wp-content/plugins` folder
8
+ 2. Activate the `ACF to REST API` plugin via the plugin admin page
9
+
10
+ Endpoints
11
+ ====
12
+
13
+ | Endpoint | READABLE | EDITABLE |
14
+ |----------|:--------:|:--------:|
15
+ | /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) |
16
+ | /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) |
17
+ | /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) |
18
+ | /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) |
19
+ | /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) |
20
+ | /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) |
21
+ | /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) |
22
+ | /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) |
23
+ | /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) |
24
+
25
+ Filters
26
+ ====
27
+ | Filter | Argument(s) |
28
+ |-----------|-----------|
29
+ | acf/rest_api/types | array **$types** |
30
+ | acf/rest_api/type | string **$type** |
31
+ | acf/rest_api/id | mixed ( string, integer, boolean ) **$id** |
32
+ | acf/rest_api/key | string **$key**<br>WP_REST_Request **$request**<br>string **$type** |
33
+ | acf/rest_api/item_permissions/get | boolean **$permission**<br>WP_REST_Request **$request**<br>string **$type** |
34
+ | acf/rest_api/item_permissions/update | boolean **$permission**<br>WP_REST_Request **$request**<br>string **$type** |
35
+ | acf/rest_api/**{type}**/prepare_item | mixed ( array, boolean ) **$item**<br>WP_REST_Request **$request** |
36
+ | 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** |
37
+
38
+ If you do not want edit/show the fields of posts. So, you must use the filter `acf/rest_api/types`
39
+
40
+ ```PHP
41
+ add_filter( 'acf/rest_api/types', function( $types ) {
42
+ if ( array_key_exists( 'post', $types ) ) {
43
+ unset( $types['post'] );
44
+ }
45
+
46
+ return $types;
47
+ } );
48
+ ```
49
+
50
+ Editing the fields
51
+ ====
52
+ The fields should be sent into the key `fields`.
53
+
54
+ ![Field Name](http://airesgoncalves.com.br/screenshot/acf-to-rest-api/readme/field-name.jpg)
55
+
56
+ **Action:** http://localhost/wp-json/acf/v2/post/1
57
+
58
+ ```HTML
59
+ <form action="http://localhost/wp-json/acf/v2/post/1" method="POST">
60
+ <?php
61
+ // http://v2.wp-api.org/guide/authentication
62
+ wp_nonce_field( 'wp_rest' );
63
+ ?>
64
+ <label>Site: <input type="text" name="fields[site]"></label>
65
+ <button type="submit">Save</button>
66
+ </form>
67
+ ```
68
+
69
+ **Action:** http://localhost/wp-json/wp/v2/posts/1
70
+
71
+ ```HTML
72
+ <form action="http://localhost/wp-json/wp/v2/posts/1" method="POST">
73
+ <?php
74
+ // http://v2.wp-api.org/guide/authentication
75
+ wp_nonce_field( 'wp_rest' );
76
+ ?>
77
+ <label>Title: <input type="text" name="title"></label>
78
+ <h3>ACF</h3>
79
+ <label>Site: <input type="text" name="fields[site]"></label>
80
+ <button type="submit">Save</button>
81
+ </form>
82
+ ```
83
+
84
+ Use the filter `acf/rest_api/key` to change the key `fields`.
85
+
86
+ ```PHP
87
+ add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
88
+ return 'acf_fields';
89
+ }, 10, 3 );
90
+ ```
91
+
92
+ Now, the fields should be sent into the key `acf_fields`
93
+
94
+ ```HTML
95
+ <form action="http://localhost/wp-json/acf/v2/post/1" method="POST">
96
+ <?php
97
+ // http://v2.wp-api.org/guide/authentication
98
+ wp_nonce_field( 'wp_rest' );
99
+ ?>
100
+ <label>Site: <input type="text" name="acf_fields[site]"></label>
101
+ <button type="submit">Save</button>
102
+ </form>
103
+ ```
readme.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.4
6
+ Stable tag: 2.0.3
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
21
+ 2. Activate the `ACF to REST API` plugin via the plugin admin page
22
+
23
+ == Changelog ==
24
+
25
+ = 2.0.3 =
26
+ error fixed when register a new post with acf fields
27
+
28
+ = 2.0.2 =
29
+ adding support for options page ( add-on )
30
+
31
+ = 2.0.1 =
32
+ Bugfix strict standards
33
+
34
+ = 2.0.0 =
35
+ New version of the plugin ACF to WP REST API
36
+ Changing name ACF to WP REST API > ACF to REST API
37
+
38
+ == Upgrade Notice ==
39
+
40
+ = 2.0.0 =
41
+ This version enables editing of the ACF fields with WordPress REST API.