Skip to content

Commit d44e0bf

Browse files
authored
Merge pull request #174 from spacedmonkey/functions-meta
Abstract meta CRUD into methods
2 parents cf1741d + 3e480f4 commit d44e0bf

File tree

5 files changed

+380
-12
lines changed

5 files changed

+380
-12
lines changed

src/Comment_Meta_Command.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,80 @@
2424
class Comment_Meta_Command extends \WP_CLI\CommandWithMeta {
2525
protected $meta_type = 'comment';
2626

27+
/**
28+
* Wrapper method for add_metadata that can be overridden in sub classes.
29+
*
30+
* @param int $object_id ID of the object the metadata is for.
31+
* @param string $meta_key Metadata key to use.
32+
* @param mixed $meta_value Metadata value. Must be serializable if
33+
* non-scalar.
34+
* @param bool $unique Optional, default is false. Whether the
35+
* specified metadata key should be unique for the
36+
* object. If true, and the object already has a
37+
* value for the specified metadata key, no change
38+
* will be made.
39+
*
40+
* @return int|false The meta ID on success, false on failure.
41+
*/
42+
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
43+
return add_comment_meta( $object_id, $meta_key, $meta_value, $unique );
44+
}
45+
46+
/**
47+
* Wrapper method for update_metadata that can be overridden in sub classes.
48+
*
49+
* @param int $object_id ID of the object the metadata is for.
50+
* @param string $meta_key Metadata key to use.
51+
* @param mixed $meta_value Metadata value. Must be serializable if
52+
* non-scalar.
53+
* @param mixed $prev_value Optional. If specified, only update existing
54+
* metadata entries with the specified value.
55+
* Otherwise, update all entries.
56+
*
57+
* @return int|bool Meta ID if the key didn't exist, true on successful
58+
* update, false on failure.
59+
*/
60+
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
61+
return update_comment_meta( $object_id, $meta_key, $meta_value, $prev_value );
62+
}
63+
64+
/**
65+
* Wrapper method for get_metadata that can be overridden in sub classes.
66+
*
67+
* @param int $object_id ID of the object the metadata is for.
68+
* @param string $meta_key Optional. Metadata key. If not specified,
69+
* retrieve all metadata for the specified object.
70+
* @param bool $single Optional, default is false. If true, return only
71+
* the first value of the specified meta_key. This
72+
* parameter has no effect if meta_key is not
73+
* specified.
74+
*
75+
* @return mixed Single metadata value, or array of values.
76+
*/
77+
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
78+
return get_comment_meta( $object_id, $meta_key, $single );
79+
}
80+
81+
/**
82+
* Wrapper method for delete_metadata that can be overridden in sub classes.
83+
*
84+
* @param int $object_id ID of the object metadata is for
85+
* @param string $meta_key Metadata key
86+
* @param mixed $meta_value Optional. Metadata value. Must be serializable
87+
* if non-scalar. If specified, only delete
88+
* metadata entries with this value. Otherwise,
89+
* delete all entries with the specified meta_key.
90+
* Pass `null, `false`, or an empty string to skip
91+
* this check. For backward compatibility, it is
92+
* not possible to pass an empty string to delete
93+
* those entries with an empty string for a value.
94+
*
95+
* @return bool True on successful delete, false on failure.
96+
*/
97+
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
98+
return delete_comment_meta( $object_id, $meta_key, $meta_value );
99+
}
100+
27101
/**
28102
* Check that the comment ID exists
29103
*
@@ -35,4 +109,3 @@ protected function check_object_id( $object_id ) {
35109
return $comment->comment_ID;
36110
}
37111
}
38-

src/Post_Meta_Command.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,78 @@ protected function check_object_id( $object_id ) {
3434
$post = $fetcher->get_check( $object_id );
3535
return $post->ID;
3636
}
37+
38+
/**
39+
* Wrapper method for add_metadata that can be overridden in sub classes.
40+
*
41+
* @param int $object_id ID of the object the metadata is for.
42+
* @param string $meta_key Metadata key to use.
43+
* @param mixed $meta_value Metadata value. Must be serializable if
44+
* non-scalar.
45+
* @param bool $unique Optional, default is false. Whether the
46+
* specified metadata key should be unique for the
47+
* object. If true, and the object already has a
48+
* value for the specified metadata key, no change
49+
* will be made.
50+
*
51+
* @return int|false The meta ID on success, false on failure.
52+
*/
53+
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
54+
return add_post_meta( $object_id, $meta_key, $meta_value, $unique );
55+
}
56+
57+
/**
58+
* Wrapper method for update_metadata that can be overridden in sub classes.
59+
*
60+
* @param int $object_id ID of the object the metadata is for.
61+
* @param string $meta_key Metadata key to use.
62+
* @param mixed $meta_value Metadata value. Must be serializable if
63+
* non-scalar.
64+
* @param mixed $prev_value Optional. If specified, only update existing
65+
* metadata entries with the specified value.
66+
* Otherwise, update all entries.
67+
*
68+
* @return int|bool Meta ID if the key didn't exist, true on successful
69+
* update, false on failure.
70+
*/
71+
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
72+
return update_post_meta( $object_id, $meta_key, $meta_value, $prev_value );
73+
}
74+
75+
/**
76+
* Wrapper method for get_metadata that can be overridden in sub classes.
77+
*
78+
* @param int $object_id ID of the object the metadata is for.
79+
* @param string $meta_key Optional. Metadata key. If not specified,
80+
* retrieve all metadata for the specified object.
81+
* @param bool $single Optional, default is false. If true, return only
82+
* the first value of the specified meta_key. This
83+
* parameter has no effect if meta_key is not
84+
* specified.
85+
*
86+
* @return mixed Single metadata value, or array of values.
87+
*/
88+
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
89+
return get_post_meta( $object_id, $meta_key, $single );
90+
}
91+
92+
/**
93+
* Wrapper method for delete_metadata that can be overridden in sub classes.
94+
*
95+
* @param int $object_id ID of the object metadata is for
96+
* @param string $meta_key Metadata key
97+
* @param mixed $meta_value Optional. Metadata value. Must be serializable
98+
* if non-scalar. If specified, only delete
99+
* metadata entries with this value. Otherwise,
100+
* delete all entries with the specified meta_key.
101+
* Pass `null, `false`, or an empty string to skip
102+
* this check. For backward compatibility, it is
103+
* not possible to pass an empty string to delete
104+
* those entries with an empty string for a value.
105+
*
106+
* @return bool True on successful delete, false on failure.
107+
*/
108+
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
109+
return delete_post_meta( $object_id, $meta_key, $meta_value );
110+
}
37111
}

src/Term_Meta_Command.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,77 @@ protected function check_object_id( $object_id ) {
3737
return $term->term_id;
3838
}
3939

40+
/**
41+
* Wrapper method for add_metadata that can be overridden in sub classes.
42+
*
43+
* @param int $object_id ID of the object the metadata is for.
44+
* @param string $meta_key Metadata key to use.
45+
* @param mixed $meta_value Metadata value. Must be serializable if
46+
* non-scalar.
47+
* @param bool $unique Optional, default is false. Whether the
48+
* specified metadata key should be unique for the
49+
* object. If true, and the object already has a
50+
* value for the specified metadata key, no change
51+
* will be made.
52+
*
53+
* @return int|false The meta ID on success, false on failure.
54+
*/
55+
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
56+
return add_term_meta( $object_id, $meta_key, $meta_value, $unique );
57+
}
58+
59+
/**
60+
* Wrapper method for update_metadata that can be overridden in sub classes.
61+
*
62+
* @param int $object_id ID of the object the metadata is for.
63+
* @param string $meta_key Metadata key to use.
64+
* @param mixed $meta_value Metadata value. Must be serializable if
65+
* non-scalar.
66+
* @param mixed $prev_value Optional. If specified, only update existing
67+
* metadata entries with the specified value.
68+
* Otherwise, update all entries.
69+
*
70+
* @return int|bool Meta ID if the key didn't exist, true on successful
71+
* update, false on failure.
72+
*/
73+
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
74+
return update_term_meta( $object_id, $meta_key, $meta_value, $prev_value );
75+
}
76+
77+
/**
78+
* Wrapper method for get_metadata that can be overridden in sub classes.
79+
*
80+
* @param int $object_id ID of the object the metadata is for.
81+
* @param string $meta_key Optional. Metadata key. If not specified,
82+
* retrieve all metadata for the specified object.
83+
* @param bool $single Optional, default is false. If true, return only
84+
* the first value of the specified meta_key. This
85+
* parameter has no effect if meta_key is not
86+
* specified.
87+
*
88+
* @return mixed Single metadata value, or array of values.
89+
*/
90+
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
91+
return get_term_meta( $object_id, $meta_key, $single );
92+
}
93+
94+
/**
95+
* Wrapper method for delete_metadata that can be overridden in sub classes.
96+
*
97+
* @param int $object_id ID of the object metadata is for
98+
* @param string $meta_key Metadata key
99+
* @param mixed $meta_value Optional. Metadata value. Must be serializable
100+
* if non-scalar. If specified, only delete
101+
* metadata entries with this value. Otherwise,
102+
* delete all entries with the specified meta_key.
103+
* Pass `null, `false`, or an empty string to skip
104+
* this check. For backward compatibility, it is
105+
* not possible to pass an empty string to delete
106+
* those entries with an empty string for a value.
107+
*
108+
* @return bool True on successful delete, false on failure.
109+
*/
110+
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
111+
return delete_term_meta( $object_id, $meta_key, $meta_value );
112+
}
40113
}

src/User_Meta_Command.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,80 @@ public function update( $args, $assoc_args ) {
226226
parent::update( $args, $assoc_args );
227227
}
228228

229+
/**
230+
* Wrapper method for add_metadata that can be overridden in sub classes.
231+
*
232+
* @param int $object_id ID of the object the metadata is for.
233+
* @param string $meta_key Metadata key to use.
234+
* @param mixed $meta_value Metadata value. Must be serializable if
235+
* non-scalar.
236+
* @param bool $unique Optional, default is false. Whether the
237+
* specified metadata key should be unique for the
238+
* object. If true, and the object already has a
239+
* value for the specified metadata key, no change
240+
* will be made.
241+
*
242+
* @return int|false The meta ID on success, false on failure.
243+
*/
244+
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
245+
return add_user_meta( $object_id, $meta_key, $meta_value, $unique );
246+
}
247+
248+
/**
249+
* Wrapper method for update_metadata that can be overridden in sub classes.
250+
*
251+
* @param int $object_id ID of the object the metadata is for.
252+
* @param string $meta_key Metadata key to use.
253+
* @param mixed $meta_value Metadata value. Must be serializable if
254+
* non-scalar.
255+
* @param mixed $prev_value Optional. If specified, only update existing
256+
* metadata entries with the specified value.
257+
* Otherwise, update all entries.
258+
*
259+
* @return int|bool Meta ID if the key didn't exist, true on successful
260+
* update, false on failure.
261+
*/
262+
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
263+
return update_user_meta( $object_id, $meta_key, $meta_value, $prev_value );
264+
}
265+
266+
/**
267+
* Wrapper method for get_metadata that can be overridden in sub classes.
268+
*
269+
* @param int $object_id ID of the object the metadata is for.
270+
* @param string $meta_key Optional. Metadata key. If not specified,
271+
* retrieve all metadata for the specified object.
272+
* @param bool $single Optional, default is false. If true, return only
273+
* the first value of the specified meta_key. This
274+
* parameter has no effect if meta_key is not
275+
* specified.
276+
*
277+
* @return mixed Single metadata value, or array of values.
278+
*/
279+
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
280+
return get_user_meta( $object_id, $meta_key, $single );
281+
}
282+
283+
/**
284+
* Wrapper method for delete_metadata that can be overridden in sub classes.
285+
*
286+
* @param int $object_id ID of the object metadata is for
287+
* @param string $meta_key Metadata key
288+
* @param mixed $meta_value Optional. Metadata value. Must be serializable
289+
* if non-scalar. If specified, only delete
290+
* metadata entries with this value. Otherwise,
291+
* delete all entries with the specified meta_key.
292+
* Pass `null, `false`, or an empty string to skip
293+
* this check. For backward compatibility, it is
294+
* not possible to pass an empty string to delete
295+
* those entries with an empty string for a value.
296+
*
297+
* @return bool True on successful delete, false on failure.
298+
*/
299+
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
300+
return delete_user_meta( $object_id, $meta_key, $meta_value );
301+
}
302+
229303
/**
230304
* Replaces user_login value with user ID
231305
* user meta is a special case that also supports user_login

0 commit comments

Comments
 (0)