From ff00574623b45a8526760127e11c42595bd7f222 Mon Sep 17 00:00:00 2001 From: Micha Ober Date: Mon, 6 Nov 2023 09:45:12 +0100 Subject: [PATCH] Use short array syntax --- acp/discord_notifications_module.php | 28 ++++----- event/notification_event_listener.php | 61 ++++++++++--------- migrations/create_tables.php | 38 ++++++------ migrations/extension_installation.php | 86 +++++++++++++-------------- notification_service.php | 24 ++++---- 5 files changed, 121 insertions(+), 116 deletions(-) diff --git a/acp/discord_notifications_module.php b/acp/discord_notifications_module.php index 90736bb..53035e5 100644 --- a/acp/discord_notifications_module.php +++ b/acp/discord_notifications_module.php @@ -125,7 +125,7 @@ public function main($id, $mode) } // Assign template values so that the page reflects the state of the extension settings - $this->template->assign_vars(array( + $this->template->assign_vars([ 'DN_MASTER_ENABLE' => $this->config['discord_notifications_enabled'], 'DN_POST_PREVIEW_LENGTH' => $this->config['discord_notifications_post_preview_length'], 'DN_TEST_MESSAGE_TEXT' => $this->language->lang('DN_TEST_MESSAGE_TEXT'), @@ -153,7 +153,7 @@ public function main($id, $mode) 'DN_CURL_AVAILABLE' => $this->curl_available, 'U_ACTION' => $this->u_action, - )); + ]); } /** @@ -363,10 +363,10 @@ private function generate_webhook_section() while ($row = $this->db->sql_fetchrow($result)) { - $tpl_row = array( + $tpl_row = [ 'ALIAS' => $row['alias'], 'URL' => $row['url'], - ); + ]; $this->template->assign_block_vars('webhookrow', $tpl_row); } $this->db->sql_freeresult($result); @@ -394,23 +394,23 @@ private function generate_forum_section() // Category forums are displayed for organizational purposes, but have no configuration if ($row['forum_type'] == FORUM_CAT) { - $tpl_row = array( + $tpl_row = [ 'S_IS_CAT' => true, 'FORUM_NAME' => $row['forum_name'], - 'PARENTS' => '', - ); + 'PARENTS' => '', + ]; $this->template->assign_block_vars('forumrow', $tpl_row); } else if ($row['forum_type'] == FORUM_POST) { // The labels for all the inputs are constructed based on the forum IDs to make it easy to know which - $tpl_row = array( - 'S_IS_CAT' => false, - 'FORUM_NAME' => $row['forum_name'], - 'FORUM_ID' => $row['forum_id'], - 'ALIAS' => $row['discord_notifications'], - 'PARENTS' => '', - ); + $tpl_row = [ + 'S_IS_CAT' => false, + 'FORUM_NAME' => $row['forum_name'], + 'FORUM_ID' => $row['forum_id'], + 'ALIAS' => $row['discord_notifications'], + 'PARENTS' => '', + ]; $parents = get_forum_parents($row); if (is_array($parents)) { diff --git a/event/notification_event_listener.php b/event/notification_event_listener.php index c927595..af5e9d6 100644 --- a/event/notification_event_listener.php +++ b/event/notification_event_listener.php @@ -79,7 +79,7 @@ public function __construct( */ static public function getSubscribedEvents() { - return array( + return [ // This event is used for performing actions directly after a post or topic has been submitted. 'core.submit_post_end' => 'handle_post_submit_action', // This event is used for performing actions directly after a post or topic has been deleted. @@ -100,7 +100,7 @@ static public function getSubscribedEvents() 'core.approve_topics_after' => 'handle_topic_approval_action', // Post approval 'core.approve_posts_after' => 'handle_post_approval_action', - ); + ]; } // ---------------------------------------------------------------------------- @@ -179,7 +179,7 @@ public function handle_post_submit_action($event) } // Build an array of the event data that we may need to pass along to the function that will construct the notification message - $post_data = array( + $post_data = [ 'user_id' => $event['data']['poster_id'], 'user_name' => $event['username'], 'forum_id' => $event['data']['forum_id'], @@ -192,7 +192,7 @@ public function handle_post_submit_action($event) 'edit_user_name' => $this->language->lang('UNKNOWN_USER'), 'edit_reason' => $event['data']['post_edit_reason'], 'content' => $event['data']['message'], - ); + ]; if ($post_data['edit_user_id'] == $post_data['user_id']) { @@ -254,7 +254,7 @@ public function handle_post_delete_action($event) // Build an array of the event data that we may need to pass along to the function that will construct the notification message. // Note that unfortunately, the event data does not give us any information indicating which user deleted the post. - $post_data = array( + $post_data = [ 'user_id' => $event['data']['poster_id'], 'user_name' => $this->language->lang('UNKNOWN_USER'), 'forum_id' => $event['forum_id'], @@ -263,7 +263,7 @@ public function handle_post_delete_action($event) 'topic_title' => $this->language->lang('UNKNOWN_TOPIC'), 'post_id' => $event['post_id'], 'delete_reason' => $event['softdelete_reason'], - ); + ]; // Fetch the forum name, topic title, and user name using the respective IDs. $forum_name = $this->notification_service->query_forum_name($post_data['forum_id']); @@ -319,13 +319,14 @@ public function handle_topic_delete_action($event) } // Copy over the data necessary to generate the notification into a new array - $delete_data = array(); - $delete_data['forum_id'] = $query_data['forum_id']; - $delete_data['forum_name'] = $query_data['forum_name']; - $delete_data['topic_title'] = $query_data['topic_title']; - $delete_data['topic_post_count'] = $query_data['topic_posts_approved']; - $delete_data['user_id'] = $query_data['topic_poster']; - $delete_data['user_name'] = $query_data['topic_first_poster_name']; + $delete_data = [ + 'forum_id' => $query_data['forum_id'], + 'forum_name' => $query_data['forum_name'], + 'topic_title' => $query_data['topic_title'], + 'topic_post_count' => $query_data['topic_posts_approved'], + 'user_id' => $query_data['topic_poster'], + 'user_name' => $query_data['topic_first_poster_name'], + ]; $this->notify_topic_deleted($delete_data, $webhook_url); } @@ -352,22 +353,24 @@ public function handle_lock_action($event) // Get the ID needed to access $event['data'], then extract all relevant data from the event that we need to generate the notification $id = array_slice($event['ids'], -1)[0]; - - $lock_data = array(); - $lock_data['forum_id'] = $event['data'][$id]['forum_id']; - $lock_data['forum_name'] = $event['data'][$id]['forum_name']; - $lock_data['post_id'] = $event['data'][$id]['post_id'] ?? ''; // only used for post_[un]lock events - $lock_data['post_subject'] = $event['data'][$id]['post_subject'] ?? ''; // only used for post_[un]lock events - $lock_data['topic_id'] = $event['data'][$id]['topic_id']; - $lock_data['topic_title'] = $event['data'][$id]['topic_title']; - // Two sets of user data captured: one for the post (if applicable) and one for the user that started the topic - $lock_data['post_user_id'] = $event['data'][$id]['poster_id'] ?? ''; // only used for post_[un]lock events - $lock_data['post_user_name'] = $event['data'][$id]['username'] ?? ''; // only used for post_[un]lock events - $lock_data['topic_user_id'] = $event['data'][$id]['topic_poster']; - $lock_data['topic_user_name'] = $event['data'][$id]['topic_first_poster_name']; + $event_data = $event['data'][$id]; + + $lock_data = [ + 'forum_id' => $event_data['forum_id'], + 'forum_name' => $event_data['forum_name'], + 'topic_id' => $event_data['topic_id'], + 'topic_title' => $event_data['topic_title'], + 'topic_user_id' => $event_data['topic_poster'], + 'topic_user_name' => $event_data['topic_first_poster_name'], + // only used for post_[un]lock events, not always set + 'post_id' => $event_data['post_id'] ?? '', + 'post_subject' => $event_data['post_subject'] ?? '', + 'post_user_id' => $event_data['poster_id'] ?? '', + 'post_user_name' => $event_data['username'] ?? '', + ]; // If the forum the post was made in does not have notifications enabled or the topic/poar is not visible, do nothing more. - $topic_visibile = $event['data'][$id]['topic_visibility'] == 1; + $topic_visibile = $event_data['topic_visibility'] == 1; $webhook_url = $this->notification_service->get_forum_notification_url($lock_data['forum_id']); if (!$webhook_url || !$topic_visibile) { @@ -429,7 +432,7 @@ public function handle_user_add_action($event) */ public function handle_user_activate_action($event) { - $user_data = array(); + $user_data = []; if ($event['user_id']) { $user_data['user_id'] = $event['user_id']; @@ -458,7 +461,7 @@ public function handle_user_activate_action($event) public function handle_user_delete_action($event) { // Extract the IDs and names of all deleted users to pass along in an array of (id => name) - $user_data = array(); + $user_data = []; foreach ($event['user_ids'] as $id) { $user_data[$id] = $event['user_rows'][$id]['username']; diff --git a/migrations/create_tables.php b/migrations/create_tables.php index 3f145cd..a9f9728 100644 --- a/migrations/create_tables.php +++ b/migrations/create_tables.php @@ -12,7 +12,7 @@ class create_tables extends \phpbb\db\migration\migration { static public function depends_on() { - return array('\mober\discordnotifications\migrations\extension_installation'); + return ['\mober\discordnotifications\migrations\extension_installation']; } /** @@ -21,23 +21,23 @@ static public function depends_on() */ public function update_schema() { - return array( - 'add_tables' => array ( - $this->table_prefix . 'discord_webhooks' => array ( - 'COLUMNS' => array ( - 'alias' => array ( + return [ + 'add_tables' => [ + $this->table_prefix . 'discord_webhooks' => [ + 'COLUMNS' => [ + 'alias' => [ 'VCHAR:255', - '' - ), - 'url' => array ( + '', + ], + 'url' => [ 'VCHAR:255', - '' - ), - ), + '', + ], + ], 'PRIMARY_KEY' => 'alias', - ), - ), - ); + ], + ], + ]; } /** @@ -46,10 +46,10 @@ public function update_schema() */ public function revert_schema() { - return array ( - 'drop_tables' => array ( + return [ + 'drop_tables' => [ $this->table_prefix . 'discord_webhooks', - ) - ); + ], + ]; } } diff --git a/migrations/extension_installation.php b/migrations/extension_installation.php index 676d977..06c9991 100644 --- a/migrations/extension_installation.php +++ b/migrations/extension_installation.php @@ -38,13 +38,13 @@ public function effectively_installed() */ public function update_schema() { - return array( - 'add_columns' => array( - $this->table_prefix . 'forums' => array( - 'discord_notifications' => array('VCHAR:255', ''), - ), - ), - ); + return [ + 'add_columns' => [ + $this->table_prefix . 'forums' => [ + 'discord_notifications' => ['VCHAR:255', ''], + ], + ], + ]; } /** @@ -55,13 +55,13 @@ public function update_schema() */ public function revert_schema() { - return array( - 'drop_columns' => array( - $this->table_prefix . 'forums' => array( + return [ + 'drop_columns' => [ + $this->table_prefix . 'forums' => [ 'discord_notifications', - ), - ), - ); + ], + ], + ]; } /** @@ -74,55 +74,55 @@ public function revert_schema() */ public function update_data() { - return array( + return [ // The "master switch" that enables notifications to be sent. This can only be set to true if the webhook URL is also valid - array('config.add', array('discord_notifications_enabled', 0)), + ['config.add', ['discord_notifications_enabled', 0]], // The maximum number of characters permitted in a discord notification message - array('config.add', array('discord_notifications_post_preview_length', 200)), + ['config.add', ['discord_notifications_post_preview_length', 200]], // Timeout values - array('config.add', array('discord_notifications_connect_timeout', 2)), - array('config.add', array('discord_notifications_exec_timeout', 2)), + ['config.add', ['discord_notifications_connect_timeout', 2]], + ['config.add', ['discord_notifications_exec_timeout', 2]], // These configurations represent the various types of notifications that can be sent, which can be individually enabled or disabled. // Upon installation, every notification type is enabled by default. // Post notifications - array('config.add', array('discord_notification_type_post_create', 1)), - array('config.add', array('discord_notification_type_post_update', 1)), - array('config.add', array('discord_notification_type_post_delete', 1)), - array('config.add', array('discord_notification_type_post_lock', 1)), - array('config.add', array('discord_notification_type_post_unlock', 1)), - array('config.add', array('discord_notification_type_post_approve', 1)), + ['config.add', ['discord_notification_type_post_create', 1]], + ['config.add', ['discord_notification_type_post_update', 1]], + ['config.add', ['discord_notification_type_post_delete', 1]], + ['config.add', ['discord_notification_type_post_lock', 1]], + ['config.add', ['discord_notification_type_post_unlock', 1]], + ['config.add', ['discord_notification_type_post_approve', 1]], // Topic notifications - array('config.add', array('discord_notification_type_topic_create', 1)), - array('config.add', array('discord_notification_type_topic_update', 1)), - array('config.add', array('discord_notification_type_topic_delete', 1)), - array('config.add', array('discord_notification_type_topic_lock', 1)), - array('config.add', array('discord_notification_type_topic_unlock', 1)), - array('config.add', array('discord_notification_type_topic_approve', 1)), + ['config.add', ['discord_notification_type_topic_create', 1]], + ['config.add', ['discord_notification_type_topic_update', 1]], + ['config.add', ['discord_notification_type_topic_delete', 1]], + ['config.add', ['discord_notification_type_topic_lock', 1]], + ['config.add', ['discord_notification_type_topic_unlock', 1]], + ['config.add', ['discord_notification_type_topic_approve', 1]], // User notifications - array('config.add', array('discord_notification_type_user_create', 1)), - array('config.add', array('discord_notification_type_user_delete', 1)), + ['config.add', ['discord_notification_type_user_create', 1]], + ['config.add', ['discord_notification_type_user_delete', 1]], // Default channel - array('config.add', array('discord_notification_default_webhook', '')), + ['config.add', ['discord_notification_default_webhook', '']], // Standard ACP module data - array('module.add', array( + ['module.add', [ 'acp', 'ACP_CAT_DOT_MODS', - 'ACP_DISCORD_NOTIFICATIONS' - )), - array('module.add', array( + 'ACP_DISCORD_NOTIFICATIONS', + ]], + ['module.add', [ 'acp', 'ACP_DISCORD_NOTIFICATIONS', - array( - 'module_basename' => '\mober\discordnotifications\acp\discord_notifications_module', - 'modes' => array('settings'), - ), - )), - ); + [ + 'module_basename' => '\mober\discordnotifications\acp\discord_notifications_module', + 'modes' => ['settings'], + ], + ]], + ]; } } diff --git a/notification_service.php b/notification_service.php index 7d84576..074bd17 100644 --- a/notification_service.php +++ b/notification_service.php @@ -182,7 +182,7 @@ public function query_topic_details($topic_id) { if (!is_numeric($topic_id)) { - return array(); + return []; } $sql = "SELECT @@ -339,14 +339,16 @@ private function execute_discord_webhook($discord_webhook_url, $color, $message, // Place the message inside the JSON structure that Discord expects to receive at the REST endpoint. $embed = [ - 'timestamp' => date('c', time()), - 'color' => $color, - 'description' => $message + 'timestamp' => date('c', time()), + 'color' => $color, + 'description' => $message, ]; if (isset($footer)) { - $embed["footer"] = ["text" => $footer]; + $embed['footer'] = [ + 'text' => $footer, + ]; } if (isset($title) && isset($preview)) @@ -357,16 +359,16 @@ private function execute_discord_webhook($discord_webhook_url, $color, $message, { $embed['fields'] = [ [ - 'name' => $title, - 'value' => $preview, - 'inline' => false - ] + 'name' => $title, + 'value' => $preview, + 'inline' => false, + ], ]; } } $payload = [ - 'embeds' => [$embed] + 'embeds' => [$embed], ]; $json = \json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); @@ -378,7 +380,7 @@ private function execute_discord_webhook($discord_webhook_url, $color, $message, // Use the CURL library to transmit the message via a POST operation to the webhook URL. $h = curl_init(); - curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-type: application/json')); + curl_setopt($h, CURLOPT_HTTPHEADER, ['Content-type: application/json']); curl_setopt($h, CURLOPT_URL, $discord_webhook_url); curl_setopt($h, CURLOPT_POST, 1); curl_setopt($h, CURLOPT_POSTFIELDS, $json);