Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 60 additions & 39 deletions src/wp-admin/includes/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,57 @@ function got_url_rewrite() {
return apply_filters( 'got_url_rewrite', $got_url_rewrite );
}

/**
* Splits lines into those preceding, between, and following the BEGIN and END markers.
*
* A line is treated as a marker only when it matches `# BEGIN {$marker}` or
* `# END {$marker}` exactly, ignoring surrounding whitespace. This allows markers
* to be indented, and prevents a marker such as `WordPress` from matching
* `WordPress_Foo`. An END marker is only recognized after the BEGIN marker.
*
* @since 7.3.0
*
* @param string[] $lines Lines to split, without trailing newline characters.
* @param string $marker The marker to look for.
* @return array {
* The split lines. Marker lines themselves are not included.
*
* @type string[] $0 Lines preceding the BEGIN marker.
* @type string[] $1 Lines between the BEGIN and END markers.
* @type string[] $2 Lines following the END marker.
* }
*/
function split_by_markers( $lines, $marker ) {
$start_marker = "# BEGIN {$marker}";
$end_marker = "# END {$marker}";

$pre_lines = array();
$existing_lines = array();
$post_lines = array();
$found_marker = false;
$found_end_marker = false;

foreach ( $lines as $line ) {
if ( ! $found_marker && trim( $line ) === $start_marker ) {
$found_marker = true;
continue;
} elseif ( $found_marker && ! $found_end_marker && trim( $line ) === $end_marker ) {
$found_end_marker = true;
continue;
}

if ( ! $found_marker ) {
$pre_lines[] = $line;
} elseif ( $found_end_marker ) {
$post_lines[] = $line;
} else {
$existing_lines[] = $line;
}
}

return array( $pre_lines, $existing_lines, $post_lines );
}

/**
* Extracts strings from between the BEGIN and END markers in the .htaccess file.
*
Expand All @@ -72,26 +123,18 @@ function extract_from_markers( $filename, $marker ) {
return $result;
}

$markerdata = explode( "\n", implode( '', file( $filename ) ) );

$state = false;
// FILE_IGNORE_NEW_LINES strips both "\n" and "\r\n" line endings.
$markerdata = file( $filename, FILE_IGNORE_NEW_LINES );

foreach ( $markerdata as $markerline ) {
if ( str_contains( $markerline, '# END ' . $marker ) ) {
$state = false;
}

if ( $state ) {
if ( str_starts_with( $markerline, '#' ) ) {
continue;
}
list( , $existing_lines ) = split_by_markers( $markerdata, $marker );

$result[] = $markerline;
foreach ( $existing_lines as $markerline ) {
// Skip comments, such as the inline instructions added by insert_with_markers().
if ( str_starts_with( ltrim( $markerline ), '#' ) ) {
continue;
}

if ( str_contains( $markerline, '# BEGIN ' . $marker ) ) {
$state = true;
}
$result[] = $markerline;
}

return $result;
Expand Down Expand Up @@ -188,29 +231,7 @@ function insert_with_markers( $filename, $marker, $insertion ) {
}

// Split out the existing file into the preceding lines, and those that appear after the marker.
$pre_lines = array();
$post_lines = array();
$existing_lines = array();
$found_marker = false;
$found_end_marker = false;

foreach ( $lines as $line ) {
if ( ! $found_marker && str_contains( $line, $start_marker ) ) {
$found_marker = true;
continue;
} elseif ( ! $found_end_marker && str_contains( $line, $end_marker ) ) {
$found_end_marker = true;
continue;
}

if ( ! $found_marker ) {
$pre_lines[] = $line;
} elseif ( $found_end_marker ) {
$post_lines[] = $line;
} else {
$existing_lines[] = $line;
}
}
list( $pre_lines, $existing_lines, $post_lines ) = split_by_markers( $lines, $marker );

// Check to see if there was a change.
if ( $existing_lines === $insertion ) {
Expand Down
115 changes: 115 additions & 0 deletions tests/phpunit/tests/admin/includes/misc/ExtractFromMarkers_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* @group admin
* @group rewrite
*
* @covers ::extract_from_markers
*/
class Tests_Admin_Includes_Misc_ExtractFromMarkers_Test extends WP_UnitTestCase {

/**
* Path to the temporary file used by the current test.
*
* @var string
*/
private $filename;

public function set_up() {
parent::set_up();
$this->filename = wp_tempnam( 'markers' );
}

public function tear_down() {
if ( file_exists( $this->filename ) ) {
unlink( $this->filename );
}
parent::tear_down();
}

/**
* @ticket 26829
*
* @dataProvider data_line_endings
*
* @param string $eol Line ending to use in the file.
*/
public function test_extract_from_markers( $eol ) {
$lines = array(
'',
'# BEGIN Empty',
'# END Empty',
'',
'# BEGIN FooBar',
'FooBar line',
'# END FooBar',
'',
'# BEGIN Foo Bar',
'Foo Bar line',
'# END Foo Bar',
'',
'# BEGIN Foo',
'Foo line',
'# END Foo',
'',
'# BEGIN Multiline',
'# A comment that is skipped.',
'',
'Multiline 2',
'Multiline 3',
'',
'Multiline 5',
'',
'# END Multiline',
);
file_put_contents( $this->filename, implode( $eol, $lines ) . $eol );

$this->assertSame( array( 'Foo line' ), extract_from_markers( $this->filename, 'Foo' ), 'A marker that is a prefix of other markers should only match its own section.' );
$this->assertSame( array( 'FooBar line' ), extract_from_markers( $this->filename, 'FooBar' ), 'A marker without spaces was not extracted correctly.' );
$this->assertSame( array( 'Foo Bar line' ), extract_from_markers( $this->filename, 'Foo Bar' ), 'A marker containing a space was not extracted correctly.' );
$this->assertSame(
array( '', 'Multiline 2', 'Multiline 3', '', 'Multiline 5', '' ),
extract_from_markers( $this->filename, 'Multiline' ),
'A multiline section, including empty lines, was not extracted correctly.'
);
$this->assertSame( array(), extract_from_markers( $this->filename, 'Empty' ), 'An empty section should return an empty array.' );
$this->assertSame( array(), extract_from_markers( $this->filename, 'Null' ), 'A missing section should return an empty array.' );
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_line_endings() {
return array(
'LF' => array( "\n" ),
'CRLF' => array( "\r\n" ),
);
}

/**
* @ticket 26829
*/
public function test_extract_from_markers_with_indented_markers() {
file_put_contents( $this->filename, "\t# BEGIN Foo\n Foo line\n # Indented comment\n # END Foo\n" );

$this->assertSame( array( ' Foo line' ), extract_from_markers( $this->filename, 'Foo' ) );
}

/**
* @ticket 26829
*/
public function test_extract_from_markers_returns_empty_array_for_empty_file() {
$this->assertSame( array(), extract_from_markers( $this->filename, 'Foo' ) );
}

/**
* @ticket 26829
*/
public function test_extract_from_markers_returns_empty_array_for_missing_file() {
unlink( $this->filename );

$this->assertSame( array(), extract_from_markers( $this->filename, 'Foo' ) );
}
}
115 changes: 115 additions & 0 deletions tests/phpunit/tests/admin/includes/misc/InsertWithMarkers_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* @group admin
* @group rewrite
*
* @covers ::insert_with_markers
*/
class Tests_Admin_Includes_Misc_InsertWithMarkers_Test extends WP_UnitTestCase {

/**
* Path to the temporary file used by the current test.
*
* @var string
*/
private $filename;

public function set_up() {
parent::set_up();
$this->filename = wp_tempnam( 'markers' );

// Leave out the inline instructions so the expected file contents stay readable.
add_filter( 'insert_with_markers_inline_instructions', '__return_empty_array' );
}

public function tear_down() {
if ( file_exists( $this->filename ) ) {
unlink( $this->filename );
}
parent::tear_down();
}

/**
* Tests that a section whose marker is a prefix of an earlier marker
* is updated, instead of the earlier section being overwritten.
*
* @ticket 26829
*/
public function test_insert_with_markers_does_not_overwrite_section_with_prefixed_marker() {
file_put_contents(
$this->filename,
"# BEGIN WordPress_Foo\nfoo\n# END WordPress_Foo\n# BEGIN WordPress\nold\n# END WordPress"
);

$this->assertTrue( insert_with_markers( $this->filename, 'WordPress', array( 'new' ) ) );
$this->assertSame(
"# BEGIN WordPress_Foo\nfoo\n# END WordPress_Foo\n# BEGIN WordPress\nnew\n# END WordPress",
file_get_contents( $this->filename )
);
}

/**
* Tests that repeated inserts do not duplicate a section when a section
* with a prefixed marker precedes it, as described in the ticket.
*
* @ticket 26829
*/
public function test_insert_with_markers_does_not_duplicate_sections() {
file_put_contents( $this->filename, "# BEGIN WordPress_Foo\n\n# END WordPress_Foo" );

insert_with_markers( $this->filename, 'WordPress', array( 'rules' ) );
insert_with_markers( $this->filename, 'WordPress', array( 'rules' ) );
insert_with_markers( $this->filename, 'WordPress', array( 'rules' ) );

$this->assertSame(
"# BEGIN WordPress_Foo\n\n# END WordPress_Foo\n# BEGIN WordPress\nrules\n# END WordPress",
file_get_contents( $this->filename )
);
}

/**
* @ticket 26829
*/
public function test_insert_with_markers_replaces_indented_section() {
file_put_contents( $this->filename, "before\n # BEGIN Foo\n old\n # END Foo\nafter" );

$this->assertTrue( insert_with_markers( $this->filename, 'Foo', array( 'new' ) ) );
$this->assertSame( "before\n# BEGIN Foo\nnew\n# END Foo\nafter", file_get_contents( $this->filename ) );
}

/**
* @ticket 26829
*/
public function test_insert_with_markers_replaces_section_in_file_with_crlf_line_endings() {
file_put_contents( $this->filename, "before\r\n# BEGIN Foo\r\nold\r\n# END Foo\r\nafter\r\n" );

$this->assertTrue( insert_with_markers( $this->filename, 'Foo', array( 'new' ) ) );
$this->assertSame( array( 'new' ), extract_from_markers( $this->filename, 'Foo' ) );
$this->assertSame( 1, substr_count( file_get_contents( $this->filename ), '# BEGIN Foo' ) );
}

/**
* @ticket 26829
*/
public function test_insert_with_markers_does_not_modify_unchanged_section() {
$contents = "# BEGIN Foo\nsame\n# END Foo";
file_put_contents( $this->filename, $contents );

$this->assertTrue( insert_with_markers( $this->filename, 'Foo', array( 'same' ) ) );
$this->assertSame( $contents, file_get_contents( $this->filename ) );
}

/**
* @ticket 26829
*/
public function test_insert_with_markers_appends_missing_section() {
file_put_contents( $this->filename, "# BEGIN FooBar\nfoobar\n# END FooBar" );

$this->assertTrue( insert_with_markers( $this->filename, 'Foo', "line 1\nline 2" ) );
$this->assertSame(
"# BEGIN FooBar\nfoobar\n# END FooBar\n# BEGIN Foo\nline 1\nline 2\n# END Foo",
file_get_contents( $this->filename )
);
}
}
Loading
Loading