Recent Topics

INSERT_POST Hack (insert a post into another one)

Started by on Jul 06, 2005 – Contents updated: Jul 06, 2005

Jul 06, 2005 21:53    

Warning
This hack is intended for [url=http://b2evolution.net]b2evolution[/url] 0.9.0.11 and 0.9.0.12 only. This is a work in progress hack far from being bug-free. Use it at your own risk

Problem
Bloggers use categories to organize posts. However, when you split an article into several posts, it would be interesting to add a table of contents to each article you could update every time you add a new article without having to edit every article separately.

Solution
I've developed a simple INSERT_POST Hack in order to insert a post into another one. By adding:

Code

<!--post_insert 123-->

into your new post, this new hack is going to add the contents of post #123 into your current post at the exact position the above code appeared into your new post.

Later, if you want to hide the contents of post #123, edit it and change its status to depreciated (or to private). If you want to make post #123 appearing again, change its status to draft (or to anything else than depreciated and private).

Implementation
Edit the b2evocore/_class_item.php file and add about line 491 the following code:

Code

//>>>START
// INSERT_POST Hack
 
// Insert other posts into the current one
$max_loops = 10;
while( $max_loops-- > 0 && preg_match( "/^(.*)<!--insert[_]?post[^0-9]{0,4}([0-9]{1,7})-->(.*)$/siD", $content, $matches ) )
{
  // Yes, we have to include another post to the current one.
  // This is usefull if you post several articles using the same "table of
  // contents" or anything else common to several posts.
  // If you want to hide the other post, change its status to 'private' or
  // 'deprecated' and nothing is going to be added to the current post.
 
  global $DB, $tableposts;
  $query = "SELECT post_content FROM $tableposts WHERE ID = '".$matches[ 2 ]."' AND post_status <> 'private' AND post_status <> 'deprecated'";
  $post_content = $DB->get_row( $query, ARRAY_N );
  $content = $matches[ 1 ].$post_content[ 0 ].$matches[ 3 ];
}
//<<<END

Now, including the arrounding existing lines, you should see something like this in the b2evocore/_class_item.php file around lines 489-513:

Code

$content = $this->content;
    $numpages = 1;
 
 
//>>>START
// INSERT_POST Hack
 
// Insert other posts into the current one
$max_loops = 10;
while( $max_loops-- > 0 && preg_match( "/^(.*)<!--insert[_]?post[^0-9]{0,4}([0-9]{1,7})-->(.*)$/siD", $content, $matches ) )
{
  // Yes, we have to include another post to the current one.
  // This is usefull if you post several articles using the same "table of
  // contents" or anything else common to several posts.
  // If you want to hide the other post, change its status to 'private' or
  // 'deprecated' and nothing is going to be added to the current post.
 
  global $DB, $tableposts;
  $query = "SELECT post_content FROM $tableposts WHERE ID = '".$matches[ 2 ]."' AND post_status <> 'private' AND post_status <> 'deprecated'";
  $post_content = $DB->get_row( $query, ARRAY_N );
  $content = $matches[ 1 ].$post_content[ 0 ].$matches[ 3 ];
}
//<<<END
 
    if( preg_match('/[pagebreak]/', $content ) )
    {  // This is a multipage post

Usage
[list]

  • First, write a new post to be inserted into other ones. Set its status as draft.
  • Second, edit the previous post to get its numeric ID (say it is 456).
  • Third, write new or edit existing posts including and add the following code:
  • Code

    <!--insert_post 456-->
    [/list:u]

    Update
    I've just added a loop to this INSERT_POST Hack (see code above) in order to handle multiple and/or recursive post insertions. The $max_loops variable limits the maximum number of iterations of the hack in order to prevent infinite loops.

    Jul 07, 2005 15:28

    If you want to take this to the next level, you could make it a renderer plugin.

    The next level after that would be a toolbar plugin (something like the image inserter/uploader) that has a popup with a list of all the posts in the system, and then inserts the proper code.

    At my job, we developed a system with similar functionality - Each "notion" can be inserted into or linked from other notions. It makes it incredibly easy to cut down the repetitive work of maintaining a help file for a large piece of software, especially when they change a reusable piece of code that is used all over the place. All that I have to do now is just change the notion where I described it once, and it's automatigically updated everywhere :)

    Jul 07, 2005 15:51

    isaac wrote:

    If you want to take this to the next level, you could make it a renderer plugin.

    The next level after that would be a toolbar plugin (something like the image inserter/uploader) that has a popup with a list of all the posts in the system, and then inserts the proper code.

    You're right, a (set of) plug-in(s) would be a better way to implement this feature. I considered to wait until the release of [url=http://b2evolution.net]b2evolution[/url] phoenix before beginning to writing plug-ins. However, the renderer and toolbar plug-ins you talk about above will be probably straightforward to port to the next version.

    I'm going to study this hack implementation as plug-ins, following your suggestion.

    BTW, I was wondering how the plug-ins are going to be implemented into the next version, especially the installation of those. [url=http://www.dotclear.net]DotClear[/url] is perfect: easy, simple, straightforward. The installation is automagic. ;)

    Jul 07, 2005 22:56

    If you want to preview Phoenix, check out https://sourceforge.net/cvs/?group_id=85535

    You'll have to use [url=http://www.wincvs.org/]WinCVS[/url] or some other CVS client to download the codebase. From there, you can set it up on localhost or some other test server. (Or you could use the web-based CVS, but that would take a looooong time, since you'd have to download file by file.) You can log in anonymously to just download the code (so you don't have to monkey around with SSH keys and whatnot.)

    Note: Do NOT use the CVS version on a live site! You won't be able to install/upgrade properly, you might lose things, and developers will get very irate if you complain about this, since they're always warning everyone not to do it.
    CVS is for development only - if you use the CVS version to run your site, and something breaks or changes without warning, you've been warned. (I actually did that for a while, and got scolded once or twice, and then finally decided it wasn't worth it.)

    The CVS version contains a test plugin that you can use. You can also take a look at the other renderer and toolbar plugins that are included. It's mostly functional, so you should be able to test it out somewhat.

    If you do that, it'd be really cool! This would be an extremely neat feature. Thanks in advance :)

    Jul 08, 2005 00:07

    Okay, I'm going to try Phoenix in order to make some fine and nice plug-ins for [url=http://b2evolution.net]b2evolution[/url] beginning by this INSERT_POST Hack.

    (I must say I [u]hate[/u] [url=https://www.cvshome.org/]CVS[/url] especially since I discovered [url=http://www.perforce.com/]Perforce[/url]. Once you've understood the power of that tool, it's difficult to step back using such tools like [url=https://www.cvshome.org/]CVS[/url] or [url=http://msdn.microsoft.com/vstudio/previous/ssafe/]VSS[/url]. And you know what? [url=http://www.perforce.com/]Perforce[/url] is [url=http://www.perforce.com/perforce/opensource-faq.html]free for open source projects[/url]... Anyway... okay, okay, I'm going to use [url=https://www.cvshome.org/]CVS[/url] for now... ;) )

    Jul 08, 2005 02:25

    Look forward to this, as a plugin it would be classy indeed.


    Form is loading...

    CMS + forums – This forum is powered by b2evolution CMS, a complete engine for your website.