1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Controller object manages tree retrieval, manipulation and publishing |
4
|
|
|
* @package WordPress_GitHub_Sync |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class WordPress_GitHub_Sync_Controller |
9
|
|
|
*/ |
10
|
|
|
class WordPress_GitHub_Sync_Controller { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Application container. |
14
|
|
|
* |
15
|
|
|
* @var WordPress_GitHub_Sync |
16
|
|
|
*/ |
17
|
|
|
public $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instantiates a new Controller object |
21
|
|
|
* |
22
|
|
|
* @param WordPress_GitHub_Sync $app Applicatio container. |
23
|
|
|
*/ |
24
|
17 |
|
public function __construct( WordPress_GitHub_Sync $app ) { |
25
|
17 |
|
$this->app = $app; |
26
|
17 |
|
} |
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
|
5 |
|
public function pull_posts() { |
36
|
5 |
|
$this->set_ajax(); |
37
|
5 |
|
if ( ! $this->app->semaphore()->is_open() ) { |
38
|
1 |
|
return $this->app->response()->error( new WP_Error( |
39
|
1 |
|
'semaphore_locked', |
40
|
1 |
|
sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'wp-github-sync' ), 'Controller::pull_posts()' ) |
41
|
1 |
|
) ); |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
if ( ! $this->app->request()->is_secret_valid() ) { |
45
|
1 |
|
return $this->app->response()->error( new WP_Error( |
46
|
1 |
|
'invalid_headers', |
47
|
1 |
|
__( 'Failed to validate secret.', 'wp-github-sync' ) |
48
|
1 |
|
) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$payload = $this->app->request()->payload(); |
52
|
|
|
|
53
|
3 |
View Code Duplication |
if ( $payload->has_error() ) { |
|
|
|
|
54
|
|
|
return $this->app->response()->error( new WP_Error( |
55
|
|
|
'invalid_payload', |
56
|
|
|
sprintf( |
57
|
|
|
__( "%s won't be imported. Error: %s", 'wp-github-sync' ), |
58
|
|
|
strtolower( $payload->get_commit_id() ) ? : '[Missing Commit ID]', |
59
|
|
|
$payload->get_error() |
60
|
|
|
) |
61
|
|
|
) ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
if ( ! $payload->should_import() ) { |
|
|
|
|
65
|
|
|
return $this->app->response()->error( new WP_Error( |
66
|
|
|
'invalid_payload', |
67
|
|
|
sprintf( |
68
|
|
|
__( "%s won't be imported.", 'wp-github-sync' ), |
69
|
|
|
strtolower( $payload->get_commit_id() ) ? : '[Missing Commit ID]' |
70
|
|
|
) |
71
|
|
|
) ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->app->semaphore()->lock(); |
75
|
|
|
remove_action( 'save_post', array( $this, 'export_post' ) ); |
76
|
|
|
remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
77
|
|
|
|
78
|
|
|
$result = $this->app->import()->payload( $payload ); |
79
|
|
|
|
80
|
|
|
$this->app->semaphore()->unlock(); |
81
|
|
|
|
82
|
|
|
if ( is_wp_error( $result ) ) { |
83
|
|
|
return $this->app->response()->error( $result ); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->app->response()->success( $result ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Imports posts from the current master branch. |
91
|
|
|
* |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
3 |
|
public function import_master() { |
95
|
3 |
|
if ( ! $this->app->semaphore()->is_open() ) { |
96
|
1 |
|
return $this->app->response()->error( new WP_Error( |
97
|
1 |
|
'semaphore_locked', |
98
|
1 |
|
sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'wp-github-sync' ), 'Controller::import_master()' ) |
99
|
1 |
|
) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$this->app->semaphore()->lock(); |
103
|
2 |
|
remove_action( 'save_post', array( $this, 'export_post' ) ); |
104
|
2 |
|
remove_action( 'save_post', array( $this, 'delete_post' ) ); |
105
|
|
|
|
106
|
2 |
|
$result = $this->app->import()->master(); |
107
|
|
|
|
108
|
2 |
|
$this->app->semaphore()->unlock(); |
109
|
|
|
|
110
|
2 |
|
if ( is_wp_error( $result ) ) { |
111
|
1 |
|
update_option( '_wpghs_import_error', $result->get_error_message() ); |
112
|
|
|
|
113
|
1 |
|
return $this->app->response()->error( $result ); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
update_option( '_wpghs_import_complete', 'yes' ); |
117
|
|
|
|
118
|
1 |
|
return $this->app->response()->success( $result ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Export all the posts in the database to GitHub. |
123
|
|
|
* |
124
|
|
|
* @return boolean |
125
|
|
|
*/ |
126
|
3 |
|
public function export_all() { |
127
|
3 |
|
if ( ! $this->app->semaphore()->is_open() ) { |
128
|
1 |
|
return $this->app->response()->error( new WP_Error( |
129
|
1 |
|
'semaphore_locked', |
130
|
1 |
|
sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'wp-github-sync' ), 'Controller::export_all()' ) |
131
|
1 |
|
) ); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
$this->app->semaphore()->lock(); |
135
|
2 |
|
$result = $this->app->export()->full(); |
136
|
2 |
|
$this->app->semaphore()->unlock(); |
137
|
|
|
|
138
|
|
|
// Maybe move option updating out of this class/upgrade message display? |
139
|
2 |
|
if ( is_wp_error( $result ) ) { |
140
|
1 |
|
update_option( '_wpghs_export_error', $result->get_error_message() ); |
141
|
|
|
|
142
|
1 |
|
return $this->app->response()->error( $result ); |
143
|
|
|
} else { |
144
|
1 |
|
update_option( '_wpghs_export_complete', 'yes' ); |
145
|
1 |
|
update_option( '_wpghs_fully_exported', 'yes' ); |
146
|
|
|
|
147
|
1 |
|
return $this->app->response()->success( $result ); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Exports a single post to GitHub by ID. |
153
|
|
|
* |
154
|
|
|
* Called on the save_post hook. |
155
|
|
|
* |
156
|
|
|
* @param int $post_id Post ID. |
157
|
|
|
* |
158
|
|
|
* @return boolean |
159
|
|
|
*/ |
160
|
3 |
View Code Duplication |
public function export_post( $post_id ) { |
|
|
|
|
161
|
3 |
|
if ( ! $this->app->semaphore()->is_open() ) { |
162
|
1 |
|
return $this->app->response()->error( new WP_Error( |
163
|
1 |
|
'semaphore_locked', |
164
|
1 |
|
sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'wp-github-sync' ), 'Controller::export_post()' ) |
165
|
1 |
|
) ); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
$this->app->semaphore()->lock(); |
169
|
2 |
|
$result = $this->app->export()->update( $post_id ); |
170
|
2 |
|
$this->app->semaphore()->unlock(); |
171
|
|
|
|
172
|
2 |
|
if ( is_wp_error( $result ) ) { |
173
|
1 |
|
return $this->app->response()->error( $result ); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
return $this->app->response()->success( $result ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Removes the post from the tree. |
181
|
|
|
* |
182
|
|
|
* Called the delete_post hook. |
183
|
|
|
* |
184
|
|
|
* @param int $post_id Post ID. |
185
|
|
|
* |
186
|
|
|
* @return boolean |
187
|
|
|
*/ |
188
|
3 |
View Code Duplication |
public function delete_post( $post_id ) { |
|
|
|
|
189
|
3 |
|
if ( ! $this->app->semaphore()->is_open() ) { |
190
|
1 |
|
return $this->app->response()->error( new WP_Error( |
191
|
1 |
|
'semaphore_locked', |
192
|
1 |
|
sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'wp-github-sync' ), 'Controller::delete_post()' ) |
193
|
1 |
|
) ); |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
$this->app->semaphore()->lock(); |
197
|
2 |
|
$result = $this->app->export()->delete( $post_id ); |
198
|
2 |
|
$this->app->semaphore()->unlock(); |
199
|
|
|
|
200
|
2 |
|
if ( is_wp_error( $result ) ) { |
201
|
1 |
|
return $this->app->response()->error( $result ); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
return $this->app->response()->success( $result ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Indicates we're running our own AJAX hook |
209
|
|
|
* and thus should respond with JSON, rather |
210
|
|
|
* than just returning data. |
211
|
|
|
*/ |
212
|
5 |
|
protected function set_ajax() { |
213
|
5 |
|
if ( ! defined( 'WPGHS_AJAX' ) ) { |
214
|
1 |
|
define( 'WPGHS_AJAX', true ); |
215
|
1 |
|
} |
216
|
5 |
|
} |
217
|
|
|
} |
218
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.