settings = new Settings(); $this->api = new ApiClient($this->settings); $this->renderer = new Renderer($this->settings, $this->api); $this->webhook = new Webhook($this->settings); } public static function instance(): self { return self::$instance ??= new self(); } public function boot(): void { add_action('init', [$this, 'initialize']); add_action('wp_head', [$this, 'verification_meta']); add_action('admin_init', [$this, 'privacy_policy']); add_filter('the_content', [$this, 'closed_comments_fallback'], 20); add_filter('comments_template', [$this, 'comments_template'], 99); add_filter('pre_render_block', [$this, 'comments_block'], 10, 3); add_filter('script_loader_tag', [$this->renderer, 'script_tag'], 10, 2); add_action('rest_api_init', [$this->webhook, 'register']); (new Admin($this->settings, $this->api))->boot(); (new Moderation($this->settings))->boot(); } public function initialize(): void { $locales = ['en' => 'en_US', 'tr' => 'tr_TR', 'de' => 'de_DE', 'fr' => 'fr_FR', 'fi' => 'fi', 'es' => 'es_ES']; $base = substr(strtolower(determine_locale()), 0, 2); if (isset($locales[$base])) { load_textdomain('kommentti', KOMMENTTI_PLUGIN_DIR.'/languages/kommentti-'.$locales[$base].'.mo'); } wp_register_style('kommentti-publisher', plugins_url('assets/publisher.css', KOMMENTTI_PLUGIN_FILE), [], KOMMENTTI_VERSION); wp_register_script('kommentti-embed', Settings::API_URL.'/embed.js', [], '6', ['in_footer' => true, 'strategy' => 'defer']); wp_register_script('kommentti-editor', plugins_url('assets/editor.js', KOMMENTTI_PLUGIN_FILE), ['wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components'], KOMMENTTI_VERSION, true); wp_localize_script('kommentti-editor', 'KommenttiEditor', [ 'title' => __('Kommentti comments', 'kommentti'), 'description' => __('The published page shows the comments for this article. Configure the connection in Settings → Kommentti.', 'kommentti'), ]); register_block_type(KOMMENTTI_PLUGIN_DIR.'/assets', ['render_callback' => fn(): string => $this->renderer->render()]); add_shortcode('kommentti_comments', fn(): string => $this->renderer->render()); } public function privacy_policy(): void { if (function_exists('wp_add_privacy_policy_content')) { wp_add_privacy_policy_content('Kommentti', wpautop(esc_html__('This site uses Kommentti to host comments. The embedded service receives connection information such as the visitor IP address and browser information. Account details and submitted comments are processed by Kommentti. Published comments contain a display name, comment text and date. Contact the site operator about its use of the service. Service privacy information: https://kommentti.com/privacy', 'kommentti'))); } } public function comments_template(string $template): string { return $this->settings->all()['placement'] === 'automatic' && is_singular() && $this->renderer->eligible() ? KOMMENTTI_PLUGIN_DIR.'/templates/comments.php' : $template; } public function comments_block(?string $preRender, array $parsedBlock, mixed $parentBlock = null): ?string { if ($preRender !== null || ($parsedBlock['blockName'] ?? '') !== 'core/comments' || $this->settings->all()['placement'] !== 'automatic' || !is_singular() || !$this->renderer->eligible()) { return $preRender; } return $this->renderer->render(); } public function closed_comments_fallback(string $content): string { $post = get_post(); if ($this->settings->all()['placement'] === 'automatic' && is_singular() && is_main_query() && in_the_loop() && $post && !comments_open($post) && (int) $post->comment_count === 0 && $this->renderer->eligible($post)) { return $content.$this->renderer->render($post); } return $content; } public function verification_meta(): void { $settings = $this->settings->all(); if ($this->settings->connected() && preg_match('/^[A-Za-z0-9]{48}$/D', $settings['verification_token'])) { echo ''."\n"; } } public static function activate(): void { add_option('kommentti_cache_generation', wp_generate_uuid4(), '', false); } public static function deactivate(): void { update_option('kommentti_cache_generation', wp_generate_uuid4(), false); delete_transient('kommentti_api_backoff'); delete_option('kommentti_fetch_lock'); } }