MyBB Community Forums

Full Version: Can't access variables in plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
What's up dear developers,
I've started working on Plugins Alerts Pack which will feature integration between the most famous MyBB plugins and MyAlerts.

I started with MySupport: I'd like to be notified when any of my threads is marked as solved/unsolved.

Everything looks ok, I've added a hook via PluginLibrary to mysupport.php file in mysupport_change_status() function which handles the status changes and my function is correctly hooked within the file.

Strange problem though: I can't access any variable within the function. Setting them as global does nothing and after some debugging I realized that every variable I'm trying to access is returning "1".

An alert is correctly added to the database so I guess the hook is working fine.

Here's my function:

function pluginspack_addAlert_MySupport()
{
	global $mybb, $db, $Alerts, $thread_info, $status;
	
	$Alerts->addAlert((int) $thread_info['uid'], 'mysupport', $thread_info['tid'], (int) $mybb->user['uid'], array(
		'status'  =>  $status,
		'tid' => $thread_info['tid'],
		'subject' => $thread_info['subject'],
		)
	);
}

I don't know why it behaves like that. Does someone know why and how to fix this? Thank you in advance.
Depending on where the hook is, the globalized variable may be available or not. E.g. within classes you can't just decide to globalize something (when using a hook inside a class) if it hasn't been globalized in the class' method.
This mean I must set to global the variables I'm going to access via the same core edit in which I add the hook?

It only happens with hooks added to another plugin. Basically I'm hooking into another plugin adding an extra hook via Pluginlibrary.
I'm confused. You should probably post your code.
Yep here it is:


function pluginspack_install()
{
	global $db, $lang, $mybb, $cache, $PL;

	if (!file_exists(PLUGINLIBRARY))
	{
		flash_message("The selected plugin could not be installed because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
		admin_redirect("index.php?module=config-plugins");
	}
	
	// check if myalerts table exist - if false, then MyAlerts is not installed, warn the user and redirect him
	if(!$db->table_exists('alerts'))
	{
		flash_message("The selected plugin could not be installed because <a href=\"http://mods.mybb.com/view/myalerts\">MyAlerts</a> is not installed. Moderation Alerts Pack requires MyAlerts to be installed in order to properly work.", "error");
		admin_redirect("index.php?module=config-plugins");
	}
	
	$PL or require_once PLUGINLIBRARY;
	
	$PL->edit_core('pluginspack', 'inc/plugins/mysupport.php',
				array(
					array('search' => '$db->update_query("threads", $status_update, $where_sql);',
                     	  'before' => '$plugins->run_hooks("mysupport_myalerts");'),
					 ),
               true);
	
    $info = pluginspack_info();
    $shadePlugins = $cache->read('shade_plugins');
    $shadePlugins[$info['name']] = array(
        'title' => $info['name'],
        'version' => $info['version']
    );
    $cache->update('shade_plugins', $shadePlugins);
	
	if (!$lang->pluginspack)
	{
		$lang->load('pluginspack');
	}
	
	$query = $db->simple_select("settinggroups", "gid", "name='myalerts'");
	$gid = intval($db->fetch_field($query, "gid"));
	
	$pluginspack_settings_1 = array(
		"name" => "myalerts_alert_mysupport",
		"title" => $lang->setting_pluginspack_alert_mysupport,
		"description" => $lang->setting_pluginspack_alert_mysupport_desc,
		"optionscode" => "yesno",
		"value" => "1",
		"disporder" => "100",
		"gid" => $gid,
	);
	
	$db->insert_query("settings", $pluginspack_settings_1);
	
	// Set our alerts on for all users by default, maintaining existing alerts values
    // Declare a data array containing all our alerts settings we'd like to add. To default them, the array must be associative and keys must be set to "on" (active) or 0 (not active)
    $possible_settings = array(
            'mysupport' => "on",
            );
    
    $query = $db->simple_select('users', 'uid, myalerts_settings', '', array());
    
    while($settings = $db->fetch_array($query))
    {
        // decode existing alerts with corresponding key values. json_decode func returns an associative array by default, we don't need to edit it
        $alert_settings = json_decode($settings['myalerts_settings']);
        
        // merge our settings with existing ones...
        $my_settings = array_merge($possible_settings, (array) $alert_settings);
        
        // and update the table cell, encoding our modified array and paying attention to SQL inj (thanks Nathan!)
        $db->update_query('users', array('myalerts_settings' => $db->escape_string(json_encode($my_settings))), 'uid='.(int) $settings['uid']);
    }

	// rebuild settings
	rebuild_settings();

}

function pluginspack_uninstall()
{
	global $db, $cache, $PL;
	
	if (!file_exists(PLUGINLIBRARY))
	{
		flash_message("The selected plugin could not be uninstalled because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
		admin_redirect("index.php?module=config-plugins");
	}
	
	$PL or require_once PLUGINLIBRARY;
	
	
	$PL->edit_core('pluginspack', 'moderation.php',
               array(),
               true);
			   	
	$db->write_query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN('myalerts_alert_mysupport')");
	
	$info = pluginspack_info();
    // delete the plugin from cache
    $shadePlugins = $cache->read('shade_plugins');
    unset($shadePlugins[$info['name']]);
    $cache->update('shade_plugins', $shadePlugins);
	// rebuild settings
	rebuild_settings();
}

// add alerts into UCP
$plugins->add_hook('myalerts_possible_settings', 'pluginspack_possibleSettings');
function pluginspack_possibleSettings(&$possible_settings)
{
	global $lang;

	if (!$lang->pluginspack)
	{
		$lang->load('pluginspack');
	}

	$_possible_settings = array('mysupport');

	$possible_settings = array_merge($possible_settings, $_possible_settings);
}

// generate text and stuff like that - fixes #1
$plugins->add_hook('myalerts_alerts_output_start', 'pluginspack_parseAlerts');
function pluginspack_parseAlerts(&$alert)
{
	global $mybb, $lang;
	
	if (!$lang->pluginspack)
	{
		$lang->load('pluginspack');
	}
	
	// MySupport
	if ($alert['alert_type'] == 'mysupport' AND $mybb->user['myalerts_settings']['mysupport'])
	{
		$alert['threadLink'] = get_thread_link($alert['content']['tid']);
		$status = $alert['content']['status'];
		if($status == 0) {
			$alert['message'] = $lang->sprintf($lang->pluginspack_mysupport_notsolved, $alert['user'], $alert['threadLink'], $alert['dateline'], $alert['content']['subject']);
		}
		elseif($status == 1) {
			$alert['message'] = $lang->sprintf($lang->pluginspack_mysupport_solved, $alert['user'], $alert['threadLink'], $alert['dateline'], $alert['content']['subject']);
		}
		$alert['rowType'] = 'mysupportAlert';
	}
}

// Generate the actual alerts

// MYSUPPORT
if ($settings['myalerts_enabled'] AND $settings['myalerts_alert_mysupport'])
{
	$plugins->add_hook('mysupport_myalerts', 'myalertsmore_addAlert_MySupport');
}
function myalertsmore_addAlert_MySupport()
{
	global $mybb, $Alerts, $multiple, $thread_info, $status_update;
	
	// $multiple, $thread_info, $status_update return "1" or NULL... :/
	
	if($multiple) {
		foreach ($thread_info as $thread) {
			$thread = get_thread($thread);
			if($thread['uid'] != $mybb->user['uid']) {
				$Alerts->addAlert((int) $thread['uid'], 'mysupport', (int) $thread['tid'], (int) $mybb->user['uid'], array(
					'status'  =>  $status_update['status'],
					'tid' => $thread['tid'],
					'subject' => $thread['subject'],
					)
				);
			}
		}
	}
	else {
		if($thread['uid'] != $mybb->user['uid']) {
			$Alerts->addAlert((int) $thread_info['uid'], 'mysupport', $thread_info['tid'], (int) $mybb->user['uid'], array(
				'status'  =>  $status_update['status'],
				'tid' => $thread_info['tid'],
				'subject' => $thread_info['subject'],
				)
			);
		}
	}
}

$multiple, $thread_info, $status_update return "1" or NULL, even if I put them in global. Do I have to globalize them also in MySupport for it to take effect? Thank you Pirata Smile
Or you could use the optional $arguments parameter for run_hooks and simply pass the variables you need directly...
Just tested and neither of them works, it keeps returning NULL Sad
It works fine. Look at how other hooks are called with arguments, e.g. the postbit hook in inc/functions_post.php
Escuse me if I didn't reply to you frostschutz but it doesn't work either. I'll try to change plugin and see if it's just a MySupport issue or it's something that affects globally.
If the hook is executed then it should work unless you are doing something wrong. Paste your code or link to code here.
Pages: 1 2