/
xiushi-shortcodes.php
248 lines (217 loc) · 9.15 KB
/
xiushi-shortcodes.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
<?php
/*
Plugin Name: Xiu Shi Shortcodes
Plugin URI: http://www.google.ca
Description: This plugin implements all the custom shortcode functionality for the 'Xiu Shi' website.
Version: 0.1
Author: Kevin Chow
Author URI: http://www.google.ca
*/
defined('ABSPATH') or die('No script kiddies please!');
function xs_shortcodes_init(){
wp_register_style('xs_css', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('xs_css');
}
add_action('init', 'xs_shortcodes_init');
//print debug info
function xs_debug_info_sc($atts){
$postTypes = get_post_types();
$taxonomies = get_taxonomies();
$ret = '';
$ret .= 'POST TYPES:<br>';
foreach($postTypes as $t){
$ret .= $t . '<br>';
}
$ret .= 'TAXONOMIES:<br>';
foreach($taxonomies as $t){
$ret .= $t . '<br>';
}
return $ret;
}
add_shortcode('xs-debug-info', 'xs_debug_info_sc');
//all-faqs shortcode.
//Usage: [all-faqs]
function xs_all_faqs_sc($atts) {
$resultData = array();
$topCategories = get_categories(array('taxonomy'=>'category', 'parent'=>0));
foreach($topCategories as $parentCat){
$queryArgs = array(
'posts_per_page' => -1,
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $parentCat->term_id,
)
)
);
$query = new WP_Query($queryArgs);
$parentFaqCount = $query->found_posts;
$subCats = xs_all_faqs_get_category_tree($parentCat->term_id);
$resultData[$parentCat->term_id] = array('id'=>$parentCat->term_id, 'description'=>$parentCat->description, 'name'=>$parentCat->name, 'url'=>get_term_link($parentCat, 'category'), 'count'=>$parentFaqCount, 'children'=>$subCats);
}
//sort top level categories by description
usort($resultData, function ($a, $b){return strcasecmp($a['name'], $b['name']);});
//Generate the output
$res = xs_all_faqs_format_result('All FAQs', $resultData);
wp_reset_postdata();
return $res;
}
add_shortcode('xs-all-faqs', 'xs_all_faqs_sc');
function xs_all_faqs_get_category_tree($catID){
$results = array();
$childCategoryIDs = get_term_children($catID, 'category');
foreach($childCategoryIDs as $childID){
$cat = get_term($childID, 'category');
$queryArgs = array(
'posts_per_page' => -1,
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $childID,
)
)
);
$query = new WP_Query($queryArgs);
if($query->found_posts > 0){
//$url = get_term_link($cat, 'category')
$url = '/category-faq?wpv-category='.$cat->slug;
$results[] = array('id'=>$cat->term_id, 'description'=>$cat->description, 'name'=>$cat->name, 'count'=>$query->found_posts, 'url'=>$url);
}
}
usort($results, function ($a, $b){return strcasecmp($a['description'], $b['description']);});
return $results;
}
//helper function to format the HTML output from the found results
function xs_all_faqs_format_result($title, array $data) {
if (empty($data))
return '';
$res = '<div class="xs-all-faqs">';
$res .= '<div class="row hr-content-block">';
//$res .= '<h3>' . $title . '</h3>';
foreach ($data as $category) {
if($category['count'] > 0 || count($category['children']) > 0){
if(isset($category['children'])){
$res .= '<div class="col-sm-6">';
$res .= '<h3>' . $category['description'] . ' <span style="font-size:0.8em;">' . $category['name'] . '</span></h3>'; //$category['count'])
//$res .= '<ul>';
$res .= '<div class="hr-subcategory-list">';
foreach($category['children'] as $category){
//$res .= '<li>';
$res .= '<div class="hr-subcategory-list-item"><span class="dashicons dashicons-marker hr-dashicon-category" style="color: #CDDC39;line-height: 22px;margin-right: 2px; font-size: 15px;"></span>';
$res .= '<a href="' . $category['url'].'">';
$res .= $category['description'] . ' | <span style="font-size: 0.8em">' . $category['name'] . '</span>';
$res .= '</a>';
$res .= '</div>';
//$res .= '</li>';
}
// $res .= '</ul>';
$res .= '</div>';//<div class="hr-subcategory-list">
$res .= '</div>';//<div class="col-sm-6">
}
}
}
/*
* <div class="row hr-content-block">
<div class="col-sm-6"> (6 columns)
<h3>parent taxonomy title</h3>
<div class="hr-subcategory-list-item"><span class="dashicons dashicons-marker hr-dashicon-category"></span>[wpv-taxonomy-link] | <span style="font-size: 0.8em">[wpv-taxonomy-description]</span>
</div>
</div>
*/
$res .= '</div>';//<div class="row hr-content-block">
$res .= '</div>';//class="xs-all-faqs"
return $res;
}
//USAGE: [xs-tooltip view="view-name" listingid="xx"]
function xs_tooltip($atts){
$linkHTML = "CLICKME";
$divId = "xsModal-";
if(!isset($atts['listingid'])){
$contentHTML = "xs-tooltip: listingid attribute not set!";
}
else if(!isset($atts['view'])){
$contentHTML = "xs-tooltip: view attribute not set!";
}
else if(function_exists("render_view")){
$divId .= $atts['listingid'];
//$debugInfo = "<br>DEBUG INFO:<br>view name=".$atts["view"]."<br>listingid=".$atts['listingid']."<br>";
$contentHTML = render_view(array('title' => $atts["view"], 'listingid'=>$atts['listingid'] ));
}
else{
$contentHTML = "xs_tooltip: wpv-views plugin required for this dialog to work properly!";
}
$ret = '<a href="#'.$divId.'">'.$linkHTML.'</a><div id="'.$divId.'" class="modalDialog"><div><a href="#close" title="Close" class="close">X</a>'.$contentHTML.'</div></div>';
return $ret;
}
add_shortcode('xs-tooltip', 'xs_tooltip');
//USAGE: [xs-cpt-link type="<type>" cat="category-name"]
function xs_cpt_link($atts){
if(!isset($atts['type']) || !isset($atts['cat'])){
return 'xs_cpt_link: Usage: [xs-cpt-link type="type" cat="category-name"]';
}
$cat['type'] = strtolower($cat['type']);
$postTypes = array(
'recommended'=>array('post', 'pointer'), //for 'recommended', look for post types 'post' and 'pointer'
);
$typeToUrl = array(
'pointer'=>'pointer',
'post'=>'category-news',
'user-image'=>'user-image',
'listing'=>'listing',
'recommended'=>'recommended-posts',
'faq'=>'category-faq',
'glossary'=>'glossary',
'promotion'=>'promotion'
);
$typeDesc = array(
'pointer'=>"专家指引",
'post'=>"行业资讯",
'user-image'=>"灵感发掘",
'listing'=>"商家目录",
'recommended'=>"入门必读",
'faq'=>"常见问题",
'glossary'=>'专业术语',
'promotion'=>'最新优惠'
);
$postType = isset($postTypes[$atts[type]]) ? $postTypes[$atts[type]] : $atts[type];
$args = array(
'post_type'=>$postType,
'category_name' => $atts['cat'],
);
//If type == 'recommended', if posts exist for (1) the cat and (2) terms "Featured" and "Recommended" exist for Taxonomy "Recommendations" then proceed, if not, discard.
if($atts['type'] == 'recommended'){
$args['tax_query'] = array(
array(
'taxonomy' => 'recommendation',
'field' => 'slug',
'terms' => array( 'featured', 'recommended' )));
}
$the_query = new WP_Query($args);
$categoryPosts = $the_query->found_posts;
$ret = "";
if($categoryPosts > 0){ //if the cat has posts, output a link
$siteUrl = get_site_url();
$format = '<a href="##SITEURL##/##TYPE##/?wpv-category=##CAT##">##DESC##</a>';
$typeUrl = array_key_exists($atts['type'], $typeToUrl) ? $typeToUrl[$atts['type']] : $atts['type'];
$desc = array_key_exists($atts['type'], $typeDesc) ? $typeDesc[$atts['type']] : $atts['type'];
$replace = array('##SITEURL##', '##TYPE##', '##CAT##', '##DESC##', '##COUNT##');
$replaceWith = array($siteUrl, $typeUrl, $atts['cat'], $desc, $categoryPosts);
$ret = str_replace($replace, $replaceWith, $format);
}
else{ //if the cat has no posts, output the desc without link
$ret = $typeDesc[$atts['type']];
}
return $ret;
}
add_shortcode('xs-cpt-link', 'xs_cpt_link');
add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );
function wpv_post_param_shortcode( $atts ) {
if ( !empty( $atts['var'] ) ) {
$var = (array)$_GET[$atts['var']];
return esc_html( implode( ', ', $var ) );
}
}