Repository navigation
Expand file tree
/
Copy pathexport.php
More file actions
263 lines (216 loc) · 5.42 KB
/
Copy pathexport.php
File metadata and controls
263 lines (216 loc) · 5.42 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* GitHub Export Manager.
*
* @package WordPress_GitHub_Sync
*/
/**
* Class WordPress_GitHub_Sync_Export
*/
class WordPress_GitHub_Sync_Export {
/**
* Option key for export user.
*/
const EXPORT_USER_OPTION = '_wpghs_export_user_id';
/**
* Application container.
*
* @var WordPress_GitHub_Sync
*/
protected $app;
/**
* Initializes a new export manager.
*
* @param WordPress_GitHub_Sync $app Application container.
*/
public function __construct( WordPress_GitHub_Sync $app ) {
$this->app = $app;
}
/**
* Updates all of the current posts in the database on master.
*
* @return string|WP_Error
*/
public function full() {
$posts = $this->app->database()->fetch_all_supported();
if ( is_wp_error( $posts ) ) {
return $posts;
}
$master = $this->app->api()->fetch()->master();
if ( is_wp_error( $master ) ) {
return $master;
}
foreach ( $posts as $post ) {
$master->tree()->add_post_to_tree( $post );
}
$master->set_message(
apply_filters(
'wpghs_commit_msg_full',
sprintf(
'Full export from WordPress at %s (%s)',
site_url(),
get_bloginfo( 'name' )
)
) . $this->get_commit_msg_tag()
);
$result = $this->app->api()->persist()->commit( $master );
if ( is_wp_error( $result ) ) {
return $result;
}
return $this->update_shas( $posts );
}
/**
* Updates the provided post ID in master.
*
* @param int $post_id Post ID to update.
*
* @return string|WP_Error
*/
public function update( $post_id ) {
$post = $this->app->database()->fetch_by_id( $post_id );
if ( is_wp_error( $post ) ) {
return $post;
}
if ( 'trash' === $post->status() ) {
return $this->delete( $post_id );
}
$master = $this->app->api()->fetch()->master();
if ( is_wp_error( $master ) ) {
return $master;
}
$master->tree()->add_post_to_tree( $post );
$master->set_message(
apply_filters(
'wpghs_commit_msg_single',
sprintf(
'Syncing %s from WordPress at %s (%s)',
$post->github_path(),
site_url(),
get_bloginfo( 'name' )
),
$post
) . $this->get_commit_msg_tag()
);
$result = $this->app->api()->persist()->commit( $master );
if ( is_wp_error( $result ) ) {
return $result;
}
return $this->update_shas( array( $post ) );
}
/**
* Updates GitHub-created posts with latest WordPress data.
*
* @param array<WordPress_GitHub_Sync_Post> $posts Array of Posts to create.
*
* @return string|WP_Error
*/
public function new_posts( array $posts ) {
$master = $this->app->api()->fetch()->master();
if ( is_wp_error( $master ) ) {
return $master;
}
foreach ( $posts as $post ) {
$master->tree()->add_post_to_tree( $post );
}
$master->set_message(
apply_filters(
'wpghs_commit_msg_new_posts',
sprintf(
'Updating new posts from WordPress at %s (%s)',
site_url(),
get_bloginfo( 'name' )
)
) . $this->get_commit_msg_tag()
);
$result = $this->app->api()->persist()->commit( $master );
if ( is_wp_error( $result ) ) {
return $result;
}
return $this->update_shas( $posts );
}
/**
* Deletes a provided post ID from master.
*
* @param int $post_id Post ID to delete.
*
* @return string|WP_Error
*/
public function delete( $post_id ) {
$post = $this->app->database()->fetch_by_id( $post_id );
if ( is_wp_error( $post ) ) {
return $post;
}
$master = $this->app->api()->fetch()->master();
if ( is_wp_error( $master ) ) {
return $master;
}
$master->tree()->remove_post_from_tree( $post );
$master->set_message(
apply_filters(
'wpghs_commit_msg_delete',
sprintf(
'Deleting %s via WordPress at %s (%s)',
$post->github_path(),
site_url(),
get_bloginfo( 'name' )
),
$post
) . $this->get_commit_msg_tag()
);
$result = $this->app->api()->persist()->commit( $master );
if ( is_wp_error( $result ) ) {
return $result;
}
return __( 'Export to GitHub completed successfully.', 'wp-github-sync' );
}
/**
* Use the new tree to save sha data
* for all the updated posts.
*
* @param WordPress_GitHub_Sync_Post[] $posts Posts to fetch updated shas for.
*
* @return string|WP_Error
*/
protected function update_shas( array $posts ) {
$master = $this->app->api()->fetch()->master();
$attempts = 1;
while ( is_wp_error( $master ) && $attempts < 5 ) {
$master = $this->app->api()->fetch()->master();
$attempts ++;
}
if ( is_wp_error( $master ) ) {
// @todo throw a big warning! not having the latest shas is BAD
// Solution: Show error message and link to kick off sha importing.
return $master;
}
foreach ( $posts as $post ) {
$blob = $master->tree()->get_blob_by_path( $post->github_path() );
if ( $blob ) {
$this->app->database()->set_post_sha( $post, $blob->sha() );
}
}
return __( 'Export to GitHub completed successfully.', 'wp-github-sync' );
}
/**
* Saves the export user to the database.
*
* @param int $user_id User ID to export with.
*
* @return bool
*/
public function set_user( $user_id ) {
return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
}
/**
* Gets the commit message tag.
*
* @return string
*/
protected function get_commit_msg_tag() {
$tag = apply_filters( 'wpghs_commit_msg_tag', 'wpghs' );
if ( ! $tag ) {
throw new Exception( __( 'Commit message tag not set. Filter `wpghs_commit_msg_tag` misconfigured.', 'wp-github-sync' ) );
}
return ' - ' . $tag;
}
}