Make WordPress Core


Ignore:
Timestamp:
12/13/2016 01:48:41 AM (10 years ago)
Author:
rmccue
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pomo/po.php

    r35714 r39591  
    168168         */
    169169        public static function prepend_each_line($string, $with) {
    170                 $php_with = var_export($with, true);
    171170                $lines = explode("\n", $string);
    172                 // do not prepend the string on the last empty line, artefact by explode
    173                 if ("\n" == substr($string, -1)) unset($lines[count($lines) - 1]);
    174                 $res = implode("\n", array_map(create_function('$x', "return $php_with.\$x;"), $lines));
    175                 // give back the empty line, we ignored above
    176                 if ("\n" == substr($string, -1)) $res .= "\n";
    177                 return $res;
     171                $append = '';
     172                if ('' === end($lines)) {
     173                        // Last line might be empty because $string was terminated
     174                        // with a newline, remove it from the $lines array,
     175                        // we'll restore state by re-terminating the string at the end
     176                        array_pop($lines);
     177                        $append = "\n";
     178                }
     179                foreach ($lines as &$line) {
     180                        $line = $with . $line;
     181                }
     182                unset($line);
     183                return implode("\n", $lines) . $append;
    178184        }
    179185
     
    281287
    282288        /**
     289         * Helper function for read_entry
     290         * @param string $context
     291         * @return bool
     292         */
     293        protected static function is_final($context) {
     294                return ($context === 'msgstr') || ($context === 'msgstr_plural');
     295        }
     296
     297        /**
    283298         * @param resource $f
    284299         * @param int      $lineno
     
    291306                $context = '';
    292307                $msgstr_index = 0;
    293                 $is_final = create_function('$context', 'return $context == "msgstr" || $context == "msgstr_plural";');
    294308                while (true) {
    295309                        $lineno++;
     
    297311                        if (!$line)  {
    298312                                if (feof($f)) {
    299                                         if ($is_final($context))
     313                                        if (self::is_final($context))
    300314                                                break;
    301315                                        elseif (!$context) // we haven't read a line and eof came
     
    311325                        if (preg_match('/^#/', $line, $m)) {
    312326                                // the comment is the start of a new entry
    313                                 if ($is_final($context)) {
     327                                if (self::is_final($context)) {
    314328                                        PO::read_line($f, 'put-back');
    315329                                        $lineno--;
     
    323337                                $this->add_comment_to_entry($entry, $line);
    324338                        } elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
    325                                 if ($is_final($context)) {
     339                                if (self::is_final($context)) {
    326340                                        PO::read_line($f, 'put-back');
    327341                                        $lineno--;
     
    334348                                $entry->context .= PO::unpoify($m[1]);
    335349                        } elseif (preg_match('/^msgid\s+(".*")/', $line, $m)) {
    336                                 if ($is_final($context)) {
     350                                if (self::is_final($context)) {
    337351                                        PO::read_line($f, 'put-back');
    338352                                        $lineno--;
     
    384398                        }
    385399                }
    386                 if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) {
     400
     401                $have_translations = false;
     402                foreach ( $entry->translations as $t ) {
     403                        if ( $t || ('0' === $t) ) {
     404                                $have_translations = true;
     405                                break;
     406                        }
     407                }
     408                if ( false === $have_translations ) {
    387409                        $entry->translations = array();
    388410                }
     411
    389412                return array('entry' => $entry, 'lineno' => $lineno);
    390413        }
Note: See TracChangeset for help on using the changeset viewer.