WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/20/2016 09:43:51 PM (5 years ago)
Author:
ocean90
Message:

i18n-tools: Respect the coding standards when adding textdomains with add-textdomain.php.

Props GaryJ, groovecoder.
Fixes #21616.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/i18n/add-textdomain.php

    r36600 r36603  
    1818     * Constructor.
    1919     */
    20     function __construct() {
     20    public function __construct() {
    2121        $makepot = new MakePOT;
    2222        $this->funcs = array_keys( $makepot->rules );
     
    2424    }
    2525
    26     function usage() {
     26    /**
     27     * Prints CLI usage.
     28     */
     29    public function usage() {
    2730        $usage = "Usage: php add-textdomain.php [-i] <domain> <file>\n\nAdds the string <domain> as a last argument to all i18n function calls in <file>\nand prints the modified php file on standard output.\n\nOptions:\n    -i    Modifies the PHP file in place, instead of printing it to standard output.\n";
    2831        fwrite(STDERR, $usage);
     
    3033    }
    3134
    32     function process_token($token_text, $inplace) {
    33         if ($inplace)
    34             $this->modified_contents .= $token_text;
    35         else
    36             echo $token_text;
     35    /**
     36     * Adds textdomain to a single file.
     37     *
     38     * @see AddTextdomain::process_string()
     39     *
     40     * @param string $domain          Text domain.
     41     * @param string $source_filename Filename with optional path.
     42     * @param bool   $inplace         True to modifies the PHP file in place. False to print to standard output.
     43     */
     44    public function process_file( $domain, $source_filename, $inplace ) {
     45        $new_source = $this->process_string( $domain, file_get_contents( $source_filename ) );
     46
     47        if ( $inplace ) {
     48            $f = fopen( $source_filename, 'w' );
     49            fwrite( $f, $new_source );
     50            fclose( $f );
     51        } else {
     52            echo $new_source;
     53        }
    3754    }
    3855
     56    /**
     57     * Adds textdomain to a string of PHP.
     58     *
     59     * Functions calls should be wrapped in opening and closing PHP delimiters as usual.
     60     *
     61     * @see AddTextdomain::process_tokens()
     62     *
     63     * @param string $domain Text domain.
     64     * @param string $string PHP code to parse.
     65     * @return string Modified source.
     66     */
     67    public function process_string( $domain, $string ) {
     68        $tokens = token_get_all( $string );
     69        return $this->process_tokens( $domain, $tokens );
     70    }
    3971
    40     function process_file($domain, $source_filename, $inplace) {
    41 
     72    /**
     73     * Adds textdomain to a set of PHP tokens.
     74     *
     75     * @param string $domain Text domain.
     76     * @param array  $tokens PHP tokens. An array of token identifiers. Each individual token identifier is either a
     77     *                       single character (i.e.: ;, ., >, !, etc.), or a three element array containing the token
     78     *                       index in element 0, the string content of the original token in element 1 and the line
     79     *                       number in element 2.
     80     * @return string Modified source.
     81     */
     82    public function process_tokens( $domain, $tokens ) {
    4283        $this->modified_contents = '';
    43         $domain = addslashes($domain);
    44 
    45         $source = file_get_contents($source_filename);
    46         $tokens = token_get_all($source);
     84        $domain = addslashes( $domain );
    4785
    4886        $in_func = false;
     
    5189        $found_domain = false;
    5290
    53         foreach($tokens as $token) {
     91        foreach($tokens as $index => $token) {
    5492            $string_success = false;
    5593            if (is_array($token)) {
     
    72110                --$parens_balance;
    73111                if ($in_func && 0 == $parens_balance) {
    74                     $token = $found_domain? ')' : ", '$domain')";
     112                    if ( ! $found_domain ) {
     113                        $token = ", '$domain'";
     114                        if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) {
     115                            $token .= ' '; // Maintain code standards if previously present
     116                            // Remove previous whitespace token to account for it.
     117                            $this->modified_contents = trim( $this->modified_contents );
     118                        }
     119                        $token .= ')';
     120                    }
    75121                    $in_func = false;
    76122                    $args_started = false;
     
    78124                }
    79125            }
    80             $this->process_token($token, $inplace);
     126            $this->modified_contents .= $token;
    81127        }
    82128
    83         if ($inplace) {
    84             $f = fopen($source_filename, 'w');
    85             fwrite($f, $this->modified_contents);
    86             fclose($f);
    87         }
     129        return $this->modified_contents;
    88130    }
    89131}
    90132
    91 
    92 // run the CLI only if the file
    93 // wasn't included
     133// Run the CLI only if the file wasn't included.
    94134$included_files = get_included_files();
    95135if ($included_files[0] == __FILE__) {
Note: See TracChangeset for help on using the changeset viewer.