Navigation Menu

Skip to content

Commit

Permalink
General: Remove most uses of create_function()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rmccue committed Dec 13, 2016
1 parent 6e2d9af commit fbc00b3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
30 changes: 28 additions & 2 deletions wp-includes/atomlib.php
Expand Up @@ -94,8 +94,8 @@ function __construct() {

$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' );
}

/**
Expand All @@ -105,6 +105,32 @@ public function 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";
Expand Down
49 changes: 36 additions & 13 deletions wp-includes/pomo/po.php
Expand Up @@ -167,14 +167,20 @@ public static function unpoify($string) {
* @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;
}

/**
Expand Down Expand Up @@ -279,6 +285,15 @@ function import_from_file($filename) {
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
Expand All @@ -290,13 +305,12 @@ function read_entry($f, $lineno = 0) {
// 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;
Expand All @@ -310,7 +324,7 @@ function read_entry($f, $lineno = 0) {
$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;
Expand All @@ -322,7 +336,7 @@ function read_entry($f, $lineno = 0) {
// 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;
Expand All @@ -333,7 +347,7 @@ function read_entry($f, $lineno = 0) {
$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;
Expand Down Expand Up @@ -383,9 +397,18 @@ function read_entry($f, $lineno = 0) {
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);
}

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Expand Up @@ -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.
Expand Down

0 comments on commit fbc00b3

Please sign in to comment.