1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

class BFIShortcodePostModel extends BFIShortcodeModel implements iBFIShortcode {
    
    const SHORTCODE = 'post';
    const ALIAS = 'posts';
    
    public $class = '';
    
    public $categories = '';
    public $orderby = 'date';
    
    public $page = '1';
    
    public function render($content = NULL, $unusedAttributeString = '') {
        $num = '3'; 
        
        // these are the initial query args
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 3,
            'ignore_sticky_posts' => true,
            );
        
        // page
        $args['offset'] = ((int)$this->page - 1) * $num;
        
        // categories
        if ($this->categories) {
            $args['cat'] = '';
            
            $catsToCheck = array();
            if (stripos($this->categories, ',') !== false) {
                $catsToCheck = explode(',', $this->categories);
            } else {
                $catsToCheck[] = $this->categories;
            }
            
            // check the categories available and find for a match
            $categories = get_categories(array('hide_empty' => 0));
            foreach ($catsToCheck as $catToCheck) {
                $catToCheck = trim(strtolower($catToCheck));
                
                $found = false;
                foreach ($categories as $category) {
                    if ($catToCheck == strtolower($category->name) ||
                        $catToCheck == $category->slug) {
                        $args['cat'] .= $args['cat'] ? ',' : '';
                        $args['cat'] .= $category->cat_ID;
                        $found = true;
                        break;
                    }
                }
                if (!$found) {
                    $args['cat'] .= $args['cat'] ? ',' : '';
                    $args['cat'] .= $catToCheck;
                }
            }
            
            // this is the default behavior if nothing matched above
            if (!$args['cat']) {
                $args['cat'] = $this->categories;
            }
        }

        // orderby
        if ($this->orderby) {
            $args['orderby'] = $this->orderby;
        }
        
        // loop through posts
        $ret = "";
        $i = 1;
        $loop = new WP_Query($args);
        while ( $loop->have_posts() ) : $loop->the_post();
        
            $mediaType = bfi_get_post_meta(get_the_ID(), "preview_type");
            $image = bfi_get_post_meta(get_the_ID(), "preview_image");
            $video = bfi_get_post_meta(get_the_ID(), "preview_video");
            
            // date
            $month = get_the_date("M");
            $day = get_the_date("j");
            $year = get_the_date("Y");
            if ($year == date("Y")) { // same year
                $date = "$month $day";
            } else {
                $date = "$month $day, $year";
            }
            
            // author
            $author = sprintf('<a href="%1$s" title="%2$s">%3$s</a>',
                get_author_posts_url(get_the_author_meta('ID')),
                sprintf(__("View all posts by %s", BFI_I18NDOMAIN), get_the_author()),
                get_the_author());
        
            $preview = '';
            if ($mediaType == "image") {
                $preview = sprintf("<a href='%s' class='%s'><img data-original='%s' src='%s'/><noscript><img src='%s'/></noscript></a>",
                    get_permalink(),
                    "lightbox icon-plus icon-3x",
                    bfi_thumb($image, array('height' => 315 * .7, 'width' => 420, 'crop' => true)), // orig image
                    bfi_thumb(BFI_BLANKIMAGE, array('height' => 315 * .7, 'width' => 420, 'crop' => true, 'format' => '1')), // blank image
                    bfi_thumb($image, array('height' => 315 * .7, 'width' => 420, 'crop' => true)) // orig image
                );
            } else {
                $video = preg_replace("/width=[\"|\']\d+[\"|\']/i", "width=\"420\"", $video);
                $video = preg_replace("/height=[\"|\']\d+[\"|\']/i", "height=\"".(315 * .7)."\"", $video);
                $preview = $video;
                $preview = "<div>$preview<div class='clearfix'></div></div>";
            }

            $class = '';
            if ($i == 1) {
                $class .= ' alpha';
            } elseif ($i == 3) {
                $class .= ' omega';
            }
            
            
            bfi_set_excerpt_readmore('');
            
            $readmore = do_shortcode(sprintf("[button class='readmore' label='%s <i class=\"icon-double-angle-right\"></i>' href='%s']", __("Read more", BFI_I18NDOMAIN), get_permalink()));
        
            $ret .= do_shortcode("<div class='shortcode-post column one-third $class'><a href='".get_permalink()."'><h4>".get_the_title()."</h4></a><small>".sprintf(__("Posted on %s by %s", BFI_I18NDOMAIN), $date, $author)."</small>$preview<div><span>".bfi_get_the_excerpt(100)."</span>$readmore</div></div>");
            
            if ($i == 3) break;
            $i++;
        
        endwhile;
        unset($loop);
        wp_reset_postdata();
        
        
        while ($i < 3) {
            $class = '';
            $class = $i == 1 ? "alpha" : $class;
            $class = $i == 3 ? "omega" : $class;
            $ret .= "<div class='shortcode-post column one-third $class'></article>";
            $i++;
        }

        $ret .= "<div class='clearfix'></div>";
        
        return $ret;
    }
}