MyBB Community Forums

Full Version: find_replace_templatesets issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm working to update my SimpleLikes plugin and am running across a bizarre issue with the find_replace_templatesets function. This is my code:

$simpleLikesJavascript = <<<HTML
$1
<script type="text/javascript">
    if (typeof jQuery == 'undefined') {
        document.write(unescape("%3Cscript src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));
    }
</script>
<script type="text/javascript" src="{\$mybb->settings['bburl']}/jscripts/like_system.js"></script>
HTML;

find_replace_templatesets(
    'headerinclude',
    '/(.*)$/',
    $simpleLikesJavascript
);

The issue is that every time I run the activate function, my JavaScript gets inserted twice. I need to replace it at the end of the template so that my custom JS has access to some of the earlier defined variables (such as my_post_key).

I can't quite work out what's causing this at all... Any ideas would be appreciated.
$1 \$1?

or remove it and the (.*) before the $?

Edit: or /$/D
You beat me frostshutz :p
the issue only occurs with trailing newlines apparently...

http://codepad.viper-7.com/1tEXEu

http://codepad.viper-7.com/bAHTrR
Yep, the following seems to work:

$simpleLikesJavascript = <<<HTML
<script type="text/javascript">
    if (typeof jQuery == 'undefined') {
        document.write(unescape("%3Cscript src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));
    }
</script>
<script type="text/javascript" src="{\$mybb->settings['bburl']}/jscripts/like_system.min.js"></script>
HTML;

find_replace_templatesets(
    'headerinclude',
    '/$/',
    $simpleLikesJavascript
);

Odd...