This commit is contained in:
2026-04-24 18:53:11 +02:00
parent 01581a1dd8
commit b1ed6fe44a
10 changed files with 392 additions and 65 deletions

View File

@@ -304,6 +304,35 @@ class SiteController extends Controller
$this->redirect("/sites/{$id}/comments{$statusQuery}");
}
public function closeExistingComments(string $id): void
{
Auth::requireLogin();
$site = Site::find((int) $id);
if (!$site) {
$this->flash('danger', 'Strona nie znaleziona.');
$this->redirect('/sites');
return;
}
$deletePendingComments = (string) $this->input('delete_pending_comments', '0') === '1';
$wp = new WordPressService();
$result = $wp->closeExistingComments($site, $deletePendingComments);
if (!empty($result['success'])) {
$postsUpdated = (int) ($result['posts_updated'] ?? 0);
$deletedPending = (int) ($result['pending_comments_deleted'] ?? 0);
$this->flash(
'success',
"Zamknieto komentarze i pingi. Zaktualizowane wpisy: {$postsUpdated}, usuniete oczekujace komentarze: {$deletedPending}."
);
} else {
$this->flash('danger', (string) ($result['message'] ?? 'Nie udalo sie zamknac komentarzy w istniejacych wpisach.'));
}
$this->redirect("/sites/{$id}/comments");
}
public function seoPanel(string $id): void
{
Auth::requireLogin();

View File

@@ -12,7 +12,7 @@ class WordPressService
{
private const BACKPRO_MU_PLUGIN_FILENAME = 'backpro-remote-tools.php';
private const BACKPRO_REMOTE_SERVICE_FILENAME = 'backpro-remote-service.php';
private const BACKPRO_REMOTE_SERVICE_VERSION = '1.5.0';
private const BACKPRO_REMOTE_SERVICE_VERSION = '1.6.0';
private const BACKPRO_NEWS_THEME_SLUG = 'backpro-news-mag';
private const BACKPRO_NEWS_THEME_SOURCE_DIR = 'assets/wp-theme-backpro-news';
private Client $client;
@@ -638,6 +638,38 @@ class WordPressService
}
}
public function closeExistingComments(array $site, bool $deletePendingComments = false): array
{
$params = ['delete_pending_comments' => $deletePendingComments ? '1' : '0'];
$result = $this->callRemoteService($site, 'close_existing_comments', $params);
if (!empty($result['success'])) {
return $this->formatCloseExistingCommentsResult($result);
}
$ensure = $this->ensureRemoteService($site);
if (empty($ensure['success'])) {
return [
'success' => false,
'posts_updated' => 0,
'pending_comments_deleted' => 0,
'message' => (string) ($ensure['message'] ?? $result['message'] ?? 'Brak endpointu BackPRO na WordPress.'),
];
}
$refreshedSite = !empty($site['id']) ? (Site::find((int) $site['id']) ?: $site) : $site;
$retry = $this->callRemoteService($refreshedSite, 'close_existing_comments', $params);
if (!empty($retry['success'])) {
return $this->formatCloseExistingCommentsResult($retry);
}
return [
'success' => false,
'posts_updated' => 0,
'pending_comments_deleted' => 0,
'message' => (string) ($retry['message'] ?? 'Nie udalo sie zamknac komentarzy w istniejacych wpisach.'),
];
}
public function ensureRemoteService(array $site): array
{
$siteData = $this->prepareRemoteServiceMetadata($site);
@@ -1180,6 +1212,16 @@ class WordPressService
];
}
private function formatCloseExistingCommentsResult(array $data): array
{
return [
'success' => true,
'posts_updated' => max(0, (int) ($data['posts_updated'] ?? 0)),
'pending_comments_deleted' => max(0, (int) ($data['pending_comments_deleted'] ?? 0)),
'message' => (string) ($data['message'] ?? 'Komentarze w istniejacych wpisach zostaly zamkniete.'),
];
}
private function formatWpRequestError(RequestException $e, string $fallback): string
{
$statusCode = $e->hasResponse() ? (int) $e->getResponse()->getStatusCode() : 0;
@@ -1342,7 +1384,7 @@ if (!defined('ABSPATH')) {
\$action = (string) (\$_POST['action'] ?? '');
if (\$action === 'ping') {
echo json_encode(['success' => true, 'message' => 'pong', 'version' => '1.5.0']);
echo json_encode(['success' => true, 'message' => 'pong', 'version' => '1.6.0']);
exit;
}
@@ -1463,6 +1505,48 @@ if (\$action === 'set_comment_settings') {
exit;
}
if (\$action === 'close_existing_comments') {
global \$wpdb;
update_option('default_comment_status', 'closed');
update_option('default_ping_status', 'closed');
\$postsUpdated = \$wpdb->query(
"UPDATE {\$wpdb->posts}
SET comment_status = 'closed', ping_status = 'closed'
WHERE post_type IN ('post', 'page')
AND post_status NOT IN ('trash', 'auto-draft')"
);
if (\$postsUpdated === false) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'posts_update_failed']);
exit;
}
\$deletePending = (string) (\$_POST['delete_pending_comments'] ?? '0') === '1';
\$deletedPending = 0;
if (\$deletePending) {
\$pendingIds = \$wpdb->get_col(
"SELECT comment_ID FROM {\$wpdb->comments} WHERE comment_approved = '0'"
);
foreach ((array) \$pendingIds as \$commentId) {
if (wp_delete_comment((int) \$commentId, true)) {
\$deletedPending++;
}
}
}
echo json_encode([
'success' => true,
'posts_updated' => (int) \$postsUpdated,
'pending_comments_deleted' => \$deletedPending,
'message' => 'Zamknieto komentarze i pingi w istniejacych wpisach.',
]);
exit;
}
if (\$action === 'cleanup') {
@unlink(__FILE__);
echo json_encode(['success' => true, 'message' => 'service_deleted']);