ACF to REST API - Version 2.0.4

Version Description

fixed error when you register new fields ( via php ) and try save them

Download this release

Release Info

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

Code changes from version 2.0.2 to 2.0.4

class-acf-to-rest-api.php CHANGED
@@ -4,7 +4,7 @@
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.2
8
  * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
  */
10
 
@@ -16,7 +16,7 @@ if ( ! class_exists( 'ACF_To_REST_API' ) ) {
16
 
17
  class ACF_To_REST_API {
18
 
19
- const VERSION = '2.0.2';
20
 
21
  public static function init() {
22
  self::includes();
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.4
8
  * Plugin URI: http://github.com/airesvsg/acf-to-rest-api
9
  */
10
 
16
 
17
  class ACF_To_REST_API {
18
 
19
+ const VERSION = '2.0.4';
20
 
21
  public static function init() {
22
  self::includes();
includes/admin/views/html-notice-missing-acf.php CHANGED
@@ -1,28 +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>
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 CHANGED
@@ -1,28 +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>
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>
lib/endpoints/class-acf-to-rest-api-attachment-controller.php CHANGED
@@ -1,14 +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
- }
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 CHANGED
@@ -18,7 +18,7 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
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
 
@@ -77,36 +77,34 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
77
  }
78
 
79
  public function rest_insert( $object, $request, $creating ) {
 
 
 
 
80
  return $this->update_item( $request );
81
  }
82
 
83
  public function prepare_item_for_database( $request ) {
84
  $item = false;
85
- $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type );
86
-
87
- if ( is_string( $key ) && ! empty( $key ) ) {
88
- $data = $request->get_param( $key );
89
- $this->format_id( $request );
90
- if ( $this->id && is_array( $data ) ) {
91
- $fields = get_field_objects( $this->id );
92
 
93
- if ( ! $fields ) {
94
- if ( function_exists( 'get_field_object' ) ) {
95
- foreach ( array_keys( $data ) as $selector ) {
96
- $field = get_field_object( $selector, $this->id, array( 'load_value' => true ) );
97
- if ( $field ) {
98
- $fields[$selector] = $field;
99
- }
100
- }
101
- }
102
- }
103
 
104
- if ( $fields ) {
105
- $item = array(
106
- 'id' => $this->id,
107
- 'fields' => $fields,
108
- 'data' => $data,
109
- );
 
 
 
 
110
  }
111
  }
112
  }
@@ -201,5 +199,62 @@ if ( ! class_exists( 'ACF_To_REST_API_Controller' ) ) {
201
  return apply_filters( "acf/rest_api/{$this->type}/get_fields", $data, $request, $response, $object );
202
  }
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  }
205
  }
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
 
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 ( is_array( $fields ) && ! empty( $fields ) ) {
102
+ $item = array(
103
+ 'id' => $this->id,
104
+ 'fields' => $fields,
105
+ 'data' => $data,
106
+ );
107
+ }
108
  }
109
  }
110
  }
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 = array();
208
+ $fields_tmp = array();
209
+
210
+ if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) {
211
+
212
+ $field_groups = acf_get_field_groups( array( 'post_id' => $id ) );
213
+
214
+ if ( is_array( $field_groups ) && ! empty( $field_groups ) ) {
215
+ foreach ( $field_groups as $field_group ) {
216
+ $field_group_fields = acf_get_fields( $field_group );
217
+ if ( is_array( $field_group_fields ) && ! empty( $field_group_fields ) ) {
218
+ foreach( array_keys( $field_group_fields ) as $i ) {
219
+ $fields_tmp[] = acf_extract_var( $field_group_fields, $i );
220
+ }
221
+ }
222
+ }
223
+ }
224
+
225
+ } else {
226
+
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 ) && ! empty( $acfs ) && is_array( $field_groups ) && ! empty( $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
+
248
+ if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) {
249
+ foreach( $fields_tmp as $field ) {
250
+ if ( is_array( $field ) && isset( $field['name'] ) ) {
251
+ $fields[$field['name']] = $field;
252
+ }
253
+ }
254
+ }
255
+
256
+ return $fields;
257
+ }
258
+
259
  }
260
  }
lib/endpoints/class-acf-to-rest-api-term-controller.php CHANGED
@@ -1,44 +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
- }
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.txt CHANGED
@@ -3,7 +3,7 @@ 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.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -22,6 +22,12 @@ http://github.com/airesvsg/acf-to-rest-api
22
 
23
  == Changelog ==
24
 
 
 
 
 
 
 
25
  = 2.0.2 =
26
  adding support for options page ( add-on )
27
 
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.4
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
22
 
23
  == Changelog ==
24
 
25
+ = 2.0.4 =
26
+ fixed error when you register new fields ( via php ) and try save them
27
+
28
+ = 2.0.3 =
29
+ error fixed when register a new post with acf fields
30
+
31
  = 2.0.2 =
32
  adding support for options page ( add-on )
33