Compare commits

..

5 commits

Author SHA1 Message Date
a626dfce4e added completness check
Some checks failed
PHP_CodeSniffer / phpcs (push) Failing after 3m59s
Unit Testing / phpunit (5.6, 6.2) (push) Failing after 4m24s
Unit Testing / phpunit (7.0) (push) Failing after 4m36s
Unit Testing / phpunit (7.2) (push) Failing after 4m28s
Unit Testing / phpunit (7.3) (push) Failing after 5m23s
Unit Testing / phpunit (7.4) (push) Failing after 5m22s
Unit Testing / phpunit (8.0) (push) Failing after 5m34s
Unit Testing / phpunit (8.1) (push) Failing after 5m35s
Unit Testing / phpunit (8.2) (push) Failing after 5m52s
Unit Testing / phpunit (latest) (push) Failing after 5m21s
2023-12-11 23:22:19 +01:00
5cc9c35811 added null check fro post_meta_time getter 2023-12-11 23:22:07 +01:00
b78bbbe372 better post metadata and property getter 2023-12-11 23:21:47 +01:00
6a51a303f7 added if header 2023-12-11 23:21:20 +01:00
af66737939 added languagemap sugar 2023-12-11 23:21:02 +01:00
2 changed files with 117 additions and 10 deletions

View file

@ -33,6 +33,13 @@ class Base_Object {
*/ */
protected $_skip_next; protected $_skip_next;
/**
* if this is set, the language maps will be set automatically with this language if not set yet todo nicer description
*
* @var string
*/
protected $_default_language_code_for_language_maps;
/** /**
* The object's unique global identifier * The object's unique global identifier
* *
@ -534,11 +541,51 @@ class Base_Object {
return $this; return $this;
} }
if ( 'content' === $key || 'summary' === $key || 'name' === $key ) { // todo nicer?
$this->set_basic_language_map( $key, $value ); // todo skip setter if language map is already set
}
$this->$key = $value; $this->$key = $value;
return $this; return $this;
} }
/**
* Setter for the languageMap default language
*
* @param string $languagecode languageocde for default
*
*/
public function set_default_language_for_maps( $languagecode ) {
$this->_default_language_code_for_language_maps = $languagecode;
return $this;
}
/**
* Generic languageMap setter.
*
* @param string $key The key for the language map.
* @param string $value The value to set.
*
*/
public function set_basic_language_map( $key, $value ) {
// todo better name, implement usage for content, summary and name
$mapkey = $key . '_map';
if ( ! isset( $this->_default_language_code_for_language_maps ) ) {
return new WP_Error( '_default_language_code_for_language_maps must be set before calling set_languageMap', __( '_default_language_code_for_language_maps must be set before calling set_languageMap', 'activitypub' ), array( 'status' => 404 ) );
}
if ( ! $this->has( $mapkey ) ) {
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
}
$this->$mapkey = array(
$this->_default_language_code_for_language_maps => $value,
);
return $this;
}
/** /**
* Generic adder. * Generic adder.
* *
@ -688,10 +735,48 @@ class Base_Object {
return \wp_json_encode( $array, \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT ); return \wp_json_encode( $array, \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
} }
public function if( bool $b ) { /**
* skips next setter if( false );
*
* @param bool $b
*
*/
public function if( $b ) {
$this->_skip_next = ! $b; $this->_skip_next = ! $b;
return $this; return $this;
} }
public function check_jsonld_completeness() {
// Just a debugging helper function
$vars = get_object_vars( $this );
$total_var_count = count( $vars );
$unset_var_array = array();
foreach ( $vars as $key => $value ) {
// ignore all _prefixed keys.
if ( '_' === substr( $key, 0, 1 ) ) {
--$total_var_count;
continue;
}
// if value is empty, try to get it from a getter.
if ( ! $value ) {
$value = call_user_func( array( $this, 'get_' . $key ) );
}
// if value is still empty, ignore it for the array and continue.
if ( ! isset( $value ) ) {
$unset_var_array[] = $key;
}
}
$unset_var_count = count( $unset_var_array );
return array(
'total_var_count' => $total_var_count,
'unset_var_count' => $unset_var_count,
'set_var_count' => $total_var_count - $unset_var_count,
'unset_vars' => $unset_var_array,
);
}
} }

View file

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