Skip to content

Commit

Permalink
Allow Post::get_field method to accept default value returned when fi…
Browse files Browse the repository at this point in the history
…eld is empty.
  • Loading branch information
Jakub Szajna committed Jan 28, 2019
1 parent 86ccdf8 commit 0de6a71
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions generators/wp/templates/chisel-starter-theme/Chisel/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ protected function prepareFakePost( $fields ) {
* Overrides get_field function to use fake meta when provided.
*
* @param $field_name
* @param $default_value mixed Default value if field is empty
*
* @return mixed
*/
public function get_field( $field_name ) {
public function get_field( $field_name, $default_value = null ) {
$value = null;
if ( $this->fakeFields && isset( $this->fakeFields[ $field_name ] ) ) {
return $this->fakeFields[ $field_name ];
$value = $this->fakeFields[ $field_name ];
} else {
$value = $this->_get_field( $field_name );
$value = $this->convert( $value, get_class( $this ) );
}

return parent::get_field( $field_name );
return $value ? $value : $default_value;
}

/**
Expand All @@ -69,4 +74,29 @@ public function get_field( $field_name ) {
public static function overrideTimberPostClass() {
return '\Chisel\Post';
}

/**
* @param string $field_name
*
* @return mixed
*/
private function _get_field( $field_name ) {
if ( $rd = $this->get_revised_data_from_method( 'get_field', $field_name ) ) {
return $rd;
}
$value = apply_filters( 'timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this );
if ( $value === null ) {
$value = get_post_meta( $this->ID, $field_name );
if ( is_array( $value ) && count( $value ) == 1 ) {
$value = $value[0];
}
if ( is_array( $value ) && count( $value ) == 0 ) {
$value = null;
}
}
$value = apply_filters( 'timber_post_get_meta_field', $value, $this->ID, $field_name, $this );
$value = $this->convert( $value, get_class( $this ) );

return $value;
}
}

0 comments on commit 0de6a71

Please sign in to comment.