Skip to content

Commit

Permalink
fix: trim filename size if it exceeds 99 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Oct 18, 2021
1 parent 896f2ea commit 0777676
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,13 @@ Feature: Export content.
"""
Europe
"""

Scenario: A blog with a title with size more than 99 bytes.
Given a WP install

When I run `wp option update blogname "A blog with an exceedingly long title (125 bytes), which would be absolutely trimmed to 99 bytes by the wp cli export command"`
And I run `wp export`
Then STDOUT should contain:
"""
ablogwithanexceedinglylongtitle125byteswhichwouldbeabsolutelytrimmedto.wordpress.2021-10-18.000.xml
"""
25 changes: 25 additions & 0 deletions src/Export_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,34 @@ static function ( $file_path ) {

private static function get_filename_template( $filename_format ) {
$sitename = sanitize_key( get_bloginfo( 'name' ) );

if ( empty( $sitename ) ) {
$sitename = 'site';
}

/**
* Get the potential filename.
*/
$potential_file_name = str_replace( [ '{site}', '{date}', '{n}' ], [ $sitename, date( 'Y-m-d' ), '%03d' ], $filename_format );

/**
* Get the length of the potential file name.
*/
$potential_file_name_size = strlen( $potential_file_name );

/**
* If the potential filename size exceeds 99 bytes, then remove the extra bytes.
*/
if ( $potential_file_name_size > 99 ) {
$extra_bytes = $potential_file_name_size - 99;
$sitename_size = strlen( $sitename );

/**
* Remove the extra bytes from the sitename.
*/
$sitename = substr( $sitename, 0, ( $sitename_size - $extra_bytes ) + 1 );
}

return str_replace( [ '{site}', '{date}', '{n}' ], [ $sitename, date( 'Y-m-d' ), '%03d' ], $filename_format ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
}

Expand Down

0 comments on commit 0777676

Please sign in to comment.