-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoopShortcodePosts.php
142 lines (129 loc) · 5.77 KB
/
LoopShortcodePosts.php
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
<?php
require_once("LoopShortcodeBase.php");
/* The basic loop shortcodes for posts of all post types */
class LoopShortcodePosts extends LoopShortcodeBase {
public $default_template = '<article class="post {{ post_class }}">
<h4><a href="{{ link }}" class="title" title="{{ excerpt|striptags|words(50) }}">{{ title|raw }}</a></h4>
<p>{{ excerpt|raw }}</p>
<div class="meta">
<span class="author">By <a href="{{ author.page }}">{{ author.display_name }}</a><span>
<span class="date"><span title="{{ date }}">{{ ago }}</span> ago</span>
<span class="comments"><span>{{ comments }}</span> {{ comments == 1 ? \'comment\' : \'comments\' }}</span>
</div>
</article>';
public $default_attributes = array(
'query' => '',
'thumbnail_size' => 'thumbnail',
'content' => 0,
'nl2br' => 0,
'texturize' => 1,
'environment' => 'loop_shortcode',
'sticky' => 0,
'recall_environment' => 0,
'recall_environment_type' => 'post__not_in',
'template' => 0
);
public function doLoop($query) {
// Query the new posts
$loopObject = new WP_Query($query);
//if($this->environment == 'main-events') print_r($loopObject);
$output = '';
// Load the variables in the loop
if ($loopObject->have_posts()):
$allPosts = $loopObject->posts;
$numPosts = count($allPosts);
foreach($allPosts as $key => $post):
$twig_vars = array();
// Setup all the variables
$twig_vars['first'] = $key == 0;
$twig_vars['last'] = $key + 1 == $numPosts;
$twig_vars['index'] = $key;
$twig_vars['results'] = $numPosts;
$twig_vars['query'] = $query;
$twig_vars['id'] = $post->ID;
$twig_vars['title'] = $post->post_title;
$twig_vars['link'] = get_permalink($twig_vars['id']);
$twig_vars['date'] = $post->post_date;
$twig_vars['time'] = mysql2date('U', $twig_vars['date']);
$twig_vars['age'] = abs(time() - $twig_vars['time']);
$twig_vars['ago'] = human_time_diff($twig_vars['time'], time());
$twig_vars['modified'] = $post->post_modified;
$twig_vars['modified_time'] = mysql2date('U', $twig_vars['modified']);
$twig_vars['modified_age'] = abs(time() - $twig_vars['modified_time']);
$twig_vars['modified_ago'] = human_time_diff($twig_vars['modified_time'], time());
if($this->content) {
$twig_vars['content'] = $post->post_content;
$twig_vars['content'] = apply_filters('the_content', $twig_vars['content']);
$twig_vars['excerpt'] = '';
} else {
$twig_vars['content'] = '';
$twig_vars['excerpt'] = get_extended($post->post_content);
$twig_vars['excerpt'] = strip_shortcodes($twig_vars['excerpt']['main']);
}
$twig_vars['post_type'] = $post->post_type;
$twig_vars['comments'] = $post->comment_count;
$twig_vars['comments_s'] = ($twig_vars['comments'] == 1) ? '' : 's';
// Author information
$twig_vars['author']['id'] = $post->post_author;
$twig_vars['author']['username'] = get_the_author_meta('user_login', $twig_vars['author']['id']);
$twig_vars['author']['display_name'] = get_the_author_meta('display_name', $twig_vars['author']['id']);
$twig_vars['author']['page'] = get_author_posts_url($twig_vars['author']['id']);
$twig_vars['author']['link'] = get_the_author_meta('user_url', $twig_vars['author']['id']);
$twig_vars['author']['email'] = get_the_author_meta('user_email', $twig_vars['author']['id']);
// Thumbnail details
$twig_vars['thumb'] = wp_get_attachment_image_src(get_post_thumbnail_id($twig_vars['id']), $this->thumbnail_size);
// Setup the thumbnail full image
$twig_vars['thumbnail'] = get_the_post_thumbnail($post->ID, $this->thumbnail_size);
// Setup post classes
$twig_vars['post_class'] = '';
$classes = get_post_class();
foreach ($classes as $classname) {
$twig_vars['post_class'] .= $classname . ' ';
}
// Setup categories
$twig_vars['categories'] = array();
// For individual category styling
$twig_vars['category_ids'] = array();
$categories = get_the_category($twig_vars['id']);
if($categories){
foreach($categories as $category) {
$buffer = array();
$buffer['link'] = get_category_link($category->term_id);
$buffer['name'] = $category->name;
$buffer['id'] = $category->term_id;
$twig_vars['category_ids'][] = $category->term_id;
$twig_vars['categories'][] = $buffer;
}
}
// Setup the custom fields
$twig_vars['custom'] = array();
$custom_buffer = get_post_meta($post->ID);
foreach($custom_buffer as $key => $custom) {
// Don't include internal meta values
if(substr($key, 0, 1) == '_') continue;
// For each element, if it's serializeable, unserialize it
if(is_array($custom)) {
$custom = array_map(function($value) {
$unserialized = @unserialize($value);
return $unserialized === false ? $value : $unserialized;
}, $custom);
}
// This makes it so you don't have to use {{ custom.myfield.0 }} for keys that only ever have one value
if(is_array($custom)) {
if(count($custom) > 1) {
$twig_vars['custom'][$key] = $custom;
} else {
$twig_vars['custom'][$key] = $custom[0];
}
} else {
$twig_vars['custom'][$key] = $custom;
}
}
// Load the twig template
$output .= $this->processTemplate($twig_vars);
endforeach;
endif;
// Return the final output!
return $output;
}
}