WP_CLI\Utils

increment_version()WP-CLI 1.0

Increments a version string using the "x.y.z-pre" format.

Can increment the major, minor or patch number by one. If $new_version == "same" the version string is not changed. If $new_version is not a known keyword, it will be used as the new version string directly.

No Hooks.

Return

String.

Usage

increment_version( $current_version, $new_version );
$current_version(string) (required)
-
$new_version(string) (required)
-

increment_version() code WP-CLI 2.8.0-alpha

function increment_version( $current_version, $new_version ) {
	// split version assuming the format is x.y.z-pre
	$current_version    = explode( '-', $current_version, 2 );
	$current_version[0] = explode( '.', $current_version[0] );

	switch ( $new_version ) {
		case 'same':
			// do nothing
			break;

		case 'patch':
			$current_version[0][2]++;

			$current_version = [ $current_version[0] ]; // Drop possible pre-release info.
			break;

		case 'minor':
			$current_version[0][1]++;
			$current_version[0][2] = 0;

			$current_version = [ $current_version[0] ]; // Drop possible pre-release info.
			break;

		case 'major':
			$current_version[0][0]++;
			$current_version[0][1] = 0;
			$current_version[0][2] = 0;

			$current_version = [ $current_version[0] ]; // Drop possible pre-release info.
			break;

		default: // not a keyword
			$current_version = [ [ $new_version ] ];
			break;
	}

	// Reconstruct version string.
	$current_version[0] = implode( '.', $current_version[0] );
	$current_version    = implode( '-', $current_version );

	return $current_version;
}