diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php index 0b412dd56511f..740c2ecd8bf99 100644 --- a/src/wp-admin/includes/misc.php +++ b/src/wp-admin/includes/misc.php @@ -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. * @@ -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; @@ -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 ) { diff --git a/tests/phpunit/tests/admin/includes/misc/ExtractFromMarkers_Test.php b/tests/phpunit/tests/admin/includes/misc/ExtractFromMarkers_Test.php new file mode 100644 index 0000000000000..4be947e2a3de3 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/ExtractFromMarkers_Test.php @@ -0,0 +1,115 @@ +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' ) ); + } +} diff --git a/tests/phpunit/tests/admin/includes/misc/InsertWithMarkers_Test.php b/tests/phpunit/tests/admin/includes/misc/InsertWithMarkers_Test.php new file mode 100644 index 0000000000000..f8baf92bc6d61 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/InsertWithMarkers_Test.php @@ -0,0 +1,115 @@ +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 ) + ); + } +} diff --git a/tests/phpunit/tests/admin/includes/misc/SplitByMarkers_Test.php b/tests/phpunit/tests/admin/includes/misc/SplitByMarkers_Test.php new file mode 100644 index 0000000000000..c39e215cf1ee2 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/SplitByMarkers_Test.php @@ -0,0 +1,94 @@ +assertSame( $expected, split_by_markers( $lines, $marker ) ); + } + + /** + * Data provider for test_split_by_markers(). + * + * @return array[] + */ + public static function data_split_by_markers() { + $pre = array( '# BEGIN FooPre', 'Line before', '# END FooPre' ); + $in = array( '# BEGIN Foo', 'Line in', '# END Foo' ); + $post = array( '# BEGIN FooPost', 'Line after', '# END FooPost' ); + + $lines = array_merge( $pre, $in, $post ); + + return array( + 'marker that is a prefix of a preceding marker' => array( + 'lines' => $lines, + 'marker' => 'FooPre', + 'expected' => array( array(), array( 'Line before' ), array_merge( $in, $post ) ), + ), + 'marker that prefixes the surrounding markers' => array( + 'lines' => $lines, + 'marker' => 'Foo', + 'expected' => array( $pre, array( 'Line in' ), $post ), + ), + 'last marker in the file' => array( + 'lines' => $lines, + 'marker' => 'FooPost', + 'expected' => array( array_merge( $pre, $in ), array( 'Line after' ), array() ), + ), + 'marker containing a space' => array( + 'lines' => array( '# BEGIN Foo Bar', 'Foo Bar line', '# END Foo Bar', '# BEGIN Foo', 'Foo line', '# END Foo' ), + 'marker' => 'Foo', + 'expected' => array( array( '# BEGIN Foo Bar', 'Foo Bar line', '# END Foo Bar' ), array( 'Foo line' ), array() ), + ), + 'indented markers' => array( + 'lines' => array( 'before', "\t# BEGIN Foo", ' Line in', ' # END Foo ', 'after' ), + 'marker' => 'Foo', + 'expected' => array( array( 'before' ), array( ' Line in' ), array( 'after' ) ), + ), + 'markers with trailing carriage returns' => array( + 'lines' => array( "before\r", "# BEGIN Foo\r", "Line in\r", "# END Foo\r", "after\r" ), + 'marker' => 'Foo', + 'expected' => array( array( "before\r" ), array( "Line in\r" ), array( "after\r" ) ), + ), + 'end marker before begin marker is not a marker' => array( + 'lines' => array( '# END Foo', '# BEGIN Foo', 'Line in', '# END Foo' ), + 'marker' => 'Foo', + 'expected' => array( array( '# END Foo' ), array( 'Line in' ), array() ), + ), + 'missing end marker' => array( + 'lines' => array( 'before', '# BEGIN Foo', 'Line in', 'more' ), + 'marker' => 'Foo', + 'expected' => array( array( 'before' ), array( 'Line in', 'more' ), array() ), + ), + 'missing markers' => array( + 'lines' => $lines, + 'marker' => 'Bar', + 'expected' => array( $lines, array(), array() ), + ), + 'empty section' => array( + 'lines' => array( '# BEGIN Foo', '# END Foo' ), + 'marker' => 'Foo', + 'expected' => array( array(), array(), array() ), + ), + 'no lines' => array( + 'lines' => array(), + 'marker' => 'Foo', + 'expected' => array( array(), array(), array() ), + ), + ); + } +}