WP_CLI\Utils
launch_editor_for_input()
Launch system's $EDITOR for the user to edit some text.
No Hooks.
Return
String|true|false
. Edited text, if file is saved from editor; false, if no change to file.
Usage
launch_editor_for_input( $input, $title, $ext );
- $input(string) (required)
- Some form of text to edit (e.g. post content).
- $title(string)
- Title to display in the editor.
Default: 'WP-CLI' - $ext(string)
- Extension to use with the temp file.
Default: 'tmp'
launch_editor_for_input() launch editor for input code WP-CLI 2.8.0-alpha
function launch_editor_for_input( $input, $title = 'WP-CLI', $ext = 'tmp' ) { check_proc_available( 'launch_editor_for_input' ); $tmpdir = get_temp_dir(); do { $tmpfile = basename( $title ); $tmpfile = preg_replace( '|\.[^.]*$|', '', $tmpfile ); $tmpfile .= '-' . substr( md5( mt_rand() ), 0, 6 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand -- no crypto and WP not loaded. $tmpfile = $tmpdir . $tmpfile . '.' . $ext; $fp = fopen( $tmpfile, 'xb' ); if ( ! $fp && is_writable( $tmpdir ) && file_exists( $tmpfile ) ) { $tmpfile = ''; continue; } if ( $fp ) { fclose( $fp ); } } while ( ! $tmpfile ); if ( ! $tmpfile ) { WP_CLI::error( 'Error creating temporary file.' ); } file_put_contents( $tmpfile, $input ); $editor = getenv( 'EDITOR' ); if ( ! $editor ) { $editor = is_windows() ? 'notepad' : 'vi'; } $descriptorspec = [ STDIN, STDOUT, STDERR ]; $process = proc_open_compat( "$editor " . escapeshellarg( $tmpfile ), $descriptorspec, $pipes ); $r = proc_close( $process ); if ( $r ) { exit( $r ); } $output = file_get_contents( $tmpfile ); unlink( $tmpfile ); if ( $output === $input ) { return false; } return $output; }