WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#1417 closed defect (bug) (fixed)

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

Reported by: EricAnderson Owned by: 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 9 years ago.
admin-functions.php.diff (1.9 KB) - added by markjaquith 9 years ago.
Patch against current 1.6 SVN

Download all attachments as: .zip

Change History (10)

comment:1 EricAnderson9 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

comment:2 EricAnderson9 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.

comment:3 EricAnderson9 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.

comment:4 EricAnderson9 years ago

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

comment:5 matt9 years ago

  • Milestone set to 1.5.2

markjaquith9 years ago

Patch against current 1.6 SVN

comment:6 markjaquith9 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

comment:7 ryan9 years ago

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

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

comment:8 anonymous7 years ago

  • Milestone 2.0 deleted

Milestone 2.0 deleted

Note: See TracTickets for help on using tickets.