-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclass-ops.php
More file actions
65 lines (57 loc) · 1.9 KB
/
class-ops.php
File metadata and controls
65 lines (57 loc) · 1.9 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
<?php
namespace DotOrg\TryWordPress;
use WP_Post;
class Ops {
private static string $post_type;
public static function init( string $post_type ): void {
static::$post_type = $post_type;
}
/**
* Register your handler for the specified subject type
*
* @param SubjectType $subject_type Type of subject.
* @param array $identifier Array containing unique slug and description.
* @param callable $handler Function that would handle the transformation of subject for the specific subject type.
* @return void
*/
public static function handle( SubjectType $subject_type, array $identifier, callable $handler ): void {
Handlers_Registry::add( $subject_type, $identifier, $handler );
}
/**
* Register your handler for the specified subject type to just observe (read-only) data
*
* @param SubjectType $subject_type Type of subject.
* @param array $identifier Array containing unique slug and description.
* @param callable $handler Function that would handle the transformation of subject for the specific subject type.
* @return void
*/
public static function observe( SubjectType $subject_type, array $identifier, callable $handler ): void {
Observers_Registry::add( $subject_type, $identifier, $handler );
}
/**
* Loops over all liberated_post posts for the specified subject_type
*
* @TODO: pagination support
*
* @param SubjectType $subject_type Type of subject.
* @return Subject[]
*/
public static function loop( SubjectType $subject_type ): array {
$args = array(
'post_type' => static::$post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
// @phpcs:ignore
'meta_query' => array(
'key' => 'subject_type',
'value' => $subject_type->value,
'compare' => '=',
),
);
$posts = get_posts( $args );
return array_map(
fn( WP_Post $post ) => Subject::from_post( $post->ID ),
$posts
);
}
}