diff --git a/includes/simplenews.api.php b/includes/simplenews.api.php index 5afc107..7fde931 100644 --- a/includes/simplenews.api.php +++ b/includes/simplenews.api.php @@ -67,3 +67,29 @@ function hook_simplenews_subscriber_insert($subscriber) { function hook_simplenews_subscriber_delete($subscriber) { } + +/** + * Invoked if a user is subscribed to a newsletter. + * + * @param $subscriber + * The subscriber object including all subscriptions of this user. + * + * @param $subscription + * The subscription object for this specific subscribe action. + */ +function hook_simplenews_subscribe_user($subscriber, $subscription) { + +} + +/** + * Invoked if a user is unsubscribed from a newsletter. + * + * @param $subscriber + * The subscriber object including all subscriptions of this user. + * + * @param $subscription + * The subscription object for this specific unsubscribe action. + */ +function hook_simplenews_unsubscribe_user($subscriber, $subscription) { + +} diff --git a/simplenews.module b/simplenews.module index 9acd135..f109fa4 100644 --- a/simplenews.module +++ b/simplenews.module @@ -1371,11 +1371,11 @@ function simplenews_subscribe_user($mail, $tid, $confirm = TRUE, $source = 'unkn $subscription->source = $source; simplenews_subscription_save($subscription); $subscriber->tids[$tid] = $tid; - // Execute simplenews subscribe trigger. simplenews_call_actions('subscribe', $subscriber); + + module_invoke_all('simplenews_subscribe_user', $subscriber, $subscription); } - module_invoke_all('simplenews_subscribe_user', $subscriber); return TRUE; } @@ -1436,8 +1436,9 @@ function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE, $source = 'un // Execute simplenews unsubscribe trigger simplenews_call_actions('unsubscribe', $subscriber); + + module_invoke_all('simplenews_unsubscribe_user', $subscriber, $subscription); } - module_invoke_all('simplenews_unsubscribe_user', $subscriber); return TRUE; } diff --git a/simplenews_rules/simplenews_rules.info b/simplenews_rules/simplenews_rules.info new file mode 100644 index 0000000..b7a3387 --- /dev/null +++ b/simplenews_rules/simplenews_rules.info @@ -0,0 +1,6 @@ +name = Simplenews rules +description = Provides integration with Rules module for Simplenews. +dependencies[] = simplenews +dependencies[] = rules +package = Mail +core = 7.x diff --git a/simplenews_rules/simplenews_rules.module b/simplenews_rules/simplenews_rules.module new file mode 100755 index 0000000..96cf792 --- /dev/null +++ b/simplenews_rules/simplenews_rules.module @@ -0,0 +1,44 @@ + t('Default'), + SIMPLENEWS_RULES_CONFIRMATION_YES => t('Yes'), + SIMPLENEWS_RULES_CONFIRMATION_NO => t('No'), + ); +} + +/** + * Implements hook_simplenews_unsubscribe_user(). + */ +function simplenews_rules_simplenews_unsubscribe_user($subscriber, $subscription) { + $args = array( + 'mail' => $subscriber->mail, + 'tid' => $subscription->tid, + ); + rules_invoke_event_by_args('simplenews_rules_event_unsubscribe', $args); +} + +/** + * Implements hook_simplenews_unsubscribe_user(). + */ +function simplenews_rules_simplenews_subscribe_user($subscriber, $subscription) { + $args = array( + 'mail' => $subscriber->mail, + 'tid' => $subscription->tid, + ); + rules_invoke_event_by_args('simplenews_rules_event_subscribe', $args); +} \ No newline at end of file diff --git a/simplenews_rules/simplenews_rules.rules.inc b/simplenews_rules/simplenews_rules.rules.inc new file mode 100755 index 0000000..5184d2f --- /dev/null +++ b/simplenews_rules/simplenews_rules.rules.inc @@ -0,0 +1,195 @@ + array( + 'label' => t('Subscribe an e-mail adress to a newsletter'), + 'group' => t('Simplenews'), + 'named parameter' => TRUE, + 'parameter' => array( + 'mail' => array( + 'type' => 'text', + 'label' => t('E-mail'), + 'description' => t('The e-mail address that should be subscribed.'), + ), + 'tid' => array( + 'type' => 'integer', + 'label' => t('Simplenews category'), + 'descrption' => t('For which newsletter category the subscription should happen.'), + 'options list' => 'simplenews_category_list', + ), + 'confirmation' => array( + 'type' => 'integer', + 'label' => t('Confirmation required'), + 'description' => t('Select if a confirmation is required. Default uses the default setting from the chosen newsletter category.'), + 'options list' => 'simplenews_rules_confirmation_list', + 'default value' => SIMPLENEWS_RULES_CONFIRMATION_DEFAULT, + ), + 'source' => array( + 'type' => 'string', + 'label' => t('Source'), + 'description' => t('A string to identify the source of this subscription'), + 'optional' => TRUE, + ), + 'source' => array( + 'type' => 'text', + 'label' => t('Source'), + 'description' => t('A string to identify the source of this subscription'), + 'optional' => TRUE, + 'default value' => 'rules', + ), + 'language' => array( + 'type' => 'token', + 'label' => t('Language'), + 'description' => t('If specified, the language to use for the subscription. Defaults to the default language.'), + 'options list' => 'entity_metadata_language_list', + 'optional' => TRUE, + 'default value' => LANGUAGE_NONE, + ), + ), + ), + 'simplenews_rules_action_unsubscribe' => array( + 'label' => t('Unsubscribe an e-mail adress from a newsletter'), + 'group' => t('Simplenews'), 'named parameter' => TRUE, + 'parameter' => array( + 'mail' => array( + 'type' => 'text', + 'label' => t('E-mail'), + 'description' => t('The e-mail address that should be unsubscribed.'), + ), + 'tid' => array( + 'type' => 'integer', + 'label' => t('Simplenews category'), + 'descrption' => t('For which newsletter category the unsubscription should happen.'), + 'options list' => 'simplenews_category_list', + ), + 'confirmation' => array( + 'type' => 'integer', + 'label' => t('Confirmation required'), + 'description' => t('Select if a confirmation is required. Default uses the default setting from the chosen newsletter category.'), + 'options list' => 'simplenews_rules_confirmation_list', + 'default value' => SIMPLENEWS_RULES_CONFIRMATION_DEFAULT, + ), + 'source' => array( + 'type' => 'text', + 'label' => t('Source'), + 'description' => t('A string to identify the source of this subscription'), + 'optional' => TRUE, + 'default value' => 'rules', + ), + 'language' => array( + 'type' => 'token', + 'label' => t('Language'), + 'description' => t('If specified, the language to use for the subscription. Defaults to the default language.'), + 'options list' => 'entity_metadata_language_list', + 'optional' => TRUE, + 'default value' => LANGUAGE_NONE, + ), + ), + ), + ); +} + +/** + * Implements hook_event_info(). + */ +function simplenews_rules_rules_event_info() { + return array( + 'simplenews_rules_event_subscribe' => array( + 'label' => t('A user has been subscribed'), + 'group' => t('Simplenews'), + 'variables' => array( + 'mail' => array( + 'type' => 'text', + 'label' => t('E-Mail'), + 'description' => t('The e-mail address that has been subscribed.'), + ), + 'tid' => array( + 'type' => 'integer', + 'label' => t('Simplenews category'), + 'descrption' => t('The newsletter category of the subscription.'), + 'options list' => 'simplenews_category_list', + ), + ), + ), + 'simplenews_rules_event_unsubscribe' => array( + 'label' => t('A user has been unsubscribed'), + 'group' => t('Simplenews'), + 'variables' => array( + 'mail' => array( + 'type' => 'text', + 'label' => t('E-mail'), + 'description' => t('The e-mail address that has been subscribed.'), + ), + 'tid' => array( + 'type' => 'integer', + 'label' => t('Simplenews category'), + 'descrption' => t('The newsletter category of the subscription.'), + 'options list' => 'simplenews_category_list', + ), + ), + ), + ); +} + +/** + * Action Implementation: Subscribe an e-mail adress to a Simplenews newsletter. + */ +function simplenews_rules_action_subscribe($args, $settings) { + if ($args['language'] == LANGUAGE_NONE) { + $args['language'] = NULL; + } + + $confirmation = simplenews_rules_map_confirmation($args); + + // Pass the call forward. + simplenews_subscribe_user($args['mail'], $args['tid'], $confirmation, $args['source'], $args['language']); +} + +/** + * Map args to the confrmation argument for subscribing/unsubscribing. + */ +function simplenews_rules_map_confirmation($args) { + switch ($args['confirmation']) { + case SIMPLENEWS_RULES_CONFIRMATION_YES: + $confirmation = TRUE; + break; + case SIMPLENEWS_RULES_CONFIRMATION_NO: + $confirmation = FALSE; + break; + case SIMPLENEWS_RULES_CONFIRMATION_DEFAULT: + $account = _simplenews_user_load($args['mail']); + $confirmation = simplenews_require_double_opt_in($args['tid'], $account); + break; + } + return $confirmation; +} + +/** + * Action Implementation: Unsubscribe an e-mail adress to a Simplenews newsletter. + */ +function simplenews_rules_action_unsubscribe($args, $settings) { + if ($args['language'] == LANGUAGE_NONE) { + $args['language'] = NULL; + } + + $confirmation = simplenews_rules_map_confirmation($args); + + // Pass the call forward. + simplenews_unsubscribe_user($args['mail'], $args['tid'], $confirmation, $args['source'], $args['language']); +} + +/** + * @} + */