Skip to content

Instantly share code, notes, and snippets.

@bls1999
Last active July 25, 2020 06:04
Show Gist options
  • Save bls1999/e743a9126588cd88fed96d2a0e21ed1d to your computer and use it in GitHub Desktop.
Save bls1999/e743a9126588cd88fed96d2a0e21ed1d to your computer and use it in GitHub Desktop.
MyBB plugin: Advanced MyCode to embed a SoundCloud track/set.
<?php
/*
MIT License
Copyright (c) 2020 Ben Schwartz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
if (!defined('IN_MYBB')) {
die("This file cannot be accessed directly.");
}
// make sure "soundcloud_run" is called at the end of the parsing process (so that HTML parsing works)
$plugins->add_hook("parse_message_end", "soundcloud_run");
function soundcloud_info()
{
return array (
"name" => "SoundCloud Embed",
"description" => "Advanced MyCode to embed a SoundCloud track/set.",
"version" => "1.1",
"website" => "https://community.mybb.com/mods.php?action=view&pid=1392",
"author" => "Ben Schwartz",
"authorsite" => "https://jiggmin2.com/",
"compatibility" => "16*,18*"
);
}
function soundcloud_activate()
{
}
function soundcloud_deactivate()
{
}
function soundcloud_run($message)
{
$match_pattern = '#\[soundcloud\](.*?)\[\/soundcloud\]#is';
if (preg_match_all($match_pattern, $message, $matches, PREG_SET_ORDER)) {
foreach ($matches as $found) {
// get link
$url = explode('[', explode(']', $found[0])[1])[0];
$url = urlencode(htmlspecialchars($url, ENT_QUOTES));
// determine height
$height = strpos($found[0], '/sets/') !== false && strpos($found[0], '?in=') === false ? 450 : 164;
// soundcloud embedding options
$params = '&amp;color=0066cc'
.'&amp;auto_play=false'
.'&amp;hide_related=true'
.'&amp;show_comments=false'
.'&amp;show_user=true'
.'&amp;show_reposts=false';
// define replacement
$embed_html = "<iframe width='100%' height='$height' scrolling='no' frameborder='no' src='https://w.soundcloud.com/player/?url=$url$params'></iframe>";
// process replacement
$message = preg_replace($match_pattern, $embed_html, $message, 1);
}
}
return $message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment