-
Notifications
You must be signed in to change notification settings - Fork 2
/
tagging_functions.php
205 lines (165 loc) · 5.29 KB
/
tagging_functions.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
<?php
function product_tag_init(){
$taxonomy = 'product_tag';
$obj_type = 'product';
if(function_exists('register_taxonomy')) {
register_taxonomy( $taxonomy, $obj_type);
}
//wp_set_object_terms(8, 'pen', 'product_tag');
}
function product_tag_cloud(){
product_tag_init();
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => ''
);
$args = wp_parse_args( $args, $defaults );
$tags = get_product_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
//var_dump($tags);
if ( empty($tags) )
return;
$return = wp_generate_product_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
if ( is_wp_error( $return ) )
return false;
else
echo apply_filters( 'product_tag_cloud', $return, $args );
}
function wp_generate_product_tag_cloud( $tags, $args = '' ) {
global $wp_rewrite;
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
);
$args = wp_parse_args( $args, $defaults );
extract($args);
if ( !$tags )
return;
$counts = $tag_links = array();
foreach ( (array) $tags as $tag ) {
$counts[$tag->name] = $tag->count;
$tag_links[$tag->name] = get_product_tag_link( $tag->term_id );
if ( is_wp_error( $tag_links[$tag->name] ) )
return $tag_links[$tag->name];
$tag_ids[$tag->name] = $tag->term_id;
}
$min_count = min($counts);
$spread = max($counts) - $min_count;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
if ( $font_spread <= 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
if ( 'name' == $orderby )
uksort($counts, 'strnatcasecmp');
else
asort($counts);
if ( 'DESC' == $order )
$counts = array_reverse( $counts, true );
$a = array();
$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
foreach ( $counts as $tag => $count ) {
$tag_id = $tag_ids[$tag];
$tag_link = clean_url($tag_links[$tag]);
$tag = str_replace(' ', ' ', wp_specialchars( $tag ));
$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
( $smallest + ( ( $count - $min_count ) * $font_step ) )
. "$unit;'>$tag</a>";
}
switch ( $format ) :
case 'array' :
$return =& $a;
break;
case 'list' :
$return = "<ul class='product_tag_cloud'>\n\t<li>";
$return .= join("</li>\n\t<li>", $a);
$return .= "</li>\n</ul>\n";
break;
default :
$return = join("\n", $a);
break;
endswitch;
return apply_filters( 'wp_generate_product_tag_cloud', $return, $tags, $args );
}
function &get_product_tags($args = '') {
global $wpdb, $category_links;
$key = md5( serialize( $args ) );
if ( $cache = wp_cache_get( 'get_product_tags', 'category' ) )
if ( isset( $cache[ $key ] ) )
return apply_filters('get_product_tags', $cache[$key], $args);
$tags = get_terms('product_tag', $args);
if ( empty($tags) )
return array();
$cache[ $key ] = $tags;
wp_cache_set( 'get_product_tags', $cache, 'category' );
$tags = apply_filters('get_product_tags', $tags, $args);
return $tags;
}
function &get_product_tag($tag, $output = OBJECT, $filter = 'raw') {
return get_term($tag, 'product_tag', $output, $filter);
}
//
// Tags
//
function get_product_tag_link( $tag_id ) {
global $wp_rewrite;
$taglink = $wp_rewrite->get_tag_permastruct();
$tag = &get_term($tag_id, 'product_tag');
if ( is_wp_error( $tag ) )
return $tag;
$slug = $tag->slug;
if ( empty($taglink) ) {
$file = get_option('home') . '/';
$taglink = $file . '?page_id=3&ptag=' . $slug;
} else {
$file = get_option('home') . '/';
$taglink = get_option('product_list_url') . '?ptag=' . $slug;
/*
$taglink = get_option('product_list_url')."tag/%tag%";
$taglink = str_replace('%tag%', $slug, $taglink);
$taglink = user_trailingslashit($taglink, 'category');*/
}
return apply_filters('product_tag_link', $taglink, $tag_id);
}
function get_the_product_tags( $id = 0 ) {
global $post;
$id = (int) $id;
if ( ! $id && ! in_the_loop() )
return false; // in-the-loop function
if ( !$id )
$id = (int) $post->ID;
$tags = get_object_term_cache($id, 'product_tag');
if ( false === $tags )
$tags = wp_get_object_terms($id, 'product_tag');
$tags = apply_filters( 'get_the_tags', $tags );
if ( empty( $tags ) )
return false;
return $tags;
}
function get_the_product_tag_list( $before = '', $sep = '', $after = '' ) {
$tags = get_the_tags();
if ( empty( $tags ) )
return false;
$tag_list = $before;
foreach ( $tags as $tag ) {
$link = get_tag_link($tag->term_id);
if ( is_wp_error( $link ) )
return $link;
$tag_links[] = '<a href="' . $link . '" rel="tag">' . $tag->name . '</a>';
}
$tag_links = join( $sep, $tag_links );
$tag_links = apply_filters( 'the_tags', $tag_links );
$tag_list .= $tag_links;
$tag_list .= $after;
return $tag_list;
}
function the_product_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
$return = get_the_product_tag_list($before, $sep, $after);
if ( is_wp_error( $return ) )
return false;
else
echo $return;
}
?>