languages = $languages; $this->webhook = $webhook; } public function write( WP_Post $source, string $language, array $fields, string $status, string $expectedSourceHash, ?string $expectedTargetHash ) { $lock = $this->lock($source); if ($lock === null) { return Lock::busy(); } try { clean_post_cache($source->ID); $current = get_post($source->ID); if (!$current instanceof WP_Post || $current->post_status === 'trash') { return new WP_Error('lingo_dev_post_not_found', 'No translated post has this ID.', ['status' => 404]); } return $this->writeLocked($current, $language, $fields, $status, $expectedSourceHash, $expectedTargetHash); } finally { Lock::release($lock); } } private function writeLocked( WP_Post $source, string $language, array $fields, string $status, string $expectedSourceHash, ?string $expectedTargetHash ) { $targetId = $this->languages->translations($source->ID)[$language] ?? null; $target = $targetId === null ? null : get_post($targetId); if ($target instanceof WP_Post && $target->post_status === 'trash') { return new WP_Error('lingo_dev_target_trashed', 'The translation is in the trash.', ['status' => 409]); } $target = $target instanceof WP_Post ? $target : null; $current = $target === null ? array_fill_keys(Content::fieldsOf($source->post_type), '') : Content::read($target); $desired = array_merge($current, $fields); $desiredHash = Content::hash($desired); $decision = WriteDecision::decide( $status, $source->post_status, Content::hash(Content::read($source)), $expectedSourceHash, $target === null ? null : $this->targetOf($target), $expectedTargetHash, $desiredHash ); if ($decision->action === WriteDecision::REFUSE) { return new WP_Error($decision->refusal, WriteDecision::message($decision->refusal), ['status' => 409]); } if ($target !== null && $decision->action === WriteDecision::UNCHANGED && !$this->lacksPassword($source, $target)) { return $this->result($target, 'unchanged'); } $this->token = wp_generate_uuid4(); $this->stored = null; add_filter('wp_insert_post_data', [$this, 'capture'], PHP_INT_MAX, 3); try { $saved = $this->webhook->withoutNotifications(function () use ($source, $target, $language, $desired, $decision) { return $target === null ? $this->create($source, $language, $desired, $decision->status) : $this->update($source, $target, $desired, $decision->status); }); } finally { remove_filter('wp_insert_post_data', [$this, 'capture'], PHP_INT_MAX); } if ($saved instanceof WP_Error) { return $saved; } clean_post_cache($saved); $post = get_post($saved); if (!$post instanceof WP_Post) { return new WP_Error('lingo_dev_write_failed', 'The translation could not be read after saving it.', ['status' => 500]); } $captured = $this->captured(); $stored = $captured === null ? null : Content::fromColumns($captured, $source->post_type); update_post_meta($post->ID, self::META, (string) wp_json_encode( $stored === null ? ['from' => $desiredHash] : ['from' => $desiredHash, 'as' => Content::hash($stored)] )); return $this->result($post, $target === null ? 'created' : 'updated', $stored); } private function captured(): ?array { return $this->stored; } public function capture(array $data, array $postarr, array $unsanitized): array { if (($unsanitized[self::TOKEN] ?? null) === $this->token) { $this->stored = $data; } return $data; } private function create(WP_Post $source, string $language, array $fields, string $status) { $meta = []; $thumbnail = get_post_thumbnail_id($source); if ($thumbnail !== false && $thumbnail > 0) { $meta['_thumbnail_id'] = $thumbnail; } $template = get_page_template_slug($source); if (is_string($template) && $template !== '') { $meta['_wp_page_template'] = $template; } $parent = $source->post_parent > 0 ? ($this->languages->translations($source->post_parent)[$language] ?? 0) : 0; $id = wp_insert_post(wp_slash(array_merge(Content::columns(Content::withoutScripts($fields)), [ 'post_type' => $source->post_type, 'post_status' => $status, self::TOKEN => $this->token, 'post_author' => (int) $source->post_author, 'post_password' => $source->post_password, 'post_parent' => $parent, 'menu_order' => $source->menu_order, 'comment_status' => $source->comment_status, 'ping_status' => $source->ping_status, 'meta_input' => $meta, ])), true); if ($id instanceof WP_Error) { return new WP_Error('lingo_dev_write_failed', $id->get_error_message(), ['status' => 500]); } try { $this->languages->link($source->ID, $id, $language); } catch (Throwable $error) { wp_delete_post($id, true); return new WP_Error('lingo_dev_link_failed', $error->getMessage(), ['status' => 500]); } return $id; } private function update(WP_Post $source, WP_Post $target, array $fields, string $status) { $columns = array_merge(Content::columns(Content::withoutScripts($fields)), [ 'ID' => $target->ID, 'post_status' => $status, self::TOKEN => $this->token, ]); if ($this->lacksPassword($source, $target)) { $columns['post_password'] = $source->post_password; } $id = wp_update_post(wp_slash($columns), true); return $id instanceof WP_Error ? new WP_Error('lingo_dev_write_failed', $id->get_error_message(), ['status' => 500]) : $id; } private function lacksPassword(WP_Post $source, WP_Post $target): bool { return $source->post_password !== '' && $target->post_password === ''; } private function targetOf(WP_Post $target): Target { $written = json_decode((string) get_post_meta($target->ID, self::META, true), true); $from = is_array($written) && is_string($written['from'] ?? null) ? $written['from'] : null; $as = is_array($written) && is_string($written['as'] ?? null) ? $written['as'] : null; return new Target($target->post_status, Content::hash(Content::read($target)), $from, $as); } private function result(WP_Post $post, string $result, ?array $stored = null): array { $fields = $stored ?? Content::read($post); return [ 'id' => $post->ID, 'result' => $result, 'status' => $post->post_status, 'modified' => Content::modified($post), 'hash' => Content::hash($fields), 'fields' => $fields, ]; } private function lock(WP_Post $source): ?string { $group = array_values($this->languages->translations($source->ID)); $group[] = $source->ID; return Lock::acquire(min($group)); } }