forked from beezwax/WP-Publish-to-Apple-News
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathclass-exporter.php
377 lines (337 loc) · 9.49 KB
/
class-exporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* Publish to Apple News: \Apple_Exporter\Exporter class
*
* @package Apple_News
* @subpackage Apple_Exporter
*/
namespace Apple_Exporter;
/**
* Export a Exporter_Content instance to Apple format.
*
* NOTE: This class is designed to work outside of WordPress just fine, so it
* can be a dependency. It can be used to create other plugins, for example, a
* Joomla or Drupal plugin. Even though this is not a WordPress class it
* follows its coding conventions.
*
* @author Federico Ramirez
* @since 0.2.0
*/
class Exporter {
/**
* The content object to be exported.
*
* @var Exporter_Content
* @access private
* @since 0.2.0
*/
private $content;
/**
* The workspace object, used to create the bundle.
*
* @var Workspace
* @access private
* @since 0.2.0
*/
private $workspace;
/**
* The settings object which is used to configure the output of the exporter.
*
* @var Settings
* @access private
* @since 0.4.0
*/
private $settings;
/**
* An ordered hash of builders. They will be executed in order when building
* the JSON array.
*
* @var array
* @access private
* @since 0.4.0
*/
private $builders;
/**
* Constructor.
*
* @param Exporter_Content $content The content to export.
* @param Workspace $workspace Optional. The workspace to use.
* @param Settings $settings Optional. Settings to use.
* @access public
*/
public function __construct( $content, $workspace = null, $settings = null ) {
$this->content = $content;
$this->workspace = ! empty( $workspace ) ? $workspace : new Workspace( $this->content_id() );
$this->settings = ! empty( $settings ) ? $settings : new Settings();
$this->builders = [];
}
/**
* An ordered hash of builders. They will be executed in order when building
* the JSON array.
*
* @since 0.4.0
* @param array $builders Optional. Builders to use.
* @access public
*/
public function initialize_builders( $builders = null ) {
if ( $builders ) {
$this->builders = $builders;
} else {
$this->register_builder( 'layout', new Builders\Layout( $this->content, $this->settings ) );
$this->register_builder( 'components', new Builders\Components( $this->content, $this->settings ) );
$this->register_builder( 'componentTextStyles', new Builders\Component_Text_Styles( $this->content, $this->settings ) );
$this->register_builder( 'componentStyles', new Builders\Component_Styles( $this->content, $this->settings ) );
$this->register_builder( 'textStyles', new Builders\Text_Styles( $this->content, $this->settings ) );
$this->register_builder( 'componentLayouts', new Builders\Component_Layouts( $this->content, $this->settings ) );
$this->register_builder( 'metadata', new Builders\Metadata( $this->content, $this->settings ) );
}
Component_Factory::initialize(
$this->workspace,
$this->settings,
$this->get_builder( 'componentTextStyles' ),
$this->get_builder( 'componentLayouts' ),
$this->get_builder( 'componentStyles' )
);
}
/**
* Register a builder.
*
* @param string $name The name of the builder to register.
* @param \Apple_Exporter\Builders\Builder $builder The builder to register.
* @access private
*/
private function register_builder( $name, $builder ) {
$this->builders[ $name ] = $builder;
}
/**
* Get a builder.
*
* @param string $name The name of the builder to fetch.
* @access private
* @return \Apple_Exporter\Builders\Builder The builder matching the provided name.
*/
private function get_builder( $name ) {
return $this->builders[ $name ];
}
/**
* Generates JSON for the article.json file. By doing this, all attachments
* get added to the workspace/tmp directory automatically.
*
* When called, `clean_workspace` must always be called before and
* afterwards.
*
* @since 0.4.0
* @access public
*/
public function generate() {
if ( ! $this->builders ) {
$this->initialize_builders();
}
$this->write_json( $this->generate_json() );
}
/**
* Gets the instance of the current workspace.
*
* @since 0.4.0
* @access public
* @return Workspace The current workspace.
*/
public function workspace() {
return $this->workspace;
}
/**
* Based on the content this instance holds, create an Article Format bundle.
* and return the path.
* This function builds the article and cleans up after.
*
* @access public
* @return string The exported content in JSON format.
*/
public function export() {
// If an export or push was cancelled, the workspace might be polluted.
// Clean beforehand.
$this->clean_workspace();
// Build the bundle content.
$this->generate();
// Some use cases for this function expect it to return the JSON.
$json = $this->get_json();
// Clean after the export action.
$this->clean_workspace();
return $json;
}
/**
* Generate article.json contents. It does so by looping though all data,
* generating valid JSON and adding attachments to workspace/tmp directory.
*
* @access private
* @return string The generated JSON for article.json
*/
private function generate_json() {
/**
* Filters the language of the article.
*
* The language is pulled from the WordPress settings using
* `get_bloginfo( 'language' )`.
*
* @since 1.4.0
*
* @param string $language The language value to be filtered.
* @param int $post_id The ID of the post being exported.
*/
$language = apply_filters(
'apple_news_exporter_language',
get_bloginfo( 'language' ),
$this->content_id()
);
// Base JSON.
$json = [
'version' => '1.11',
'identifier' => 'post-' . $this->content_id(),
'language' => $language,
'title' => wp_strip_all_tags( $this->content_title() ),
];
// Builders.
$json['documentStyle'] = $this->build_article_style();
foreach ( $this->builders as $name => $builder ) {
$arr = $builder->to_array();
if ( $arr ) {
$json[ $name ] = $arr;
}
}
/**
* Filters the final article JSON before being sent to Apple News.
*
* One of the most powerful filters available. This allows you to change
* any aspect of the final JSON right before it is sent to Apple News.
* Some sites use this to replace the content with their own custom
* templates.
*
* @param array $json The PHP array representation of the article JSON before it is encoded into a string.
* @param int $post_id The ID of the post.
*/
$json = apply_filters( 'apple_news_generate_json', $json, $this->content_id() );
// Clean up the data array and convert to JSON format.
self::prepare_for_encoding( $json );
$json = wp_json_encode( $json );
return $json;
}
/**
* Write the JSON output to the Workspace.
*
* @param string $content The content to be written to the workspace.
* @access private
*/
private function write_json( $content ) {
$this->workspace->write_json( $content );
}
/**
* Get the JSON output from the workspace.
*
* @access public
* @return string The JSON from the workspace.
*/
public function get_json() {
return $this->workspace->get_json();
}
/**
* Get the bundles from the workspace.
*
* @access public
* @return array An array of bundles from the workspace.
*/
public function get_bundles() {
return $this->workspace->get_bundles();
}
/**
* Get the errors from the workspace.
*
* @access public
* @return array An array of errors from the workspace.
*/
public function get_errors() {
return $this->workspace->get_errors();
}
/**
* Clean up the current workspace.
*
* @access private
*/
private function clean_workspace() {
$this->workspace->clean_up();
}
/**
* Build the article style.
*
* @access private
* @return array An array containing the background color for the article.
*/
private function build_article_style() {
// Get information about the currently used theme.
$theme = \Apple_Exporter\Theme::get_used();
$conditional = [];
if ( ! empty( $theme->get_value( 'body_background_color_dark' ) ) ) {
$conditional = [
'conditional' => [
'backgroundColor' => $theme->get_value( 'body_background_color_dark' ),
'conditions' => [
'minSpecVersion' => '1.14',
'preferredColorScheme' => 'dark',
],
],
];
}
return array_merge(
[ 'backgroundColor' => $theme->get_value( 'body_background_color' ) ],
$conditional
);
}
/**
* Get the Exporter_Content object
*
* @access public
* @return Exporter_Content The content from the exporter.
*/
public function get_content() {
return $this->content;
}
/**
* Get the content ID.
*
* @access private
* @return int The ID of the post being exported.
*/
private function content_id() {
return $this->content->id();
}
/**
* Get the content title.
*
* @access private
* @return string The title of the content being exported.
*/
private function content_title() {
$title = $this->content->title();
return ! empty( $title ) ? $title : __( 'Untitled Article', 'apple-news' );
}
/**
* Cleans up data destined for JSON conversion.
*
* @param mixed $data The data to clean up.
*
* @access private
*/
public static function prepare_for_encoding( &$data ) {
// If the value is an array, loop through it and process each element.
if ( is_array( $data ) ) {
foreach ( $data as &$datum ) {
self::prepare_for_encoding( $datum );
}
}
// If the value is a string, clean it up.
if ( is_string( $data ) ) {
// Strip hidden white space.
$data = preg_replace( '/\h/u', ' ', $data );
return;
}
}
}