paused++; try { return $write(); } finally { $this->paused--; } } public function postSaved(int $postId, WP_Post $post): void { if (wp_is_post_revision($postId) !== false || wp_is_post_autosave($postId) !== false) { return; } if (in_array($post->post_status, ['auto-draft', 'inherit'], true)) { return; } $this->enqueue($post, $post->post_status); } public function postDeleted(int $postId, WP_Post $post): void { $this->enqueue($post, 'deleted'); } public function flush(): void { $url = $this->url(); $queue = $this->queue; $this->queue = []; if ($url === null) { return; } foreach ($queue as $delivery) { $request = [ 'timeout' => 3, 'redirection' => 0, 'headers' => ['Content-Type' => 'application/json'], 'body' => (string) wp_json_encode($delivery), 'data_format' => 'body', ]; if (wp_get_environment_type() === 'local') { wp_remote_post($url, $request); } else { wp_safe_remote_post($url, $request); } } } private function enqueue(WP_Post $post, string $status): void { if ($this->paused > 0 || $this->url() === null) { return; } $languages = Plugin::languages(); if ($languages === null || !$languages->isTranslated($post->post_type)) { return; } $language = $languages->postLanguage($post->ID); if ($language === null) { return; } if ($this->queue === []) { add_action('shutdown', [$this, 'flush']); } $this->queue[$post->ID] = [ 'id' => $post->ID, 'type' => $post->post_type, 'language' => $language, 'status' => $status, ]; } }