| | |
| | | namespace JVBase\managers\queue\executors; |
| | | |
| | | use JVBase\managers\queue\{Executor, Operation, Progress, Result, Storage}; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\utility\Features; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Registrar; |
| | | use Exception; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | |
| | | |
| | | $results = []; |
| | | $errors = []; |
| | | $success = []; |
| | | $timelineParents = []; |
| | | $timelineStatus = []; |
| | | $timelineSharedFields = []; |
| | | $newPostsMap = []; |
| | | |
| | | foreach ($posts as $id => $postData) { |
| | | try { |
| | |
| | | ]); |
| | | |
| | | if (!$newId || is_wp_error($newId)) { |
| | | $errors[$id] = 'Could not create post'; |
| | | $progress->failItem($id, 'Could not create post'); |
| | | continue; |
| | | } |
| | | |
| | | $newPostsMap[$id] = $newId; |
| | | $this->savePostFields($newId, $postData); |
| | | $results[$id] = [ |
| | | 'success' => true, |
| | | 'new_id' => $newId, |
| | | 'processed_fields' => array_keys($postData) |
| | | ]; |
| | | $success[$newId] = array_keys($postData); |
| | | $progress->advance(); |
| | | continue; |
| | | } |
| | |
| | | } |
| | | |
| | | $this->savePostFields((int)$id, $postData); |
| | | |
| | | |
| | | if (Features::forContent($content)->has('is_timeline')) { |
| | | $registrar = Registrar::getInstance($content); |
| | | if ($registrar && $registrar->hasFeature('is_timeline')) { |
| | | $post = get_post((int)$id); |
| | | $parentId = $post->post_parent > 0 ? $post->post_parent : $post->ID; |
| | | $sharedFields = array_keys(array_filter(JVB_CONTENT[$content]['fields'], function ($field) { |
| | | $fields = $registrar->getFields(); |
| | | $sharedFields = array_keys(array_filter($fields, function ($field) { |
| | | return !array_key_exists('for_all', $field) || !$field['for_all']; |
| | | })); |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | $results[$id] = [ |
| | | 'success' => true, |
| | | 'processed_fields' => array_keys($postData) |
| | | ]; |
| | | $progress->advance(); |
| | | |
| | | $success[$id] = array_keys($postData); |
| | | $progress->advance(); |
| | | } catch (Exception $e) { |
| | | $progress->failItem($id, $e->getMessage()); |
| | | $errors[$id] = $e->getMessage(); |
| | | $results[$id] = [ |
| | | 'success' => false, |
| | | 'error' => $e->getMessage() |
| | | ]; |
| | | } |
| | | } |
| | | |
| | |
| | | $outcome = count($errors) === count($posts) ? 'failed' : 'partial'; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | return new Result( |
| | | outcome: $outcome, |
| | | result: [ |
| | | 'posts' => $results, |
| | | 'posts' => $success, |
| | | 'errors' => $errors, |
| | | 'updated_count' => count(array_filter($results, fn($r) => $r['success'] ?? false)), |
| | | 'new_posts' => $newPostsMap, |
| | | 'updated_count' => count($success), |
| | | 'failed_count' => count($errors) |
| | | ] |
| | | ); |
| | |
| | | private function savePostFields(int $postId, array $postData): bool |
| | | { |
| | | $content = $postData['content'] ?? ''; |
| | | $fields = jvbGetFields($content); |
| | | $fields = Registrar::getFieldsFor($content); |
| | | |
| | | $allowedFields = array_filter($postData, function ($key) use ($fields) { |
| | | return array_key_exists($key, $fields); |
| | |
| | | return true; |
| | | } |
| | | |
| | | $meta = new MetaManager($postId, 'post'); |
| | | return $meta->setAll($allowedFields); |
| | | return Meta::forPost($postId) |
| | | ->setAll($allowedFields) |
| | | ->save(); |
| | | } |
| | | |
| | | // ───────────────────────────────────────────────────────────── |
| | |
| | | |
| | | $lastKey = array_key_last($posts); |
| | | foreach ($posts as $index => $post) { |
| | | $meta = new MetaManager($post->ID, 'post'); |
| | | $meta = Meta::forPost($post->ID); |
| | | if ($index === 0) { |
| | | $meta->updateValue('timeline', '', false); |
| | | $meta->set('timeline', ''); |
| | | $previousPost = $post; |
| | | continue; // Parent has no timeline |
| | | } |
| | |
| | | if ($timeline) { |
| | | $termId = $this->getOrCreateTerm($timeline, 'timeline'); |
| | | if ($termId) { |
| | | $success = $meta->updateValue('timeline', $termId, false); |
| | | $success = $meta->set('timeline', $termId); |
| | | } |
| | | } |
| | | } |
| | |
| | | if ($lastKey === $index) { |
| | | $latestTimestamp = strtotime($post->post_date); |
| | | } |
| | | |
| | | $meta->save(); |
| | | $previousPost = $post; |
| | | } |
| | | |
| | |
| | | protected function checkSharedFields(array $fields): void |
| | | { |
| | | foreach ($fields as $parentID => $shared) { |
| | | $meta = new MetaManager($parentID, 'post'); |
| | | $meta = Meta::forPost($parentID); |
| | | $values = $meta->getAll($shared); |
| | | |
| | | $children = get_children([ |
| | |
| | | } |
| | | |
| | | foreach ($children as $child) { |
| | | $childMeta = new MetaManager($child, 'post'); |
| | | $result = $childMeta->setAll($values, false); |
| | | Meta::forPost($child)->setAll($values)->save(false); |
| | | } |
| | | } |
| | | } |