WordPress.org

Making WordPress.org

Changeset 3057


Ignore:
Timestamp:
05/02/2016 02:32:40 PM (5 years ago)
Author:
ocean90
Message:

Translate, Theme Directory: Introduce Plugin::import_or_update_theme_on_status_change() to import a theme which can be triggered by the cron API.

See #1685.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/sync/class-translation-sync.php

    r3046 r3057  
    3131     * Starts the sync of plugin translations between two projects.
    3232     *
    33      * Gets trigged by the cron API and the hook `sync_plugin_translations`.
     33     * Gets triggered by the cron API and the hook `sync_plugin_translations`.
    3434     *
    3535     * @param array $args Arguments from the job. Should include the path
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/class-plugin.php

    r3056 r3057  
    4141     */
    4242    public function plugins_loaded() {
     43        add_action( 'wporg_translate_import_or_update_theme', array( $this, 'import_or_update_theme_on_status_change' ) );
     44
    4345        if ( defined( 'WP_CLI' ) && WP_CLI ) {
    4446            $this->register_cli_commands();
     
    4749
    4850    /**
     51     * Starts the import of a theme.
     52     *
     53     * Gets triggered by the cron API and the hook `wporg_translate_import_or_update_theme`.
     54     *
     55     * @param array $args Arguments from the job. Should include the slug and the version
     56     *                    of the theme.
     57     * @return bool False on failure, true on success.
     58     */
     59    public function import_or_update_theme_on_status_change( $args ) {
     60        $time = date( 'r' );
     61        $message = "_Time: {$time}_\nImport of theme {$args['theme']} {$args['version']} in process...\n";
     62
     63        // Import in a separate process.
     64        $cmd = WPORGTRANSLATE_WPCLI . ' wporg-translate set-theme-project ' . escapeshellarg( $args['theme'] ) . ' ' . escapeshellarg( $args['version'] );
     65        $output = '';
     66        $return_var = 0;
     67        exec( $cmd, $output, $return_var );
     68        if ( $return_var ) {
     69            $message .= "\tFailure: " . implode( "\n\t", $output ) . "\n";
     70        } else {
     71            $message .= "\t" . implode( "\n\t", $output ) . "\n";
     72            $message .= 'Import was successfully processed.';
     73        }
     74
     75        $attachment = [
     76            'title'      => "Import of {$args['theme']}",
     77            'title_link' => "https://translate.wordpress.org/projects/wp-themes/{$args['theme']}",
     78            'text'       => $message,
     79            'fallback'   => "Theme {$args['theme']} was imported.",
     80            'color'      => '#82878c',
     81            'mrkdwn_in'  => [ 'text' ],
     82        ];
     83        $this->slack( $attachment );
     84
     85        return true;
     86    }
     87
     88    /**
    4989     * Registers CLI commands if WP-CLI is loaded.
    5090     */
    51     function register_cli_commands() {
     91    public function register_cli_commands() {
    5292        WP_CLI::add_command( 'wporg-translate set-theme-project', __NAMESPACE__ . '\CLI\Set_Theme_Project' );
    5393    }
     94
     95    /**
     96     * Sends a notifcation to the the Slack channel.
     97     *
     98     * @param array $attachment The attachment of a notification.
     99     */
     100    private function slack( $attachment ) {
     101        if ( ! defined( 'GLOTPRESS_SLACK_WEBHOOK' ) ) {
     102            return;
     103        }
     104
     105        require_once API_WPORGPATH . 'includes/slack-config.php';
     106        $send = new \Dotorg\Slack\Send( GLOTPRESS_SLACK_WEBHOOK );
     107        $send->add_attachment( $attachment );
     108        $send->set_username( 'Theme Imports' );
     109        $send->send( '#meta-language-packs' );
     110    }
    54111}
Note: See TracChangeset for help on using the changeset viewer.