Files
wordpress-theme-aksrt/slim-forum.php
T
kate522 ce1c13b947 refactor(classes): organize inc/classes into domain folders
Move ~94 root class shells and root *-traits into domain directories
(core, auth, user, credits, content, payment/gateways, forum, shop,
sms, ai, messages, extensions, widgets, settings, admin). Update
bootstrap require paths, co-locate traits with owners, fix forum/user
relative requires, drop duplicate shop controller requires, and wire
home-modules load. Path-only; class names and init order unchanged.
2026-07-17 11:11:57 +08:00

217 lines
8.3 KiB
PHP

<?php
/**
* Script to replace method bodies with delegation stubs in class-aksrt-forum.php
*/
$file = __DIR__ . '/inc/classes/forum/class-aksrt-forum.php';
$content = file_get_contents($file);
if ($content === false) {
echo "ERROR: Cannot read file\n";
exit(1);
}
/**
* Replace a method body with a one-liner delegation.
* Finds the method signature, then locates the matching closing brace.
*/
function replace_method(string $content, string $signature, string $body): string {
$pos = strpos($content, $signature);
if ($pos === false) {
echo "WARNING: Could not find: $signature\n";
return $content;
}
// Find the opening brace of the method (first { after the signature)
$open_brace = strpos($content, '{', $pos + strlen($signature));
if ($open_brace === false) {
$open_brace = strpos($content, '{', $pos);
if ($open_brace === false) {
echo "WARNING: Could not find opening brace for: $signature\n";
return $content;
}
}
// Find matching closing brace by counting brace depth
$depth = 1;
$i = $open_brace + 1;
$len = strlen($content);
$in_string_single = false;
$in_string_double = false;
$in_line_comment = false;
$in_block_comment = false;
while ($i < $len && $depth > 0) {
$ch = $content[$i];
$next = ($i + 1 < $len) ? $content[$i + 1] : '';
if ($in_line_comment) {
if ($ch === "\n") $in_line_comment = false;
$i++;
continue;
}
if ($in_block_comment) {
if ($ch === '*' && $next === '/') { $in_block_comment = false; $i += 2; continue; }
$i++;
continue;
}
if ($in_string_single) {
if ($ch === '\\') { $i += 2; continue; }
if ($ch === "'") $in_string_single = false;
$i++;
continue;
}
if ($in_string_double) {
if ($ch === '\\') { $i += 2; continue; }
if ($ch === '"') $in_string_double = false;
$i++;
continue;
}
if ($ch === '/' && $next === '/') { $in_line_comment = true; $i += 2; continue; }
if ($ch === '/' && $next === '*') { $in_block_comment = true; $i += 2; continue; }
if ($ch === "'") { $in_string_single = true; $i++; continue; }
if ($ch === '"') { $in_string_double = true; $i++; continue; }
if ($ch === '{') $depth++;
elseif ($ch === '}') $depth--;
$i++;
}
if ($depth !== 0) {
echo "WARNING: Could not find matching closing brace for: $signature\n";
return $content;
}
$close_brace = $i - 1;
// Get indentation of the method
$line_start = strrpos(substr($content, 0, $pos), "\n");
if ($line_start === false) $line_start = 0;
else $line_start++;
$indent = '';
for ($j = $line_start; $j < $pos; $j++) {
if ($content[$j] === ' ' || $content[$j] === "\t") $indent .= $content[$j];
else break;
}
// Replace from open_brace to close_brace (inclusive)
$replacement = "{\n" . $indent . " " . $body . "\n" . $indent . "}";
$content = substr($content, 0, $open_brace) . $replacement . substr($content, $close_brace + 1);
return $content;
}
// --- DRAFT SERVICE METHODS ---
$content = replace_method($content,
'public function publish_draft(int $user_id, int $draft_id, array $data)',
'return $this->draft_service()->publish_draft($user_id, $draft_id, $data);');
$content = replace_method($content,
'public function ajax_delete_draft(): void',
'$this->draft_service()->ajax_delete_draft();');
$content = replace_method($content,
'public function ajax_load_draft(): void',
'$this->draft_service()->ajax_load_draft();');
$content = replace_method($content,
'public function ajax_save_draft(): void',
'$this->draft_service()->ajax_save_draft();');
$content = replace_method($content,
'public function load_draft_data(int $user_id, int $draft_id): ?array',
'return $this->draft_service()->load_draft_data($user_id, $draft_id);');
$content = replace_method($content,
'public function delete_draft(int $user_id, int $draft_id): bool',
'return $this->draft_service()->delete_draft($user_id, $draft_id);');
$content = replace_method($content,
'public function get_user_drafts(int $user_id, int $limit = 20): array',
'return $this->draft_service()->get_user_drafts($user_id, $limit);');
$content = replace_method($content,
'public function get_user_draft_count(int $user_id): int',
'return $this->draft_service()->get_user_draft_count($user_id);');
$content = replace_method($content,
'public function save_draft(int $user_id, array $data)',
'return $this->draft_service()->save_draft($user_id, $data);');
// --- COMMENT SERVICE METHODS ---
$content = replace_method($content,
'public function get_commenter_badge(int $user_id, int $topic_id): ?array',
'return $this->comment_service()->get_commenter_badge($user_id, $topic_id);');
$content = replace_method($content,
'public function is_hot_comment(int $comment_id): bool',
'return $this->comment_service()->is_hot_comment($comment_id);');
$content = replace_method($content,
'public function maybe_mark_hot_comment(int $comment_id): bool',
'return $this->comment_service()->maybe_mark_hot_comment($comment_id);');
$content = replace_method($content,
'public function set_comment_topping(int $comment_id, int $level): bool',
'return $this->comment_service()->set_comment_topping($comment_id, $level);');
$sig_forum_comments = "public function get_forum_comments(int \$topic_id, string \$sort = 'newest', bool \$author_only = false, int \$page = 1, int \$per_page = 30): array";
$content = replace_method($content, $sig_forum_comments,
'return $this->comment_service()->get_forum_comments($topic_id, $sort, $author_only, $page, $per_page);');
$content = replace_method($content,
'public function get_adopted_comment_id(int $topic_id): int',
'return $this->comment_service()->get_adopted_comment_id($topic_id);');
$content = replace_method($content,
'public function adopt_answer(int $topic_id, int $comment_id, int $operator_id)',
'return $this->comment_service()->adopt_answer($topic_id, $comment_id, $operator_id);');
$content = replace_method($content,
'public function render_forum_comments(array $comments, int $topic_id, int $adopted_id = 0): void',
'$this->comment_service()->render_forum_comments($comments, $topic_id, $adopted_id);');
$content = replace_method($content,
'public function ajax_comment_topping(): void',
'$this->comment_service()->ajax_comment_topping();');
$content = replace_method($content,
'public function ajax_forum_comments(): void',
'$this->comment_service()->ajax_forum_comments();');
// --- SCORE/NOTIFICATION SERVICE METHODS ---
$content = replace_method($content,
'public function on_forum_comment_posted(int $comment_id, $approved, array $commentdata = []): void',
'$this->score_service()->on_forum_comment_posted($comment_id, $approved, $commentdata);');
$content = replace_method($content,
'public function notify_topic_scored(int $topic_id, int $scorer_id, int $amount): void',
'$this->score_service()->notify_topic_scored($topic_id, $scorer_id, $amount);');
$content = replace_method($content,
'public function notify_topic_reply(int $topic_id, int $commenter_id): void',
'$this->score_service()->notify_topic_reply($topic_id, $commenter_id);');
$content = replace_method($content,
'public function notify_moderation_result(int $topic_id, string $action, int $operator_id): void',
'$this->score_service()->notify_moderation_result($topic_id, $action, $operator_id);');
$content = replace_method($content,
'public function notify_topic_pending(int $topic_id, int $author_id): void',
'$this->score_service()->notify_topic_pending($topic_id, $author_id);');
$content = replace_method($content,
'private function notify(int $user_id, string $type, string $content, array $data = []): void',
'$this->score_service()->notify($user_id, $type, $content, $data);');
// --- Write result ---
$result = file_put_contents($file, $content);
if ($result === false) {
echo "ERROR: Cannot write file\n";
exit(1);
}
echo "SUCCESS: File updated. New size: " . strlen($content) . " bytes\n";
echo "Line count: " . (substr_count($content, "\n") + 1) . "\n";