├── languages └── pt_BR.mo ├── shared ├── includes │ └── admin │ │ ├── views │ │ ├── html-settings-section.php │ │ ├── html-notice-donation.php │ │ ├── html-settings-field.php │ │ ├── html-notice-missing-rest-api.php │ │ └── html-notice-missing-acf.php │ │ └── classes │ │ ├── class-acf-to-rest-api-donation.php │ │ └── class-acf-to-rest-api-settings.php └── assets │ ├── js │ └── acf-to-rest-api-donation.js │ └── css │ └── acf-to-rest-api-donation.css ├── legacy └── v2 │ ├── lib │ └── endpoints │ │ ├── class-acf-to-rest-api-attachment-controller.php │ │ ├── class-acf-to-rest-api-option-controller.php │ │ ├── class-acf-to-rest-api-term-controller.php │ │ └── class-acf-to-rest-api-controller.php │ └── class-acf-to-rest-api-v2.php ├── v3 ├── lib │ ├── endpoints │ │ ├── class-acf-to-rest-api-users-controller.php │ │ ├── class-acf-to-rest-api-comments-controller.php │ │ ├── class-acf-to-rest-api-posts-controller.php │ │ ├── class-acf-to-rest-api-terms-controller.php │ │ ├── class-acf-to-rest-api-attachments-controller.php │ │ ├── class-acf-to-rest-api-options-controller.php │ │ └── class-acf-to-rest-api-controller.php │ ├── class-acf-to-rest-api-acf-field-settings.php │ └── class-acf-to-rest-api-acf-api.php └── class-acf-to-rest-api-v3.php ├── composer.json ├── readme.txt ├── class-acf-to-rest-api.php └── readme.md /languages/pt_BR.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdullahthewebbee/Acf-to-rest-api-master/HEAD/languages/pt_BR.mo -------------------------------------------------------------------------------- /shared/includes/admin/views/html-settings-section.php: -------------------------------------------------------------------------------- 1 | 7 | 8 |

9 | -------------------------------------------------------------------------------- /shared/assets/js/acf-to-rest-api-donation.js: -------------------------------------------------------------------------------- 1 | ( function( $ ) { 2 | $( '.acf-to-rest-api-donation-button-notice-dismiss' ).click( function( e ) { 3 | e.preventDefault(); 4 | $( this ).closest('.acf-to-rest-api-donation-notice').slideUp(); 5 | $.post( acf_to_rest_api_donation.ajax_url, { 6 | action: 'acf_to_rest_api_dismiss_notice', 7 | nonce: acf_to_rest_api_donation.nonce 8 | } ); 9 | } ); 10 | } )( jQuery ); -------------------------------------------------------------------------------- /legacy/v2/lib/endpoints/class-acf-to-rest-api-attachment-controller.php: -------------------------------------------------------------------------------- 1 | type = 'attachment'; 11 | parent::register_hooks(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-users-controller.php: -------------------------------------------------------------------------------- 1 | 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 | } 21 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-comments-controller.php: -------------------------------------------------------------------------------- 1 | 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 | } 21 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-posts-controller.php: -------------------------------------------------------------------------------- 1 | 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 | } 21 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-terms-controller.php: -------------------------------------------------------------------------------- 1 | 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 | } 21 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-attachments-controller.php: -------------------------------------------------------------------------------- 1 | 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 | } 21 | -------------------------------------------------------------------------------- /shared/includes/admin/views/html-notice-donation.php: -------------------------------------------------------------------------------- 1 | 8 | 9 |
10 |

{ }

11 | 12 |
13 | -------------------------------------------------------------------------------- /shared/includes/admin/views/html-settings-field.php: -------------------------------------------------------------------------------- 1 | 8 | 9 |
10 | 11 | 15 |

16 |
17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 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.3.3", 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": "GPL-2.0-only", 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 | } 26 | -------------------------------------------------------------------------------- /legacy/v2/lib/endpoints/class-acf-to-rest-api-option-controller.php: -------------------------------------------------------------------------------- 1 | rest_base = 'options'; 12 | } 13 | 14 | public function register_routes() { 15 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/?(?P[\w\-\_]+)?', array( 16 | array( 17 | 'methods' => WP_REST_Server::READABLE, 18 | 'callback' => array( $this, 'get_item' ), 19 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), 20 | ), 21 | array( 22 | 'methods' => WP_REST_Server::EDITABLE, 23 | 'callback' => array( $this, 'update_item' ), 24 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), 25 | ), 26 | ) ); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-options-controller.php: -------------------------------------------------------------------------------- 1 | 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[\w\-\_]+)/?(?P[\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 | } 32 | -------------------------------------------------------------------------------- /shared/includes/admin/views/html-notice-missing-rest-api.php: -------------------------------------------------------------------------------- 1 | 24 | 25 |
26 |

27 |

target="_blank">

28 |
29 | -------------------------------------------------------------------------------- /shared/includes/admin/views/html-notice-missing-acf.php: -------------------------------------------------------------------------------- 1 | 24 | 25 |
26 |

27 |

target="_blank">

28 |
29 | -------------------------------------------------------------------------------- /shared/assets/css/acf-to-rest-api-donation.css: -------------------------------------------------------------------------------- 1 | .acf-to-rest-api-donation-notice { 2 | background: #fff; 3 | border: 0; 4 | border-radius: 4px; 5 | color: #cd331f; 6 | padding: 1px 5px; 7 | position: relative; 8 | } 9 | 10 | .acf-to-rest-api-donation-button-notice-dismiss:focus { 11 | box-shadow: none; 12 | } 13 | 14 | .acf-to-rest-api-donation-button-notice-dismiss:hover { 15 | opacity: .5; 16 | } 17 | 18 | .acf-to-rest-api-donation-button-notice-dismiss:before { 19 | color: #FF8676; 20 | } 21 | 22 | .acf-to-rest-api-donation-plugin-name, 23 | .acf-to-rest-api-donation-button { 24 | background: #fff; 25 | border: 1px solid #cd331f; 26 | border-radius: 4px; 27 | color: #cd331f; 28 | margin-right: 5px; 29 | padding: 2px 15px; 30 | text-decoration: none; 31 | } 32 | 33 | .acf-to-rest-api-donation-plugin-name, 34 | .acf-to-rest-api-donation-button:hover { 35 | background: #cd331f; 36 | color: #fff; 37 | } 38 | 39 | .acf-to-rest-api-donation-plugin-name { 40 | font-weight: bold; 41 | } 42 | 43 | .acf-to-rest-api-donation-button:hover .dashicons-heart { 44 | animation: .8s infinite heart_beat; 45 | } 46 | 47 | .acf-to-rest-api-donation-button:focus { 48 | box-shadow: none; 49 | } 50 | 51 | @keyframes heart_beat { 52 | 0% { 53 | transform: scale(1); 54 | } 55 | 25% { 56 | transform: scale(1.1); 57 | } 58 | 40% { 59 | transform: scale(1); 60 | } 61 | 60% { 62 | transform: scale(1.1); 63 | } 64 | 100% { 65 | transform: scale(1); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /legacy/v2/lib/endpoints/class-acf-to-rest-api-term-controller.php: -------------------------------------------------------------------------------- 1 | namespace, '/' . $this->type . '/(?P[\w\-\_]+)/(?P[\d]+)/?(?P[\w\-\_]+)?', 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 ( $this->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 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; 45 | } 46 | } 47 | } 48 | 49 | return $taxonomy; 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 | } 59 | -------------------------------------------------------------------------------- /legacy/v2/class-acf-to-rest-api-v2.php: -------------------------------------------------------------------------------- 1 | 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 | } 54 | -------------------------------------------------------------------------------- /v3/class-acf-to-rest-api-v3.php: -------------------------------------------------------------------------------- 1 | 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 | } 57 | -------------------------------------------------------------------------------- /shared/includes/admin/classes/class-acf-to-rest-api-donation.php: -------------------------------------------------------------------------------- 1 | admin_url( 'admin-ajax.php' ), 38 | 'nonce' => wp_create_nonce( self::$action_nonce ), 39 | ) ); 40 | } 41 | 42 | public static function donation_notice() { 43 | include dirname( __FILE__ ) . '/../views/html-notice-donation.php'; 44 | } 45 | 46 | public static function dismiss_notice() { 47 | if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], self::$action_nonce ) ) { 48 | wp_die(); 49 | } 50 | 51 | $user_id = get_current_user_id(); 52 | if ( $user_id ) { 53 | update_user_meta( $user_id, self::$version_meta, ACF_To_REST_API::VERSION ); 54 | } 55 | 56 | exit; 57 | } 58 | 59 | private static function show() { 60 | $version = null; 61 | $user_id = get_current_user_id(); 62 | 63 | if ( $user_id ) { 64 | $version = get_user_meta( $user_id, self::$version_meta, true ); 65 | } 66 | 67 | return is_admin() && $version !== ACF_To_REST_API::VERSION; 68 | } 69 | 70 | } 71 | 72 | add_action( 'admin_init', array( 'ACF_To_REST_API_Donation', 'init' ) ); 73 | 74 | } 75 | -------------------------------------------------------------------------------- /shared/includes/admin/classes/class-acf-to-rest-api-settings.php: -------------------------------------------------------------------------------- 1 | sprintf( '%s', admin_url( 'options-permalink.php#acf-to-rest-api-settings' ), esc_html__( 'Settings', 'acf-to-rest-api' ) ), 43 | ); 44 | 45 | $new_actions += $actions; 46 | 47 | return $new_actions; 48 | } 49 | 50 | return $actions; 51 | } 52 | 53 | public static function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) { 54 | if ( isset( $plugin_data['slug'] ) && 'acf-to-rest-api' == $plugin_data['slug'] ) { 55 | $plugin_meta['acf-to-rest-api-github'] = sprintf( '%s', self::$github_url, esc_html__( 'Fork me on GitHub' ) ); 56 | $plugin_meta['acf_to_rest_api_donation'] = sprintf( ' %s', ACF_To_REST_API_Donation::DONATION_URL, esc_html__( 'Make a donation', 'acf-to-rest-api' ) ); 57 | } 58 | 59 | return $plugin_meta; 60 | } 61 | 62 | public static function acf_admin_setting() { 63 | add_settings_section( 64 | 'acf_to_rest_api_settings_section', 65 | __( 'ACF to REST API', 'acf-to-rest-api' ), 66 | array( __CLASS__, 'add_settings_section' ), 67 | 'permalink' 68 | ); 69 | 70 | add_settings_field( 71 | 'acf_to_rest_api_request_version', 72 | __( 'Request Version', 'acf-to-rest-api' ), 73 | array( __CLASS__, 'add_settings_field' ), 74 | 'permalink', 75 | 'acf_to_rest_api_settings_section' 76 | ); 77 | } 78 | 79 | private static function save() { 80 | if ( ! is_admin() ) { 81 | return; 82 | } 83 | 84 | if ( isset( $_POST['acf_to_rest_api_settings'] ) ) { 85 | $settings = $_POST['acf_to_rest_api_settings']; 86 | if ( array_key_exists( 'request_version', $settings ) ) { 87 | update_option( 'acf_to_rest_api_request_version', absint( $settings['request_version'] ) ); 88 | } 89 | } 90 | } 91 | 92 | } 93 | 94 | ACF_To_REST_API_Settings::init(); 95 | 96 | } 97 | -------------------------------------------------------------------------------- /v3/lib/class-acf-to-rest-api-acf-field-settings.php: -------------------------------------------------------------------------------- 1 | __( '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' => 0, 39 | ), true ); 40 | } else { ?> 41 | 42 | 43 | 44 | 45 | 46 | '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 | 61 | 62 | __( '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' => 0, 76 | ), true ); 77 | } else { ?> 78 | 79 | 80 | 81 | 82 | 83 | '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 | 98 | 99 | ACF to REST API 104 | 105 | == Upgrade Notice == 106 | 107 | = 2.0.0 = 108 | This version enables editing of the ACF fields with WordPress REST API. -------------------------------------------------------------------------------- /class-acf-to-rest-api.php: -------------------------------------------------------------------------------- 1 | includes(); 55 | } 56 | } 57 | 58 | public static function handle_request_version() { 59 | if ( is_null( self::$request_version ) ) { 60 | if ( defined( 'ACF_TO_REST_API_REQUEST_VERSION' ) ) { 61 | self::$request_version = (int) ACF_TO_REST_API_REQUEST_VERSION; 62 | } else { 63 | self::$request_version = (int) get_option( 'acf_to_rest_api_request_version', self::$default_request_version ); 64 | } 65 | } 66 | return self::$request_version; 67 | } 68 | 69 | private static function hooks() { 70 | $acf_plugin_version = get_option( 'acf_version' ); 71 | $hook_type = $acf_plugin_version >= '5.12' ? 'rest_pre_dispatch' : 'rest_api_init'; 72 | 73 | add_action( 'init', array( __CLASS__, 'load_plugin_textdomain' ) ); 74 | if ( self::is_plugin_active( 'all' ) ) { 75 | add_action( $hook_type, array( __CLASS__, 'create_rest_routes' ), 10 ); 76 | if ( self::$default_request_version == self::handle_request_version() ) { 77 | ACF_To_REST_API_ACF_Field_Settings::hooks(); 78 | } 79 | } else { 80 | add_action( 'admin_notices', array( __CLASS__, 'missing_notice' ) ); 81 | } 82 | } 83 | 84 | public static function load_plugin_textdomain() { 85 | $locale = apply_filters( 'plugin_locale', get_locale(), 'acf-to-rest-api' ); 86 | load_textdomain( 'acf-to-rest-api', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/languages/' . $locale . '.mo' ); 87 | } 88 | 89 | public static function create_rest_routes() { 90 | self::instance()->create_rest_routes(); 91 | } 92 | 93 | public static function is_plugin_active( $plugin ) { 94 | if ( 'rest-api' == $plugin ) { 95 | return class_exists( 'WP_REST_Controller' ); 96 | } elseif ( 'acf' == $plugin ) { 97 | return class_exists( 'acf' ); 98 | } elseif ( 'all' == $plugin ) { 99 | return class_exists( 'WP_REST_Controller' ) && class_exists( 'acf' ); 100 | } 101 | 102 | return false; 103 | } 104 | 105 | public static function is_plugin_installed( $plugin ) { 106 | if ( ! function_exists( 'get_plugins' ) ) { 107 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 108 | } 109 | 110 | $paths = false; 111 | if ( 'rest-api' == $plugin ) { 112 | $paths = array( 'rest-api/plugin.php' ); 113 | } elseif ( 'acf' == $plugin ) { 114 | $paths = array( 'advanced-custom-fields-pro/acf.php', 'acf-pro/acf.php', 'advanced-custom-fields/acf.php' ); 115 | } 116 | 117 | if ( $paths ) { 118 | $plugins = get_plugins(); 119 | if ( is_array( $plugins ) && count( $plugins ) > 0 ) { 120 | foreach ( $paths as $path ) { 121 | if ( isset( $plugins[ $path ] ) && ! empty( $plugins[ $path ] ) ) { 122 | return $path; 123 | } 124 | } 125 | } 126 | } 127 | 128 | return false; 129 | } 130 | 131 | public static function missing_notice() { 132 | self::instance()->missing_notice(); 133 | } 134 | } 135 | 136 | add_action( 'plugins_loaded', array( 'ACF_To_REST_API', 'init' ) ); 137 | 138 | } 139 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ACF to REST API 2 | ==== 3 | Exposes [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 🆕](#deprecated-filters) 11 | - [Request API Version 🆕](#request-api-version) 12 | - [Field Settings 🆕](#field-settings) 13 | - [Editing the Fields](#editing-the-fields) 14 | - [Examples](#examples) 15 | - [Get ACF Fields Recursively 🆕](#get-acf-fields-recursively) 16 | - [Cache](#cache) 17 | 18 | Installation 19 | ==== 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 | Endpoints 24 | ==== 25 | 26 | | Endpoint | READABLE | EDITABLE | 27 | |----------|:--------:|:--------:| 28 | | /wp-json/acf/v3/posts 🆕 | ✅ | ❌ | 29 | | /wp-json/acf/v3/posts/**{id}** | ✅ | ✅ | 30 | | /wp-json/acf/v3/posts/**{id}**/**{field-name}** | ✅ | ✅ | 31 | | /wp-json/acf/v3/pages 🆕 | ✅ | ❌ | 32 | | /wp-json/acf/v3/pages/**{id}** | ✅ | ✅ | 33 | | /wp-json/acf/v3/pages/**{id}**/**{field-name}** | ✅ | ✅ | 34 | | /wp-json/acf/v3/users 🆕 | ✅ | ❌ | 35 | | /wp-json/acf/v3/users/**{id}** | ✅ | ✅ | 36 | | /wp-json/acf/v3/users/**{id}**/**{field-name}** | ✅ | ✅ | 37 | | /wp-json/acf/v3/**{taxonomy}** 🆕 | ✅ | ❌ | 38 | | /wp-json/acf/v3/**{taxonomy}**/**{id}** 🆕 | ✅ | ✅ | 39 | | /wp-json/acf/v3/**{taxonomy}**/**{id}**/**{field-name}** 🆕 | ✅ | ✅ | 40 | | /wp-json/acf/v3/comments 🆕 | ✅ | ❌ | 41 | | /wp-json/acf/v3/comments/**{id}** | ✅ | ✅ | 42 | | /wp-json/acf/v3/comments/**{id}**/**{field-name}** | ✅ | ✅ | 43 | | /wp-json/acf/v3/media 🆕 | ✅ | ❌ | 44 | | /wp-json/acf/v3/media/**{id}** | ✅ | ✅ | 45 | | /wp-json/acf/v3/media/**{id}**/**{field-name}** | ✅ | ✅ | 46 | | /wp-json/acf/v3/**{post-type}** 🆕 | ✅ | ❌ | 47 | | /wp-json/acf/v3/**{post-type}**/**{id}** 🆕 | ✅ | ✅ | 48 | | /wp-json/acf/v3/**{post-type}**/**{id}**/**{field-name}** 🆕 | ✅ | ✅ | 49 | | /wp-json/acf/v3/options/**{id}** 🆕 | ✅ | ✅ | 50 | | /wp-json/acf/v3/options/**{id}**/**{field-name}** 🆕 | ✅ | ✅ | 51 | 52 | Filters 53 | ==== 54 | | Filter | Argument(s) | 55 | |-----------|-----------| 56 | | acf/rest_api/id | mixed ( string, integer, boolean ) **$id**
string **$type** 🆕
string **$controller** 🆕 | 57 | | acf/rest_api/key | string **$key**
WP_REST_Request **$request**
string **$type** | 58 | | acf/rest_api/item_permissions/get | boolean **$permission**
WP_REST_Request **$request**
string **$type** | 59 | | acf/rest_api/item_permissions/update | boolean **$permission**
WP_REST_Request **$request**
string **$type** | 60 | | acf/rest_api/**{type}**/prepare_item | mixed ( array, boolean ) **$item**
WP_REST_Request **$request** | 61 | | acf/rest_api/**{type}**/get_fields | mixed ( array, WP_REST_Request ) **$data**
mixed ( WP_REST_Request, NULL ) **$request** | 62 | | acf/rest_api/field_settings/show_in_rest 🆕 | boolean **$show** | 63 | | acf/rest_api/field_settings/edit_in_rest 🆕 | boolean **$edit** | 64 | 65 | Basic example of how to use the filters, in this case I will set a new permission to get the fields 66 | ```PHP 67 | add_filter( 'acf/rest_api/item_permissions/get', function( $permission ) { 68 | return current_user_can( 'edit_posts' ); 69 | } ); 70 | ``` 71 | 72 | Deprecated filters 73 | ==== 74 | | Filter | Argument(s) | 75 | |-----------|-----------| 76 | | acf/rest_api/type | string **$type** | 77 | | acf/rest_api/types | array **$types** | 78 | | acf/rest_api/default_rest_base | boolean **$default**
string **$type** | 79 | 80 | Request API version 81 | ==== 82 | See below how to select the Request API Version. 83 | 84 | 1. Open the plugins page; 85 | 2. Click the settings link under the pluing name ( `ACF to REST API` ); 86 | 3. Select your version in the `ACF to REST API` session; 87 | 4. Click in the button Save changes. 88 | 89 | The other alternative is to define the constant `ACF_TO_REST_API_REQUEST_VERSION` in your `wp-config.php` 90 | 91 | ```PHP 92 | define( 'ACF_TO_REST_API_REQUEST_VERSION', 2 ); 93 | ``` 94 | 95 | Field Settings 96 | ==== 97 | In this version is possible to configure the field options via admin. 98 | 99 | The options are enabled using the filters below, by default theses options are disabled. 100 | 101 | ```PHP 102 | // Enable the option show in rest 103 | add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' ); 104 | 105 | // Enable the option edit in rest 106 | add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' ); 107 | ``` 108 | 109 | Editing the fields 110 | ==== 111 | The fields should be sent into the key `fields`. 112 | 113 | **Action:** http://localhost/wp-json/acf/v3/posts/1 114 | 115 | ```HTML 116 |
117 | 121 | 122 | 123 |
124 | ``` 125 | 126 | **Action:** http://localhost/wp-json/wp/v2/posts/1 127 | 128 | ```HTML 129 |
130 | 134 | 135 |

ACF

136 | 137 | 138 |
139 | ``` 140 | 141 | Use the filter `acf/rest_api/key` to change the key `fields`. 142 | 143 | ```PHP 144 | add_filter( 'acf/rest_api/key', function( $key, $request, $type ) { 145 | return 'acf_fields'; 146 | }, 10, 3 ); 147 | ``` 148 | 149 | Now, the fields should be sent into the key `acf_fields` 150 | 151 | ```HTML 152 |
153 | 157 | 158 | 159 |
160 | ``` 161 | -------------------------------------------------------------------------------- /v3/lib/class-acf-to-rest-api-acf-api.php: -------------------------------------------------------------------------------- 1 | type = $type; 16 | $this->controller = $controller; 17 | } 18 | 19 | protected function format_id() { 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( $object ) { 42 | $this->id = false; 43 | 44 | if ( is_numeric( $object ) ) { 45 | $this->id = $object; 46 | } elseif ( is_array( $object ) && isset( $object['id'] ) ) { 47 | $this->id = $object['id']; 48 | } elseif ( is_object( $object ) ) { 49 | if ( $object instanceof WP_REST_Response ) { 50 | $data = $object->get_data(); 51 | if ( isset( $data['id'] ) ) { 52 | $this->id = $data['id']; 53 | } 54 | } elseif ( $object instanceof WP_REST_Request ) { 55 | $this->id = $object->get_param( 'id' ); 56 | } elseif ( isset( $object->ID ) ) { 57 | $this->id = $object->ID; 58 | } elseif ( isset( $object->comment_ID ) ) { 59 | $this->id = $object->comment_ID; 60 | } elseif ( isset( $object->term_id ) ) { 61 | $this->id = $object->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(); 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 | $is_option = 'option' == $this->type && 'options' == $this->id; 84 | if ( $field && ( $is_option || ! $is_option && $this->id > 0 ) ) { 85 | $data[ $field ] = get_field( $field, $this->id ); 86 | } else { 87 | $fields = get_fields( $this->id ); 88 | if ( ! $fields ) { 89 | $this->get_field_objects( $this->id ); 90 | $fields = $this->get_fields_fallback(); 91 | } 92 | $data['acf'] = $fields; 93 | } 94 | 95 | if ( apply_filters( 'acf/rest_api/field_settings/show_in_rest', false ) && $this->get_field_objects( $this->id ) ) { 96 | if ( $field ) { 97 | $this->show_in_rest( $data, $field, $this->field_objects ); 98 | } else { 99 | foreach ( array_keys( $data['acf'] ) as $key ) { 100 | $this->show_in_rest( $data['acf'], $key, $this->field_objects ); 101 | } 102 | } 103 | } 104 | } else { 105 | $data['acf'] = array(); 106 | } 107 | 108 | return apply_filters( 'acf/rest_api/' . $this->type . '/get_fields', $data, $request ); 109 | } 110 | 111 | protected function get_fields_fallback() { 112 | $fields = array(); 113 | 114 | if ( ! empty( $this->field_objects ) ) { 115 | foreach ( $this->field_objects as $objects ) { 116 | if( isset( $objects['name'] ) && ! empty( $objects['name'] ) ) { 117 | $fields[ $objects['name'] ] = get_field( $objects['name'], $this->id ); 118 | } 119 | } 120 | } 121 | 122 | return $fields; 123 | } 124 | 125 | public function get_field_objects( $id ) { 126 | if ( empty( $id ) ) { 127 | return false; 128 | } 129 | 130 | $this->field_objects = false; 131 | $fields_tmp = array(); 132 | 133 | if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) && function_exists( 'acf_extract_var' ) ) { 134 | $field_groups = acf_get_field_groups( array( 'post_id' => $id ) ); 135 | 136 | if ( is_array( $field_groups ) && ! empty( $field_groups ) ) { 137 | foreach ( $field_groups as $field_group ) { 138 | $field_group_fields = acf_get_fields( $field_group ); 139 | if ( is_array( $field_group_fields ) && ! empty( $field_group_fields ) ) { 140 | foreach ( array_keys( $field_group_fields ) as $i ) { 141 | $fields_tmp[] = acf_extract_var( $field_group_fields, $i ); 142 | } 143 | } 144 | } 145 | } 146 | } else { 147 | if ( strpos( $id, 'user_' ) !== false ) { 148 | $filter = array( 'ef_user' => str_replace( 'user_', '', $id ) ); 149 | } elseif ( strpos( $id, 'taxonomy_' ) !== false ) { 150 | $filter = array( 'ef_taxonomy' => str_replace( 'taxonomy_', '', $id ) ); 151 | } else { 152 | $filter = array( 'post_id' => $id ); 153 | } 154 | 155 | $field_groups = apply_filters( 'acf/location/match_field_groups', array(), $filter ); 156 | $acfs = apply_filters( 'acf/get_field_groups', array() ); 157 | 158 | if ( is_array( $acfs ) && ! empty( $acfs ) && is_array( $field_groups ) && ! empty( $field_groups ) ) { 159 | foreach ( $acfs as $acf ) { 160 | if ( in_array( $acf['id'], $field_groups ) ) { 161 | $fields_tmp = array_merge( $fields_tmp, apply_filters( 'acf/field_group/get_fields', array(), $acf['id'] ) ); 162 | } 163 | } 164 | } 165 | } 166 | 167 | if ( is_array( $fields_tmp ) && ! empty( $fields_tmp ) ) { 168 | $this->field_objects = array(); 169 | foreach ( $fields_tmp as $field ) { 170 | if ( is_array( $field ) && isset( $field['name'] ) ) { 171 | $this->field_objects[ $field['name'] ] = $field; 172 | } 173 | } 174 | } 175 | 176 | return $this->field_objects; 177 | } 178 | 179 | public function show_in_rest( &$data, $field, $field_objects ) { 180 | if ( ! array_key_exists( $field, $field_objects ) || ! isset( $field_objects[ $field ]['show_in_rest'] ) || ! $field_objects[ $field ]['show_in_rest'] ) { 181 | unset( $data[ $field ] ); 182 | } 183 | } 184 | 185 | public function edit_in_rest( $field ) { 186 | return ! ( apply_filters( 'acf/rest_api/field_settings/edit_in_rest', false ) && isset( $field['edit_in_rest'] ) && ! $field['edit_in_rest'] ); 187 | } 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /v3/lib/endpoints/class-acf-to-rest-api-controller.php: -------------------------------------------------------------------------------- 1 | 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[\d]+)/?(?P[\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' => array( $this, 'register_field_callback' ), 54 | 'schema' => array( 55 | 'description' => __( 'Expose advanced custom fields.', 'acf-to-rest-api' ), 56 | 'type' => 'object', 57 | ), 58 | ) ); 59 | } 60 | 61 | public function register_field_callback( $data ) { 62 | $fields = $this->acf->get_fields( $data ); 63 | return $fields['acf']; 64 | } 65 | 66 | public function register() { 67 | $this->register_routes(); 68 | $this->register_hooks(); 69 | $this->register_field(); 70 | } 71 | 72 | public function get_item( $request ) { 73 | $fields = $this->acf->get_fields( $request ); 74 | return rest_ensure_response( $fields ); 75 | } 76 | 77 | public function get_item_permissions_check( $request ) { 78 | return apply_filters( 'acf/rest_api/item_permissions/get', true, $request, $this->type ); 79 | } 80 | 81 | public function get_items( $request ) { 82 | if ( ! method_exists( $this->controller, 'get_items' ) ) { 83 | return new WP_Error( 'cant_get_items', __( 'Cannot get items', 'acf-to-rest-api' ), array( 'status' => 500 ) ); 84 | } 85 | 86 | $this->set_default_parameters( $request ); 87 | $data = $this->controller->get_items( $request )->get_data(); 88 | 89 | $response = array(); 90 | if ( ! empty( $data ) ) { 91 | foreach ( $data as $v ) { 92 | if ( isset( $v['acf'] ) ) { 93 | $response[] = array( 94 | 'id' => $v['id'], 95 | 'acf' => $v['acf'], 96 | ); 97 | } 98 | } 99 | } 100 | 101 | return apply_filters( 'acf/rest_api/' . $this->type . '/get_items', rest_ensure_response( $response ), rest_ensure_request( $request ) ); 102 | } 103 | 104 | public function get_items_permissions_check( $request ) { 105 | return apply_filters( 'acf/rest_api/items_permissions/get', true, $request, $this->type ); 106 | } 107 | 108 | public function update_item_permissions_check( $request ) { 109 | return apply_filters( 'acf/rest_api/item_permissions/update', current_user_can( 'edit_posts' ), $request, $this->type ); 110 | } 111 | 112 | public function update_item( $request ) { 113 | $item = $this->prepare_item_for_database( $request ); 114 | if ( is_array( $item ) && count( $item ) > 0 ) { 115 | foreach ( $item['data'] as $key => $value ) { 116 | if ( isset( $item['fields'][ $key ]['key'] ) ) { 117 | $field = $item['fields'][ $key ]; 118 | $edit = $this->acf->edit_in_rest( $field ); 119 | if ( $edit ) { 120 | if ( function_exists( 'acf_update_value' ) ) { 121 | acf_update_value( $value, $item['id'], $field ); 122 | } elseif ( function_exists( 'update_field' ) ) { 123 | update_field( $field['key'], $value, $item['id'] ); 124 | } else { 125 | do_action( 'acf/update_value', $value, $item['id'], $field ); 126 | } 127 | } 128 | } 129 | } 130 | 131 | return new WP_REST_Response( $this->acf->get_fields( $request ), 200 ); 132 | } 133 | 134 | return new WP_Error( 'cant_update_item', __( 'Cannot update item', 'acf-to-rest-api' ), array( 'status' => 500 ) ); 135 | } 136 | 137 | public function rest_insert( $object, $request, $creating ) { 138 | if ( $request instanceof WP_REST_Request ) { 139 | $id = $this->acf->get_id( $object ); 140 | if ( ! $id ) { 141 | $id = $this->acf->get_id( $request ); 142 | } 143 | $request->set_param( 'id', $id ); 144 | } 145 | 146 | return $this->update_item( $request ); 147 | } 148 | 149 | public function prepare_item_for_database( $request ) { 150 | $item = false; 151 | if ( $request instanceof WP_REST_Request ) { 152 | $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type ); 153 | if ( is_string( $key ) && ! empty( $key ) ) { 154 | $data = $request->get_param( $key ); 155 | $field = $request->get_param( 'field' ); 156 | $id = $this->acf->get_id( $request ); 157 | if ( $id && is_array( $data ) ) { 158 | $fields = $this->acf->get_field_objects( $id ); 159 | if ( is_array( $fields ) && ! empty( $fields ) ) { 160 | if ( $field && isset( $data[ $field ] ) ) { 161 | $data = array( $field => $data[ $field ] ); 162 | } 163 | $item = array( 164 | 'id' => $id, 165 | 'fields' => $fields, 166 | 'data' => $data, 167 | ); 168 | } 169 | } 170 | } 171 | } 172 | return apply_filters( 'acf/rest_api/' . $this->type . '/prepare_item', $item, $request ); 173 | } 174 | 175 | protected function set_default_parameters( &$request ) { 176 | if ( $request instanceof WP_REST_Request ) { 177 | $params = $request->get_params(); 178 | foreach ( self::$default_params as $k => $v ) { 179 | if ( ! isset( $params[ $k ] ) ) { 180 | $request->set_param( $k, $v ); 181 | } 182 | } 183 | } 184 | } 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /legacy/v2/lib/endpoints/class-acf-to-rest-api-controller.php: -------------------------------------------------------------------------------- 1 | type = apply_filters( 'acf/rest_api/type', $type ); 16 | $this->namespace = 'acf/v2'; 17 | $this->rest_base = $this->get_rest_base( $this->type ); 18 | } 19 | 20 | public function register_hooks() { 21 | if ( $this->type ) { 22 | add_filter( 'rest_prepare_' . $this->type, array( $this, 'rest_prepare' ), 10, 3 ); 23 | add_action( 'rest_insert_' . $this->type, array( $this, 'rest_insert' ), 10, 3 ); 24 | } 25 | } 26 | 27 | public function register_routes() { 28 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)/?(?P[\w\-\_]+)?', array( 29 | array( 30 | 'methods' => WP_REST_Server::READABLE, 31 | 'callback' => array( $this, 'get_item' ), 32 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), 33 | ), 34 | array( 35 | 'methods' => WP_REST_Server::EDITABLE, 36 | 'callback' => array( $this, 'update_item' ), 37 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), 38 | ), 39 | ) ); 40 | } 41 | 42 | 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; 52 | } 53 | 54 | public function get_item( $request ) { 55 | return $this->get_fields( $request ); 56 | } 57 | 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 | } 69 | 70 | public function update_item( $request ) { 71 | $item = $this->prepare_item_for_database( $request ); 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' ) ) { 80 | update_field( $field['key'], $value, $item['id'] ); 81 | } else { 82 | do_action( 'acf/update_value', $value, $item['id'], $field ); 83 | } 84 | } 85 | } 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 | 93 | public function rest_insert( $object, $request, $creating ) { 94 | if ( $request instanceof WP_REST_Request && ! $this->get_id( $request ) && $this->get_id( $object ) ) { 95 | $request->set_param( 'id', $this->id ); 96 | } 97 | 98 | return $this->update_item( $request ); 99 | } 100 | 101 | public function prepare_item_for_database( $request ) { 102 | $item = false; 103 | 104 | if ( $request instanceof WP_REST_Request ) { 105 | $key = apply_filters( 'acf/rest_api/key', 'fields', $request, $this->type ); 106 | 107 | if ( is_string( $key ) && ! empty( $key ) ) { 108 | $data = $request->get_param( $key ); 109 | $field = $request->get_param( 'field' ); 110 | 111 | $this->format_id( $request ); 112 | 113 | if ( $this->id && is_array( $data ) ) { 114 | $fields = $this->get_field_objects( $this->id ); 115 | 116 | if ( is_array( $fields ) && ! empty( $fields ) ) { 117 | if ( $field && isset( $data[ $field ] ) ) { 118 | $data = array( $field => $data[ $field ] ); 119 | } 120 | 121 | $item = array( 122 | 'id' => $this->id, 123 | 'fields' => $fields, 124 | 'data' => $data, 125 | ); 126 | } 127 | } 128 | } 129 | } 130 | 131 | return apply_filters( 'acf/rest_api/' . $this->type . '/prepare_item', $item, $request ); 132 | } 133 | 134 | protected function get_id( $object ) { 135 | $this->id = false; 136 | 137 | if ( is_numeric( $object ) ) { 138 | $this->id = $object; 139 | } elseif ( is_array( $object ) ) { 140 | $object = array_change_key_case( $object, CASE_UPPER ); 141 | if ( array_key_exists( 'ID', $object ) ) { 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' ); 149 | } elseif ( isset( $object->ID ) ) { 150 | $this->id = $object->ID; 151 | } elseif ( isset( $object->comment_ID ) ) { 152 | $this->id = $object->comment_ID; 153 | } elseif ( isset( $object->term_id ) ) { 154 | $this->id = $object->term_id; 155 | } 156 | } 157 | 158 | $this->id = absint( $this->id ); 159 | 160 | return $this->id; 161 | } 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; 170 | case 'user' : 171 | $this->id = 'user_' . $this->id; 172 | break; 173 | case 'term' : 174 | if ( $object instanceof WP_Term ) { 175 | $taxonomy = $object->taxonomy; 176 | } elseif ( $object instanceof WP_REST_Request ) { 177 | $taxonomy = $object->get_param( 'taxonomy' ); 178 | } 179 | $this->id = $taxonomy . '_' . $this->id; 180 | break; 181 | case 'option' : 182 | $this->id = 'options'; 183 | break; 184 | } 185 | 186 | $this->id = apply_filters( 'acf/rest_api/id', $this->id ); 187 | 188 | return $this->id; 189 | } 190 | 191 | protected function get_fields( $request, $response = null, $object = null ) { 192 | $data = array(); 193 | $field = null; 194 | $swap = $response instanceof WP_REST_Response; 195 | 196 | if ( $request instanceof WP_REST_Request ) { 197 | $field = $request->get_param( 'field' ); 198 | } 199 | 200 | if ( $swap ) { 201 | $data = $response->get_data(); 202 | } 203 | 204 | if ( empty( $object ) ) { 205 | if ( ! empty( $request ) ) { 206 | $object = $request; 207 | } elseif ( ! empty( $data ) ) { 208 | $object = $response; 209 | } 210 | } 211 | 212 | $this->format_id( $object ); 213 | 214 | if ( $this->id ) { 215 | if ( $field ) { 216 | $data = array( $field => get_field( $field, $this->id ) ); 217 | } else { 218 | $data['acf'] = get_fields( $this->id ); 219 | } 220 | } else { 221 | $data['acf'] = array(); 222 | } 223 | 224 | if ( $swap ) { 225 | $response->data = $data; 226 | $data = $response; 227 | } 228 | 229 | return apply_filters( 'acf/rest_api/' . $this->type . '/get_fields', $data, $request, $response, $object ); 230 | } 231 | 232 | protected function get_field_objects( $id ) { 233 | if ( empty( $id ) ) { 234 | return false; 235 | } 236 | 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 | } 251 | } 252 | } 253 | } else { 254 | if ( strpos( $id, 'user_' ) !== false ) { 255 | $filter = array( 'ef_user' => str_replace( 'user_', '', $id ) ); 256 | } elseif ( strpos( $id, 'taxonomy_' ) !== false ) { 257 | $filter = array( 'ef_taxonomy' => str_replace( 'taxonomy_', '', $id ) ); 258 | } else { 259 | $filter = array( 'post_id' => $id ); 260 | } 261 | 262 | $field_groups = apply_filters( 'acf/location/match_field_groups', array(), $filter ); 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 | } 270 | } 271 | } 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 | } 281 | 282 | return $fields; 283 | } 284 | 285 | } 286 | } 287 | --------------------------------------------------------------------------------