Compare commits

..

No commits in common. "a626dfce4e438abf73f0686b615dd8f0b4a11e50" and "6cbe66d121695eafab9aa83807b3f62830c5148d" have entirely different histories.

2 changed files with 10 additions and 117 deletions

View file

@ -33,13 +33,6 @@ class Base_Object {
*/
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
*
@ -541,51 +534,11 @@ class Base_Object {
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;
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.
*
@ -735,48 +688,10 @@ class Base_Object {
return \wp_json_encode( $array, \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
}
/**
* skips next setter if( false );
*
* @param bool $b
*
*/
public function if( $b ) {
public function if( bool $b ) {
$this->_skip_next = ! $b;
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,39 +78,20 @@ abstract class Base { // todo renmae to Base_tranformer
/**
* Retrieves a post meta field for the configured post.
*
* @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.
* @param string $key Optional. The meta key to retrieve. By default,
* returns data for all keys. Default empty.
* @param bool $single Optional. Whether to return a single value.
* This parameter has no effect if `$key` is not specified.
* Default false.
* @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( $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;
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
return \get_post_meta( $this->wp_post->ID, $key, $single );
}
/**
@ -121,9 +102,6 @@ abstract class Base { // todo renmae to Base_tranformer
*/
public function get_post_meta_time( string $key ) {
$time = $this->get_post_meta( $key, true ); // todo what if not exists
if ( null === $time ) {
return null;
}
$time = \strtotime( $time );
return \gmdate( $this->timeformat, $time );
}