2023-03-09, 05:23 AM
<?php
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.");
}
// Define the plugin information
function testing1_info()
{
return array(
"name" => "testing1Merger",
"description" => "This plugin allows you to merge selected threads after a specified duration.",
"website" => "",
"author" => "xdonaskelva",
"authorsite" => "",
"version" => "1.0",
"guid" => "",
"codename" => "testing1",
"compatibility" => "*"
);
}
// Install the plugin
function testing1_install()
{
global $db;
// Create the plugin task in the task manager
$task = array(
'title' => 'Merge Threads',
'file' => 'merge_threads_task.php',
'minute' => '0',
'hour' => '0',
'day' => '*',
'month' => '*',
'weekday' => '*',
'nextrun' => '0',
'enabled' => 1,
'logging' => 1
);
$db->insert_query('tasks', $task);
// Create the testing1_merge_threads table
if(!$db->table_exists('testing1_merge_threads')) {
$db->write_query("CREATE TABLE " . TABLE_PREFIX . "testing1_merge_threads (
`id` int(10) unsigned NOT NULL auto_increment,
`task_id` int(10) unsigned NOT NULL,
`thread_ids` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;");
}
}
// Check if the plugin is installed
function testing1_is_installed()
{
global $db;
return $db->table_exists('tasks');
}
// Uninstall the plugin
function testing1_uninstall()
{
global $db;
// Delete the plugin task from the task manager
$db->delete_query('tasks', "file='merge_threads_task.php'");
// Remove any plugin settings from the settings table
$db->delete_query('settings', "name LIKE 'testing1_%'");
}
// Activate the plugin
function testing1_activate()
{
global $db, $mybb;
// Create the plugin settings in the settings table
$setting_group = array(
'name' => 'testing1',
'title' => 'Delayed Thread Merge Settings',
'description' => 'Settings for the Delayed Thread Merge plugin.',
'disporder' => 5,
'isdefault' => 0
);
$gid = $db->insert_query('settinggroups', $setting_group);
$db->update_query('settings', array('gid' => $gid), "name LIKE 'testing1_%'");
rebuild_settings();
}
// Deactivate the plugin
function testing1_deactivate()
{
global $db;
// Remove the plugin settings from the settings table
$db->delete_query('settings', "name LIKE 'testing1_%'");
$db->delete_query('settinggroups', "name='testing1'");
rebuild_settings();
}
// Merge the selected threads after a specified duration
function merge_threads_delayed($tid_array, $delay) {
global $db;
// Get the current timestamp and add the delay
$timestamp = time() + $delay;
// Create a new task in the task manager to merge the threads at the specified time
$task = array(
'title' => 'Merge Threads',
'file' => 'merge_threads_task.php',
'minute' => date('i', $timestamp),
'hour' => date('H', $timestamp),
'day' => date('d', $timestamp),
'month' => date('m', $timestamp),
'weekday' => date('w', $timestamp),
'nextrun' => $timestamp,
'enabled' => 1,
'logging' => 1,
'priority' => 5
);
$db->insert_query('tasks', $task);
// Store the thread IDs and the task ID in the testing1_merge_threads table
$thread_ids = implode(',', $tid_array);
$task_id = $db->insert_id();
$db->insert_query('testing1_merge_threads', array('task_id' => $task_id, 'thread_ids' => $thread_ids));
}
// Handle the merge threads form submission
if($mybb->request_method == "post" && isset($mybb->input['merge_threads_submit'])) {
// Get the selected thread IDs and the merge delay from the form data
$tid_array = $mybb->get_input('tid', MyBB::INPUT_ARRAY);
$delay = intval($mybb->input['merge_delay']);
// Merge the selected threads after the specified delay
merge_threads_delayed($tid_array, $delay);
// Redirect back to the forum display
redirect("forumdisplay.php", "The selected threads will be merged after {$delay} seconds.");
}
// Add the merge threads form to the thread moderation options
$plugins->add_hook('showthread_start', 'merge_threads_showthread');
function merge_threads_showthread() {
global $mybb, $lang, $thread, $moderation;
// Check if the user has permission to merge threads
if(!is_moderator($thread['fid'], "canmergeposts")) {
return;
}
// Add the merge threads form to the thread moderation options
eval("\$merge_threads = \"".$templates->get("merge_threads")."\";");
$moderation .= $merge_threads;
}
// Add the merge threads form template
$plugins->add_hook('global_start', 'merge_threads_global');
function merge_threads_global() {
global $templates;
// Define the merge threads form template
$templates->add_template("
<form method=\"post\" action=\"merge_threads.php\">
<input type=\"hidden\" name=\"my_post_key\" value=\"{\$mybb->post_code}\" />
<input type=\"hidden\" name=\"action\" value=\"merge_threads\" />
<table>
<tr>
<td>
<span class=\"smalltext\"><strong>{$lang->merge_threads}</strong></span>
<div class=\"smalltext\">{$lang->merge_threads_desc}</div>
</td>
<td>
<input type=\"text\" name=\"merge_delay\" value=\"\" size=\"4\" maxlength=\"4\" /> {$lang->merge_threads_delay}
</td>
<td>
<input type=\"submit\" name=\"merge_threads_submit\" value=\"{$lang->merge_threads_submit}\" class=\"button\" />
</td>
</tr>
</table>
</form>
");
}
?>
merge_threads_task.php
<?php
require_once './global.php';
// Set the timezone to your desired timezone
date_default_timezone_set('Asia/Kolkata');
// Set the merge time in seconds (e.g. 24 hours = 86400 seconds)
$merge_time = 86400;
// Get the current time
$current_time = TIME_NOW;
// Get the time when the threads should be merged
$merge_start_time = $current_time + $merge_time;
// Get the threads to be merged (replace THREAD_ID_1 and THREAD_ID_2 with the actual thread IDs)
$thread_ids = array(THREAD_ID_1, THREAD_ID_2);
// Schedule the merge task
$task = array(
'title' => 'Merge Threads',
'description' => 'Merge threads with IDs ' . implode(',', $thread_ids),
'file' => 'merge_threads.php',
'minute' => date('i', $merge_start_time),
'hour' => date('G', $merge_start_time),
'day' => date('j', $merge_start_time),
'month' => date('n', $merge_start_time),
'weekday' => date('N', $merge_start_time),
'enabled' => 1,
);
$db->insert_query('tasks', $task);
// Output success message
echo 'Merge task scheduled successfully.';
?>
// In this code, we first include the global.php file to access MyBB's functions and database. We then set the timezone and merge time in seconds (e.g. 24 hours).
// Next, we get the current time and add the merge time to get the time when the threads should be merged. We also define the thread IDs to be merged.
// Finally, we create a task array with the necessary details for the merge_threads.php file to execute the merge operation. We insert the task into the database using MyBB's insert_query() function and output a success message.
// Note: You will need to create a separate PHP file merge_threads.php to actually perform the merge operation. The merge_threads_task.php file just schedules the task.
merge_threads.php
<?php
// Include the MyBB core files
define('IN_MYBB', 1);
require_once './global.php';
// Get the thread IDs to merge from the scheduled task's data
$thread_ids = explode(',', $mybb->settings['merge_threads_task_thread_ids']);
// Get the forum ID to move the merged thread to from the scheduled task's data
$forum_id = (int) $mybb->settings['merge_threads_task_forum_id'];
// Get the subject of the merged thread from the scheduled task's data
$subject = $mybb->settings['merge_threads_task_subject'];
// Get the reason for the merge from the scheduled task's data
$reason = $mybb->settings['merge_threads_task_reason'];
// Merge the threads
require_once MYBB_ROOT.'inc/class_moderation.php';
$moderation = new Moderation;
$moderation->merge_threads($thread_ids, $forum_id, $subject, $reason);
// Update the threads' forum IDs and redirect URLs to the merged thread
$new_tid = $moderation->tid;
foreach ($thread_ids as $tid) {
$db->update_query('threads', array('fid' => $forum_id, 'redirect' => $new_tid), 'tid = '.$tid);
}
// Log the merge in the moderation log
require_once MYBB_ROOT.'inc/functions_modcp.php';
$log_info = array(
'tid' => $new_tid,
'forum_id' => $forum_id,
'thread_subject' => $subject,
'thread_url' => get_thread_link($new_tid),
'threads_merged' => implode(',', $thread_ids)
);
log_moderator_action($log_info, 'merge');
// Redirect to the merged thread
header('Location: '.get_thread_link($new_tid));
exit;
// This code first retrieves the necessary data (thread IDs to merge, forum ID to move the merged thread to, subject of the merged thread, and reason for the merge) from the merge_threads_task.php scheduled task's data. It then uses MyBB's Moderation class to merge the threads, updates the forum IDs and redirect URLs of the merged threads, logs the merge in the moderation log, and finally redirects the user to the merged thread.
What is missing? i am new with MyBB and not able to make this plugin work on my forum for merging. please help guys