Skip to content

Commit

Permalink
REST call added
Browse files Browse the repository at this point in the history
  • Loading branch information
liedekef committed Dec 23, 2024
1 parent 0b1fb13 commit d6a121e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 37 deletions.
12 changes: 6 additions & 6 deletions cli_mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function help( $progname ) {
echo "Usage: $progname -d <email>\n";
echo "Or : $progname --groupid=<groupid>\n";
echo "-d <email>\tCauses the email to be forwarded to the EME group with the specified email address\n";
echo "-groupid=<groupid>\tCauses the email to be forwarded to the EME group with the specified ID\n";
echo "Optional provide --allowed_senders, to indicate specific email addresses that can send to the group (comma-separated) By default everyone in that group can send to the group, this option limits it to just the mentioned email addresses or the keyword 'ALL', which allows the world to send.\n";
echo "Optional provide --extra_allowed_senders, to allow extra email addresses to send to this group (comma-separated list)\n";
echo "Optional provide -f, to indicate the sender email. Normally this is extracted from the email, but programs like postfix can provide this already too and are more suited for email parsing\n";
echo "Optional provide -a, to indicate all people in the group should receive the mail, and not only those with the 'massmail' option active.\n";
echo "Optional provide --fast: by default the mails are queued inside EME and follow the general queueing rules, if you want these mails to be sent as quickly as possible add --fast\n\n";
echo "--groupid=<groupid>\tCauses the email to be forwarded to the EME group with the specified ID\n";
echo " Optional provide --allowed_senders, to indicate specific email addresses that can send to the group (comma-separated) By default everyone in that group can send to the group, this option limits it to just the mentioned email addresses or the keyword 'ALL', which allows the world to send.\n";
echo " Optional provide --extra_allowed_senders, to allow extra email addresses to send to this group (comma-separated list)\n";
echo " Optional provide -f, to indicate the sender email. Normally this is extracted from the email, but programs like postfix can provide this already too and are more suited for email parsing\n";
echo " Optional provide -a, to indicate all people in the group should receive the mail, and not only those with the 'massmail' option active.\n";
echo " Optional provide --fast: by default the mails are queued inside EME and follow the general queueing rules, if you want these mails to be sent as quickly as possible add --fast\n";
echo "Examples:\n";
echo "$progname -d mygroup@email\n";
echo "==> will forward emails to group with matching email mygroup@email\n\n";
Expand Down
19 changes: 19 additions & 0 deletions eme-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,23 @@ function eme_custom_dashboard_next_events() {
$format_footer = '</ul>';
echo eme_get_events_list(limit: 10, format: $format, format_header: $format_header, format_footer: $format_footer);
}

add_action( 'rest_api_init', function () {
register_rest_route( 'events-made-easy/v1', '/processqueue/(?P<interval>\d+)', array(
'methods' => 'GET',
'callback' => 'eme_send_queued',
'args' => array(
'interval' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
'permission_callback' => function () {
return current_user_can( get_option( 'eme_cap_send_other_mails' ) ) ||
current_user_can( get_option( 'eme_cap_send_mails' ) );
}
) );
} );

?>
40 changes: 23 additions & 17 deletions eme-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ function eme_plan_queue_mails() {
if ( get_option( 'eme_queue_mails' ) ) {
$schedules = wp_get_schedules();
// we stored the choosen schedule in the option with the same name eme_cron_send_queued
// and take hourly as sensible default if empty
// and take hourly as sensible default
$schedule = get_option( 'eme_cron_send_queued' );
if ( empty( $schedule ) || ! isset( $schedules[ $schedule ] ) ) {
$schedule = 'hourly';
update_option( 'eme_cron_send_queued', $schedule );
}
if ( ! wp_next_scheduled( 'eme_cron_send_queued' ) ) {
wp_schedule_event( time(), $schedule, 'eme_cron_send_queued' );
} else {
$current_schedule = wp_get_schedule( 'eme_cron_send_queued' );
if ( $current_schedule != $schedule ) {
wp_unschedule_hook( 'eme_cron_send_queued' );
wp_schedule_event( time(), $schedule, 'eme_cron_send_queued' );
}
}
if ( empty( $schedule ) ) {
wp_unschedule_hook( 'eme_cron_send_queued' );
} else {
if ( ! isset( $schedules[ $schedule ] ) ) {
$schedule = 'hourly';
update_option( 'eme_cron_send_queued', $schedule );
}
if ( ! wp_next_scheduled( 'eme_cron_send_queued' ) ) {
wp_schedule_event( time(), $schedule, 'eme_cron_send_queued' );
} else {
$current_schedule = wp_get_schedule( 'eme_cron_send_queued' );
if ( $current_schedule != $schedule ) {
wp_unschedule_hook( 'eme_cron_send_queued' );
wp_schedule_event( time(), $schedule, 'eme_cron_send_queued' );
}
}
}
if ( ! get_option( 'eme_cron_queue_count' ) ) {
update_option( 'eme_cron_queue_count', 50 );
}
Expand Down Expand Up @@ -280,8 +284,10 @@ function eme_cron_page() {
update_option( 'eme_cron_new_events_footer', $eme_cron_new_events_footer );
$eme_cron_queue_count = intval(get_option( 'eme_cron_queue_count' ));
$eme_cron_queued_schedule = wp_get_schedule( 'eme_cron_send_queued' );
$mail_schedule = $schedules[ $eme_cron_queued_schedule ];
$message = sprintf( __( '%s there will be a check if new events should be mailed to EME registered people (those will then be queued and send out in batches of %d %s)', 'events-made-easy' ), $new_events_schedule['display'], $eme_cron_queue_count, $mail_schedule['display'] );
if (!empty($eme_cron_queued_schedule)) {
$mail_schedule = $schedules[ $eme_cron_queued_schedule ];
$message = sprintf( __( '%s there will be a check if new events should be mailed to EME registered people (those will then be queued and send out in batches of %d %s)', 'events-made-easy' ), $new_events_schedule['display'], $eme_cron_queue_count, $mail_schedule['display'] );
}
}
} else {
$message = __( 'New events will not be mailed to EME registered people.', 'events-made-easy' );
Expand Down Expand Up @@ -372,7 +378,7 @@ function eme_cron_form( $message = '' ) {

if ( $eme_queued_count && ( ! get_option( 'eme_queue_mails' ) || ! get_option( 'eme_cron_queue_count' ) || ! wp_next_scheduled( 'eme_cron_send_queued' ) ) ) {
echo '<br>';
esc_html_e( 'WARNING: messages found in the queue but the mail queue is not configured correctly, so they will not be sent out', 'events-made-easy' );
esc_html_e( 'WARNING: messages found in the queue but the mail queue is not configured correctly, so they will not be sent out via WP. Make sure to use the registered REST API call with appropriate options from system cron to process the queue.', 'events-made-easy' );
} else {
$eme_cron_send_queued_schedule = wp_get_schedule( 'eme_cron_send_queued' );
if ( isset( $schedules[ $eme_cron_send_queued_schedule ] ) ) {
Expand Down
39 changes: 26 additions & 13 deletions eme-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,30 @@ function eme_get_queued( $now ) {
return $wpdb->get_results( $sql, ARRAY_A );
}

function eme_send_queued() {
function eme_send_queued_rest( WP_REST_Request $request ) {
$force_interval = $request['interval'];
//if (defined('REST_REQUEST')) {
// return new WP_REST_Response( $force_interval, 200 );
//}
if (is_numeric($force_interval))
eme_send_queued($force_interval);
}

function eme_send_queued($force_interval=0) {
// we'll build in a safety precaution to make sure to never surpass the schedule duration
$start_time = time();
$scheduled = wp_get_schedule( 'eme_cron_send_queued' );
if (!$scheduled) { // issue with wp cron? Then take 1 hour, to make sure this still runs ok
$scheduled = 3600;
}
$wp_schedules = wp_get_schedules();
$interval = $wp_schedules[ $scheduled ]['interval'];
// let's keep 5 seconds to ourselves (see at the end of this function)
$interval -= 5;
if (!$force_interval || !is_numeric($force_interval)) {
$scheduled = wp_get_schedule( 'eme_cron_send_queued' );
if (!$scheduled) { // issue with wp cron? Then take 1 hour, to make sure this still runs ok
$scheduled = 'hourly';
}
$wp_schedules = wp_get_schedules();
$interval = $wp_schedules[ $scheduled ]['interval'];
// let's keep 5 seconds to ourselves (see at the end of this function)
$interval -= 5;
} else {
$interval = $force_interval-5;
}

$eme_mail_send_html = get_option( 'eme_mail_send_html' );
$eme_mail_sleep = intval( get_option( 'eme_mail_sleep' ) );
Expand Down Expand Up @@ -1678,7 +1691,7 @@ function eme_send_mails_ajax_actions( $action ) {
if ( ! $res['mail_problems'] ) {
if ( $queue ) {
if ( ! wp_next_scheduled( 'eme_cron_send_queued' ) ) {
$ajaxResult['htmlmessage'] = "<div id='message' class='updated eme-message-admin'><p>" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) ) . '</p></div>';
$ajaxResult['htmlmessage'] = "<div id='message' class='updated eme-message-admin'><p>" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now or make sure to run the registered REST API call with the appropriate arguments to process the queue via system cron.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) ) . '</p></div>';
} else {
$ajaxResult['htmlmessage'] = "<div id='message' class='updated eme-message-admin'><p>" . __( 'The mailing has been planned.', 'events-made-easy' ) . '</p></div>';
}
Expand Down Expand Up @@ -1879,7 +1892,7 @@ function eme_send_mails_ajax_actions( $action ) {
if ( ! $mail_problems ) {
if ( $queue ) {
if ( ! wp_next_scheduled( 'eme_cron_send_queued' ) ) {
$ajaxResult['htmlmessage'] = "<div id='message' class='updated eme-message-admin'><p>" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) ) . '</p></div>';
$ajaxResult['htmlmessage'] = "<div id='message' class='updated eme-message-admin'><p>" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now or make sure to run the registered REST API call with the appropriate arguments to process the queue via system cron.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) ) . '</p></div>';
} else {
$ajaxResult['htmlmessage'] = "<div id='message' class='updated eme-message-admin'><p>" . __( 'The mailing has been planned.', 'events-made-easy' ) . '</p></div>';
}
Expand Down Expand Up @@ -2443,7 +2456,7 @@ function eme_emails_page() {
?>
<div class='eme-message-admin'><p>
<?php
printf( __( 'Email queueing has been activated but not yet configured. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) );
printf( __( 'Email queueing has been activated but not yet configured. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now or make sure to run the registered REST API call with the appropriate arguments to process the queue via system cron.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) );
?>
</p></div>
<?php
Expand Down Expand Up @@ -2622,7 +2635,7 @@ function eme_emails_page() {
?>
<div class='eme-message-admin'><p>
<?php
printf( __( 'Email queueing has been activated but not yet configured. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) );
printf( __( 'Email queueing has been activated but not yet configured. Go in the <a href="%s">Scheduled actions</a> submenu and configure it now or make sure to run the registered REST API call with the appropriate arguments to process the queue via system cron.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) );
?>
</p></div>
<?php
Expand Down
5 changes: 4 additions & 1 deletion eme-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,10 @@ function eme_options_page() {
print "<option $selected value='$key'>" . $schedule['display'] . '</option>';
}
?>
</select>
</select><br>
<?php
esc_html_e( 'If you plan to use the registered REST API call to send your mails, set this to "Not scheduled".', 'events-made-easy' );
?>
</td>
</tr>
<?php
Expand Down
8 changes: 8 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ Events list and calendars can be added to your blogs through widgets, shortcodes
See the FAQ section at the [Official site](https://www.e-dynamics.be/wordpress/).

== Changelog ==
= 2.5.25 () =
* Added a REST API call, in case you don't trust WP cron to process the queue. As an example, you can call:
curl --insecure --user "username:password" https://localhost/wordpress/wp-json/events-made-easy/v1/processqueue/60
==> don't user --insecure for public sites, and change "https://localhost/wordpress" by your wordpress url
==> change the "username" by your user and the "password" by an application password generated in your WP user settings
==> "60" means the script can run at most for 55 seconds (=60-5, 5 being a safety measure). Never set this higher than your cron recurrence of course
==> set the timing option for queue processing to "not scheduled" in EME, so the two don't interfere :-) But in fact: it doesn't really matter: EME is resilient enough to cope with both at the same time (but better be safe than sorry).

= 2.5.24 (2024/12/22) =
* Some more table fixes (more logical resizing)
* Last release of 2024 :-)
Expand Down

0 comments on commit d6a121e

Please sign in to comment.