Skip to content

Commit

Permalink
Merge pull request #174 from spacedmonkey/functions-meta
Browse files Browse the repository at this point in the history
Abstract meta CRUD into methods
  • Loading branch information
schlessera authored May 15, 2018
2 parents cf1741d + 3e480f4 commit d44e0bf
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 12 deletions.
75 changes: 74 additions & 1 deletion src/Comment_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,80 @@
class Comment_Meta_Command extends \WP_CLI\CommandWithMeta {
protected $meta_type = 'comment';

/**
* Wrapper method for add_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param bool $unique Optional, default is false. Whether the
* specified metadata key should be unique for the
* object. If true, and the object already has a
* value for the specified metadata key, no change
* will be made.
*
* @return int|false The meta ID on success, false on failure.
*/
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_comment_meta( $object_id, $meta_key, $meta_value, $unique );
}

/**
* Wrapper method for update_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return int|bool Meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_comment_meta( $object_id, $meta_key, $meta_value, $prev_value );
}

/**
* Wrapper method for get_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified,
* retrieve all metadata for the specified object.
* @param bool $single Optional, default is false. If true, return only
* the first value of the specified meta_key. This
* parameter has no effect if meta_key is not
* specified.
*
* @return mixed Single metadata value, or array of values.
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_comment_meta( $object_id, $meta_key, $single );
}

/**
* Wrapper method for delete_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param mixed $meta_value Optional. Metadata value. Must be serializable
* if non-scalar. If specified, only delete
* metadata entries with this value. Otherwise,
* delete all entries with the specified meta_key.
* Pass `null, `false`, or an empty string to skip
* this check. For backward compatibility, it is
* not possible to pass an empty string to delete
* those entries with an empty string for a value.
*
* @return bool True on successful delete, false on failure.
*/
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
return delete_comment_meta( $object_id, $meta_key, $meta_value );
}

/**
* Check that the comment ID exists
*
Expand All @@ -35,4 +109,3 @@ protected function check_object_id( $object_id ) {
return $comment->comment_ID;
}
}

74 changes: 74 additions & 0 deletions src/Post_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,78 @@ protected function check_object_id( $object_id ) {
$post = $fetcher->get_check( $object_id );
return $post->ID;
}

/**
* Wrapper method for add_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param bool $unique Optional, default is false. Whether the
* specified metadata key should be unique for the
* object. If true, and the object already has a
* value for the specified metadata key, no change
* will be made.
*
* @return int|false The meta ID on success, false on failure.
*/
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_post_meta( $object_id, $meta_key, $meta_value, $unique );
}

/**
* Wrapper method for update_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return int|bool Meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_post_meta( $object_id, $meta_key, $meta_value, $prev_value );
}

/**
* Wrapper method for get_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified,
* retrieve all metadata for the specified object.
* @param bool $single Optional, default is false. If true, return only
* the first value of the specified meta_key. This
* parameter has no effect if meta_key is not
* specified.
*
* @return mixed Single metadata value, or array of values.
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_post_meta( $object_id, $meta_key, $single );
}

/**
* Wrapper method for delete_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param mixed $meta_value Optional. Metadata value. Must be serializable
* if non-scalar. If specified, only delete
* metadata entries with this value. Otherwise,
* delete all entries with the specified meta_key.
* Pass `null, `false`, or an empty string to skip
* this check. For backward compatibility, it is
* not possible to pass an empty string to delete
* those entries with an empty string for a value.
*
* @return bool True on successful delete, false on failure.
*/
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
return delete_post_meta( $object_id, $meta_key, $meta_value );
}
}
73 changes: 73 additions & 0 deletions src/Term_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,77 @@ protected function check_object_id( $object_id ) {
return $term->term_id;
}

/**
* Wrapper method for add_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param bool $unique Optional, default is false. Whether the
* specified metadata key should be unique for the
* object. If true, and the object already has a
* value for the specified metadata key, no change
* will be made.
*
* @return int|false The meta ID on success, false on failure.
*/
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_term_meta( $object_id, $meta_key, $meta_value, $unique );
}

/**
* Wrapper method for update_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return int|bool Meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_term_meta( $object_id, $meta_key, $meta_value, $prev_value );
}

/**
* Wrapper method for get_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified,
* retrieve all metadata for the specified object.
* @param bool $single Optional, default is false. If true, return only
* the first value of the specified meta_key. This
* parameter has no effect if meta_key is not
* specified.
*
* @return mixed Single metadata value, or array of values.
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_term_meta( $object_id, $meta_key, $single );
}

/**
* Wrapper method for delete_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param mixed $meta_value Optional. Metadata value. Must be serializable
* if non-scalar. If specified, only delete
* metadata entries with this value. Otherwise,
* delete all entries with the specified meta_key.
* Pass `null, `false`, or an empty string to skip
* this check. For backward compatibility, it is
* not possible to pass an empty string to delete
* those entries with an empty string for a value.
*
* @return bool True on successful delete, false on failure.
*/
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
return delete_term_meta( $object_id, $meta_key, $meta_value );
}
}
74 changes: 74 additions & 0 deletions src/User_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,80 @@ public function update( $args, $assoc_args ) {
parent::update( $args, $assoc_args );
}

/**
* Wrapper method for add_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param bool $unique Optional, default is false. Whether the
* specified metadata key should be unique for the
* object. If true, and the object already has a
* value for the specified metadata key, no change
* will be made.
*
* @return int|false The meta ID on success, false on failure.
*/
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_user_meta( $object_id, $meta_key, $meta_value, $unique );
}

/**
* Wrapper method for update_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return int|bool Meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_user_meta( $object_id, $meta_key, $meta_value, $prev_value );
}

/**
* Wrapper method for get_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified,
* retrieve all metadata for the specified object.
* @param bool $single Optional, default is false. If true, return only
* the first value of the specified meta_key. This
* parameter has no effect if meta_key is not
* specified.
*
* @return mixed Single metadata value, or array of values.
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_user_meta( $object_id, $meta_key, $single );
}

/**
* Wrapper method for delete_metadata that can be overridden in sub classes.
*
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param mixed $meta_value Optional. Metadata value. Must be serializable
* if non-scalar. If specified, only delete
* metadata entries with this value. Otherwise,
* delete all entries with the specified meta_key.
* Pass `null, `false`, or an empty string to skip
* this check. For backward compatibility, it is
* not possible to pass an empty string to delete
* those entries with an empty string for a value.
*
* @return bool True on successful delete, false on failure.
*/
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
return delete_user_meta( $object_id, $meta_key, $meta_value );
}

/**
* Replaces user_login value with user ID
* user meta is a special case that also supports user_login
Expand Down
Loading

0 comments on commit d44e0bf

Please sign in to comment.