From e1611bb7c67e64383170ca3d1b061c1401c48f92 Mon Sep 17 00:00:00 2001 From: Niclas Date: Wed, 29 Jan 2025 11:54:48 +0100 Subject: [PATCH 1/2] feat: sticky behavior and highlight refactor --- source/php/Module/Posts/Helper/GetPosts.php | 24 +++--- source/php/Module/Posts/Posts.php | 6 +- .../TemplateController/AbstractController.php | 80 +++++++++++++++++-- .../ExpandableListTemplate.php | 6 +- .../Posts/TemplateController/ListTemplate.php | 7 +- .../Posts/views/features-grid.blade.php | 5 +- source/php/Module/Posts/views/grid.blade.php | 7 +- source/php/Module/Posts/views/index.blade.php | 7 +- source/php/Module/Posts/views/news.blade.php | 7 +- .../Posts/views/partials/post/box.blade.php | 7 -- .../partials/post/collection-item.blade.php | 9 +-- .../views/partials/post/news-item.blade.php | 9 +-- .../php/Module/Posts/views/segment.blade.php | 5 +- 13 files changed, 123 insertions(+), 56 deletions(-) diff --git a/source/php/Module/Posts/Helper/GetPosts.php b/source/php/Module/Posts/Helper/GetPosts.php index 8a9a44221..8bae11ece 100644 --- a/source/php/Module/Posts/Helper/GetPosts.php +++ b/source/php/Module/Posts/Helper/GetPosts.php @@ -47,7 +47,7 @@ private function getPostsFromSelectedSites(array $fields, int $page):array { $stickyPostIds = $this->getStickyPostIds($fields, $page); $stickyPostsFromSite = $this->getStickyPostsForSite($fields, $page, $stickyPostIds); - $wpQuery = $this->wpQueryFactory->create($this->getPostArgs($fields, $page, $stickyPostIds, count($stickyPostsFromSite))); + $wpQuery = $this->wpQueryFactory->create($this->getPostArgs($fields, $page, $stickyPostIds)); $postsFromSite = $wpQuery->get_posts(); $stickyPostsFromSite = $this->addSiteDataToPosts($stickyPostsFromSite, $site); @@ -67,31 +67,32 @@ private function getPostsFromSelectedSites(array $fields, int $page):array { } // Limit the number of posts to the desired count to avoid exceeding the limit. - $stickyPostsFromSite = $this->sortPosts($stickyPosts, $fields['posts_sort_by'] ?? 'date', $fields['posts_sort_order'] ?? 'desc'); + $stickyPosts = $this->sortPosts($stickyPosts, $fields['posts_sort_by'] ?? 'date', $fields['posts_sort_order'] ?? 'desc'); $posts = $this->sortPosts($posts, $fields['posts_sort_by'] ?? 'date', $fields['posts_sort_order'] ?? 'desc'); - $posts = array_merge($stickyPostsFromSite, $posts); $posts = array_slice($posts, 0, $this->getPostsPerPage($fields)); return [ 'posts' => $posts, - 'maxNumPages' => $maxNumPages + 'maxNumPages' => $maxNumPages, + 'stickyPosts' => $stickyPosts, ]; } $stickyPostIds = $this->getStickyPostIds($fields, $page); - $stickyPostsFromSite = $this->getStickyPostsForSite($fields, $page, $stickyPostIds); + $stickyPosts = $this->getStickyPostsForSite($fields, $page, $stickyPostIds); - $wpQuery = $this->wpQueryFactory->create($this->getPostArgs($fields, $page, $stickyPostIds, count($stickyPostsFromSite))); + $wpQuery = $this->wpQueryFactory->create($this->getPostArgs($fields, $page, $stickyPostIds)); - $stickyPostsFromSite = $this->sortPosts($stickyPostsFromSite, $fields['posts_sort_by'] ?? 'date', $fields['posts_sort_order'] ?? 'desc'); + $stickyPosts = $this->sortPosts($stickyPosts, $fields['posts_sort_by'] ?? 'date', $fields['posts_sort_order'] ?? 'desc'); - $posts = array_merge($stickyPostsFromSite, $wpQuery->get_posts()); + $posts = $wpQuery->get_posts(); return [ 'posts' => $posts, - 'maxNumPages' => $wpQuery->max_num_pages + 'maxNumPages' => $wpQuery->max_num_pages, + 'stickyPosts' => $stickyPosts, ]; } @@ -156,7 +157,7 @@ private function getStickyPostIds(array $fields, int $page): array /** * Get post args */ - private function getPostArgs(array $fields, int $page, array $stickyPostIds = [], int $stickyCount = 0) + private function getPostArgs(array $fields, int $page, array $stickyPostIds = []) { $metaQuery = false; $orderby = !empty($fields['posts_sort_by']) ? $fields['posts_sort_by'] : 'date'; @@ -262,8 +263,7 @@ private function getPostArgs(array $fields, int $page, array $stickyPostIds = [] } // Number of posts - $postsPerPage = $this->getPostsPerPage($fields) - $stickyCount; - $getPostsArgs['posts_per_page'] = $postsPerPage < 0 ? 0 : $postsPerPage; + $getPostsArgs['posts_per_page'] = $this->getPostsPerPage($fields); // Apply pagination $getPostsArgs['paged'] = $page; diff --git a/source/php/Module/Posts/Posts.php b/source/php/Module/Posts/Posts.php index b0861f2e4..737e1f1c7 100644 --- a/source/php/Module/Posts/Posts.php +++ b/source/php/Module/Posts/Posts.php @@ -131,7 +131,8 @@ public function data(): array $data['postsSources'] = $this->fields['posts_data_network_sources'] ?? []; $postsAndPaginationData = $this->getPostsAndPaginationData(); - $data['posts'] = $postsAndPaginationData['posts']; + $data['posts'] = $postsAndPaginationData['posts']; + $data['stickyPosts'] = $postsAndPaginationData['stickyPosts']; if( !empty($this->fields['posts_pagination']) && $this->fields['posts_pagination'] === 'page_numbers' ) { $data['maxNumPages'] = $postsAndPaginationData['maxNumPages']; @@ -431,7 +432,8 @@ public function getPostsAndPaginationData(): array return [ 'posts' => [], - 'maxNumPages' => 0 + 'maxNumPages' => 0, + 'stickyPosts' => [] ]; } diff --git a/source/php/Module/Posts/TemplateController/AbstractController.php b/source/php/Module/Posts/TemplateController/AbstractController.php index 6cdd2297a..54dfc60a3 100644 --- a/source/php/Module/Posts/TemplateController/AbstractController.php +++ b/source/php/Module/Posts/TemplateController/AbstractController.php @@ -31,10 +31,29 @@ public function __construct(\Modularity\Module\Posts\Posts $module) $this->module = $module; $this->fields = $module->fields; $this->data = $this->addDataViewData($module->data, $module->fields); - $this->data['posts'] = $this->preparePosts($module->data['posts']); + $this->data['posts'] = $this->preparePosts($module); + $this->data['classList'] = []; } + /** + * Prepare posts for display. + * + * @param \Modularity\Module\Posts\Posts $module + * + * @return array + */ + public function preparePosts(\Modularity\Module\Posts\Posts $module) + { + $stickyPosts = $module->data['stickyPosts'] ?? []; + $stickyPosts = $this->addStickyPostsData($stickyPosts); + $stickyPosts = $this->addPostData($stickyPosts); + $posts = $this->addPostData($module->data['posts']); + $posts = array_merge($stickyPosts, $posts); + + return $posts; + } + /** * Prepare and set data fields for posts display. * @@ -47,9 +66,11 @@ public function addDataViewData(array $data, array $fields) { $data['posts_columns'] = apply_filters('Modularity/Display/replaceGrid', $fields['posts_columns']); $data['ratio'] = $fields['ratio'] ?? '16:9'; + $data['highlight_first_column_as'] = $fields['posts_display_highlighted_as'] ?? 'block'; $data['highlight_first_column'] = !empty($fields['posts_highlight_first']) ? - ColumnHelper::getFirstColumnSize($data['posts_columns']) : false; + ColumnHelper::getFirstColumnSize($data['posts_columns']) : + false; $data['imagePosition'] = $fields['image_position'] ?? false; return $data; @@ -63,7 +84,7 @@ public function addDataViewData(array $data, array $fields) * @return array * TODO: This should require an array, but cant because sometimes it gets null. */ - public function preparePosts($posts = []) + public function addPostData($posts = []) { $wpService = WpService::get(); @@ -100,7 +121,9 @@ public function preparePosts($posts = []) if(!empty($posts)) { foreach ($posts as $index => &$post) { - $post = $this->setPostViewData($post, $index); + $post = $this->setPostViewData($post, $index); + $post->classList = $post->classList ?? []; + $post = $this->addHighlightData($post, $index); // Apply $this->getDefaultValuesForPosts() to the post object without turning it into an array foreach ($this->getDefaultValuesForPosts() as $key => $value) { @@ -114,6 +137,31 @@ public function preparePosts($posts = []) return $posts; } + /** + * Add post columns class. + * + * @param object $post + * @param false|int $index + * + * @return object + */ + private function addHighlightData(object $post, $index): object + { + $columnsClass = $this->data['posts_columns'] ?? 'o-grid-12@md'; + + if (!empty($post->isSticky)) { + $columnsClass = 'o-grid-12@md'; + $post->isHighlighted = true; + } elseif ($index === 0 && !empty($this->data['highlight_first_column'])) { + $columnsClass = $this->data['highlight_first_column']; + $post->isHighlighted = true; + } + + $post->classList[] = $columnsClass; + + return $post; + } + /** * Get default values for keys in the post object. * @@ -137,6 +185,7 @@ private function getDefaultValuesForPosts() { 'imagePosition' => true, 'image' => false, 'attributeList' => [], + 'isSticky' => false, 'commentCount' => false, ]; } @@ -162,7 +211,8 @@ private function setPostViewData(object $post, $index = false) $post->commentCount = in_array('comment_count', $this->data['posts_fields'] ?? []) ? (string) $post->getCommentCount() : false; $post->readingTime = in_array('reading_time', $this->data['posts_fields'] ?? []) ? $post->readingTime : false; - $post->attributeList = !empty($post->attributeList) ? $post->attributeList : []; + $post->attributeList = !empty($post->attributeList) ? $post->attributeList : []; + $post->attributeList['data-js-item-id'] = $post->getId(); if (!empty($post->image) && is_array($post->image)) { $post->image['removeCaption'] = true; @@ -241,4 +291,24 @@ private function getImageContractOrByRatio(array $images, $imageContract) { return false; } + + /** + * Add sticky posts data. + * + * @param array $stickyPosts + * + * @return array + */ + private function addStickyPostsData(array $stickyPosts = []) + { + if (empty($stickyPosts)) { + return []; + } + + foreach ($stickyPosts as &$post) { + $post->isSticky = true; + } + + return $stickyPosts; + } } diff --git a/source/php/Module/Posts/TemplateController/ExpandableListTemplate.php b/source/php/Module/Posts/TemplateController/ExpandableListTemplate.php index 170d2c361..bfc16e5ec 100644 --- a/source/php/Module/Posts/TemplateController/ExpandableListTemplate.php +++ b/source/php/Module/Posts/TemplateController/ExpandableListTemplate.php @@ -58,7 +58,7 @@ public function __construct(\Modularity\Module\Posts\Posts $module) $this->data['posts_hide_title_column'] = ($this->fields['posts_hide_title_column']) ? true : false; $this->data['title_column_label'] = $this->fields['title_column_label'] ?? null; $this->data['allow_freetext_filtering'] = $this->fields['allow_freetext_filtering'] ?? null; - $this->data['prepareAccordion'] = $this->prepare(); + $this->data['prepareAccordion'] = $this->prepareExpandableList(); } /** @@ -96,13 +96,13 @@ public function getColumnValues(): array * * @return array|null */ - public function prepare(): ?array + public function prepareExpandableList(): ?array { $columnValues = $this->getColumnValues(); $accordion = []; - $this->data['posts'] = $this->preparePosts($this->data['posts']); + $this->data['posts'] = $this->preparePosts($this->module); if (!empty($this->data['posts']) && is_array($this->data['posts'])) { foreach ($this->data['posts'] as $index => $item) { diff --git a/source/php/Module/Posts/TemplateController/ListTemplate.php b/source/php/Module/Posts/TemplateController/ListTemplate.php index c153b03f4..c713c82b6 100644 --- a/source/php/Module/Posts/TemplateController/ListTemplate.php +++ b/source/php/Module/Posts/TemplateController/ListTemplate.php @@ -22,7 +22,8 @@ public function __construct(\Modularity\Module\Posts\Posts $module) { $this->args = $module->args; $this->data = $module->data; - $this->data['posts'] = $this->prepare([ + $this->module = $module; + $this->data['posts'] = $this->prepareList([ 'posts_data_source' => $this->data['posts_data_source'] ?? '', 'archive_link' => $this->data['archive_link'] ?? '', 'archive_link_url' => $this->data['archive_link_url'] ?? '', @@ -35,11 +36,11 @@ public function __construct(\Modularity\Module\Posts\Posts $module) * @param array $postData array of data settings * @return array */ - public function prepare(array $postData) + public function prepareList(array $postData) { $posts = []; if (!empty($this->data['posts']) && is_array($this->data['posts'])) { - $this->data['posts'] = $this->preparePosts($this->data['posts']); + $this->data['posts'] = $this->preparePosts($this->module); foreach ($this->data['posts'] as $post) { if ($post->getPostType() === 'attachment') { diff --git a/source/php/Module/Posts/views/features-grid.blade.php b/source/php/Module/Posts/views/features-grid.blade.php index 69178ff30..45f41d1a7 100644 --- a/source/php/Module/Posts/views/features-grid.blade.php +++ b/source/php/Module/Posts/views/features-grid.blade.php @@ -7,7 +7,10 @@ aria-labelledby="{{ 'mod-posts-' . $ID . '-label' }}"> @if($posts) @foreach ($posts as $post) -
+
attributeList) ? implode(' ', array_map(function($key, $value) { + return $key . '=' . $value; + }, array_keys($post->attributeList), $post->attributeList)) : '' }}> @include('partials.post.box')
@endforeach diff --git a/source/php/Module/Posts/views/grid.blade.php b/source/php/Module/Posts/views/grid.blade.php index 67ddf91d1..092a8d254 100644 --- a/source/php/Module/Posts/views/grid.blade.php +++ b/source/php/Module/Posts/views/grid.blade.php @@ -6,8 +6,11 @@ @if (!$hideTitle && !empty($postTitle)) aria-labelledby="{{ 'mod-posts-' . $ID . '-label' }}" @endif> @if($posts) @foreach ($posts as $post) -
attributeList) ? implode(' ', $post->attributeList) : ''}}> - @if ($loop->first && $highlight_first_column && $highlight_first_column_as === 'card') +
attributeList) ? implode(' ', array_map(function($key, $value) { + return $key . '=' . $value; + }, array_keys($post->attributeList), $post->attributeList)) : '' }}> + @if ($post->isHighlighted && $highlight_first_column_as === 'card') @include('partials.post.card') @else @include('partials.post.block') diff --git a/source/php/Module/Posts/views/index.blade.php b/source/php/Module/Posts/views/index.blade.php index 106570c9d..a86ee170c 100644 --- a/source/php/Module/Posts/views/index.blade.php +++ b/source/php/Module/Posts/views/index.blade.php @@ -7,8 +7,11 @@ @if (!$hideTitle && !empty($postTitle)) aria-labelledby="{{ 'mod-posts-' . $ID . '-label' }}" @endif> @if($posts) @foreach ($posts as $post) -
attributeList) ? implode(' ', $post->attributeList) : ''}}> - @if ($loop->first && $highlight_first_column && $highlight_first_column_as === 'block') +
attributeList) ? implode(' ', array_map(function($key, $value) { + return $key . '=' . $value; + }, array_keys($post->attributeList), $post->attributeList)) : '' }}> + @if ($highlight_first_column_as === 'block' && $post->isHighlighted) @include('partials.post.block', ['ratio' => '16:9']) @else @include('partials.post.card') diff --git a/source/php/Module/Posts/views/news.blade.php b/source/php/Module/Posts/views/news.blade.php index 13b8cfd18..bd7820ec1 100644 --- a/source/php/Module/Posts/views/news.blade.php +++ b/source/php/Module/Posts/views/news.blade.php @@ -7,10 +7,9 @@ @if (!$hideTitle && !empty($postTitle)) aria-labelledby="{{ 'mod-posts-' . $ID . '-label' }}" @endif> @if (!empty($posts)) @foreach ($posts as $post) - @include('partials.post.news-item', [ - 'posts_columns' => $loop->first && $highlight_first_column ? $highlight_first_column : $posts_columns, - 'standing' => $loop->first && $highlight_first_column ? true : false - ]) + @include('partials.post.news-item', [ + 'standing' => $post->isHighlighted ? true : false + ]) @endforeach @endif
\ No newline at end of file diff --git a/source/php/Module/Posts/views/partials/post/box.blade.php b/source/php/Module/Posts/views/partials/post/box.blade.php index 9a4d13907..719b430de 100644 --- a/source/php/Module/Posts/views/partials/post/box.blade.php +++ b/source/php/Module/Posts/views/partials/post/box.blade.php @@ -6,13 +6,6 @@ 'date' => $post->postDateFormatted, 'ratio' => $ratio, 'image' => $post->imageContract ?? $post->image, - 'classList' => $post->classList ?? [], - 'attributeList' => array_merge( - $post->attributeList ?? [], - [ - 'data-js-item-id' => $post->id - ] - ), ]) @slot('metaArea') @includeWhen(!empty($post->readingTime), 'partials.read-time') diff --git a/source/php/Module/Posts/views/partials/post/collection-item.blade.php b/source/php/Module/Posts/views/partials/post/collection-item.blade.php index 4ccdf4732..037e49a59 100644 --- a/source/php/Module/Posts/views/partials/post/collection-item.blade.php +++ b/source/php/Module/Posts/views/partials/post/collection-item.blade.php @@ -1,16 +1,11 @@ @collection__item([ 'link' => $post->permalink, - 'classList' => array_merge($post->classList ?? [], [$posts_columns]), + 'classList' => $post->classList ?? [], 'context' => ['module.posts.collection__item'], 'before' => $post->readingTime, 'containerAware' => true, 'bordered' => true, - 'attributeList' => array_merge( - $post->attributeList ?? [], - [ - 'data-js-item-id' => $post->id - ] - ), + 'attributeList' => $post->attributeList ?? [] ]) @includeWhen( !empty($post->callToActionItems['floating']['icon']), diff --git a/source/php/Module/Posts/views/partials/post/news-item.blade.php b/source/php/Module/Posts/views/partials/post/news-item.blade.php index ae9bce334..197b09a71 100644 --- a/source/php/Module/Posts/views/partials/post/news-item.blade.php +++ b/source/php/Module/Posts/views/partials/post/news-item.blade.php @@ -7,14 +7,9 @@ 'link' => $post->permalink, 'context' => ['module.posts.news-item'], 'hasPlaceholderImage' => $post->hasPlaceholderImage, - 'classList' => array_merge($post->classList ?? [], [$posts_columns]), + 'classList' => $post->classList ?? [], 'standing' => $standing, - 'attributeList' => array_merge( - $post->attributeList ?? [], - [ - 'data-js-item-id' => $post->id - ] - ), + 'attributeList' => $post->attributeList ?? [], ]) @slot('headerLeftArea') @if (!empty($postsSources) && count($postsSources) > 1 && !empty($post->originalSite)) diff --git a/source/php/Module/Posts/views/segment.blade.php b/source/php/Module/Posts/views/segment.blade.php index 876c2a350..0e77677ae 100644 --- a/source/php/Module/Posts/views/segment.blade.php +++ b/source/php/Module/Posts/views/segment.blade.php @@ -7,7 +7,10 @@
@foreach ($posts as $post) -
attributeList) ? implode(' ', $post->attributeList) : ''}}> +
attributeList) ? implode(' ', array_map(function($key, $value) { + return $key . '=' . $value; + }, array_keys($post->attributeList), $post->attributeList)) : '' }}> @include('partials.post.segment')
@endforeach From 7d544f959ad89c53d47d6d51056be96b38f041af Mon Sep 17 00:00:00 2001 From: Niclas Norin <103985736+NiclasNorin@users.noreply.github.com> Date: Mon, 3 Feb 2025 16:36:51 +0100 Subject: [PATCH 2/2] fix: private in post query (#800) --- source/php/Module/Posts/Helper/GetPosts.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/php/Module/Posts/Helper/GetPosts.php b/source/php/Module/Posts/Helper/GetPosts.php index 8bae11ece..ee42c03ec 100644 --- a/source/php/Module/Posts/Helper/GetPosts.php +++ b/source/php/Module/Posts/Helper/GetPosts.php @@ -136,6 +136,11 @@ private function getStickyPostsForSite(array $fields, int $page, array $stickyPo $args['order'] = 'DESC'; $args['posts_per_page'] = $this->getPostsPerPage($fields); + $args['post_status'] = ['publish', 'inherit']; + if ($this->wpService->isUserLoggedIn()) { + $args['post_status'][] = 'private'; + } + $wpQuery = $this->wpQueryFactory->create($args); return $wpQuery->get_posts();