-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfmb-template-helper.php
271 lines (239 loc) · 6.99 KB
/
cfmb-template-helper.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
class CFMB_Template_Helper {
/**
* @param $ins object (optional) an instance of CF_Mini_Blog
* @param $queried_object object (optional) a term or post
*/
public function __construct($ins = null, $queried_object = null) {
if (!($ins instanceof CF_Mini_Blog)) {
$ins = CF_Mini_Blog::factory();
}
$this->ins = $ins;
if (!$queried_object) {
$queried_object = get_queried_object();
}
$this->queried_object = $queried_object;
$this->term_sidebar_map = $this->ins->term_sidebar_map;
$this->term_menu_map = $this->ins->term_menu_map;
}
/**
* Pass in post or term.
* Will tell you if the queried object is a post or term.
* @return bool
*/
public function queried_object_quacks_like($type) {
$is_a_duck = false;
if ($this->queried_object) {
switch ($type) {
case 'post':
$is_a_duck = property_exists($this->queried_object, 'ID');
break;
case 'term':
$is_a_duck = property_exists($this->queried_object, 'term_id');
break;
}
}
return $is_a_duck;
}
public function get_related_terms($only_active_terms = true) {
$related_terms = array();
// If it's a post
if ($this->queried_object_quacks_like('post')) {
$terms = get_the_terms($this->queried_object->ID, $this->ins->taxonomy);
if ($terms && !is_wp_error($terms)) {
// Make sure that the primary term is added to the first item of the array.
// All helper functions assume this to be the 'only' mini blog
if ($this->ins->get_setting('select_multiple')) {
$primary_found = false;
$primary_term = get_post_meta($this->queried_object->ID, $this->ins->primary_meta_key, true);
foreach ($terms as $term_key => $term) {
if ($primary_term == $term->term_id) {
unset($terms[$term_key]);
array_unshift($terms, $term);
$primary_found = true;
break;
}
}
if (!$primary_found || in_array($primary_term, $this->ins->get_inactive_mini_blogs())) {
$terms = array();
}
}
/* Double-check to see if the terms gotten are indeed from the
correct taxonomy. WordPress will send back all terms if
there are no terms, due to some weird SQL thing */
$temp_terms = $terms;
$first = array_shift($temp_terms);
if ($first->taxonomy == $this->ins->taxonomy) {
$related_terms = $terms;
}
}
}
// If it's a term and in our taxonomy...
elseif (
$this->queried_object_quacks_like('term')
&& ($this->queried_object->taxonomy == $this->ins->taxonomy)
) {
$related_terms = array($this->queried_object);
}
// Make sure the terms that are related are also active
if ($only_active_terms && count($related_terms)) {
$tmp = array();
foreach ($related_terms as $term) {
if ($this->ins->is_mini_blog_active($term)) {
$tmp[] = $term;
}
}
$related_terms = $tmp;
}
return $related_terms;
}
/**
* Get back an array of sidebars related to the current query.
* Since multiple terms can potentially belong to a query, this function
* passes back an array of related sidebars.
*
* It's related if one of these things is true:
* - It's a singular query and the post contains the term for the sidebar
* - It's a taxonomy archive query and the taxonomy archive term matches
* a sidebar term.
* AND the sidebar that matches these conditions is in use.
*
* @return array
*/
public function get_related_sidebars() {
if (!isset($this->related_sidebars) || !$this->related_sidebars) {
$matches = array();
$map = $this->term_sidebar_map;
$related_terms = $this->get_related_terms();
foreach ($related_terms as $term) {
if (
array_key_exists($term->slug, $map)
&& is_active_sidebar($map[$term->slug])
) {
$matches[$term->slug] = $map[$term->slug];
}
}
// Memoize
$this->related_sidebars = $matches;
}
return $this->related_sidebars;
}
public function get_all_mini_blogs($args = array(), $exclude_inactive = true) {
if ($exclude_inactive) {
$args['exclude'] = $this->ins->get_inactive_mini_blogs();
}
return $this->ins->get_mini_blogs($args);
}
/**
* Boolean formulation of get_related_sidebars().
* @return bool
*/
public function has_related_sidebar() {
return (bool) count($this->get_related_sidebars());
}
/**
* Choose a sidebar for the current page based on related.
*/
public function choose_dynamic_sidebar() {
$sidebars = $this->get_related_sidebars();
if (!count($sidebars)) {
return;
}
$sidebars_temp = $sidebars;
dynamic_sidebar(array_shift($sidebars_temp));
}
public function get_related_menu_id() {
$terms = $this->get_related_terms();
if (!$terms) {
return '';
}
$term = array_shift($terms);
if (array_key_exists($term->slug, $this->term_menu_map)) {
return $this->term_menu_map[$term->slug];
}
}
public function get_attachment_image($size = 'thumbnail', $attr = '') {
$attachment_id = $this->get_related_meta('thumbnail');
if ($attachment_id) {
return wp_get_attachment_image($attachment_id, $size, false, $attr);
}
return '';
}
public function get_masthead($size = '') {
$terms = $this->get_related_terms();
$term = array_shift($terms);
if ($term) {
$taxonomy = $this->ins->taxonomy;
$term_url = get_term_link($term, $taxonomy);
$image = $this->get_attachment_image($size);
if ($image) {
return '<a class="masthead-banner" href="'.$term_url.'">' . $image . '</a>';
}
}
return '';
}
public function mini_blog_url() {
$terms = $this->get_related_terms();
$term = array_shift($terms);
if ($term) {
return get_term_link($term, $term->taxonomy);
}
return '';
}
public function mini_blog_link() {
$terms = $this->get_related_terms();
$term = array_shift($terms);
if ($term) {
$term_url = get_term_link($term, $term->taxonomy);
return '<a href="'.esc_url($term_url).'">'.esc_html($term->name).'</a>';
}
return '';
}
public function mini_blog_feed_link_url() {
$terms = $this->get_related_terms();
$term = array_shift($terms);
$feed_url = '';
if ($term) {
$feed_url = get_term_feed_link(
$term->term_id,
$this->ins->taxonomy,
'rss2'
);
}
return $feed_url;
}
/**
* Gets post meta related to current view
*/
public function get_related_meta($key) {
$out = null;
$terms = $this->get_related_terms();
if (count($terms)) {
$term = array_shift($terms);
if ($term && $term->term_id) {
$out = $this->ins->get_mini_blog_meta($term->term_id, $key);
}
}
return $out;
}
public function get_leaderboard_code() {
return $this->get_related_meta('leaderboard_code');
}
public function get_mobile_ad_code() {
return $this->get_related_meta('mobile_ad_code');
}
public function get_analytics_code() {
return $this->get_related_meta('analytics_code');
}
public function uses_dark_theme() {
$meta = $this->get_related_meta('dark_theme');
return $meta == "1";
}
public function uses_image_only_excerpts() {
$meta = $this->get_related_meta('image_only_excerpts');
return $meta == "1";
}
public function render_analytics_code() {
echo $this->get_analytics_code();
}
}