12 | 12 | |
13 | 13 | // Guess the default parameters: |
14 | 14 | if ( empty( $opts ) && $argc == 2 ) { |
15 | 15 | $opts['plugin'] = $argv[1]; |
16 | 16 | $argv[1] = '--plugin ' . $argv[1]; |
17 | 17 | } |
18 | 18 | if ( empty( $opts['url'] ) ) { |
19 | 19 | $opts['url'] = 'https://wordpress.org/plugins/'; |
20 | 20 | } |
21 | 21 | if ( empty( $opts['abspath'] ) && false !== strpos( __DIR__, 'wp-content' ) ) { |
22 | 22 | $opts['abspath'] = substr( __DIR__, 0, strpos( __DIR__, 'wp-content' ) ); |
23 | 23 | } |
24 | 24 | |
25 | 25 | if ( empty( $opts['changed-tags'] ) ) { |
26 | 26 | $opts['changed-tags'] = array( 'trunk' ); |
27 | 27 | } else { |
28 | 28 | $opts['changed-tags'] = explode( ',', $opts['changed-tags'] ); |
29 | 29 | } |
30 | 30 | |
37 | 39 | fwrite( STDERR, "--url and --abspath will be guessed if possible.\n" ); |
38 | 40 | die(); |
39 | 41 | } |
40 | 42 | } |
41 | 43 | |
42 | 44 | // Bootstrap WordPress |
43 | 45 | $_SERVER['HTTP_HOST'] = parse_url( $opts['url'], PHP_URL_HOST ); |
44 | 46 | $_SERVER['REQUEST_URI'] = parse_url( $opts['url'], PHP_URL_PATH ); |
45 | 47 | |
46 | 48 | require rtrim( $opts['abspath'], '/' ) . '/wp-load.php'; |
47 | 49 | |
48 | 50 | if ( ! class_exists( '\WordPressdotorg\Plugin_Directory\Plugin_Directory' ) ) { |
49 | 51 | fwrite( STDERR, "Error! This site doesn't have the Plugin Directory plugin enabled.\n" ); |
50 | 52 | if ( defined( 'WPORG_PLUGIN_DIRECTORY_BLOGID' ) ) { |
51 | 53 | fwrite( STDERR, "Run the following command instead:\n" ); |
52 | 54 | fwrite( STDERR, "\tphp " . implode( ' ', $argv ) . ' --url ' . get_site_url( WPORG_PLUGIN_DIRECTORY_BLOGID, '/' ) . "\n" ); |
53 | 55 | } |
54 | 56 | die(); |
55 | 57 | } |
56 | 58 | |
57 | 59 | $plugin_slug = $opts['plugin']; |
58 | 60 | $changed_tags = $opts['changed-tags']; |
59 | 61 | $start_time = microtime( 1 ); |
60 | 62 | |
| 63 | // If the create flag is set, check if the post exists first: |
| 64 | if ( $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 | |
61 | 77 | // If async, queue it to be parsed instead. |
62 | 78 | if ( $opts['async'] ) { |
63 | 79 | Jobs\Plugin_Import::queue( $plugin_slug, array( 'tags_touched' => $changed_tags ) ); |
64 | 80 | echo "Queueing Import for $plugin_slug... OK\n"; |
65 | 81 | die(); |
66 | 82 | } |
67 | 83 | |
68 | 84 | echo "Processing Import for $plugin_slug... "; |
69 | 85 | try { |
70 | 86 | $importer = new CLI\Import(); |
71 | 87 | $importer->import_from_svn( $plugin_slug, $changed_tags ); |
72 | 88 | echo 'OK. Took ' . round( microtime( 1 ) - $start_time, 2 ) . "s\n"; |
73 | 89 | } catch ( \Exception $e ) { |
74 | 90 | echo 'Failed. Took ' . round( microtime( 1 ) - $start_time, 2 ) . "s\n"; |
75 | 91 | |