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