Support » Plugin: SyntaxHighlighter Evolved » php opening & closing tags break code blocks

  • Resolved Ewout

    (@pomegranate)


    With a recent update, some of my code blocks broke: it closed the php shortcode automatically when there were multiple php opening and closing tags in the snippet.

    Example:

    [php]
    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_due_date', 10, 2 );
    function wpo_wcpdf_due_date ($template_type, $order) {
    	if ($template_type == 'invoice') { // put due date only on invoice
    		$invoice_date = get_post_meta( $order->id, '_wcpdf_invoice_date', true );
    		$due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 14 days') );
    		?>
    		<tr class="due-date">
    			<th>Due Date:</th>
    			<td><?php echo $due_date; ?></td>
    		</tr>
    		<?php
    	}
    }
    [/php]

    would save as (note the small but significant change in the first line):

    [php][/php]
    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_due_date', 10, 2 );
    function wpo_wcpdf_due_date ($template_type, $order) {
    	if ($template_type == 'invoice') { // put due date only on invoice
    		$invoice_date = get_post_meta( $order->id, '_wcpdf_invoice_date', true );
    		$due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 14 days') );
    		?>
    		<tr class="due-date">
    			<th>Due Date:</th>
    			<td><?php echo $due_date; ?></td>
    		</tr>
    		<?php
    	}
    }
    [/php]

    This could be fixed by doing:

    [php]
    <?php
    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_due_date', 10, 2 );
    function wpo_wcpdf_due_date ($template_type, $order) {
    	if ($template_type == 'invoice') { // put due date only on invoice
    		$invoice_date = get_post_meta( $order->id, '_wcpdf_invoice_date', true );
    		$due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 14 days') );
    		?>
    		<tr class="due-date">
    			<th>Due Date:</th>
    			<td><?php echo $due_date; ?></td>
    		</tr>
    		<?php
    	}
    }
    ?>
    [/php]

    but that’s not what I want because I don’t want my customers to put the php tags in there theme functions (when blindly copy pasting).

    Is there any other way to prevent this? Is this a bug or a feature?

    https://wordpress.org/plugins/syntaxhighlighter/

Viewing 11 replies - 1 through 11 (of 11 total)
  • +1 I have the same issue with csharp highlighting. The only fix I have found for the moment is to have some form of tags at the start and end of the code eg:

    [csharp]
    <some tag>
    // all my code here
    `</some tag>
    [/csharp]

    The issue is that obviously these tags show up in the highlighted code which looks stupid. This only happened after the update. Is there a way to avoid having to use arbitrary tags?

    Plugin Contributor Alex Mills

    (@viper007bond)

    I can reproduce this bug and am investigating.

    Plugin Contributor Alex Mills

    (@viper007bond)

    Well that was interesting to debug. Basically it comes down to security improvements in core.

    The recent update to my plugin was intended to prevent my plugin’s shortcodes from being used inside of HTML tags which was causing a security vulnerability. For example, this shouldn’t be allowed:

    <strong [code]hello[/code]></strong>

    My release fixed that but now the <?php is being interpreted as an opening HTML tag and so the closing shortcode is sort of being ignored.

    So when my plugin comes along, core is parsing the [php] as a self-closing shortcode, similar to [gallery] works.

    However my plugin parses it’s shortcodes when on the way into the database in order to HTML entity escape the contents of the shortcodes, for example [code]<foo>[/code] into [code]& lt;foo& gt;[/code] (without the spaces). This is so that even if my plugin is disabled, you’ll never get bad HTML run on your site.

    To do this, it takes the content and then re-wraps it in the shortcode tags. It gets passed <foo> and returns [code]& lt;foo& gt;[/code] so that the shortcode tags stick around (normally you want shortode tags removed when they’re processed).

    So that explains where the extra [/php] is coming from.

    Anyway, I’ve opened a core bug ticket to see if this is working as intended or not: https://core.trac.wordpress.org/ticket/35856

    If it is, I’ll come up with some type of work around.

    Ewout

    (@pomegranate)

    Thank you Alex, I really appreciate the effort!

    Keep up the good work 🙂

    Plugin Contributor Alex Mills

    (@viper007bond)

    This should be fixed in version 3.2.1 which was just released. I figured out a way to work around the issue.

    Hi

    Seems, the problem still exists (v3.2.1).

    Test code:

    [sourcecode language="csharp"]
       while(sb.Length<c_min_text_size)
        sb.Append(c_template_str);
    [/sourcecode]
    Ewout

    (@pomegranate)

    I can confirm the fix works for the issue I had with the PHP opening/closing tags specifically. Thanks for the great work alex! 5 stars from me 🙂

    Ewout

    Plugin Contributor Alex Mills

    (@viper007bond)

    Seems, the problem still exists (v3.2.1).

    Hmm, working fine for me, at least via the text editor (the visual editor is kind of a mess). What’s the exact issue that’s occurring for you? I’ll need more details if I’m going to be able to help. 🙂

    Hi

    The problem with existing posts with source code.

    After installation of v3.2.1, source code displayed incorrectly.

    Need open the code in editor and save it – “sb.Length < c_min_text_size” will be saved in database as “sb.Length [html entity for <] c_min_text_size”.

    ———–
    Step for reproduction:

    1. Create new post in WP (with your v3.2.1):

    [sourcecode language="csharp"]
    while(sb.Length<c_min_text_size)
    sb.Append(c_template_str);
    [/sourcecode]

    2. Open the WP database in Workbench

    3. Select the post record:
    select * from wp_posts p where p.post_name like ‘%NEW_TEST_POST_NAME%’

    4. Replace the ‘Post_content’ field to:

    [sourcecode language="csharp"]
    while(sb.Length<c_min_text_size)
    sb.Append(c_template_str);
    [/sourcecode]

    5. Open the post in web browser.

    Plugin Contributor Alex Mills

    (@viper007bond)

    I think the support forums are messing up your code. Can you throw it on http://pastebin.com/ or some other code posting site?

    Also when was your broken post written? Was it recently, when you have version 3.2.0 installed? Or is this a very old post and version 3.2.1 has broken it?

    Posts written with plugin version 3.2.0 installed may very well have been broken and you’ll need to manually fix it.

    Posts written before a few weeks ago should still work fine. Here’s an example from my site:

    SyntaxHighlighter Evolved

    If you’re looking at the post contents in the database, then HTML entities within the shortcodes should be escaped. That is, the < in your code should be a < in the editor but it should be &lt; in the database. If you see this, then it is working correctly!

    Hi Alex.

    Forgive me for pause with answer.

    My posts with source code were created manually. I directly created the database records, when I migrated my site (~15 years old) onto WP. My WP-site uses custom theme with disabled ‘wpautop’.

    I do not mask the ‘<‘,’>’ and other symbols. Because all worked without this transfomation. May be it is my mistake.

    Before 3.2.0 all worked fine. After – no.

    I will think about correction of my database in future.

    —–
    The URL of problem source code – http://www.ibprovider.com/eng/examples/lcpi_oledb_net__c001__example_0008.html

    —–
    Regards,
    Dmitry Kovalenko.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘php opening & closing tags break code blocks’ is closed to new replies.