Hi all,
I'm new to coding MyBB plugins and have formed an addon which hides content when using the following BBCode:
[hide]Hidden content here...[/hide]
What does it do?
We have 2 settings in the admin control panel:
When a user uses the above BBCode:
Guests will see the following message:
A user who doesn't meet the required minimum posts will see:
A user who does meet the required minimum posts but has not replied to the topic will see:
An admin (and other ignored usergroups) would see the hidden content no matter what.
Here's my code:
Any feedback?
Regards,
I'm new to coding MyBB plugins and have formed an addon which hides content when using the following BBCode:
[hide]Hidden content here...[/hide]
What does it do?
We have 2 settings in the admin control panel:
- Ignore usergroups
- Minimum posts
When a user uses the above BBCode:
Guests will see the following message:
Quote:Please login or register to view hidden content.
A user who doesn't meet the required minimum posts will see:
Quote:Please make 5 posts to view hidden content.
A user who does meet the required minimum posts but has not replied to the topic will see:
Quote:Please reply to this topic to view hidden content.
An admin (and other ignored usergroups) would see the hidden content no matter what.
Here's my code:
<?php
/**
* Jordan Francis
* Oct 8, 2015
* Copyright 2015, All Rights Reserved
* Website:
*/
if(!defined('IN_MYBB')) die('This file cannot be accessed directly.');
// Hooks
$plugins->add_hook('showthread_start', 'hidecontent_showthread_start');
$plugins->add_hook('parse_message', 'hidecontent_parse_message');
$plugins->add_hook('parse_quoted_message', 'hidecontent_quoted_message');
function hidecontent_info()
{
return array(
'name' => 'Hide Content',
'description' => 'Hides content from guests and users without no. of posts/replying to the thread.',
'website' => 'http://mybb.com',
'author' => 'Jordan F',
'authorsite' => 'http://mybb.com',
'version' => '1.0',
'compatibility' => '18*',
'guid' => ''
);
}
function hidecontent_activate()
{
global $db;
$group = array(
'name' => 'hidecontent',
'title' => 'Hide Content',
'description' => 'Settings relating to the hide content plugin.',
'isdefault' => 0
);
$db->insert_query('settinggroups', $group);
$gid = $db->insert_id();
$settings = array(
array(
'name' => 'hidecontent_ignoregroups',
'title' => 'Ignore Usergroups',
'description' => 'Usergroups to ignore? Their usergroup ID comma seperated without spaces.',
'optionscode' => 'groupselect',
'value' => '4',
'disporder' => 1,
'gid' => intval($gid)
),
array(
'name' => 'hidecontent_minposts',
'title' => 'Post Requirement',
'description' => 'The standard amount of posts to view hidden content?',
'optionscode' => 'numeric',
'value' => '5',
'disporder' => 2,
'gid' => intval($gid)
)
);
foreach($settings as $setting)
{
$db->insert_query('settings', $setting);
}
rebuild_settings();
}
// Delete relevant settings from database
function hidecontent_deactivate()
{
global $db;
$db->delete_query('settinggroups',"name = 'hidecontent'");
$db->delete_query('settings',"name LIKE 'hidecontent_%'");
rebuild_settings();
}
function hidecontent_showthread_start()
{
global $db, $thread, $mybb, $replied;
$query = $db->simple_select('posts', 'pid', "tid='{$thread['tid']}' AND uid='{$mybb->user['uid']}' AND visible='{$thread['visible']}'", array('limit' => 1));
$replied = $db->num_rows($query);
}
function hidecontent_parse_message($message)
{
global $mybb, $post, $replied;
// User should be able to see their own hidden content
if($mybb->user['uid'] != $post['uid'])
{
// Ignore if user is within an ignored usergroup
if (!is_member($mybb->settings['hidecontent_ignoregroups'], $mybb->user))
{
$replace = '<div class="hide_box">';
// Guest?
if ($mybb->user['usergroup'] == 1)
{
$lock = true;
$replace .= 'Please <a href="member.php?action=login">login</a> or <a href="member.php?action=register">register</a> to view hidden content.';
}
else
{
// Does the user meet the minimum post requirement?
if ($mybb->user['postnum'] < intval($mybb->settings['hidecontent_minposts']))
{
$lock = true;
$replace .= "Users require at least {$mybb->settings['hidecontent_minposts']} posts to view hidden content.";
}
elseif ($replied == 0)
{
// The user has NOT replied to this thread
$lock = true;
$replace .= "Please reply to this thread to view hidden content.";
}
}
$replace .= '</div>';
}
}
// Replace hidden content with user friendly message
if($lock)
$message = preg_replace('~\[hide\](.*?)\[/hide\]~s', $replace, $message);
// When unlocked, remove the hide BBCode
else
$message = preg_replace("~\[hide\](.*?)\[/hide\]~s", "$1", $message);
return $message;
}
// For when a user quotes hidden content
function hidecontent_quoted_message(&$quoted_post)
{
$quoted_post['message'] = preg_replace('~\[hide\](.*?)\[/hide\]~s', '[b][color=#FF9C9C]Hidden content...[/color][/b]', $quoted_post['message']);
}
Any feedback?
Regards,