|
| 1 | +<?php |
| 2 | + |
| 3 | +// phpcs:disable Generic.Files.LineLength.MaxExceeded |
| 4 | + |
| 5 | +namespace P4\MasterTheme\Migrations; |
| 6 | + |
| 7 | +use P4\MasterTheme\MigrationRecord; |
| 8 | +use P4\MasterTheme\MigrationScript; |
| 9 | + |
| 10 | +/** |
| 11 | + * Migrate Articles block to Posts List blocks. |
| 12 | + */ |
| 13 | +class M039MigrateArticlesBlockToPostsListBlock extends MigrationScript |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Perform the actual migration. |
| 17 | + * |
| 18 | + * @param MigrationRecord $record Information on the execution, can be used to add logs. |
| 19 | + * phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter -- interface implementation |
| 20 | + */ |
| 21 | + public static function execute(MigrationRecord $record): void |
| 22 | + { |
| 23 | + $check_is_valid_block = function ($block) { |
| 24 | + return self::check_is_valid_block($block); |
| 25 | + }; |
| 26 | + |
| 27 | + $transform_block = function ($block) { |
| 28 | + return self::transform_block($block); |
| 29 | + }; |
| 30 | + |
| 31 | + Utils\Functions::execute_block_migration( |
| 32 | + Utils\Constants::BLOCK_ARTICLES, |
| 33 | + $check_is_valid_block, |
| 34 | + $transform_block, |
| 35 | + ); |
| 36 | + } |
| 37 | + // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter |
| 38 | + |
| 39 | + /** |
| 40 | + * Check whether a block is a Articles block. |
| 41 | + * |
| 42 | + * @param array $block - A block data array. |
| 43 | + */ |
| 44 | + private static function check_is_valid_block(array $block): bool |
| 45 | + { |
| 46 | + // Check if the block is valid. |
| 47 | + if (!is_array($block)) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + // Check if the block has a 'blockName' key. |
| 52 | + if (!isset($block['blockName'])) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + // Check if the block is a Articles block. If not, abort. |
| 57 | + if ($block['blockName'] !== Utils\Constants::BLOCK_ARTICLES) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a new Query block based on attributes of a Articles block. |
| 66 | + * |
| 67 | + * @param array $block - The current articles block. |
| 68 | + * @return array - The new block. |
| 69 | + */ |
| 70 | + private static function transform_block(array $block): array |
| 71 | + { |
| 72 | + $existing_block_attrs = self::get_posts_list_block_attrs($block); |
| 73 | + |
| 74 | + $tags = $existing_block_attrs['tags']; |
| 75 | + $posts_override = $existing_block_attrs['posts']; |
| 76 | + $post_types = $existing_block_attrs['post_types']; |
| 77 | + $layout_type = $existing_block_attrs['layout'] === 'carousel' ? 'flex' : 'grid'; |
| 78 | + $classname = $existing_block_attrs['layout'] === 'carousel' ? 'carousel' : 'grid'; |
| 79 | + $rows = $existing_block_attrs['rows']; |
| 80 | + |
| 81 | + $attrs = self::set_query_block_attrs($tags, $posts_override, $post_types, $layout_type, $rows); |
| 82 | + |
| 83 | + $inner_blocks = [ |
| 84 | + self::get_head_group_block($existing_block_attrs['title']), |
| 85 | + self::get_paragraph_block($existing_block_attrs['description']), |
| 86 | + self::get_query_no_results_block(), |
| 87 | + self::get_post_template(), |
| 88 | + self::get_buttons_block(), |
| 89 | + self::get_nav_links_block(), |
| 90 | + ]; |
| 91 | + |
| 92 | + return Utils\Functions::create_block_query( |
| 93 | + $inner_blocks, |
| 94 | + $attrs, |
| 95 | + $classname |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the attributes of a Post lists block. |
| 101 | + * |
| 102 | + * @param array $existing_block - The current Articles block. |
| 103 | + * @return array - The attributes. |
| 104 | + */ |
| 105 | + private static function get_posts_list_block_attrs(array $existing_block): array |
| 106 | + { |
| 107 | + $attrs = $existing_block['attrs']; |
| 108 | + |
| 109 | + return [ |
| 110 | + 'title' => isset($attrs['title']) ? $attrs['title'] : '', |
| 111 | + 'description' => isset($attrs['description']) ? $attrs['description'] : '', |
| 112 | + 'cover_type' => isset($attrs['cover_type']) ? $attrs['cover_type'] : 'content', |
| 113 | + 'layout' => isset($attrs['layout']) ? $attrs['layout'] : 'grid', |
| 114 | + 'tags' => isset($attrs['tags']) ? $attrs['tags'] : [], |
| 115 | + 'posts' => isset($attrs['posts']) ? $attrs['posts'] : [], |
| 116 | + 'post_types' => isset($attrs['post_types']) ? $attrs['post_types'] : [], |
| 117 | + 'rows' => isset($attrs['initialRowsLimit']) ? $attrs['initialRowsLimit'] : 0, |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Set all the attributes to create a new Query block. |
| 123 | + * |
| 124 | + * @param array $tags - The list of post tags. |
| 125 | + * @param array $posts_override - The list of posts to include in the query. |
| 126 | + * @param array $post_types - The list of terms of the "p4-page-type" taxonomy. |
| 127 | + * @param string $layout_type - The layout type (grid or flex). |
| 128 | + * @param int $per_page - The number of elements per page. |
| 129 | + * @return array - The attributes. |
| 130 | + */ |
| 131 | + private static function set_query_block_attrs( |
| 132 | + array $tags, |
| 133 | + array $posts_override, |
| 134 | + array $post_types, |
| 135 | + string $layout_type, |
| 136 | + int $per_page, |
| 137 | + ): array { |
| 138 | + $query = []; |
| 139 | + $query['perPage'] = ($per_page >= 2 || $layout_type === 'flex') ? 8 : 4; |
| 140 | + $query['pages'] = 0; |
| 141 | + $query['offset'] = 0; |
| 142 | + $query['postType'] = Utils\Constants::POST_TYPES_POST; |
| 143 | + $query['order'] = 'desc'; |
| 144 | + $query['orderBy'] = 'date'; |
| 145 | + $query['author'] = ''; |
| 146 | + $query['search'] = ''; |
| 147 | + $query['exclude'] = []; |
| 148 | + $query['sticky'] = ''; |
| 149 | + $query['inherit'] = false; |
| 150 | + $query['hasPassword'] = false; |
| 151 | + $query['postIn'] = $posts_override; |
| 152 | + |
| 153 | + if (!empty($tags)) { |
| 154 | + $query['taxQuery']['post_tag'] = $tags; |
| 155 | + } |
| 156 | + if (!empty($post_types)) { |
| 157 | + $query['taxQuery']['p4-page-type'] = $post_types; |
| 158 | + } |
| 159 | + |
| 160 | + $layout = []; |
| 161 | + $layout['type'] = $layout_type; |
| 162 | + $layout['columnCount'] = 4; |
| 163 | + |
| 164 | + $attrs = []; |
| 165 | + $attrs['queryId'] = 0; |
| 166 | + $attrs['query'] = $query; |
| 167 | + $attrs['namespace'] = Utils\Constants::BLOCK_POSTS_LIST; |
| 168 | + $attrs['layout'] = $layout; |
| 169 | + return $attrs; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Create and get a new buttons container block. |
| 174 | + * |
| 175 | + * @return array - The new block. |
| 176 | + */ |
| 177 | + private static function get_buttons_block(): array |
| 178 | + { |
| 179 | + return Utils\Functions::create_block_buttons( |
| 180 | + [ |
| 181 | + 'lock' => [ |
| 182 | + 'move' => true, |
| 183 | + ], |
| 184 | + 'layout' => [ |
| 185 | + 'type' => 'flex', |
| 186 | + 'justifyContent' => 'space-between', |
| 187 | + 'orientation' => 'horizontal', |
| 188 | + 'flexWrap' => 'nowrap', |
| 189 | + ], |
| 190 | + 'className' => 'carousel-controls', |
| 191 | + ], |
| 192 | + [ |
| 193 | + Utils\Functions::create_block_single_button( |
| 194 | + ['className' => 'carousel-control-prev'], |
| 195 | + __('Prev', 'planet4-blocks'), |
| 196 | + ), |
| 197 | + Utils\Functions::create_block_single_button( |
| 198 | + ['className' => 'carousel-control-next'], |
| 199 | + __('Next', 'planet4-blocks'), |
| 200 | + ), |
| 201 | + ] |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Create and get a new post template. |
| 207 | + * |
| 208 | + * @return array - The new template. |
| 209 | + */ |
| 210 | + private static function get_post_template(): array |
| 211 | + { |
| 212 | + return Utils\Functions::create_post_template( |
| 213 | + [ |
| 214 | + Utils\Functions::create_block_columns( |
| 215 | + [], |
| 216 | + [ |
| 217 | + Utils\Functions::create_new_block( |
| 218 | + Utils\Constants::BLOCK_FEAT_IMAGE, |
| 219 | + ['isLink' => true] |
| 220 | + ), |
| 221 | + Utils\Functions::create_group_block( |
| 222 | + [ |
| 223 | + Utils\Functions::create_group_block( |
| 224 | + [ |
| 225 | + Utils\Functions::create_new_block( |
| 226 | + Utils\Constants::BLOCK_TERMS, |
| 227 | + ['term' => 'post_tag', 'separator' => ' '] |
| 228 | + ), |
| 229 | + Utils\Functions::create_new_block( |
| 230 | + Utils\Constants::BLOCK_TERMS, |
| 231 | + ['term' => 'category', 'separator' => ' | '] |
| 232 | + ), |
| 233 | + ], |
| 234 | + ['className' => 'posts-list-meta'] |
| 235 | + ), |
| 236 | + Utils\Functions::create_new_block( |
| 237 | + Utils\Constants::BLOCK_TITLE |
| 238 | + ), |
| 239 | + Utils\Functions::create_new_block( |
| 240 | + Utils\Constants::BLOCK_EXCERPT |
| 241 | + ), |
| 242 | + Utils\Functions::create_group_block( |
| 243 | + [ |
| 244 | + Utils\Functions::create_new_block( |
| 245 | + Utils\Constants::BLOCK_AUTHOR, |
| 246 | + ['isLink' => true] |
| 247 | + ), |
| 248 | + Utils\Functions::create_new_block( |
| 249 | + Utils\Constants::BLOCK_DATE |
| 250 | + ), |
| 251 | + ], |
| 252 | + ['className' => 'posts-list-meta',] |
| 253 | + ), |
| 254 | + ], |
| 255 | + [] |
| 256 | + ), |
| 257 | + ] |
| 258 | + ), |
| 259 | + ], |
| 260 | + [ |
| 261 | + 'lock' => [ |
| 262 | + 'move' => true, |
| 263 | + 'remove' => true, |
| 264 | + ], |
| 265 | + ] |
| 266 | + ); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Create and get a new navigation link block. |
| 271 | + * |
| 272 | + * @return mixed - The navigation link block if the All Stories page is set. |
| 273 | + */ |
| 274 | + private static function get_nav_links_block(): array |
| 275 | + { |
| 276 | + if (!get_option('page_for_posts')) { |
| 277 | + return []; |
| 278 | + } |
| 279 | + |
| 280 | + return Utils\Functions::create_new_block( |
| 281 | + Utils\Constants::BLOCK_NAV_LINK, |
| 282 | + [ |
| 283 | + 'label' => __('See all stories', 'planet4-blocks'), |
| 284 | + 'url' => get_permalink(get_option('page_for_posts')), |
| 285 | + 'className' => 'see-all-link', |
| 286 | + ] |
| 287 | + ); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Create and get a new query-no-results block. |
| 292 | + * |
| 293 | + * @return array - The new block. |
| 294 | + */ |
| 295 | + private static function get_query_no_results_block(): array |
| 296 | + { |
| 297 | + return Utils\Functions::create_block_query_no_results( |
| 298 | + [ |
| 299 | + Utils\Functions::create_block_paragraph( |
| 300 | + ['placeholder' => __('No posts found. (This default text can be edited)', 'planet4-blocks')], |
| 301 | + __('No posts found.', 'planet4-blocks'), |
| 302 | + ), |
| 303 | + ], |
| 304 | + [], |
| 305 | + ); |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * Create and get a new paragraph block. |
| 310 | + * |
| 311 | + * @param string $description - The text for the paragraph. |
| 312 | + * @return array - The new block. |
| 313 | + */ |
| 314 | + private static function get_paragraph_block(string $description): array |
| 315 | + { |
| 316 | + return Utils\Functions::create_block_paragraph( |
| 317 | + [ |
| 318 | + 'lock' => [ |
| 319 | + 'move' => true, |
| 320 | + ], |
| 321 | + 'placeholder' => __('Enter description', 'planet4-blocks'), |
| 322 | + 'style' => [ |
| 323 | + 'spacing' => [ |
| 324 | + 'margin' => [ |
| 325 | + 'top' => '24px', |
| 326 | + 'bottom' => '36px', |
| 327 | + ], |
| 328 | + ], |
| 329 | + ], |
| 330 | + ], |
| 331 | + $description |
| 332 | + ); |
| 333 | + } |
| 334 | + |
| 335 | + /** |
| 336 | + * Create and get a new group block for the Covers head. |
| 337 | + * |
| 338 | + * @param string $title - The block title. |
| 339 | + * @return array - The new block. |
| 340 | + */ |
| 341 | + private static function get_head_group_block(string $title): array |
| 342 | + { |
| 343 | + return Utils\Functions::create_group_block( |
| 344 | + [ |
| 345 | + Utils\Functions::create_block_heading( |
| 346 | + ['lock' => ['move' => true]], |
| 347 | + $title |
| 348 | + ), |
| 349 | + self::get_nav_links_block(), |
| 350 | + ], |
| 351 | + [ |
| 352 | + 'layout' => [ |
| 353 | + 'type' => 'flex', |
| 354 | + 'justifyContent' => 'space-between', |
| 355 | + ], |
| 356 | + ] |
| 357 | + ); |
| 358 | + } |
| 359 | +} |
0 commit comments