Make WordPress Core

Ticket #26829: 26829.2.diff

File 26829.2.diff, 3.9 KB (added by Faison, 9 years ago)

New patch to allow for proper unit testing.

  • 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;
    7682                        if ( $state )
    7783                                $result[] = $markerline;
    78                         if (strpos($markerline, '# BEGIN ' . $marker) !== false)
     84                        if ( "# BEGIN {$marker}" === $markerline )
    7985                                $state = true;
    8086                }
    8187        }
     
    9298 *
    9399 * @since 1.5.0
    94100 *
    95  * @param unknown_type $filename
    96  * @param unknown_type $marker
    97  * @param unknown_type $insertion
     101 * @param string $filename
     102 * @param string $marker
     103 * @param array $insertion
     104 * @param object $wp_filesystem
    98105 * @return bool True on write success, false on failure.
    99106 */
    100 function insert_with_markers( $filename, $marker, $insertion ) {
    101         if (!file_exists( $filename ) || is_writeable( $filename ) ) {
    102                 if (!file_exists( $filename ) ) {
     107function insert_with_markers( $filename, $marker, $insertion, $wp_filesystem = null ) {
     108        if ( null == $wp_filesystem ) {
     109                WP_Filesystem();
     110                global $wp_filesystem;
     111        }
     112
     113        if ( ! $wp_filesystem->exists( $filename ) || $wp_filesystem->is_writable( $filename ) ) {
     114                if ( ! $wp_filesystem->exists( $filename ) ) {
    103115                        $markerdata = '';
    104116                } else {
    105                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
     117                        $markerdata = $wp_filesystem->get_contents_array( $filename );
    106118                }
    107119
    108                 if ( !$f = @fopen( $filename, 'w' ) )
    109                         return false;
     120                $new_markerdata = array();
    110121
    111122                $foundit = false;
    112123                if ( $markerdata ) {
    113124                        $state = true;
    114125                        foreach ( $markerdata as $n => $markerline ) {
    115                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
     126                                $markerline = rtrim( $markerline );
     127                                if ( "# BEGIN {$marker}" === $markerline )
    116128                                        $state = false;
    117129                                if ( $state ) {
    118                                         if ( $n + 1 < count( $markerdata ) )
    119                                                 fwrite( $f, "{$markerline}\n" );
    120                                         else
    121                                                 fwrite( $f, "{$markerline}" );
     130                                        $new_markerdata[] = $markerline;
    122131                                }
    123                                 if (strpos($markerline, '# END ' . $marker) !== false) {
    124                                         fwrite( $f, "# BEGIN {$marker}\n" );
     132                                if ( "# END {$marker}" === $markerline ) {
     133                                        $new_markerdata[] = "# BEGIN {$marker}";
    125134                                        if ( is_array( $insertion ))
    126135                                                foreach ( $insertion as $insertline )
    127                                                         fwrite( $f, "{$insertline}\n" );
    128                                         fwrite( $f, "# END {$marker}\n" );
     136                                                        $new_markerdata[] = $insertline;
     137                                        $new_markerdata[] = "# END {$marker}";
    129138                                        $state = true;
    130139                                        $foundit = true;
    131140                                }
    132141                        }
    133142                }
    134143                if (!$foundit) {
    135                         fwrite( $f, "\n# BEGIN {$marker}\n" );
     144                        $new_markerdata[] = '';
     145                        $new_markerdata[] = "# BEGIN {$marker}";
    136146                        foreach ( $insertion as $insertline )
    137                                 fwrite( $f, "{$insertline}\n" );
    138                         fwrite( $f, "# END {$marker}\n" );
     147                                $new_markerdata[] = $insertline;
     148                        $new_markerdata[] = "# END {$marker}";
    139149                }
    140                 fclose( $f );
    141                 return true;
    142         } else {
    143                 return false;
     150               
     151                if( $wp_filesystem->put_contents( $filename, implode( "\n", $new_markerdata ) ) ) {
     152                        return true;
     153                }
     154
    144155        }
     156       
     157        return false;
    145158}
    146159
    147160/**