-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathimport.php
More file actions
221 lines (184 loc) · 4.83 KB
/
import.php
File metadata and controls
221 lines (184 loc) · 4.83 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
<?php
/**
* GitHub Import Manager
*
* @package WordPress_GitHub_Sync
*/
/**
* Class WordPress_GitHub_Sync_Import
*/
class WordPress_GitHub_Sync_Import {
/**
* Application container.
*
* @var WordPress_GitHub_Sync
*/
protected $app;
/**
* Initializes a new import manager.
*
* @param WordPress_GitHub_Sync $app Application container.
*/
public function __construct( WordPress_GitHub_Sync $app ) {
$this->app = $app;
}
/**
* Imports a payload.
*
* @param WordPress_GitHub_Sync_Payload $payload GitHub payload object.
*
* @return string|WP_Error
*/
public function payload( WordPress_GitHub_Sync_Payload $payload ) {
/**
* Whether there's an error during import.
*
* @var false|WP_Error $error
*/
$error = false;
$commits = $payload->get_commits();
$result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ), $commits );
if ( is_wp_error( $result ) ) {
$error = $result;
}
$removed = array();
foreach ( $payload->get_commits() as $commit ) {
$removed = array_merge( $removed, $commit->removed );
}
foreach ( array_unique( $removed ) as $path ) {
$result = $this->app->database()->delete_post_by_path( $path );
if ( is_wp_error( $result ) ) {
if ( $error ) {
$error->add( $result->get_error_code(), $result->get_error_message() );
} else {
$error = $result;
}
}
}
if ( $error ) {
return $error;
}
return __( 'Payload processed', 'wp-github-sync' );
}
/**
* Imports the latest commit on the master branch.
*
* @return string|WP_Error
*/
public function master() {
return $this->commit( $this->app->api()->fetch()->master(), null );
}
/**
* Imports a provided commit into the database.
*
* @param WordPress_GitHub_Sync_Commit|WP_Error $commit Commit to import.
*
* @return string|WP_Error
*/
protected function commit( $commit , $commits_array) {
$head_commit = array();
$mod_files = array();
$update_all = true;
foreach ( $commits_array as $commit_obj ) {
if ( $commit_obj != null ) {
$next_commit = json_decode(json_encode($commit_obj), true);
if ( array_key_exists( 'modified', $next_commit ) ) {
$mod_files = array_merge($mod_files, $next_commit['modified']);
$update_all = false;
}
}
}
if ( is_wp_error( $commit ) ) {
return $commit;
}
if ( $commit->already_synced() ) {
return new WP_Error( 'commit_synced', __( 'Already synced this commit.', 'wp-github-sync' ) );
}
$posts = array();
$new = array();
foreach ( $commit->tree()->blobs() as $blob ) {
if ( ! $this->importable_blob( $blob ) ) {
continue;
}
$post = $this->blob_to_post( $blob );
$found = $update_all;
foreach ( $mod_files as $modified_file ) {
if ($blob->path() == $modified_file) {
$found = true;
}
}
if ( $post->is_new() ) {
$posts[] = $post;
$new[] = $post;
} elseif ( $found ) {
$posts[] = $post;
}
}
$result = $this->app->database()->save_posts( $posts, $commit->author_email() );
if ( is_wp_error( $result ) ) {
return $result;
}
if ( $new ) {
$result = $this->app->export()->new_posts( $new );
if ( is_wp_error( $result ) ) {
return $result;
}
}
return $posts;
}
/**
* Checks whether the provided blob should be imported.
*
* @param WordPress_GitHub_Sync_Blob $blob Blob to validate.
*
* @return bool
*/
protected function importable_blob( WordPress_GitHub_Sync_Blob $blob ) {
global $wpdb;
// Skip the repo's readme.
if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) {
return false;
}
// If the blob sha already matches a post, then move on.
if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) {
return false;
}
if ( ! $blob->has_frontmatter() ) {
return false;
}
return true;
}
/**
* Imports a single blob content into matching post.
*
* @param WordPress_GitHub_Sync_Blob $blob Blob to transform into a Post.
*
* @return WordPress_GitHub_Sync_Post
*/
protected function blob_to_post( WordPress_GitHub_Sync_Blob $blob ) {
$args = array( 'post_content' => $blob->content_import() );
$meta = $blob->meta();
if ( $meta ) {
if ( array_key_exists( 'layout', $meta ) ) {
$args['post_type'] = $meta['layout'];
unset( $meta['layout'] );
}
if ( array_key_exists( 'published', $meta ) ) {
$args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
unset( $meta['published'] );
}
if ( array_key_exists( 'post_title', $meta ) ) {
$args['post_title'] = $meta['post_title'];
unset( $meta['post_title'] );
}
if ( array_key_exists( 'ID', $meta ) ) {
$args['ID'] = $meta['ID'];
unset( $meta['ID'] );
}
}
$meta['_sha'] = $blob->sha();
$post = new WordPress_GitHub_Sync_Post( $args, $this->app->api() );
$post->set_meta( $meta );
return $post;
}
}