Skip to content

Commit

Permalink
Improve error handling in DustPress\Query::get_acf_post(). Fix #87.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ville Siltala committed Oct 3, 2018
1 parent 5adef23 commit 7ab29c8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).


## [Unreleased]

### Fixed

- `DustPress\Query::get_acf_post()` method returns null if no post is found with the given id. Fixes [#87](https://github.com/devgeniem/dustpress/issues/87).
- `DustPress\Query::get_acf_post()` will not throw an error if ACF is not active.

## [1.16.5] - 2018-09-30 ## [1.16.5] - 2018-09-30


### Fixed ### Fixed
Expand Down
68 changes: 38 additions & 30 deletions classes/query.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public static function get_post( $id = null, $args = array() ) {


if ( is_object( $current_post ) ) { if ( is_object( $current_post ) ) {
self::get_post_meta( $current_post, $meta_keys, $single ); self::get_post_meta( $current_post, $meta_keys, $single );
} else { }
else {
// Return null. // Return null.
return $post; return $post;
} }
Expand Down Expand Up @@ -102,45 +103,52 @@ public static function get_acf_post( $id = null, $args = array() ) {


if ( $post->ID !== $id ) { if ( $post->ID !== $id ) {
$acfpost = get_post( $id ); $acfpost = get_post( $id );
} else { }
else {
$acfpost = $post; $acfpost = $post;
} }


if ( is_object( $acfpost ) ) { // No post was found with the given id or the global post is empty.
$acfpost->fields = get_fields( $acfpost->ID ); if ( $acfpost === null || ! is_object( $acfpost ) ) {
return null;
}


// Get fields with relational post data as a whole acf object if ( function_exists( 'get_fields' ) ) {
if ( $current_recursion_level < $max_recursion_level ) { $acfpost->fields = get_fields( $acfpost->ID );
}


// Let's avoid infinite loops by default by stopping recursion after one level. You may dig deeper in your view model. // Get fields with relational post data as a whole acf object
$options['current_recursion_level'] = apply_filters( 'dustpress/query/current_recursion_level', ++$current_recursion_level ); if ( $current_recursion_level < $max_recursion_level ) {


if ( is_array( $acfpost->fields ) && count( $acfpost->fields ) > 0 ) { // Let's avoid infinite loops by default by stopping recursion after one level. You may dig deeper in your view model.
foreach ( $acfpost->fields as &$field ) { $options['current_recursion_level'] = apply_filters( 'dustpress/query/current_recursion_level', ++$current_recursion_level );
$field = self::handle_field( $field, $options );
}
}
} elseif ( true == $whole_fields ) {
if ( is_array( $acfpost->fields ) && count( $acfpost->fields ) > 0 ) {
foreach( $acfpost->fields as $name => &$field ) {
$field = get_field_object( $name, $acfpost->ID, true );
}
}
}


self::get_post_meta( $acfpost, $meta_keys, $single ); if ( ! empty( $acfpost->fields ) && is_array( $acfpost->fields ) ) {
} foreach ( $acfpost->fields as &$field ) {
$field = self::handle_field( $field, $options );
}
}
}
elseif ( true == $whole_fields ) {
if ( ! empty( $acfpost->fields ) && is_array( $acfpost->fields ) ) {
foreach( $acfpost->fields as $name => &$field ) {
$field = get_field_object( $name, $acfpost->ID, true );
}
}
}


$acfpost->permalink = get_permalink( $acfpost->ID ); self::get_post_meta( $acfpost, $meta_keys, $single );


// Get ACF image object. $acfpost->permalink = get_permalink( $acfpost->ID );
if ( function_exists( 'acf_get_attachment' ) ) {
$attachment_id = get_post_thumbnail_id( $acfpost->ID );


if ( $attachment_id ) { // Get ACF image object.
$acfpost->image = acf_get_attachment( $attachment_id ); if ( function_exists( 'acf_get_attachment' ) ) {
} $attachment_id = get_post_thumbnail_id( $acfpost->ID );
}
if ( $attachment_id ) {
$acfpost->image = acf_get_attachment( $attachment_id );
}
}


return self::cast_post_to_type( $acfpost, $output ); return self::cast_post_to_type( $acfpost, $output );
} }
Expand Down

0 comments on commit 7ab29c8

Please sign in to comment.