Skip to content

Commit

Permalink
PLANET-7445: Migrate Split Two Columns blocks to Columns blocks (#2433)
Browse files Browse the repository at this point in the history
* PLANET-7521: Migrate Media Blocks

- Media blocks were migrated to audio/video default blocks

* PLANET-7521: Fix lint issues

- Lint issues were fixed

* [RESET] PLANET-7521: Fix feedback

- The feedback received in the PR was fixed

* PLANET-7445 Migrate Split Two Columns block existing content

Migrate split 2 columns block into columns blocks

Ref. https://jira.greenpeace.org/browse/PLANET-7445

* PLANET-7445 Fix migration script for old split2column block

Ref. https://jira.greenpeace.org/browse/PLANET-7445

* PLANET-7445 Update migration script ID to 034

Update migration scrip id & file name

* PLANET-7445 Update PR as per base branch utility constants and functions

Use a reusable code from base branch

* PLANET-7445 Added migration name in cmd output

The migration name is helpful to track the exact migration start and if any errors occur during execution

* PLANET-7445 Fix feedback changes

Ref https://jira.greenpeace.org/browse/PLANET-7445

* PLANET-7445: Migrate split 2 columns block

- Implement minor adjustments and extra conditions

* PLANET-7445: Migrate split 2 columns block

- Rename migration

* PLANET-7445: Migrate split 2 columns block

- Include all post status

* PLANET-7445: Migrate split 2 columns block

- Fix comments from code review

* PLANET-7521: Migrate Media Blocks

- Media blocks were migrated to audio/video default blocks

* PLANET-7521: Fix lint issues

- Lint issues were fixed

* [RESET] PLANET-7521: Fix feedback

- The feedback received in the PR was fixed

* PLANET-7445 Migrate Split Two Columns block existing content

Migrate split 2 columns block into columns blocks

Ref. https://jira.greenpeace.org/browse/PLANET-7445

* PLANET-7445 Fix migration script for old split2column block

Ref. https://jira.greenpeace.org/browse/PLANET-7445

* PLANET-7445 Update migration script ID to 034

Update migration scrip id & file name

* PLANET-7445 Update PR as per base branch utility constants and functions

Use a reusable code from base branch

* PLANET-7445 Added migration name in cmd output

The migration name is helpful to track the exact migration start and if any errors occur during execution

* PLANET-7445 Fix feedback changes

Ref https://jira.greenpeace.org/browse/PLANET-7445

* PLANET-7445: Migrate split 2 columns block

- Implement minor adjustments and extra conditions

* PLANET-7445: Migrate split 2 columns block

- Rename migration

* PLANET-7445: Migrate split 2 columns block

- Include all post status

* PLANET-7445: Migrate split 2 columns block

- Add reusable functions

* PLANET-7445: Migrate split 2 columns block

- Fix lint issues

* [RESET] PLANET-7445: Migrate split 2 columns block

- Fix edge cases
- Manual database reset

* PLANET-7445: Migrate split 2 columns block

- Make the execute function reusable

* [RESET] PLANET-7445: Migrate split 2 columns block

- Fix lint issues

---------

Co-authored-by: Sagar Deshmukh <sagarsdeshmukh91@gmail.com>
  • Loading branch information
mardelnet and sagarsdeshmukh authored Nov 11, 2024
1 parent ad7dfbd commit 0e42518
Show file tree
Hide file tree
Showing 7 changed files with 585 additions and 29 deletions.
5 changes: 0 additions & 5 deletions exporter-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ function get_attachments_used_in_content(string $content): array
}
break;

case 'planet4-blocks/split-two-columns':
$attachment_ids[] = $block['attrs']['issue_image'] ?? '';
$attachment_ids[] = $block['attrs']['tag_image'] ?? '';
break;

case 'planet4-blocks/columns':
if (isset($block['attrs']['columns'])) {
foreach ($block['attrs']['columns'] as $column) {
Expand Down
1 change: 0 additions & 1 deletion src/BlockSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ public function set_allowed_block_types(array|bool $allowed_block_types, object
self::PAGE_BLOCK_TYPES,
! Features::is_active(self::BETA_BLOCKS_FEATURE) ? [] : self::BETA_PAGE_BLOCK_TYPES,
! $enform_active ? [] : [ self::P4_BLOCKS_PREFIX . '/enform' ],
(bool) planet4_get_option('new_ia') ? [] : [ self::P4_BLOCKS_PREFIX . '/split-two-columns' ],
self::BLOCK_TEMPLATES,
);

Expand Down
7 changes: 0 additions & 7 deletions src/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ public function update_attachements(int $post_id, int $original_post_id, array $
}
break;

case 'planet4-blocks/split-two-columns':
$filter_data[] = isset($block['attrs']['issue_image'])
? 'issue_image":' . $block['attrs']['issue_image'] : '';
$filter_data[] = isset($block['attrs']['tag_image'])
? 'tag_image":' . $block['attrs']['tag_image'] : '';
break;

case 'planet4-blocks/columns':
if (isset($block['attrs']['columns'])) {
foreach ($block['attrs']['columns'] as $column) {
Expand Down
240 changes: 240 additions & 0 deletions src/Migrations/M032MigrateSplit2ColumnBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php

declare(strict_types=1);

namespace P4\MasterTheme\Migrations;

use P4\MasterTheme\MigrationRecord;
use P4\MasterTheme\MigrationScript;

/**
* Migrate split-two-columns block to columns block.
*/
class M032MigrateSplit2ColumnBlock extends MigrationScript
{
/**
* Extract split-two-columns block from page/posts and transform it into columns block.
*
* @param MigrationRecord $record Information on the execution, can be used to add logs.
* phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter -- interface implementation
*/
public static function execute(MigrationRecord $record): void
{
$check_is_valid_block = function ($block) {
return self::check_is_valid_block($block);
};

$transform_block = function ($block) {
return self::transform_block($block);
};

Utils\Functions::execute_block_migration(
Utils\Constants::BLOCK_SPLIT_TWO_COLUMNS,
$check_is_valid_block,
$transform_block,
);
}
// phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter

/**
* Check whether a block is a Split 2 columns block.
*
* @param array $block - A block data array.
*/
private static function check_is_valid_block(array $block): bool
{
// Check if the block is valid.
if (!is_array($block)) {
return false;
}

// Check if the block has a 'blockName' key.
if (!isset($block['blockName'])) {
return false;
}

// Check if the block is a split-two-columns block. If not, abort.
return $block['blockName'] === Utils\Constants::BLOCK_SPLIT_TWO_COLUMNS;
}

/**
* Transform the Split 2 columns block into a Columns block.
*
* @param array $block - A block data array.
* @return array - The transformed block.
*/
private static function transform_block(array $block): array
{
$block_attrs = self::get_split_2_columns_block_attrs($block['attrs']);

return Utils\Functions::create_block_columns(
[],
[
self::create_left_column($block_attrs),
self::create_right_column($block_attrs),
]
);
}

/**
* Create the left column of the new blocks.
*
* @param array $block_attrs - The original block attributes.
* @return array - The left column blocks.
*/
private static function create_left_column(array $block_attrs): array
{
$heading_block = Utils\Functions::create_block_heading(
['level' => 3],
$block_attrs['column1']['title']
);

$paragraph_block_1 = Utils\Functions::create_block_paragraph(
[],
$block_attrs['column1']['description']
);

$paragraph_block_2 = Utils\Functions::create_block_paragraph(
[],
'<a href="' . $block_attrs['column1']['link_path'] . '">' . $block_attrs['column1']['link_text'] . '</a>'
);

$media_text_block = Utils\Functions::create_media_text_block(
[
'mediaId' => $block_attrs['column1']['image_id'],
'mediaLink' => $block_attrs['column1']['image_src'],
'mediaType' => 'image',
],
[
$heading_block,
$paragraph_block_1,
$paragraph_block_2,
],
$block_attrs['column1']['image_src'],
(int)$block_attrs['column1']['image_id']
);

$blocks = Utils\Functions::create_block_single_column(
['verticalAlignment' => 'center'],
[$media_text_block],
);

return $blocks;
}

/**
* Create the right column of the new blocks.
*
* @param array $block_attrs - The original block attributes.
* @return array - The right column blocks.
*/
private static function create_right_column(array $block_attrs): array
{
$heading_block = Utils\Functions::create_block_heading(
['level' => 3],
$block_attrs['column2']['title'] ? '#' . $block_attrs['column2']['title'] : ''
);

$paragraph_block = Utils\Functions::create_block_paragraph(
[],
$block_attrs['column2']['description']
);

if ($block_attrs['column2']['button_link']) {
$single_button_block = Utils\Functions::create_block_single_button(
['className' => 'is-style-cta'],
$block_attrs['column2']['button_text'],
$block_attrs['column2']['button_link'],
);
}

$buttons_block = Utils\Functions::create_block_buttons(
['className' => 'carousel-controls'],
[$single_button_block],
);

$media_text_block = Utils\Functions::create_media_text_block(
[
'mediaId' => $block_attrs['column2']['image_id'],
'mediaLink' => $block_attrs['column2']['image_src'],
'mediaType' => 'image',
],
[
$heading_block,
$paragraph_block,
$buttons_block,
],
$block_attrs['column2']['image_src'],
(int)$block_attrs['column2']['image_id']
);

$blocks = Utils\Functions::create_block_single_column(
['verticalAlignment' => 'center'],
[$media_text_block],
);

return $blocks;
}

/**
* Get the split-two-columns block attrs.
*
* @param array $block - A parsed split-two-columns block.
* @return array - A block attrs array.
* phpcs:disable Generic.Files.LineLength.MaxExceeded
*/
private static function get_split_2_columns_block_attrs(array $block): array
{
$block_attrs = [];

// For old split 2 column versions.
$issue_id = (int) ( $block['select_issue'] ?? null );
$tag_id = (int) ( $block['select_tag'] ?? null );
$issue_image_id = (int) ( $block['issue_image'] ?? $block['issue_image_id'] ?? get_post_thumbnail_id($issue_id) ?? 0 );
$tag_image_id = (int) ( $block['tag_image'] ?? $block['tag_image_id'] ?? get_term_meta($tag_id, 'tag_attachment_id', true) ?? 0 );

if ($issue_id) {
$issue_meta_data = get_post_meta($issue_id);
$block['title'] = !empty($block['title']) ? $block['title'] : $issue_meta_data['p4_title'][0] ?? get_the_title($issue_id);
$block['issue_description'] = $block['issue_description'] ?? $issue_meta_data['p4_description'][0] ?? '';
$block['issue_link_path'] = $block['issue_link_path'] ?? get_permalink($issue_id);
$block['issue_link_text'] = $block['issue_link_text'] ?? __('Learn more about this issue', 'planet4-blocks');

$block['issue_image_id'] = $block['issue_image_id'] ?? $issue_image_id;
$block['issue_image_src'] = $block['issue_image_src'] ?? ($issue_image_id ? wp_get_attachment_url($issue_image_id) : '');
}

if ($tag_id) {
$tag = get_term($tag_id);
if ($tag instanceof \WP_Term) {
$block['tag_name'] = $block['tag_name'] ?? $tag->name ?? '';
$block['tag_link'] = get_tag_link($tag);
$block['tag_description'] = $block['tag_description'] ?? $tag->description ?? '';

$block['tag_image_id'] = $block['tag_image_id'] ?? $tag_image_id;
$block['tag_image_src'] = $block['tag_image_src'] ?? ($tag_image_id ? wp_get_attachment_url($tag_image_id) : '');

$block['button_text'] = $block['button_text'] ?? __('Get involved', 'planet4-blocks');
$block['button_link'] = $block['button_link'] ?? $block['tag_link'] ?? '';
}
}

$block_attrs['column1']['title'] = $block['title'] ?? '';
$block_attrs['column1']['description'] = wp_trim_words($block['issue_description'] ?? '', 12);
$block_attrs['column1']['link_text'] = $block['issue_link_text'] ?? '';
$block_attrs['column1']['link_path'] = $block['issue_link_path'] ?? '';
$block_attrs['column1']['image_id'] = $block['issue_image_id'] ?? '';
$block_attrs['column1']['image_src'] = $block['issue_image_src'] ?? '';

$block_attrs['column2']['title'] = $block['tag_name'] ?? '';
$block_attrs['column2']['description'] = wp_trim_words($block['tag_description'] ?? '', 12);
$block_attrs['column2']['button_text'] = $block['button_text'] ?? '';
$block_attrs['column2']['button_link'] = $block['button_link'] ?? '';
$block_attrs['column2']['link_path'] = $block['tag_link'] ?? '';
$block_attrs['column2']['image_id'] = $block['tag_image_id'] ?? '';
$block_attrs['column2']['image_src'] = $block['tag_image_src'] ?? '';

return $block_attrs;
}
// phpcs:enable Generic.Files.LineLength.MaxExceeded
}
14 changes: 14 additions & 0 deletions src/Migrations/Utils/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ class Constants
private const PREFIX_CORE_BLOCKS = 'core';

public const BLOCK_MEDIA_VIDEO = self::PREFIX_P4_BLOCKS . '/media-video';
public const BLOCK_SPLIT_TWO_COLUMNS = self::PREFIX_P4_BLOCKS . '/split-two-columns';

public const BLOCK_EMBED = self::PREFIX_CORE_BLOCKS . '/embed';
public const BLOCK_AUDIO = self::PREFIX_CORE_BLOCKS . '/audio';
public const BLOCK_VIDEO = self::PREFIX_CORE_BLOCKS . '/video';
public const BLOCK_GROUP = self::PREFIX_CORE_BLOCKS . '/group';
public const BLOCK_HEADING = self::PREFIX_CORE_BLOCKS . '/heading';
public const BLOCK_PARAGRAPH = self::PREFIX_CORE_BLOCKS . '/paragraph';
public const BLOCK_SINGLE_COLUMN = self::PREFIX_CORE_BLOCKS . '/column';
public const BLOCK_COLUMNS = self::PREFIX_CORE_BLOCKS . '/columns';
public const BLOCK_SINGLE_BUTTON = self::PREFIX_CORE_BLOCKS . '/button';
public const BLOCK_BUTTONS = self::PREFIX_CORE_BLOCKS . '/buttons';
public const BLOCK_MEDIA_TEXT = self::PREFIX_CORE_BLOCKS . '/media-text';

public const POST_TYPES_PAGE = 'page';
public const POST_TYPES_POST = 'post';
Expand All @@ -30,4 +36,12 @@ class Constants
self::POST_TYPES_ACTION,
self::POST_TYPES_CAMPAIGN,
];

public const POST_STATUS_LIST = [
'publish',
'pending',
'draft',
'future',
'private',
];
}
Loading

0 comments on commit 0e42518

Please sign in to comment.