Skip to content

Commit

Permalink
Merge pull request #13 from wp-cli/functions-fix
Browse files Browse the repository at this point in the history
Extract all supported functions.
  • Loading branch information
schlessera authored Jan 22, 2018
2 parents 0ed7e09 + 3d5d0b6 commit 3771364
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 41 deletions.
188 changes: 188 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,191 @@ Feature: Generate a POT file of a WordPress plugin
"""
And STDERR should be empty
And the foo-plugin/languages/foo-plugin.pot file should exist

Scenario: Extract all supported functions
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
*/
__( '__', 'foo-plugin' );
esc_attr__( 'esc_attr__', 'foo-plugin' );
esc_html__( 'esc_html__', 'foo-plugin' );
_e( '_e', 'foo-plugin' );
esc_attr_e( 'esc_attr_e', 'foo-plugin' );
esc_html_e( 'esc_html_e', 'foo-plugin' );
_x( '_x', '_x_context', 'foo-plugin' );
_ex( '_ex', '_ex_context', 'foo-plugin' );
esc_attr_x( 'esc_attr_x', 'esc_attr_x_context', 'foo-plugin' );
esc_html_x( 'esc_html_x', 'esc_html_x_context', 'foo-plugin' );
_n( '_n_single', '_n_plural', $number, 'foo-plugin' );
_nx( '_nx_single', '_nx_plural', $number, '_nx_context', 'foo-plugin' );
_n_noop( '_n_noop_single', '_n_noop_plural', 'foo-plugin' );
_nx_noop( '_nx_noop_single', '_nx_noop_plural', '_nx_noop_context', 'foo-plugin' );
// Compat.
_( '_', 'foo-plugin' );
// Deprecated.
_c( '_c', 'foo-plugin' );
_nc( '_nc_single', '_nc_plural', $number, 'foo-plugin' );
__ngettext( '__ngettext_single', '__ngettext_plural', $number, 'foo-plugin' );
__ngettext_noop( '__ngettext_noop_single', '__ngettext_noop_plural', 'foo-plugin' );
__unsupported_func( '__unsupported_func', 'foo-plugin' );
__( 'wrong-domain', 'wrong-domain' );
"""

When I run `wp makepot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And the foo-plugin/foo-plugin.pot file should exist
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "esc_attr__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "esc_html__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_e"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "esc_attr_e"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "esc_html_e"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_x"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "_x_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_ex"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "_ex_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "esc_attr_x"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "esc_attr_x_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "esc_html_x"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "esc_html_x_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_n_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "_n_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_nx_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "_nx_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "_nx_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_n_noop_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "_n_noop_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_nx_noop_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "_nx_noop_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "_nx_noop_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_c"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "_nc_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "_nc_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "__ngettext_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "__ngettext_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "__ngettext_noop_single"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid_plural "__ngettext_noop_plural"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "__"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "__unsupported_func"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "wrong-domain"
"""
45 changes: 23 additions & 22 deletions src/WordPress_Code_Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ class WordPress_Code_Extractor extends PhpCode {
'extractComments' => 'translators',
'constants' => [],
'functions' => [
'_' => 'dgettext',
'__' => 'dgettext',
'_e' => 'dgettext',
'_c' => 'pgettext',
'_n' => 'ngettext',
'_n_noop' => 'noop',
'_nc' => 'dgettext',
'__ngettext' => 'dgettext',
'__ngettext_noop' => 'noop',
'_x' => 'pgettext',
'_ex' => 'pgettext',
'_nx' => 'dnpgettext',
'_nx_noop' => 'noop',
'_n_js' => 'ngettext',
'_nx_js' => 'npgettext',
'esc_attr__' => 'dgettext',
'esc_html__' => 'dgettext',
'esc_attr_e' => 'dgettext',
'esc_html_e' => 'dgettext',
'esc_attr_x' => 'dgettext',
'esc_html_x' => 'pgettext',
'comments_number_link' => 'ngettext',
'__' => 'text_domain',
'esc_attr__' => 'text_domain',
'esc_html__' => 'text_domain',
'_e' => 'text_domain',
'esc_attr_e' => 'text_domain',
'esc_html_e' => 'text_domain',
'_x' => 'text_context_domain',
'_ex' => 'text_context_domain',
'esc_attr_x' => 'text_context_domain',
'esc_html_x' => 'text_context_domain',
'_n' => 'single_plural_number_domain',
'_nx' => 'single_plural_number_context_domain',
'_n_noop' => 'single_plural_domain',
'_nx_noop' => 'single_plural_context_domain',

// Compat.
'_' => 'gettext', // Same as 'text_domain'.

// Deprecated.
'_c' => 'text_domain',
'_nc' => 'single_plural_number_domain',
'__ngettext' => 'single_plural_number_domain',
'__ngettext_noop' => 'single_plural_domain',
],
];

Expand Down
39 changes: 20 additions & 19 deletions src/WordPress_Functions_Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,58 @@ public function saveGettextFunctions( Translations $translations, array $options
$domain = $context = $original = $plural = null;

switch ( $functions[ $name ] ) {
case 'pgettext':
if ( ! isset( $args[1] ) ) {
continue 2;
}

list( $original, $context ) = $args;
break;

case 'dgettext':
case 'text_domain':
case 'gettext':
if ( ! isset( $args[1] ) ) {
continue 2;
}

list( $original, $domain ) = $args;
break;

case 'dpgettext':
case 'text_context_domain':
if ( ! isset( $args[2] ) ) {
continue 2;
}

list( $original, $context, $domain ) = $args;
break;

case 'npgettext':
if ( ! isset( $args[2] ) ) {
case 'single_plural_number_domain':
if ( ! isset( $args[3] ) ) {
continue 2;
}

list( $original, $plural, $context ) = $args;
list( $original, $plural, $number, $domain ) = $args;
break;

case 'dnpgettext':
if ( ! isset( $args[3] ) ) {
case 'single_plural_number_context_domain':
if ( ! isset( $args[4] ) ) {
continue 2;
}

list( $original, $plural, $context, $domain ) = $args;
list( $original, $plural, $number, $context, $domain ) = $args;
break;

case 'dngettext':
case 'single_plural_domain':
if ( ! isset( $args[2] ) ) {
continue 2;
}

list( $original, $plural, $domain ) = $args;
break;

case 'single_plural_context_domain':
if ( ! isset( $args[3] ) ) {
continue 2;
}

list( $original, $plural, $context, $domain ) = $args;
break;

default:
parent::saveGettextFunctions( $translations, $options );
return;
// Should never happen.
\WP_CLI::error( sprintf( "Internal error: unknown function map '%s' for '%s'.", $functions[ $name ], $name ) );
}

// Todo: Require a domain?
Expand Down

0 comments on commit 3771364

Please sign in to comment.