1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* API Blob model. |
4
|
|
|
* @package WordPress_GitHub_Sync |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class WordPress_GitHub_Sync_Blob |
9
|
|
|
*/ |
10
|
|
|
class WordPress_GitHub_Sync_Blob { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Raw blob data. |
14
|
|
|
* |
15
|
|
|
* @var stdClass |
16
|
|
|
*/ |
17
|
|
|
protected $data; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Complete blob content. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $content; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* content mime type. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $mimetype; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* content file extention. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $file_extension; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Blob sha. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $sha; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Blob path. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $path; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Whether the blob has frontmatter. |
56
|
|
|
* |
57
|
|
|
* @var boolean |
58
|
|
|
*/ |
59
|
|
|
protected $frontmatter = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Instantiates a new Blob object. |
63
|
|
|
* |
64
|
|
|
* @param stdClass $data Raw blob data. |
65
|
|
|
*/ |
66
|
|
|
public function __construct( stdClass $data ) { |
67
|
|
|
$this->data = $data; |
68
|
|
|
|
69
|
|
|
$this->interpret_data(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function mimetype() { |
73
|
|
|
|
74
|
|
|
if(!$this->mimetype){ |
|
|
|
|
75
|
|
|
// Detect minetype, might want to store this |
76
|
|
|
// as meta data |
77
|
|
|
$finfo = finfo_open(); |
78
|
|
|
$this->mimetype = finfo_buffer($finfo, $this->content, FILEINFO_MIME_TYPE); |
|
|
|
|
79
|
|
|
finfo_close($finfo); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->mimetype; |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function file_extension() { |
87
|
|
|
return $this->file_extension; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the raw blob content. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function content() { |
96
|
|
|
return $this->content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set's the blob's content. |
101
|
|
|
* |
102
|
|
|
* @param string $content Raw blob content. |
103
|
|
|
* @param bool $base64 Whether the content is base64 encoded. |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function set_content( $content, $base64 = false ) { |
108
|
|
|
if ( $base64 ) { |
109
|
|
|
$content = base64_decode( $content ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->frontmatter = '---' === substr( $this->content = $content, 0, 3 ) ? true : false; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the blob sha. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function sha() { |
123
|
|
|
return $this->sha; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return's the blob path. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function path() { |
132
|
|
|
return $this->path; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Whether the blob has frontmatter. |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function has_frontmatter() { |
141
|
|
|
return $this->frontmatter; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the formatted/filtered blob content used for import. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function content_import() { |
150
|
|
|
$content = $this->content(); |
151
|
|
|
|
152
|
|
|
if ( $this->has_frontmatter() ) { |
153
|
|
|
// Break out content. |
154
|
|
|
preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches ); |
155
|
|
|
$content = array_pop( $matches ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ( $this->file_extension == "md" && function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
|
|
|
|
159
|
|
|
$content = wpmarkdown_markdown_to_html( $content ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Filters the content for import. |
164
|
|
|
*/ |
165
|
|
|
return apply_filters( 'wpghs_content_import', trim( $content ) ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the blob meta. |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function meta() { |
174
|
|
|
$meta = array(); |
175
|
|
|
|
176
|
|
|
if ( $this->has_frontmatter() ) { |
177
|
|
|
// Break out meta, if present. |
178
|
|
|
preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
179
|
|
|
array_pop( $matches ); |
180
|
|
|
|
181
|
|
|
$meta = cyps_load( $matches[2] ); |
182
|
|
|
if ( isset( $meta['permalink'] ) ) { |
183
|
|
|
$meta['permalink'] = str_replace( home_url(), '', $meta['permalink'] ); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $meta; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Formats the blob into an API call body. |
192
|
|
|
* |
193
|
|
|
* @return stdClass |
194
|
|
|
*/ |
195
|
|
|
public function to_body() { |
196
|
|
|
$data = new stdClass; |
197
|
|
|
|
198
|
|
|
$data->mode = '100644'; |
199
|
|
|
$data->type = 'blob'; |
200
|
|
|
|
201
|
|
|
$data->path = $this->path(); |
202
|
|
|
|
203
|
|
|
if ( $this->sha() ) { |
204
|
|
|
$data->sha = $this->sha(); |
205
|
|
|
} else { |
206
|
|
|
$data->content = $this->content(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $data; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Interprets the blob's data into properties. |
214
|
|
|
*/ |
215
|
|
|
protected function interpret_data() { |
216
|
|
|
$this->sha = isset( $this->data->sha ) ? $this->data->sha : ''; |
217
|
|
|
$this->path = isset( $this->data->path ) ? $this->data->path : ''; |
218
|
|
|
|
219
|
|
|
$this->file_extension = pathinfo($this->path,PATHINFO_EXTENSION); |
|
|
|
|
220
|
|
|
|
221
|
|
|
$this->set_content( |
222
|
|
|
isset( $this->data->content ) ? trim( $this->data->content ) : '', |
223
|
|
|
isset( $this->data->encoding ) && 'base64' === $this->data->encoding ? true : false |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|