1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GitHub Import Manager |
4
|
|
|
* |
5
|
|
|
* @package WordPress_GitHub_Sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class WordPress_GitHub_Sync_Import |
10
|
|
|
*/ |
11
|
|
|
class WordPress_GitHub_Sync_Import { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Application container. |
15
|
|
|
* |
16
|
|
|
* @var WordPress_GitHub_Sync |
17
|
|
|
*/ |
18
|
|
|
protected $app; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Initializes a new import manager. |
22
|
|
|
* |
23
|
|
|
* @param WordPress_GitHub_Sync $app Application container. |
24
|
|
|
*/ |
25
|
10 |
|
public function __construct( WordPress_GitHub_Sync $app ) { |
26
|
10 |
|
$this->app = $app; |
27
|
10 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Imports a payload. |
31
|
|
|
* |
32
|
|
|
* @param WordPress_GitHub_Sync_Payload $payload GitHub payload object. |
33
|
|
|
* |
34
|
|
|
* @return string|WP_Error |
35
|
|
|
*/ |
36
|
2 |
|
public function payload( WordPress_GitHub_Sync_Payload $payload ) { |
37
|
|
|
/** |
38
|
|
|
* Whether there's an error during import. |
39
|
|
|
* |
40
|
|
|
* @var false|WP_Error $error |
41
|
|
|
*/ |
42
|
2 |
|
$error = false; |
43
|
|
|
|
44
|
2 |
|
$result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ) ); |
|
|
|
|
45
|
|
|
|
46
|
2 |
|
if ( is_wp_error( $result ) ) { |
47
|
1 |
|
$error = $result; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
2 |
|
$removed = array(); |
51
|
2 |
|
foreach ( $payload->get_commits() as $commit ) { |
52
|
2 |
|
$removed = array_merge( $removed, $commit->removed ); |
53
|
2 |
|
} |
54
|
2 |
|
foreach ( array_unique( $removed ) as $path ) { |
55
|
2 |
|
$result = $this->app->database()->delete_post_by_path( $path ); |
56
|
|
|
|
57
|
2 |
|
if ( is_wp_error( $result ) ) { |
58
|
|
|
if ( $error ) { |
59
|
|
|
$error->add( $result->get_error_code(), $result->get_error_message() ); |
60
|
|
|
} else { |
61
|
|
|
$error = $result; |
62
|
|
|
} |
63
|
|
|
} |
64
|
2 |
|
} |
65
|
|
|
|
66
|
2 |
|
if ( $error ) { |
67
|
1 |
|
return $error; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return __( 'Payload processed', 'wp-github-sync' ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Imports the latest commit on the master branch. |
75
|
|
|
* |
76
|
|
|
* @return string|WP_Error |
77
|
|
|
*/ |
78
|
8 |
|
public function master() { |
79
|
8 |
|
return $this->commit( $this->app->api()->fetch()->master() ); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Imports a provided commit into the database. |
84
|
|
|
* |
85
|
|
|
* @param WordPress_GitHub_Sync_Commit|WP_Error $commit Commit to import. |
86
|
|
|
* |
87
|
|
|
* @return string|WP_Error |
88
|
|
|
*/ |
89
|
10 |
|
protected function commit( $commit ) { |
90
|
10 |
|
if ( is_wp_error( $commit ) ) { |
91
|
2 |
|
return $commit; |
92
|
|
|
} |
93
|
|
|
|
94
|
8 |
|
if ( $commit->already_synced() ) { |
95
|
1 |
|
return new WP_Error( 'commit_synced', __( 'Already synced this commit.', 'wp-github-sync' ) ); |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
$posts = array(); |
99
|
7 |
|
$new = array(); |
100
|
|
|
|
101
|
7 |
|
foreach ( $commit->tree()->blobs() as $blob ) { |
102
|
7 |
|
if ( ! $this->importable_blob( $blob ) ) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
$posts[] = $post = $this->blob_to_post( $blob ); |
107
|
|
|
|
108
|
7 |
|
if ( $post->is_new() ) { |
109
|
6 |
|
$new[] = $post; |
110
|
6 |
|
} |
111
|
7 |
|
} |
112
|
|
|
|
113
|
7 |
|
$result = $this->app->database()->save_posts( $posts, $commit->author_email() ); |
114
|
|
|
|
115
|
7 |
|
if ( is_wp_error( $result ) ) { |
116
|
|
|
return $result; |
117
|
|
|
} |
118
|
|
|
|
119
|
7 |
|
if ( $new ) { |
|
|
|
|
120
|
6 |
|
$result = $this->app->export()->new_posts( $new ); |
121
|
|
|
|
122
|
6 |
|
if ( is_wp_error( $result ) ) { |
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
6 |
|
} |
126
|
|
|
|
127
|
7 |
|
return $posts; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Checks whether the provided blob should be imported. |
132
|
|
|
* |
133
|
|
|
* @param WordPress_GitHub_Sync_Blob $blob Blob to validate. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
7 |
|
protected function importable_blob( WordPress_GitHub_Sync_Blob $blob ) { |
138
|
7 |
|
global $wpdb; |
139
|
|
|
|
140
|
|
|
// Skip the repo's readme. |
141
|
7 |
|
if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// If the blob sha already matches a post, then move on. |
146
|
7 |
|
if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
7 |
|
if ( ! $blob->has_frontmatter() ) { |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
7 |
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Imports a single blob content into matching post. |
159
|
|
|
* |
160
|
|
|
* @param WordPress_GitHub_Sync_Blob $blob Blob to transform into a Post. |
161
|
|
|
* |
162
|
|
|
* @return WordPress_GitHub_Sync_Post |
163
|
|
|
*/ |
164
|
7 |
|
protected function blob_to_post( WordPress_GitHub_Sync_Blob $blob ) { |
165
|
7 |
|
$args = array( 'post_content' => $blob->content_import() ); |
166
|
7 |
|
$meta = $blob->meta(); |
167
|
|
|
|
168
|
7 |
|
if ( $meta ) { |
|
|
|
|
169
|
5 |
|
if ( array_key_exists( 'layout', $meta ) ) { |
170
|
1 |
|
$args['post_type'] = $meta['layout']; |
171
|
1 |
|
unset( $meta['layout'] ); |
172
|
1 |
|
} |
173
|
|
|
|
174
|
5 |
|
if ( array_key_exists( 'published', $meta ) ) { |
175
|
1 |
|
$args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
176
|
1 |
|
unset( $meta['published'] ); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
5 |
|
if ( array_key_exists( 'post_title', $meta ) ) { |
180
|
1 |
|
$args['post_title'] = $meta['post_title']; |
181
|
1 |
|
unset( $meta['post_title'] ); |
182
|
1 |
|
} |
183
|
|
|
|
184
|
5 |
|
if ( array_key_exists( 'ID', $meta ) ) { |
185
|
1 |
|
$args['ID'] = $meta['ID']; |
186
|
1 |
|
unset( $meta['ID'] ); |
187
|
1 |
|
} |
188
|
5 |
|
|
189
|
|
|
if ( array_key_exists( 'post_date', $meta ) ) { |
190
|
7 |
|
|
191
|
|
|
if ( empty( $meta['post_date'] ) ) { |
192
|
7 |
|
$meta['post_date'] = current_time( 'mysql' ); |
193
|
7 |
|
} |
194
|
|
|
|
195
|
7 |
|
$args['post_date'] = $meta['post_date']; |
196
|
|
|
|
197
|
|
|
$args['post_date_gmt'] = get_gmt_from_date( $meta['post_date'] ); |
198
|
|
|
unset( $meta['post_date'] ); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$meta['_sha'] = $blob->sha(); |
203
|
|
|
|
204
|
|
|
$post = new WordPress_GitHub_Sync_Post( $args, $this->app->api() ); |
205
|
|
|
$post->set_meta( $meta ); |
206
|
|
|
|
207
|
|
|
return $post; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.