acf_shortcode()ACF 1.1.1

Эта функция обработки шорткода [acf]. Пример шорткода: [acf field="heading" post_id="123" format_value="1"].

Это внутренняя функция, которая используется для регистрации шорткода. Её не нужно использовать где-либо.

Работает на основе: get_field()

Возвращает

Строку|null.

Использование

acf_shortcode( $atts );
$atts(массив)

Массив параметров. Возможны следующие аргументы:

  • $field(строка) (обязательный)
    Имя, ключ поля.

  • $post_id(разное) (обязательный)
    ID поста поле которого нужно получить.

  • $format_value(true/false)
    Нужно ли форматировать значение поля.
    По умолчанию: true

Примеры

Список изменений

С версии 1.1.1 Введена.

Код acf_shortcode() ACF 6.4.2

function acf_shortcode( $atts ) {
	// Return if the ACF shortcode is disabled.
	if ( ! acf_get_setting( 'enable_shortcode' ) ) {
		if ( is_preview() ) {
			return apply_filters( 'acf/shortcode/disabled_message', esc_html__( '[The ACF shortcode is disabled on this site]', 'acf' ) );
		} else {
			return;
		}
	}

	if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
		// Prevent the ACF shortcode in FSE block template parts by default.
		if ( ! doing_filter( 'the_content' ) && ! apply_filters( 'acf/shortcode/allow_in_block_themes_outside_content', false ) ) {
			return;
		}
	}

	// Limit previews of ACF shortcode data for users without publish_posts permissions.
	$preview_capability = apply_filters( 'acf/shortcode/preview_capability', 'publish_posts' );
	if ( is_preview() && ! current_user_can( $preview_capability ) ) {
		return apply_filters( 'acf/shortcode/preview_capability_message', esc_html__( '[ACF shortcode value disabled for preview]', 'acf' ) );
	}

	// Mitigate issue where some AJAX requests can return ACF field data.
	$ajax_capability = apply_filters( 'acf/ajax/shortcode_capability', 'edit_posts' );
	if ( wp_doing_ajax() && ( $ajax_capability !== false ) && ! current_user_can( $ajax_capability ) ) {
		return;
	}

	$atts = shortcode_atts(
		array(
			'field'        => '',
			'post_id'      => false,
			'format_value' => true,
		),
		$atts,
		'acf'
	);

	// Decode the post ID for filtering.
	$post_id         = acf_get_valid_post_id( $atts['post_id'] );
	$decoded_post_id = acf_decode_post_id( $post_id );

	// If we've decoded to a post, ensure the post is publicly visible.
	if ( $decoded_post_id['type'] === 'post' ) {
		if ( $atts['post_id'] !== false && ( (int) $atts['post_id'] !== (int) acf_get_valid_post_id() ) && ( ! is_post_publicly_viewable( $decoded_post_id['id'] ) ) && apply_filters( 'acf/shortcode/prevent_access_to_fields_on_non_public_posts', true ) ) {
			if ( is_preview() ) {
				return apply_filters( 'acf/shortcode/post_not_public_message', esc_html__( '[The ACF shortcode cannot display fields from non-public posts]', 'acf' ) );
			} else {
				return;
			}
		}
	}

	$access_already_prevented = apply_filters( 'acf/prevent_access_to_unknown_fields', false );
	$filter_applied           = false;

	if ( ! $access_already_prevented ) {
		$filter_applied = true;
		add_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
	}

	// Try to get the field value, ensuring any non-safe HTML is stripped from wysiwyg fields via `acf_the_content`
	$field = get_field_object( $atts['field'], $post_id, $atts['format_value'], true, true );
	$value = $field ? $field['value'] : get_field( $atts['field'], $post_id, $atts['format_value'], true );

	$field_type = is_array( $field ) && isset( $field['type'] ) ? $field['type'] : 'text';

	if ( ! acf_field_type_supports( $field_type, 'bindings', true ) ) {
		if ( is_preview() ) {
			return apply_filters( 'acf/shortcode/field_not_supported_message', '[' . esc_html__( 'The requested ACF field type does not support output in bindings or the ACF Shortcode.', 'acf' ) . ']' );
		} else {
			return;
		}
	}

	if ( isset( $field['allow_in_bindings'] ) && ! $field['allow_in_bindings'] ) {
		if ( is_preview() ) {
			return apply_filters( 'acf/shortcode/field_not_allowed_message', '[' . esc_html__( 'The requested ACF field is not allowed to be output in bindings or the ACF Shortcode.', 'acf' ) . ']' );
		} else {
			return;
		}
	}

	if ( apply_filters( 'acf/shortcode/prevent_access', false, $atts, $decoded_post_id['id'], $decoded_post_id['type'], $field_type, $field ) ) {
		return;
	}

	if ( is_array( $value ) ) {
		$value = implode( ', ', $value );
	}

	// Temporarily always get the unescaped version for action comparison.
	$unescaped_value = get_field( $atts['field'], $post_id, $atts['format_value'], false );

	// Remove the filter preventing access to unknown filters now we've got all the values.
	if ( $filter_applied ) {
		remove_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
	}

	if ( is_array( $unescaped_value ) ) {
		$unescaped_value = implode( ', ', $unescaped_value );
	}

	if ( ! is_scalar( $unescaped_value ) ) {
		$unescaped_value = false;
	}

	// Handle getting the unescaped version if we're allowed unsafe html.
	if ( apply_filters( 'acf/shortcode/allow_unsafe_html', false, $atts, $field_type, $field ) ) {
		$value = $unescaped_value;
	} elseif ( $unescaped_value !== false && (string) $value !== (string) $unescaped_value ) {
		do_action( 'acf/removed_unsafe_html', __FUNCTION__, $atts['field'], $field, $post_id );
	}

	return $value;
}