simplenews.drush.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Implements hook_drush_command().
  4. */
  5. function simplenews_drush_command() {
  6. $items = array();
  7. $items['simplenews-spool-count'] = array(
  8. 'description' => 'Print the current simplenews mail spool count',
  9. 'aliases' => array('sn-sc'),
  10. 'drupal dependencies' => array('simplenews'),
  11. 'options' => array(
  12. 'pipe' => dt('Just print the count value to allow parsing'),
  13. )
  14. );
  15. $items['simplenews-spool-send'] = array(
  16. 'description' => 'Send the defined amount of mail spool entries.',
  17. 'examples' => array(
  18. 'drush sn-ss' => dt('Send the default amount of mails, as defined by the simplenews_throttle variable.'),
  19. 'drush sn-ss 0' => dt('Send all mails.'),
  20. 'drush sn-ss 100' => dt('Send 100 mails'),
  21. ),
  22. 'options' => array(
  23. 'pipe' => dt('Just print the sent and remaining count on separate lines to allow parsing'),
  24. ),
  25. 'aliases' => array('sn-ss'),
  26. 'drupal dependencies' => array('simplenews'),
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Drush command to count the mail spool queue.
  32. */
  33. function drush_simplenews_spool_count() {
  34. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  35. $count = simplenews_count_spool();
  36. $no_description = drush_get_option('pipe');
  37. if ($no_description) {
  38. drush_print_pipe($count);
  39. }
  40. else {
  41. drush_log(dt('Current simplenews mail spool count: @count', array('@count' => $count)), 'status');
  42. }
  43. }
  44. /**
  45. * Drush command to send the mail spool queue.
  46. */
  47. function drush_simplenews_spool_send($limit = FALSE) {
  48. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  49. if ($limit === FALSE) {
  50. $limit = variable_get('simplenews_throttle');
  51. }
  52. elseif ($limit == 0) {
  53. $limit = SIMPLENEWS_UNLIMITED;
  54. }
  55. $start_time = microtime(TRUE);
  56. $sent = simplenews_mail_spool($limit);
  57. simplenews_clear_spool();
  58. simplenews_send_status_update();
  59. $durance = round(microtime(TRUE) - $start_time, 2);
  60. // Report the number of sent mails.
  61. if ($sent > 0) {
  62. $remaining = simplenews_count_spool();
  63. if (drush_get_option('pipe')) {
  64. // For pipe, print the sent first and then the remaining count, separated by a space.
  65. drush_print_pipe($sent . " " . $remaining);
  66. }
  67. else {
  68. drush_log(dt('Sent @count mails from the queue in @sec seconds.', array('@count' => $sent, '@sec' => $durance)), 'status');
  69. drush_log(dt('Remaining simplenews mail spool count: @count', array('@count' => $remaining)), 'status');
  70. }
  71. }
  72. }