/** * The cron hook will take care of sending all the scheduled newsletters * * @param string $hook name of the hook * @param string $type type of the hook * @param string $returnvalue returnvalue of the hook * @param array $params params of the hook * * @return void */ function newsletter_cron_handler($hook, $type, $returnvalue, $params) { if (!empty($params) && is_array($params)) { $cron_ts = elgg_extract("time", $params, time()); // check for time drift if (date("i", $cron_ts) >= 30) { // example of the time: 14:59:56 // which should be the hourly cron for 15:00:00 $cron_ts = $cron_ts + 30 * 60; } // make the timestamp to an hour $newsletter_ts = mktime(date("H", $cron_ts), 0, 0, date("n", $cron_ts), date("j", $cron_ts), date("Y", $cron_ts)); $options = array("type" => "object", "subtype'" => Newsletter::SUBTYPE, "limit" => false, "metadata_name_value_pairs" => array("name" => "scheduled", "value" => $newsletter_ts)); // ignore access $ia = elgg_set_ignore_access(true); $newsletters = elgg_get_entities_from_metadata($options); // retore access elgg_set_ignore_access($ia); if (!empty($newsletters)) { foreach ($newsletters as $newsletter) { newsletter_start_commandline_sending($newsletter); } } } }
/** * The cron hook will take care of sending all the scheduled newsletters * * @param string $hook name of the hook * @param string $type type of the hook * @param string $returnvalue returnvalue of the hook * @param array $params params of the hook * * @return void */ public static function sendNewsletters($hook, $type, $returnvalue, $params) { if (empty($params) || !is_array($params)) { return; } $cron_ts = elgg_extract('time', $params, time()); // check for time drift if (date('i', $cron_ts) >= 30) { // example of the time: 14:59:56 // which should be the hourly cron for 15:00:00 $cron_ts = $cron_ts + 30 * 60; } // make the timestamp to an hour $newsletter_ts = mktime(date('H', $cron_ts), 0, 0, date('n', $cron_ts), date('j', $cron_ts), date('Y', $cron_ts)); // ignore access $ia = elgg_set_ignore_access(true); $newsletters = new \ElggBatch('elgg_get_entities_from_metadata', ['type' => 'object', 'subtype' => \Newsletter::SUBTYPE, 'limit' => false, 'metadata_name_value_pairs' => ['name' => 'scheduled', 'value' => $newsletter_ts]]); foreach ($newsletters as $newsletter) { newsletter_start_commandline_sending($newsletter); } // retore access elgg_set_ignore_access($ia); }
/** * Action file to directly send a newsletter to its recipients */ $guid = (int) get_input("guid"); $status_notification = get_input("status_notification"); $show_in_archive = (int) get_input("show_in_archive"); if (empty($guid)) { register_error(elgg_echo("InvalidParameterException:MissingParameter")); forward(REFERER); } $entity = get_entity($guid); if (empty($entity) || !$entity->canEdit()) { register_error(elgg_echo("InvalidParameterException:NoEntityFound")); forward(REFERER); } if (!elgg_instanceof($entity, "object", Newsletter::SUBTYPE)) { register_error(elgg_echo("ClassException:ClassnameNotClass", array($guid, elgg_echo("item:object:" . Newsletter::SUBTYPE)))); } $entity->scheduled = mktime(date("G"), 0, 1, date("n"), date("j"), date("Y")); $entity->show_in_archive = $show_in_archive; // status notification email address if (!empty($status_notification)) { $entity->status_notification = $status_notification; } else { unset($entity->status_notification); } newsletter_start_commandline_sending($entity); // sleep to make sure the log page has content sleep(2); system_message(elgg_echo("newsletter:action:send:success")); forward("newsletter/log/" . $entity->getGUID());