1 | <?php |
||||||
2 | /** |
||||||
3 | * GitHub Export Manager. |
||||||
4 | * |
||||||
5 | * @package Writing_On_GitHub |
||||||
6 | */ |
||||||
7 | |||||||
8 | /** |
||||||
9 | * Class Writing_On_GitHub_Export |
||||||
10 | */ |
||||||
11 | class Writing_On_GitHub_Export { |
||||||
12 | |||||||
13 | /** |
||||||
14 | * Application container. |
||||||
15 | * |
||||||
16 | * @var Writing_On_GitHub |
||||||
17 | */ |
||||||
18 | protected $app; |
||||||
19 | |||||||
20 | /** |
||||||
21 | * Initializes a new export manager. |
||||||
22 | * |
||||||
23 | * @param Writing_On_GitHub $app Application container. |
||||||
24 | */ |
||||||
25 | public function __construct( Writing_On_GitHub $app ) { |
||||||
26 | $this->app = $app; |
||||||
27 | } |
||||||
28 | |||||||
29 | /** |
||||||
30 | * Updates all of the current posts in the database on master. |
||||||
31 | * |
||||||
32 | * @param bool $force |
||||||
33 | * |
||||||
34 | * @return string|WP_Error |
||||||
35 | */ |
||||||
36 | public function full( $force = false ) { |
||||||
37 | $posts = $this->app->database()->fetch_all_supported( $force ); |
||||||
38 | |||||||
39 | if ( is_wp_error( $posts ) ) { |
||||||
40 | /* @var WP_Error $posts */ |
||||||
41 | return $posts; |
||||||
42 | } |
||||||
43 | |||||||
44 | $error = ''; |
||||||
45 | |||||||
46 | foreach ( $posts as $post ) { |
||||||
47 | $result = $this->update( $post->id() ); |
||||||
48 | if ( is_wp_error( $result ) ) { |
||||||
49 | /* @var WP_Error $result */ |
||||||
50 | $error = wogh_append_error( $error, $result ); |
||||||
51 | } |
||||||
52 | } |
||||||
53 | |||||||
54 | if ( is_wp_error( $error ) ) { |
||||||
55 | /* @var WP_Error $error */ |
||||||
56 | return $error; |
||||||
57 | } |
||||||
58 | |||||||
59 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||||||
60 | } |
||||||
61 | |||||||
62 | |||||||
63 | /** |
||||||
64 | * Check if it exists in github |
||||||
65 | * @param int $post_id |
||||||
66 | * @return boolean |
||||||
67 | */ |
||||||
68 | protected function github_path( $post_id ) { |
||||||
69 | $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
||||||
70 | |||||||
71 | if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
||||||
72 | return $github_path; |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
73 | } |
||||||
74 | |||||||
75 | return false; |
||||||
76 | } |
||||||
77 | |||||||
78 | /** |
||||||
79 | * Updates the provided post ID in master. |
||||||
80 | * |
||||||
81 | * @param int $post_id Post ID to update. |
||||||
82 | * |
||||||
83 | * @return string|WP_Error |
||||||
84 | */ |
||||||
85 | public function update( $post_id ) { |
||||||
86 | $post = $this->app->database()->fetch_by_id( $post_id ); |
||||||
87 | |||||||
88 | if ( is_wp_error( $post ) ) { |
||||||
89 | /* @var WP_Error $post */ |
||||||
90 | return $post; |
||||||
91 | } |
||||||
92 | |||||||
93 | if ( 'trash' === $post->status() ) { |
||||||
0 ignored issues
–
show
The method
status() does not exist on WP_Error .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
94 | return $this->delete( $post_id ); |
||||||
95 | } |
||||||
96 | |||||||
97 | if ( $old_github_path = $this->github_path( $post->id() ) ) { |
||||||
0 ignored issues
–
show
The method
id() does not exist on WP_Error .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
98 | error_log("old_github_path: $old_github_path"); |
||||||
99 | $post->set_old_github_path($old_github_path); |
||||||
0 ignored issues
–
show
The method
set_old_github_path() does not exist on WP_Error .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
100 | } |
||||||
101 | |||||||
102 | $result = $this->export_post( $post ); |
||||||
0 ignored issues
–
show
It seems like
$post can also be of type WP_Error ; however, parameter $post of Writing_On_GitHub_Export::export_post() does only seem to accept Writing_On_GitHub_Post , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
103 | |||||||
104 | if ( is_wp_error( $result ) ) { |
||||||
105 | /* @var WP_Error $result */ |
||||||
106 | return $result; |
||||||
107 | } |
||||||
108 | |||||||
109 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||||||
110 | } |
||||||
111 | |||||||
112 | /** |
||||||
113 | * Post to blob |
||||||
114 | * @param Writing_On_GitHub_Post $post |
||||||
115 | * @return WP_Error|Writing_On_GitHub_Blob |
||||||
116 | */ |
||||||
117 | protected function post_to_blob( Writing_On_GitHub_Post $post ) { |
||||||
118 | if ( ! $post->get_blob() |
||||||
119 | && $post->old_github_path() |
||||||
120 | && wogh_is_dont_export_content() ) { |
||||||
121 | |||||||
122 | |||||||
123 | $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() ); |
||||||
124 | |||||||
125 | if ( is_wp_error( $blob ) ) { |
||||||
126 | /** @var WP_Error $blob */ |
||||||
127 | return $blob; |
||||||
128 | } |
||||||
129 | |||||||
130 | $post->set_blob( $blob ); |
||||||
0 ignored issues
–
show
It seems like
$blob can also be of type WP_Error ; however, parameter $blob of Writing_On_GitHub_Post::set_blob() does only seem to accept Writing_On_GitHub_Blob , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
131 | } |
||||||
132 | |||||||
133 | return $post->to_blob(); |
||||||
134 | } |
||||||
135 | |||||||
136 | /** |
||||||
137 | * Export post to github |
||||||
138 | * @param Writing_On_GitHub_Post $post |
||||||
139 | * @return WP_Error|true |
||||||
140 | */ |
||||||
141 | public function export_post( Writing_On_GitHub_Post $post ) { |
||||||
142 | // check blob |
||||||
143 | $blob = $this->post_to_blob( $post ); |
||||||
144 | if ( is_wp_error( $blob ) ) { |
||||||
145 | /** @var WP_Error $blob */ |
||||||
146 | return $blob; |
||||||
147 | } |
||||||
148 | |||||||
149 | $result = false; |
||||||
150 | |||||||
151 | $persist = $this->app->api()->persist(); |
||||||
152 | $github_path = $post->github_path(); |
||||||
153 | $old_github_path = $post->old_github_path(); |
||||||
154 | |||||||
155 | if ( $old_github_path && $old_github_path != $github_path ) { |
||||||
156 | // rename |
||||||
157 | $message = apply_filters( |
||||||
158 | 'wogh_commit_msg_move_post', |
||||||
159 | sprintf( |
||||||
160 | 'Move %s to %s via WordPress at %s (%s)', |
||||||
161 | $old_github_path, $github_path, |
||||||
162 | site_url(), |
||||||
163 | get_bloginfo( 'name' ) |
||||||
164 | ) |
||||||
165 | ) . $this->get_commit_msg_tag(); |
||||||
166 | |||||||
167 | $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
||||||
0 ignored issues
–
show
The method
sha() does not exist on WP_Error .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
168 | if ( is_wp_error( $result ) ) { |
||||||
169 | return $result; |
||||||
0 ignored issues
–
show
|
|||||||
170 | } |
||||||
171 | |||||||
172 | $result = $persist->create_file( $blob, $message ); |
||||||
173 | if ( is_wp_error( $result ) ) { |
||||||
174 | return $result; |
||||||
0 ignored issues
–
show
|
|||||||
175 | } |
||||||
176 | } elseif ( ! $old_github_path ) { |
||||||
177 | // create new |
||||||
178 | $message = apply_filters( |
||||||
179 | 'wogh_commit_msg_new_post', |
||||||
180 | sprintf( |
||||||
181 | 'Create new post %s from WordPress at %s (%s)', |
||||||
182 | $github_path, |
||||||
183 | site_url(), |
||||||
184 | get_bloginfo( 'name' ) |
||||||
185 | ) |
||||||
186 | ) . $this->get_commit_msg_tag(); |
||||||
187 | $result = $persist->create_file( $blob, $message ); |
||||||
188 | if ( is_wp_error( $result ) ) { |
||||||
189 | return $result; |
||||||
0 ignored issues
–
show
|
|||||||
190 | } |
||||||
191 | } elseif ( $old_github_path && $old_github_path == $github_path ) { |
||||||
192 | // update |
||||||
193 | $sha = wogh_git_sha( $blob->content() ); |
||||||
0 ignored issues
–
show
The method
content() does not exist on WP_Error .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
194 | if ( $sha === $blob->sha() ) { |
||||||
195 | // don't export when has not changed |
||||||
196 | return true; |
||||||
197 | } |
||||||
198 | $message = apply_filters( |
||||||
199 | 'wogh_commit_msg_update_post', |
||||||
200 | sprintf( |
||||||
201 | 'Update post %s from WordPress at %s (%s)', |
||||||
202 | $github_path, |
||||||
203 | site_url(), |
||||||
204 | get_bloginfo( 'name' ) |
||||||
205 | ) |
||||||
206 | ) . $this->get_commit_msg_tag(); |
||||||
207 | $result = $persist->update_file( $blob, $message ); |
||||||
208 | if ( is_wp_error( $result ) ) { |
||||||
209 | return $result; |
||||||
0 ignored issues
–
show
|
|||||||
210 | } |
||||||
211 | } |
||||||
212 | |||||||
213 | $sha = $result->content->sha; |
||||||
0 ignored issues
–
show
|
|||||||
214 | $post->set_sha( $sha ); |
||||||
215 | $post->set_old_github_path( $github_path ); |
||||||
216 | |||||||
217 | return true; |
||||||
218 | } |
||||||
219 | |||||||
220 | /** |
||||||
221 | * Deletes a provided post ID from master. |
||||||
222 | * |
||||||
223 | * @param int $post_id Post ID to delete. |
||||||
224 | * |
||||||
225 | * @return string|WP_Error |
||||||
226 | */ |
||||||
227 | public function delete( $post_id ) { |
||||||
228 | $post = $this->app->database()->fetch_by_id( $post_id ); |
||||||
229 | |||||||
230 | if ( is_wp_error( $post ) ) { |
||||||
231 | /* @var WP_Error $post */ |
||||||
232 | return $post; |
||||||
233 | } |
||||||
234 | |||||||
235 | $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
||||||
236 | |||||||
237 | $message = apply_filters( |
||||||
238 | 'wogh_commit_msg_delete', |
||||||
239 | sprintf( |
||||||
240 | 'Deleting %s via WordPress at %s (%s)', |
||||||
241 | $github_path, |
||||||
0 ignored issues
–
show
It seems like
$github_path can also be of type false ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
242 | site_url(), |
||||||
243 | get_bloginfo( 'name' ) |
||||||
244 | ), |
||||||
245 | $post |
||||||
246 | ) . $this->get_commit_msg_tag(); |
||||||
247 | |||||||
248 | $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
||||||
249 | |||||||
250 | if ( is_wp_error( $result ) ) { |
||||||
251 | /* @var WP_Error $result */ |
||||||
252 | return $result; |
||||||
253 | } |
||||||
254 | |||||||
255 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||||||
256 | } |
||||||
257 | |||||||
258 | |||||||
259 | /** |
||||||
260 | * Saves the export user to the database. |
||||||
261 | * |
||||||
262 | * @param int $user_id User ID to export with. |
||||||
263 | * |
||||||
264 | * @return bool |
||||||
265 | */ |
||||||
266 | public function set_user( $user_id ) { |
||||||
267 | return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
||||||
0 ignored issues
–
show
|
|||||||
268 | } |
||||||
269 | |||||||
270 | /** |
||||||
271 | * Gets the commit message tag. |
||||||
272 | * |
||||||
273 | * @return string |
||||||
274 | */ |
||||||
275 | protected function get_commit_msg_tag() { |
||||||
276 | $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
||||||
277 | |||||||
278 | if ( ! $tag ) { |
||||||
279 | throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
||||||
280 | } |
||||||
281 | |||||||
282 | return ' - ' . $tag; |
||||||
283 | } |
||||||
284 | } |
||||||
285 |