-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclass-transformer.php
More file actions
82 lines (70 loc) · 2.18 KB
/
class-transformer.php
File metadata and controls
82 lines (70 loc) · 2.18 KB
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
<?php
namespace DotOrg\TryWordPress;
class Transformer {
public const string META_KEY_LIBERATED_SOURCE = '_data_liberation_source';
public const string META_KEY_LIBERATED_OUTPUT = '_data_liberation_output';
public function __construct() {
TransformersRegistry::add(
SubjectType::BLOGPOST,
array(
'slug' => 'try_wordpress',
'description' => 'Try WordPress handling blog-post natively',
),
array(
$this,
'handler',
)
);
TransformersRegistry::add(
SubjectType::PAGE,
array(
'slug' => 'try_wordpress',
'description' => 'Try WordPress handling page natively',
),
array(
$this,
'handler',
)
);
}
public function get_post_type( Subject $subject ): string {
return match ( $subject->type ) {
'page' => 'page',
default => 'post',
};
}
public function handler( Subject $subject ) {
// Since parsed versions come from paste_handler in frontend, look for them in postmeta, instead of subject instance
$title = get_post_meta( $subject->id(), 'parsed_title', true );
if ( empty( $title ) ) {
$title = '[Title]';
}
$body = get_post_meta( $subject->id(), 'parsed_content', true );
if ( empty( $body ) ) {
$body = '[Body]';
}
$args = array(
'post_author' => $subject->author_id(),
'post_date' => get_post_meta( $subject->id(), 'parsed_date', true ),
'post_content' => $body,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => $this->get_post_type( $subject ),
);
// have we already transformed this subject before?
$transformed_post_id = get_post_meta( $subject->id(), self::META_KEY_LIBERATED_OUTPUT, true );
if ( ! empty( $transformed_post_id ) ) {
$args['ID'] = $transformed_post_id;
}
add_filter( 'wp_insert_post_empty_content', '__return_false' );
$inserted_post_id = wp_insert_post( $args );
remove_filter( 'wp_insert_post_empty_content', '__return_false' );
// @TODO: handle attachments, terms etc in future
// Note: Do not need anything from postmeta.
// We should potentially use another plugin here for this purpose and call its API to do it for us.
if ( 0 === $inserted_post_id ) {
return null;
}
return $inserted_post_id;
}
}