From d6a121ed714a3738eb673f70eaa50dac828b576f Mon Sep 17 00:00:00 2001 From: Franky Van Liedekerke Date: Mon, 23 Dec 2024 14:01:43 +0100 Subject: [PATCH] REST call added --- cli_mail.php | 12 ++++++------ eme-actions.php | 19 +++++++++++++++++++ eme-cron.php | 40 +++++++++++++++++++++++----------------- eme-mailer.php | 39 ++++++++++++++++++++++++++------------- eme-options.php | 5 ++++- readme.txt | 8 ++++++++ 6 files changed, 86 insertions(+), 37 deletions(-) diff --git a/cli_mail.php b/cli_mail.php index b156c410..49fe2dd5 100644 --- a/cli_mail.php +++ b/cli_mail.php @@ -23,12 +23,12 @@ function help( $progname ) { echo "Usage: $progname -d \n"; echo "Or : $progname --groupid=\n"; echo "-d \tCauses the email to be forwarded to the EME group with the specified email address\n"; - echo "-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=\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"; diff --git a/eme-actions.php b/eme-actions.php index b9a9d6dc..893f22c7 100644 --- a/eme-actions.php +++ b/eme-actions.php @@ -670,4 +670,23 @@ function eme_custom_dashboard_next_events() { $format_footer = ''; 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\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' ) ); + } + ) ); +} ); + ?> diff --git a/eme-cron.php b/eme-cron.php index ae65b5bf..f81f675d 100644 --- a/eme-cron.php +++ b/eme-cron.php @@ -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 ); } @@ -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' ); @@ -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 '
'; - 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 ] ) ) { diff --git a/eme-mailer.php b/eme-mailer.php index 06a47e49..1a085b73 100644 --- a/eme-mailer.php +++ b/eme-mailer.php @@ -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' ) ); @@ -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'] = "

" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the Scheduled actions submenu and configure it now.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) ) . '

'; + $ajaxResult['htmlmessage'] = "

" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the Scheduled actions 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' ) ) . '

'; } else { $ajaxResult['htmlmessage'] = "

" . __( 'The mailing has been planned.', 'events-made-easy' ) . '

'; } @@ -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'] = "

" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the Scheduled actions submenu and configure it now.', 'events-made-easy' ), admin_url( 'admin.php?page=eme-cron' ) ) . '

'; + $ajaxResult['htmlmessage'] = "

" . sprintf( __( 'The mailing has been put on the queue, but you have not yet configured the queueing. Go in the Scheduled actions 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' ) ) . '

'; } else { $ajaxResult['htmlmessage'] = "

" . __( 'The mailing has been planned.', 'events-made-easy' ) . '

'; } @@ -2443,7 +2456,7 @@ function eme_emails_page() { ?>

Scheduled actions 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 Scheduled actions 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' ) ); ?>

Scheduled actions 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 Scheduled actions 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' ) ); ?>

" . $schedule['display'] . ''; } ?> - +
+ 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 :-)