vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   How do you script BB Code? (https://vborg.vbsupport.ru/showthread.php?t=200769)

Jaxel 01-05-2009 02:47 PM

How do you script BB Code?
 
Okay... lets say I want to make a bbcode... [VIDEO]536[/VIDEO]

I want to parse some PHP code on this code. How would I handle this?

PHP Code:

function fetch_bbcode_video($videoid)
{
 require_once(
DIR '/includes/class_videosharingservice.php');
 global 
$vbphrase$vbulletin$show$stylevar;
 if (!
$videoinfo $vbulletin->db->query_first("
  SELECT video.* 
  FROM " 
TABLE_PREFIX "video AS video 
  WHERE video.videoid = '" 
$videoid "'")
 )
 {
  return 
false;
 }
 
$classname "vB_VideoSharingService_$video[videoservice]";
 
$obj = new $classname($vbulletin);
 
$embedcode $obj->fetch_embedcode($videoinfo['videoidservice']);
 
$video['cattitle'] =& $vbulletin->videocats["$video[videocategoryid]"]['title'];
 
$video['caturl'] = construct_category_url($vbulletin->videocats["$video[videocategoryid]"]);
 
$video['url'] = construct_video_url($video);
 
$video['userurl'] = construct_user_url($video);
 if (
$video['timelength'] == 0)
 {
  
$video['timelength'] = "???";
 }
 else
 {
  
$duration $video['timelength'];
  
$minutes floor($duration 60);
  
$seconds $duration 60;
  
$seconds str_pad($seconds2"0"STR_PAD_LEFT); 
  
$video['timelength'] = "$minutes:$seconds";
 }
 eval(
'$html = "' fetch_template('bbcode_video') . '";');
 return 
$html;



Jaxel 01-07-2009 10:04 PM

Okay... since no one knows how to script BB code... how about something different...

How would I make a script that examines a new post and preg_matches a thread and replaces it with content? So if someone made a post and it had:
[VIDEO]536[/VIDEO]
I would want it to preg_match it with:
\[(VIDEO|video)](\d*)\[/(VIDEO|video)]
Then of course, I would run some PHP code on the preg_match[2] returned as 536.

Would this be possible?

Bellardia 01-07-2009 10:14 PM

Quote:

Originally Posted by Jaxel (Post 1704550)
Okay... since no one knows how to script BB code... how about something different...

How would I make a script that examines a new post and preg_matches a thread and replaces it with content? So if someone made a post and it had:
[VIDEO]536[/VIDEO]
I would want it to preg_match it with:
\[(VIDEO|video)]([\d]*)\[/(VIDEO|video)]
Then of course, I would run some PHP code on the preg_match[2] returned as 536.

Would this be possible?

I posted this in another thread a bit earlier, it should help you out

You'll require two plugins for this. I posted their source as xml just because it included everything. Just pay particular attention to the hook location and the basic functions I included.

This one will take the text to be posted, and submit it to a function so can it can be run through a preg_replace or similar function.
Code:

<plugin active="1" executionorder="5">

                        <title>Title Of Mod</title>

                        <hookname>postbit_display_complete</hookname>

                        <phpcode><![CDATA[global $vbulletin;

                        $this->post['message'] = Function_Name($this->post['message']);

                        ]]></phpcode>

</plugin>

Another code in the global_start hook can be used to hold the source of the link replacement, modify the post and return it however you want.
Code:

<title>Plugin Title</title>
<hookname>global_start</hookname>
<phpcode><![CDATA[
            function Function_Name($post) {
                                global $vbulletin;
                //Preg_replace + other Functions
                return $post;
            }
]]></phpcode>


Dismounted 01-08-2009 02:12 AM

That is not adding another BB code. You can add a proper BB code (using PHP processing) by adding your tag to the processing array and setting the callback as the external callback function. Look at the bottom of class_bbcode.php.

Jaxel 01-10-2009 12:36 PM

Huh? I'm confused... is there a guide somewhere?

All I want to do is have a BB Code, with a PHP function run on it.

Dismounted 01-11-2009 04:15 AM

Quote:

Originally Posted by Dismounted (Post 1704749)
That is not adding another BB code. You can add a proper BB code (using PHP processing) by adding your tag to the processing array and setting the callback as the external callback function. Look at the bottom of class_bbcode.php.

All the information you need is in that post.

Jaxel 01-12-2009 05:09 PM

Is there any way of doing this WITHOUT editing VB files? And I dont know what I am supposed to be looking at in class_bbcode.php.

Dismounted 01-13-2009 04:41 AM

Quote:

Originally Posted by Jaxel (Post 1709910)
Is there any way of doing this WITHOUT editing VB files?

Yes, and that is what I'm telling you.
Quote:

Originally Posted by Jaxel (Post 1709910)
And I dont know what I am supposed to be looking at in class_bbcode.php.

You will see how tags are defined. Define your own tag and callback.

Jaxel 01-14-2009 04:35 PM

Do you mind actually explaining what needs to be done? Because I still dont see what I'm supposed to be looking at.

*EDIT* ah... I think I get it now, you wanted me to see: ($hook = vBulletinHook::fetch_hook('bbcode_fetch_tags')) ? eval($hook) : false;

I tried doing this...

Code:

// [VIDEO]
require_once(DIR . '/includes/functions_videodirectory.php');

$tag_list['no_option']['video'] = array(
        'callback' => 'handle_external',
        'external_callback' => 'handle_bbcode_video',
        'strip_empty' => true
);

But I get an error:
Fatal error: Call to undefined method vB_BbCodeParser::handle_bbcode_video() in /home/eightway/public_html/includes/class_bbcode.php on line 1143

How do I get it to call handle_bbcode_video in functions_videodirectory.php?

--------------- Added [DATE]1231967882[/DATE] at [TIME]1231967882[/TIME] ---------------

Well I figured to just use the function in the plugin itself, instead of calling a file... But I'm having trouble passing the parameter data...

Code:

// [VIDEO]
$tag_list['no_option']['video'] = array(
        'callback' => 'handle_external',
        'external_callback' => 'handle_bbcode_video',
        'strip_empty' => true
);

function handle_bbcode_video($videoid)
{
        global $vbulletin;

        if (!$videoinfo = $vbulletin->db->query_first("
                SELECT video.*
                FROM " . TABLE_PREFIX . "video AS video
                WHERE video.videoid = '" . $videoid . "'")
        )
        {
                return 'This video does not exist';
        }

        require_once(DIR . '/includes/class_videosharingservice.php');
        require_once(DIR . '/includes/videoserviceapi/class_' . strtolower($videoinfo['videoservice']) . '.php');

        $classname = 'vB_VideoSharingService_' . $videoinfo['videoservice'];
        $obj = new $classname($vbulletin);

        $embedhtml = $obj->fetch_embedcode($videoinfo['videoidservice']);

        return $embedhtml;
}

For some reason, $videoid is always empty. Why is this? It ALWAYS returns 'This video does not exist'.

--------------- Added [DATE]1231974120[/DATE] at [TIME]1231974120[/TIME] ---------------

Is there a guide or something that explains what all the array options in $tag_list do?

Dismounted 01-15-2009 04:23 AM

Quote:

Originally Posted by Jaxel (Post 1712273)
Do you mind actually explaining what needs to be done?

You've done a lot of work - and it shows, because you're nearly there. I'm going to give you the rest of it, because unlike a lot of people, you've actually tried doing it yourself. :)

Hook: bbcode_fetch_tags
PHP Code:

// [VIDEO]
$tag_list['no_option']['video'] = array(
    
'callback' => 'handle_external',
    
'external_callback' => 'handle_bbcode_video',
    
'strip_empty' => true
); 

Hook: bbcode_create
PHP Code:

// include our custom functions
require_once(DIR '/includes/functions_videodirectory.php');
require_once(
DIR '/includes/class_videosharingservice.php'); 

Function: handle_bbcode_video (/includes/functions_videodirectory.php)
PHP Code:

function handle_bbcode_video(&$parser$value$option)
{
        global 
$vbulletin;

        
// clean video id
        
$videoid intval($value);

        
// fetch video information -- this is costly, it is run every time the bb code tag is used
        
$videoinfo $vbulletin->db->query_first("
                SELECT *
                FROM " 
TABLE_PREFIX "video
                WHERE video.videoid = " 
$videoid "
                LIMIT 1
        "
);

        
// check video exists
        
if (empty($videoinfo))
        {
                
// this will show up on posts -- make sure this is what you want
                
return 'This video does not exist.';
        }

        
// instantiate our video class
        
require_once(DIR '/includes/videoserviceapi/class_' strtolower($videoinfo['videoservice']) . '.php');
        
$classname 'vB_VideoSharingService_' $videoinfo['videoservice'];
        
$obj = new $classname($vbulletin);

        
// return embed code
        
return $obj->fetch_embedcode($videoinfo['videoidservice']);


I've cleaned up and commented the function for you - you should read some of the comments.


All times are GMT. The time now is 08:53 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01386 seconds
  • Memory Usage 1,786KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_code_printable
  • (4)bbcode_php_printable
  • (5)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete