better post metadata and property getter

This commit is contained in:
ruru4143 2023-12-11 23:21:47 +01:00
parent 6a51a303f7
commit b78bbbe372

View file

@ -78,21 +78,40 @@ abstract class Base { // todo renmae to Base_tranformer
/**
* Retrieves a post meta field for the configured post.
*
* @param string $key Optional. The meta key to retrieve. By default,
* returns data for all keys. Default empty.
* @param string $key The meta key to retrieve. By default,
* returns data for all keys.
* @param bool $single Optional. Whether to return a single value.
* This parameter has no effect if `$key` is not specified.
* Default false.
* @param mixed $default_value Optional. if the key doesn't exists, it returns the default value.
* Default null.
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$post_id` (non-numeric, zero, or negative value).
* An empty string if a valid but non-existing post ID is passed.
*/
public function get_post_meta( string $key = '', $single = false, $default = null ) {
// todo test what if not present
// empty string or empty array, then return default
public function get_post_meta( $key, $single = false, $default_value = null ) {
if ( \metadata_exists( 'post', $this->wp_post->ID, $key ) ) {
return \get_post_meta( $this->wp_post->ID, $key, $single );
}
return $default_value;
}
/**
* Retrieves a post property field for the configured post.
*
* @param string $key The property key to retrieve. By default,
* returns data for all keys.
* @param mixed $default_value Optional. if the key doesn't exists, it returns the default value.
* Default null.
* @return mixed The value of the property.
*/
public function get_post_property( $key, $default_value = null ) {
if ( isset( $this->wp_post->$key ) ) {
return $this->wp_post->$key;
}
return $default_value;
}
/**
* Retrieves a post meta field for the configured post.