SimplePortal

Customization => Custom Coding => Topic started by: AngelinaBelle on February 28, 2010, 08:50:02 PM

Title: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on February 28, 2010, 08:50:02 PM
Having hunted through ModifyBBCSettings and parse_bbc, I thought I understood that the way to suppress BBC tags was to stuff them into $modSettings['disabledBBC'], then, later, set $modSettings['disabledBBC'] back to the way it started. 
 
But I'm not having much luck.
Within the block, I've attempted

 echo 'disabled BBCcodes ', $modSettings['disabledBBC'], '<br/>';
 $supressTags = array('pdf','pdftest','flash');
 $realdisabledbbc=$modSettings['disabledBBC'];
 $temp=array();
 if (!empty($realdisabledbbc)) $temp=explode(',', $realdisabledbbc);
 foreach ($temp as $k => $tag)
  echo '$temp[',$k,'] ', $tag, '<br/>';
 foreach ( $supressTags as $k => $tag)
  if (! in_array($tag, $temp, True) ) $temp[]=$tag;
 
 foreach ($temp as $k => $tag)
  echo '$temp[',$k,'] ', $tag, '<br/>';
 $modSettings['disabledBBC'] = implode(',',$temp);
 
 echo 'now disabled BBCcodes ', $modSettings['disabledBBC'], '<br/>';

 
But the tags pdf and pdftest are not suppressed UNLESS I have unchecked them in the BBCcode admin panel.
 
Can anyone tell me what I am missing?
 
Thanks
Title: Re: How to disable BBC tags in a block?
Post by: AngelinaBelle on March 01, 2010, 10:41:06 AM
So I've started hacking parse_bbc -- actually, I'm just filling it full of debugging echo statements.
 
I can see that, in general, $modSettings['disabledBBC'] is ignored because it is inside a block controlled by
[size=2]if (empty($bbc_codes) || $message === false)
[/size]
 
So just shoving stuff into $modSettings['disabledBBC'] is clearly not going to work for me. So the statement at the top of the file
Quote
string parse_bbc(string message, bool smileys = true, string cache_id = '')
- this very hefty function parses bbc in message.
- only parses bbc tags which are not disabled in disabledBBC.
- also handles basic HTML, if enablePostHTML is on.
- caches the from/to replace regular expressions so as not to reload
them every time a string is parsed.
- only parses smileys if smileys is true.
- does nothing if the enableBBC setting is off.
- applies the fixLongWords magic if the setting is set to on.
- uses the cache_id as a unique identifier to facilitate any caching
it may do.
- returns the modified message.
Is clearly not true. It seems an extra call to parse_bbc with $message===False is required to stuff the contents of $modSettings['disabledBBC'] into static $disabled. It is really static $disabled that parse_bbc obeys.
 
I was stuck because I believed the documentation.
 
The correct solution is

 echo 'disabled BBCcodes ', $modSettings['disabledBBC'], '<br/>';
 $supressTags = array('pdf','pdftest','flash');
 $realdisabledbbc=$modSettings['disabledBBC'];
 $temp=array();
 if (!empty($realdisabledbbc)) $temp=explode(',', $realdisabledbbc);
 foreach ($temp as $k => $tag)
  echo '$temp[',$k,'] ', $tag, '<br/>';
 foreach ( $supressTags as $k => $tag)
  if (! in_array($tag, $temp, True) ) $temp[]=$tag;
 
 $modSettings['disabledBBC'] = implode(',',$temp);
 parse_bbc(FALSE);

 
But that does not solve the problem of how to re-enable the tags after I'm done with the block!
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on March 01, 2010, 12:23:49 PM
I feel awfully nervous about modifying parse_bbc.
 
But I can't see any way of removing tags from the disabled list after I'm done with the block.
 
If you call parse_bbc(FALSE), it updates its static $disabled by adding anything it finds in $modSettings['disabledBBC'], but does not remove anything. And since static $disabled is private to parse_bbc, there is no way to get to it outside of parse_bbc.
 
Here is my solution to re-enabling BBC tags at the end of the block
[size=2]  $modSettings['disabledBBC'] = $realdisabledbbc;
  parse_bbc(FALSE);

[/size]
 
And, in parse_BBC
Code: (find Subs.php) [Select]
  if (!empty($modSettings['disabledBBC']))
  {
   $temp = explode(',', strtolower($modSettings['disabledBBC']));
   foreach ($temp as $tag)
    $disabled[trim($tag)] = true;
  }
Code: (replace) [Select]
  if (!empty($modSettings['disabledBBC']))
  {
   $temp = explode(',', strtolower($modSettings['disabledBBC']));
   $disabled=array();
   foreach ($temp as $tag)
    $disabled[trim($tag)] = true;
  }
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on March 02, 2010, 02:00:21 PM
Actually, I was just told in http://www.smf-friends.org/index.php?topic=1300.msg27053#msg27053 (http://www.smf-friends.org/index.php?topic=1300.msg27053#msg27053) that parse_bbc already has a mechanism for that.
 
So I think I'll stop fiddling with parse_bbc.
 
Here's the *real* solution to the problem:
Code: (function sp_articles in PortalBlocks.php) [Select]
$supressTags = array('pdf','pdftest','flash'); // add more tags to the list, if you want
 
 $parse_tags = array();
 $temp = parse_bbc(FALSE);
 foreach ($temp as $k => $code)
  if ( ! in_array($code['tag'],$supressTags) ) $parse_tags[]=$code['tag'];

and, further down in the same function,
Code: [Select]
   if (!empty($parameters['truncate']) && $func['strlen']($article['msg']['body']) > $parameters['truncate'] )
   {
     $article['msg']['body'] = sp_truncateText($article['msg']['body']);
     $articles[$k]['msg']['body'] =
      parse_bbc( $article['msg']['body'],
         $article['msg']['smileysEnabled'],
         $article['msg']['id'],
         $parse_tags
      ) . '<a href="'.$article['href'].'">...</a>';
   }
   else
    $articles[$k]['msg']['body'] = parse_bbc($article['msg']['body'], $article['msg']['smileysEnabled'],
          $article['msg']['id'], $parse_tags);

No modifications to parse_bbc required.
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: [SiNaN] on March 03, 2010, 08:43:50 AM
Actually, I was just told in http://www.smf-friends.org/index.php?topic=1300.msg27053#msg27053 (http://www.smf-friends.org/index.php?topic=1300.msg27053#msg27053) that parse_bbc already has a mechanism for that.

That functionality is a killer, unless you have SP installed. ;)
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on March 03, 2010, 09:53:32 AM
That functionality is a killer, unless you have SP installed. ;)

What do you mean? That it is great, or that it causes poor performance?
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: [SiNaN] on March 03, 2010, 11:30:44 AM
Oh, right. I meant it is very poor on performance.
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on March 03, 2010, 12:44:20 PM
So SP has improved the performance of that part of parse_bbc?
 
 
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: [SiNaN] on March 03, 2010, 01:15:51 PM
Yeah, it had to. It is used by shoutbox to limit the number of tags allowed. Without that fix, parsing a dozen of shouts made the forum unusable.
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on March 03, 2010, 02:11:41 PM
OH! you are telling me that SP Adds the parse_tags parameter to parse_bbc?
I hadn't noticed. It looks like it is native to SMF2.0
 I, in my ignorance, had come up with another solution:  make parse_bbc obey $modSettings['disabledBBC'], so you can disable some BBC, parse the content, then re-enable the BBC.
 
I suppose the parse_tags solution performs better.
 
 
----------------------------------------
 
Anyway, I am thinking I probably should have posted my modified sp_articles block (http://simpleportal.net/index.php?board=39.0 (http://simpleportal.net/index.php?board=39.0)) in mods instead of in custom coding

 
 
 
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: [SiNaN] on March 03, 2010, 04:19:16 PM
SP adds that functionality to 1.1 versions and fixes the performance issue on 2.0 versions.
Title: Re: How to disable BBC tags in a block? And re-enable them afterwards?
Post by: AngelinaBelle on March 03, 2010, 06:03:15 PM
That's why we love the SimplePortal!
SimplePortal 2.3.8 © 2008-2024, SimplePortal