Make WordPress Core

Ticket #26829: 26829.3.diff

File 26829.3.diff, 4.2 KB (added by Faison, 9 years ago)

New patch that adds brackets to single-lined if's and foreach's

  • wp-admin/includes/misc.php

     
    5656 *
    5757 * @since 1.5.0
    5858 *
    59  * @param unknown_type $filename
    60  * @param unknown_type $marker
     59 * @param string $filename
     60 * @param string $marker
     61 * @param object $wp_filesystem
    6162 * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
    6263 */
    63 function extract_from_markers( $filename, $marker ) {
     64function extract_from_markers( $filename, $marker, $wp_filesystem = null ) {
    6465        $result = array ();
    6566
    66         if (!file_exists( $filename ) ) {
     67        if ( null == $wp_filesystem ) {
     68                WP_Filesystem();
     69                global $wp_filesystem;
     70        }
     71
     72        if ( ! $wp_filesystem->exists( $filename ) ) {
    6773                return $result;
    6874        }
    6975
    70         if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
    71         {
     76        if ( $markerdata = $wp_filesystem->get_contents_array( $filename ) ) {
    7277                $state = false;
    7378                foreach ( $markerdata as $markerline ) {
    74                         if (strpos($markerline, '# END ' . $marker) !== false)
     79                        $markerline = rtrim( $markerline );
     80                        if ( "# END {$marker}" === $markerline ) {
    7581                                $state = false;
    76                         if ( $state )
     82                        }
     83                        if ( $state ) {
    7784                                $result[] = $markerline;
    78                         if (strpos($markerline, '# BEGIN ' . $marker) !== false)
     85                        }
     86                        if ( "# BEGIN {$marker}" === $markerline ) {
    7987                                $state = true;
     88                        }
    8089                }
    8190        }
    8291
     
    92101 *
    93102 * @since 1.5.0
    94103 *
    95  * @param unknown_type $filename
    96  * @param unknown_type $marker
    97  * @param unknown_type $insertion
     104 * @param string $filename
     105 * @param string $marker
     106 * @param array $insertion
     107 * @param object $wp_filesystem
    98108 * @return bool True on write success, false on failure.
    99109 */
    100 function insert_with_markers( $filename, $marker, $insertion ) {
    101         if (!file_exists( $filename ) || is_writeable( $filename ) ) {
    102                 if (!file_exists( $filename ) ) {
     110function insert_with_markers( $filename, $marker, $insertion, $wp_filesystem = null ) {
     111        if ( null == $wp_filesystem ) {
     112                WP_Filesystem();
     113                global $wp_filesystem;
     114        }
     115
     116        if ( ! $wp_filesystem->exists( $filename ) || $wp_filesystem->is_writable( $filename ) ) {
     117                if ( ! $wp_filesystem->exists( $filename ) ) {
    103118                        $markerdata = '';
    104119                } else {
    105                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
     120                        $markerdata = $wp_filesystem->get_contents_array( $filename );
    106121                }
    107122
    108                 if ( !$f = @fopen( $filename, 'w' ) )
    109                         return false;
     123                $new_markerdata = array();
    110124
    111125                $foundit = false;
    112126                if ( $markerdata ) {
    113127                        $state = true;
    114128                        foreach ( $markerdata as $n => $markerline ) {
    115                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
     129                                $markerline = rtrim( $markerline );
     130                                if ( "# BEGIN {$marker}" === $markerline ) {
    116131                                        $state = false;
     132                                }
    117133                                if ( $state ) {
    118                                         if ( $n + 1 < count( $markerdata ) )
    119                                                 fwrite( $f, "{$markerline}\n" );
    120                                         else
    121                                                 fwrite( $f, "{$markerline}" );
     134                                        $new_markerdata[] = $markerline;
    122135                                }
    123                                 if (strpos($markerline, '# END ' . $marker) !== false) {
    124                                         fwrite( $f, "# BEGIN {$marker}\n" );
    125                                         if ( is_array( $insertion ))
    126                                                 foreach ( $insertion as $insertline )
    127                                                         fwrite( $f, "{$insertline}\n" );
    128                                         fwrite( $f, "# END {$marker}\n" );
     136                                if ( "# END {$marker}" === $markerline ) {
     137                                        $new_markerdata[] = "# BEGIN {$marker}";
     138                                        if ( is_array( $insertion ) ) {
     139                                                foreach ( $insertion as $insertline ) {
     140                                                        $new_markerdata[] = $insertline;
     141                                                }
     142                                        }
     143                                        $new_markerdata[] = "# END {$marker}";
    129144                                        $state = true;
    130145                                        $foundit = true;
    131146                                }
    132147                        }
    133148                }
    134                 if (!$foundit) {
    135                         fwrite( $f, "\n# BEGIN {$marker}\n" );
    136                         foreach ( $insertion as $insertline )
    137                                 fwrite( $f, "{$insertline}\n" );
    138                         fwrite( $f, "# END {$marker}\n" );
     149                if ( ! $foundit ) {
     150                        $new_markerdata[] = '';
     151                        $new_markerdata[] = "# BEGIN {$marker}";
     152                        foreach ( $insertion as $insertline ) {
     153                                $new_markerdata[] = $insertline;
     154                        }
     155                        $new_markerdata[] = "# END {$marker}";
    139156                }
    140                 fclose( $f );
    141                 return true;
    142         } else {
    143                 return false;
     157               
     158                if( $wp_filesystem->put_contents( $filename, implode( "\n", $new_markerdata ) ) ) {
     159                        return true;
     160                }
     161
    144162        }
     163       
     164        return false;
    145165}
    146166
    147167/**