forked from mAAdhaTTah/wordpress-github-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
158 lines (141 loc) · 3.44 KB
/
Copy pathcli.php
File metadata and controls
158 lines (141 loc) · 3.44 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* WP_CLI Commands
* @package WordPress_GitHub_Sync
*/
/**
* Class WordPress_GitHub_Sync_CLI
*/
class WordPress_GitHub_Sync_CLI extends WP_CLI_Command {
/**
* Application container.
*
* @var WordPress_GitHub_Sync
*/
protected $app;
/**
* Grab the Application container on instantiation.
*/
public function __construct() {
$this->app = WordPress_GitHub_Sync::$instance;
}
/**
* Exports an individual post
* all your posts to GitHub
*
* ## OPTIONS
*
* <post_id|all>
* : The post ID to export or 'all' for full site
*
* <user_id>
* : The user ID you'd like to save the commit as
*
* ## EXAMPLES
*
* wp wpghs export all 1
* wp wpghs export 1 1
*
* @synopsis <post_id|all> <user_id>
*
* @param array $args Command arguments.
*/
public function export( $args ) {
list( $post_id, $user_id ) = $args;
if ( ! is_numeric( $user_id ) ) {
WP_CLI::error( __( 'Invalid user ID', 'wp-github-sync' ) );
}
$this->app->export()->set_user( $user_id );
if ( 'all' === $post_id ) {
WP_CLI::line( __( 'Starting full export to GitHub.', 'wp-github-sync' ) );
$this->app->controller()->export_all();
} elseif ( is_numeric( $post_id ) ) {
WP_CLI::line(
sprintf(
__( 'Exporting post ID to GitHub: %d', 'wp-github-sync' ),
$post_id
)
);
$this->app->controller()->export_post( (int) $post_id );
} else {
WP_CLI::error( __( 'Invalid post ID', 'wp-github-sync' ) );
}
}
/**
* Imports the post in your GitHub repo
* into your WordPress blog
*
* ## OPTIONS
*
* <user_id>
* : The user ID you'd like to save the commit as
*
* ## EXAMPLES
*
* wp wpghs import 1
*
* @synopsis <user_id>
*
* @param array $args Command arguments.
*/
public function import( $args ) {
list( $user_id ) = $args;
if ( ! is_numeric( $user_id ) ) {
WP_CLI::error( __( 'Invalid user ID', 'wp-github-sync' ) );
}
update_option( '_wpghs_export_user_id', (int) $user_id );
WP_CLI::line( __( 'Starting import from GitHub.', 'wp-github-sync' ) );
$this->app->controller()->import_master();
}
/**
* Fetches the provided sha or the repository's
* master branch and caches it.
*
* ## OPTIONS
*
* <user_id>
* : The user ID you'd like to save the commit as
*
* ## EXAMPLES
*
* wp wpghs prime --branch=master
* wp wpghs prime --sha=<commit_sha>
*
* @synopsis [--sha=<commit_sha>] [--branch]
*
* @param array $args Command arguments.
* @param array $assoc_args Command associated arguments.
*/
public function prime( $args, $assoc_args ) {
if ( isset( $assoc_args['branch'] ) ) {
WP_CLI::line( __( 'Starting branch import.', 'wp-github-sync' ) );
$commit = $this->app->api()->fetch()->master();
if ( is_wp_error( $commit ) ) {
WP_CLI::error(
sprintf(
__( 'Failed to import and cache branch with error: %s', 'wp-github-sync' ),
$commit->get_error_message()
)
);
} else {
WP_CLI::success(
sprintf(
__( 'Successfully imported and cached commit %s from branch.', 'wp-github-sync' ),
$commit->sha()
)
);
}
} else if ( isset( $assoc_args['sha'] ) ) {
WP_CLI::line( 'Starting sha import.' );
$commit = $this->app->api()->fetch()->commit( $assoc_args['sha'] );
WP_CLI::success(
sprintf(
__( 'Successfully imported and cached commit %s.', 'wp-github-sync' ),
$commit->sha()
)
);
} else {
WP_CLI::error( 'Invalid fetch.' );
}
}
}