ACF_Local_JSON::save_filepublicACF 5.9.0

Saves an ACF JSON file.

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

Хуки из метода

Возвращает

true|false.

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

$ACF_Local_JSON = new ACF_Local_JSON();
$ACF_Local_JSON->save_file( $key, $post );
$key(строка) (обязательный)
The ACF post key.
$post(массив) (обязательный)
The main ACF post array.

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

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

Код ACF_Local_JSON::save_file() ACF 6.4.2

public function save_file( $key, $post ) {
	$paths          = $this->get_save_paths( $key, $post );
	$filename       = $this->get_filename( $key, $post );
	$file           = false;
	$first_writable = false;

	if ( ! $filename ) {
		return false;
	}

	foreach ( $paths as $path ) {
		if ( ! is_writable( $path ) ) { //phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- non-compatible function for this purpose.
			continue;
		}

		if ( false === $first_writable ) {
			$first_writable = $path;
		}

		$file_to_check = trailingslashit( $path ) . $filename;

		if ( is_file( $file_to_check ) ) {
			$file = $file_to_check;
		}
	}

	if ( ! $file ) {
		if ( $first_writable ) {
			$file = trailingslashit( $first_writable ) . $filename;
		} else {
			return false;
		}
	}

	// Make sure this is a valid ACF post type.
	$post_type = acf_determine_internal_post_type( $key );
	if ( ! $post_type ) {
		return false;
	}

	// Append modified time.
	if ( $post['ID'] ) {
		$post['modified'] = get_post_modified_time( 'U', true, $post['ID'] );
	} else {
		$post['modified'] = strtotime( 'now' );
	}

	// Prepare for export and save the file.
	$post   = acf_prepare_internal_post_type_for_export( $post, $post_type );
	$result = file_put_contents( $file, acf_json_encode( $post ) . apply_filters( 'acf/json/eof_newline', PHP_EOL ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- potentially could run outside of admin.

	// Return true if bytes were written.
	return is_int( $result );
}