函数文档

_upgrade_422_remove_genericons()

💡 云策文档标注

概述

_upgrade_422_remove_genericons() 函数用于在 WordPress 升级过程中清理 Genericons 示例文件。它通过遍历主题和插件目录,查找并删除这些文件,确保系统整洁。

关键要点

  • 函数 _upgrade_422_remove_genericons() 在 WordPress 4.2.2 版本引入,作为核心升级的一部分。
  • 它使用全局变量 $wp_theme_directories 和 $wp_filesystem 来操作文件系统。
  • 函数首先在主题目录和插件目录中查找 Genericons 文件,然后尝试删除或清空这些文件。
  • 如果文件删除失败,函数会尝试将文件内容置空,以确保清理操作完成。
  • 相关函数包括 trailingslashit() 用于添加尾部斜杠,以及 update_core() 用于执行核心升级。

代码示例

function _upgrade_422_remove_genericons() {
	global $wp_theme_directories, $wp_filesystem;

	// A list of the affected files using the filesystem absolute paths.
	$affected_files = array();

	// Themes.
	foreach ( $wp_theme_directories as $directory ) {
		$affected_theme_files = _upgrade_422_find_genericons_files_in_folder( $directory );
		$affected_files       = array_merge( $affected_files, $affected_theme_files );
	}

	// Plugins.
	$affected_plugin_files = _upgrade_422_find_genericons_files_in_folder( WP_PLUGIN_DIR );
	$affected_files        = array_merge( $affected_files, $affected_plugin_files );

	foreach ( $affected_files as $file ) {
		$gen_dir = $wp_filesystem->find_folder( trailingslashit( dirname( $file ) ) );

		if ( empty( $gen_dir ) ) {
			continue;
		}

		// The path when the file is accessed via WP_Filesystem may differ in the case of FTP.
		$remote_file = $gen_dir . basename( $file );

		if ( ! $wp_filesystem->exists( $remote_file ) ) {
			continue;
		}

		if ( ! $wp_filesystem->delete( $remote_file, false, 'f' ) ) {
			$wp_filesystem->put_contents( $remote_file, '' );
		}
	}
}

注意事项

  • 此函数主要用于 WordPress 核心升级过程,开发者通常不需要直接调用它。
  • 操作涉及文件系统,需确保 $wp_filesystem 已正确初始化,以避免权限或路径问题。
  • 如果文件删除失败,函数会尝试清空文件内容,这可能在某些环境下导致意外行为,建议在测试环境中验证。

📄 原文内容

Cleans up Genericons example files.

Source

function _upgrade_422_remove_genericons() {
	global $wp_theme_directories, $wp_filesystem;

	// A list of the affected files using the filesystem absolute paths.
	$affected_files = array();

	// Themes.
	foreach ( $wp_theme_directories as $directory ) {
		$affected_theme_files = _upgrade_422_find_genericons_files_in_folder( $directory );
		$affected_files       = array_merge( $affected_files, $affected_theme_files );
	}

	// Plugins.
	$affected_plugin_files = _upgrade_422_find_genericons_files_in_folder( WP_PLUGIN_DIR );
	$affected_files        = array_merge( $affected_files, $affected_plugin_files );

	foreach ( $affected_files as $file ) {
		$gen_dir = $wp_filesystem->find_folder( trailingslashit( dirname( $file ) ) );

		if ( empty( $gen_dir ) ) {
			continue;
		}

		// The path when the file is accessed via WP_Filesystem may differ in the case of FTP.
		$remote_file = $gen_dir . basename( $file );

		if ( ! $wp_filesystem->exists( $remote_file ) ) {
			continue;
		}

		if ( ! $wp_filesystem->delete( $remote_file, false, 'f' ) ) {
			$wp_filesystem->put_contents( $remote_file, '' );
		}
	}
}

Changelog

Version Description
4.2.2 Introduced.