Make WordPress Core

Ticket #26829: 26829.refreshed.2.diff

File 26829.refreshed.2.diff, 3.9 KB (added by chesio, 6 years ago)

Update for refreshed patch

  • 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
     
    6299 *
    63100 * @param string $filename
    64101 * @param string $marker
    65  * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
     102 * @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
    70107        if ( ! file_exists( $filename ) ) {
    71108                return $result;
    72109        }
    73110
    74         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
     111        $markerdata = @file( $filename, FILE_IGNORE_NEW_LINES );
    75112
    76         $state = false;
    77         foreach ( $markerdata as $markerline ) {
    78                 if ( false !== strpos( $markerline, '# END ' . $marker ) ) {
    79                         $state = false;
    80                         if ( $state ) {
    81                                 $result[] = $markerline;
    82                         }
    83                         if ( false !== strpos( $markerline, '# BEGIN ' . $marker ) ) {
    84                                 $state = true;
    85                         }
    86                 }
     113        if ( ! empty( $markerdata ) ) {
     114                // We are only interested in lines contained by marker.
     115                list( , $result, ) = split_by_markers( $markerdata, $marker );
    87116        }
    88117
    89118        return $result;
     
    90119}
    91120
    92121/**
    93  * 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
    94123 * BEGIN and END markers.
    95124 *
    96125 * Replaces existing marked info. Retains surrounding
     
    119148                $insertion = explode( "\n", $insertion );
    120149        }
    121150
    122         $start_marker = "# BEGIN {$marker}";
    123         $end_marker   = "# END {$marker}";
    124 
    125151        $fp = fopen( $filename, 'r+' );
    126152        if ( ! $fp ) {
    127153                return false;
     
    136162        }
    137163
    138164        // Split out the existing file into the preceding lines, and those that appear after the marker
    139         $pre_lines = $post_lines = $existing_lines = array();
    140         $found_marker = $found_end_marker = false;
    141         foreach ( $lines as $line ) {
    142                 if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
    143                         $found_marker = true;
    144                         continue;
    145                 } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
    146                         $found_end_marker = true;
    147                         continue;
    148                 }
    149                 if ( ! $found_marker ) {
    150                         $pre_lines[] = $line;
    151                 } elseif ( $found_marker && $found_end_marker ) {
    152                         $post_lines[] = $line;
    153                 } else {
    154                         $existing_lines[] = $line;
    155                 }
    156         }
     165        list($pre_lines, $existing_lines, $post_lines) = split_by_markers($lines, $marker);
    157166
    158167        // Check to see if there was a change
    159168        if ( $existing_lines === $insertion ) {
     
    166175        // Generate the new file data
    167176        $new_file_data = implode( "\n", array_merge(
    168177                $pre_lines,
    169                 array( $start_marker ),
     178                array( "# BEGIN {$marker}" ),
    170179                $insertion,
    171                 array( $end_marker ),
     180                array( "# END {$marker}" ),
    172181                $post_lines
    173182        ) );
    174183