diff --git a/comment_notify.inc b/comment_notify.inc index 59f5f05e881cfbd9a118411cd4caaad5fd9919b1..db839510330749b75904578cc10a452dfd9945bc 100644 --- a/comment_notify.inc +++ b/comment_notify.inc @@ -2,17 +2,19 @@ /** * @file - * * Contains functions which utilize the database and other internal helpers. */ + use Drupal\comment\CommentInterface; /** * Get the notification preferences for a specific user. * - * @param integer $uid + * @param int $uid + * The user id. + * * @return mixed - * StdClass if found, else NULL + * StdClass if found, else NULL */ function comment_notify_get_user_notification_setting($uid) { $users = &drupal_static(__FUNCTION__); @@ -42,21 +44,27 @@ function comment_notify_get_user_notification_setting($uid) { return $users[$uid]; } +/** + * Returns the default values of the notification settings. + */ function comment_notify_get_default_notification_setting() { - return (object) array( + return (object) [ 'comment_notify' => \Drupal::config('comment_notify.settings')->get('enable_default.watcher'), - 'node_notify' => \Drupal::config('comment_notify.settings')->get('enable_default.entity_author') - ); + 'node_notify' => \Drupal::config('comment_notify.settings')->get('enable_default.entity_author'), + ]; } /** * Remove comment notification preferences for a user. * - * @param integer $uid - * @return boolean + * @param int $uid + * The user id. + * + * @return bool + * TRUE if the preferences were removed correctly, FALSE if weren't removed. */ function comment_notify_delete_user_notification_setting($uid) { - return (bool)db_delete('comment_notify_user_settings') + return (bool) db_delete('comment_notify_user_settings') ->condition('uid', $uid) ->execute(); } @@ -64,8 +72,11 @@ function comment_notify_delete_user_notification_setting($uid) { /** * Get a user's default preference for comment notification. * - * @param integer $uid - * @return integer + * @param int $uid + * The User ID. + * + * @return int + * Return the comment preference. */ function comment_notify_get_user_comment_notify_preference($uid) { $setting = comment_notify_get_user_notification_setting($uid); @@ -80,8 +91,11 @@ function comment_notify_get_user_comment_notify_preference($uid) { * * This is notification on nodes where the user is the author. * - * @param integer $uid - * @return integer + * @param int $uid + * The User ID. + * + * @return int + * Return the node_notify value. */ function comment_notify_get_user_node_notify_preference($uid) { $setting = comment_notify_get_user_notification_setting($uid); @@ -94,16 +108,21 @@ function comment_notify_get_user_node_notify_preference($uid) { /** * Sets the notification preferences for a specific user. * - * @param integer $uid - * @param integer $node_notification - * @param integer $comment_notification - * @return boolean + * @param int $uid + * The User ID. + * @param int $node_notification + * The node notification value. + * @param int $comment_notification + * The comment notification value. + * + * @return bool + * TRUE if the notification was set correctly. */ function comment_notify_set_user_notification_setting($uid, $node_notification = NULL, $comment_notification = NULL) { if (!$uid) { throw new Exception('Cannot set user preference, uid missing'); } - $fields = array('uid' => $uid); + $fields = ['uid' => $uid]; if (!is_null($node_notification)) { $fields['node_notify'] = $node_notification; @@ -118,7 +137,7 @@ function comment_notify_set_user_notification_setting($uid, $node_notification = else { $query = db_insert('comment_notify_user_settings'); } - return (bool)$query + return (bool) $query ->fields($fields) ->execute(); } @@ -126,70 +145,82 @@ function comment_notify_set_user_notification_setting($uid, $node_notification = /** * Add a notification against a comment. * - * @param integer $cid - * @param integer $notify + * @param int $cid + * Comment Id. + * @param int $notify + * The notification type. * @param string $notify_hash - * @param integer|NULL $notified + * The comment hash. + * @param int|null $notified + * If the user has been already notified. * - * @return boolean + * @return bool + * TRUE if the notification was added correctly. */ function comment_notify_add_notification($cid, $notify, $notify_hash, $notified) { // Check if comment already exist. $results = db_select('comment_notify', 'cn') - ->fields('cn', array('cid')) + ->fields('cn', ['cid']) ->condition('cn.cid', $cid) ->execute() ->fetchField(); // Update comment if exist. if ($results) { - return (bool)db_update('comment_notify') - ->fields(array( + return (bool) db_update('comment_notify') + ->fields([ 'notify' => $notify === NULL ? 0 : $notify, 'notify_hash' => $notify_hash, 'notified' => $notified === NULL ? 0 : $notified, - )) + ]) ->condition('cid', $cid) ->execute(); } // Create new entry. else { - return (bool)db_insert('comment_notify') - ->fields(array( + return (bool) db_insert('comment_notify') + ->fields([ 'cid' => $cid, 'notify' => $notify === NULL ? 0 : $notify, 'notify_hash' => $notify_hash, 'notified' => $notified === NULL ? 0 : $notified, - )) + ]) ->execute(); } } /** - * Remove all the notifications linked with a comment + * Remove all the notifications linked with a comment. * - * @param integer $cid - * @return boolean + * @param int $cid + * The comment ID. + * + * @return bool + * TRUE if all the notifications were removed correctly. */ function comment_notify_remove_all_notifications($cid) { - return (bool)db_delete('comment_notify') + return (bool) db_delete('comment_notify') ->condition('cid', $cid) ->execute(); } /** - * Updated a notification with a different notification type + * Updated a notification with a different notification type. + * + * @param int $cid + * The comment id. + * @param int $notify + * The value that is going to be updated. * - * @param integer $cid - * @param integer $notify - * @return boolean + * @return bool + * TRUE if the notification was updated correctly. */ function comment_notify_update_notification($cid, $notify) { - return (bool)db_update('comment_notify') - ->fields(array( + return (bool) db_update('comment_notify') + ->fields([ 'notify' => $notify === NULL ? 0 : $notify, - )) + ]) ->condition('cid', $cid) ->execute(); } @@ -197,12 +228,15 @@ function comment_notify_update_notification($cid, $notify) { /** * Get the type of notification for a comment notification record. * - * @param integer $cid - * @return integer + * @param int $cid + * The comment id. + * + * @return int + * Return the notification type. */ function comment_notify_get_notification_type($cid) { return db_select('comment_notify', 'cn') - ->fields('cn', array('notify')) + ->fields('cn', ['notify']) ->condition('cid', $cid) ->execute() ->fetchField(); @@ -211,37 +245,42 @@ function comment_notify_get_notification_type($cid) { /** * Get a list of mails which need to be contacted for a node. * - * @param integer $nid + * @param int $nid + * The node id. * @param string $comment_type + * The comment type. * * @return \Drupal\comment\CommentInterface[] * A list of comment entities. */ function comment_notify_get_watchers($nid, $comment_type) { - $cids = db_query("SELECT c.cid FROM {comment_field_data} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users_field_data} u ON c.uid = u.uid WHERE c.entity_id = :nid AND c.comment_type = :comment_type AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", array( + $cids = db_query("SELECT c.cid FROM {comment_field_data} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users_field_data} u ON c.uid = u.uid WHERE c.entity_id = :nid AND c.comment_type = :comment_type AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", [ ':nid' => $nid, ':comment_type' => $comment_type, ':status' => CommentInterface::PUBLISHED, ':notify' => COMMENT_NOTIFY_DISABLED, - ))->fetchCol(); + ])->fetchCol(); return \Drupal::entityManager()->getStorage('comment')->loadMultiple($cids); } /** - * Record that the owner of a comment notification request has already been notified. + * Records that the owner of a comment notification request has been notified. * - * @param integer $cid - * @return boolean + * @param int $comment + * The comment ID. + * + * @return bool + * True if it can be updated correctly. */ function comment_notify_mark_comment_as_notified($comment) { // First, mark the passed comment (an object, so passed by reference). $comment->notified = 1; // Next, store this fact in the DB as well. - return (bool)db_update('comment_notify') - ->fields(array( + return (bool) db_update('comment_notify') + ->fields([ 'notified' => 1, - )) + ]) ->condition('cid', $comment->id()) ->execute(); } @@ -249,15 +288,18 @@ function comment_notify_mark_comment_as_notified($comment) { /** * Unsubscribe all comment notification requests associated with an email. * - * If the email belongs to a user, it will unsubscribe all of their Comment Notify records. - * If it does not, then it will unsubscribe all anonymous users. + * If the email belongs to a user, it will unsubscribe all of their Comment + * Notify records. If it does not, then it will unsubscribe all anonymous users. * * @param string $mail - * @return boolean + * The mail that is going to be unsubscribed. + * + * @return bool + * TRUE if the comment was unsubscribed correctly. */ function comment_notify_unsubscribe_by_email($mail) { $update_query = db_update('comment_notify'); - $update_query->fields(array('notify' => 0)); + $update_query->fields(['notify' => 0]); $comment_query = \Drupal::entityQuery('comment'); @@ -269,7 +311,7 @@ function comment_notify_unsubscribe_by_email($mail) { } $update_query->condition('cid', $comment_query->execute(), 'IN'); - return (bool)$update_query->execute(); + return (bool) $update_query->execute(); } /** @@ -278,16 +320,19 @@ function comment_notify_unsubscribe_by_email($mail) { * This is used in the unsubscribe link. * * @param string $hash - * @return boolean + * The hash that identified the comment. + * + * @return bool + * Returns TRUE if the comment was unsubscribed correctly, FALSE otherwise. */ function comment_notify_unsubscribe_by_hash($hash) { $query = db_select('comment_notify', 'cn'); $query->join('comment_field_data', 'cf', 'cn.cid = cf.cid'); $query->condition('cn.notify_hash', $hash) - ->condition('cn.notify', COMMENT_NOTIFY_DISABLED, '!=') - ->fields('cn', array('cid', 'notify', 'notified')) - ->fields('cf', array('entity_id', 'uid')) - ->execute()->fetchObject(); + ->condition('cn.notify', COMMENT_NOTIFY_DISABLED, '!=') + ->fields('cn', ['cid', 'notify', 'notified']) + ->fields('cf', ['entity_id', 'uid']) + ->execute()->fetchObject(); $notification = $query->execute()->fetchObject(); if (empty($notification)) { @@ -297,24 +342,24 @@ function comment_notify_unsubscribe_by_hash($hash) { // If this notification is at the node level and the commenter has a Drupal // account, delete all notifications for this node. if (COMMENT_NOTIFY_NODE == $notification->notify && $notification->uid) { - $result = db_query("SELECT cid FROM {comment_field_data} WHERE entity_id = :entity_id AND uid = :uid", array(':entity_id' => $notification->entity_id, ':uid' => $notification->uid)); + $result = db_query("SELECT cid FROM {comment_field_data} WHERE entity_id = :entity_id AND uid = :uid", [':entity_id' => $notification->entity_id, ':uid' => $notification->uid]); $cids = $result->fetchCol(); // Update all comment notifications to be disabled. - return (bool)db_update('comment_notify') - ->fields(array( + return (bool) db_update('comment_notify') + ->fields([ 'notify' => 0, - )) + ]) ->condition('cid', $cids, 'IN') ->execute(); } else { - // Update this notification to be disabled. - return (bool)db_update('comment_notify') - ->fields(array( - 'notify' => 0, - )) - ->condition('notify_hash', $hash) - ->execute(); + // Update this notification to be disabled. + return (bool) db_update('comment_notify') + ->fields([ + 'notify' => 0, + ]) + ->condition('notify_hash', $hash) + ->execute(); } } diff --git a/comment_notify.install b/comment_notify.install index 440d120252515598f18c99d4efc66ffa5f1146fb..ea6d041cefdea509ff1ecab3daa95da53fd3edbf 100644 --- a/comment_notify.install +++ b/comment_notify.install @@ -1,7 +1,8 @@ addExpression("MD5(:salt || c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.entity_id || c.hostname || c.cid)", 'notify_hash', array(':salt' => $salt)); + $comments_select->addExpression("MD5(:salt || c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.entity_id || c.hostname || c.cid)", 'notify_hash', [':salt' => $salt]); } else { - $comments_select->addExpression("MD5(CONCAT_WS('', :salt, c.mail, COALESCE(u.mail, u.init), c.uid, c.name, c.entity_id, c.hostname, c.cid))", 'notify_hash', array(':salt' => $salt)); + $comments_select->addExpression("MD5(CONCAT_WS('', :salt, c.mail, COALESCE(u.mail, u.init), c.uid, c.name, c.entity_id, c.hostname, c.cid))", 'notify_hash', [':salt' => $salt]); } db_insert('comment_notify')->from($comments_select)->execute(); @@ -31,64 +32,72 @@ function comment_notify_install() { * Implements hook_schema(). */ function comment_notify_schema() { - $schema['comment_notify'] = array( - 'description' => t('Stores information about which commenters on the site have subscriped to followup emails.'), - 'fields' => array( - 'cid' => array( + $schema['comment_notify'] = [ + 'description' => 'Stores information about which commenters on the site have subscriped to followup emails.', + 'fields' => [ + 'cid' => [ 'type' => 'int', 'unsigned' => TRUE, 'description' => 'The comment id from {comments}.cid', 'not null' => TRUE, - 'disp-width' => '11'), - 'notify' => array( + 'disp-width' => '11', + ], + 'notify' => [ 'type' => 'int', 'description' => 'An integer indicating the type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.', 'size' => 'tiny', 'not null' => TRUE, - 'disp-width' => '11'), - 'notify_hash' => array( + 'disp-width' => '11', + ], + 'notify_hash' => [ 'type' => 'varchar', 'description' => 'A hash of unique information about the commenter. Used for unsubscribing users.', 'length' => '128', 'not null' => TRUE, - 'default' => ''), - 'notified' => array( + 'default' => '', + ], + 'notified' => [ 'type' => 'int', 'description' => 'A boolean indicator for whether or not a notification for the comment has been sent: 1 means yes, 0 means no.', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, - 'disp-width' => '11'), - ), - 'primary key' => array('cid'), - 'indexes' => array( - 'notify_hash' => array('notify_hash')), - ); - $schema['comment_notify_user_settings'] = array( - 'fields' => array( - 'uid' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'description' => 'The user id from {users}.cid', - 'not null' => TRUE, - 'disp-width' => '11'), - 'node_notify' => array( - 'type' => 'int', - 'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'disp-width' => '11'), - 'comment_notify' => array( - 'type' => 'int', - 'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'disp-width' => '11'), - ), - 'primary key' => array('uid'), - ); + 'disp-width' => '11', + ], + ], + 'primary key' => ['cid'], + 'indexes' => [ + 'notify_hash' => ['notify_hash'], + ], + ]; + $schema['comment_notify_user_settings'] = [ + 'fields' => [ + 'uid' => [ + 'type' => 'int', + 'unsigned' => TRUE, + 'description' => 'The user id from {users}.cid', + 'not null' => TRUE, + 'disp-width' => '11', + ], + 'node_notify' => [ + 'type' => 'int', + 'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + 'disp-width' => '11', + ], + 'comment_notify' => [ + 'type' => 'int', + 'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + 'disp-width' => '11', + ], + ], + 'primary key' => ['uid'], + ]; return $schema; } diff --git a/comment_notify.migrate.inc b/comment_notify.migrate.inc index 92b406faa4fc899b615315e39cd6c606ef338409..064ea54a347aa7b7a8a06225386143e3cf7a3e08 100644 --- a/comment_notify.migrate.inc +++ b/comment_notify.migrate.inc @@ -9,31 +9,35 @@ * Field handler. */ class CommentNotifyMigrationHandler extends MigrateDestinationHandler { + + /** + * {@inheritdoc} + */ public function __construct() { - $this->registerTypes(array('comment')); + $this->registerTypes(['comment']); } /** * Make the destination field visible. */ public function fields() { - return array( + return [ 'notify' => t('Comment Notify: Whether to send notifications for this comment'), 'notified' => t('Comment Notify: Whether notifications have been sent for this comment'), 'notify_hash' => t('Comment Notify: Hash representing this notification'), - ); + ]; } /** * Implements MigrateDestinationHandler::prepare(). * * @param $comment - * The comment object being prepared for saving. + * The comment object being prepared for saving. * @param $row - * Raw source data for the migration - ignored. + * Raw source data for the migration - ignored. */ public function prepare($comment, $row) { - // By default, set notifications off + // By default, set notifications off. if (!isset($comment->notify)) { $comment->notify = 0; } @@ -46,23 +50,24 @@ class CommentNotifyMigrationHandler extends MigrateDestinationHandler { * Implements MigrateDestinationHandler::complete(). * * @param $comment - * The comment object taht was just saved. + * The comment object taht was just saved. * @param $row - * Raw source data for the migration - ignored. + * Raw source data for the migration - ignored. */ public function complete($comment, $row) { if (!isset($comment->notified) || $comment->notified) { comment_notify_mark_comment_as_notified($comment); } } + } -/* - * Implementats hook_migrate_api(). +/** + * Implements hook_migrate_api(). */ function comment_notify_migrate_api() { - $api = array( + $api = [ 'api' => 2, - ); + ]; return $api; } diff --git a/comment_notify.module b/comment_notify.module index eebacd21c3cbc11c9a3ca3ee2d5ac62d8067a1dd..8ea01d89086c84f02a1c837f45dc4d35bb469d8d 100644 --- a/comment_notify.module +++ b/comment_notify.module @@ -2,10 +2,12 @@ /** * @file + * This module provides comment follow-up e-mail notifications. * - * This module provides comment follow-up e-mail notification for anonymous and registered users. + * It works for anonymous and registered users. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\comment\CommentInterface; use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Utility\Html; @@ -24,10 +26,10 @@ define('COMMENT_NOTIFY_COMMENT', 2); * Provide an array of available options for notification on a comment. */ function _comment_notify_options() { - $total_options = array( + $total_options = [ COMMENT_NOTIFY_NODE => t('All comments'), - COMMENT_NOTIFY_COMMENT => t('Replies to my comment') - ); + COMMENT_NOTIFY_COMMENT => t('Replies to my comment'), + ]; $selected_options = array_filter(\Drupal::config('comment_notify.settings')->get('available_alerts')); $available_options = array_intersect_key($total_options, $selected_options); @@ -35,7 +37,9 @@ function _comment_notify_options() { return $available_options; } - +/** + * Add the comment_notify fields in the comment form. + */ function comment_notify_form_comment_form_alter(&$form, FormStateInterface $form_state, $form_id) { $user = \Drupal::currentUser(); if (!($user->hasPermission('subscribe to comments') || $user->hasPermission('administer comments'))) { @@ -45,7 +49,7 @@ function comment_notify_form_comment_form_alter(&$form, FormStateInterface $form /** @var \Drupal\Core\Entity\EntityInterface $commented_entity */ $commented_entity = $form_state->getFormObject()->getEntity()->getCommentedEntity(); - // Only add the checkbox if this is an enabled content type + // Only add the checkbox if this is an enabled content type. $enabled_types = \Drupal::config('comment_notify.settings')->get('node_types'); if (!in_array($commented_entity->bundle(), $enabled_types)) { return; @@ -54,7 +58,8 @@ function comment_notify_form_comment_form_alter(&$form, FormStateInterface $form $available_options = _comment_notify_options(); // Add the checkbox for anonymous users. if ($user->isAnonymous()) { - // If anonymous users can't enter their e-mail don't tempt them with the checkbox. + // If anonymous users can't enter their e-mail don't tempt them with the + // checkbox. if (empty($form['author']['mail'])) { return; } @@ -64,26 +69,27 @@ function comment_notify_form_comment_form_alter(&$form, FormStateInterface $form $preference = comment_notify_get_user_comment_notify_preference($user->id()); // If you want to hide this on your site see http://drupal.org/node/322482 - $form['comment_notify_settings'] = array( + $form['comment_notify_settings'] = [ '#attached' => ['library' => ['comment_notify/comment_notify']], - ); - $form['comment_notify_settings']['notify'] = array( + ]; + $form['comment_notify_settings']['notify'] = [ '#type' => 'checkbox', '#title' => t('Notify me when new comments are posted'), '#default_value' => (bool) $preference, - ); + ]; - $form['comment_notify_settings']['notify_type'] = array( + $form['comment_notify_settings']['notify_type'] = [ '#type' => 'radios', '#options' => $available_options, '#default_value' => $preference != "none" ? $preference : 1, - ); + ]; if (count($available_options) == 1) { $form['comment_notify_settings']['notify_type']['#type'] = 'hidden'; $form['comment_notify_settings']['notify_type']['#value'] = key($available_options); } - // If this is an existing comment we set the default value based on their selection last time. + // If this is an existing comment we set the default value based on their + // selection last time. /** @var \Drupal\comment\CommentInterface $comment */ $comment = $form_state->getFormObject()->getEntity(); if (!$comment->isNew()) { @@ -100,15 +106,22 @@ function comment_notify_form_comment_form_alter(&$form, FormStateInterface $form $form['actions']['submit']['#submit'][] = '_comment_notify_submit_comment_form'; } +/** + * Checks if the values used in the comment_notify fields are valid. + */ function comment_notify_comment_validate(&$form, FormStateInterface $form_state) { $user = \Drupal::currentUser(); // We assume that if they are non-anonymous then they have a valid mail. - // For anonymous users, though, we verify that they entered a mail and let comment.module validate it is real. + // For anonymous users, though, we verify that they entered a mail and let + // comment.module validate it is real. if ($user->isAnonymous() && $form['comment_notify_settings']['notify']['#value'] && empty($form['author']['mail']['#value'])) { $form_state->setErrorByName('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.')); } } +/** + * Sends the mail alerts if necessary. + */ function comment_notify_comment_publish($comment) { // And send notifications - the real purpose of the module. _comment_notify_mailalert($comment); @@ -177,14 +190,16 @@ function comment_notify_comment_insert(CommentInterface $comment) { } } +/** + * Deletes all the notifications when a comment is deleted. + */ function comment_notify_comment_delete(CommentInterface $comment) { module_load_include('inc', 'comment_notify', 'comment_notify'); comment_notify_remove_all_notifications($comment->id()); } - /** - * Implement hook_form_alter(). + * Implements hook_form_alter(). */ function comment_notify_form_user_form_alter(&$form, FormStateInterface &$form_state, $form_id) { module_load_include('inc', 'comment_notify', 'comment_notify'); @@ -204,48 +219,48 @@ function comment_notify_form_user_form_alter(&$form, FormStateInterface &$form_s // If the user cannot create nodes nor has the 'subscribe to comments' // permission then there is no need to alter the user_form. - if ((!\Drupal::currentUser()->hasPermission('administer nodes') && $nodes === FALSE) && ($user->hasPermission( 'subscribe to comments') === FALSE)) { + if ((!\Drupal::currentUser()->hasPermission('administer nodes') && $nodes === FALSE) && ($user->hasPermission('subscribe to comments') === FALSE)) { return; } - $form['comment_notify_settings'] = array( + $form['comment_notify_settings'] = [ '#type' => 'details', '#title' => t('Comment follow-up notification settings'), '#weight' => 4, '#open' => TRUE, - ); + ]; if (\Drupal::currentUser()->hasPermission('administer nodes') || $nodes) { - $form['comment_notify_settings']['node_notify'] = array( + $form['comment_notify_settings']['node_notify'] = [ '#type' => 'checkbox', '#title' => t('Receive content follow-up notification e-mails'), '#default_value' => isset($notify_settings->node_notify) ? $notify_settings->node_notify : NULL, - '#description' => t('Check this box to receive an e-mail notification for follow-ups on your content. You can not disable notifications for individual threads.') - ); + '#description' => t('Check this box to receive an e-mail notification for follow-ups on your content. You can not disable notifications for individual threads.'), + ]; } else { - $form['comment_notify_settings']['node_notify'] = array( + $form['comment_notify_settings']['node_notify'] = [ '#type' => 'hidden', '#value' => COMMENT_NOTIFY_DISABLED, - ); + ]; } if ($user->hasPermission('subscribe to comments')) { $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications'); $available_options += _comment_notify_options(); - $form['comment_notify_settings']['comment_notify'] = array( + $form['comment_notify_settings']['comment_notify'] = [ '#type' => 'select', '#title' => t('Receive comment follow-up notification e-mails'), - '#default_value' => isset ($notify_settings->comment_notify) ? $notify_settings->comment_notify : NULL, + '#default_value' => isset($notify_settings->comment_notify) ? $notify_settings->comment_notify : NULL, '#options' => $available_options, - '#description' => t("Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don't want follow-up mails anymore - i.e. for very popular posts.") - ); + '#description' => t("Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don't want follow-up mails anymore - i.e. for very popular posts."), + ]; } else { - $form['comment_notify_settings']['comment_notify'] = array( + $form['comment_notify_settings']['comment_notify'] = [ '#type' => 'hidden', '#value' => COMMENT_NOTIFY_DISABLED, - ); + ]; } $form['actions']['submit']['#submit'][] = '_comment_notify_submit_user_form'; } @@ -268,10 +283,11 @@ function _comment_notify_submit_user_form(array &$form, FormStateInterface $form /** * Implements hook_user_delete(). */ -function comment_notify_user_predelete(\Drupal\Core\Entity\EntityInterface $entity) { +function comment_notify_user_predelete(EntityInterface $entity) { // This hook is invoked when the account is deleted. comment_notify_remove_user_settings($entity->id()); } + /** * Implements hook_user_cancel(). */ @@ -281,7 +297,7 @@ function comment_notify_user_cancel($edit, $account, $method) { } /** - * Remove the user settings of of the user with the given $uid + * Remove the user settings of of the user with the given $uid. * * @param int $uid * The user id. @@ -300,7 +316,7 @@ function comment_notify_comment_load($comments) { $query->join('comment_field_data', 'c', 'c.cid = cn.cid'); $query->leftJoin('users_field_data', 'u', 'c.uid = u.uid'); $query->condition('c.cid', array_keys($comments), 'IN'); - $query->fields('cn', array('cid', 'notify', 'notify_hash', 'notified')); + $query->fields('cn', ['cid', 'notify', 'notify_hash', 'notified']); $query->addField('c', 'mail', 'cmail'); $query->addField('u', 'init', 'uinit'); $query->addField('u', 'mail', 'umail'); @@ -343,7 +359,7 @@ function _comment_notify_mailalert(CommentInterface $comment) { /** @var \Drupal\node\NodeInterface $node */ $node = \Drupal::entityManager()->getStorage('node')->load($nid); - // No mails if this is not an enabled content type + // No mails if this is not an enabled content type. $enabled_types = $config->get('node_types'); if (!in_array($node->bundle(), $enabled_types) && !empty($enabled_types)) { return; @@ -358,14 +374,15 @@ function _comment_notify_mailalert(CommentInterface $comment) { else { $comment_mail = $comment->getAuthorEmail(); } - $sent_to = array(); + $sent_to = []; // Send to a subscribed author if they are not the current commenter. $author = $node->getOwner(); $author_notify_settings = comment_notify_get_user_notification_setting($author->id()) ?: comment_notify_get_default_notification_setting(); // Do they explicitly want this? Or is it default to send to users? - // Is the comment author not the node author? Do they have access? Do they have an email (e.g. anonymous)? + // Is the comment author not the node author? Do they have access? Do they + // have an email (e.g. anonymous)? if ( ( (!empty($author_notify_settings->node_notify) && $author_notify_settings->node_notify == 1) @@ -405,15 +422,20 @@ function _comment_notify_mailalert(CommentInterface $comment) { } if ($mail != $comment_mail && !in_array(strtolower($mail), $sent_to) && ($alert->getOwnerId() != $comment->getOwnerId() || $alert->getOwnerId() == 0)) { - $message = array(); + $message = []; - // Make sure they have access to this node before showing a bunch of node information. + // Make sure they have access to this node before showing a bunch of node + // information. if (!$node->access('view', $recipient_user)) { continue; } $raw_values = $config->get('mail_templates.watcher'); - $token_data = ['comment' => $comment, 'node' => $node, 'comment-subscribed' => $alert]; + $token_data = [ + 'comment' => $comment, + 'node' => $node, + 'comment-subscribed' => $alert, + ]; $message['subject'] = PlainTextOutput::renderFromHtml(\Drupal::token()->replace($raw_values['subject'], $token_data)); $message['body'] = \Drupal::token()->replace($raw_values['body'], $token_data); @@ -467,29 +489,30 @@ function comment_notify_get_unsubscribe_url(CommentInterface $comment) { } return NULL; } + /** * Implements hook_field_extra_fields(). */ function comment_notify_entity_extra_field_info() { module_load_include('inc', 'comment_notify', 'comment_notify'); - $extras = array(); + $extras = []; foreach (\Drupal::config('comment_notify.settings')->get('node_types') as $node_type) { $comment_field = FieldConfig::loadByName('node', $node_type, 'comment'); if ($comment_field) { $comment_type = $comment_field->getSetting('comment_type'); - $extras['comment'][$comment_type]['form']['comment_notify_settings'] = array( + $extras['comment'][$comment_type]['form']['comment_notify_settings'] = [ 'label' => t('Comment Notify settings'), - 'description' => t('@node_type settings for Comment Notify', array('@node_type' => NodeType::load($node_type)->label())), + 'description' => t('@node_type settings for Comment Notify', ['@node_type' => NodeType::load($node_type)->label()]), 'weight' => 1, - ); + ]; } } - $extras['user']['user']['form']['comment_notify_settings'] = array( + $extras['user']['user']['form']['comment_notify_settings'] = [ 'label' => t('Comment Notify settings'), 'description' => t('User settings for Comment Notify'), 'weight' => 4, - ); + ]; return $extras; } diff --git a/comment_notify.tokens.inc b/comment_notify.tokens.inc index fcb66a6569abae9733967c3fad66a2322a7731ef..1b2e206561473306c7d80ca1e4d63554214310a1 100644 --- a/comment_notify.tokens.inc +++ b/comment_notify.tokens.inc @@ -4,6 +4,7 @@ * @file * Builds placeholder replacement tokens for comment_notify.module. */ + use Drupal\Core\Render\BubbleableMetadata; /** @@ -11,18 +12,18 @@ use Drupal\Core\Render\BubbleableMetadata; */ function comment_notify_token_info() { // Comment tokens. - $info['tokens']['comment']['unsubscribe-url'] = array( + $info['tokens']['comment']['unsubscribe-url'] = [ 'name' => t('Unsubscribe URL'), 'description' => t('The URL to disable notifications for the comment.'), 'type' => 'url', - ); + ]; // Comment subscriber token type (extends the comment token type). - $info['types']['comment-subscribed'] = array( + $info['types']['comment-subscribed'] = [ 'name' => t('Subscribed comment'), 'description' => t('Tokens related to a comment that is subscribed to new comments.'), 'type' => 'comment', - ); + ]; return $info; } @@ -30,8 +31,8 @@ function comment_notify_token_info() { /** * Implements hook_tokens(). */ -function comment_notify_tokens($type, $tokens, array $data = array(), array $options = array(), BubbleableMetadata $bubbleable_metadata) { - $url_options = array('absolute' => TRUE); +function comment_notify_tokens($type, $tokens, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata) { + $url_options = ['absolute' => TRUE]; if (isset($options['language'])) { $url_options['language'] = $options['language']; $language_code = $options['language']->language; @@ -41,7 +42,7 @@ function comment_notify_tokens($type, $tokens, array $data = array(), array $opt } $sanitize = !empty($options['sanitize']); - $replacements = array(); + $replacements = []; if ($type == 'comment' && !empty($data['comment'])) { $comment = $data['comment']; @@ -58,13 +59,13 @@ function comment_notify_tokens($type, $tokens, array $data = array(), array $opt // [comment:unsubscribe-url:*] chained token replacements. if (($unsubscribe_url_tokens = \Drupal::token()->findWithPrefix($tokens, 'unsubscribe-url')) && $unsubscribe_url = comment_notify_get_unsubscribe_url($comment)) { - $replacements += \Drupal::token()->generate('url', $unsubscribe_url_tokens, array('path' => $unsubscribe_url), $options, $bubbleable_metadata); + $replacements += \Drupal::token()->generate('url', $unsubscribe_url_tokens, ['path' => $unsubscribe_url], $options, $bubbleable_metadata); } } // Comment subscriber tokens (pass through to comment token replacement). if ($type == 'comment-subscribed' && !empty($data['comment-subscribed'])) { - $replacements += \Drupal::token()->generate('comment', $tokens, array('comment' => $data['comment-subscribed']), $options, $bubbleable_metadata); + $replacements += \Drupal::token()->generate('comment', $tokens, ['comment' => $data['comment-subscribed']], $options, $bubbleable_metadata); } return $replacements; diff --git a/src/Controller/CommentNotifyController.php b/src/Controller/CommentNotifyController.php index 0b7bb85324be57df3b8a2b2b376697b3bb0252f6..291cf502bddb35ed67b8102dec2aed5682d46ca7 100644 --- a/src/Controller/CommentNotifyController.php +++ b/src/Controller/CommentNotifyController.php @@ -15,7 +15,8 @@ class CommentNotifyController extends ControllerBase { * @param string $hash * A hash identifying the notification entry to disable. * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * @return array + * A renderable array. */ public function disable($hash) { module_load_include('inc', 'comment_notify', 'comment_notify'); diff --git a/src/Form/CommentNotifySettings.php b/src/Form/CommentNotifySettings.php index 0b7c3b4dbb6d9201a0923fbbcff50b210e5c2ed0..cacd43382118e850aeb3c029dba8c6f4ccdc8059 100644 --- a/src/Form/CommentNotifySettings.php +++ b/src/Form/CommentNotifySettings.php @@ -29,6 +29,9 @@ class CommentNotifySettings extends ConfigFormBase { return ['comment_notify.settings']; } + /** + * {@inheritdoc} + */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('comment_notify.settings'); @@ -150,6 +153,9 @@ class CommentNotifySettings extends ConfigFormBase { return parent::buildForm($form, $form_state); } + /** + * {@inheritdoc} + */ public function validateForm(array &$form, FormStateInterface $form_state) { if (!array_filter($form_state->getValue('available_alerts'))) { $form_state->setErrorByName('available_alerts', 'You must enable at least one subscription mode.'); @@ -164,8 +170,20 @@ class CommentNotifySettings extends ConfigFormBase { ->set('node_types', array_keys(array_filter($form_state->getValue('node_types')))) ->set('available_alerts', $form_state->getValue('available_alerts')) ->set('enable_default', $form_state->getValue('enable_default')) - ->set('mail_templates.watcher.body', $form_state->getValue(['mail_templates', 'watcher', 'body'])) - ->set('mail_templates.entity_author.body', $form_state->getValue(['mail_templates', 'entity_author', 'body'])) + ->set('mail_templates.watcher.body', $form_state->getValue( + [ + 'mail_templates', + 'watcher', + 'body', + ] + )) + ->set('mail_templates.entity_author.body', $form_state->getValue( + [ + 'mail_templates', + 'entity_author', + 'body', + ] + )) ->save(); parent::submitForm($form, $form_state); } diff --git a/src/Form/CommentNotifyUnsubscribe.php b/src/Form/CommentNotifyUnsubscribe.php index 34afda363c633282165d318d3097f3d307ea4b8c..473d9e9d9d79b0d97a9558386836219fabb43748 100644 --- a/src/Form/CommentNotifyUnsubscribe.php +++ b/src/Form/CommentNotifyUnsubscribe.php @@ -4,7 +4,6 @@ namespace Drupal\comment_notify\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Render\Element; /** * Unsubscribe form for Comment Notify. @@ -44,7 +43,7 @@ class CommentNotifyUnsubscribe extends FormBase { module_load_include('inc', 'comment_notify', 'comment_notify'); $email = trim($form_state->getValue(['email'])); $comments = comment_notify_unsubscribe_by_email($email); - // Update the admin about the state of this comment notification subscription. + // Update the admin about the state of the subscription. if ($comments == 0) { drupal_set_message($this->t("There were no active comment notifications for that email.")); } diff --git a/tests/src/Functional/CommentNotifyAnonymousTest.php b/tests/src/Functional/CommentNotifyAnonymousTest.php index b35a144fe38507bc50e07fcc3b6ea6fe433a4d59..179b8b291d98ea7c7f23371e6f683ee311bf50da 100644 --- a/tests/src/Functional/CommentNotifyAnonymousTest.php +++ b/tests/src/Functional/CommentNotifyAnonymousTest.php @@ -11,6 +11,9 @@ use Drupal\Core\Session\AccountInterface; */ class CommentNotifyAnonymousTest extends CommentNotifyTestBase { + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); @@ -28,8 +31,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase { } /** - * Tests that the mail must be required if an anonymous user wants - * to receive notifications. + * Tests that the mail is required for anonymous users. */ public function testMail() { /** @var \Drupal\node\Entity\Node $node */ @@ -146,18 +148,6 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase { ); $captured_emails = $this->container->get('state')->get('system.test_mail_collector'); $this->assertEmpty($captured_emails, 'No notifications has been sent.'); - - - // @TODO Move this test to the settings form. -// \Drupal::configFactory()->getEditable('comment_notify.settings')->set('available_alerts', [1 => FALSE, 2 => TRUE])->save(); -// $subscribe_0 = ['notify' => TRUE]; -// $contact_0 = ['mail' => $this->getRandomEmailAddress()]; -// $anonymous_comment_0 = $this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe_0, $contact_0); -// -// // Confirm that the notification is saved. -// $result = comment_notify_get_notification_type($anonymous_comment_0['id']); -// $this->assertEquals($result, 2, 'Notify selection option 0 is saved properly.'); - } } diff --git a/tests/src/Functional/CommentNotifyTestBase.php b/tests/src/Functional/CommentNotifyTestBase.php index 65cf6ee368bc5dc5018f02cd201f4d1d80d4eae5..b0193c6ef36892378e0561a06f82c42cf42253df 100644 --- a/tests/src/Functional/CommentNotifyTestBase.php +++ b/tests/src/Functional/CommentNotifyTestBase.php @@ -19,7 +19,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase { /** * Admin User. * - * @var \Drupal\user\Entity\User $adminUser + * @var \Drupal\user\Entity\User */ protected $adminUser; @@ -64,8 +64,8 @@ abstract class CommentNotifyTestBase extends BrowserTestBase { /** * Post comment. * - * @param \Drupal\node\NodeInterface $node - * Node to post comment on. + * @param string $url + * The url where the comment will be submitted. * @param string $subject * Comment subject. * @param string $comment @@ -77,9 +77,9 @@ abstract class CommentNotifyTestBase extends BrowserTestBase { * array of values to set contact info. * * @return array|bool - * return an array with the comment or false if the post comment fails. + * return an array with the comment or false if the post comment fails. */ - protected function postComment($url, $subject, $comment, $notify, $contact = NULL) { + protected function postComment($url, $subject, $comment, array $notify, $contact = NULL) { $edit = []; $edit['subject[0][value]'] = $subject; $edit['comment_body[0][value]'] = $comment; @@ -94,7 +94,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase { $this->drupalPostForm($url, $edit, t('Save')); - $match = array(); + $match = []; // Get comment ID. preg_match('/#comment-([^"]+)/', $this->getURL(), $match); @@ -119,12 +119,12 @@ abstract class CommentNotifyTestBase extends BrowserTestBase { * Checks current page for specified comment. * * @param object $comment - * Comment object. + * Comment object. * @param bool $reply - * The comment is a reply to another comment. + * The comment is a reply to another comment. * * @return bool - * Comment found. + * Comment found. */ protected function commentExists($comment, $reply = FALSE) { if ($comment && is_object($comment)) { @@ -151,7 +151,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase { /** * Returns a randomly generated valid email address. * - * @return string. + * @return string * A random email. */ public function getRandomEmailAddress() { diff --git a/tests/src/Functional/CommentNotifyUserPreferencesTest.php b/tests/src/Functional/CommentNotifyUserPreferencesTest.php index 5206031e4e0ac9ed2be2feaf74c297d160c32ad3..f11644d7a99c12b4f706ef01a9edabd1b2c4a99c 100644 --- a/tests/src/Functional/CommentNotifyUserPreferencesTest.php +++ b/tests/src/Functional/CommentNotifyUserPreferencesTest.php @@ -14,12 +14,14 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase { /** * Authenticated User. * - * @var \Drupal\user\Entity\User $authenticatedUser + * @var \Drupal\user\Entity\User */ protected $authenticatedUser; /** * Permissions required by the module. + * + * @var array */ protected $permissions = [ 'post comments', @@ -27,14 +29,18 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase { 'subscribe to comments', ]; + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); $this->authenticatedUser = $this->drupalCreateUser($this->permissions); } /** - * The User's comment notify box should display different options depending - * the permissions of the user. + * Tests that the comment notify box is displayed correctly. + * + * It should display different options depending the permissions of the user. */ public function testUserCommentNotifyBox() { // The user hasn't the subscribe to comments permission nor the 'administer @@ -125,6 +131,9 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase { $this->drupalLogout(); } + /** + * Tests the "Comment Follow-up notifications" options. + */ public function testsCommentFollowUpsNotifications() { $this->drupalLogin($this->authenticatedUser); $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString()); @@ -171,7 +180,7 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase { // Tests that the option is present in the user profile if the user has the // 'administer nodes' permission. - $permissions = array_merge($this->permissions,['administer nodes']); + $permissions = array_merge($this->permissions, ['administer nodes']); $this->authenticatedUser = $this->createUser($permissions); $this->drupalLogin($this->authenticatedUser); $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString()); @@ -243,8 +252,7 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase { } /** - * Test that when the user account is canceled or deleted all the settings - * related with the CommentNotify module are deleted. + * Tests that when a user is canceled all the notifications are deleted. */ public function testUserCancelAccount() { $cancel_method_options = [