From f8a61a54186f6e7c7696ec06012387513a52d152 Mon Sep 17 00:00:00 2001 From: Sagar Deshmukh Date: Tue, 31 Dec 2024 22:19:44 +0530 Subject: [PATCH] PLANET-7511 Migrate Articles block to Posts List block Ref. https://jira.greenpeace.org/browse/PLANET-7511 --- ...38MigrateArticlesBlockToPostsListBlock.php | 359 ++++++++++++++++++ src/Migrations/Utils/Constants.php | 1 + 2 files changed, 360 insertions(+) create mode 100644 src/Migrations/M038MigrateArticlesBlockToPostsListBlock.php diff --git a/src/Migrations/M038MigrateArticlesBlockToPostsListBlock.php b/src/Migrations/M038MigrateArticlesBlockToPostsListBlock.php new file mode 100644 index 0000000000..2231d49e7b --- /dev/null +++ b/src/Migrations/M038MigrateArticlesBlockToPostsListBlock.php @@ -0,0 +1,359 @@ + isset($attrs['title']) ? $attrs['title'] : '', + 'description' => isset($attrs['description']) ? $attrs['description'] : '', + 'cover_type' => isset($attrs['cover_type']) ? $attrs['cover_type'] : 'content', + 'layout' => isset($attrs['layout']) ? $attrs['layout'] : 'grid', + 'tags' => isset($attrs['tags']) ? $attrs['tags'] : [], + 'posts' => isset($attrs['posts']) ? $attrs['posts'] : [], + 'post_types' => isset($attrs['post_types']) ? $attrs['post_types'] : [], + 'rows' => isset($attrs['initialRowsLimit']) ? $attrs['initialRowsLimit'] : 0, + ]; + } + + /** + * Set all the attributes to create a new Query block. + * + * @param array $tags - The list of post tags. + * @param array $posts_override - The list of posts to include in the query. + * @param array $post_types - The list of terms of the "p4-page-type" taxonomy. + * @param string $layout_type - The layout type (grid or flex). + * @param int $per_page - The number of elements per page. + * @return array - The attributes. + */ + private static function set_query_block_attrs( + array $tags, + array $posts_override, + array $post_types, + string $layout_type, + int $per_page, + ): array { + $query = []; + $query['perPage'] = ($per_page >= 2 || $layout_type === 'flex') ? 8 : 4; + $query['pages'] = 0; + $query['offset'] = 0; + $query['postType'] = Utils\Constants::POST_TYPES_POST; + $query['order'] = 'desc'; + $query['orderBy'] = 'date'; + $query['author'] = ''; + $query['search'] = ''; + $query['exclude'] = []; + $query['sticky'] = ''; + $query['inherit'] = false; + $query['hasPassword'] = false; + $query['postIn'] = $posts_override; + + if (!empty($tags)) { + $query['taxQuery']['post_tag'] = $tags; + } + if (!empty($post_types)) { + $query['taxQuery']['p4-page-type'] = $post_types; + } + + $layout = []; + $layout['type'] = $layout_type; + $layout['columnCount'] = 4; + + $attrs = []; + $attrs['queryId'] = 0; + $attrs['query'] = $query; + $attrs['namespace'] = Utils\Constants::BLOCK_POSTS_LIST; + $attrs['layout'] = $layout; + return $attrs; + } + + /** + * Create and get a new buttons container block. + * + * @return array - The new block. + */ + private static function get_buttons_block(): array + { + return Utils\Functions::create_block_buttons( + [ + 'lock' => [ + 'move' => true, + ], + 'layout' => [ + 'type' => 'flex', + 'justifyContent' => 'space-between', + 'orientation' => 'horizontal', + 'flexWrap' => 'nowrap', + ], + 'className' => 'carousel-controls', + ], + [ + Utils\Functions::create_block_single_button( + ['className' => 'carousel-control-prev'], + __('Prev', 'planet4-blocks'), + ), + Utils\Functions::create_block_single_button( + ['className' => 'carousel-control-next'], + __('Next', 'planet4-blocks'), + ), + ] + ); + } + + /** + * Create and get a new post template. + * + * @return array - The new template. + */ + private static function get_post_template(): array + { + return Utils\Functions::create_post_template( + [ + Utils\Functions::create_block_columns( + [], + [ + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_FEAT_IMAGE, + ['isLink' => true] + ), + Utils\Functions::create_group_block( + [ + Utils\Functions::create_group_block( + [ + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_TERMS, + ['term' => 'post_tag', 'separator' => ' '] + ), + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_TERMS, + ['term' => 'category', 'separator' => ' | '] + ), + ], + ['className' => 'posts-list-meta'] + ), + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_TITLE + ), + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_EXCERPT + ), + Utils\Functions::create_group_block( + [ + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_AUTHOR, + ['isLink' => true] + ), + Utils\Functions::create_new_block( + Utils\Constants::BLOCK_DATE + ), + ], + ['className' => 'posts-list-meta',] + ), + ], + [] + ), + ] + ), + ], + [ + 'lock' => [ + 'move' => true, + 'remove' => true, + ], + ] + ); + } + + /** + * Create and get a new navigation link block. + * + * @return mixed - The navigation link block if the All Stories page is set. + */ + private static function get_nav_links_block(): array + { + if (!get_option('page_for_posts')) { + return []; + } + + return Utils\Functions::create_new_block( + Utils\Constants::BLOCK_NAV_LINK, + [ + 'label' => __('See all stories', 'planet4-blocks'), + 'url' => get_permalink(get_option('page_for_posts')), + 'className' => 'see-all-link', + ] + ); + } + + /** + * Create and get a new query-no-results block. + * + * @return array - The new block. + */ + private static function get_query_no_results_block(): array + { + return Utils\Functions::create_block_query_no_results( + [ + Utils\Functions::create_block_paragraph( + ['placeholder' => __('No posts found. (This default text can be edited)', 'planet4-blocks')], + __('No posts found.', 'planet4-blocks'), + ), + ], + [], + ); + } + + /** + * Create and get a new paragraph block. + * + * @param string $description - The text for the paragraph. + * @return array - The new block. + */ + private static function get_paragraph_block(string $description): array + { + return Utils\Functions::create_block_paragraph( + [ + 'lock' => [ + 'move' => true, + ], + 'placeholder' => __('Enter description', 'planet4-blocks'), + 'style' => [ + 'spacing' => [ + 'margin' => [ + 'top' => '24px', + 'bottom' => '36px', + ], + ], + ], + ], + $description + ); + } + + /** + * Create and get a new group block for the Covers head. + * + * @param string $title - The block title. + * @return array - The new block. + */ + private static function get_head_group_block(string $title): array + { + return Utils\Functions::create_group_block( + [ + Utils\Functions::create_block_heading( + ['lock' => ['move' => true]], + $title + ), + self::get_nav_links_block(), + ], + [ + 'layout' => [ + 'type' => 'flex', + 'justifyContent' => 'space-between', + ], + ] + ); + } +} diff --git a/src/Migrations/Utils/Constants.php b/src/Migrations/Utils/Constants.php index 15a0e05ce9..d3ee68d41b 100644 --- a/src/Migrations/Utils/Constants.php +++ b/src/Migrations/Utils/Constants.php @@ -16,6 +16,7 @@ class Constants public const BLOCK_SOCIAL_MEDIA = self::PREFIX_P4_BLOCKS . '/social-media'; public const BLOCK_P4_COLUMNS = self::PREFIX_P4_BLOCKS . '/columns'; public const BLOCK_ENFORM = self::PREFIX_P4_BLOCKS . '/enform'; + public const BLOCK_ARTICLES = self::PREFIX_P4_BLOCKS . '/articles'; public const BLOCK_EMBED = self::PREFIX_CORE_BLOCKS . '/embed'; public const BLOCK_AUDIO = self::PREFIX_CORE_BLOCKS . '/audio';