Skip to content

Commit

Permalink
Use short array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ober committed Nov 6, 2023
1 parent f23a9b2 commit ff00574
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 116 deletions.
28 changes: 14 additions & 14 deletions acp/discord_notifications_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -153,7 +153,7 @@ public function main($id, $mode)

'DN_CURL_AVAILABLE' => $this->curl_available,
'U_ACTION' => $this->u_action,
));
]);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
{
Expand Down
61 changes: 32 additions & 29 deletions event/notification_event_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
);
];
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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'],
Expand All @@ -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'])
{
Expand Down Expand Up @@ -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'],
Expand All @@ -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']);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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'];
Expand Down
38 changes: 19 additions & 19 deletions migrations/create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand All @@ -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',
),
),
);
],
],
];
}

/**
Expand All @@ -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',
)
);
],
];
}
}
86 changes: 43 additions & 43 deletions migrations/extension_installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''],
],
],
];
}

/**
Expand All @@ -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',
),
),
);
],
],
];
}

/**
Expand All @@ -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'],
],
]],
];
}
}
Loading

0 comments on commit ff00574

Please sign in to comment.