rest_is_field_included

The timeline below displays how wordpress function rest_is_field_included has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Given an array of fields to include in a response, some of which may be
 * `nested.fields`, determine whether the provided field should be included
 * in the response body.
 *
 * If a parent field is passed in, the presence of any nested field within
 * that parent will cause the method to return `true`. For example "title"
 * will return true if any of `title`, `title.raw` or `title.rendered` is
 * provided.
 *
 * @since 5.3.0
 *
 * @param string $field  A field to test for inclusion in the response body.
 * @param array  $fields An array of string fields supported by the endpoint.
 * @return bool Whether to include the field or not.
 */
function rest_is_field_included($field, $fields)
{
    if (in_array($field, $fields, true)) {
        return true;
    }
    foreach ($fields as $accepted_field) {
        /*
         * Check to see if $field is the parent of any item in $fields.
         * A field "parent" should be accepted if "parent.child" is accepted.
         */
        if (str_starts_with($accepted_field, "{$field}.")) {
            return true;
        }
        /*
         * Conversely, if "parent" is accepted, all "parent.child" fields
         * should also be accepted.
         */
        if (str_starts_with($field, "{$accepted_field}.")) {
            return true;
        }
    }
    return false;
}

WordPress Version: 5.4

/**
 * Given an array of fields to include in a response, some of which may be
 * `nested.fields`, determine whether the provided field should be included
 * in the response body.
 *
 * If a parent field is passed in, the presence of any nested field within
 * that parent will cause the method to return `true`. For example "title"
 * will return true if any of `title`, `title.raw` or `title.rendered` is
 * provided.
 *
 * @since 5.3.0
 *
 * @param string $field  A field to test for inclusion in the response body.
 * @param array  $fields An array of string fields supported by the endpoint.
 * @return bool Whether to include the field or not.
 */
function rest_is_field_included($field, $fields)
{
    if (in_array($field, $fields, true)) {
        return true;
    }
    foreach ($fields as $accepted_field) {
        // Check to see if $field is the parent of any item in $fields.
        // A field "parent" should be accepted if "parent.child" is accepted.
        if (strpos($accepted_field, "{$field}.") === 0) {
            return true;
        }
        // Conversely, if "parent" is accepted, all "parent.child" fields
        // should also be accepted.
        if (strpos($field, "{$accepted_field}.") === 0) {
            return true;
        }
    }
    return false;
}

WordPress Version: 5.3

/**
 * Given an array of fields to include in a response, some of which may be
 * `nested.fields`, determine whether the provided field should be included
 * in the response body.
 *
 * If a parent field is passed in, the presence of any nested field within
 * that parent will cause the method to return `true`. For example "title"
 * will return true if any of `title`, `title.raw` or `title.rendered` is
 * provided.
 *
 * @since 5.3.0
 *
 * @param string $field  A field to test for inclusion in the response body.
 * @param array  $fields An array of string fields supported by the endpoint.
 * @return bool Whether to include the field or not.
 */
function rest_is_field_included($field, $fields)
{
    if (in_array($field, $fields, true)) {
        return true;
    }
    foreach ($fields as $accepted_field) {
        // Check to see if $field is the parent of any item in $fields.
        // A field "parent" should be accepted if "parent.child" is accepted.
        if (strpos($accepted_field, "{$field}.") === 0) {
            return true;
        }
        // Conversely, if "parent" is accepted, all "parent.child" fields should
        // also be accepted.
        if (strpos($field, "{$accepted_field}.") === 0) {
            return true;
        }
    }
    return false;
}