v-wordpress-plugin-updater/v-update-api/app/Models/LogModel.php
Nikolai X. Shadeauxs 8fc3bc20ad
Some checks failed
CI & Security / CI Scan (push) Failing after 9s
CI & Security / CodeQL (JavaScript) (push) Failing after 6s
CI & Security / Semgrep (PHP) (push) Failing after 8s
modified: .github/copilot-instructions.md
modified:   CHANGELOG.md
	modified:   README.md
2026-04-06 14:39:21 -04:00

107 lines
3.2 KiB
PHP

<?php
// phpcs:ignoreFile PSR1.Files.SideEffects.FoundWithSymbols
/**
* Project: UpdateAPI
* Author: Vontainment <services@vontainment.com>
* License: https://opensource.org/licenses/MIT MIT License
* Link: https://vontainment.com
* Version: 4.5.0
*
* File: LogModel.php
* Description: WordPress Update API
*/
namespace App\Models;
use App\Core\DatabaseManager;
class LogModel
{
/**
* Insert one log row.
*
* @param string $domain Domain name.
* @param string $type Log type (e.g., 'plugin', 'theme').
* @param string $status Status (e.g., 'Success', 'Failed').
* @return void
*/
public static function addLog(string $domain, string $type, string $status): void
{
$connection = DatabaseManager::connection();
$connection->executeStatement(
'INSERT INTO logs (domain, type, date, status) VALUES (?, ?, ?, ?)',
[$domain, $type, date('Y-m-d'), $status]
);
}
/**
* Process log entries from the database and generate grouped HTML output.
*
* @param string $type Log type (e.g., 'plugin', 'theme').
* @return string HTML output of grouped log entries.
*/
public static function getLogs(string $type): string
{
$connection = DatabaseManager::connection();
$rows = $connection->fetchAllAssociative(
'SELECT domain, date, status FROM logs WHERE type = ? ORDER BY date DESC',
[$type]
);
$logByDomain = [];
foreach ($rows as $row) {
if (!isset($logByDomain[$row['domain']])) {
$logByDomain[$row['domain']] = [
'date' => $row['date'],
'status' => $row['status'],
];
}
}
if (empty($logByDomain)) {
return 'No log entries found.';
}
ob_start();
echo '<div class="log-row">';
foreach ($logByDomain as $domain => $entry) {
$dateDiff = (strtotime(date('Y-m-d')) - strtotime($entry['date'])) / (60 * 60 * 24);
$classes = '';
if ($entry['status'] == 'Failed') {
$classes .= ' error';
} elseif ($entry['status'] == 'Success') {
$classes .= ' success';
}
if ($dateDiff > 30) {
$classes .= ' lost';
}
$classes = trim($classes);
echo '<div class="log-sub-box' . ($classes ? " $classes" : '') . '">';
echo '<h3>' . htmlspecialchars($domain, ENT_QUOTES, 'UTF-8') . '</h3>';
$color = $entry['status'] == 'Failed' ? 'red' : 'green';
echo '<p class="log-entry" style="color:' . $color . ';">' .
htmlspecialchars($entry['date'], ENT_QUOTES, 'UTF-8') . ' ' .
htmlspecialchars($entry['status'], ENT_QUOTES, 'UTF-8') .
'</p>';
echo '</div>';
}
echo '</div>';
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Clear all known logs.
*
* @return void
*/
public static function clearAllLogs(): void
{
DatabaseManager::connection()->executeStatement('DELETE FROM logs');
}
}