-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject_Metadata.php
More file actions
139 lines (116 loc) · 4.24 KB
/
Copy pathObject_Metadata.php
File metadata and controls
139 lines (116 loc) · 4.24 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
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
<?php
/**
* Object_Metadata class file
*
* @package mantle-framework
*/
namespace Mantle\Support;
use ArrayAccess;
use InvalidArgumentException;
use Mantle\Contracts\Support\Jsonable;
/**
* Fluent class for retrieving object meta data as type-safe objects.
*
* Supports all meta data objects in WordPress: posts, terms, users, and comments.
*/
class Object_Metadata implements ArrayAccess, Jsonable, \JsonSerializable, \Stringable {
use Interacts_With_Data;
/**
* Retrieve metadata from the database.
*
* @param string $meta_type Meta type.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $default Default value. Default is null.
*/
public static function of( string $meta_type, int $object_id, string $meta_key, mixed $default = null ): static {
$value = get_metadata( $meta_type, $object_id, $meta_key, true );
if ( '' === $value ) {
$value = $default;
}
return new static( $meta_type, $object_id, $meta_key, $value );
}
/**
* Create a new instance of the class.
*
* @param mixed $value Value.
*/
public static function create( mixed $value ): static {
return new static( null, null, null, $value );
}
/**
* Constructor.
*
* @param string|null $meta_type Meta type.
* @param int|null $object_id Object ID.
* @param string|null $meta_key Meta key.
* @param mixed $value Meta value.
* @param bool $throw Whether to throw an exception if the metadata is not a compatible type.
*/
public function __construct( protected readonly ?string $meta_type, protected readonly ?int $object_id, protected readonly ?string $meta_key, mixed $value, bool $throw = false ) {
$this->throw = $throw;
$this->value = $value;
}
/**
* Save the meta data.
*
* @throws InvalidArgumentException If the object is retrieving sub-property of a metadata and the meta type is not passed.
*/
public function save(): static {
if ( ! $this->meta_type || ! $this->object_id || ! $this->meta_key ) {
throw new InvalidArgumentException( 'Unable to save sub-property of a metadata.' );
}
update_metadata( $this->meta_type, $this->object_id, $this->meta_key, $this->value );
$this->value = get_metadata( $this->meta_type, $this->object_id, $this->meta_key, true );
return $this;
}
/**
* Add the meta data.
*
* @throws InvalidArgumentException If the object is retrieving sub-property of a metadata and the meta type is not passed.
*/
public function add(): static {
if ( ! $this->meta_type || ! $this->object_id || ! $this->meta_key ) {
throw new InvalidArgumentException( 'Unable to add sub-property of a metadata.' );
}
add_metadata( $this->meta_type, $this->object_id, $this->meta_key, $this->value );
$this->value = get_metadata( $this->meta_type, $this->object_id, $this->meta_key, true );
return $this;
}
/**
* Delete the meta data.
*
* @throws InvalidArgumentException If the object is a sub-property of a metadata.
*/
public function delete(): void {
if ( ! $this->meta_type || ! $this->object_id || ! $this->meta_key ) {
throw new InvalidArgumentException( 'Unable to delete meta on a sub-property of a metadata.' );
}
delete_metadata( $this->meta_type, $this->object_id, $this->meta_key );
}
/**
* Delete a specific meta key that matches the value.
*
* @param mixed $value Value to delete.
*
* @throws InvalidArgumentException If the object is a sub-property of a metadata.
*/
public function delete_value( mixed $value ): void {
if ( ! $this->meta_type || ! $this->object_id || ! $this->meta_key ) {
throw new InvalidArgumentException( 'Unable to delete meta on a sub-property of a metadata.' );
}
delete_metadata( $this->meta_type, $this->object_id, $this->meta_key, $value );
}
/**
* Fetch all meta values for the given meta.
*
* @throws InvalidArgumentException If the object is a sub-property of a metadata.
*/
public function all(): static {
if ( ! $this->meta_type || ! $this->object_id || ! $this->meta_key ) {
throw new InvalidArgumentException( 'Unable to fetch all meta on a sub-property of a metadata.' );
}
$value = get_metadata( $this->meta_type, $this->object_id, $this->meta_key );
return new static( $this->meta_type, $this->object_id, $this->meta_key, $value, $this->throw );
}
}