Ok. I tried to make my first plugin (don't look at bad code, it is only sandbox for proof of concept, I will rewrite it after getting everything to work).
Here is my code:
<?php
// Hooks
$plugins->add_hook("global_end", "lastforumposts");
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.");
}
function lastforumposts_info()
{
global $lang;
$lang->load("lastforumposts");
return array(
"name" => $lang->plugname,
"description" => $lang->plugdesc,
"website" => "https://mybb.com/",
"author" => "Nickon",
"authorsite" => "https://mybb.com/",
"version" => "1.0.1",
"guid" => "lastforumposts",
"compatibility" => "18*"
);
}
function lastforumposts_install()
{
global $db, $lang;
$lang->load("lastforumposts");
$new_setting_group = array(
"name" => "lastforumposts",
"title" => $lang->settings_name,
"disporder" => 1,
"isdefault" => 0,
"description" => ""
);
$gid = $db->insert_query("settinggroups", $new_setting_group);
$settings[] = array(
"name" => "lastforumposts_threadcount",
"title" => $lang->num_posts_to_show,
"description" => "",
"optionscode" => "text",
"disporder" => 1,
"value" => 5,
"gid" => $gid
);
$settings[] = array(
"name" => "lastforumposts_forum_include",
"title" => $lang->forums_to_include,
"description" => "",
"optionscode" => "forumselectsingle",
"disporder" => 2,
"value" => NULL,
"gid" => $gid
);
$settings[] = array(
"name" => "lastforumposts_forum_wrapper_prefix",
"title" => $lang->wrapper_prefix,
"description" => $lang->wrapper_prefix_desc,
"optionscode" => "text",
"disporder" => 3,
"value" => "",
"gid" => $gid
);
$settings[] = array(
"name" => "lastforumposts_forum_wrapper_suffix",
"title" => $lang->wrapper_suffix,
"description" => $lang->wrapper_suffix_desc,
"optionscode" => "text",
"disporder" => 4,
"value" => "",
"gid" => $gid
);
foreach($settings as $array => $content)
{
$db->insert_query("settings", $content);
}
rebuild_settings();
}
function lastforumposts_is_installed()
{
global $db;
$query = $db->simple_select("settinggroups", "*", "name='lastforumposts'");
if($db->num_rows($query))
{
return TRUE;
}
return FALSE;
}
function lastforumposts_activate()
{
global $db, $lang;
$lang->load("lastforumposts");
$insert_array = array(
'title' => 'last_forum_posts',
'template' => $db->escape_string('{$wrapperprefix}{$thread[\'subject\']}{$wrappersufix}'),
'sid' => '-1',
'version' => '',
'dateline' => TIME_NOW
);
$db->insert_query("templates", $insert_array);
}
function lastforumposts_deactivate()
{
global $db;
$db->delete_query("templates", "title IN('last_forum_posts')");
}
function lastforumposts_uninstall()
{
global $db;
$query = $db->simple_select("settinggroups", "gid", "name='lastforumposts'");
$gid = $db->fetch_field($query, "gid");
if(!$gid) {
return;
}
$db->delete_query("settinggroups", "name='lastforumposts'");
$db->delete_query("settings", "gid=$gid");
rebuild_settings();
}
function lastforumposts()
{
global $mybb, $lang, $db, $templates, $lastforumposts;
$threadcount = (int) $mybb->settings['lastforumposts_threadcount'];
$includedforums = (int) $mybb->settings['lastforumposts_forum_include'];
$wrapperprefix = $mybb->settings['lastforumposts_forum_wrapper_prefix'];
$wrappersufix = $mybb->settings['lastforumposts_forum_wrapper_sufix'];
if(!$threadcount || $threadcount <= 0) {
$threadcount = 5;
}
if(!$wrapperprefix) {
$wrapperprefix = "";
}
if(!$wrappersufix) {
$wrappersufix = "";
}
if(!$includedforums) {
$includedforums = 0;
}
$query = $db->query("
SELECT t.*
FROM ".TABLE_PREFIX."threads t
WHERE fid = ".$includedforums."
ORDER BY t.lastpost DESC
LIMIT 0,".$threadcount
);
$lastforumposts = "";
while($thread = $db->fetch_array($query))
{
$thread['subject'] = htmlspecialchars_uni($thread['subject']);
$thread['username'] = htmlspecialchars_uni($thread['username']);
$thread['threadlink'] = get_thread_link($thread['tid']);
echo("THREAD SUBJECT: ".$thread['subject'])."<br>";
echo("USERNAME: ".$thread['username'])."<br>";
echo("THREAD LINK: ".$thread['threadlink'])."<br>";
echo("<br><br>");
eval("\$lastforumposts .= \"".$templates->get("last_forum_posts")."\";");
}
}
?>
As I said before - I want to read thread's subjects and show them somewhere on my MyBB.
Echoes return everything correctly for me and my template appears in Global templates (I have also MyTheme Templates just under the globals).
On my theme I have some jQuery component that needs a list of <a> elements to show up animated announcements.
$wrapperprefix and
$wrappersufix variables are used to write HTML code (in case of my jQuery component - I want them to be <a> and </a>).
After installing plugin I modified other template (in MyTheme Templates) and added {$lastforumposts} inside.
<div class="newsticker">
<div class="newsTicker">
<span class="news-header"><i class="fa fa-microphone"></i> Ogłoszenia</span>
<div class="news-articleWrap">
{$lastforumposts}
</div>
</div>
</div>
After refreshing the page I can't see anything div.news-articleWrap is empty inside
I don't know if I used a wrong hook or my template is bad. Maybe templates don't work between different "sets" (I mean Global against MyTheme templates)...