Making WordPress.org

Ticket #4377: 4377.diff

File 4377.diff, 3.5 KB (added by dd32, 6 years ago)
  • bin/import-plugin.php

     
    11<?php
    22namespace WordPressdotorg\Plugin_Directory;
    33
    44// This script should only be called in a CLI environment.
    55if ( 'cli' != php_sapi_name() ) {
    66        die();
    77}
    88
    99ob_start();
    1010
    11 $opts = getopt( '', array( 'url:', 'abspath:', 'plugin:', 'changed-tags:', 'async' ) );
     11$opts = getopt( '', array( 'url:', 'abspath:', 'plugin:', 'changed-tags:', 'async', 'create' ) );
    1212
    1313// Guess the default parameters:
    1414if ( empty( $opts ) && $argc == 2 ) {
    1515        $opts['plugin'] = $argv[1];
    1616        $argv[1]        = '--plugin ' . $argv[1];
    1717}
    1818if ( empty( $opts['url'] ) ) {
    1919        $opts['url'] = 'https://wordpress.org/plugins/';
    2020}
    2121if ( empty( $opts['abspath'] ) && false !== strpos( __DIR__, 'wp-content' ) ) {
    2222        $opts['abspath'] = substr( __DIR__, 0, strpos( __DIR__, 'wp-content' ) );
    2323}
    2424
    2525if ( empty( $opts['changed-tags'] ) ) {
    2626        $opts['changed-tags'] = array( 'trunk' );
    2727} else {
    2828        $opts['changed-tags'] = explode( ',', $opts['changed-tags'] );
    2929}
    3030
    31 $opts['async'] = isset( $opts['async'] );
     31$opts['async']  = isset( $opts['async'] );
     32$opts['create'] = isset( $opts['create'] );
    3233
    3334foreach ( array( 'url', 'abspath', 'plugin' ) as $opt ) {
    3435        if ( empty( $opts[ $opt ] ) ) {
    3536                fwrite( STDERR, "Missing Parameter: $opt\n" );
    3637                fwrite( STDERR, "Usage: php {$argv[0]} --plugin hello-dolly --abspath /home/example/public_html --url https://wordpress.org/plugins/\n" );
     38                fwrite( STDERR, "Optional: --async to queue a job to import, --create to create a Post if none exist.\n" );
    3739                fwrite( STDERR, "--url and --abspath will be guessed if possible.\n" );
    3840                die();
    3941        }
    4042}
    4143
    4244// Bootstrap WordPress
    4345$_SERVER['HTTP_HOST']   = parse_url( $opts['url'], PHP_URL_HOST );
    4446$_SERVER['REQUEST_URI'] = parse_url( $opts['url'], PHP_URL_PATH );
    4547
    4648require rtrim( $opts['abspath'], '/' ) . '/wp-load.php';
    4749
    4850if ( ! class_exists( '\WordPressdotorg\Plugin_Directory\Plugin_Directory' ) ) {
    4951        fwrite( STDERR, "Error! This site doesn't have the Plugin Directory plugin enabled.\n" );
    5052        if ( defined( 'WPORG_PLUGIN_DIRECTORY_BLOGID' ) ) {
    5153                fwrite( STDERR, "Run the following command instead:\n" );
    5254                fwrite( STDERR, "\tphp " . implode( ' ', $argv ) . ' --url ' . get_site_url( WPORG_PLUGIN_DIRECTORY_BLOGID, '/' ) . "\n" );
    5355        }
    5456        die();
    5557}
    5658
    5759$plugin_slug  = $opts['plugin'];
    5860$changed_tags = $opts['changed-tags'];
    5961$start_time   = microtime( 1 );
    6062
     63// If the create flag is set, check if the post exists first:
     64if ( $opts['create'] && ! Plugin_Directory::get_plugin_post( $plugin_slug ) ) {
     65
     66        $create_result = Plugin_Directory::create_plugin_post( array(
     67                'post_name'  => $plugin_slug,
     68        ) );
     69
     70        if ( ! $create_result || is_wp_error( $create_result ) ) {
     71                echo "Failed. $plugin post was not be found, and failed to be created.\n";
     72                fwrite( STDERR, "[{$plugin_slug}] Plugin Import Failed: " . $create_result->get_error_message() . "\n" );
     73                exit( 1 );
     74        }
     75}
     76
    6177// If async, queue it to be parsed instead.
    6278if ( $opts['async'] ) {
    6379        Jobs\Plugin_Import::queue( $plugin_slug, array( 'tags_touched' => $changed_tags ) );
    6480        echo "Queueing Import for $plugin_slug... OK\n";
    6581        die();
    6682}
    6783
    6884echo "Processing Import for $plugin_slug... ";
    6985try {
    7086        $importer = new CLI\Import();
    7187        $importer->import_from_svn( $plugin_slug, $changed_tags );
    7288        echo 'OK. Took ' . round( microtime( 1 ) - $start_time, 2 ) . "s\n";
    7389} catch ( \Exception $e ) {
    7490        echo 'Failed. Took ' . round( microtime( 1 ) - $start_time, 2 ) . "s\n";
    7591