acf_rest_reference_is_exposable()
Determines whether the current user should see the fully-expanded target of a reference field (Relationship, Post Object, Image, Gallery, File).
Delegates to the REST controller core registered for the target post type (via WP_Post_Type::get_rest_controller(); usually WP_REST_Posts_Controller, or WP_REST_Attachments_Controller for attachments, or a custom class declared via rest_controller_class) so this stays in lockstep with what /wp/v2/<post_type>/<id> returns for the same caller. Targets whose post type is not REST-exposed (show_in_rest === false) are treated as non-exposable, matching core.
Fail-closed contract: if core has no REST controller for the post type (for example, a rest_controller_class that doesn't extend WP_REST_Controller), or the controller does not implement check_read_permission(), the target is treated as non-exposable. A bare current_user_can('read_post',$id) fallback is intentionally NOT used because it evaluates to false for anonymous callers on published posts (anon lacks the read cap on standard installs), so the fallback would silently reduce publicly readable posts. Sites running a non-standard REST controller for a post type can re-expose targets via the acf/rest/expose_reference_target filter after making an informed decision.
Password-protected targets whose password has not been entered are only exposable to users who can edit them, even though the REST controller's read check passes for published protected posts.
Хуки из функции
Возвращает
boolean.
Использование
acf_rest_reference_is_exposable( $target_id, $field );
- $target_id(int) (обязательный)
- The referenced resource ID (post or attachment).
- $field(массив) (обязательный)
- The field array (context for filter subscribers).
Список изменений
| С версии 6.8.10 | Введена. |
Since ACF 6.8.11 Password-protected targets are only exposed to users who entered the password or can edit them.
Код acf_rest_reference_is_exposable() acf rest reference is exposable ACF 6.8.11
function acf_rest_reference_is_exposable( $target_id, $field ) {
$exposable = false;
$post = $target_id > 0 ? get_post( $target_id ) : null;
if ( $post instanceof WP_Post ) {
$post_type_obj = get_post_type_object( $post->post_type );
if ( $post_type_obj && ! empty( $post_type_obj->show_in_rest ) ) {
$controller = $post_type_obj->get_rest_controller();
if ( $controller && method_exists( $controller, 'check_read_permission' ) ) {
$exposable = (bool) $controller->check_read_permission( $post );
}
// Core's read check passes for published password-protected posts, so hide them from anyone who can't edit them.
if ( $exposable && post_password_required( $post ) && ! current_user_can( 'edit_post', $post->ID ) ) {
$exposable = false;
}
}
}
/**
* Filters whether an expanded reference target should be exposed to the
* current caller when `?acf_format=standard` is in play. Return true to
* re-expose a target the default cap check would reduce.
*
* @since ACF 6.8.10
*
* @param boolean $exposable Whether the target's full expansion should be exposed.
* @param integer $target_id The referenced resource ID.
* @param array $field The ACF field array.
*/
return (bool) apply_filters( 'acf/rest/expose_reference_target', $exposable, $target_id, $field );
}