-
Notifications
You must be signed in to change notification settings - Fork 79
/
command.php
69 lines (59 loc) · 1.54 KB
/
command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace WP_CLI_Org;
use WP_CLI;
use Mustache_Engine;
/**
* WP-CLI commands for generating the docs
*/
/**
* Run all generation commands to generate full website.
*
* @when before_wp_load
*/
function generate() {
generate_homepage();
}
WP_CLI::add_command( 'website generate', 'WP_CLI_Org\generate' );
/**
* Generate the homepage from WP-CLI's README.md
*
* @when before_wp_load
*/
function generate_homepage() {
$ret = trim( shell_exec( 'which wp' ) );
if ( empty( $ret ) ) {
WP_CLI::error( 'Could not find path to wp executable.' );
}
if ( 'link' === filetype( $ret ) ) {
$ret = readlink( $ret );
}
$readme_path = dirname( dirname( $ret ) ) . '/README.md';
if ( ! is_file( $readme_path ) ) {
WP_CLI::error( 'Could not find README.md in wp executable PATH. Please make sure wp executable points to git clone.' );
}
$contents = file_get_contents( $readme_path );
$search = <<<EOT
WP-CLI
======
EOT;
$replace = <<<EOT
---
layout: default
title: Command line interface for WordPress
---
EOT;
$contents = str_replace( $search, $replace, $contents );
file_put_contents( dirname( __FILE__ ) . '/index.md', $contents );
WP_CLI::success( 'Updated index.md from project README.md.' );
}
WP_CLI::add_command( 'website generate-homepage', '\WP_CLI_Org\generate_homepage' );
function invoke_wp_cli( $cmd ) {
ob_start();
system( "WP_CLI_CONFIG_PATH=/dev/null $cmd", $return_code );
$json = ob_get_clean();
if ( $return_code ) {
echo "WP-CLI returned error code: $return_code\n";
exit(1);
}
return json_decode( $json, true );
}