forked from mAAdhaTTah/wordpress-github-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
462 lines (372 loc) · 12 KB
/
Copy pathcontroller.php
File metadata and controls
462 lines (372 loc) · 12 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
* Controller object manages tree retrieval, manipulation and publishing
*/
class WordPress_GitHub_Sync_Controller {
/**
* Api object
* @var WordPress_GitHub_Sync_Api
*/
public $api;
/**
* Whether any posts have changed
* @var boolean
*/
public $changed = false;
/**
* Array of posts to export
* @var array
*/
public $posts = array();
/**
* Array representing new tre
* @var array
*/
public $tree = array();
/**
* Commit message
* @var string
*/
public $msg = '';
/**
* Instantiates a new Controller object
*
* $posts - array of post IDs to export
*/
public function __construct() {
$this->api = new WordPress_GitHub_Sync_Api;
}
/**
* Reads the Webhook payload and syncs posts as necessary
*/
public function pull($payload) {
if ( strtolower( $payload->repository->full_name ) !== strtolower( $this->api->repository() ) ) {
WordPress_GitHub_Sync::write_log( strtolower( $payload->repository->full_name ) . __( ' is an invalid repository.', WordPress_GitHub_Sync::$text_domain ) );
return;
}
// the last term in the ref is the branch name
$refs = explode( '/', $payload->ref );
$branch = array_pop( $refs );
if ( 'master' !== $branch ) {
WordPress_GitHub_Sync::write_log( __( 'Not on the master branch.', WordPress_GitHub_Sync::$text_domain ) );
return;
}
// We add wpghs to commits we push out, so we shouldn't pull them in again
if ( 'wpghs' === substr( $payload->head_commit->message, -5 ) ) {
WordPress_GitHub_Sync::write_log( __( 'Already synced this commit.', WordPress_GitHub_Sync::$text_domain ) );
return;
}
$commit = $this->api->get_commit( $payload->head_commit->id );
if ( is_wp_error( $commit ) ) {
WordPress_GitHub_Sync::write_log( __( 'Failed getting commit with error: ', WordPress_GitHub_Sync::$text_domain ) . $commit->get_error_message() );
return;
}
$this->import_tree( $commit->tree->sha );
// Deleting posts from a payload is the only place
// we need to search posts by path; another way?
$removed = array();
foreach ( $payload->commits as $commit ) {
$removed = array_merge( $removed, $commit->removed );
}
foreach ( array_unique( $removed ) as $path ) {
$post = new WordPress_GitHub_Sync_Post( $path );
wp_delete_post( $post->id );
}
WordPress_GitHub_Sync::write_log( __( 'Payload processed', WordPress_GitHub_Sync::$text_domain ) );
}
/**
* Imports posts from the current master branch
*/
public function import_master() {
$commit = $this->api->last_commit();
if ( is_wp_error( $commit ) ) {
WordPress_GitHub_Sync::write_log( __( 'Failed getting last commit with error: ', WordPress_GitHub_Sync::$text_domain ) . $commit->get_error_message() );
return;
}
if ( 'wpghs' === substr( $commit->message, -5 ) ) {
WordPress_GitHub_Sync::write_log( __( 'Already synced this commit.', WordPress_GitHub_Sync::$text_domain ) );
return;
}
$this->import_tree( $commit->tree->sha );
}
/**
* Imports posts from a given tree sha
*/
public function import_tree($sha) {
$tree = $this->api->get_tree_recursive( $sha );
if ( is_wp_error( $tree ) ) {
WordPress_GitHub_Sync::write_log( __( 'Failed getting recursive tree with error: ', WordPress_GitHub_Sync::$text_domain ) . $tree->get_error_message() );
return;
}
foreach ( $tree as $blob ) {
$this->import_blob( $blob );
}
WordPress_GitHub_Sync::write_log( __( 'Imported tree ', WordPress_GitHub_Sync::$text_domain ) . $sha );
}
/**
* Imports a single blob content into matching post
*/
public function import_blob($blob) {
global $wpdb;
// Skip the repo's readme
if ( 'readme' === strtolower( substr( $blob->path, 0, 6 ) ) ) {
WordPress_GitHub_Sync::write_log( __( 'Skipping README', WordPress_GitHub_Sync::$text_domain ) );
return;
}
// If the blob sha already matches a post, then move on
$id = $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sha' AND meta_value = '$blob->sha'" );
if ( $id ) {
WordPress_GitHub_Sync::write_log( __( 'Already synced blob ', WordPress_GitHub_Sync::$text_domain ) . $blob->path );
return;
}
$blob = $this->api->get_blob( $blob->sha );
if ( is_wp_error( $blob ) ) {
WordPress_GitHub_Sync::write_log( __( 'Failed getting blob with error: ', WordPress_GitHub_Sync::$text_domain ) . $blob->get_error_message() );
return;
}
$content = base64_decode( $blob->content );
// If it doesn't have YAML frontmatter, then move on
if ( '---' !== substr( $content, 0, 3 ) ) {
WordPress_GitHub_Sync::write_log( __( 'No front matter on blob ', WordPress_GitHub_Sync::$text_domain ) . $blob->sha );
return;
}
// Break out meta, if present
preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches );
$body = array_pop( $matches );
if ( 3 === count( $matches ) ) {
$meta = spyc_load( $matches[2] );
if ( isset( $meta['permalink'] ) ) {
$meta['permalink'] = str_replace( home_url(), '', get_permalink( $meta['permalink'] ) );
}
} else {
$meta = array();
}
if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) {
$body = wpmarkdown_markdown_to_html( $body );
}
// Can we really just mash everything together here?
$args = array_merge( $meta, array(
'post_content' => $body,
'_sha' => $blob->sha,
) );
if ( ! isset($args['ID']) ) {
wp_insert_post( $args );
} else {
wp_update_post( $args );
}
}
/**
* Export all the posts in the database to GitHub
*/
public function export_all() {
global $wpdb;
if ( $this->locked() ) {
return;
}
$posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ('post', 'page' )" );
$this->msg = 'Full export from WordPress at ' . site_url() . ' (' . get_bloginfo( 'name' ) . ') - wpghs';
$this->get_tree();
WordPress_GitHub_Sync::write_log( __( 'Building the tree.', WordPress_GitHub_Sync::$text_domain ) );
foreach ( $posts as $post_id ) {
$post = new WordPress_GitHub_Sync_Post( $post_id );
$this->post_to_tree( $post );
}
$this->finalize();
}
/**
* Exports a single post to GitHub by ID
*/
public function export_post($post_id) {
if ( $this->locked() ) {
return;
}
$this->posts[] = $post_id;
$post = new WordPress_GitHub_Sync_Post( $post_id );
$this->msg = 'Syncing ' . $post->github_path() . ' from WordPress at ' . site_url() . ' (' . get_bloginfo( 'name' ) . ') - wpghs';
$this->get_tree();
WordPress_GitHub_Sync::write_log( __( 'Building the tree.', WordPress_GitHub_Sync::$text_domain ) );
$this->post_to_tree( $post );
$this->finalize();
}
/**
* Removes the post from the tree
*/
public function delete_post($post_id) {
if ( $this->locked() ) {
return;
}
$this->posts[] = $post_id;
$post = new WordPress_GitHub_Sync_Post( $post_id );
$this->msg = 'Deleting ' . $post->github_path() . ' via WordPress at ' . site_url() . ' (' . get_bloginfo( 'name' ) . ') - wpghs';
$this->get_tree();
WordPress_GitHub_Sync::write_log( __( 'Building the tree.', WordPress_GitHub_Sync::$text_domain ) );
$this->post_to_tree( $post, true );
$this->finalize();
}
/**
* Takes the next post off the top of the list
* and exports it to the new GitHub tree
*/
public function post_to_tree($post, $remove = false) {
$match = false;
foreach ( $this->tree as $index => $blob ) {
if ( ! isset( $blob->sha ) ) {
continue;
}
if ( $blob->sha === $post->sha() ) {
unset($this->tree[ $index ]);
$match = true;
if ( ! $remove ) {
$this->tree[] = $this->new_blob( $post, $blob );
} else {
$this->changed = true;
}
break;
}
}
if ( ! $match ) {
$this->tree[] = $this->new_blob( $post );
$this->changed = true;
}
}
/**
* After all the blobs are saved,
* create the tree, commit, and adjust master ref
*/
public function finalize() {
if ( ! $this->changed ) {
$this->no_change();
return;
}
WordPress_GitHub_Sync::write_log( __( 'Creating the tree.', WordPress_GitHub_Sync::$text_domain ) );
$tree = $this->api->create_tree( array_values( $this->tree ) );
if ( is_wp_error( $tree ) ) {
$this->error( $tree );
return;
}
WordPress_GitHub_Sync::write_log( __( 'Creating the commit.', WordPress_GitHub_Sync::$text_domain ) );
$commit = $this->api->create_commit( $tree->sha, $this->msg );
if ( is_wp_error( $commit ) ) {
$this->error( $commit );
return;
}
WordPress_GitHub_Sync::write_log( __( 'Setting the master branch to our new commit.', WordPress_GitHub_Sync::$text_domain ) );
$ref = $this->api->set_ref( $commit->sha );
if ( is_wp_error( $ref ) ) {
$this->error( $ref );
return;
}
$rtree = $this->api->last_tree_recursive();
WordPress_GitHub_Sync::write_log( __( 'Saving the shas.', WordPress_GitHub_Sync::$text_domain ) );
$this->save_post_shas( $rtree );
$this->success();
}
/**
* Combines a post and (potentially) a blob
*
* If no blob is provided, turns post into blob
*
* If blob is provided, compares blob to post
* and updates blob data based on differences
*/
public function new_blob($post, $blob = array()) {
if ( empty($blob) ) {
$blob = $this->blob_from_post( $post );
} else {
unset($blob->url);
unset($blob->size);
if ( $blob->path !== $post->github_path() ) {
$blob->path = $post->github_path();
$this->changed = true;
}
$blob_data = $this->api->get_blob( $blob->sha );
if ( base64_decode( $blob_data->content ) !== $post->github_content() ) {
unset($blob->sha);
$blob->content = $post->github_content();
$this->changed = true;
}
}
return $blob;
}
/**
* Creates a blob with the data required for the tree
*/
public function blob_from_post($post) {
$blob = new stdClass;
$blob->path = $post->github_path();
$blob->mode = '100644';
$blob->type = 'blob';
$blob->content = $post->github_content();
return $blob;
}
/**
* Use the new tree to save sha data
* for all the updated posts
*/
public function save_post_shas($tree) {
foreach ( $this->posts as $post_id ) {
$post = new WordPress_GitHub_Sync_Post( $post_id );
$match = false;
foreach ( $tree as $blob ) {
// this might be a problem if the filename changed since it was set
// (i.e. post updated in middle mass export)
// solution?
if ( $post->github_path() === $blob->path ) {
$post->set_sha( $blob->sha );
$match = true;
break;
}
}
if ( ! $match ) {
WordPress_GitHub_Sync::write_log( __( 'No sha matched for post ID ', WordPress_GitHub_Sync::$text_domain ) . $post_id );
}
}
}
/**
* Check if we're clear to call the api
*/
public function locked() {
global $wpghs;
if ( ! $this->api->oauth_token() || ! $this->api->repository() || $wpghs->push_lock ) {
return true;
}
return false;
}
/**
* Writes out the results of an unchanged export
*/
public function no_change() {
update_option( '_wpghs_export_complete', 'yes' );
WordPress_GitHub_Sync::write_log( __( 'There were no changes, so no additional commit was added.', WordPress_GitHub_Sync::$text_domain ), 'warning' );
}
/**
* Writes out the results of a successful export
*/
public function success() {
update_option( '_wpghs_export_complete', 'yes' );
update_option( '_wpghs_fully_exported', 'yes' );
WordPress_GitHub_Sync::write_log( __( 'Export to GitHub completed successfully.', WordPress_GitHub_Sync::$text_domain ), 'success' );
}
/**
* Writes out the results of an error and saves the data
*/
public function error($result) {
update_option( '_wpghs_export_error', $result->get_error_message() );
WordPress_GitHub_Sync::write_log( __( 'Error exporting to GitHub. Error: ', WordPress_GitHub_Sync::$text_domain ) . $result->get_error_message(), 'error' );
}
/**
* Retrieve the saved tree we're building
* or get the latest tree from the repo
*/
public function get_tree() {
if ( ! empty($this->tree) ) {
return;
}
$tree = $this->api->last_tree_recursive();
if ( is_wp_error( $tree ) ) {
WordPress_GitHub_Sync::write_log( __( 'Failed getting tree with error: ', WordPress_GitHub_Sync::$text_domain ) . $tree->get_error_message() );
return;
}
$this->tree = $tree;
}
}