Permalink
Browse files

Improve error handling in DustPress\Query::get_acf_post(). Fix #87.

  • Loading branch information...
villesiltala committed Oct 3, 2018
1 parent 5adef23 commit 7ab29c83d6f7673aa6aa3ad60e5ef15b3d05421e
Showing with 45 additions and 30 deletions.
  1. +7 −0 CHANGELOG.md
  2. +38 −30 classes/query.php
View
@@ -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/)
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
### Fixed
View
@@ -55,7 +55,8 @@ public static function get_post( $id = null, $args = array() ) {
if ( is_object( $current_post ) ) {
self::get_post_meta( $current_post, $meta_keys, $single );
} else {
}
else {
// Return null.
return $post;
}
@@ -102,45 +103,52 @@ public static function get_acf_post( $id = null, $args = array() ) {
if ( $post->ID !== $id ) {
$acfpost = get_post( $id );
} else {
}
else {
$acfpost = $post;
}
if ( is_object( $acfpost ) ) {
$acfpost->fields = get_fields( $acfpost->ID );
// No post was found with the given id or the global post is empty.
if ( $acfpost === null || ! is_object( $acfpost ) ) {
return null;
}
// Get fields with relational post data as a whole acf object
if ( $current_recursion_level < $max_recursion_level ) {
if ( function_exists( 'get_fields' ) ) {
$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.
$options['current_recursion_level'] = apply_filters( 'dustpress/query/current_recursion_level', ++$current_recursion_level );
// Get fields with relational post data as a whole acf object
if ( $current_recursion_level < $max_recursion_level ) {
if ( is_array( $acfpost->fields ) && count( $acfpost->fields ) > 0 ) {
foreach ( $acfpost->fields as &$field ) {
$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 );
}
}
}
// Let's avoid infinite loops by default by stopping recursion after one level. You may dig deeper in your view model.
$options['current_recursion_level'] = apply_filters( 'dustpress/query/current_recursion_level', ++$current_recursion_level );
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.
if ( function_exists( 'acf_get_attachment' ) ) {
$attachment_id = get_post_thumbnail_id( $acfpost->ID );
$acfpost->permalink = get_permalink( $acfpost->ID );
if ( $attachment_id ) {
$acfpost->image = acf_get_attachment( $attachment_id );
}
}
// Get ACF image object.
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 );
}

0 comments on commit 7ab29c8

Please sign in to comment.