Skip to content

Commit d6d74ab

Browse files
authored
Merge pull request #82 from Liblastic/preview-fix
Preview fix
2 parents 6851477 + 2b6611d commit d6d74ab

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.16.2] - 2018-09-03
8+
9+
### Changed
10+
- Fixed 'dustpress/router' filter for WordPress preview functionality.
11+
- Removed unnecessary `$id` parameter from `get_post_meta()`.
12+
- Changed functions `get_post()` and `get_acf_post()` to use global post if desired post id same as global post.
13+
714
## [1.16.1] - 2018-06-20
815

916
### Added

classes/query.php

+46-32
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class Query {
3434
*
3535
* @return array|object|null Type corresponding to output type on success or null on failure.
3636
*/
37-
public static function get_post( $id, $args = array() ) {
37+
public static function get_post( $id = null, $args = array() ) {
38+
39+
global $post;
40+
3841
$defaults = [
3942
'meta_keys' => 'null',
4043
'single' => false,
@@ -45,20 +48,23 @@ public static function get_post( $id, $args = array() ) {
4548

4649
extract( $options );
4750

48-
$post_data = get_post( $id, 'ARRAY_A' );
51+
// If id is not the same as global post get_post by id.
52+
if ( $post->ID !== $id ) {
53+
$post = get_post( $id );
54+
}
4955

50-
if ( is_array( $post_data ) ) {
51-
self::get_post_meta( $post_data, $post_data['ID'], $meta_keys, $single );
56+
if ( is_object( $post ) ) {
57+
self::get_post_meta( $post, $meta_keys, $single );
5258
} else {
5359
// Return null.
54-
return $post_data;
60+
return $post;
5561
}
5662

57-
$post_data['permalink'] = get_permalink( $id );
58-
$post_data['image_id'] = get_post_thumbnail_id( $id );
63+
$post->permalink = get_permalink( $post->ID );
64+
$post->image_id = get_post_thumbnail_id( $post->ID );
5965

6066
// Cast the post and return.
61-
return self::cast_post_to_type( $post_data, $output );
67+
return self::cast_post_to_type( $post, $output );
6268
}
6369

6470
/**
@@ -77,7 +83,9 @@ public static function get_post( $id, $args = array() ) {
7783
*
7884
* @return array|object|null Type corresponding to output type on success or null on failure.
7985
*/
80-
public static function get_acf_post( $id, $args = array() ) {
86+
public static function get_acf_post( $id = null, $args = array() ) {
87+
88+
global $post;
8189

8290
$defaults = [
8391
'meta_keys' => null,
@@ -92,39 +100,45 @@ public static function get_acf_post( $id, $args = array() ) {
92100

93101
extract( $options );
94102

95-
$acfpost = get_post( $id, 'ARRAY_A' );
103+
if ( $post->ID !== $id ) {
104+
$acfpost = get_post( $id );
105+
} else {
106+
$acfpost = $post;
107+
}
96108

97-
if ( is_array( $acfpost ) ) {
98-
$acfpost['fields'] = get_fields( $id );
109+
if ( is_object( $acfpost ) ) {
110+
$acfpost->fields = get_fields( $post->ID );
99111

100112
// Get fields with relational post data as a whole acf object
101113
if ( $current_recursion_level < $max_recursion_level ) {
102114

103115
// Let's avoid infinite loops by default by stopping recursion after one level. You may dig deeper in your view model.
104116
$options['current_recursion_level'] = apply_filters( 'dustpress/query/current_recursion_level', ++$current_recursion_level );
105117

106-
if ( is_array( $acfpost['fields'] ) && count( $acfpost['fields'] ) > 0 ) {
107-
foreach ( $acfpost['fields'] as &$field ) {
118+
if ( is_object( $acfpost->fields ) && count( $acfpost->fields ) > 0 ) {
119+
foreach ( $acfpost->fields as &$field ) {
108120
$field = self::handle_field( $field, $options );
109121
}
110122
}
111123
} elseif ( true == $whole_fields ) {
112-
if ( is_array( $acfpost['fields'] ) && count( $acfpost['fields'] ) > 0 ) {
113-
foreach( $acfpost['fields'] as $name => &$field ) {
114-
$field = get_field_object($name, $id, true);
124+
if ( is_object( $acfpost->fields ) && count( $acfpost->fields ) > 0 ) {
125+
foreach( $acfpost->fields as $name => &$field ) {
126+
$field = get_field_object( $name, $post->ID, true );
115127
}
116128
}
117129
}
118-
self::get_post_meta( $acfpost, $id, $meta_keys, $single );
130+
131+
self::get_post_meta( $acfpost, $meta_keys, $single );
119132
}
120133

121-
$acfpost['permalink'] = get_permalink( $id );
134+
$acfpost->permalink = get_permalink( $post->ID );
122135

123-
if ( function_exists("acf_get_attachment") ) {
124-
$attachment_id = get_post_thumbnail_id( $id );
136+
// Get ACF image object.
137+
if ( function_exists( 'acf_get_attachment' ) ) {
138+
$attachment_id = get_post_thumbnail_id( $post->ID );
125139

126140
if ( $attachment_id ) {
127-
$acfpost['image'] = acf_get_attachment( $attachment_id );
141+
$acfpost->image = acf_get_attachment( $attachment_id );
128142
}
129143
}
130144

@@ -370,26 +384,26 @@ private static function parse_query_object( $query ) {
370384
* @since 0.0.1
371385
*
372386
* @param array &$post The queried post.
373-
* @param int $id Id for the post.
374387
* @param array/string $meta_keys Wanted meta keys or string 'ALL' to fetch all.
375388
* @param boolean $single If true, return only the first value of the specified meta_key.
376389
*
377390
* @return array
378391
*/
379-
private static function get_post_meta( &$post, $id, $meta_keys = NULL, $single ) {
392+
private static function get_post_meta( &$post, $meta_keys = NULL, $single = false ) {
393+
380394
$meta = array();
381395

382396
if ( $meta_keys === 'all' ) {
383397
// Get all metadata.
384-
$meta = get_metadata( 'post', $id, '', $single );
398+
$meta = get_metadata( 'post', $post->ID, '', $single );
385399
} elseif ( is_array( $meta_keys ) ) {
386400
// Get the wanted metadata by defined keys.
387401
foreach ( $meta_keys as $key ) {
388-
$meta[$key] = get_metadata( 'post', $id, $key, $single );
402+
$meta[$key] = get_metadata( 'post', $post->ID, $key, $single );
389403
}
390404
}
391405

392-
$post['meta'] = $meta;
406+
$post->meta = $meta;
393407
}
394408

395409
/**
@@ -428,6 +442,7 @@ private static function get_meta_for_posts( &$posts = [], $meta_keys = NULL, $si
428442

429443
/**
430444
* Used to cast posts to a desired type.
445+
* Defaults to standard WordPress object.
431446
*
432447
* @type function
433448
* @date 26/1/2016
@@ -440,13 +455,12 @@ private static function get_meta_for_posts( &$posts = [], $meta_keys = NULL, $si
440455
*/
441456
private static function cast_post_to_type( $post, $type ) {
442457

443-
if ( 'OBJECT' === $type ) {
444-
return (object) $post;
445-
} elseif ( 'ARRAY_N' === $type ) {
446-
return array_values( $post );
458+
if ( $type === 'ARRAY_A' ) {
459+
$post = $post->to_array();
460+
} elseif ( $type === 'ARRAY_N' ) {
461+
$post = array_values( $post->to_array() );
447462
}
448463

449-
// Defaults to ARRAY_A
450464
return $post;
451465
}
452466

dustpress.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Author: Miika Arponen & Ville Siltala / Geniem Oy
77
Author URI: http://www.geniem.com
88
License: GPLv3
9-
Version: 1.16.1
9+
Version: 1.16.2
1010
*/
1111

1212
final class DustPress {
@@ -194,8 +194,8 @@ public function create_instance() {
194194
// Filter for wanted post ID
195195
$new_post = apply_filters( 'dustpress/router', $post_id );
196196

197-
// If developer wanted a post ID, make it happen
198-
if ( ! is_null( $new_post ) ) {
197+
// If developer wanted a post by post ID.
198+
if ( $new_post !== $post_id ) {
199199
$post = get_post( $new_post );
200200

201201
setup_postdata( $post );

0 commit comments

Comments
 (0)