simplenews.drush.inc 2.5 KB

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