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
|
|
|
* Blob sha. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $sha; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Blob path. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $path; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Whether the blob has frontmatter. |
42
|
|
|
* |
43
|
|
|
* @var boolean |
44
|
|
|
*/ |
45
|
|
|
protected $frontmatter = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Instantiates a new Blob object. |
49
|
|
|
* |
50
|
|
|
* @param stdClass $data Raw blob data. |
51
|
|
|
*/ |
52
|
10 |
|
public function __construct( stdClass $data ) { |
53
|
10 |
|
$this->data = $data; |
54
|
|
|
|
55
|
10 |
|
$this->interpret_data(); |
56
|
10 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the raw blob content. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
8 |
|
public function content() { |
64
|
8 |
|
return $this->content; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set's the blob's content. |
69
|
|
|
* |
70
|
|
|
* @param string $content Raw blob content. |
71
|
|
|
* @param bool $base64 Whether the content is base64 encoded. |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
10 |
|
public function set_content( $content, $base64 = false ) { |
76
|
10 |
|
if ( $base64 ) { |
77
|
7 |
|
$content = base64_decode( $content ); |
78
|
7 |
|
} |
79
|
|
|
|
80
|
10 |
|
$this->frontmatter = '---' === substr( $this->content = $content, 0, 3 ) ? true : false; |
81
|
|
|
|
82
|
10 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
/** |
85
|
|
|
* Returns the blob sha. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
8 |
|
public function sha() { |
90
|
8 |
|
return $this->sha; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return's the blob path. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
8 |
|
public function path() { |
99
|
8 |
|
return $this->path; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Whether the blob has frontmatter. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
2 |
|
public function has_frontmatter() { |
108
|
2 |
|
return $this->frontmatter; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the formatted/filtered blob content used for import. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
2 |
|
public function content_import() { |
117
|
2 |
|
$content = $this->content(); |
118
|
|
|
|
119
|
2 |
|
if ( $this->has_frontmatter() ) { |
120
|
|
|
// Break out content. |
121
|
1 |
|
preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches ); |
122
|
1 |
|
$content = array_pop( $matches ); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
2 |
|
if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
126
|
2 |
|
$content = wpmarkdown_markdown_to_html( $content ); |
127
|
2 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Filters the content for import. |
131
|
|
|
*/ |
132
|
2 |
|
return apply_filters( 'wpghs_content_import', trim( $content ) ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the blob meta. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
2 |
|
public function meta() { |
141
|
2 |
|
$meta = array(); |
142
|
|
|
|
143
|
2 |
|
if ( $this->has_frontmatter() ) { |
144
|
|
|
// Break out meta, if present. |
145
|
1 |
|
preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
146
|
1 |
|
array_pop( $matches ); |
147
|
|
|
|
148
|
1 |
|
$meta = spyc_load( $matches[2] ); |
149
|
1 |
|
if ( isset( $meta['permalink'] ) ) { |
150
|
1 |
|
$meta['permalink'] = str_replace( home_url(), '', $meta['permalink'] ); |
151
|
1 |
|
} |
152
|
1 |
|
} |
153
|
|
|
|
154
|
2 |
|
return $meta; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Formats the blob into an API call body. |
159
|
|
|
* |
160
|
|
|
* @return stdClass |
161
|
|
|
*/ |
162
|
5 |
|
public function to_body() { |
163
|
5 |
|
$data = new stdClass; |
164
|
|
|
|
165
|
5 |
|
$data->mode = '100644'; |
166
|
5 |
|
$data->type = 'blob'; |
167
|
|
|
|
168
|
5 |
|
$data->path = $this->path(); |
169
|
|
|
|
170
|
5 |
|
if ( $this->sha() ) { |
171
|
1 |
|
$data->sha = $this->sha(); |
172
|
1 |
|
} else { |
173
|
4 |
|
$data->content = $this->content(); |
174
|
|
|
} |
175
|
|
|
|
176
|
5 |
|
return $data; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Interprets the blob's data into properties. |
181
|
|
|
*/ |
182
|
10 |
|
protected function interpret_data() { |
183
|
10 |
|
$this->sha = isset( $this->data->sha ) ? $this->data->sha : ''; |
184
|
10 |
|
$this->path = isset( $this->data->path ) ? $this->data->path : ''; |
185
|
|
|
|
186
|
10 |
|
$this->set_content( |
187
|
10 |
|
isset( $this->data->content ) ? trim( $this->data->content ) : '', |
188
|
10 |
|
isset( $this->data->encoding ) && 'base64' === $this->data->encoding ? true : false |
189
|
10 |
|
); |
190
|
10 |
|
} |
191
|
|
|
} |
192
|
|
|
|