Last time we left off with the original script being separated into functions resulting in a new script with similar functionality. Today we are going to focus on a little more cleaning and the addition of a new if statement and change to the SQL statement, to handle attachment posts (uploads eg images etc).
$kids = $data->get_results(“SELECT * FROM wp_posts WHERE post_parent=”.$pID);
The current SQL call above, caters for all (*) information within the respective tables to be gathered. Whilst this can be useful it really is a bit overkill and not very efficient. As all we have really needed up to this stage is the post_title and ID data this can be rationalized as follows.
$kids = $data->get_results(“SELECT ID, post_title FROM wp_posts WHERE post_parent=”.$pID);
However due to the archiving structure implemented within wordpress attachments are also outputted by the current version of Paper Trail, as they are also stored via the post table. To deal with this we need an additional chunck of information, the post_type. With this added the call now looks as follows:
$kids = $data->get_results(“SELECT ID, post_title, post_type FROM wp_posts WHERE post_parent=”.$pID);
With this now sorted an additional if statement can be added to the code to separate this “unwanted” data eg:
if ( $pType == “page” || $pType == “post” ) {
echo “type: “.$pType.”<br />”;
} else {
echo “dont show me”;
} // [$pType check, if]
Ok, simple enough however what this also does is provide another use for the Paper Trail script. Now we have the ability to separate and output attachment data if required. Cool! (I smell another script function, lol)
The following is my first pass at an attachment extension identifier function. The function works by exploding the title into chunks at each “.” and then using the final chunk polls an array of extensions types to find a match. The function has only been set up for images thus far but you should get the idea:
<?php
function defineAttachmentType($pTitle) {
echo “Type: “.$pTitle.”<br />”;
$imgIDs[0]= “JPG”;
$imgIDs[1]= “jpg”;
$imgIDs[2]= “GIF”;
$imgIDs[3]= “gif”;
$imgIDs[4]= “PNG”;
$imgIDs[5]= “png”;
$isIMG = 0;
$atts = explode(“.”, $pTitle);
$aCnt = (sizeof($atts)-1); // php arrays start at zero
$aID = $atts[$aCnt];
foreach ($imgIDs as $img) {
if ($img == $aID) {
$isIMG = 1;
}
}
if ($isIMG) {
echo $pTitle.” is a .”.$aID.” image”.”<br />”;
}
}
?>
The line: echo $pTitle.” is a .”.$aID.” image”.”<br />”; has been added for debugging purposes. In realtime usage this is replaced this with a return call eg:
return “img”;
<?php
function pollPath($start , $child) {
$paths = explode(“/”, $_SERVER['REQUEST_URI']);
$count = (sizeof($paths)-1);
$trail = “/”;
$i = 1;
while ( $i <= $count ) {
$ptitle = $paths[$i];
$pURL = $trail.$ptitle;
$class = pollClass($i , $count);
echo “<li class=”.$class.”><a href=”.$start.$pURL.”>”.$ptitle.”</a></li>”;
$trail = $pURL.”/”;
$i = $i + 1;
} // end while
} // end pollPath
?>
<?php
function pollClass($i , $count) {
if ($i == ($count-1)) {
$class = “trail-parent”;
} elseif ($i == ($count)) {
$class = “trail-current”;
} else {
$class = “trail-item”;
}
return $class;
}
?>
<?php
function pollChildren($pID, $data, $pURL) {
$kids = $data->get_results(“SELECT ID, post_title, post_type FROM wp_posts WHERE post_parent=”.$pID);
if ($kids) {
// echo “<ul class=’child’ style=’display:inline; clear:both; float:left;’>”;
foreach ($kids as $kid) {
$pTitle = $kid->post_title;
$kID = $kid->ID;
$pType = $kid->post_type;
if ( $pType == “page” || $pType == “post” ) {
// echo “type: “.$pType.”<br />”;
// } else {
// echo “dont show me”;
// } // [$pType check, if]
echo “<ul class=’”.$kID.”‘ style=’display:block; border:solid 1px #3F464A; margin:4px; overflow:hidden;’>”;
if ( $pURL == “-” ) {
$pURI = str_replace(” “, “-”, $_SERVER['REQUEST_URI'].”/”.$pTitle);
$pURI = strtolower($pURI);
} else {
$pURI = str_replace(” “, “-”, $pURL.”/”.$pTitle);
$pURI = strtolower($pURI);
} // [$pURL string cleaning, if]
echo “<li style=’width:100%’><a style=’display:block;’ href=’”.$pURI.”‘>”.$pTitle.”</a></li>”.”<br />”;
$kidz = $data->get_results(“SELECT * FROM wp_posts WHERE post_parent=”.$kID);
if ($kidz) {
pollChildren($kID, $data, $pURI);
} else {
echo “</ul>”;
} // [$kidz, if]
} else { } // [$pType check, if]
} // [$kids as $kid, foreach]
} // [$kids, if]
echo “</ul>”;
} // [pollChildren()]
?>
<?php
function displayChildren($kID) {
$children = wp_list_pages(‘title_li=&child_of=’.$kID.’&echo=0&depth=2′);
if ($children) { echo $children; }
}
?>
<?php
function buildPageTrail($start , $startName, $pID, $data) {
echo “<ul class=’paper-trail’>”;
echo “<li class=’trail-root’><a href=”.$start.”>”.$startName.”</a></li>”; // hard coded root vars passed in
pollPath($start , $startName);
echo “<br />”;
pollChildren($pID, $data, “-”);
echo “</ul>”;
}
?>
<div style=”margin:10px; clear:both;”>
<?php $data = $wpdb; ?>
<img alt=”permalink image” class=”permalink” src=”<?php bloginfo(‘template_directory’); ?>/images/permalink.gif” />
<div class=”post-head”>papertrail</div>
<?php buildPageTrail(“http://blog.dyadica.net”, “root”, $post->ID, $data); ?>
</div>
say what do you think