WordPress.org

Make WordPress Core

Ticket #26829: 26829.refreshed.diff

File 26829.refreshed.diff, 3.8 KB (added by chesio, 3 years ago)

Refreshed patch using new split_by_markers() function to consistently handle lines splitting

  • src/wp-admin/includes/misc.php

     
    5656}
    5757
    5858/**
     59 * Split input lines into lines preceding, contained by and following BEGIN and
     60 * END markers (in the .htaccess file).
     61 *
     62 * @param array $lines Input file lines without newline characters.
     63 * @param string $marker
     64 * @return array Preceding, existing and following lines as arrays.
     65 */
     66function split_by_markers( $lines, $marker ) {
     67        $start_marker = "# BEGIN {$marker}";
     68        $end_marker   = "# END {$marker}";
     69
     70        $pre_lines = $post_lines = $existing_lines = array();
     71        $found_start_marker = $found_end_marker = false;
     72
     73        foreach ( $lines as $line ) {
     74                if ( ! $found_start_marker && ( $line === $start_marker ) ) {
     75                        $found_start_marker = true;
     76                        continue;
     77                }
     78                if ( ! $found_end_marker && ( $line === $end_marker ) ) {
     79                        $found_end_marker = true;
     80                        continue;
     81                }
     82
     83                if ( ! $found_start_marker ) {
     84                        $pre_lines[] = $line;
     85                } elseif ( ! $found_end_marker ) {
     86                        $existing_lines[] = $line;
     87                } else {
     88                        $post_lines[] = $line;
     89                }
     90        }
     91
     92        return array( $pre_lines, $existing_lines, $post_lines );
     93}
     94
     95/**
    5996 * Extracts strings from between the BEGIN and END markers in the .htaccess file.
    6097 *
    6198 * @since 1.5.0
     
    65102 * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
    66103 */
    67104function extract_from_markers( $filename, $marker ) {
    68         $result = array ();
     105        $result = array();
    69106
    70         if (!file_exists( $filename ) ) {
     107        if ( ! file_exists( $filename ) ) {
    71108                return $result;
    72109        }
    73110
    74         if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
    75         {
    76                 $state = false;
    77                 foreach ( $markerdata as $markerline ) {
    78                         if (strpos($markerline, '# END ' . $marker) !== false)
    79                                 $state = false;
    80                         if ( $state )
    81                                 $result[] = $markerline;
    82                         if (strpos($markerline, '# BEGIN ' . $marker) !== false)
    83                                 $state = true;
    84                 }
     111        $markerdata = @file( $filename, FILE_IGNORE_NEW_LINES );
     112
     113        if ( ! empty ( $markerdata ) ) {
     114                // We are only interested in lines contained by marker.
     115                list( , $result, ) = split_by_markers($markerdata, $marker);
    85116        }
    86117
    87118        return $result;
     
    88119}
    89120
    90121/**
    91  * Inserts an array of strings into a file (.htaccess ), placing it between
     122 * Inserts an array of strings into a file (.htaccess), placing it between
    92123 * BEGIN and END markers.
    93124 *
    94125 * Replaces existing marked info. Retains surrounding
     
    117148                $insertion = explode( "\n", $insertion );
    118149        }
    119150
    120         $start_marker = "# BEGIN {$marker}";
    121         $end_marker   = "# END {$marker}";
    122 
    123151        $fp = fopen( $filename, 'r+' );
    124152        if ( ! $fp ) {
    125153                return false;
     
    134162        }
    135163
    136164        // Split out the existing file into the preceding lines, and those that appear after the marker
    137         $pre_lines = $post_lines = $existing_lines = array();
    138         $found_marker = $found_end_marker = false;
    139         foreach ( $lines as $line ) {
    140                 if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
    141                         $found_marker = true;
    142                         continue;
    143                 } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
    144                         $found_end_marker = true;
    145                         continue;
    146                 }
    147                 if ( ! $found_marker ) {
    148                         $pre_lines[] = $line;
    149                 } elseif ( $found_marker && $found_end_marker ) {
    150                         $post_lines[] = $line;
    151                 } else {
    152                         $existing_lines[] = $line;
    153                 }
    154         }
     165        list($pre_lines, $existing_lines, $post_lines) = split_by_markers($lines, $marker);
    155166
    156167        // Check to see if there was a change
    157168        if ( $existing_lines === $insertion ) {
     
    164175        // Generate the new file data
    165176        $new_file_data = implode( "\n", array_merge(
    166177                $pre_lines,
    167                 array( $start_marker ),
     178                array( "# BEGIN {$marker}" ),
    168179                $insertion,
    169                 array( $end_marker ),
     180                array( "# END {$marker}" ),
    170181                $post_lines
    171182        ) );
    172183