Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
require_once dirname( __FILE__ ) . '/makepot.php';
if ( !defined( 'STDERR' ) ) {
define( 'STDERR', fopen( 'php://stderr', 'w' ) );
}
/**
* Extend to add text domain check
*/
class TS_StringExtractor extends StringExtractor {
public $text_domain = '';
public function __construct( $rules, $text_domain ) {
$this->text_domain = $text_domain;
return parent::__construct( $rules );
}
public function find_function_calls( $function_names, $code) {
$calls = parent::find_function_calls( $function_names, $code );
/**
* Check text-domain
*/
if ( !empty($this->text_domain) ) {
foreach ( $calls as $key => $call ) {
// Use the number of args from rules and assume the last is text-domain
$domain_index = count( $this->rules[ $call['name'] ] );
$text_domain = $call['args'][ $domain_index ];
// Remove invalid text-domain
if ( $text_domain !== $this->text_domain ) {
unset( $calls[ $key ] );
}
}
}
return $calls;
}
}
/**
* Extend to add support for text-domain variable
*/
class TS_MakePOT extends MakePOT
{
public function wp_theme( $dir, $text_domain = null, $output = null ) {
if ( !empty($text_domain) ) {
$this->extractor = new TS_StringExtractor( $this->rules, $text_domain );
}
return parent::wp_theme( $dir, $output, $text_domain );
}
}
// run the CLI only if the file
// wasn't included
$included_files = get_included_files();
if ($included_files[0] == __FILE__) {
$makepot = new TS_MakePOT;
if ((4 == count($argv) || 5 == count($argv)) && in_array($method = str_replace('-', '_', $argv[1]), get_class_methods($makepot))) {
$res = call_user_func(array(&$makepot, $method), realpath($argv[2]), isset($argv[3])? $argv[3] : null, isset($argv[4])? $argv[4] : null);
if (false === $res) {
fwrite(STDERR, "Couldn't generate POT file!\n");
}
} else {
$usage = "Usage: php ts-make-pot.php PROJECT DIRECTORY TEXTDOMAIN [OUTPUT]\n\n";
$usage .= "Generate POT file from the files in DIRECTORY [OUTPUT]\n";
$usage .= "Available projects: ".implode(', ', $makepot->projects)."\n";
fwrite(STDERR, $usage);
exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment