Ticket #26829: 26829.2.diff
File 26829.2.diff, 3.9 KB (added by , 9 years ago) |
---|
-
wp-admin/includes/misc.php
56 56 * 57 57 * @since 1.5.0 58 58 * 59 * @param unknown_type $filename 60 * @param unknown_type $marker 59 * @param string $filename 60 * @param string $marker 61 * @param object $wp_filesystem 61 62 * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers. 62 63 */ 63 function extract_from_markers( $filename, $marker ) {64 function extract_from_markers( $filename, $marker, $wp_filesystem = null ) { 64 65 $result = array (); 65 66 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 ) ) { 67 73 return $result; 68 74 } 69 75 70 if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) )); 71 { 76 if ( $markerdata = $wp_filesystem->get_contents_array( $filename ) ) { 72 77 $state = false; 73 78 foreach ( $markerdata as $markerline ) { 74 if (strpos($markerline, '# END ' . $marker) !== false) 79 $markerline = rtrim( $markerline ); 80 if ( "# END {$marker}" === $markerline ) 75 81 $state = false; 76 82 if ( $state ) 77 83 $result[] = $markerline; 78 if ( strpos($markerline, '# BEGIN ' . $marker) !== false)84 if ( "# BEGIN {$marker}" === $markerline ) 79 85 $state = true; 80 86 } 81 87 } … … 92 98 * 93 99 * @since 1.5.0 94 100 * 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 98 105 * @return bool True on write success, false on failure. 99 106 */ 100 function insert_with_markers( $filename, $marker, $insertion ) { 101 if (!file_exists( $filename ) || is_writeable( $filename ) ) { 102 if (!file_exists( $filename ) ) { 107 function 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 ) ) { 103 115 $markerdata = ''; 104 116 } else { 105 $markerdata = explode( "\n", implode( '', file( $filename ) ));117 $markerdata = $wp_filesystem->get_contents_array( $filename ); 106 118 } 107 119 108 if ( !$f = @fopen( $filename, 'w' ) ) 109 return false; 120 $new_markerdata = array(); 110 121 111 122 $foundit = false; 112 123 if ( $markerdata ) { 113 124 $state = true; 114 125 foreach ( $markerdata as $n => $markerline ) { 115 if (strpos($markerline, '# BEGIN ' . $marker) !== false) 126 $markerline = rtrim( $markerline ); 127 if ( "# BEGIN {$marker}" === $markerline ) 116 128 $state = false; 117 129 if ( $state ) { 118 if ( $n + 1 < count( $markerdata ) ) 119 fwrite( $f, "{$markerline}\n" ); 120 else 121 fwrite( $f, "{$markerline}" ); 130 $new_markerdata[] = $markerline; 122 131 } 123 if ( strpos($markerline, '# END ' . $marker) !== false) {124 fwrite( $f, "# BEGIN {$marker}\n" );132 if ( "# END {$marker}" === $markerline ) { 133 $new_markerdata[] = "# BEGIN {$marker}"; 125 134 if ( is_array( $insertion )) 126 135 foreach ( $insertion as $insertline ) 127 fwrite( $f, "{$insertline}\n" );128 fwrite( $f, "# END {$marker}\n" );136 $new_markerdata[] = $insertline; 137 $new_markerdata[] = "# END {$marker}"; 129 138 $state = true; 130 139 $foundit = true; 131 140 } 132 141 } 133 142 } 134 143 if (!$foundit) { 135 fwrite( $f, "\n# BEGIN {$marker}\n" ); 144 $new_markerdata[] = ''; 145 $new_markerdata[] = "# BEGIN {$marker}"; 136 146 foreach ( $insertion as $insertline ) 137 fwrite( $f, "{$insertline}\n" );138 fwrite( $f, "# END {$marker}\n" );147 $new_markerdata[] = $insertline; 148 $new_markerdata[] = "# END {$marker}"; 139 149 } 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 144 155 } 156 157 return false; 145 158 } 146 159 147 160 /**