Forum Moderators: coopster

Message Too Old, No Replies

Is this possible?

trying to "inject" code into an established case/break

         

doodlebee

3:06 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



For the record, this is for WordPress. however, WP is PHP-based, so if it's not possib;e to even do this in PHP, then it's *definitely* not possible to do in WordPress.

So here's the thing. There's a bit of code in WP core files - it's a class that's set. Within this class is a function that has a switch (case/break).

What I need to know is, is it possible to write a function *outside* this file that will insert an extra line of code into the switch?

For example, say the code is this:

class Something extends Another_Class{
function start($args) {
switch($args['style']) {
case: "div":
echo "<div>\n";
break;
case: "ol":
echo <ol>\n";
break;
}

So I'm wanting to know if it's possib;e to write a piece of external code that will change the "div" case to this:

case: "div":
echo "<div id='someID'>\n";
echo "<div>\n";
break;

So does anyone know if anything like this is possible? If so, can someone provide me a nudge on how? (like what am I looking for to pull this off?)

Thanks :)

doodlebee

5:22 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



Okay, I found out what I need to do is extend the existing class. However, I'm having trouble inserting the "echo" line into the case I need.

Oh this is such fun :)

dreamcatcher

8:42 am on Mar 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi doodlebee,

Not quite sure I`m understanding what you are trying to accomplish. General functions are global in scope, so any function you create outside of a class can be used anywhere inside any class.

If its a function from another class, then you are right to use the extends option.

dc

doodlebee

11:58 am on Mar 31, 2009 (gmt 0)

10+ Year Member



Hi Dreamcatcher :)

yes, someone else mentioned the "extend" for the class - and I'm working to put that in place - but this part of PHP is a little beyond me. I get the idea (that it extends the original class), but I'm not really clear on the specifics. Right now, the class I'm trying to "overwrite" is a little complicated. Well, for me anyway! LOL

I'm trying to take the existing class, which is this (I'm cleaning it up a bit - getting rid of extraneous stuff):

class Walker_Comment extends Walker {
var $tree_type = 'comment';
var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');

function start_lvl(&$output, $depth, $args) {
$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
case 'div':
break;
case 'ol':
echo "<ol class='children'>\n";
break;
default:
case 'ul':
echo "<ul class='children'>\n";
break;
}
}

function end_lvl(&$output, $depth, $args) {
$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
case 'div':
break;
case 'ol':
echo "</ol>\n";
break;
default:
case 'ul':
echo "</ul>\n";
break;
}
}

function start_el(&$output, $comment, $depth, $args) {
$depth++;
$GLOBALS['comment_depth'] = $depth;

if ( !empty($args['callback']) ) {call_user_func($args['callback'], $comment, $args, $depth);
return;
}

$GLOBALS['comment'] = $comment;
extract($args, EXTR_SKIP);

if ( 'div' == $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
?>

/* this part is actually the layout
}

function end_el(&$output, $comment, $depth, $args) {
if ( !empty($args['end-callback']) ) {
call_user_func($args['end-callback'], $comment, $args, $depth);
return;
}
if ( 'div' == $args['style'] )
echo "</div>\n";
else
echo "</li>\n";
}

}

Now the bolded stuff above is what I want to change - I basically just want to add lines to the "switch" function (that's all I'm trying to do). So I wrote an "overwrite", and it takes - but only partially. When I actually get it to take without any errors, then the entire page is blank (however the source code is all there - which is REALLY bizarre to me - usually if the whole page is blank, the source code is blank too - but in this case, the background color shows up, but nothing else visually, but the source code is ALL there. Even my error logs are clean!). if I edit it to fix it so stuff is visible, I get error messages (and quite frankly, nothing shows up still LOL - just the errors)

So *something* is taking, but I haven't been able to muddle through *what* exactly is going on.

If you want to see what I did:

class my_Walker_Comment extends Walker {
var $tree_type = 'comment';
var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');

function start_lvl(&$output, $depth, $args) {
$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
case 'div':

echo "<div class='replylink'>Show/Hide Replies</div>\n";
echo "<div class='children'>\n";

break;
case 'ol':
echo "<ol class='children'>\n";
break;
default:
case 'ul':
echo "<ul class='children'>\n";
break;
}
}

function end_lvl(&$output, $depth, $args) {
$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
case 'div':

echo "<!--/end children-->\n";
echo "</div>\n\n";

break;
case 'ol':
echo "</ol>\n";
break;
default:
case 'ul':
echo "</ul>\n";
break;
}
}
}

$walker = new my_Walker_Comment();
$walker->start_lvl(&$output, $depth, $args);
$walker->end_lvl(&$output, $depth, $args);

When I keep the "&$output, $depth, $args" part in the "$walker->" above, then the entire page is blank (save the source code and the background color), if I leave that part out, I get errors about missing arguments (which is to be expected I guess).

The bolded stuff is what I'm trying to add to the original class.

You know, it's suddenly dawned on me that maybe it *is* working, but the reason everything is blank is because I'm not redoing the entire class - just part of it. Hmm...

doodlebee

12:34 pm on Mar 31, 2009 (gmt 0)

10+ Year Member



Yep - turned out that was it - I was redoing the class, but I wasn't including the stuff for the display - I was only doing it partway.

Amazing what a good night's sleep will make you see :)

Once I bascially redid the entire class, I got it to do what i wanted :)

dreamcatcher

3:28 pm on Mar 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Excellent, glad you got it sorted. Its nice to feel some kind of accomplishment.

dc