Passed
Push — ci ( 410349...9342ca )
by litefeel
02:28
created
lib/export.php 1 patch
Indentation   +266 added lines, -266 removed lines patch added patch discarded remove patch
@@ -10,270 +10,270 @@
 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|string
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
-            $message = apply_filters(
194
-                'wogh_commit_msg_update_post',
195
-                sprintf(
196
-                    'Update post %s from WordPress at %s (%s)',
197
-                    $github_path,
198
-                    site_url(),
199
-                    get_bloginfo( 'name' )
200
-                )
201
-            ) . $this->get_commit_msg_tag();
202
-            $result = $persist->update_file( $blob, $message );
203
-            if ( is_wp_error( $result ) ) {
204
-                return $result;
205
-            }
206
-        }
207
-
208
-        $sha = $result->content->sha;
209
-        $post->set_sha($sha);
210
-        $post->set_old_github_path($github_path);
211
-
212
-        return true;
213
-    }
214
-
215
-    /**
216
-     * Deletes a provided post ID from master.
217
-     *
218
-     * @param int $post_id Post ID to delete.
219
-     *
220
-     * @return string|WP_Error
221
-     */
222
-    public function delete( $post_id ) {
223
-        $post = $this->app->database()->fetch_by_id( $post_id );
224
-
225
-        if ( is_wp_error( $post ) ) {
226
-            /* @var WP_Error $post */
227
-            return $post;
228
-        }
229
-
230
-        $github_path = get_post_meta( $post_id, '_wogh_github_path', true );
231
-
232
-        $message = apply_filters(
233
-            'wogh_commit_msg_delete',
234
-            sprintf(
235
-                'Deleting %s via WordPress at %s (%s)',
236
-                $github_path,
237
-                site_url(),
238
-                get_bloginfo( 'name' )
239
-            ),
240
-            $post
241
-        ) . $this->get_commit_msg_tag();
242
-
243
-        $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
244
-
245
-        if ( is_wp_error( $result ) ) {
246
-            /* @var WP_Error $result */
247
-            return $result;
248
-        }
249
-
250
-        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
251
-    }
252
-
253
-
254
-    /**
255
-     * Saves the export user to the database.
256
-     *
257
-     * @param int $user_id User ID to export with.
258
-     *
259
-     * @return bool
260
-     */
261
-    public function set_user( $user_id ) {
262
-        return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
263
-    }
264
-
265
-    /**
266
-     * Gets the commit message tag.
267
-     *
268
-     * @return string
269
-     */
270
-    protected function get_commit_msg_tag() {
271
-        $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
272
-
273
-        if ( ! $tag ) {
274
-            throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
275
-        }
276
-
277
-        return ' - ' . $tag;
278
-    }
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|string
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
+			$message = apply_filters(
194
+				'wogh_commit_msg_update_post',
195
+				sprintf(
196
+					'Update post %s from WordPress at %s (%s)',
197
+					$github_path,
198
+					site_url(),
199
+					get_bloginfo( 'name' )
200
+				)
201
+			) . $this->get_commit_msg_tag();
202
+			$result = $persist->update_file( $blob, $message );
203
+			if ( is_wp_error( $result ) ) {
204
+				return $result;
205
+			}
206
+		}
207
+
208
+		$sha = $result->content->sha;
209
+		$post->set_sha($sha);
210
+		$post->set_old_github_path($github_path);
211
+
212
+		return true;
213
+	}
214
+
215
+	/**
216
+	 * Deletes a provided post ID from master.
217
+	 *
218
+	 * @param int $post_id Post ID to delete.
219
+	 *
220
+	 * @return string|WP_Error
221
+	 */
222
+	public function delete( $post_id ) {
223
+		$post = $this->app->database()->fetch_by_id( $post_id );
224
+
225
+		if ( is_wp_error( $post ) ) {
226
+			/* @var WP_Error $post */
227
+			return $post;
228
+		}
229
+
230
+		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
231
+
232
+		$message = apply_filters(
233
+			'wogh_commit_msg_delete',
234
+			sprintf(
235
+				'Deleting %s via WordPress at %s (%s)',
236
+				$github_path,
237
+				site_url(),
238
+				get_bloginfo( 'name' )
239
+			),
240
+			$post
241
+		) . $this->get_commit_msg_tag();
242
+
243
+		$result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
244
+
245
+		if ( is_wp_error( $result ) ) {
246
+			/* @var WP_Error $result */
247
+			return $result;
248
+		}
249
+
250
+		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
251
+	}
252
+
253
+
254
+	/**
255
+	 * Saves the export user to the database.
256
+	 *
257
+	 * @param int $user_id User ID to export with.
258
+	 *
259
+	 * @return bool
260
+	 */
261
+	public function set_user( $user_id ) {
262
+		return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
263
+	}
264
+
265
+	/**
266
+	 * Gets the commit message tag.
267
+	 *
268
+	 * @return string
269
+	 */
270
+	protected function get_commit_msg_tag() {
271
+		$tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
272
+
273
+		if ( ! $tag ) {
274
+			throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
275
+		}
276
+
277
+		return ' - ' . $tag;
278
+	}
279 279
 }
Please login to merge, or discard this patch.
lib/import.php 1 patch
Indentation   +313 added lines, -313 removed lines patch added patch discarded remove patch
@@ -10,317 +10,317 @@
 block discarded – undo
10 10
  */
11 11
 class Writing_On_GitHub_Import {
12 12
 
13
-    /**
14
-     * Application container.
15
-     *
16
-     * @var Writing_On_GitHub
17
-     */
18
-    protected $app;
19
-
20
-    /**
21
-     * Initializes a new import 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
-     * Imports a payload.
31
-     * @param  Writing_On_GitHub_Payload $payload
32
-     *
33
-     * @return string|WP_Error
34
-     */
35
-    public function payload( Writing_On_GitHub_Payload $payload ) {
36
-
37
-        $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() );
38
-
39
-        if ( is_wp_error( $result ) ) {
40
-            /* @var WP_Error $result */
41
-            return $result;
42
-        }
43
-
44
-        if ( is_array( $result ) ) {
45
-            $result = $this->import_files( $result );
46
-        }
47
-
48
-        if ( is_wp_error( $result ) ) {
49
-            return $files;
50
-        }
51
-
52
-        return __( 'Payload processed', 'writing-on-github' );
53
-    }
54
-
55
-    /**
56
-     * import blob by files
57
-     * @param  Writing_On_GitHub_File_Info[] $files
58
-     *
59
-     * @return true|WP_Error
60
-     */
61
-    protected function import_files( $files ) {
62
-
63
-        $error = true;
64
-
65
-        foreach ( $files as $file ) {
66
-            if ( ! $this->importable_file( $file ) ) {
67
-                continue;
68
-            }
69
-
70
-            $blob = $this->app->api()->fetch()->blob( $file );
71
-            // network error ?
72
-            if ( ! $blob instanceof Writing_On_GitHub_Blob ) {
73
-                continue;
74
-            }
75
-
76
-            $is_remove = 'removed' == $file->status;
77
-
78
-            $result = false;
79
-            if ( $this->importable_raw_file( $blob ) ) {
80
-                $result = $this->import_raw_file( $blob, $is_remove );
81
-            } elseif ( $this->importable_post( $blob ) ) {
82
-                if ( $is_remove ) {
83
-                    $result = $this->delete_post( $blob );
84
-                } else {
85
-                    $result = $this->import_post( $blob );
86
-                }
87
-            }
88
-
89
-            if ( is_wp_error( $result ) ) {
90
-                /* @var WP_Error $result */
91
-                $error = wogh_append_error( $error, $result );
92
-            }
93
-        }
94
-
95
-        return $error;
96
-    }
97
-
98
-    /**
99
-     * Imports the latest commit on the master branch.
100
-     *
101
-     * @return string|WP_Error
102
-     */
103
-    public function master() {
104
-        $result = $this->app->api()->fetch()->tree_recursive();
105
-
106
-        if ( is_wp_error( $result ) ) {
107
-            /* @var WP_Error $result */
108
-            return $result;
109
-        }
110
-
111
-        if ( is_array( $result ) ) {
112
-            $result = $this->import_files( $result );
113
-        }
114
-
115
-        if ( is_wp_error( $result ) ) {
116
-            /* @var WP_Error $result */
117
-            return $result;
118
-        }
119
-
120
-        return __( 'Payload processed', 'writing-on-github' );
121
-    }
122
-
123
-    /**
124
-     * Checks whether the provided blob should be imported.
125
-     *
126
-     * @param Writing_On_GitHub_File_Info $file
127
-     *
128
-     * @return bool
129
-     */
130
-    protected function importable_file( Writing_On_GitHub_File_Info $file ) {
131
-
132
-        $path = $file->path;
133
-
134
-        // only _pages, _posts and images
135
-        $prefixs = array( '_pages/', '_posts/', 'images/');
136
-        foreach ($prefixs as $prefix) {
137
-            if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) {
138
-                return true;
139
-            }
140
-        }
141
-        return false;
142
-    }
143
-
144
-    /**
145
-     * Checks whether the provided blob should be imported.
146
-     *
147
-     * @param Writing_On_GitHub_Blob $blob Blob to validate.
148
-     *
149
-     * @return bool
150
-     */
151
-    protected function importable_post( Writing_On_GitHub_Blob $blob ) {
152
-        // global $wpdb;
153
-
154
-        // // Skip the repo's readme.
155
-        // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) {
156
-        //  return false;
157
-        // }
158
-
159
-        // // If the blob sha already matches a post, then move on.
160
-        // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) {
161
-        //  return false;
162
-        // }
163
-
164
-        if ( ! $blob->has_frontmatter() ) {
165
-            return false;
166
-        }
167
-
168
-        return true;
169
-    }
170
-
171
-    /**
172
-     * Delete post
173
-     * @param  Writing_On_GitHub_Blob $blob
174
-     * @return WP_Error|bool
175
-     */
176
-    protected function delete_post( Writing_On_GitHub_Blob $blob ) {
177
-        $id = $blob->id();
178
-        if ( empty( $id ) ) {
179
-            return false;
180
-        }
181
-        $result = $this->app->database()->delete_post( $id );
182
-        if ( is_wp_error( $result ) ) {
183
-            /* @var WP_Error $result */
184
-            return $result;
185
-        }
186
-        return true;
187
-    }
188
-
189
-    /**
190
-     * Imports a post into wordpress
191
-     * @param  Writing_On_GitHub_Blob $blob
192
-     * @return WP_Error|bool
193
-     */
194
-    protected function import_post( Writing_On_GitHub_Blob $blob ) {
195
-        $post = $this->blob_to_post( $blob );
196
-
197
-        if ( ! $post instanceof Writing_On_GitHub_Post ) {
198
-            return false;
199
-        }
200
-
201
-        $result = $this->app->database()->save_post( $post );
202
-        if ( is_wp_error( $result ) ) {
203
-            /** @var WP_Error $result */
204
-            return $result;
205
-        }
206
-
207
-        if ( $post->is_new() ||
208
-                ! wogh_equal_front_matter( $post, $blob ) ) {
209
-
210
-            $result = $this->app->export()->export_post( $post );
211
-
212
-            if ( is_wp_error( $result ) ) {
213
-                /** @var WP_Error $result */
214
-                return $result;
215
-            }
216
-        }
217
-
218
-        clean_post_cache( $post->id() );
219
-
220
-        return true;
221
-    }
222
-
223
-    /**
224
-     * import raw file
225
-     * @param  Writing_On_GitHub_Blob $blob
226
-     * @return bool
227
-     */
228
-    protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) {
229
-        if ( $blob->has_frontmatter() ) {
230
-            return false;
231
-        }
232
-
233
-        // only images
234
-        if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) {
235
-            return false;
236
-        }
237
-
238
-        return true;
239
-    }
240
-
241
-    /**
242
-     * Imports a raw file content into file system.
243
-     * @param  Writing_On_GitHub_Blob $blob
244
-     * @param  bool                   $is_remove
245
-     */
246
-    protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) {
247
-        $arr = wp_upload_dir();
248
-        $path = $arr['basedir'] . '/writing-on-github/' . $blob->path();
249
-        if ( $is_remove ) {
250
-            if ( file_exists($path) ) {
251
-                unlink($path);
252
-            }
253
-        } else {
254
-            $dirname = dirname($path);
255
-            if ( ! file_exists($dirname) ) {
256
-                wp_mkdir_p($dirname);
257
-            }
258
-
259
-            file_put_contents($path, $blob->content());
260
-        }
261
-        return true;
262
-    }
263
-
264
-    /**
265
-     * Imports a single blob content into matching post.
266
-     *
267
-     * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post.
268
-     *
269
-     * @return Writing_On_GitHub_Post|false
270
-     */
271
-    protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
272
-        $args = array( 'post_content' => $blob->content_import() );
273
-        $meta = $blob->meta();
274
-
275
-        $id = false;
276
-
277
-        if ( ! empty( $meta ) ) {
278
-            if ( array_key_exists( 'layout', $meta ) ) {
279
-                $args['post_type'] = $meta['layout'];
280
-                unset( $meta['layout'] );
281
-            }
282
-
283
-            if ( array_key_exists( 'published', $meta ) ) {
284
-                $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
285
-                unset( $meta['published'] );
286
-            }
287
-
288
-            if ( array_key_exists( 'post_title', $meta ) ) {
289
-                $args['post_title'] = $meta['post_title'];
290
-                unset( $meta['post_title'] );
291
-            }
292
-
293
-            if ( array_key_exists( 'post_name', $meta ) ) {
294
-                $args['post_name'] = $meta['post_name'];
295
-                unset( $meta['post_name'] );
296
-            }
297
-
298
-            if ( array_key_exists( 'ID', $meta ) ) {
299
-                $id = $args['ID'] = $meta['ID'];
300
-                $blob->set_id($id);
301
-                unset( $meta['ID'] );
302
-            }
303
-        }
304
-
305
-        $meta['_wogh_sha'] = $blob->sha();
306
-
307
-        if ( $id ) {
308
-            $old_sha = get_post_meta( $id, '_wogh_sha', true );
309
-            $old_github_path = get_post_meta( $id, '_wogh_github_path', true );
310
-
311
-            // dont save post when has same sha
312
-            if ( $old_sha  && $old_sha == $meta['_wogh_sha'] &&
313
-                 $old_github_path && $old_github_path == $blob->path() ) {
314
-                return false;
315
-            }
316
-        }
317
-
318
-        $post = new Writing_On_GitHub_Post( $args, $this->app->api() );
319
-        $post->set_old_github_path( $blob->path() );
320
-        $post->set_meta( $meta );
321
-        $post->set_blob( $blob );
322
-        $blob->set_id( $post->id() );
323
-
324
-        return $post;
325
-    }
13
+	/**
14
+	 * Application container.
15
+	 *
16
+	 * @var Writing_On_GitHub
17
+	 */
18
+	protected $app;
19
+
20
+	/**
21
+	 * Initializes a new import 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
+	 * Imports a payload.
31
+	 * @param  Writing_On_GitHub_Payload $payload
32
+	 *
33
+	 * @return string|WP_Error
34
+	 */
35
+	public function payload( Writing_On_GitHub_Payload $payload ) {
36
+
37
+		$result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() );
38
+
39
+		if ( is_wp_error( $result ) ) {
40
+			/* @var WP_Error $result */
41
+			return $result;
42
+		}
43
+
44
+		if ( is_array( $result ) ) {
45
+			$result = $this->import_files( $result );
46
+		}
47
+
48
+		if ( is_wp_error( $result ) ) {
49
+			return $files;
50
+		}
51
+
52
+		return __( 'Payload processed', 'writing-on-github' );
53
+	}
54
+
55
+	/**
56
+	 * import blob by files
57
+	 * @param  Writing_On_GitHub_File_Info[] $files
58
+	 *
59
+	 * @return true|WP_Error
60
+	 */
61
+	protected function import_files( $files ) {
62
+
63
+		$error = true;
64
+
65
+		foreach ( $files as $file ) {
66
+			if ( ! $this->importable_file( $file ) ) {
67
+				continue;
68
+			}
69
+
70
+			$blob = $this->app->api()->fetch()->blob( $file );
71
+			// network error ?
72
+			if ( ! $blob instanceof Writing_On_GitHub_Blob ) {
73
+				continue;
74
+			}
75
+
76
+			$is_remove = 'removed' == $file->status;
77
+
78
+			$result = false;
79
+			if ( $this->importable_raw_file( $blob ) ) {
80
+				$result = $this->import_raw_file( $blob, $is_remove );
81
+			} elseif ( $this->importable_post( $blob ) ) {
82
+				if ( $is_remove ) {
83
+					$result = $this->delete_post( $blob );
84
+				} else {
85
+					$result = $this->import_post( $blob );
86
+				}
87
+			}
88
+
89
+			if ( is_wp_error( $result ) ) {
90
+				/* @var WP_Error $result */
91
+				$error = wogh_append_error( $error, $result );
92
+			}
93
+		}
94
+
95
+		return $error;
96
+	}
97
+
98
+	/**
99
+	 * Imports the latest commit on the master branch.
100
+	 *
101
+	 * @return string|WP_Error
102
+	 */
103
+	public function master() {
104
+		$result = $this->app->api()->fetch()->tree_recursive();
105
+
106
+		if ( is_wp_error( $result ) ) {
107
+			/* @var WP_Error $result */
108
+			return $result;
109
+		}
110
+
111
+		if ( is_array( $result ) ) {
112
+			$result = $this->import_files( $result );
113
+		}
114
+
115
+		if ( is_wp_error( $result ) ) {
116
+			/* @var WP_Error $result */
117
+			return $result;
118
+		}
119
+
120
+		return __( 'Payload processed', 'writing-on-github' );
121
+	}
122
+
123
+	/**
124
+	 * Checks whether the provided blob should be imported.
125
+	 *
126
+	 * @param Writing_On_GitHub_File_Info $file
127
+	 *
128
+	 * @return bool
129
+	 */
130
+	protected function importable_file( Writing_On_GitHub_File_Info $file ) {
131
+
132
+		$path = $file->path;
133
+
134
+		// only _pages, _posts and images
135
+		$prefixs = array( '_pages/', '_posts/', 'images/');
136
+		foreach ($prefixs as $prefix) {
137
+			if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) {
138
+				return true;
139
+			}
140
+		}
141
+		return false;
142
+	}
143
+
144
+	/**
145
+	 * Checks whether the provided blob should be imported.
146
+	 *
147
+	 * @param Writing_On_GitHub_Blob $blob Blob to validate.
148
+	 *
149
+	 * @return bool
150
+	 */
151
+	protected function importable_post( Writing_On_GitHub_Blob $blob ) {
152
+		// global $wpdb;
153
+
154
+		// // Skip the repo's readme.
155
+		// if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) {
156
+		//  return false;
157
+		// }
158
+
159
+		// // If the blob sha already matches a post, then move on.
160
+		// if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) {
161
+		//  return false;
162
+		// }
163
+
164
+		if ( ! $blob->has_frontmatter() ) {
165
+			return false;
166
+		}
167
+
168
+		return true;
169
+	}
170
+
171
+	/**
172
+	 * Delete post
173
+	 * @param  Writing_On_GitHub_Blob $blob
174
+	 * @return WP_Error|bool
175
+	 */
176
+	protected function delete_post( Writing_On_GitHub_Blob $blob ) {
177
+		$id = $blob->id();
178
+		if ( empty( $id ) ) {
179
+			return false;
180
+		}
181
+		$result = $this->app->database()->delete_post( $id );
182
+		if ( is_wp_error( $result ) ) {
183
+			/* @var WP_Error $result */
184
+			return $result;
185
+		}
186
+		return true;
187
+	}
188
+
189
+	/**
190
+	 * Imports a post into wordpress
191
+	 * @param  Writing_On_GitHub_Blob $blob
192
+	 * @return WP_Error|bool
193
+	 */
194
+	protected function import_post( Writing_On_GitHub_Blob $blob ) {
195
+		$post = $this->blob_to_post( $blob );
196
+
197
+		if ( ! $post instanceof Writing_On_GitHub_Post ) {
198
+			return false;
199
+		}
200
+
201
+		$result = $this->app->database()->save_post( $post );
202
+		if ( is_wp_error( $result ) ) {
203
+			/** @var WP_Error $result */
204
+			return $result;
205
+		}
206
+
207
+		if ( $post->is_new() ||
208
+				! wogh_equal_front_matter( $post, $blob ) ) {
209
+
210
+			$result = $this->app->export()->export_post( $post );
211
+
212
+			if ( is_wp_error( $result ) ) {
213
+				/** @var WP_Error $result */
214
+				return $result;
215
+			}
216
+		}
217
+
218
+		clean_post_cache( $post->id() );
219
+
220
+		return true;
221
+	}
222
+
223
+	/**
224
+	 * import raw file
225
+	 * @param  Writing_On_GitHub_Blob $blob
226
+	 * @return bool
227
+	 */
228
+	protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) {
229
+		if ( $blob->has_frontmatter() ) {
230
+			return false;
231
+		}
232
+
233
+		// only images
234
+		if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) {
235
+			return false;
236
+		}
237
+
238
+		return true;
239
+	}
240
+
241
+	/**
242
+	 * Imports a raw file content into file system.
243
+	 * @param  Writing_On_GitHub_Blob $blob
244
+	 * @param  bool                   $is_remove
245
+	 */
246
+	protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) {
247
+		$arr = wp_upload_dir();
248
+		$path = $arr['basedir'] . '/writing-on-github/' . $blob->path();
249
+		if ( $is_remove ) {
250
+			if ( file_exists($path) ) {
251
+				unlink($path);
252
+			}
253
+		} else {
254
+			$dirname = dirname($path);
255
+			if ( ! file_exists($dirname) ) {
256
+				wp_mkdir_p($dirname);
257
+			}
258
+
259
+			file_put_contents($path, $blob->content());
260
+		}
261
+		return true;
262
+	}
263
+
264
+	/**
265
+	 * Imports a single blob content into matching post.
266
+	 *
267
+	 * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post.
268
+	 *
269
+	 * @return Writing_On_GitHub_Post|false
270
+	 */
271
+	protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
272
+		$args = array( 'post_content' => $blob->content_import() );
273
+		$meta = $blob->meta();
274
+
275
+		$id = false;
276
+
277
+		if ( ! empty( $meta ) ) {
278
+			if ( array_key_exists( 'layout', $meta ) ) {
279
+				$args['post_type'] = $meta['layout'];
280
+				unset( $meta['layout'] );
281
+			}
282
+
283
+			if ( array_key_exists( 'published', $meta ) ) {
284
+				$args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
285
+				unset( $meta['published'] );
286
+			}
287
+
288
+			if ( array_key_exists( 'post_title', $meta ) ) {
289
+				$args['post_title'] = $meta['post_title'];
290
+				unset( $meta['post_title'] );
291
+			}
292
+
293
+			if ( array_key_exists( 'post_name', $meta ) ) {
294
+				$args['post_name'] = $meta['post_name'];
295
+				unset( $meta['post_name'] );
296
+			}
297
+
298
+			if ( array_key_exists( 'ID', $meta ) ) {
299
+				$id = $args['ID'] = $meta['ID'];
300
+				$blob->set_id($id);
301
+				unset( $meta['ID'] );
302
+			}
303
+		}
304
+
305
+		$meta['_wogh_sha'] = $blob->sha();
306
+
307
+		if ( $id ) {
308
+			$old_sha = get_post_meta( $id, '_wogh_sha', true );
309
+			$old_github_path = get_post_meta( $id, '_wogh_github_path', true );
310
+
311
+			// dont save post when has same sha
312
+			if ( $old_sha  && $old_sha == $meta['_wogh_sha'] &&
313
+				 $old_github_path && $old_github_path == $blob->path() ) {
314
+				return false;
315
+			}
316
+		}
317
+
318
+		$post = new Writing_On_GitHub_Post( $args, $this->app->api() );
319
+		$post->set_old_github_path( $blob->path() );
320
+		$post->set_meta( $meta );
321
+		$post->set_blob( $blob );
322
+		$blob->set_id( $post->id() );
323
+
324
+		return $post;
325
+	}
326 326
 }
Please login to merge, or discard this patch.
lib/post.php 1 patch
Indentation   +470 added lines, -470 removed lines patch added patch discarded remove patch
@@ -9,474 +9,474 @@
 block discarded – undo
9 9
  */
10 10
 class Writing_On_GitHub_Post {
11 11
 
12
-    /**
13
-     * Api object
14
-     *
15
-     * @var Writing_On_GitHub_Api
16
-     */
17
-    public $api;
18
-
19
-    /**
20
-     * Post ID
21
-     * @var integer
22
-     */
23
-    public $id = 0;
24
-
25
-    /**
26
-     * Blob object
27
-     * @var Writing_On_GitHub_Blob
28
-     */
29
-    public $blob;
30
-
31
-    /**
32
-     * Post object
33
-     * @var WP_Post
34
-     */
35
-    public $post;
36
-
37
-    /**
38
-     * Post args.
39
-     *
40
-     * @var array
41
-     */
42
-    protected $args;
43
-
44
-    /**
45
-     * Post meta.
46
-     *
47
-     * @var array
48
-     */
49
-    protected $meta;
50
-
51
-    /**
52
-     * Whether the post has been saved.
53
-     *
54
-     * @var bool
55
-     */
56
-    protected $new = true;
57
-
58
-
59
-    protected $old_github_path;
60
-
61
-    /**
62
-     * Instantiates a new Post object
63
-     *
64
-     * @param int|array                 $id_or_args Either a post ID or an array of arguments.
65
-     * @param Writing_On_GitHub_Api $api API object.
66
-     *
67
-     * @todo remove database operations from this method
68
-     */
69
-    public function __construct( $id_or_args, Writing_On_GitHub_Api $api ) {
70
-        $this->api = $api;
71
-
72
-        if ( is_numeric( $id_or_args ) ) {
73
-            $this->id   = (int) $id_or_args;
74
-            $this->post = get_post( $this->id );
75
-            $this->new  = false;
76
-        }
77
-
78
-        if ( is_array( $id_or_args ) ) {
79
-            $this->args = $id_or_args;
80
-
81
-            if ( isset( $this->args['ID'] ) ) {
82
-                $this->post = get_post( $this->args['ID'] );
83
-
84
-                if ( $this->post ) {
85
-                    $this->id  = $this->post->ID;
86
-                    $this->new = false;
87
-                } else {
88
-                    unset( $this->args['ID'] );
89
-                }
90
-            }
91
-        }
92
-    }
93
-
94
-    public function id() {
95
-        return $this->id;
96
-    }
97
-
98
-    /**
99
-     * Returns the post type
100
-     */
101
-    public function type() {
102
-        return $this->post->post_type;
103
-    }
104
-
105
-    /**
106
-     * Returns the post type
107
-     */
108
-    public function status() {
109
-        return $this->post->post_status;
110
-    }
111
-
112
-    /**
113
-     * Returns the post name
114
-     */
115
-    public function name() {
116
-        return $this->post->post_name;
117
-    }
118
-
119
-    /**
120
-     * Returns true if the post has a password
121
-     * @return bool
122
-     */
123
-    public function has_password() {
124
-        return ! empty( $this->post->post_password );
125
-    }
126
-
127
-    /**
128
-     * Combines the 2 content parts for GitHub
129
-     */
130
-    public function github_content() {
131
-        $content = $this->front_matter() . $this->post_content();
132
-        $ending  = apply_filters( 'wogh_line_endings', "\n" );
133
-
134
-        return preg_replace( '~(*BSR_ANYCRLF)\R~', $ending, $content );
135
-    }
136
-
137
-    /**
138
-     * The post's YAML frontmatter
139
-     *
140
-     * Returns String the YAML frontmatter, ready to be written to the file
141
-     */
142
-    public function front_matter() {
143
-        return "---\n" . spyc_dump( $this->meta() ) . "---\n";
144
-    }
145
-
146
-    /**
147
-     * Returns the post_content
148
-     *
149
-     * Markdownify's the content if applicable
150
-     */
151
-    public function post_content() {
152
-        $content = $this->post->post_content;
153
-
154
-        if ( function_exists( 'wpmarkdown_html_to_markdown' ) ) {
155
-            $content = wpmarkdown_html_to_markdown( $content );
156
-        } else if ( class_exists( 'WPCom_Markdown' ) ) {
157
-            if ( WPCom_Markdown::get_instance()->is_markdown( $this->post->ID ) ) {
158
-                $content = $this->post->post_content_filtered;
159
-            }
160
-        }
161
-
162
-        return apply_filters( 'wogh_content_export', $content, $this );
163
-    }
164
-
165
-    public function old_github_path() {
166
-        return $this->old_github_path;
167
-    }
168
-
169
-    public function set_old_github_path( $path ) {
170
-        $this->old_github_path = $path;
171
-        update_post_meta( $this->id, '_wogh_github_path', $path );
172
-    }
173
-
174
-
175
-    /**
176
-     * Retrieves or calculates the proper GitHub path for a given post
177
-     *
178
-     * Returns (string) the path relative to repo root
179
-     */
180
-    public function github_path() {
181
-        $path = $this->github_directory() . $this->github_filename();
182
-
183
-        return $path;
184
-    }
185
-
186
-    /**
187
-     * Get GitHub directory based on post
188
-     *
189
-     * @return string
190
-     */
191
-    public function github_directory() {
192
-        if ( 'publish' !== $this->status() ) {
193
-            return apply_filters( 'wogh_directory_unpublished', '_drafts/', $this );
194
-        }
195
-
196
-        $name = '';
197
-
198
-        switch ( $this->type() ) {
199
-            case 'post':
200
-                $name = 'posts';
201
-                break;
202
-            case 'page':
203
-                $name = 'pages';
204
-                break;
205
-            default:
206
-                $obj = get_post_type_object( $this->type() );
207
-
208
-                if ( $obj ) {
209
-                    $name = strtolower( $obj->labels->name );
210
-                }
211
-
212
-                if ( ! $name ) {
213
-                    $name = '';
214
-                }
215
-        }
216
-
217
-        if ( $name ) {
218
-            $name = '_' . $name . '/';
219
-        }
220
-
221
-        return apply_filters( 'wogh_directory_published', $name, $this );
222
-    }
223
-
224
-    /**
225
-     * Build GitHub filename based on post
226
-     */
227
-    public function github_filename() {
228
-        if ( 'post' === $this->type() ) {
229
-            $filename = get_the_time( 'Y-m-d-', $this->id ) . $this->get_name() . '.md';
230
-        } else {
231
-            $filename = $this->get_name() . '.md';
232
-        }
233
-
234
-        return apply_filters( 'wogh_filename', $filename, $this );
235
-    }
236
-
237
-    /**
238
-     * Returns a post slug we can use in the GitHub filename
239
-     *
240
-     * @return string
241
-     */
242
-    protected function get_name() {
243
-        if ( '' !== $this->name() ) {
244
-            return $this->name();
245
-        }
246
-
247
-        return sanitize_title( get_the_title( $this->post ) );
248
-    }
249
-
250
-    /**
251
-     * is put on github
252
-     * @return boolean
253
-     */
254
-    public function is_on_github() {
255
-        $sha = get_post_meta( $this->id, '_wogh_sha', true );
256
-        $github_path = get_post_meta( $this->id, '_wogh_github_path', true );
257
-        if ( $sha && $github_path ) {
258
-            return true;
259
-        }
260
-        return false;
261
-    }
262
-
263
-    /**
264
-     * Returns the URL for the post on GitHub.
265
-     *
266
-     * @return string
267
-     */
268
-    public function github_view_url() {
269
-        $github_path = get_post_meta( $this->id, '_wogh_github_path', true );
270
-        $repository = $this->api->fetch()->repository();
271
-        $branch = $this->api->fetch()->branch();
272
-
273
-        return "https://github.com/$repository/blob/$branch/$github_path";
274
-    }
275
-
276
-    /**
277
-     * Returns the URL for the post on GitHub.
278
-     *
279
-     * @return string
280
-     */
281
-    public function github_edit_url() {
282
-        $github_path = get_post_meta( $this->id, '_wogh_github_path', true );
283
-        $repository = $this->api->fetch()->repository();
284
-        $branch = $this->api->fetch()->branch();
285
-
286
-        return "https://github.com/$repository/edit/$branch/$github_path";
287
-    }
288
-
289
-    /**
290
-     * Retrieve post type directory from blob path.
291
-     *
292
-     * @param string $path Path string.
293
-     *
294
-     * @return string
295
-     */
296
-    public function get_directory_from_path( $path ) {
297
-        $directory = explode( '/', $path );
298
-        $directory = count( $directory ) > 0 ? $directory[0] : '';
299
-
300
-        return $directory;
301
-    }
302
-
303
-    /**
304
-     * Determines the last author to modify the post
305
-     *
306
-     * Returns Array an array containing the author name and email
307
-     */
308
-    public function last_modified_author() {
309
-        if ( $last_id = get_post_meta( $this->id, '_edit_last', true ) ) {
310
-            $user = get_userdata( $last_id );
311
-
312
-            if ( $user ) {
313
-                return array( 'name' => $user->display_name, 'email' => $user->user_email );
314
-            }
315
-        }
316
-
317
-        return array();
318
-    }
319
-
320
-    /**
321
-     * The post's sha
322
-     * Cached as post meta, or will make a live call if need be
323
-     *
324
-     * Returns String the sha1 hash
325
-     */
326
-    public function sha() {
327
-        $sha = get_post_meta( $this->id, '_wogh_sha', true );
328
-
329
-        // If we've done a full export and we have no sha
330
-        // then we should try a live check to see if it exists.
331
-        // if ( ! $sha && 'yes' === get_option( '_wogh_fully_exported' ) ) {
332
-
333
-        //  // @todo could we eliminate this by calling down the full tree and searching it
334
-        //  $data = $this->api->fetch()->remote_contents( $this );
335
-
336
-        //  if ( ! is_wp_error( $data ) ) {
337
-        //      update_post_meta( $this->id, '_wogh_sha', $data->sha );
338
-        //      $sha = $data->sha;
339
-        //  }
340
-        // }
341
-
342
-        // if the sha still doesn't exist, then it's empty
343
-        if ( ! $sha || is_wp_error( $sha ) ) {
344
-            $sha = '';
345
-        }
346
-
347
-        return $sha;
348
-    }
349
-
350
-    /**
351
-     * Save the sha to post
352
-     *
353
-     * @param string $sha
354
-     */
355
-    public function set_sha( $sha ) {
356
-        update_post_meta( $this->id, '_wogh_sha', $sha );
357
-    }
358
-
359
-    /**
360
-     * The post's metadata
361
-     *
362
-     * Returns Array the post's metadata
363
-     */
364
-    public function meta() {
365
-        $meta = array(
366
-            'ID'           => $this->id,
367
-            'post_title'   => get_the_title( $this->post ),
368
-            'post_name'    => $this->post->post_name,
369
-            'author'       => ( $author = get_userdata( $this->post->post_author ) ) ? $author->display_name : '',
370
-            'post_date'    => $this->post->post_date,
371
-            'post_excerpt' => $this->post->post_excerpt,
372
-            'layout'       => get_post_type( $this->post ),
373
-            'link'         => get_permalink( $this->post ),
374
-            'published'    => 'publish' === $this->status() ? true : false,
375
-            'tags'         => wp_get_post_tags( $this->id, array( 'fields' => 'names' ) ),
376
-            'categories'   => wp_get_post_categories( $this->id, array( 'fields' => 'names' ) )
377
-        );
378
-        if ( empty($this->post->post_name) ) {
379
-            unset($meta['post_name']);
380
-        }
381
-        if ( empty($this->post->post_excerpt) ) {
382
-            unset($meta['post_excerpt']);
383
-        }
384
-        if ( 'yes' == get_option('wogh_ignore_author') ) {
385
-            unset($meta['author']);
386
-        }
387
-
388
-        //convert traditional post_meta values, hide hidden values, skip already populated values
389
-        // foreach ( get_post_custom( $this->id ) as $key => $value ) {
390
-
391
-        //  if ( '_' === substr( $key, 0, 1 ) || isset( $meta[ $key ] ) ) {
392
-        //      continue;
393
-        //  }
394
-
395
-        //  $meta[ $key ] = $value;
396
-
397
-        // }
398
-
399
-        return apply_filters( 'wogh_post_meta', $meta, $this );
400
-    }
401
-
402
-    /**
403
-     * Returns whether the Post has been saved in the DB yet.
404
-     *
405
-     * @return bool
406
-     */
407
-    public function is_new() {
408
-        return $this->new;
409
-    }
410
-
411
-    /**
412
-     * Sets the Post's meta.
413
-     *
414
-     * @param array $meta
415
-     */
416
-    public function set_meta( $meta ) {
417
-        $this->meta = $meta;
418
-    }
419
-
420
-    /**
421
-     * Returns the Post's arguments.
422
-     *
423
-     * @return array
424
-     */
425
-    public function get_args() {
426
-        return $this->args;
427
-    }
428
-
429
-    /**
430
-     * Returns the Post's meta.
431
-     *
432
-     * @return array
433
-     */
434
-    public function get_meta() {
435
-        return $this->meta;
436
-    }
437
-
438
-    /**
439
-     * Get the blob
440
-     * @return Writing_On_GitHub_Blob
441
-     */
442
-    public function get_blob() {
443
-        return $this->blob;
444
-    }
445
-
446
-    /**
447
-     * Set the blob
448
-     * @param Writing_On_GitHub_Blob $blob
449
-     */
450
-    public function set_blob( Writing_On_GitHub_Blob $blob ) {
451
-        $this->blob = $blob;
452
-    }
453
-
454
-    /**
455
-     * Sets the Post's WP_Post object.
456
-     *
457
-     * @param WP_Post $post
458
-     *
459
-     * @return $this
460
-     */
461
-    public function set_post( WP_Post $post ) {
462
-        $this->post = $post;
463
-        $this->id   = $post->ID;
464
-
465
-        return $this;
466
-    }
467
-
468
-    /**
469
-     * Transforms the Post into a Blob.
470
-     *
471
-     * @return Writing_On_GitHub_Blob
472
-     */
473
-    public function to_blob() {
474
-        $data = new stdClass;
475
-
476
-        $data->path    = $this->github_path();
477
-        $data->content = $this->github_content();
478
-        $data->sha     = $this->sha();
479
-
480
-        return new Writing_On_GitHub_Blob( $data );
481
-    }
12
+	/**
13
+	 * Api object
14
+	 *
15
+	 * @var Writing_On_GitHub_Api
16
+	 */
17
+	public $api;
18
+
19
+	/**
20
+	 * Post ID
21
+	 * @var integer
22
+	 */
23
+	public $id = 0;
24
+
25
+	/**
26
+	 * Blob object
27
+	 * @var Writing_On_GitHub_Blob
28
+	 */
29
+	public $blob;
30
+
31
+	/**
32
+	 * Post object
33
+	 * @var WP_Post
34
+	 */
35
+	public $post;
36
+
37
+	/**
38
+	 * Post args.
39
+	 *
40
+	 * @var array
41
+	 */
42
+	protected $args;
43
+
44
+	/**
45
+	 * Post meta.
46
+	 *
47
+	 * @var array
48
+	 */
49
+	protected $meta;
50
+
51
+	/**
52
+	 * Whether the post has been saved.
53
+	 *
54
+	 * @var bool
55
+	 */
56
+	protected $new = true;
57
+
58
+
59
+	protected $old_github_path;
60
+
61
+	/**
62
+	 * Instantiates a new Post object
63
+	 *
64
+	 * @param int|array                 $id_or_args Either a post ID or an array of arguments.
65
+	 * @param Writing_On_GitHub_Api $api API object.
66
+	 *
67
+	 * @todo remove database operations from this method
68
+	 */
69
+	public function __construct( $id_or_args, Writing_On_GitHub_Api $api ) {
70
+		$this->api = $api;
71
+
72
+		if ( is_numeric( $id_or_args ) ) {
73
+			$this->id   = (int) $id_or_args;
74
+			$this->post = get_post( $this->id );
75
+			$this->new  = false;
76
+		}
77
+
78
+		if ( is_array( $id_or_args ) ) {
79
+			$this->args = $id_or_args;
80
+
81
+			if ( isset( $this->args['ID'] ) ) {
82
+				$this->post = get_post( $this->args['ID'] );
83
+
84
+				if ( $this->post ) {
85
+					$this->id  = $this->post->ID;
86
+					$this->new = false;
87
+				} else {
88
+					unset( $this->args['ID'] );
89
+				}
90
+			}
91
+		}
92
+	}
93
+
94
+	public function id() {
95
+		return $this->id;
96
+	}
97
+
98
+	/**
99
+	 * Returns the post type
100
+	 */
101
+	public function type() {
102
+		return $this->post->post_type;
103
+	}
104
+
105
+	/**
106
+	 * Returns the post type
107
+	 */
108
+	public function status() {
109
+		return $this->post->post_status;
110
+	}
111
+
112
+	/**
113
+	 * Returns the post name
114
+	 */
115
+	public function name() {
116
+		return $this->post->post_name;
117
+	}
118
+
119
+	/**
120
+	 * Returns true if the post has a password
121
+	 * @return bool
122
+	 */
123
+	public function has_password() {
124
+		return ! empty( $this->post->post_password );
125
+	}
126
+
127
+	/**
128
+	 * Combines the 2 content parts for GitHub
129
+	 */
130
+	public function github_content() {
131
+		$content = $this->front_matter() . $this->post_content();
132
+		$ending  = apply_filters( 'wogh_line_endings', "\n" );
133
+
134
+		return preg_replace( '~(*BSR_ANYCRLF)\R~', $ending, $content );
135
+	}
136
+
137
+	/**
138
+	 * The post's YAML frontmatter
139
+	 *
140
+	 * Returns String the YAML frontmatter, ready to be written to the file
141
+	 */
142
+	public function front_matter() {
143
+		return "---\n" . spyc_dump( $this->meta() ) . "---\n";
144
+	}
145
+
146
+	/**
147
+	 * Returns the post_content
148
+	 *
149
+	 * Markdownify's the content if applicable
150
+	 */
151
+	public function post_content() {
152
+		$content = $this->post->post_content;
153
+
154
+		if ( function_exists( 'wpmarkdown_html_to_markdown' ) ) {
155
+			$content = wpmarkdown_html_to_markdown( $content );
156
+		} else if ( class_exists( 'WPCom_Markdown' ) ) {
157
+			if ( WPCom_Markdown::get_instance()->is_markdown( $this->post->ID ) ) {
158
+				$content = $this->post->post_content_filtered;
159
+			}
160
+		}
161
+
162
+		return apply_filters( 'wogh_content_export', $content, $this );
163
+	}
164
+
165
+	public function old_github_path() {
166
+		return $this->old_github_path;
167
+	}
168
+
169
+	public function set_old_github_path( $path ) {
170
+		$this->old_github_path = $path;
171
+		update_post_meta( $this->id, '_wogh_github_path', $path );
172
+	}
173
+
174
+
175
+	/**
176
+	 * Retrieves or calculates the proper GitHub path for a given post
177
+	 *
178
+	 * Returns (string) the path relative to repo root
179
+	 */
180
+	public function github_path() {
181
+		$path = $this->github_directory() . $this->github_filename();
182
+
183
+		return $path;
184
+	}
185
+
186
+	/**
187
+	 * Get GitHub directory based on post
188
+	 *
189
+	 * @return string
190
+	 */
191
+	public function github_directory() {
192
+		if ( 'publish' !== $this->status() ) {
193
+			return apply_filters( 'wogh_directory_unpublished', '_drafts/', $this );
194
+		}
195
+
196
+		$name = '';
197
+
198
+		switch ( $this->type() ) {
199
+			case 'post':
200
+				$name = 'posts';
201
+				break;
202
+			case 'page':
203
+				$name = 'pages';
204
+				break;
205
+			default:
206
+				$obj = get_post_type_object( $this->type() );
207
+
208
+				if ( $obj ) {
209
+					$name = strtolower( $obj->labels->name );
210
+				}
211
+
212
+				if ( ! $name ) {
213
+					$name = '';
214
+				}
215
+		}
216
+
217
+		if ( $name ) {
218
+			$name = '_' . $name . '/';
219
+		}
220
+
221
+		return apply_filters( 'wogh_directory_published', $name, $this );
222
+	}
223
+
224
+	/**
225
+	 * Build GitHub filename based on post
226
+	 */
227
+	public function github_filename() {
228
+		if ( 'post' === $this->type() ) {
229
+			$filename = get_the_time( 'Y-m-d-', $this->id ) . $this->get_name() . '.md';
230
+		} else {
231
+			$filename = $this->get_name() . '.md';
232
+		}
233
+
234
+		return apply_filters( 'wogh_filename', $filename, $this );
235
+	}
236
+
237
+	/**
238
+	 * Returns a post slug we can use in the GitHub filename
239
+	 *
240
+	 * @return string
241
+	 */
242
+	protected function get_name() {
243
+		if ( '' !== $this->name() ) {
244
+			return $this->name();
245
+		}
246
+
247
+		return sanitize_title( get_the_title( $this->post ) );
248
+	}
249
+
250
+	/**
251
+	 * is put on github
252
+	 * @return boolean
253
+	 */
254
+	public function is_on_github() {
255
+		$sha = get_post_meta( $this->id, '_wogh_sha', true );
256
+		$github_path = get_post_meta( $this->id, '_wogh_github_path', true );
257
+		if ( $sha && $github_path ) {
258
+			return true;
259
+		}
260
+		return false;
261
+	}
262
+
263
+	/**
264
+	 * Returns the URL for the post on GitHub.
265
+	 *
266
+	 * @return string
267
+	 */
268
+	public function github_view_url() {
269
+		$github_path = get_post_meta( $this->id, '_wogh_github_path', true );
270
+		$repository = $this->api->fetch()->repository();
271
+		$branch = $this->api->fetch()->branch();
272
+
273
+		return "https://github.com/$repository/blob/$branch/$github_path";
274
+	}
275
+
276
+	/**
277
+	 * Returns the URL for the post on GitHub.
278
+	 *
279
+	 * @return string
280
+	 */
281
+	public function github_edit_url() {
282
+		$github_path = get_post_meta( $this->id, '_wogh_github_path', true );
283
+		$repository = $this->api->fetch()->repository();
284
+		$branch = $this->api->fetch()->branch();
285
+
286
+		return "https://github.com/$repository/edit/$branch/$github_path";
287
+	}
288
+
289
+	/**
290
+	 * Retrieve post type directory from blob path.
291
+	 *
292
+	 * @param string $path Path string.
293
+	 *
294
+	 * @return string
295
+	 */
296
+	public function get_directory_from_path( $path ) {
297
+		$directory = explode( '/', $path );
298
+		$directory = count( $directory ) > 0 ? $directory[0] : '';
299
+
300
+		return $directory;
301
+	}
302
+
303
+	/**
304
+	 * Determines the last author to modify the post
305
+	 *
306
+	 * Returns Array an array containing the author name and email
307
+	 */
308
+	public function last_modified_author() {
309
+		if ( $last_id = get_post_meta( $this->id, '_edit_last', true ) ) {
310
+			$user = get_userdata( $last_id );
311
+
312
+			if ( $user ) {
313
+				return array( 'name' => $user->display_name, 'email' => $user->user_email );
314
+			}
315
+		}
316
+
317
+		return array();
318
+	}
319
+
320
+	/**
321
+	 * The post's sha
322
+	 * Cached as post meta, or will make a live call if need be
323
+	 *
324
+	 * Returns String the sha1 hash
325
+	 */
326
+	public function sha() {
327
+		$sha = get_post_meta( $this->id, '_wogh_sha', true );
328
+
329
+		// If we've done a full export and we have no sha
330
+		// then we should try a live check to see if it exists.
331
+		// if ( ! $sha && 'yes' === get_option( '_wogh_fully_exported' ) ) {
332
+
333
+		//  // @todo could we eliminate this by calling down the full tree and searching it
334
+		//  $data = $this->api->fetch()->remote_contents( $this );
335
+
336
+		//  if ( ! is_wp_error( $data ) ) {
337
+		//      update_post_meta( $this->id, '_wogh_sha', $data->sha );
338
+		//      $sha = $data->sha;
339
+		//  }
340
+		// }
341
+
342
+		// if the sha still doesn't exist, then it's empty
343
+		if ( ! $sha || is_wp_error( $sha ) ) {
344
+			$sha = '';
345
+		}
346
+
347
+		return $sha;
348
+	}
349
+
350
+	/**
351
+	 * Save the sha to post
352
+	 *
353
+	 * @param string $sha
354
+	 */
355
+	public function set_sha( $sha ) {
356
+		update_post_meta( $this->id, '_wogh_sha', $sha );
357
+	}
358
+
359
+	/**
360
+	 * The post's metadata
361
+	 *
362
+	 * Returns Array the post's metadata
363
+	 */
364
+	public function meta() {
365
+		$meta = array(
366
+			'ID'           => $this->id,
367
+			'post_title'   => get_the_title( $this->post ),
368
+			'post_name'    => $this->post->post_name,
369
+			'author'       => ( $author = get_userdata( $this->post->post_author ) ) ? $author->display_name : '',
370
+			'post_date'    => $this->post->post_date,
371
+			'post_excerpt' => $this->post->post_excerpt,
372
+			'layout'       => get_post_type( $this->post ),
373
+			'link'         => get_permalink( $this->post ),
374
+			'published'    => 'publish' === $this->status() ? true : false,
375
+			'tags'         => wp_get_post_tags( $this->id, array( 'fields' => 'names' ) ),
376
+			'categories'   => wp_get_post_categories( $this->id, array( 'fields' => 'names' ) )
377
+		);
378
+		if ( empty($this->post->post_name) ) {
379
+			unset($meta['post_name']);
380
+		}
381
+		if ( empty($this->post->post_excerpt) ) {
382
+			unset($meta['post_excerpt']);
383
+		}
384
+		if ( 'yes' == get_option('wogh_ignore_author') ) {
385
+			unset($meta['author']);
386
+		}
387
+
388
+		//convert traditional post_meta values, hide hidden values, skip already populated values
389
+		// foreach ( get_post_custom( $this->id ) as $key => $value ) {
390
+
391
+		//  if ( '_' === substr( $key, 0, 1 ) || isset( $meta[ $key ] ) ) {
392
+		//      continue;
393
+		//  }
394
+
395
+		//  $meta[ $key ] = $value;
396
+
397
+		// }
398
+
399
+		return apply_filters( 'wogh_post_meta', $meta, $this );
400
+	}
401
+
402
+	/**
403
+	 * Returns whether the Post has been saved in the DB yet.
404
+	 *
405
+	 * @return bool
406
+	 */
407
+	public function is_new() {
408
+		return $this->new;
409
+	}
410
+
411
+	/**
412
+	 * Sets the Post's meta.
413
+	 *
414
+	 * @param array $meta
415
+	 */
416
+	public function set_meta( $meta ) {
417
+		$this->meta = $meta;
418
+	}
419
+
420
+	/**
421
+	 * Returns the Post's arguments.
422
+	 *
423
+	 * @return array
424
+	 */
425
+	public function get_args() {
426
+		return $this->args;
427
+	}
428
+
429
+	/**
430
+	 * Returns the Post's meta.
431
+	 *
432
+	 * @return array
433
+	 */
434
+	public function get_meta() {
435
+		return $this->meta;
436
+	}
437
+
438
+	/**
439
+	 * Get the blob
440
+	 * @return Writing_On_GitHub_Blob
441
+	 */
442
+	public function get_blob() {
443
+		return $this->blob;
444
+	}
445
+
446
+	/**
447
+	 * Set the blob
448
+	 * @param Writing_On_GitHub_Blob $blob
449
+	 */
450
+	public function set_blob( Writing_On_GitHub_Blob $blob ) {
451
+		$this->blob = $blob;
452
+	}
453
+
454
+	/**
455
+	 * Sets the Post's WP_Post object.
456
+	 *
457
+	 * @param WP_Post $post
458
+	 *
459
+	 * @return $this
460
+	 */
461
+	public function set_post( WP_Post $post ) {
462
+		$this->post = $post;
463
+		$this->id   = $post->ID;
464
+
465
+		return $this;
466
+	}
467
+
468
+	/**
469
+	 * Transforms the Post into a Blob.
470
+	 *
471
+	 * @return Writing_On_GitHub_Blob
472
+	 */
473
+	public function to_blob() {
474
+		$data = new stdClass;
475
+
476
+		$data->path    = $this->github_path();
477
+		$data->content = $this->github_content();
478
+		$data->sha     = $this->sha();
479
+
480
+		return new Writing_On_GitHub_Blob( $data );
481
+	}
482 482
 }
Please login to merge, or discard this patch.
lib/function.php 1 patch
Indentation   +8 added lines, -8 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,5 +31,5 @@  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
 }
Please login to merge, or discard this patch.
lib/client/fetch.php 1 patch
Indentation   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -9,118 +9,118 @@
 block discarded – undo
9 9
  */
10 10
 class Writing_On_GitHub_Fetch_Client extends Writing_On_GitHub_Base_Client {
11 11
 
12
-    /**
13
-     * Compare a commit by sha with master from the GitHub API
14
-     *
15
-     * @param string $sha Sha for commit to retrieve.
16
-     *
17
-     * @return Writing_On_GitHub_File_Info[]|WP_Error
18
-     */
19
-    public function compare( $sha ) {
20
-        // https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master
21
-        $endpoint = $this->compare_endpoint();
22
-        $branch = $this->branch();
23
-        $data = $this->call( 'GET', "$endpoint/$sha...$branch" );
24
-
25
-        if ( is_wp_error( $data ) ) {
26
-            return $data;
27
-        }
28
-
29
-        $files = array();
30
-        foreach ($data->files as $file) {
31
-            $file->path = $file->filename;
32
-            $files[] = new Writing_On_GitHub_File_Info($file);
33
-        }
34
-
35
-        return $files;
36
-    }
37
-
38
-    /**
39
-     * Calls the content API to get the post's contents and metadata
40
-     *
41
-     * Returns Object the response from the API
42
-     *
43
-     * @param Writing_On_GitHub_Post $post Post to retrieve remote contents for.
44
-     *
45
-     * @return mixed
46
-     */
47
-    public function remote_contents( $post ) {
48
-        return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) );
49
-    }
50
-
51
-
52
-
53
-    public function exists( $path ) {
54
-        $result = $this->call( 'GET', $this->content_endpoint( $path ) );
55
-        if ( is_wp_error( $result ) ) {
56
-            return false;
57
-        }
58
-        return true;
59
-    }
60
-
61
-    /**
62
-     * Retrieves a tree by sha recursively from the GitHub API
63
-     *
64
-     * @param string $sha Commit sha to retrieve tree from.
65
-     *
66
-     * @return Writing_On_GitHub_File_Info[]|WP_Error
67
-     */
68
-    public function tree_recursive( $sha = '_default' ) {
69
-
70
-        if ( '_default' === $sha ) {
71
-            $sha = $this->branch();
72
-        }
73
-
74
-        $data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' );
75
-
76
-        if ( is_wp_error( $data ) ) {
77
-            return $data;
78
-        }
79
-
80
-        $files = array();
81
-
82
-        foreach ( $data->tree as $index => $thing ) {
83
-            // We need to remove the trees because
84
-            // the recursive tree includes both
85
-            // the subtrees as well the subtrees' blobs.
86
-            if ( 'blob' === $thing->type ) {
87
-                $thing->status = '';
88
-                $files[] = new Writing_On_GitHub_File_Info( $thing );
89
-            }
90
-        }
91
-
92
-        return $files;
93
-    }
94
-
95
-    /**
96
-     * Retrieves the blob data for a given sha
97
-     *
98
-     * @param Writing_On_GitHub_File_Info $fileinfo
99
-     *
100
-     * @return Writing_On_GitHub_Blob|WP_Error
101
-     */
102
-    public function blob( Writing_On_GitHub_File_Info $fileinfo ) {
103
-        $data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha );
104
-
105
-        if ( is_wp_error( $data ) ) {
106
-            return $data;
107
-        }
108
-
109
-        $data->path = $fileinfo->path;
110
-        return new Writing_On_GitHub_Blob( $data );
111
-    }
112
-
113
-    /**
114
-     * Get blob by path
115
-     * @param  string $path
116
-     * @return Writing_On_GitHub_Blob|WP_Error
117
-     */
118
-    public function blob_by_path( $path ) {
119
-        $result = $this->call( 'GET', $this->content_endpoint( $path ) );
120
-        if ( is_wp_error( $result ) ) {
121
-            return $result;
122
-        }
123
-
124
-        return new Writing_On_GitHub_Blob( $result );
125
-    }
12
+	/**
13
+	 * Compare a commit by sha with master from the GitHub API
14
+	 *
15
+	 * @param string $sha Sha for commit to retrieve.
16
+	 *
17
+	 * @return Writing_On_GitHub_File_Info[]|WP_Error
18
+	 */
19
+	public function compare( $sha ) {
20
+		// https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master
21
+		$endpoint = $this->compare_endpoint();
22
+		$branch = $this->branch();
23
+		$data = $this->call( 'GET', "$endpoint/$sha...$branch" );
24
+
25
+		if ( is_wp_error( $data ) ) {
26
+			return $data;
27
+		}
28
+
29
+		$files = array();
30
+		foreach ($data->files as $file) {
31
+			$file->path = $file->filename;
32
+			$files[] = new Writing_On_GitHub_File_Info($file);
33
+		}
34
+
35
+		return $files;
36
+	}
37
+
38
+	/**
39
+	 * Calls the content API to get the post's contents and metadata
40
+	 *
41
+	 * Returns Object the response from the API
42
+	 *
43
+	 * @param Writing_On_GitHub_Post $post Post to retrieve remote contents for.
44
+	 *
45
+	 * @return mixed
46
+	 */
47
+	public function remote_contents( $post ) {
48
+		return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) );
49
+	}
50
+
51
+
52
+
53
+	public function exists( $path ) {
54
+		$result = $this->call( 'GET', $this->content_endpoint( $path ) );
55
+		if ( is_wp_error( $result ) ) {
56
+			return false;
57
+		}
58
+		return true;
59
+	}
60
+
61
+	/**
62
+	 * Retrieves a tree by sha recursively from the GitHub API
63
+	 *
64
+	 * @param string $sha Commit sha to retrieve tree from.
65
+	 *
66
+	 * @return Writing_On_GitHub_File_Info[]|WP_Error
67
+	 */
68
+	public function tree_recursive( $sha = '_default' ) {
69
+
70
+		if ( '_default' === $sha ) {
71
+			$sha = $this->branch();
72
+		}
73
+
74
+		$data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' );
75
+
76
+		if ( is_wp_error( $data ) ) {
77
+			return $data;
78
+		}
79
+
80
+		$files = array();
81
+
82
+		foreach ( $data->tree as $index => $thing ) {
83
+			// We need to remove the trees because
84
+			// the recursive tree includes both
85
+			// the subtrees as well the subtrees' blobs.
86
+			if ( 'blob' === $thing->type ) {
87
+				$thing->status = '';
88
+				$files[] = new Writing_On_GitHub_File_Info( $thing );
89
+			}
90
+		}
91
+
92
+		return $files;
93
+	}
94
+
95
+	/**
96
+	 * Retrieves the blob data for a given sha
97
+	 *
98
+	 * @param Writing_On_GitHub_File_Info $fileinfo
99
+	 *
100
+	 * @return Writing_On_GitHub_Blob|WP_Error
101
+	 */
102
+	public function blob( Writing_On_GitHub_File_Info $fileinfo ) {
103
+		$data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha );
104
+
105
+		if ( is_wp_error( $data ) ) {
106
+			return $data;
107
+		}
108
+
109
+		$data->path = $fileinfo->path;
110
+		return new Writing_On_GitHub_Blob( $data );
111
+	}
112
+
113
+	/**
114
+	 * Get blob by path
115
+	 * @param  string $path
116
+	 * @return Writing_On_GitHub_Blob|WP_Error
117
+	 */
118
+	public function blob_by_path( $path ) {
119
+		$result = $this->call( 'GET', $this->content_endpoint( $path ) );
120
+		if ( is_wp_error( $result ) ) {
121
+			return $result;
122
+		}
123
+
124
+		return new Writing_On_GitHub_Blob( $result );
125
+	}
126 126
 }
Please login to merge, or discard this patch.