WPSEO_Metabox::get_custom_fields_replace_vars()privateYoast 1.0

Gets the custom replace variables for custom fields.

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

Хуков нет.

Возвращает

Массив. Array containing all the replacement variables.

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

// private - только в коде основоного (родительского) класса
$result = $this->get_custom_fields_replace_vars( $post );
$post(WP_Post) (обязательный)
The post to check for custom fields.

Код WPSEO_Metabox::get_custom_fields_replace_vars() Yoast 19.11

private function get_custom_fields_replace_vars( $post ) {
	$custom_replace_vars = [];

	// If no post object is passed, return the empty custom_replace_vars array.
	if ( ! is_object( $post ) ) {
		return $custom_replace_vars;
	}

	$custom_fields = get_post_custom( $post->ID );

	// If $custom_fields is an empty string or generally not an array, return early.
	if ( ! is_array( $custom_fields ) ) {
		return $custom_replace_vars;
	}

	$meta = YoastSEO()->meta->for_post( $post->ID );

	if ( ! $meta ) {
		return $custom_replace_vars;
	}

	// Simply concatenate all fields containing replace vars so we can handle them all with a single regex find.
	$replace_vars_fields = implode(
		' ',
		[
			$meta->presentation->title,
			$meta->presentation->meta_description,
		]
	);

	preg_match_all( '/%%cf_([A-Za-z0-9_]+)%%/', $replace_vars_fields, $matches );
	$fields_to_include = $matches[1];
	foreach ( $custom_fields as $custom_field_name => $custom_field ) {
		// Skip private custom fields.
		if ( substr( $custom_field_name, 0, 1 ) === '_' ) {
			continue;
		}

		// Skip custom fields that are not used, new ones will be fetched dynamically.
		if ( ! in_array( $custom_field_name, $fields_to_include, true ) ) {
			continue;
		}

		// Skip custom field values that are serialized.
		if ( is_serialized( $custom_field[0] ) ) {
			continue;
		}

		$custom_replace_vars[ $custom_field_name ] = $custom_field[0];
	}

	return $custom_replace_vars;
}