1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WP_CLI Commands |
4
|
|
|
* @package WordPress_GitHub_Sync |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class WordPress_GitHub_Sync_CLI |
9
|
|
|
*/ |
10
|
|
|
class WordPress_GitHub_Sync_CLI extends WP_CLI_Command { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Application container. |
14
|
|
|
* |
15
|
|
|
* @var WordPress_GitHub_Sync |
16
|
|
|
*/ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Grab the Application container on instantiation. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() { |
23
|
|
|
$this->app = WordPress_GitHub_Sync::$instance; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Exports an individual post |
28
|
|
|
* all your posts to GitHub |
29
|
|
|
* |
30
|
|
|
* ## OPTIONS |
31
|
|
|
* |
32
|
|
|
* <post_id|all> |
33
|
|
|
* : The post ID to export or 'all' for full site |
34
|
|
|
* |
35
|
|
|
* <user_id> |
36
|
|
|
* : The user ID you'd like to save the commit as |
37
|
|
|
* |
38
|
|
|
* ## EXAMPLES |
39
|
|
|
* |
40
|
|
|
* wp wpghs export all 1 |
41
|
|
|
* wp wpghs export 1 1 |
42
|
|
|
* |
43
|
|
|
* @synopsis <post_id|all> <user_id> |
44
|
|
|
* |
45
|
|
|
* @param array $args Command arguments. |
46
|
|
|
*/ |
47
|
|
|
public function export( $args ) { |
48
|
|
|
list( $post_id, $user_id ) = $args; |
49
|
|
|
|
50
|
|
|
if ( ! is_numeric( $user_id ) ) { |
51
|
|
|
WP_CLI::error( __( 'Invalid user ID', 'wp-github-sync' ) ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->app->export()->set_user( $user_id ); |
55
|
|
|
|
56
|
|
|
if ( 'all' === $post_id ) { |
57
|
|
|
WP_CLI::line( __( 'Starting full export to GitHub.', 'wp-github-sync' ) ); |
58
|
|
|
$this->app->controller()->export_all(); |
59
|
|
|
} elseif ( is_numeric( $post_id ) ) { |
60
|
|
|
WP_CLI::line( |
61
|
|
|
sprintf( |
62
|
|
|
__( 'Exporting post ID to GitHub: %d', 'wp-github-sync' ), |
63
|
|
|
$post_id |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
$this->app->controller()->export_post( (int) $post_id ); |
67
|
|
|
} else { |
68
|
|
|
WP_CLI::error( __( 'Invalid post ID', 'wp-github-sync' ) ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Imports the post in your GitHub repo |
74
|
|
|
* into your WordPress blog |
75
|
|
|
* |
76
|
|
|
* ## OPTIONS |
77
|
|
|
* |
78
|
|
|
* <user_id> |
79
|
|
|
* : The user ID you'd like to save the commit as |
80
|
|
|
* |
81
|
|
|
* ## EXAMPLES |
82
|
|
|
* |
83
|
|
|
* wp wpghs import 1 |
84
|
|
|
* |
85
|
|
|
* @synopsis <user_id> |
86
|
|
|
* |
87
|
|
|
* @param array $args Command arguments. |
88
|
|
|
*/ |
89
|
|
|
public function import( $args ) { |
90
|
|
|
list( $user_id ) = $args; |
91
|
|
|
|
92
|
|
|
if ( ! is_numeric( $user_id ) ) { |
93
|
|
|
WP_CLI::error( __( 'Invalid user ID', 'wp-github-sync' ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
update_option( '_wpghs_export_user_id', (int) $user_id ); |
97
|
|
|
|
98
|
|
|
WP_CLI::line( __( 'Starting import from GitHub.', 'wp-github-sync' ) ); |
99
|
|
|
|
100
|
|
|
$this->app->controller()->import_master(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Fetches the provided sha or the repository's |
105
|
|
|
* master branch and caches it. |
106
|
|
|
* |
107
|
|
|
* ## OPTIONS |
108
|
|
|
* |
109
|
|
|
* <user_id> |
110
|
|
|
* : The user ID you'd like to save the commit as |
111
|
|
|
* |
112
|
|
|
* ## EXAMPLES |
113
|
|
|
* |
114
|
|
|
* wp wpghs prime --branch=master |
115
|
|
|
* wp wpghs prime --sha=<commit_sha> |
116
|
|
|
* |
117
|
|
|
* @synopsis [--sha=<commit_sha>] [--branch] |
118
|
|
|
* |
119
|
|
|
* @param array $args Command arguments. |
120
|
|
|
* @param array $assoc_args Command associated arguments. |
121
|
|
|
*/ |
122
|
|
|
public function prime( $args, $assoc_args ) { |
123
|
|
|
if ( isset( $assoc_args['branch'] ) ) { |
124
|
|
|
WP_CLI::line( __( 'Starting branch import.', 'wp-github-sync' ) ); |
125
|
|
|
|
126
|
|
|
$commit = $this->app->api()->fetch()->master(); |
127
|
|
|
|
128
|
|
|
if ( is_wp_error( $commit ) ) { |
129
|
|
|
WP_CLI::error( |
130
|
|
|
sprintf( |
131
|
|
|
__( 'Failed to import and cache branch with error: %s', 'wp-github-sync' ), |
132
|
|
|
$commit->get_error_message() |
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
} else { |
136
|
|
|
WP_CLI::success( |
137
|
|
|
sprintf( |
138
|
|
|
__( 'Successfully imported and cached commit %s from branch.', 'wp-github-sync' ), |
139
|
|
|
$commit->sha() |
|
|
|
|
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} else if ( isset( $assoc_args['sha'] ) ) { |
144
|
|
|
WP_CLI::line( 'Starting sha import.' ); |
145
|
|
|
|
146
|
|
|
$commit = $this->app->api()->fetch()->commit( $assoc_args['sha'] ); |
147
|
|
|
|
148
|
|
|
WP_CLI::success( |
149
|
|
|
sprintf( |
150
|
|
|
__( 'Successfully imported and cached commit %s.', 'wp-github-sync' ), |
151
|
|
|
$commit->sha() |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} else { |
155
|
|
|
WP_CLI::error( 'Invalid fetch.' ); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: