Version Description
adding support for options page ( add-on )
Download this release
Release Info
Developer | airesvsg |
Plugin | ACF to REST API |
Version | 2.0.2 |
Comparing to | |
See all releases |
Version 2.0.2
- class-acf-to-rest-api.php +133 -0
- includes/admin/views/html-notice-missing-acf.php +28 -0
- includes/admin/views/html-notice-missing-rest-api.php +28 -0
- languages/pt_BR.mo +0 -0
- lib/endpoints/class-acf-to-rest-api-attachment-controller.php +14 -0
- lib/endpoints/class-acf-to-rest-api-controller.php +205 -0
- lib/endpoints/class-acf-to-rest-api-option-controller.php +63 -0
- lib/endpoints/class-acf-to-rest-api-term-controller.php +44 -0
- readme.md +103 -0
- readme.txt +38 -0
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.2
|
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.2';
|
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,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
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 |
+
}
|
113 |
+
|
114 |
+
return apply_filters( "acf/rest_api/{$this->type}/prepare_item", $item, $request );
|
115 |
+
}
|
116 |
+
|
117 |
+
protected function get_id( $object ) {
|
118 |
+
$this->id = false;
|
119 |
+
|
120 |
+
if ( is_numeric( $object ) ) {
|
121 |
+
$this->id = $object;
|
122 |
+
} elseif ( is_array( $object ) ) {
|
123 |
+
$object = array_change_key_case( $object, CASE_UPPER );
|
124 |
+
if ( array_key_exists( 'ID', $object ) ) {
|
125 |
+
$this->id = $object['ID'];
|
126 |
+
}
|
127 |
+
} elseif ( is_object( $object ) ) {
|
128 |
+
if( $object instanceof WP_REST_Response ) {
|
129 |
+
return $this->get_id( $object->get_data() );
|
130 |
+
} elseif ( $object instanceof WP_REST_Request ) {
|
131 |
+
$this->id = $object->get_param( 'id' );
|
132 |
+
} elseif ( isset( $object->ID ) ) {
|
133 |
+
$this->id = $object->ID;
|
134 |
+
} elseif ( isset( $object->comment_ID ) ) {
|
135 |
+
$this->id = $object->comment_ID;
|
136 |
+
} elseif ( isset( $object->term_id ) ) {
|
137 |
+
$this->id = $object->term_id;
|
138 |
+
}
|
139 |
+
}
|
140 |
+
|
141 |
+
$this->id = absint( $this->id );
|
142 |
+
|
143 |
+
return $this->id;
|
144 |
+
}
|
145 |
+
|
146 |
+
protected function format_id( $object ) {
|
147 |
+
$this->get_id( $object );
|
148 |
+
|
149 |
+
switch( $this->type ) {
|
150 |
+
case 'comment' :
|
151 |
+
$this->id = 'comment_' . $this->id;
|
152 |
+
break;
|
153 |
+
case 'user' :
|
154 |
+
$this->id = 'user_' . $this->id;
|
155 |
+
break;
|
156 |
+
case 'term' :
|
157 |
+
if ( $object instanceof WP_Term ) {
|
158 |
+
$taxonomy = $object->taxonomy;
|
159 |
+
} elseif ( $object instanceof WP_REST_Request ) {
|
160 |
+
$taxonomy = $object->get_param( 'taxonomy' );
|
161 |
+
}
|
162 |
+
$this->id = $taxonomy . '_' . $this->id;
|
163 |
+
break;
|
164 |
+
case 'option' :
|
165 |
+
$this->id = 'option';
|
166 |
+
break;
|
167 |
+
}
|
168 |
+
|
169 |
+
return apply_filters( 'acf/rest_api/id', $this->id );
|
170 |
+
}
|
171 |
+
|
172 |
+
protected function get_fields( $request, $response = null, $object = null ) {
|
173 |
+
$data = array();
|
174 |
+
$swap = $response instanceof WP_REST_Response;
|
175 |
+
|
176 |
+
if ( $swap ) {
|
177 |
+
$data = $response->get_data();
|
178 |
+
}
|
179 |
+
|
180 |
+
if ( empty( $object ) ) {
|
181 |
+
if ( ! empty( $request ) ) {
|
182 |
+
$object = $request;
|
183 |
+
} elseif ( ! empty( $data ) ) {
|
184 |
+
$object = $response;
|
185 |
+
}
|
186 |
+
}
|
187 |
+
|
188 |
+
$this->format_id( $object );
|
189 |
+
|
190 |
+
if ( $this->id ) {
|
191 |
+
$data['acf'] = get_fields( $this->id );
|
192 |
+
} else {
|
193 |
+
$data['acf'] = array();
|
194 |
+
}
|
195 |
+
|
196 |
+
if ( $swap ) {
|
197 |
+
$response->data = $data;
|
198 |
+
$data = $response;
|
199 |
+
}
|
200 |
+
|
201 |
+
return apply_filters( "acf/rest_api/{$this->type}/get_fields", $data, $request, $response, $object );
|
202 |
+
}
|
203 |
+
|
204 |
+
}
|
205 |
+
}
|
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,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.2
|
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.2 =
|
26 |
+
adding support for options page ( add-on )
|
27 |
+
|
28 |
+
= 2.0.1 =
|
29 |
+
Bugfix strict standards
|
30 |
+
|
31 |
+
= 2.0.0 =
|
32 |
+
New version of the plugin ACF to WP REST API
|
33 |
+
Changing name ACF to WP REST API > ACF to REST API
|
34 |
+
|
35 |
+
== Upgrade Notice ==
|
36 |
+
|
37 |
+
= 2.0.0 =
|
38 |
+
This version enables editing of the ACF fields with WordPress REST API.
|