General: Remove most uses of create_function()

create_function() is equivalent to eval(), and most of our uses can be refactored. This is simpler, more secure, and slightly more performant.

Props sgolemon.
Fixes #37082.

Built from https://develop.svn.wordpress.org/trunk@39591


git-svn-id: http://core.svn.wordpress.org/trunk@39531 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan McCue 2016-12-13 01:49:39 +00:00
parent 6e2d9af8b6
commit fbc00b31ef
3 changed files with 65 additions and 16 deletions

View File

@ -94,8 +94,8 @@ class AtomParser {
$this->feed = new AtomFeed();
$this->current = null;
$this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
$this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
$this->map_attrs_func = array( __CLASS__, 'map_attrs' );
$this->map_xmlns_func = array( __CLASS__, 'map_xmlns' );
}
/**
@ -105,6 +105,32 @@ class AtomParser {
self::__construct();
}
/**
* Map attributes to key="val"
*
* @param string $k Key
* @param string $v Value
* @return string
*/
public static function map_attrs($k, $v) {
return "$k=\"$v\"";
}
/**
* Map XML namespace to string.
*
* @param indexish $p XML Namespace element index
* @param array $n Two-element array pair. [ 0 => {namespace}, 1 => {url} ]
* @return string 'xmlns="{url}"' or 'xmlns:{namespace}="{url}"'
*/
public static function map_xmlns($p, $n) {
$xd = "xmlns";
if( 0 < strlen($n[0]) ) {
$xd .= ":{$n[0]}";
}
return "{$xd}=\"{$n[1]}\"";
}
function _p($msg) {
if($this->debug) {
print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";

View File

@ -167,14 +167,20 @@ class PO extends Gettext_Translations {
* @param string $with prepend lines with this string
*/
public static function prepend_each_line($string, $with) {
$php_with = var_export($with, true);
$lines = explode("\n", $string);
// do not prepend the string on the last empty line, artefact by explode
if ("\n" == substr($string, -1)) unset($lines[count($lines) - 1]);
$res = implode("\n", array_map(create_function('$x', "return $php_with.\$x;"), $lines));
// give back the empty line, we ignored above
if ("\n" == substr($string, -1)) $res .= "\n";
return $res;
$append = '';
if ('' === end($lines)) {
// Last line might be empty because $string was terminated
// with a newline, remove it from the $lines array,
// we'll restore state by re-terminating the string at the end
array_pop($lines);
$append = "\n";
}
foreach ($lines as &$line) {
$line = $with . $line;
}
unset($line);
return implode("\n", $lines) . $append;
}
/**
@ -279,6 +285,15 @@ class PO extends Gettext_Translations {
return true;
}
/**
* Helper function for read_entry
* @param string $context
* @return bool
*/
protected static function is_final($context) {
return ($context === 'msgstr') || ($context === 'msgstr_plural');
}
/**
* @param resource $f
* @param int $lineno
@ -290,13 +305,12 @@ class PO extends Gettext_Translations {
// can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
$context = '';
$msgstr_index = 0;
$is_final = create_function('$context', 'return $context == "msgstr" || $context == "msgstr_plural";');
while (true) {
$lineno++;
$line = PO::read_line($f);
if (!$line) {
if (feof($f)) {
if ($is_final($context))
if (self::is_final($context))
break;
elseif (!$context) // we haven't read a line and eof came
return null;
@ -310,7 +324,7 @@ class PO extends Gettext_Translations {
$line = trim($line);
if (preg_match('/^#/', $line, $m)) {
// the comment is the start of a new entry
if ($is_final($context)) {
if (self::is_final($context)) {
PO::read_line($f, 'put-back');
$lineno--;
break;
@ -322,7 +336,7 @@ class PO extends Gettext_Translations {
// add comment
$this->add_comment_to_entry($entry, $line);
} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
if ($is_final($context)) {
if (self::is_final($context)) {
PO::read_line($f, 'put-back');
$lineno--;
break;
@ -333,7 +347,7 @@ class PO extends Gettext_Translations {
$context = 'msgctxt';
$entry->context .= PO::unpoify($m[1]);
} elseif (preg_match('/^msgid\s+(".*")/', $line, $m)) {
if ($is_final($context)) {
if (self::is_final($context)) {
PO::read_line($f, 'put-back');
$lineno--;
break;
@ -383,9 +397,18 @@ class PO extends Gettext_Translations {
return false;
}
}
if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) {
$have_translations = false;
foreach ( $entry->translations as $t ) {
if ( $t || ('0' === $t) ) {
$have_translations = true;
break;
}
}
if ( false === $have_translations ) {
$entry->translations = array();
}
return array('entry' => $entry, 'lineno' => $lineno);
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8-alpha-39590';
$wp_version = '4.8-alpha-39591';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.