back to phpList.org

Keeping track of number of recipients in a plugin?

development
plugins
Tags: #<Tag:0x00007f26778aa710> #<Tag:0x00007f26778aa558>

#1

I haven’t been able to find documentation on where the number of recipients is given to the plugin scope. This is likely a hidden feature, but I haven’t figured it out yet. I know there’s a counters variable in processqueue.php that gets a part of itself sent to a total variable in the same file, but I haven’t figured out how this is accessible within a plugin or anywhere else in the code for that matter.

EDIT: I realize there’s a processSendStats that sends the counters variable once every message has been sent, but it’s not clear how we’re supposed to use the data before messages process. In my scenario, you may want to know the total messages you’re planning to send before it happens so you can do custom logic for processing those messages.


#2

Interesting, it may be that a new plug-in hook will be necessary in phpList 3 to report this information at the time that you need it. Have you tried adding such a hook?


#3

I don’t think I did it in an optimal way, but here’s some bits and pieces of what I scribbled up in test files during my project.

In defaultplugin.php, after the processError function, I added:

/**
 * sendWhatWeGot
 * if mail sending plugin needs to, it will go ahead and send the data it is ready to send
 * this function is good if we need to suspend a message because it allows the plugin to finish sending what's processed already
 */
public function sendWhatWeGot()
{
	return false;
}

The idea here is that any plugin that is holding onto email data to be send in one go should override this functionality. It gets called in actions/processqueue.php. I wasn’t 100% sure where this functionality should be called, but I placed it around lines 1000 to 1020 within this code block:
$status = Sql_Fetch_Array_query(“select id,status from {$tables[‘message’]} where id = $messageid”);
if (!$status[‘id’]) {
ProcessError($GLOBALS[‘I18N’]->get(‘Message I was working on has disappeared’));
} elseif ($status[‘status’] != ‘inprocess’) {
$script_stage = 6;
foreach($GLOBALS[‘plugins’] as $pluginname => $plugin) {
if ($plugin->sendWhatWeGot()) {
break;
}
}
ProcessError($GLOBALS[‘I18N’]->get(‘Sending of this message has been suspended’));
}
flush();

That allows us to be able to hold onto emails until the campaign ends or the campaign is paused. In order to tell the plugins how many messages there should be in total, I added this code in processqueue.php:

// now we have all our users to send the message to
$counters['total_users_for_message '.$messageid] = Sql_Affected_Rows();

if ($skipped >= 10000) {
    $counters['total_users_for_message '.$messageid] -= $skipped;
}
if (isset($counters['total_users_for_message '.$messageid])) {
    $total = $counters['total_users_for_message '.$messageid];
} else {
    $total = 0;
}
foreach($GLOBALS['plugins'] as $pluginname => $plugin) {
	$plugin->setTotal($total);
}

I apologize for the poor formatting. This forum does not seem to have a code tag for formatting code. I also apologize for any misinformation about how PHPList works. I had very little knowledge of the inner workings of this framework before starting this project, and there’s probably a much better way of handling this data. This is just what worked for me.