ACF_Post_Type::build_safe_context_for_metabox_cbpublicACF 6.3.8

Ensure the metabox being called does not perform any unsafe operations.

Метод класса: ACF_Post_Type{}

Возвращает

Разное. The callback result.

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

$ACF_Post_Type = new ACF_Post_Type();
$ACF_Post_Type->build_safe_context_for_metabox_cb( $post );
$post(WP_Post) (обязательный)
The post being rendered.

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

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

Код ACF_Post_Type::build_safe_context_for_metabox_cb() ACF 6.4.2

public function build_safe_context_for_metabox_cb( $post ) {
	$post_types = $this->get_posts();
	$this_post  = array_filter(
		$post_types,
		function ( $post_type ) use ( $post ) {
			return $post_type['post_type'] === $post->post_type;
		}
	);
	if ( empty( $this_post ) || ! is_array( $this_post ) ) {
		// Unable to find the ACF post type. Don't do anything.
		return;
	}
	$acf_post_type = array_shift( $this_post );
	$original_cb   = isset( $acf_post_type['register_meta_box_cb'] ) ? $acf_post_type['register_meta_box_cb'] : false;

	// Prevent access to any wp_ prefixed functions in a callback.
	if ( apply_filters( 'acf/post_type/prevent_access_to_wp_functions_in_meta_box_cb', true ) && substr( strtolower( $original_cb ), 0, 3 ) === 'wp_' ) {
		// Don't execute register meta box callbacks if an internal wp function by default.
		return;
	}

	$unset     = array( '_POST', '_GET', '_REQUEST', '_COOKIE', '_SESSION', '_FILES', '_ENV', '_SERVER' );
	$originals = array();

	foreach ( $unset as $var ) {
		if ( isset( $GLOBALS[ $var ] ) ) {
			$originals[ $var ] = $GLOBALS[ $var ];
			$GLOBALS[ $var ]   = array(); //phpcs:ignore -- used for building a safe context
		}
	}

	$return = false;
	if ( is_callable( $original_cb ) ) {
		$return = call_user_func( $original_cb, $post );
	}

	foreach ( $unset as $var ) {
		if ( isset( $originals[ $var ] ) ) {
			$GLOBALS[ $var ] = $originals[ $var ]; //phpcs:ignore -- used for restoring the original context
		}
	}

	return $return;
}