Skip to content

Commit 887c78d

Browse files
committed
add force import frome github
1 parent 5026891 commit 887c78d

5 files changed

Lines changed: 43 additions & 28 deletions

File tree

lib/admin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,15 @@ public function trigger_cron() {
256256
if ( 'export' === $_GET['action'] ) {
257257
Writing_On_GitHub::$instance->start_export();
258258
}
259-
260259
if ( 'force_export' === $_GET['action'] ) {
261260
Writing_On_GitHub::$instance->start_export(true);
262261
}
263-
264262
if ( 'import' === $_GET['action'] ) {
265263
Writing_On_GitHub::$instance->start_import();
266264
}
265+
if ( 'force_import' === $_GET['action'] ) {
266+
Writing_On_GitHub::$instance->start_import(true);
267+
}
267268

268269
wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) );
269270
die;

lib/controller.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ public function pull_posts() {
9090

9191
/**
9292
* Imports posts from the current master branch.
93-
*
93+
* @param integer $user_id
94+
* @param boolean $force
9495
* @return boolean
9596
*/
96-
public function import_master( $user_id = 0 ) {
97+
public function import_master( $user_id = 0, $force = false ) {
9798
if ( ! $this->app->semaphore()->is_open() ) {
9899
return $this->app->response()->error( new WP_Error(
99100
'semaphore_locked',
@@ -109,7 +110,7 @@ public function import_master( $user_id = 0 ) {
109110
wp_set_current_user( $user_id );
110111
}
111112

112-
$result = $this->app->import()->master();
113+
$result = $this->app->import()->master( $force );
113114

114115
$this->app->semaphore()->unlock();
115116

lib/import.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ public function payload( Writing_On_GitHub_Payload $payload ) {
5555
/**
5656
* import blob by files
5757
* @param Writing_On_GitHub_File_Info[] $files
58+
* @param boolean $force
5859
*
5960
* @return true|WP_Error
6061
*/
61-
protected function import_files( $files ) {
62+
protected function import_files( $files, $force = false ) {
6263

6364
$error = true;
6465

@@ -82,7 +83,7 @@ protected function import_files( $files ) {
8283
if ( $is_remove ) {
8384
$result = $this->delete_post( $blob );
8485
} else {
85-
$result = $this->import_post( $blob );
86+
$result = $this->import_post( $blob, $force );
8687
}
8788
}
8889

@@ -98,9 +99,10 @@ protected function import_files( $files ) {
9899
/**
99100
* Imports the latest commit on the master branch.
100101
*
102+
* @param boolean $force
101103
* @return string|WP_Error
102104
*/
103-
public function master() {
105+
public function master( $force = false ) {
104106
$result = $this->app->api()->fetch()->tree_recursive();
105107

106108
if ( is_wp_error( $result ) ) {
@@ -109,7 +111,7 @@ public function master() {
109111
}
110112

111113
if ( is_array( $result ) ) {
112-
$result = $this->import_files( $result );
114+
$result = $this->import_files( $result, $force );
113115
}
114116

115117
if ( is_wp_error( $result ) ) {
@@ -189,10 +191,11 @@ protected function delete_post( Writing_On_GitHub_Blob $blob ) {
189191
/**
190192
* Imports a post into wordpress
191193
* @param Writing_On_GitHub_Blob $blob
194+
* @param boolean $force
192195
* @return WP_Error|bool
193196
*/
194-
protected function import_post( Writing_On_GitHub_Blob $blob ) {
195-
$post = $this->blob_to_post( $blob );
197+
protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) {
198+
$post = $this->blob_to_post( $blob, $force );
196199

197200
if ( ! $post instanceof Writing_On_GitHub_Post ) {
198201
return false;
@@ -265,10 +268,11 @@ protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) {
265268
* Imports a single blob content into matching post.
266269
*
267270
* @param Writing_On_GitHub_Blob $blob Blob to transform into a Post.
271+
* @param boolean $force
268272
*
269273
* @return Writing_On_GitHub_Post|false
270274
*/
271-
protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
275+
protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) {
272276
$args = array( 'post_content' => $blob->content_import() );
273277
$meta = $blob->meta();
274278

@@ -304,7 +308,7 @@ protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
304308

305309
$meta['_wogh_sha'] = $blob->sha();
306310

307-
if ( $id ) {
311+
if ( ! $force && $id ) {
308312
$old_sha = get_post_meta( $id, '_wogh_sha', true );
309313
$old_github_path = get_post_meta( $id, '_wogh_github_path', true );
310314

views/options.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@
1717
<td><code><?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>?action=wogh_push_request</code></td>
1818
</tr>
1919
<tr>
20-
<th scope="row"><?php esc_html_e( 'Bulk actions', 'writing-on-github' ); ?></th>
21-
<td>
22-
<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'export' ) ) ); ?>">
23-
<?php esc_html_e( 'Export to GitHub', 'writing-on-github' ); ?>
24-
</a> |
25-
<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_export' ) ) ); ?>">
26-
<?php esc_html_e( 'Force export to GitHub', 'writing-on-github' ); ?>
27-
</a> |
28-
<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'import' ) ) ); ?>">
29-
<?php esc_html_e( 'Import from GitHub', 'writing-on-github' ); ?>
30-
</a>
31-
</td>
20+
<th scope="row"><?php esc_html_e( 'Export', 'writing-on-github' ); ?></th>
21+
<td>
22+
<p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'export' ) ) ); ?>">
23+
<input type="button" class="button button-secondary" value="<?php esc_html_e( 'Export to GitHub', 'writing-on-github' ); ?>" />
24+
</a></p>
25+
<p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_export' ) ) ); ?>">
26+
<input type="button" class="button button-secondary" value="<?php esc_html_e( 'Force export to GitHub', 'writing-on-github' ); ?>" />
27+
</a></p>
28+
</td>
29+
</tr>
30+
<tr>
31+
<th scope="row"><?php esc_html_e( 'Import', 'writing-on-github' ); ?></th>
32+
<td>
33+
<p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'import' ) ) ); ?>">
34+
<input type="button" class="button button-secondary" value="<?php esc_html_e( 'Import from GitHub', 'writing-on-github' ); ?>" />
35+
</a></p>
36+
<p><a href="<?php echo esc_url( add_query_arg( array( 'action' => 'force_import' ) ) ); ?>">
37+
<input type="button" class="button button-secondary" value="<?php esc_html_e( 'Force Import from GitHub', 'writing-on-github' ); ?>" />
38+
</a></p>
39+
</td>
40+
</tr>
3241
</table>
3342
<?php submit_button(); ?>
3443
</form>

writing-on-github.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function boot() {
132132
add_action( 'delete_post', array( $this->controller, 'delete_post' ) );
133133
add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) );
134134
add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 );
135-
add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 1 );
135+
add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 2 );
136136
add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 );
137137

138138
// add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 );
@@ -217,8 +217,8 @@ public function start_export( $force = false ) {
217217
/**
218218
* Sets and kicks off the import cronjob
219219
*/
220-
public function start_import() {
221-
$this->start_cron( 'import' );
220+
public function start_import( $force = false ) {
221+
$this->start_cron( 'import', $force );
222222
}
223223

224224
/**

0 commit comments

Comments
 (0)