Ticket #26829: 26829.3.diff
File 26829.3.diff, 4.2 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 if ( $state ) 82 } 83 if ( $state ) { 77 84 $result[] = $markerline; 78 if (strpos($markerline, '# BEGIN ' . $marker) !== false) 85 } 86 if ( "# BEGIN {$marker}" === $markerline ) { 79 87 $state = true; 88 } 80 89 } 81 90 } 82 91 … … 92 101 * 93 102 * @since 1.5.0 94 103 * 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 98 108 * @return bool True on write success, false on failure. 99 109 */ 100 function insert_with_markers( $filename, $marker, $insertion ) { 101 if (!file_exists( $filename ) || is_writeable( $filename ) ) { 102 if (!file_exists( $filename ) ) { 110 function 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 ) ) { 103 118 $markerdata = ''; 104 119 } else { 105 $markerdata = explode( "\n", implode( '', file( $filename ) ));120 $markerdata = $wp_filesystem->get_contents_array( $filename ); 106 121 } 107 122 108 if ( !$f = @fopen( $filename, 'w' ) ) 109 return false; 123 $new_markerdata = array(); 110 124 111 125 $foundit = false; 112 126 if ( $markerdata ) { 113 127 $state = true; 114 128 foreach ( $markerdata as $n => $markerline ) { 115 if (strpos($markerline, '# BEGIN ' . $marker) !== false) 129 $markerline = rtrim( $markerline ); 130 if ( "# BEGIN {$marker}" === $markerline ) { 116 131 $state = false; 132 } 117 133 if ( $state ) { 118 if ( $n + 1 < count( $markerdata ) ) 119 fwrite( $f, "{$markerline}\n" ); 120 else 121 fwrite( $f, "{$markerline}" ); 134 $new_markerdata[] = $markerline; 122 135 } 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}"; 129 144 $state = true; 130 145 $foundit = true; 131 146 } 132 147 } 133 148 } 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}"; 139 156 } 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 144 162 } 163 164 return false; 145 165 } 146 166 147 167 /**