@@ -9,231 +9,231 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Controller { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - public $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Instantiates a new Controller object |
|
21 | - * |
|
22 | - * @param Writing_On_GitHub $app Applicatio container. |
|
23 | - */ |
|
24 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | - $this->app = $app; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Webhook callback as triggered from GitHub push. |
|
30 | - * |
|
31 | - * Reads the Webhook payload and syncs posts as necessary. |
|
32 | - * |
|
33 | - * @return boolean |
|
34 | - */ |
|
35 | - public function pull_posts() { |
|
36 | - $this->set_ajax(); |
|
37 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | - return $this->app->response()->error( new WP_Error( |
|
39 | - 'semaphore_locked', |
|
40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | - ) ); |
|
42 | - } |
|
43 | - |
|
44 | - if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | - return $this->app->response()->error( new WP_Error( |
|
46 | - 'invalid_headers', |
|
47 | - __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | - ) ); |
|
49 | - } |
|
50 | - |
|
51 | - // ping |
|
52 | - if ( $this->app->request()->is_ping() ) { |
|
53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
54 | - } |
|
55 | - |
|
56 | - // push |
|
57 | - if ( ! $this->app->request()->is_push() ) { |
|
58 | - return $this->app->response()->error( new WP_Error( |
|
59 | - 'invalid_headers', |
|
60 | - __( 'Failed to validate webhook event.', 'writing-on-github' ) |
|
61 | - ) ); |
|
62 | - } |
|
63 | - $payload = $this->app->request()->payload(); |
|
64 | - |
|
65 | - $error = $payload->should_import(); |
|
66 | - if ( is_wp_error( $error ) ) { |
|
67 | - /* @var WP_Error $error */ |
|
68 | - return $this->app->response()->error( $error ); |
|
69 | - } |
|
70 | - |
|
71 | - $this->app->semaphore()->lock(); |
|
72 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
73 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
74 | - |
|
75 | - $result = $this->app->import()->payload( $payload ); |
|
76 | - |
|
77 | - $this->app->semaphore()->unlock(); |
|
78 | - |
|
79 | - if ( is_wp_error( $result ) ) { |
|
80 | - /* @var WP_Error $result */ |
|
81 | - return $this->app->response()->error( $result ); |
|
82 | - } |
|
83 | - |
|
84 | - return $this->app->response()->success( $result ); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Imports posts from the current master branch. |
|
89 | - * @param integer $user_id |
|
90 | - * @param boolean $force |
|
91 | - * @return boolean |
|
92 | - */ |
|
93 | - public function import_master( $user_id = 0, $force = false ) { |
|
94 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
95 | - return $this->app->response()->error( new WP_Error( |
|
96 | - 'semaphore_locked', |
|
97 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
98 | - ) ); |
|
99 | - } |
|
100 | - |
|
101 | - $this->app->semaphore()->lock(); |
|
102 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
103 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
104 | - |
|
105 | - if ( $user_id ) { |
|
106 | - wp_set_current_user( $user_id ); |
|
107 | - } |
|
108 | - |
|
109 | - $result = $this->app->import()->master( $force ); |
|
110 | - |
|
111 | - $this->app->semaphore()->unlock(); |
|
112 | - |
|
113 | - if ( is_wp_error( $result ) ) { |
|
114 | - /* @var WP_Error $result */ |
|
115 | - update_option( '_wogh_import_error', $result->get_error_message() ); |
|
116 | - |
|
117 | - return $this->app->response()->error( $result ); |
|
118 | - } |
|
119 | - |
|
120 | - update_option( '_wogh_import_complete', 'yes' ); |
|
121 | - |
|
122 | - return $this->app->response()->success( $result ); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Export all the posts in the database to GitHub. |
|
127 | - * |
|
128 | - * @param int $user_id |
|
129 | - * @param boolean $force |
|
130 | - * @return boolean |
|
131 | - */ |
|
132 | - public function export_all( $user_id = 0, $force = false ) { |
|
133 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
134 | - return $this->app->response()->error( new WP_Error( |
|
135 | - 'semaphore_locked', |
|
136 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
137 | - ) ); |
|
138 | - } |
|
139 | - |
|
140 | - $this->app->semaphore()->lock(); |
|
141 | - |
|
142 | - if ( $user_id ) { |
|
143 | - wp_set_current_user( $user_id ); |
|
144 | - } |
|
145 | - |
|
146 | - $result = $this->app->export()->full($force); |
|
147 | - $this->app->semaphore()->unlock(); |
|
148 | - |
|
149 | - // Maybe move option updating out of this class/upgrade message display? |
|
150 | - if ( is_wp_error( $result ) ) { |
|
151 | - /* @var WP_Error $result */ |
|
152 | - update_option( '_wogh_export_error', $result->get_error_message() ); |
|
153 | - |
|
154 | - return $this->app->response()->error( $result ); |
|
155 | - } else { |
|
156 | - update_option( '_wogh_export_complete', 'yes' ); |
|
157 | - update_option( '_wogh_fully_exported', 'yes' ); |
|
158 | - |
|
159 | - return $this->app->response()->success( $result ); |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Exports a single post to GitHub by ID. |
|
165 | - * |
|
166 | - * Called on the save_post hook. |
|
167 | - * |
|
168 | - * @param int $post_id Post ID. |
|
169 | - * |
|
170 | - * @return boolean |
|
171 | - */ |
|
172 | - public function export_post( $post_id ) { |
|
173 | - if ( wp_is_post_revision( $post_id ) ) { |
|
174 | - return; |
|
175 | - } |
|
176 | - |
|
177 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
178 | - return $this->app->response()->error( new WP_Error( |
|
179 | - 'semaphore_locked', |
|
180 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
181 | - ) ); |
|
182 | - } |
|
183 | - |
|
184 | - $this->app->semaphore()->lock(); |
|
185 | - $result = $this->app->export()->update( $post_id ); |
|
186 | - $this->app->semaphore()->unlock(); |
|
187 | - |
|
188 | - if ( is_wp_error( $result ) ) { |
|
189 | - /* @var WP_Error $result */ |
|
190 | - return $this->app->response()->error( $result ); |
|
191 | - } |
|
192 | - |
|
193 | - return $this->app->response()->success( $result ); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Removes the post from the tree. |
|
198 | - * |
|
199 | - * Called the delete_post hook. |
|
200 | - * |
|
201 | - * @param int $post_id Post ID. |
|
202 | - * |
|
203 | - * @return boolean |
|
204 | - */ |
|
205 | - public function delete_post( $post_id ) { |
|
206 | - if ( wp_is_post_revision( $post_id ) ) { |
|
207 | - return; |
|
208 | - } |
|
209 | - |
|
210 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
211 | - return $this->app->response()->error( new WP_Error( |
|
212 | - 'semaphore_locked', |
|
213 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
214 | - ) ); |
|
215 | - } |
|
216 | - |
|
217 | - $this->app->semaphore()->lock(); |
|
218 | - $result = $this->app->export()->delete( $post_id ); |
|
219 | - $this->app->semaphore()->unlock(); |
|
220 | - |
|
221 | - if ( is_wp_error( $result ) ) { |
|
222 | - /* @var WP_Error $result */ |
|
223 | - return $this->app->response()->error( $result ); |
|
224 | - } |
|
225 | - |
|
226 | - return $this->app->response()->success( $result ); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Indicates we're running our own AJAX hook |
|
231 | - * and thus should respond with JSON, rather |
|
232 | - * than just returning data. |
|
233 | - */ |
|
234 | - protected function set_ajax() { |
|
235 | - if ( ! defined( 'WOGH_AJAX' ) ) { |
|
236 | - define( 'WOGH_AJAX', true ); |
|
237 | - } |
|
238 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + public $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Instantiates a new Controller object |
|
21 | + * |
|
22 | + * @param Writing_On_GitHub $app Applicatio container. |
|
23 | + */ |
|
24 | + public function __construct( Writing_On_GitHub $app ) { |
|
25 | + $this->app = $app; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Webhook callback as triggered from GitHub push. |
|
30 | + * |
|
31 | + * Reads the Webhook payload and syncs posts as necessary. |
|
32 | + * |
|
33 | + * @return boolean |
|
34 | + */ |
|
35 | + public function pull_posts() { |
|
36 | + $this->set_ajax(); |
|
37 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | + return $this->app->response()->error( new WP_Error( |
|
39 | + 'semaphore_locked', |
|
40 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | + ) ); |
|
42 | + } |
|
43 | + |
|
44 | + if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | + return $this->app->response()->error( new WP_Error( |
|
46 | + 'invalid_headers', |
|
47 | + __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | + ) ); |
|
49 | + } |
|
50 | + |
|
51 | + // ping |
|
52 | + if ( $this->app->request()->is_ping() ) { |
|
53 | + return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
54 | + } |
|
55 | + |
|
56 | + // push |
|
57 | + if ( ! $this->app->request()->is_push() ) { |
|
58 | + return $this->app->response()->error( new WP_Error( |
|
59 | + 'invalid_headers', |
|
60 | + __( 'Failed to validate webhook event.', 'writing-on-github' ) |
|
61 | + ) ); |
|
62 | + } |
|
63 | + $payload = $this->app->request()->payload(); |
|
64 | + |
|
65 | + $error = $payload->should_import(); |
|
66 | + if ( is_wp_error( $error ) ) { |
|
67 | + /* @var WP_Error $error */ |
|
68 | + return $this->app->response()->error( $error ); |
|
69 | + } |
|
70 | + |
|
71 | + $this->app->semaphore()->lock(); |
|
72 | + remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
73 | + remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
74 | + |
|
75 | + $result = $this->app->import()->payload( $payload ); |
|
76 | + |
|
77 | + $this->app->semaphore()->unlock(); |
|
78 | + |
|
79 | + if ( is_wp_error( $result ) ) { |
|
80 | + /* @var WP_Error $result */ |
|
81 | + return $this->app->response()->error( $result ); |
|
82 | + } |
|
83 | + |
|
84 | + return $this->app->response()->success( $result ); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Imports posts from the current master branch. |
|
89 | + * @param integer $user_id |
|
90 | + * @param boolean $force |
|
91 | + * @return boolean |
|
92 | + */ |
|
93 | + public function import_master( $user_id = 0, $force = false ) { |
|
94 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
95 | + return $this->app->response()->error( new WP_Error( |
|
96 | + 'semaphore_locked', |
|
97 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
98 | + ) ); |
|
99 | + } |
|
100 | + |
|
101 | + $this->app->semaphore()->lock(); |
|
102 | + remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
103 | + remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
104 | + |
|
105 | + if ( $user_id ) { |
|
106 | + wp_set_current_user( $user_id ); |
|
107 | + } |
|
108 | + |
|
109 | + $result = $this->app->import()->master( $force ); |
|
110 | + |
|
111 | + $this->app->semaphore()->unlock(); |
|
112 | + |
|
113 | + if ( is_wp_error( $result ) ) { |
|
114 | + /* @var WP_Error $result */ |
|
115 | + update_option( '_wogh_import_error', $result->get_error_message() ); |
|
116 | + |
|
117 | + return $this->app->response()->error( $result ); |
|
118 | + } |
|
119 | + |
|
120 | + update_option( '_wogh_import_complete', 'yes' ); |
|
121 | + |
|
122 | + return $this->app->response()->success( $result ); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Export all the posts in the database to GitHub. |
|
127 | + * |
|
128 | + * @param int $user_id |
|
129 | + * @param boolean $force |
|
130 | + * @return boolean |
|
131 | + */ |
|
132 | + public function export_all( $user_id = 0, $force = false ) { |
|
133 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
134 | + return $this->app->response()->error( new WP_Error( |
|
135 | + 'semaphore_locked', |
|
136 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
137 | + ) ); |
|
138 | + } |
|
139 | + |
|
140 | + $this->app->semaphore()->lock(); |
|
141 | + |
|
142 | + if ( $user_id ) { |
|
143 | + wp_set_current_user( $user_id ); |
|
144 | + } |
|
145 | + |
|
146 | + $result = $this->app->export()->full($force); |
|
147 | + $this->app->semaphore()->unlock(); |
|
148 | + |
|
149 | + // Maybe move option updating out of this class/upgrade message display? |
|
150 | + if ( is_wp_error( $result ) ) { |
|
151 | + /* @var WP_Error $result */ |
|
152 | + update_option( '_wogh_export_error', $result->get_error_message() ); |
|
153 | + |
|
154 | + return $this->app->response()->error( $result ); |
|
155 | + } else { |
|
156 | + update_option( '_wogh_export_complete', 'yes' ); |
|
157 | + update_option( '_wogh_fully_exported', 'yes' ); |
|
158 | + |
|
159 | + return $this->app->response()->success( $result ); |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Exports a single post to GitHub by ID. |
|
165 | + * |
|
166 | + * Called on the save_post hook. |
|
167 | + * |
|
168 | + * @param int $post_id Post ID. |
|
169 | + * |
|
170 | + * @return boolean |
|
171 | + */ |
|
172 | + public function export_post( $post_id ) { |
|
173 | + if ( wp_is_post_revision( $post_id ) ) { |
|
174 | + return; |
|
175 | + } |
|
176 | + |
|
177 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
178 | + return $this->app->response()->error( new WP_Error( |
|
179 | + 'semaphore_locked', |
|
180 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
181 | + ) ); |
|
182 | + } |
|
183 | + |
|
184 | + $this->app->semaphore()->lock(); |
|
185 | + $result = $this->app->export()->update( $post_id ); |
|
186 | + $this->app->semaphore()->unlock(); |
|
187 | + |
|
188 | + if ( is_wp_error( $result ) ) { |
|
189 | + /* @var WP_Error $result */ |
|
190 | + return $this->app->response()->error( $result ); |
|
191 | + } |
|
192 | + |
|
193 | + return $this->app->response()->success( $result ); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Removes the post from the tree. |
|
198 | + * |
|
199 | + * Called the delete_post hook. |
|
200 | + * |
|
201 | + * @param int $post_id Post ID. |
|
202 | + * |
|
203 | + * @return boolean |
|
204 | + */ |
|
205 | + public function delete_post( $post_id ) { |
|
206 | + if ( wp_is_post_revision( $post_id ) ) { |
|
207 | + return; |
|
208 | + } |
|
209 | + |
|
210 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
211 | + return $this->app->response()->error( new WP_Error( |
|
212 | + 'semaphore_locked', |
|
213 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
214 | + ) ); |
|
215 | + } |
|
216 | + |
|
217 | + $this->app->semaphore()->lock(); |
|
218 | + $result = $this->app->export()->delete( $post_id ); |
|
219 | + $this->app->semaphore()->unlock(); |
|
220 | + |
|
221 | + if ( is_wp_error( $result ) ) { |
|
222 | + /* @var WP_Error $result */ |
|
223 | + return $this->app->response()->error( $result ); |
|
224 | + } |
|
225 | + |
|
226 | + return $this->app->response()->success( $result ); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Indicates we're running our own AJAX hook |
|
231 | + * and thus should respond with JSON, rather |
|
232 | + * than just returning data. |
|
233 | + */ |
|
234 | + protected function set_ajax() { |
|
235 | + if ( ! defined( 'WOGH_AJAX' ) ) { |
|
236 | + define( 'WOGH_AJAX', true ); |
|
237 | + } |
|
238 | + } |
|
239 | 239 | } |
@@ -9,134 +9,134 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Payload { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - protected $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Payload data. |
|
21 | - * |
|
22 | - * @var stdClass |
|
23 | - */ |
|
24 | - protected $data; |
|
25 | - |
|
26 | - /** |
|
27 | - * Writing_On_GitHub_Payload constructor. |
|
28 | - * |
|
29 | - * @param Writing_On_GitHub $app Application container. |
|
30 | - * @param string $raw_data Raw request data. |
|
31 | - */ |
|
32 | - public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
33 | - $this->app = $app; |
|
34 | - $this->data = json_decode( $raw_data ); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns whether payload should be imported. |
|
39 | - * |
|
40 | - * @return true|WP_Error |
|
41 | - */ |
|
42 | - public function should_import() { |
|
43 | - // @todo how do we get this without importing the whole api object just for this? |
|
44 | - if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
45 | - return new WP_Error( |
|
46 | - 'incorrect_repository', |
|
47 | - sprintf( 'Incorrect repository, %s -> %s .', |
|
48 | - $this->data->repository->full_name, |
|
49 | - $this->app->api()->fetch()->repository() |
|
50 | - ) |
|
51 | - ); |
|
52 | - } |
|
53 | - |
|
54 | - // The last term in the ref is the payload_branch name. |
|
55 | - $refs = explode( '/', $this->data->ref ); |
|
56 | - $payload_branch = array_pop( $refs ); |
|
57 | - |
|
58 | - $branch = $this->app->api()->fetch()->branch(); |
|
59 | - |
|
60 | - if ( $branch !== $payload_branch ) { |
|
61 | - return new WP_Error( |
|
62 | - 'incorrect_branch', |
|
63 | - sprintf( 'Incorrect branch, %s -> %s .', |
|
64 | - $payload_branch, |
|
65 | - $branch |
|
66 | - ) |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - // We add a tag to commits we push out, so we shouldn't pull them in again. |
|
71 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
72 | - |
|
73 | - if ( ! $tag ) { |
|
74 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
75 | - } |
|
76 | - |
|
77 | - if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
78 | - return new WP_Error( |
|
79 | - 'skip_import', |
|
80 | - 'Skip import on auto export post.' |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - if ( ! $this->get_commit_id() ) { |
|
85 | - return new WP_Error( |
|
86 | - 'invalid_payload', |
|
87 | - "[Missing Commit ID] won't be imported." |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - return true; |
|
92 | - } |
|
93 | - |
|
94 | - public function get_before_commit_id() { |
|
95 | - return $this->data->before ? $this->data->before : null; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns the sha of the head commit. |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function get_commit_id() { |
|
104 | - return $this->data->head_commit ? $this->data->head_commit->id : null; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns the email address for the commit author. |
|
109 | - * |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public function get_author_email() { |
|
113 | - return $this->data->head_commit->author->email; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns array commits for the payload. |
|
118 | - * |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function get_commits() { |
|
122 | - return $this->data->commits; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns the repository's full name. |
|
127 | - * |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function get_repository_name() { |
|
131 | - return $this->data->repository->full_name; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Returns the payload's commit message. |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - protected function message() { |
|
140 | - return $this->data->head_commit->message; |
|
141 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + protected $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Payload data. |
|
21 | + * |
|
22 | + * @var stdClass |
|
23 | + */ |
|
24 | + protected $data; |
|
25 | + |
|
26 | + /** |
|
27 | + * Writing_On_GitHub_Payload constructor. |
|
28 | + * |
|
29 | + * @param Writing_On_GitHub $app Application container. |
|
30 | + * @param string $raw_data Raw request data. |
|
31 | + */ |
|
32 | + public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
33 | + $this->app = $app; |
|
34 | + $this->data = json_decode( $raw_data ); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns whether payload should be imported. |
|
39 | + * |
|
40 | + * @return true|WP_Error |
|
41 | + */ |
|
42 | + public function should_import() { |
|
43 | + // @todo how do we get this without importing the whole api object just for this? |
|
44 | + if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
45 | + return new WP_Error( |
|
46 | + 'incorrect_repository', |
|
47 | + sprintf( 'Incorrect repository, %s -> %s .', |
|
48 | + $this->data->repository->full_name, |
|
49 | + $this->app->api()->fetch()->repository() |
|
50 | + ) |
|
51 | + ); |
|
52 | + } |
|
53 | + |
|
54 | + // The last term in the ref is the payload_branch name. |
|
55 | + $refs = explode( '/', $this->data->ref ); |
|
56 | + $payload_branch = array_pop( $refs ); |
|
57 | + |
|
58 | + $branch = $this->app->api()->fetch()->branch(); |
|
59 | + |
|
60 | + if ( $branch !== $payload_branch ) { |
|
61 | + return new WP_Error( |
|
62 | + 'incorrect_branch', |
|
63 | + sprintf( 'Incorrect branch, %s -> %s .', |
|
64 | + $payload_branch, |
|
65 | + $branch |
|
66 | + ) |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + // We add a tag to commits we push out, so we shouldn't pull them in again. |
|
71 | + $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
72 | + |
|
73 | + if ( ! $tag ) { |
|
74 | + throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
75 | + } |
|
76 | + |
|
77 | + if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
78 | + return new WP_Error( |
|
79 | + 'skip_import', |
|
80 | + 'Skip import on auto export post.' |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + if ( ! $this->get_commit_id() ) { |
|
85 | + return new WP_Error( |
|
86 | + 'invalid_payload', |
|
87 | + "[Missing Commit ID] won't be imported." |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + return true; |
|
92 | + } |
|
93 | + |
|
94 | + public function get_before_commit_id() { |
|
95 | + return $this->data->before ? $this->data->before : null; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns the sha of the head commit. |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function get_commit_id() { |
|
104 | + return $this->data->head_commit ? $this->data->head_commit->id : null; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns the email address for the commit author. |
|
109 | + * |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public function get_author_email() { |
|
113 | + return $this->data->head_commit->author->email; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns array commits for the payload. |
|
118 | + * |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function get_commits() { |
|
122 | + return $this->data->commits; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns the repository's full name. |
|
127 | + * |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function get_repository_name() { |
|
131 | + return $this->data->repository->full_name; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Returns the payload's commit message. |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + protected function message() { |
|
140 | + return $this->data->head_commit->message; |
|
141 | + } |
|
142 | 142 | } |