-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-dtree-build.php
More file actions
167 lines (161 loc) · 8.91 KB
/
wp-dtree-build.php
File metadata and controls
167 lines (161 loc) · 8.91 KB
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
<?php
function wpdt_build_tree($nodelist, $args){ //internal
if(!$nodelist || count($nodelist) < 1){ return ''; }
$wpdtreeopt = get_option('wpdt_options');
extract($args, EXTR_SKIP);
unset($args);
global $wpdt_tree_ids;
$openlink_title = esc_attr__($openlink, 'wpdtree');
$closelink_title = esc_attr__($closelink, 'wpdtree');
$openlink = esc_html__($openlink, 'wpdtree');
$closelink = esc_html__($closelink, 'wpdtree');
$separator = esc_html__($oclink_sep, 'wpdtree');
$blogpath = trailingslashit(get_bloginfo('url'));
$addhome = false; //seriously stupid idea. just add a filter to the widget-output instead.
if($addhome){
$home_node = array( 'id' => 3.14, 'pid' => 0, 'url' => esc_url(get_home_url()),
'name' => esc_js(esc_html(get_bloginfo('name'))),
'title' => esc_js(esc_attr(get_bloginfo('description'))));
array_unshift($nodelist, $home_node);
}
$tree = '';
$t = $treetype.$wpdt_tree_ids[$treetype]; //a unique handle for the tree.
$tree .= ($openlink || $closelink) ? "<span class='oclinks oclinks_{$treetype}' id='oclinks_{$t}'>" : '';
$tree .= ($openlink) ? "<a href='javascript:{$t}.openAll();' title='{$openlink_title}'>{$openlink}</a>" : '';
$tree .= ($separator && $openlink && $closelink)? "<span class='oclinks_sep oclinks_sep_{$treetype}' id='oclinks_sep_{$t}'>{$separator}</span>" : '';
$tree .= ($closelink)? "<a href='javascript:{$t}.closeAll();' title='{$closelink_title}'>{$closelink}</a>" : '';
$tree .= ($openlink || $closelink) ? "</span>\n" : '';
$tree .= ($wpdtreeopt['openscript']) ? $wpdtreeopt['openscript'] : "<script type='text/javascript'>"; //this happens for some reason?
$tree .= "if(document.getElementById && document.getElementById('oclinks_{$t}')){document.getElementById('oclinks_{$t}').style.display = 'block';}\n";
$tree .= "var {$t} = new wpdTree('{$t}', '{$blogpath}','{$truncate}');
{$t}.config.useLines={$uselines};
{$t}.config.useIcons={$useicons};
{$t}.config.closeSameLevel={$closelevels};
{$t}.config.folderLinks={$folderlinks};
{$t}.config.useSelection={$showselection};
{$t}.a(0,'root','','','','','');\n";
foreach($nodelist as $nodedata){
$nodedata['url'] = str_replace($blogpath, '', esc_url($nodedata['url'])); //make all path's relative, to save space.
$target = (!empty($nodedata['target'])) ? esc_js(esc_attr($nodedata['target'])) : '';
$rsspath = ($showrss) ? esc_js(wpdt_get_rss($nodedata, $treetype)) : '';
if((!$nodedata['title']) || ($nodedata['name'] == $nodedata['title'])){
$nodedata['name'] = esc_js(esc_html($nodedata['name']));
$nodedata['title'] = ''; //save space, let the javascript default title to name.
}else{
$nodedata['name'] = esc_js(esc_html(wpdt_truncate($nodedata['name'], $truncate)));
$nodedata['title'] = esc_js(esc_attr($nodedata['title']));
}
$tree .= "{$t}.a({$nodedata['id']},{$nodedata['pid']},'{$nodedata['name']}','{$nodedata['title']}','{$nodedata['url']}','{$target}','{$rsspath}');\n";
}
$tree .= "document.write({$t});\n";
$tree .= ($wpdtreeopt['closescript']) ? $wpdtreeopt['closescript'] : '</script>' ;
unset($wpdtreeopt);
unset($nodelist);
return $tree;
}
function wpdt_truncate($string, $max = 16, $replacement = '...'){
if ($max < 1 || strlen($string) <= $max){ return $string; }
$leave = $max - strlen($replacement);
return substr_replace($string, $replacement, $leave);
}
function wpdt_get_rss($nodedata, $treetype){
$rsslink = '';
$feedtype = 'rss2';
if($nodedata['id'] <= 0){
if(get_option('permalink_structure') == ''){
$rsslink = '?feed='.$feedtype.'&'.$treetype.'='.$nodedata['id'];
} else{
//$path = str_replace(trailingslashit(get_bloginfo('url')), '', $nodedata['url']);
$rsslink = trailingslashit($nodedata['url']).'feed';
}
}
return $rsslink;
}
function wpdt_force_open_to($opento, $tree_id, $treestring){
$result = "\n/*WP-dTree: force open to: '{$opento}' */\n";
if(trim($opento) == 'all'){
$result .= $tree_id.".openAll();\n";
} else {
$requests = explode(',', $opento);
foreach($requests as $request){
$result .= wpdt_open_tree_to($request, $tree_id, $treestring, true);
}
}
return $result;
}
/* This function is hairy. It helps if you take a look at the JS-source in the HTML first. Here's one typical line:
arc1.a(4695,2,'Post Title','','2010/10/post-title/','','');
We're trying to find the node-ID (4695 in this case) corresponding to the requested URL. */
function wpdt_open_tree_to($request, $tree_id, $treestring, $forced = false){
global $wp_query;
$opt = get_option('wpdt_options');
$prefix = is_category() ? '-' : ''; //category IDs are negated to avoid ID-trampling in the tree
if(is_numeric($request)){ //assume request was a node ID.
return "$tree_id.openTo('{$prefix}{$request}', true); /*was numeric*/\n";
}
if(!$forced){ //don't allow shortcutting when forcing a node open.
if(is_home()){
return "$tree_id.closeAll(); /*is home*/\n";
}
if($wp_query){
$maybe_id = $wp_query->get_queried_object_id();//If the request is a category, author, permalink or page
if($maybe_id){
return "$tree_id.openTo('{$prefix}{$maybe_id}', true); /*wp_query object id*/\n";
}
$maybe_id = (isset($wp_query->post->ID) && $wp_query->found_posts == 1) ? $wp_query->post->ID : false;
if($maybe_id !== false){//if more than one post, ignore the id (will be top-post but we want the category/archive view)
return "$tree_id.openTo('{$maybe_id}', true); /*wp_query post ID*/\n";
}
$paged = $wp_query->query_vars['paged'];
if($paged > 0){//the dtree is unaware of paging, so remove it from request.
$request = str_replace("?paged={$paged}", '', $request);
$request = str_replace("/page/{$paged}", '', $request);
}
}
}
//Okay, we were fed an URL. Let's clean it up to look like it would in the JS-source.
$path = ltrim($request, '/'); //REQUEST_URI should be '/blog/category/post/' or somesuch. Remove leading slash.
$blogurl = get_bloginfo('url'); //yields: http://blog.server.com, http://server.com/~userdir - you get the picture.
if(empty($path) || $path == $blogurl || $path == '/'){ //we've probably requested "home", so let's do nothing
return "/*\nWP-dTree: $tree_id request seems to be home.\n*/";
}else if(strpos($path, $blogurl) === 0){ //REQUEST_URI included http://server.com/ (happens on some hosts)
$path = str_replace($blogurl, '', $path); //all URLs are relative in the JS source (to save space), so let's get rid of the blog url.
} else { //some servers (with userdir) gives us: '~userdir/blog/category/post/'
$segments = explode('/', $path); //$segments[0] could be '~userdir' or 'blog' now
if(strpos($blogurl, $segments[0])!== false){//REQUEST_URI gave us the userdir - this is included in the blog url, so lets remove it.
$path = ltrim(str_replace($segments[0], '', $path));
}
}
$path = ltrim($path, '/'); //REQUEST_URI should be '/blog/category/post/' or somesuch. Remove leading slash.
if(empty($path)){return "/*WP-dTree: requested path was empty*/";} //this should never happen, so let's handle it. :P
$path = "'".$path."'"; //the JS parameters are surrounded by '', so let's be explicit (avoid 2010/10 match with 2010/10/post-title)
//Now to isolate the ID. First we find the line where it appears
$parts = explode($path, $treestring); //split the script around the path, to immedietly narrow the search. (thus we know line is at the end of the first part)
if(count($parts) < 2){
return "/*WP-dTree: $tree_id request was {$path}. Couldn't find it.*/\n";
}
$parts = $parts[0]; //we know line is at the end of the first part
$needle = $tree_id.'.a(';
$ls = false;
if(version_compare(PHP_VERSION, '5.0.0', '<')) { //php 4.
$ls = strlen($parts) - strpos(strrev($parts), strrev($needle)); //strrpos for PHP4 only supports single char needles... - strlen($needle)
}else{
$ls = strrpos($parts, $needle)+strlen($needle); //count backwards to the start of the line. will require only a dozen steps no matter how large the tree was
}
if($ls === false){return '';} //no linestart? preposterous.
$le = stripos($parts, ',', $ls); //start at the 'tree#.a(' and find the first ',' (denoting end of the first parameter)
if($le === false){return '';} //no parameter list? wierd.
$number = substr($parts, $ls, $le-$ls); //et voila! we have isolated the ID-parameter in the javascript.
unset($parts);
if(is_numeric($number)){
return "/*WP-dTree: $tree_id request was {$path}. I found: '".esc_js($number)."'*/\n\n{$tree_id}.openTo('{$number}', true);\n";
}
return "/*WP-dTree: {PHP_VERSION} $tree_id request was {$path}. I found: ".esc_js($number)."*/\n"; //if we get down here something was wrong. output some debug-info.
}
function wpdt_get_tree_id($treestring){
if(!$treestring){return false;}
$s = stripos($treestring, 'var ')+4; // 4 = strlen('var ')
$e = stripos($treestring, ' = new wpdTree', $s); //var {id} = new wpdTree
return substr($treestring, $s, $e-$s);
}
?>