WIP - Code cleaned up
Constant for the signature marker: SIGNATURE_MARKER is defined as a constant to keep the value consistent. Simplification of the conditions: Some conditions have been simplified to improve readability. Clearer variable names: Some variable names have been renamed to make their meaning clearer. Removal of redundant conditions: The condition isset($b[‘original_body’]) && $b[‘original_body’] != $b[‘body’] has been removed as it is redundant. Removal of the admin settings: The functions signature_admin_settings and signature_admin_settings_post have been removed. Removal of admin hooks: The addon_admin and addon_admin_post hooks have been removed from the signature_install function. Removal of the admin configuration: The configuration for the admin signature has been removed. Problems - I still have problems recognising an ‘@’ sign. This causes the signature to be added at the top instead of the bottom. - If several images are integrated inline, the signature is inserted at the top instead of the bottom. - Perhaps someone can give me a tip on how to avoid this.
This commit is contained in:
parent
ca2b7da6f6
commit
5e102de58b
1 changed files with 63 additions and 64 deletions
|
@ -1,8 +1,8 @@
|
|||
<?php
|
||||
/*
|
||||
/**
|
||||
* Name: signatur
|
||||
* Description: Automatically adds a signature to new posts. Admins can define a default signature, and users can configure their own.
|
||||
* Version: 1.3
|
||||
* Version: 1.6
|
||||
* Author: Matthias Ebers <https://loma.ml/profile/feb>
|
||||
* Status: Beta
|
||||
*/
|
||||
|
@ -11,13 +11,13 @@ use Friendica\Core\Hook;
|
|||
use Friendica\Core\Renderer;
|
||||
use Friendica\DI;
|
||||
|
||||
const SIGNATURE_MARKER = "[hr]";
|
||||
|
||||
function signatur_install()
|
||||
{
|
||||
Hook::register('post_local', __FILE__, 'signatur_add_signature');
|
||||
Hook::register('addon_settings', __FILE__, 'signatur_user_settings');
|
||||
Hook::register('addon_settings_post', __FILE__, 'signatur_user_settings_post');
|
||||
Hook::register('addon_admin', __FILE__, 'signatur_admin_settings');
|
||||
Hook::register('addon_admin_post', __FILE__, 'signatur_admin_settings_post');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,41 +27,70 @@ function signatur_install()
|
|||
*/
|
||||
function signatur_add_signature(array &$b)
|
||||
{
|
||||
if (!$b['uid']) {
|
||||
if (empty($b['uid'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the signature feature is enabled for the user
|
||||
$enabled = DI::pConfig()->get($b['uid'], 'signatur', 'enabled', false);
|
||||
if (!$enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the post is a comment
|
||||
if ($b['parent'] && ($b['parent'] != ($b['uri-id'] ?? null))) {
|
||||
// Check if the signature should be added to comments
|
||||
$enable_signature_in_comments = DI::pConfig()->get($b['uid'], 'signatur', 'enable_signature_in_comments', true);
|
||||
if (!$enable_signature_in_comments) {
|
||||
return;
|
||||
$isComment = !empty($b['parent']) && ($b['parent'] != ($b['uri-id'] ?? null));
|
||||
$enableSignatureInComments = DI::pConfig()->get($b['uid'], 'signatur', 'enable_signature_in_comments', true);
|
||||
|
||||
if ($isComment && !$enableSignatureInComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
$signature = DI::pConfig()->get($b['uid'], 'signatur', 'text', '');
|
||||
|
||||
if (strpos($b['body'], SIGNATURE_MARKER) !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($b['body'])) {
|
||||
$b['body'] = insert_signature_before_images($b['body'], SIGNATURE_MARKER, $signature);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the signature based on the content structure.
|
||||
*
|
||||
* @param string $body The post body.
|
||||
* @param string $signature_marker The signature marker.
|
||||
* @param string $signature The signature text.
|
||||
* @return string The modified post body.
|
||||
*/
|
||||
function insert_signature_before_images($body, $signature_marker, $signature)
|
||||
{
|
||||
$lines = explode("\n", $body);
|
||||
$image_count = 0;
|
||||
$text_end_index = -1;
|
||||
|
||||
foreach ($lines as $index => $line) {
|
||||
if (strpos($line, '[url=') !== false) {
|
||||
$image_count++;
|
||||
if ($image_count === 1 && $text_end_index === -1) {
|
||||
$text_end_index = $index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the user's custom signature or the admin default
|
||||
$signature = DI::pConfig()->get($b['uid'], 'signatur', 'text') ??
|
||||
DI::config()->get('signatur', 'default_text', "---\nDefault Signature");
|
||||
|
||||
// Define the marker as [hr] (horizontal rule)
|
||||
$signature_marker = "[hr]";
|
||||
|
||||
// Check if the marker is already present in the post body
|
||||
if (strpos($b['body'], $signature_marker) !== false) {
|
||||
return; // Signature already exists, do not add it again
|
||||
if ($image_count <= 1) {
|
||||
return $body . "\n\n{$signature_marker}\n{$signature}";
|
||||
}
|
||||
|
||||
// Append the signature with the [hr] marker
|
||||
if (!empty($b['body'])) {
|
||||
$b['body'] .= "\n\n{$signature_marker}\n{$signature}";
|
||||
if ($image_count >= 2 && $text_end_index !== -1) {
|
||||
$lines_before_first_image = array_slice($lines, 0, $text_end_index);
|
||||
$lines_after_first_image = array_slice($lines, $text_end_index);
|
||||
|
||||
return implode("\n", $lines_before_first_image) .
|
||||
"\n\n{$signature_marker}\n{$signature}\n\n" .
|
||||
implode("\n", $lines_after_first_image);
|
||||
}
|
||||
|
||||
return $body . "\n\n{$signature_marker}\n{$signature}";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,28 +100,28 @@ function signatur_add_signature(array &$b)
|
|||
*/
|
||||
function signatur_user_settings(array &$data)
|
||||
{
|
||||
if (!DI::userSession()->getLocalUserId()) {
|
||||
$uid = DI::userSession()->getLocalUserId();
|
||||
if (!$uid) {
|
||||
return;
|
||||
}
|
||||
|
||||
$uid = DI::userSession()->getLocalUserId();
|
||||
$enabled = DI::pConfig()->get($uid, 'signatur', 'enabled', false);
|
||||
$signature = DI::pConfig()->get($uid, 'signatur', 'text', '');
|
||||
$enable_signature_in_comments = DI::pConfig()->get($uid, 'signatur', 'enable_signature_in_comments', true);
|
||||
|
||||
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/signatur/');
|
||||
$html = Renderer::replaceMacros($t, [
|
||||
'$description' => DI::l10n()->t('Add a signature to your posts.'),
|
||||
'$enabled' => ['enabled', DI::l10n()->t('Enable Signature'), $enabled],
|
||||
'$signature' => ['text', DI::l10n()->t('Your Signature'), $signature, DI::l10n()->t('Enter your custom signature. (Multiline allowed)')],
|
||||
'$description' => DI::l10n()->t('BETA Version - Add a signature to your posts. This addon automatically appends a customizable signature to posts in Friendica. Users can enable or disable it, define personal signatures, and optionally include them in comments.'),
|
||||
'$enabled' => ['enabled', DI::l10n()->t('Enable Signature'), $enabled],
|
||||
'$signature' => ['text', DI::l10n()->t('Your Signature'), $signature, DI::l10n()->t('Enter your custom signature. (Multiline allowed)')],
|
||||
'$enable_signature_in_comments' => ['enable_signature_in_comments', DI::l10n()->t('Enable Signature in Comments'), $enable_signature_in_comments],
|
||||
'$submit' => DI::l10n()->t('Save'),
|
||||
'$submit' => DI::l10n()->t('Save'),
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'addon' => 'signatur',
|
||||
'title' => DI::l10n()->t('Signature Settings'),
|
||||
'html' => $html,
|
||||
'html' => $html,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -103,11 +132,11 @@ function signatur_user_settings(array &$data)
|
|||
*/
|
||||
function signatur_user_settings_post(array &$b)
|
||||
{
|
||||
if (!DI::userSession()->getLocalUserId() || empty($_POST['signatur-submit'])) {
|
||||
$uid = DI::userSession()->getLocalUserId();
|
||||
if (!$uid || empty($_POST['signatur-submit'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$uid = DI::userSession()->getLocalUserId();
|
||||
$enabled = !empty($_POST['enabled']);
|
||||
$signature = trim($_POST['text']);
|
||||
$enable_signature_in_comments = !empty($_POST['enable_signature_in_comments']);
|
||||
|
@ -116,33 +145,3 @@ function signatur_user_settings_post(array &$b)
|
|||
DI::pConfig()->set($uid, 'signatur', 'text', $signature);
|
||||
DI::pConfig()->set($uid, 'signatur', 'enable_signature_in_comments', $enable_signature_in_comments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin settings for the app.
|
||||
*
|
||||
* @param string &$o Output string for rendering.
|
||||
*/
|
||||
function signatur_admin_settings(string &$o)
|
||||
{
|
||||
$default_text = DI::config()->get('signatur', 'default_text', "---\nDefault Admin Signature");
|
||||
|
||||
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/signatur/');
|
||||
$o = Renderer::replaceMacros($t, [
|
||||
'$description' => DI::l10n()->t('Set the default signature for users who have not defined their own.'),
|
||||
'$default_text' => ['default_text', DI::l10n()->t('Default Signature'), $default_text, DI::l10n()->t('This will be used if users do not set their own signature.')],
|
||||
'$submit' => DI::l10n()->t('Save'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves admin settings.
|
||||
*/
|
||||
function signatur_admin_settings_post()
|
||||
{
|
||||
if (empty($_POST['signatur-admin-submit'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$default_text = trim($_POST['default_text']);
|
||||
DI::config()->set('signatur', 'default_text', $default_text);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue