-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclass-subject.php
69 lines (56 loc) · 1.8 KB
/
class-subject.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
<?php
namespace DotOrg\TryWordPress;
use WP_Post;
/**
* Subject class offers an easy view of liberated data, abstracting away the implementation details
*
* WP specific data is private and only exposed via methods to limit leakage of implementation details
* Raw data is available as public fields
*/
class Subject {
private int $id;
private int $author_id;
public string $source_html;
public string $source_url;
public string $type;
public string $title;
public string $date;
public string $content;
/**
* Creates a new Subject instance from a WordPress post.
*
* @param int $post_id The WordPress post ID to create the subject from.
* @return Subject|false The Subject instance or false if the post doesn't exist.
*/
public static function from_post( int $post_id ): Subject|false {
$post = get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return false;
}
return new self( $post );
}
/**
* Private constructor to enforce using the factory method.
*
* @param WP_Post $post The WordPress post to create the subject from.
*/
private function __construct( WP_Post $post ) {
$this->id = $post->ID;
$this->author_id = $post->post_author;
$this->source_html = $post->post_content_filtered;
$this->source_url = $post->guid;
$this->type = get_post_meta( $post->ID, 'subject_type', true );
$this->title = get_post_meta( $post->ID, 'raw_title', true );
$this->date = get_post_meta( $post->ID, 'raw_date', true );
$this->content = get_post_meta( $post->ID, 'raw_content', true );
}
public function id(): int {
return $this->id;
}
public function author_id(): int {
return $this->author_id;
}
public function transformed_post_id(): int {
return absint( get_post_meta( $this->id, Transformer::META_KEY_LIBERATED_OUTPUT, true ) );
}
}