New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract meta CRUD into methods #174

Merged
merged 8 commits into from May 15, 2018
Next

Abstract meta CRUD, into methods

  • Loading branch information...
Jonny Harris
Jonny Harris committed Apr 16, 2018
commit df1a16ae6638e5882f31bfe2fa0ad16ac172d480
@@ -24,6 +24,23 @@
class Comment_Meta_Command extends \WP_CLI\CommandWithMeta {
protected $meta_type = 'comment';

This comment has been minimized.

@schlessera

schlessera Apr 16, 2018

Member

This should only have 1 blank line here.

@schlessera

schlessera Apr 16, 2018

Member

This should only have 1 blank line here.

protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_comment_meta( $object_id, $meta_key, $meta_value, $unique );
}
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_comment_meta( $object_id, $meta_key, $meta_value, $prev_value );
}
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_comment_meta( $object_id, $meta_key, $single );
}
protected function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
return delete_comment_meta( $object_id, $meta_key, $meta_value, $delete_all );
}
/**
* Check that the comment ID exists
*
@@ -35,4 +52,3 @@ protected function check_object_id( $object_id ) {
return $comment->comment_ID;
}
}
View
@@ -34,4 +34,20 @@ protected function check_object_id( $object_id ) {
$post = $fetcher->get_check( $object_id );
return $post->ID;
}
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_post_meta( $object_id, $meta_key, $meta_value, $unique );
}
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_post_meta( $object_id, $meta_key, $meta_value, $prev_value );
}
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_post_meta( $object_id, $meta_key, $single );
}
protected function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
return delete_post_meta( $object_id, $meta_key, $meta_value, $delete_all );
}
}
View
@@ -37,4 +37,20 @@ protected function check_object_id( $object_id ) {
return $term->term_id;
}
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_term_meta( $object_id, $meta_key, $meta_value, $unique );
}
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_term_meta( $object_id, $meta_key, $meta_value, $prev_value );
}
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_term_meta( $object_id, $meta_key, $single );
}
protected function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
return delete_term_meta( $object_id, $meta_key, $meta_value, $delete_all );
}

This comment has been minimized.

@schlessera

schlessera Apr 16, 2018

Member

Redundant blank line here.

@schlessera

schlessera Apr 16, 2018

Member

Redundant blank line here.

}
View
@@ -226,6 +226,22 @@ public function update( $args, $assoc_args ) {
parent::update( $args, $assoc_args );
}
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_user_meta( $object_id, $meta_key, $meta_value, $unique );
}
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_user_meta( $object_id, $meta_key, $meta_value, $prev_value );
}
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_user_meta( $object_id, $meta_key, $single );
}
protected function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
return delete_user_meta( $object_id, $meta_key, $meta_value, $delete_all );
}
/**
* Replaces user_login value with user ID
* user meta is a special case that also supports user_login
@@ -1,4 +1,4 @@
<?php
$this->update_metadata( <?php

This comment has been minimized.

@schlessera

schlessera Apr 16, 2018

Member

There's nonsense up here... ;)

@schlessera

schlessera Apr 16, 2018

Member

There's nonsense up here... ;)

namespace WP_CLI;
@@ -68,7 +68,7 @@ public function list_( $args, $assoc_args ) {
$object_id = $this->check_object_id( $object_id );
$metadata = get_metadata( $this->meta_type, $object_id );
$metadata = $this->get_metadata( $object_id );
if ( ! $metadata ) {
$metadata = array();
}
@@ -147,7 +147,7 @@ public function get( $args, $assoc_args ) {
$object_id = $this->check_object_id( $object_id );
$value = get_metadata( $this->meta_type, $object_id, $meta_key, true );
$value = $this->get_metadata( $object_id, $meta_key, true );
if ( '' === $value )
die(1);
@@ -186,8 +186,8 @@ public function delete( $args, $assoc_args ) {
if ( Utils\get_flag_value( $assoc_args, 'all' ) ) {
$errors = false;
foreach( get_metadata( $this->meta_type, $object_id ) as $meta_key => $values ) {
$success = delete_metadata( $this->meta_type, $object_id, $meta_key );
foreach( $this->get_metadata( $object_id ) as $meta_key => $values ) {
$success = $this->delete_metadata( $object_id, $meta_key );
if ( $success ) {
WP_CLI::log( "Deleted '{$meta_key}' custom field." );
} else {
@@ -201,7 +201,7 @@ public function delete( $args, $assoc_args ) {
WP_CLI::success( 'Deleted all custom fields.' );
}
} else {
$success = delete_metadata( $this->meta_type, $object_id, $meta_key, $meta_value );
$success = $this->delete_metadata( $object_id, $meta_key, $meta_value );
if ( $success ) {
WP_CLI::success( "Deleted custom field." );
} else {
@@ -242,7 +242,7 @@ public function add( $args, $assoc_args ) {
$object_id = $this->check_object_id( $object_id );
$meta_value = wp_slash( $meta_value );
$success = add_metadata( $this->meta_type, $object_id, $meta_key, $meta_value );
$success = $this->add_metadata( $object_id, $meta_key, $meta_value );
if ( $success ) {
WP_CLI::success( "Added custom field." );
@@ -285,13 +285,13 @@ public function update( $args, $assoc_args ) {
$object_id = $this->check_object_id( $object_id );
$meta_value = sanitize_meta( $meta_key, $meta_value, $this->meta_type );
$old_value = sanitize_meta( $meta_key, get_metadata( $this->meta_type, $object_id, $meta_key, true ), $this->meta_type );
$old_value = sanitize_meta( $meta_key, $this->get_metadata( $object_id, $meta_key, true ), $this->meta_type );
if ( $meta_value === $old_value ) {
WP_CLI::success( "Value passed for custom field '$meta_key' is unchanged." );
} else {
$meta_value = wp_slash( $meta_value );
$success = update_metadata( $this->meta_type, $object_id, $meta_key, $meta_value );
$success = $this->update_metadata( $object_id, $meta_key, $meta_value );
if ( $success ) {
WP_CLI::success( "Updated custom field '$meta_key'." );
@@ -336,7 +336,7 @@ public function pluck( $args, $assoc_args ) {
return $key;
}, array_slice( $args, 2 ) );
$value = get_metadata( $this->meta_type, $object_id, $meta_key, true );
$value = $this->get_metadata( $object_id, $meta_key, true );
$traverser = new RecursiveDataStructureTraverser( $value );
@@ -405,7 +405,7 @@ public function patch( $args, $assoc_args ) {
}
/* Need to make a copy of $current_meta_value here as it is modified by reference */
$current_meta_value = $old_meta_value = sanitize_meta( $meta_key, get_metadata( $this->meta_type, $object_id, $meta_key, true ), $this->meta_type );
$current_meta_value = $old_meta_value = sanitize_meta( $meta_key, $this->get_metadata( $object_id, $meta_key, true ), $this->meta_type );
if ( is_object( $current_meta_value ) ) {
$old_meta_value = clone $current_meta_value;
}
@@ -424,7 +424,7 @@ public function patch( $args, $assoc_args ) {
WP_CLI::success( "Value passed for custom field '$meta_key' is unchanged." );
} else {
$slashed = wp_slash( $patched_meta_value );
$success = update_metadata( $this->meta_type, $object_id, $meta_key, $slashed );
$success = $this->update_metadata( $object_id, $meta_key, $slashed );
if ( $success ) {
WP_CLI::success( "Updated custom field '$meta_key'." );
@@ -434,6 +434,22 @@ public function patch( $args, $assoc_args ) {
}
}
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $unique );
}
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $prev_value );
}
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_metadata( $this->meta_type, $object_id, $meta_key, $single );
}
protected function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
return delete_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $delete_all );
}
/**
* Get the fields for this object's meta
*
ProTip! Use n and p to navigate between commits in a pull request.