TimberPostsCollection::maybe_set_preview()   C
last analyzed

Complexity

Conditions 11
Paths 2

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 5.2653
cc 11
eloc 16
nc 2
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// Exit if accessed directly
4
if ( !defined( 'ABSPATH' ) )
5
    exit;
6
7
class TimberPostsCollection extends ArrayObject {
8
9
    public function __construct( $posts = array(), $post_class = 'TimberPost' ) {
10
        $returned_posts = array();
11
        if ( is_null( $posts ) ){
12
            $posts = array();
13
        }
14
        foreach ( $posts as $post_object ) {
15
            $post_class_use = $post_class;
16
17
            if ( is_array( $post_class ) ) {
18
                $post_type      = get_post_type( $post_object );
19
                $post_class_use = 'TimberPost';
20
21
                if ( isset( $post_class[$post_type] ) ) {
22
                    $post_class_use = $post_class[$post_type];
23
24
                } else {
25
                    if ( is_array( $post_class ) ) {
26
                        TimberHelper::error_log( $post_type . ' of ' . $post_object->ID . ' not found in ' . print_r( $post_class, true ) );
27
                    } else {
28
                        TimberHelper::error_log( $post_type . ' not found in ' . $post_class );
29
                    }
30
                }
31
            }
32
33
            // Don't create yet another object if $post_object is already of the right type
34
            if ( is_a( $post_object, $post_class_use ) ) {
35
                $post = $post_object;
36
            } else {
37
                $post = new $post_class_use( $post_object );
38
            }
39
40
            if ( isset( $post->ID ) ) {
41
                $returned_posts[] = $post;
42
            }
43
        }
44
45
        $returned_posts = self::maybe_set_preview($returned_posts);
46
47
        parent::__construct( $returned_posts, $flags = 0, 'TimberPostsIterator' );
48
    }
49
50
    public function get_posts() {
51
        return $this->getArrayCopy();
52
    }
53
54
     /**
55
     * @param array $posts
56
     * @return array
57
     */
58
    static function maybe_set_preview( $posts ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
        if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview']
60
               && isset( $_GET['preview_id'] ) && $_GET['preview_id']
61
               && current_user_can( 'edit_post', $_GET['preview_id'] ) ) {
62
            // No need to check the nonce, that already happened in _show_post_preview on init
63
64
            $preview_id = $_GET['preview_id'];
65
            foreach( $posts as &$post ) {
66
                if ( is_object( $post ) && $post->ID == $preview_id ) {
67
                    // Based on _set_preview( $post ), but adds import_custom
68
                    $preview = wp_get_post_autosave( $preview_id );
69
                    if ( is_object($preview) ) {
70
71
                        $preview = sanitize_post($preview);
72
73
                        $post->post_content = $preview->post_content;
74
                        $post->post_title = $preview->post_title;
75
                        $post->post_excerpt = $preview->post_excerpt;
76
                        $post->import_custom( $preview_id );
77
78
                        add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
79
                    }
80
                }
81
            }
82
83
        }
84
85
        return $posts;
86
    }
87
88
}
89
90
class TimberPostsIterator extends ArrayIterator {
91
92
    public function current() {
93
        global $post;
94
        $post = parent::current();
95
        return $post;
96
    }
97
}
98