root/jp-bread-crumb-trail/trunk/jp-bct.php

Revision 634, 9.4 KB (checked in by jixor, 5 years ago)

Added preliminary support for paged articles.

Line 
1<?php
2/*
3Plugin Name: Bread Crumb Trail Generator
4Version: 1.1 Beta
5Plugin URI: http://jp.jixor.com/archives/bread-crumb-trail/
6Author: Stephen Ingram
7Author URI: http://jp.jixor.com/
8Description: Generate a bread crumb trail. The main purpose of this plugin is to give the user some feedback as to what they are actually viewing. To call use jp_bct(); Options are 'root', 'showhome', 'before', 'after', 'divider', 'eachlink', 'current' and 'homename' (all optional). See plugin page or source for more details and instructions. Note that you can't make the current page a link. I'm aware that technically this isn't a BCT. However 99% of the time you see 'BCT' this implementation is what they are talking about.
9*/
10
11/* ************************************************************************
12
13JP_BCT License (based on BSD license)
14Copyright (c) 2005, jp.jixor.com, Stephen Ingram
15All rights reserved.
16
17        PREAMBLE
18
19  Feel free to modify the source providing you stick the the following
20conditions. Please also inform me of any modifications you make so that I
21may possibly add them to the release on my site. I will credit authors
22where appropriate. License may change. However this is fairly simple,
23basically don't claim its your own and I'm not responsible for your use of
24it.
25
26---------------------------------------------------------------------------
27
28        LICENSE
29
30Redistribution and use in source and binary forms, with or without
31modification, are permitted provided that the following conditions are met:
32
331) Redistributions of source code must retain the above copyright notice,
34   this list of conditions and the following disclaimer.
35
362) Neither the name of jixor.com, nor the names of its
37   contributors may be used to endorse or promote products derived from this
38   software without specific prior written permission.
39
40THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50POSSIBILITY OF SUCH DAMAGE.
51
52************************************************************************ */
53
54/*
55Please report any errors or problems to me via the plugin's comments section. If you want your post to remain private just say so.
56
57Thanks to Denis (http://www.semiologic.com/) for keeping me on my toes.
58
59There may be some debate as to if this is actually a bct or not, I think showing the hierarchical structure of the site through a list of backlinks is a bct even it it doesnt show the exact path you have taken. I think that approach is actually foolish (usability wise) and I ahve never actually seen it anyway (other than when I tested it).
60*/
61
62// Begin editable
63define(JP_BCT_CFG,'wp-content/jp-bct/jp-bct.ini'); // You may place settings in an ini file, feel free to change the path here. However when you update it will return to the default.
64// End editable
65
66function jp_bct($root='/', $showhome=TRUE, $before='<div id="bct">You\'re browsing: ', $after='</div>', $divider=' / ', $eachlink='<a href="%url%" title="Browse to: %name%">%name%</a>', $current='<strong>%name%</strong>', $homename='Home',$debug=FALSE,$pagedtext='Page ') {
67
68        /*
69        Other than debug iformation this script should be fully multilingual. You'll just have to manually specify formatting for different languages.
70
71        'root' - The location of the root of your site. If your site is http://www.example.com/ the root should be "/". However if your site is
72                http://www.example.com/userslog/ the root would be "/userslog/".
73        'showhome' - Will show the bct when the user is on the home page, however it will just be the test specified by 'homename' and not a link.
74        'before' - I suggest you insert your caption here. Could use something like 'You are here:', just whatever floats your boat.
75        'after' - Placed after the bct.
76        'divider' - Placed between each item in the bct, thats not before the first and not after the last.
77        'eachlink' - Format of each link item in the bct. Uses replacments - %url% - url for the individual bct, and '%name$ - text for the bct,
78                formatting applied by script.
79        'current' - Format for the item representing the current page, being the last item in the bct. Replaces '%name%' only.
80        'homename' - Text displayed for the home link, if you don't want to call it home then just change this.
81        'debug' - Seting this will make the script always display debug information. You can however just set ?debug in your uri to enable it.
82        'pagedtext' - If you are browsing multipaged article this will be added preceding the page number in the uri to make it more readable.
83        */
84
85        global $single;
86        if ($single) global $name, $p, $posts;
87
88        // Can also put settings in an ini file. Make sure if you use an ini file that its valid!
89        if (file_exists('wp-content/jp-bct/jp-bct.ini')) extract(parse_ini_file('wp-content/jp-bct/jp-bct.ini')); // For auto install additional files should be here.
90
91        if (isset($_GET['debug'])) $debug = TRUE; // You can display debug output easy.
92
93
94        // FORMAT INPUT
95
96        // Get and clean the URI.
97        $uri = html_entity_decode(rawurldecode(strtolower($_SERVER['REQUEST_URI']))); // To the maxxx
98
99        // Trim $root from the start of the URI.
100        $uri = substr($uri,strlen($root));
101
102        // If there is a page .php that is not index you'll want that, but you wont want the ext.
103        if (strrpos($uri,'.')) $uri = substr($uri,0,strrpos($uri,'.'));
104
105        // You don't want index only.
106        $uri = preg_replace('/index$/','',$uri);
107
108        // Clear out any query crap (if a file wasnt specified).
109        $uri = preg_replace('/\?.*/','',$uri);
110
111        // Remove slashes from the start and end.
112        $uri = ltrim($uri,'/');
113
114        $uri = rtrim($uri,'/');
115
116        // Possible to improve the above script some time. Also possible the above still have a few bugs in situations I haven't thought of.
117
118        // ==== If matches structure that has article title / is single
119        // replace last elemtnt or if last element is numberic replace second last element with article title.
120
121
122
123        ($uri != '') ? $elements = explode('/', $uri) : $counter = 0; // If the uri is not empty explode it.
124
125        if ($elements) $counter = count($elements); // If it was exploded count the number of elements it produced.
126
127        echo '<!-- jp_bct: uri:"'.$uri.'" - counter:"'.$counter.'" -->'; // General debug - always people in situations I haven't thought of will have some debug. Probally pointless but you know.
128
129        if ($debug) {
130                echo 'Debug:<br />Cleaned URI:'.$uri.'<br />Counter: '.$counter.'<br />Exploded: ';
131                print_r($elements);
132        }
133
134
135
136        // Only show home if showhome is set or there are links in the bct.
137        //      I think it would be possible to improve this section with a bit of reworking, however the current set-up seems to work fine for the moment.
138        if (($showhome) and ($counter == 0)) {
139
140                $output = $before;
141                $output .= str_replace('%name%',$homename,$current);
142                $output .= $after;
143
144                echo $output . "\n";
145
146                return;
147
148        } elseif ($counter > 0) {
149
150                if ($single) { // Replace slug name with real article title.
151
152                        // Check for $name and $p (post id)
153
154                        if ($name) { // I dont know how else you call an individual post other than via id($p) or searching and returning an exact match. I know you can browse a date that has only one item in it but I'll look into that later.
155
156                                // I don't know much about paged articles so I'm not sure how to handle a paged situation, however I think this should work fine.
157                                if (!is_numeric($elements[$counter-1])) {
158                                        $elements[$counter-1] = $posts[0]->post_title;
159                                } else {
160                                        $elements[$counter-2] = $posts[0]->post_title;
161                                        $elements[$counter-1] = $pagedtext.$elements[$counter-1]; // To make the bct more readable.
162                                }
163
164                                /* Debug Stuff
165                                echo $elements[$counter-1];
166                                echo '<!--';
167                                print_r($posts);
168                                echo '-->';
169                                */
170
171                        }
172
173                        // Problem: If you search and it returns an exact match your view as if single however $single is not set! - I'm going to ignore this for the moment.
174
175                }
176
177                $output = $before;
178
179                $linkname = $homename;
180                $linkurl = $root;
181                $output .= jp_getcrumb($eachlink,$divider,array('name'=>$linkname,'url'=>$linkurl)); // Home link
182
183                for ($i=0; $i<$counter; $i++)
184                {
185
186                        $link = $elements[$i];
187                        $linkname = str_replace('-',' ',ucfirst($link));
188
189                        if ($i == ($counter-1)) {
190
191                                $crumb = str_replace('%name%',$linkname,$current);
192
193                        } else {
194
195                                $linkurl = $linkurl . $link . '/';
196
197                                $crumb = jp_getcrumb($eachlink,$divider,array('name'=>$linkname,'url'=>$linkurl));
198
199                        }
200
201                        $output .= $crumb;
202
203                }
204
205                $output .= $after;
206
207                echo $output . "\n";
208
209                return;
210
211        }
212
213}
214
215
216
217function jp_getcrumb($eachlink,$divider,$data) // For use by jp_bct() only.
218{
219
220        $elements = array('name','url');
221
222        $return = $eachlink;
223
224        foreach ($elements as $element)
225        {
226                $replace = 'link'.$element;
227                $return = str_replace('%'.$element.'%',$data[$element],$return);
228        }
229
230        if ($divider) $return .= $divider;
231
232        return $return;
233
234}
235
236?>
Note: See TracBrowser for help on using the browser.