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. git-svn-id: https://develop.svn.wordpress.org/trunk@39591 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
b171b64d3c
commit
32ed2a1ffa
@ -94,8 +94,8 @@ class AtomParser {
|
|||||||
|
|
||||||
$this->feed = new AtomFeed();
|
$this->feed = new AtomFeed();
|
||||||
$this->current = null;
|
$this->current = null;
|
||||||
$this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
|
$this->map_attrs_func = array( __CLASS__, 'map_attrs' );
|
||||||
$this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
|
$this->map_xmlns_func = array( __CLASS__, 'map_xmlns' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -105,6 +105,32 @@ class AtomParser {
|
|||||||
self::__construct();
|
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) {
|
function _p($msg) {
|
||||||
if($this->debug) {
|
if($this->debug) {
|
||||||
print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
|
print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
|
||||||
|
@ -167,14 +167,20 @@ class PO extends Gettext_Translations {
|
|||||||
* @param string $with prepend lines with this string
|
* @param string $with prepend lines with this string
|
||||||
*/
|
*/
|
||||||
public static function prepend_each_line($string, $with) {
|
public static function prepend_each_line($string, $with) {
|
||||||
$php_with = var_export($with, true);
|
|
||||||
$lines = explode("\n", $string);
|
$lines = explode("\n", $string);
|
||||||
// do not prepend the string on the last empty line, artefact by explode
|
$append = '';
|
||||||
if ("\n" == substr($string, -1)) unset($lines[count($lines) - 1]);
|
if ('' === end($lines)) {
|
||||||
$res = implode("\n", array_map(create_function('$x', "return $php_with.\$x;"), $lines));
|
// Last line might be empty because $string was terminated
|
||||||
// give back the empty line, we ignored above
|
// with a newline, remove it from the $lines array,
|
||||||
if ("\n" == substr($string, -1)) $res .= "\n";
|
// we'll restore state by re-terminating the string at the end
|
||||||
return $res;
|
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;
|
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 resource $f
|
||||||
* @param int $lineno
|
* @param int $lineno
|
||||||
@ -290,13 +305,12 @@ class PO extends Gettext_Translations {
|
|||||||
// can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
|
// can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
|
||||||
$context = '';
|
$context = '';
|
||||||
$msgstr_index = 0;
|
$msgstr_index = 0;
|
||||||
$is_final = create_function('$context', 'return $context == "msgstr" || $context == "msgstr_plural";');
|
|
||||||
while (true) {
|
while (true) {
|
||||||
$lineno++;
|
$lineno++;
|
||||||
$line = PO::read_line($f);
|
$line = PO::read_line($f);
|
||||||
if (!$line) {
|
if (!$line) {
|
||||||
if (feof($f)) {
|
if (feof($f)) {
|
||||||
if ($is_final($context))
|
if (self::is_final($context))
|
||||||
break;
|
break;
|
||||||
elseif (!$context) // we haven't read a line and eof came
|
elseif (!$context) // we haven't read a line and eof came
|
||||||
return null;
|
return null;
|
||||||
@ -310,7 +324,7 @@ class PO extends Gettext_Translations {
|
|||||||
$line = trim($line);
|
$line = trim($line);
|
||||||
if (preg_match('/^#/', $line, $m)) {
|
if (preg_match('/^#/', $line, $m)) {
|
||||||
// the comment is the start of a new entry
|
// the comment is the start of a new entry
|
||||||
if ($is_final($context)) {
|
if (self::is_final($context)) {
|
||||||
PO::read_line($f, 'put-back');
|
PO::read_line($f, 'put-back');
|
||||||
$lineno--;
|
$lineno--;
|
||||||
break;
|
break;
|
||||||
@ -322,7 +336,7 @@ class PO extends Gettext_Translations {
|
|||||||
// add comment
|
// add comment
|
||||||
$this->add_comment_to_entry($entry, $line);
|
$this->add_comment_to_entry($entry, $line);
|
||||||
} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
|
} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
|
||||||
if ($is_final($context)) {
|
if (self::is_final($context)) {
|
||||||
PO::read_line($f, 'put-back');
|
PO::read_line($f, 'put-back');
|
||||||
$lineno--;
|
$lineno--;
|
||||||
break;
|
break;
|
||||||
@ -333,7 +347,7 @@ class PO extends Gettext_Translations {
|
|||||||
$context = 'msgctxt';
|
$context = 'msgctxt';
|
||||||
$entry->context .= PO::unpoify($m[1]);
|
$entry->context .= PO::unpoify($m[1]);
|
||||||
} elseif (preg_match('/^msgid\s+(".*")/', $line, $m)) {
|
} elseif (preg_match('/^msgid\s+(".*")/', $line, $m)) {
|
||||||
if ($is_final($context)) {
|
if (self::is_final($context)) {
|
||||||
PO::read_line($f, 'put-back');
|
PO::read_line($f, 'put-back');
|
||||||
$lineno--;
|
$lineno--;
|
||||||
break;
|
break;
|
||||||
@ -383,9 +397,18 @@ class PO extends Gettext_Translations {
|
|||||||
return false;
|
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();
|
$entry->translations = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
return array('entry' => $entry, 'lineno' => $lineno);
|
return array('entry' => $entry, 'lineno' => $lineno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user