Passed
Push — ci ( 3ea26a...990482 )
by litefeel
02:30
created
lib/function.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -8,10 +8,10 @@  discard block
 block discarded – undo
8 8
  * @return WP_Error
9 9
  */
10 10
 function wogh_append_error( $error, $error2 ) {
11
-    if ( is_wp_error( $error ) ) {
12
-        $error->add( $error2->get_error_code(), $error2->get_error_message() );
13
-    }
14
-    return $error2;
11
+	if ( is_wp_error( $error ) ) {
12
+		$error->add( $error2->get_error_code(), $error2->get_error_message() );
13
+	}
14
+	return $error2;
15 15
 }
16 16
 
17 17
 /**
@@ -21,9 +21,9 @@  discard block
 block discarded – undo
21 21
  * @return bool
22 22
  */
23 23
 function wogh_equal_front_matter( $post, $blob ) {
24
-    $str1 = $post->front_matter();
25
-    $str2 = $blob->front_matter();
26
-    return trim($str1) === trim($str2);
24
+	$str1 = $post->front_matter();
25
+	$str2 = $blob->front_matter();
26
+	return trim($str1) === trim($str2);
27 27
 }
28 28
 
29 29
 /**
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
  * @return bool
32 32
  */
33 33
 function wogh_is_dont_export_content() {
34
-    return 'yes' === get_option( 'wogh_dont_export_content' );
34
+	return 'yes' === get_option( 'wogh_dont_export_content' );
35 35
 }
36 36
 
37 37
 /**
@@ -41,8 +41,8 @@  discard block
 block discarded – undo
41 41
  * @return string
42 42
  */
43 43
 function wogh_git_sha( $contnet ) {
44
-    // $header = "blob $len\0"
45
-    // sha1($header . $content)
46
-    $len = strlen( $content );
47
-    return sha1( "blob $len\0$content" );
44
+	// $header = "blob $len\0"
45
+	// sha1($header . $content)
46
+	$len = strlen( $content );
47
+	return sha1( "blob $len\0$content" );
48 48
 }
Please login to merge, or discard this patch.
lib/export.php 1 patch
Indentation   +271 added lines, -271 removed lines patch added patch discarded remove patch
@@ -10,275 +10,275 @@
 block discarded – undo
10 10
  */
11 11
 class Writing_On_GitHub_Export {
12 12
 
13
-    /**
14
-     * Application container.
15
-     *
16
-     * @var Writing_On_GitHub
17
-     */
18
-    protected $app;
19
-
20
-    /**
21
-     * Initializes a new export manager.
22
-     *
23
-     * @param Writing_On_GitHub $app Application container.
24
-     */
25
-    public function __construct( Writing_On_GitHub $app ) {
26
-        $this->app = $app;
27
-    }
28
-
29
-    /**
30
-     * Updates all of the current posts in the database on master.
31
-     *
32
-     * @param  bool    $force
33
-     *
34
-     * @return string|WP_Error
35
-     */
36
-    public function full( $force = false ) {
37
-        $posts = $this->app->database()->fetch_all_supported( $force );
38
-
39
-        if ( is_wp_error( $posts ) ) {
40
-            /* @var WP_Error $posts */
41
-            return $posts;
42
-        }
43
-
44
-        $error = '';
45
-
46
-        foreach ( $posts as $post ) {
47
-            $result = $this->update( $post->id() );
48
-            if ( is_wp_error( $result ) ) {
49
-                /* @var WP_Error $result */
50
-                $error = wogh_append_error( $error, $result );
51
-            }
52
-        }
53
-
54
-        if ( is_wp_error( $error ) ) {
55
-            /* @var WP_Error $error */
56
-            return $error;
57
-        }
58
-
59
-        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
60
-    }
61
-
62
-
63
-    /**
64
-     * Check if it exists in github
65
-     * @param  int  $post_id
66
-     * @return boolean
67
-     */
68
-    protected function github_path( $post_id ) {
69
-        $github_path = get_post_meta( $post_id, '_wogh_github_path', true );
70
-
71
-        if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) {
72
-            return $github_path;
73
-        }
74
-
75
-        return false;
76
-    }
77
-
78
-    /**
79
-     * Updates the provided post ID in master.
80
-     *
81
-     * @param int $post_id Post ID to update.
82
-     *
83
-     * @return string|WP_Error
84
-     */
85
-    public function update( $post_id ) {
86
-        $post = $this->app->database()->fetch_by_id( $post_id );
87
-
88
-        if ( is_wp_error( $post ) ) {
89
-            /* @var WP_Error $post */
90
-            return $post;
91
-        }
92
-
93
-        if ( 'trash' === $post->status() ) {
94
-            return $this->delete( $post_id );
95
-        }
96
-
97
-        if ( $old_github_path = $this->github_path( $post->id() ) ) {
98
-            error_log("old_github_path: $old_github_path");
99
-            $post->set_old_github_path($old_github_path);
100
-        }
101
-
102
-        $result = $this->export_post( $post );
103
-
104
-        if ( is_wp_error( $result ) ) {
105
-            /* @var WP_Error $result */
106
-            return $result;
107
-        }
108
-
109
-        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
110
-    }
111
-
112
-    /**
113
-     * Post to blob
114
-     * @param  Writing_On_GitHub_Post $post
115
-     * @return WP_Error|Writing_On_GitHub_Blob
116
-     */
117
-    protected function post_to_blob( Writing_On_GitHub_Post $post ) {
118
-        if ( ! $post->get_blob()
119
-            && $post->old_github_path()
120
-            && wogh_is_dont_export_content() ) {
121
-
122
-
123
-            $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() );
124
-
125
-            if ( is_wp_error( $blob ) ) {
126
-                /** @var WP_Error $blob */
127
-                return $blob;
128
-            }
129
-
130
-            $post->set_blob( $blob );
131
-        }
132
-
133
-        return $post->to_blob();
134
-    }
135
-
136
-    /**
137
-     * Export post to github
138
-     * @param  Writing_On_GitHub_Post $post
139
-     * @return WP_Error|true
140
-     */
141
-    public function export_post( Writing_On_GitHub_Post $post ) {
142
-        // check blob
143
-        $blob = $this->post_to_blob( $post );
144
-        if ( is_wp_error( $blob ) ) {
145
-            /** @var WP_Error $blob */
146
-            return $blob;
147
-        }
148
-
149
-        $result = false;
150
-
151
-        $persist = $this->app->api()->persist();
152
-        $github_path = $post->github_path();
153
-        $old_github_path = $post->old_github_path();
154
-
155
-        if ( $old_github_path && $old_github_path != $github_path ) {
156
-            // rename
157
-            $message = apply_filters(
158
-                'wogh_commit_msg_move_post',
159
-                sprintf(
160
-                    'Move %s to %s via WordPress at %s (%s)',
161
-                    $old_github_path, $github_path,
162
-                    site_url(),
163
-                    get_bloginfo( 'name' )
164
-                )
165
-            ) . $this->get_commit_msg_tag();
166
-
167
-            $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message );
168
-            if ( is_wp_error( $result ) ) {
169
-                return $result;
170
-            }
171
-
172
-            $result = $persist->create_file( $blob, $message );
173
-            if ( is_wp_error( $result ) ) {
174
-                return $result;
175
-            }
176
-        } elseif ( ! $old_github_path ) {
177
-            // create new
178
-            $message = apply_filters(
179
-                'wogh_commit_msg_new_post',
180
-                sprintf(
181
-                    'Create new post %s from WordPress at %s (%s)',
182
-                    $github_path,
183
-                    site_url(),
184
-                    get_bloginfo( 'name' )
185
-                )
186
-            ) . $this->get_commit_msg_tag();
187
-            $result = $persist->create_file( $blob, $message );
188
-            if ( is_wp_error( $result ) ) {
189
-                return $result;
190
-            }
191
-        } elseif ( $old_github_path && $old_github_path == $github_path ) {
192
-            // update
193
-            $sha = wogh_git_sha( $blob->content() );
194
-            if ( $sha === $blob->sha() ) {
195
-                // don't export when has not changed
196
-                return true;
197
-            }
198
-            $message = apply_filters(
199
-                'wogh_commit_msg_update_post',
200
-                sprintf(
201
-                    'Update post %s from WordPress at %s (%s)',
202
-                    $github_path,
203
-                    site_url(),
204
-                    get_bloginfo( 'name' )
205
-                )
206
-            ) . $this->get_commit_msg_tag();
207
-            $result = $persist->update_file( $blob, $message );
208
-            if ( is_wp_error( $result ) ) {
209
-                return $result;
210
-            }
211
-        }
212
-
213
-        $sha = $result->content->sha;
214
-        $post->set_sha( $sha );
215
-        $post->set_old_github_path( $github_path );
216
-
217
-        return true;
218
-    }
219
-
220
-    /**
221
-     * Deletes a provided post ID from master.
222
-     *
223
-     * @param int $post_id Post ID to delete.
224
-     *
225
-     * @return string|WP_Error
226
-     */
227
-    public function delete( $post_id ) {
228
-        $post = $this->app->database()->fetch_by_id( $post_id );
229
-
230
-        if ( is_wp_error( $post ) ) {
231
-            /* @var WP_Error $post */
232
-            return $post;
233
-        }
234
-
235
-        $github_path = get_post_meta( $post_id, '_wogh_github_path', true );
236
-
237
-        $message = apply_filters(
238
-            'wogh_commit_msg_delete',
239
-            sprintf(
240
-                'Deleting %s via WordPress at %s (%s)',
241
-                $github_path,
242
-                site_url(),
243
-                get_bloginfo( 'name' )
244
-            ),
245
-            $post
246
-        ) . $this->get_commit_msg_tag();
247
-
248
-        $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
249
-
250
-        if ( is_wp_error( $result ) ) {
251
-            /* @var WP_Error $result */
252
-            return $result;
253
-        }
254
-
255
-        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
256
-    }
257
-
258
-
259
-    /**
260
-     * Saves the export user to the database.
261
-     *
262
-     * @param int $user_id User ID to export with.
263
-     *
264
-     * @return bool
265
-     */
266
-    public function set_user( $user_id ) {
267
-        return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
268
-    }
269
-
270
-    /**
271
-     * Gets the commit message tag.
272
-     *
273
-     * @return string
274
-     */
275
-    protected function get_commit_msg_tag() {
276
-        $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
277
-
278
-        if ( ! $tag ) {
279
-            throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
280
-        }
281
-
282
-        return ' - ' . $tag;
283
-    }
13
+	/**
14
+	 * Application container.
15
+	 *
16
+	 * @var Writing_On_GitHub
17
+	 */
18
+	protected $app;
19
+
20
+	/**
21
+	 * Initializes a new export manager.
22
+	 *
23
+	 * @param Writing_On_GitHub $app Application container.
24
+	 */
25
+	public function __construct( Writing_On_GitHub $app ) {
26
+		$this->app = $app;
27
+	}
28
+
29
+	/**
30
+	 * Updates all of the current posts in the database on master.
31
+	 *
32
+	 * @param  bool    $force
33
+	 *
34
+	 * @return string|WP_Error
35
+	 */
36
+	public function full( $force = false ) {
37
+		$posts = $this->app->database()->fetch_all_supported( $force );
38
+
39
+		if ( is_wp_error( $posts ) ) {
40
+			/* @var WP_Error $posts */
41
+			return $posts;
42
+		}
43
+
44
+		$error = '';
45
+
46
+		foreach ( $posts as $post ) {
47
+			$result = $this->update( $post->id() );
48
+			if ( is_wp_error( $result ) ) {
49
+				/* @var WP_Error $result */
50
+				$error = wogh_append_error( $error, $result );
51
+			}
52
+		}
53
+
54
+		if ( is_wp_error( $error ) ) {
55
+			/* @var WP_Error $error */
56
+			return $error;
57
+		}
58
+
59
+		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
60
+	}
61
+
62
+
63
+	/**
64
+	 * Check if it exists in github
65
+	 * @param  int  $post_id
66
+	 * @return boolean
67
+	 */
68
+	protected function github_path( $post_id ) {
69
+		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
70
+
71
+		if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) {
72
+			return $github_path;
73
+		}
74
+
75
+		return false;
76
+	}
77
+
78
+	/**
79
+	 * Updates the provided post ID in master.
80
+	 *
81
+	 * @param int $post_id Post ID to update.
82
+	 *
83
+	 * @return string|WP_Error
84
+	 */
85
+	public function update( $post_id ) {
86
+		$post = $this->app->database()->fetch_by_id( $post_id );
87
+
88
+		if ( is_wp_error( $post ) ) {
89
+			/* @var WP_Error $post */
90
+			return $post;
91
+		}
92
+
93
+		if ( 'trash' === $post->status() ) {
94
+			return $this->delete( $post_id );
95
+		}
96
+
97
+		if ( $old_github_path = $this->github_path( $post->id() ) ) {
98
+			error_log("old_github_path: $old_github_path");
99
+			$post->set_old_github_path($old_github_path);
100
+		}
101
+
102
+		$result = $this->export_post( $post );
103
+
104
+		if ( is_wp_error( $result ) ) {
105
+			/* @var WP_Error $result */
106
+			return $result;
107
+		}
108
+
109
+		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
110
+	}
111
+
112
+	/**
113
+	 * Post to blob
114
+	 * @param  Writing_On_GitHub_Post $post
115
+	 * @return WP_Error|Writing_On_GitHub_Blob
116
+	 */
117
+	protected function post_to_blob( Writing_On_GitHub_Post $post ) {
118
+		if ( ! $post->get_blob()
119
+			&& $post->old_github_path()
120
+			&& wogh_is_dont_export_content() ) {
121
+
122
+
123
+			$blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() );
124
+
125
+			if ( is_wp_error( $blob ) ) {
126
+				/** @var WP_Error $blob */
127
+				return $blob;
128
+			}
129
+
130
+			$post->set_blob( $blob );
131
+		}
132
+
133
+		return $post->to_blob();
134
+	}
135
+
136
+	/**
137
+	 * Export post to github
138
+	 * @param  Writing_On_GitHub_Post $post
139
+	 * @return WP_Error|true
140
+	 */
141
+	public function export_post( Writing_On_GitHub_Post $post ) {
142
+		// check blob
143
+		$blob = $this->post_to_blob( $post );
144
+		if ( is_wp_error( $blob ) ) {
145
+			/** @var WP_Error $blob */
146
+			return $blob;
147
+		}
148
+
149
+		$result = false;
150
+
151
+		$persist = $this->app->api()->persist();
152
+		$github_path = $post->github_path();
153
+		$old_github_path = $post->old_github_path();
154
+
155
+		if ( $old_github_path && $old_github_path != $github_path ) {
156
+			// rename
157
+			$message = apply_filters(
158
+				'wogh_commit_msg_move_post',
159
+				sprintf(
160
+					'Move %s to %s via WordPress at %s (%s)',
161
+					$old_github_path, $github_path,
162
+					site_url(),
163
+					get_bloginfo( 'name' )
164
+				)
165
+			) . $this->get_commit_msg_tag();
166
+
167
+			$result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message );
168
+			if ( is_wp_error( $result ) ) {
169
+				return $result;
170
+			}
171
+
172
+			$result = $persist->create_file( $blob, $message );
173
+			if ( is_wp_error( $result ) ) {
174
+				return $result;
175
+			}
176
+		} elseif ( ! $old_github_path ) {
177
+			// create new
178
+			$message = apply_filters(
179
+				'wogh_commit_msg_new_post',
180
+				sprintf(
181
+					'Create new post %s from WordPress at %s (%s)',
182
+					$github_path,
183
+					site_url(),
184
+					get_bloginfo( 'name' )
185
+				)
186
+			) . $this->get_commit_msg_tag();
187
+			$result = $persist->create_file( $blob, $message );
188
+			if ( is_wp_error( $result ) ) {
189
+				return $result;
190
+			}
191
+		} elseif ( $old_github_path && $old_github_path == $github_path ) {
192
+			// update
193
+			$sha = wogh_git_sha( $blob->content() );
194
+			if ( $sha === $blob->sha() ) {
195
+				// don't export when has not changed
196
+				return true;
197
+			}
198
+			$message = apply_filters(
199
+				'wogh_commit_msg_update_post',
200
+				sprintf(
201
+					'Update post %s from WordPress at %s (%s)',
202
+					$github_path,
203
+					site_url(),
204
+					get_bloginfo( 'name' )
205
+				)
206
+			) . $this->get_commit_msg_tag();
207
+			$result = $persist->update_file( $blob, $message );
208
+			if ( is_wp_error( $result ) ) {
209
+				return $result;
210
+			}
211
+		}
212
+
213
+		$sha = $result->content->sha;
214
+		$post->set_sha( $sha );
215
+		$post->set_old_github_path( $github_path );
216
+
217
+		return true;
218
+	}
219
+
220
+	/**
221
+	 * Deletes a provided post ID from master.
222
+	 *
223
+	 * @param int $post_id Post ID to delete.
224
+	 *
225
+	 * @return string|WP_Error
226
+	 */
227
+	public function delete( $post_id ) {
228
+		$post = $this->app->database()->fetch_by_id( $post_id );
229
+
230
+		if ( is_wp_error( $post ) ) {
231
+			/* @var WP_Error $post */
232
+			return $post;
233
+		}
234
+
235
+		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
236
+
237
+		$message = apply_filters(
238
+			'wogh_commit_msg_delete',
239
+			sprintf(
240
+				'Deleting %s via WordPress at %s (%s)',
241
+				$github_path,
242
+				site_url(),
243
+				get_bloginfo( 'name' )
244
+			),
245
+			$post
246
+		) . $this->get_commit_msg_tag();
247
+
248
+		$result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
249
+
250
+		if ( is_wp_error( $result ) ) {
251
+			/* @var WP_Error $result */
252
+			return $result;
253
+		}
254
+
255
+		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
256
+	}
257
+
258
+
259
+	/**
260
+	 * Saves the export user to the database.
261
+	 *
262
+	 * @param int $user_id User ID to export with.
263
+	 *
264
+	 * @return bool
265
+	 */
266
+	public function set_user( $user_id ) {
267
+		return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
268
+	}
269
+
270
+	/**
271
+	 * Gets the commit message tag.
272
+	 *
273
+	 * @return string
274
+	 */
275
+	protected function get_commit_msg_tag() {
276
+		$tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
277
+
278
+		if ( ! $tag ) {
279
+			throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
280
+		}
281
+
282
+		return ' - ' . $tag;
283
+	}
284 284
 }
Please login to merge, or discard this patch.