| | |
| | | <?php |
| | | namespace JVBase\managers\queue\executors; |
| | | |
| | | use JVBase\managers\CacheManager; |
| | | use JVBase\managers\queue\{Executor, Operation, Progress, Result}; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\utility\Features; |
| | |
| | | } |
| | | |
| | | $this->savePostFields($newId, $postData); |
| | | $results[$id] = ['success' => true, 'new_id' => $newId]; |
| | | $results[$id] = [ |
| | | 'success' => true, |
| | | 'new_id' => $newId, |
| | | 'processed_fields' => array_keys($postData) |
| | | ]; |
| | | |
| | | if (Features::forContent($content)->has('is_timeline')) { |
| | | $this->updateTimelineLatestDate($newId); |
| | |
| | | // Existing post update |
| | | if (!$this->verifyOwnership((int)$id)) { |
| | | $progress->failItem($id, 'No permission to modify this post'); |
| | | $errors[$id] = 'No permission'; |
| | | continue; |
| | | } |
| | | // Check if this is a timeline post |
| | |
| | | $this->updateTimelineLatestDate($parentId); |
| | | } |
| | | |
| | | $results[$id] = ['success' => true]; |
| | | $results[$id] = [ |
| | | 'success' => true, |
| | | 'processed_fields' => array_keys($postData) |
| | | ]; |
| | | $progress->advance(1); |
| | | |
| | | // Clear caches |
| | | CacheManager::for($content)->clear(); |
| | | if (jvbSiteUsesFeedBlock()) { |
| | | CacheManager::for('feed')->clear(); |
| | | } |
| | | |
| | | } catch (Exception $e) { |
| | | $progress->failItem($id, $e->getMessage()); |
| | | $errors[$id] = $e->getMessage(); |
| | | $results[$id] = [ |
| | | 'success' => false, |
| | | 'error' => $e->getMessage() |
| | | ]; |
| | | } |
| | | } |
| | | if (!empty($updateTimelineOrder)) { |
| | | foreach ($updateTimelineOrder as $parentID) { |
| | | $this->reorderTimelineByDate($parentID); |
| | | $processedParents = []; // Track to avoid duplicate processing |
| | | |
| | | foreach ($updateTimelineOrder as $oldParentID) { |
| | | if (in_array($oldParentID, $processedParents)) continue; |
| | | |
| | | $actualParentId = $this->reorderTimelineByDate($oldParentID); |
| | | $processedParents[] = $actualParentId; |
| | | |
| | | // If parent changed, mark the new parent as processed too |
| | | if ($actualParentId !== $oldParentID) { |
| | | $processedParents[] = $oldParentID; |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | return new Result( |
| | | outcome: $outcome, |
| | | result: $results |
| | | result: [ |
| | | 'posts' => $results, |
| | | 'errors' => $errors, |
| | | 'updated_count' => count(array_filter($results, fn($r) => $r['success'] ?? false)), |
| | | 'failed_count' => count($errors) |
| | | ] |
| | | ); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private function reorderTimelineByDate(int $parentId): void |
| | | private function reorderTimelineByDate(int $parentId): int |
| | | { |
| | | $parent = get_post($parentId); |
| | | if (!$parent) return; |
| | | if (!$parent) return $parentId; |
| | | |
| | | // Get all posts in this timeline (parent + children) |
| | | $children = get_posts([ |
| | |
| | | }); |
| | | |
| | | $newParent = $allPosts[0]; |
| | | $actualParentId = $newParent->ID; // Track the actual parent |
| | | |
| | | // If parent changed, restructure |
| | | if ($newParent->ID !== $parentId) { |
| | |
| | | |
| | | $timelineTerm = $this->calculateTimelineTerm($previousPost, $post); |
| | | if ($timelineTerm) { |
| | | $this->getorCreateTerm($post->ID, $timelineTerm, 'timeline'); |
| | | $this->getOrCreateTerm($post->ID, $timelineTerm, 'timeline'); |
| | | } |
| | | |
| | | $previousPost = $post; |
| | | } |
| | | |
| | | $this->updateTimelineLatestDate($newParent->ID); |
| | | // Update latest_date AFTER reordering with the actual parent |
| | | $this->updateTimelineLatestDate($actualParentId); |
| | | |
| | | return $actualParentId; // Return the actual parent ID |
| | | } |
| | | |
| | | private function updateTimelineLatestDate(int $parentId): void |
| | |
| | | 'fields' => 'ids' |
| | | ]); |
| | | |
| | | // Count: parent + children |
| | | $number = count($children) + 1; |
| | | |
| | | $allPostIds = array_merge([$parentId], $children); |
| | | |
| | | // Get all timestamps |
| | |
| | | |
| | | $latestTimestamp = max($timestamps); |
| | | |
| | | // Store as UNIX timestamp |
| | | // Update both meta fields |
| | | update_post_meta($parentId, BASE . 'number', $number); |
| | | update_post_meta($parentId, BASE . 'latest_date', $latestTimestamp); |
| | | } |
| | | |
| | |
| | | $results = $this->createFromDirect($operation, $data, $images, $progress); |
| | | } |
| | | |
| | | // Clear caches |
| | | CacheManager::for($data['content'])->clear(); |
| | | CacheManager::for('feed')->clear(); |
| | | |
| | | return new Result( |
| | | outcome: !empty($results) ? 'success' : 'failed', |
| | | result: $results |