@@ -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 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @param Writing_On_GitHub $app Applicatio container. |
23 | 23 | */ |
24 | - public function __construct( Writing_On_GitHub $app ) { |
|
24 | + public function __construct(Writing_On_GitHub $app) { |
|
25 | 25 | $this->app = $app; |
26 | 26 | } |
27 | 27 | |
@@ -34,54 +34,54 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function pull_posts() { |
36 | 36 | $this->set_ajax(); |
37 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | - return $this->app->response()->error( new WP_Error( |
|
37 | + if ( ! $this->app->semaphore()->is_open()) { |
|
38 | + return $this->app->response()->error(new WP_Error( |
|
39 | 39 | 'semaphore_locked', |
40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | - ) ); |
|
40 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::pull_posts()') |
|
41 | + )); |
|
42 | 42 | } |
43 | 43 | |
44 | - if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | - return $this->app->response()->error( new WP_Error( |
|
44 | + if ( ! $this->app->request()->is_secret_valid()) { |
|
45 | + return $this->app->response()->error(new WP_Error( |
|
46 | 46 | 'invalid_headers', |
47 | - __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | - ) ); |
|
47 | + __('Failed to validate secret.', 'writing-on-github') |
|
48 | + )); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | // ping |
52 | - if ( $this->app->request()->is_ping() ) { |
|
53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
52 | + if ($this->app->request()->is_ping()) { |
|
53 | + return $this->app->response()->success(__('Wordpress is ready.', 'writing-on-github')); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | // push |
57 | - if ( ! $this->app->request()->is_push() ) { |
|
58 | - return $this->app->response()->error( new WP_Error( |
|
57 | + if ( ! $this->app->request()->is_push()) { |
|
58 | + return $this->app->response()->error(new WP_Error( |
|
59 | 59 | 'invalid_headers', |
60 | - __( 'Failed to validate webhook event.', 'writing-on-github' ) |
|
61 | - ) ); |
|
60 | + __('Failed to validate webhook event.', 'writing-on-github') |
|
61 | + )); |
|
62 | 62 | } |
63 | 63 | $payload = $this->app->request()->payload(); |
64 | 64 | |
65 | 65 | $error = $payload->should_import(); |
66 | - if ( is_wp_error( $error ) ) { |
|
66 | + if (is_wp_error($error)) { |
|
67 | 67 | /* @var WP_Error $error */ |
68 | - return $this->app->response()->error( $error ); |
|
68 | + return $this->app->response()->error($error); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | $this->app->semaphore()->lock(); |
72 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
73 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
72 | + remove_action('save_post', array($this, 'export_post')); |
|
73 | + remove_action('delete_post', array($this, 'delete_post')); |
|
74 | 74 | |
75 | - $result = $this->app->import()->payload( $payload ); |
|
75 | + $result = $this->app->import()->payload($payload); |
|
76 | 76 | |
77 | 77 | $this->app->semaphore()->unlock(); |
78 | 78 | |
79 | - if ( is_wp_error( $result ) ) { |
|
79 | + if (is_wp_error($result)) { |
|
80 | 80 | /* @var WP_Error $result */ |
81 | - return $this->app->response()->error( $result ); |
|
81 | + return $this->app->response()->error($result); |
|
82 | 82 | } |
83 | 83 | |
84 | - return $this->app->response()->success( $result ); |
|
84 | + return $this->app->response()->success($result); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -90,36 +90,36 @@ discard block |
||
90 | 90 | * @param boolean $force |
91 | 91 | * @return boolean |
92 | 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( |
|
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 | 96 | 'semaphore_locked', |
97 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
98 | - ) ); |
|
97 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::import_master()') |
|
98 | + )); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | $this->app->semaphore()->lock(); |
102 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
103 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
102 | + remove_action('save_post', array($this, 'export_post')); |
|
103 | + remove_action('save_post', array($this, 'delete_post')); |
|
104 | 104 | |
105 | - if ( $user_id ) { |
|
106 | - wp_set_current_user( $user_id ); |
|
105 | + if ($user_id) { |
|
106 | + wp_set_current_user($user_id); |
|
107 | 107 | } |
108 | 108 | |
109 | - $result = $this->app->import()->master( $force ); |
|
109 | + $result = $this->app->import()->master($force); |
|
110 | 110 | |
111 | 111 | $this->app->semaphore()->unlock(); |
112 | 112 | |
113 | - if ( is_wp_error( $result ) ) { |
|
113 | + if (is_wp_error($result)) { |
|
114 | 114 | /* @var WP_Error $result */ |
115 | - update_option( '_wogh_import_error', $result->get_error_message() ); |
|
115 | + update_option('_wogh_import_error', $result->get_error_message()); |
|
116 | 116 | |
117 | - return $this->app->response()->error( $result ); |
|
117 | + return $this->app->response()->error($result); |
|
118 | 118 | } |
119 | 119 | |
120 | - update_option( '_wogh_import_complete', 'yes' ); |
|
120 | + update_option('_wogh_import_complete', 'yes'); |
|
121 | 121 | |
122 | - return $this->app->response()->success( $result ); |
|
122 | + return $this->app->response()->success($result); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -129,34 +129,34 @@ discard block |
||
129 | 129 | * @param boolean $force |
130 | 130 | * @return boolean |
131 | 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( |
|
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 | 135 | 'semaphore_locked', |
136 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
137 | - ) ); |
|
136 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_all()') |
|
137 | + )); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | $this->app->semaphore()->lock(); |
141 | 141 | |
142 | - if ( $user_id ) { |
|
143 | - wp_set_current_user( $user_id ); |
|
142 | + if ($user_id) { |
|
143 | + wp_set_current_user($user_id); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | $result = $this->app->export()->full($force); |
147 | 147 | $this->app->semaphore()->unlock(); |
148 | 148 | |
149 | 149 | // Maybe move option updating out of this class/upgrade message display? |
150 | - if ( is_wp_error( $result ) ) { |
|
150 | + if (is_wp_error($result)) { |
|
151 | 151 | /* @var WP_Error $result */ |
152 | - update_option( '_wogh_export_error', $result->get_error_message() ); |
|
152 | + update_option('_wogh_export_error', $result->get_error_message()); |
|
153 | 153 | |
154 | - return $this->app->response()->error( $result ); |
|
154 | + return $this->app->response()->error($result); |
|
155 | 155 | } else { |
156 | - update_option( '_wogh_export_complete', 'yes' ); |
|
157 | - update_option( '_wogh_fully_exported', 'yes' ); |
|
156 | + update_option('_wogh_export_complete', 'yes'); |
|
157 | + update_option('_wogh_fully_exported', 'yes'); |
|
158 | 158 | |
159 | - return $this->app->response()->success( $result ); |
|
159 | + return $this->app->response()->success($result); |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
@@ -169,28 +169,28 @@ discard block |
||
169 | 169 | * |
170 | 170 | * @return boolean |
171 | 171 | */ |
172 | - public function export_post( $post_id ) { |
|
173 | - if ( wp_is_post_revision( $post_id ) ) { |
|
172 | + public function export_post($post_id) { |
|
173 | + if (wp_is_post_revision($post_id)) { |
|
174 | 174 | return; |
175 | 175 | } |
176 | 176 | |
177 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
178 | - return $this->app->response()->error( new WP_Error( |
|
177 | + if ( ! $this->app->semaphore()->is_open()) { |
|
178 | + return $this->app->response()->error(new WP_Error( |
|
179 | 179 | 'semaphore_locked', |
180 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
181 | - ) ); |
|
180 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_post()') |
|
181 | + )); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | $this->app->semaphore()->lock(); |
185 | - $result = $this->app->export()->update( $post_id ); |
|
185 | + $result = $this->app->export()->update($post_id); |
|
186 | 186 | $this->app->semaphore()->unlock(); |
187 | 187 | |
188 | - if ( is_wp_error( $result ) ) { |
|
188 | + if (is_wp_error($result)) { |
|
189 | 189 | /* @var WP_Error $result */ |
190 | - return $this->app->response()->error( $result ); |
|
190 | + return $this->app->response()->error($result); |
|
191 | 191 | } |
192 | 192 | |
193 | - return $this->app->response()->success( $result ); |
|
193 | + return $this->app->response()->success($result); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -202,28 +202,28 @@ discard block |
||
202 | 202 | * |
203 | 203 | * @return boolean |
204 | 204 | */ |
205 | - public function delete_post( $post_id ) { |
|
206 | - if ( wp_is_post_revision( $post_id ) ) { |
|
205 | + public function delete_post($post_id) { |
|
206 | + if (wp_is_post_revision($post_id)) { |
|
207 | 207 | return; |
208 | 208 | } |
209 | 209 | |
210 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
211 | - return $this->app->response()->error( new WP_Error( |
|
210 | + if ( ! $this->app->semaphore()->is_open()) { |
|
211 | + return $this->app->response()->error(new WP_Error( |
|
212 | 212 | 'semaphore_locked', |
213 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
214 | - ) ); |
|
213 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::delete_post()') |
|
214 | + )); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | $this->app->semaphore()->lock(); |
218 | - $result = $this->app->export()->delete( $post_id ); |
|
218 | + $result = $this->app->export()->delete($post_id); |
|
219 | 219 | $this->app->semaphore()->unlock(); |
220 | 220 | |
221 | - if ( is_wp_error( $result ) ) { |
|
221 | + if (is_wp_error($result)) { |
|
222 | 222 | /* @var WP_Error $result */ |
223 | - return $this->app->response()->error( $result ); |
|
223 | + return $this->app->response()->error($result); |
|
224 | 224 | } |
225 | 225 | |
226 | - return $this->app->response()->success( $result ); |
|
226 | + return $this->app->response()->success($result); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | /** |
@@ -232,8 +232,8 @@ discard block |
||
232 | 232 | * than just returning data. |
233 | 233 | */ |
234 | 234 | protected function set_ajax() { |
235 | - if ( ! defined( 'WOGH_AJAX' ) ) { |
|
236 | - define( 'WOGH_AJAX', true ); |
|
235 | + if ( ! defined('WOGH_AJAX')) { |
|
236 | + define('WOGH_AJAX', true); |
|
237 | 237 | } |
238 | 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 | } |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | * @param Writing_On_GitHub $app Application container. |
30 | 30 | * @param string $raw_data Raw request data. |
31 | 31 | */ |
32 | - public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
32 | + public function __construct(Writing_On_GitHub $app, $raw_data) { |
|
33 | 33 | $this->app = $app; |
34 | - $this->data = json_decode( $raw_data ); |
|
34 | + $this->data = json_decode($raw_data); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
@@ -41,10 +41,10 @@ discard block |
||
41 | 41 | */ |
42 | 42 | public function should_import() { |
43 | 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() ) ) { |
|
44 | + if (strtolower($this->data->repository->full_name) !== strtolower($this->app->api()->fetch()->repository())) { |
|
45 | 45 | return new WP_Error( |
46 | 46 | 'incorrect_repository', |
47 | - sprintf( 'Incorrect repository, %s -> %s .', |
|
47 | + sprintf('Incorrect repository, %s -> %s .', |
|
48 | 48 | $this->data->repository->full_name, |
49 | 49 | $this->app->api()->fetch()->repository() |
50 | 50 | ) |
@@ -52,15 +52,15 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | // The last term in the ref is the payload_branch name. |
55 | - $refs = explode( '/', $this->data->ref ); |
|
56 | - $payload_branch = array_pop( $refs ); |
|
55 | + $refs = explode('/', $this->data->ref); |
|
56 | + $payload_branch = array_pop($refs); |
|
57 | 57 | |
58 | 58 | $branch = $this->app->api()->fetch()->branch(); |
59 | 59 | |
60 | - if ( $branch !== $payload_branch ) { |
|
60 | + if ($branch !== $payload_branch) { |
|
61 | 61 | return new WP_Error( |
62 | 62 | 'incorrect_branch', |
63 | - sprintf( 'Incorrect branch, %s -> %s .', |
|
63 | + sprintf('Incorrect branch, %s -> %s .', |
|
64 | 64 | $payload_branch, |
65 | 65 | $branch |
66 | 66 | ) |
@@ -68,20 +68,20 @@ discard block |
||
68 | 68 | } |
69 | 69 | |
70 | 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' ); |
|
71 | + $tag = apply_filters('wogh_commit_msg_tag', 'wogh'); |
|
72 | 72 | |
73 | - if ( ! $tag ) { |
|
74 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
73 | + if ( ! $tag) { |
|
74 | + throw new Exception(__('Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github')); |
|
75 | 75 | } |
76 | 76 | |
77 | - if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
77 | + if ($tag === substr($this->message(), -1 * strlen($tag))) { |
|
78 | 78 | return new WP_Error( |
79 | 79 | 'skip_import', |
80 | 80 | 'Skip import on auto export post.' |
81 | 81 | ); |
82 | 82 | } |
83 | 83 | |
84 | - if ( ! $this->get_commit_id() ) { |
|
84 | + if ( ! $this->get_commit_id()) { |
|
85 | 85 | return new WP_Error( |
86 | 86 | 'invalid_payload', |
87 | 87 | "[Missing Commit ID] won't be imported." |