WordPress.org

Making WordPress.org

Changeset 7801


Ignore:
Timestamp:
10/28/2018 02:07:44 PM (22 months ago)
Author:
ocean90
Message:

Translate: Extend wporg-translate make-core-pot command to support generating POT files for WordPress 3.7+.

See #3748.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-make-core-pot.php

    r7792 r7801  
    1111
    1212    const PACKAGE_NAME = 'WordPress';
     13
     14    /**
     15     * WordPress directory to scan for string extraction.
     16     *
     17     * @var string
     18     */
     19    protected $source;
     20
     21    /**
     22     * Directory to store resulting POT files.
     23     *
     24     * @var string
     25     */
     26    protected $destination;
    1327
    1428    /**
     
    2741     */
    2842    public function __invoke( $args, $assoc_args ) {
    29         $source      = trailingslashit( realpath( $args[0] ) );
    30         $destination = trailingslashit( realpath( $args[1] ) );
     43        $this->source      = realpath( $args[0] );
     44        $this->destination = realpath( $args[1] );
     45
     46        $wp_version = $this->get_wp_version();
     47        if ( ! $wp_version ) {
     48            WP_CLI::error( 'WordPress version not found.' );
     49        }
     50
     51        if ( version_compare( $wp_version, '3.7-beta', '<' ) ) {
     52            WP_CLI::error( 'Unsupported WordPress version. Use makepot.php.' );
     53        }
    3154
    3255        $dry_run = Utils\get_flag_value( $assoc_args, 'dry-run', false );
     
    4366
    4467        // Continents and cities.
    45         $command  = 'i18n make-pot ' . escapeshellarg( $source );
    46         $command .= ' ' . escapeshellarg( $destination . 'wordpress-continents-cities.pot' );
     68        $command  = 'i18n make-pot ' . escapeshellarg( $this->source );
     69        $command .= ' ' . escapeshellarg( $this->destination . '/wordpress-continents-cities.pot' );
    4770        $command .= ' --include="wp-admin/includes/continents-cities.php"';
    4871        $command .= ' --package-name=' . escapeshellarg( self::PACKAGE_NAME );
     
    5679
    5780        // Front end.
    58         $command  = 'i18n make-pot ' . escapeshellarg( $source );
    59         $command .= ' ' . escapeshellarg( $destination . 'wordpress.pot' );
     81        $command  = 'i18n make-pot ' . escapeshellarg( $this->source );
     82        $command .= ' ' . escapeshellarg( $this->destination . '/wordpress.pot' );
    6083        $command .= ' --exclude="wp-admin/*,wp-content/themes/*,wp-includes/class-pop3.php,wp-content/plugins/akismet/"';
    6184        $command .= ' --package-name=' . escapeshellarg( self::PACKAGE_NAME );
     
    7194        $hello_dolly_pot = wp_tempnam( 'hello-dolly.pot' );
    7295
    73         $command  = 'i18n make-pot ' . escapeshellarg( $source . 'wp-content/plugins' );
     96        $command  = 'i18n make-pot ' . escapeshellarg( $this->source . '/wp-content/plugins' );
    7497        $command .= ' ' . escapeshellarg( $hello_dolly_pot );
    7598        $command .= ' --exclude="akismet/*"';
     
    84107        ! $dry_run && WP_CLI::runcommand( $command/*, [ 'launch' => false ]*/ );
    85108
    86         # Admin.
    87         $command  = 'i18n make-pot ' . escapeshellarg( $source );
    88         $command .= ' ' . escapeshellarg( $destination . 'wordpress-admin.pot' );
    89         $command .= ' --exclude="wp-admin/network/*,wp-admin/network.php,wp-admin/includes/class-wp-ms*,wp-admin/includes/network.php,wp-admin/includes/continents-cities.php"';
     109        // Admin.
     110        $admin_network_files = [
     111            'wp-admin/network/*',
     112            'wp-admin/network.php',
     113        ];
     114
     115        // See https://core.trac.wordpress.org/ticket/34910.
     116        if ( version_compare( $wp_version, '4.5-beta', '>=' ) ) {
     117            $admin_network_files = array_merge( $admin_network_files, [
     118                'wp-admin/includes/class-wp-ms*',
     119                'wp-admin/includes/network.php',
     120            ] );
     121        }
     122
     123        $command  = 'i18n make-pot ' . escapeshellarg( $this->source );
     124        $command .= ' ' . escapeshellarg( $this->destination . '/wordpress-admin.pot' );
     125        $command .= ' --exclude=' . escapeshellarg( implode( ',', array_merge( [ 'wp-admin/includes/continents-cities.php' ], $admin_network_files ) ) );
    90126        $command .= ' --include="wp-admin/*"';
    91127        $command .= ' --merge=' . escapeshellarg( $hello_dolly_pot );
    92         $command .= ' --subtract=' . escapeshellarg( $destination . 'wordpress.pot' );
     128        $command .= ' --subtract=' . escapeshellarg( $this->destination . '/wordpress.pot' );
    93129        $command .= ' --package-name=' . escapeshellarg( self::PACKAGE_NAME );
    94130        $command .= ' --headers=' . escapeshellarg( $headers );
     
    102138        unlink( $hello_dolly_pot );
    103139
    104         # Admin Network.
    105         $command  = 'i18n make-pot ' . escapeshellarg( $source );
    106         $command .= ' ' . escapeshellarg( $destination . 'wordpress-admin-network.pot' );
    107         $command .= ' --include="wp-admin/network/*,wp-admin/network.php,wp-admin/includes/class-wp-ms*,wp-admin/includes/network.php"';
    108         $command .= ' --subtract=' . escapeshellarg( sprintf( '%1$swordpress.pot,%1$swordpress-admin.pot', $destination ) );
     140        // Admin Network.
     141        $command  = 'i18n make-pot ' . escapeshellarg( $this->source );
     142        $command .= ' ' . escapeshellarg( $this->destination . '/wordpress-admin-network.pot' );
     143        $command .= ' --include=' . escapeshellarg( implode( ',', $admin_network_files ) );
     144        $command .= ' --subtract=' . escapeshellarg( sprintf( '%1$s/wordpress.pot,%1$s/wordpress-admin.pot', $this->destination ) );
    109145        $command .= ' --package-name=' . escapeshellarg( self::PACKAGE_NAME );
    110146        $command .= ' --headers=' . escapeshellarg( $headers );
     
    116152        ! $dry_run && WP_CLI::runcommand( $command/*, [ 'launch' => false ]*/ );
    117153    }
     154
     155    /**
     156     * Extracts the WordPress version number from wp-includes/version.php.
     157     *
     158     * @return string|false Version number on success, false otherwise.
     159     */
     160    private function get_wp_version() {
     161        $version_php = $this->source . '/wp-includes/version.php';
     162        if ( ! file_exists( $version_php ) || ! is_readable( $version_php ) ) {
     163            return false;
     164        }
     165
     166        return preg_match( '/\$wp_version\s*=\s*\'(.*?)\';/', file_get_contents( $version_php ), $matches ) ? $matches[1] : false;
     167    }
    118168}
Note: See TracChangeset for help on using the changeset viewer.