wp-forge/wp-upgrade-handler

WordPress插件和主题升级流程处理的库。

1.0 2020-05-21 00:04 UTC

This package is auto-updated.

Last update: 2024-09-21 20:56:38 UTC


README

WordPress插件和主题升级流程处理的库。

安装

  • 运行 composer require wp-forge/wp-upgrade-handler
  • 请确保在您的项目中包含了 vendor/autoload.php 文件。

使用方法

以下是一个在WordPress插件或主题中使用此库的示例

<?php

use WP_Forge\UpgradeHandler\UpgradeHandler;

// Define the current plugin version in the code
define( 'MY_PLUGIN_VERSION', '1.4.1' );

// Only handle upgrades in the admin
if ( is_admin() ) {

	// Handle plugin upgrades
	$upgrade_handler = new UpgradeHandler(
		__DIR__ . '/upgrades',              // Directory where upgrade routines live
		get_option( 'my_plugin_version' ),  // Old plugin version (from database)
		MY_PLUGIN_VERSION                   // New plugin version (from code)
	);

	// Returns true if the old version doesn't match the new version
	$did_upgrade = $upgrade_handler->maybe_upgrade();

	if ( $did_upgrade ) {
		// If an upgrade occurred, update the new version in the database to prevent running the routine(s) again.
		update_option( 'my_plugin_version', MY_PLUGIN_VERSION, true );
	}
}

如果您刚刚发布了插件版本 1.4.1,但为 1.4.11.3.9 创建了升级流程,那么从版本 1.3.8 或更早版本升级的用户将自动运行这两个升级流程。如果有人从版本 1.3.9 或更高版本升级,则只会运行 1.4.1 的升级流程。

创建升级流程

例如,假设我刚刚发布了插件版本 1.4.1。升级流程需要更改数据库中的选项名称。在添加上述代码之后,我只需要在我的 upgrades 目录中创建一个名为 1.4.1.php 的文件。

该文件的以下内容将是

<?php

// Rename 'old_option' to 'new_option', if necessary.
$old_option = get_option( 'old_option' );
if( $old_option ) {
    update_option( 'new_option', get_option( 'old_option' ) );
    delete_option( 'old_option' );
}