Make WordPress Core

Opened 19 years ago

Closed 19 years ago

Last modified 17 years ago

#1417 closed defect (bug) (fixed)

The method insert_with_markers() does not perform as it is supposed to

Reported by: ericanderson's profile EricAnderson Owned by: markjaquith's profile markjaquith
Milestone: Priority: high
Severity: major Version: 1.5.1.1
Component: Administration Keywords: bg|commit
Focuses: Cc:

Description

This method does not insert a new line after the # END {$marker} code, which causes it (somehow, yet unknown) to erase the # END {$marker} line, so when plugin calls this code, the endline gets erased and is replaced with a # BEGIN {$new_marker} ... now if the original code calls the insert_with_markers again, it never sees its end line, and thus replaces everything.

The code is more complicated than it needs to be and the attached replacement function removes the complicated code and solves the problem. The original code did some odd checking for whether to use a new line before the statement or not, but in simply using a new line after every statement, this is avoided.

function insert_with_markers($filename, $marker, $insertion) {
	if (!file_exists($filename) || is_writeable($filename)) {
		if (!file_exists($filename)) {
			$markerdata = '';
		} else {
			$markerdata = explode("\n", implode('', file($filename)));
		}

		print_r($markerdata);

		$f = fopen($filename, 'w');
		$foundit = false;
		if ($markerdata) {
			$state = true;
			foreach($markerdata as $markerline) {
				if (strstr($markerline, "# BEGIN {$marker}\n")) $state = false;
				if ($state) fwrite($f, "{$markerline}\n");
				if (strstr($markerline, "# END {$marker}\n")) {
					fwrite($f, "# BEGIN {$marker}\n");
					if(is_array($insertion)) foreach($insertion as $insertline) fwrite($f, "{$insertline}\n");
					fwrite($f, "# END {$marker}\n");
					$state = true;
					$foundit = true;
				}
			}
		}
		if (!$foundit) {
			fwrite($f, "# BEGIN {$marker}\n");
			foreach($insertion as $insertline) fwrite($f, "{$insertline}\n");
			fwrite($f, "# END {$marker}\n");				
		}
		fclose($f);
		return true;
	} else {
		return false;
	}
}

Attachments (2)

insert_with_markers.2.php (1.3 KB) - added by EricAnderson 19 years ago.
admin-functions.php.diff (1.9 KB) - added by markjaquith 19 years ago.
Patch against current 1.6 SVN

Download all attachments as: .zip

Change History (10)

#1 @EricAnderson
19 years ago

I forgot to mention that the code for this block starts at line 476 (atleast in 1.5.1.1) of /wp-admin/admin-functions.php

#2 @EricAnderson
19 years ago

Oops! Further investigation shows the problem has to do with the last line in the file not having an extra newline at the end of it. The same fix applies.

#3 @EricAnderson
19 years ago

I just noticed I left my debug print_r() in the file I attached as well as the pasted code... That needs to be removed.

Since I can't attach the file again, you can grab the new method here:

http://ericanderson.us/wp-content/ea.us/files/wordpress-fixes/insert_with_markers.zip

Sorry for the inconvience.

#4 @EricAnderson
19 years ago

Doh! Yet again, this bug tracking software tricked me, so now the file is uploaded, ignore the first one :)

#5 @matt
19 years ago

  • Milestone set to 1.5.2

@markjaquith
19 years ago

Patch against current 1.6 SVN

#6 @markjaquith
19 years ago

  • Keywords bg|commit added
  • Owner changed from anonymous to markjaquith
  • Priority changed from normal to high
  • Severity changed from normal to major
  • Status changed from new to assigned

#7 @ryan
19 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [2797]) Improved insert_with_markers(). Props Eric Anderson. fixes #1417

#8 @(none)
17 years ago

  • Milestone 2.0 deleted

Milestone 2.0 deleted

Note: See TracTickets for help on using tickets.