-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANET-7369 Use custom button text setting in Actions List block (#2292)
* PLANET-7369 Use custom button text setting in Actions List block This is to replicate the Covers block behaviour
- Loading branch information
Showing
9 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {useEntityProp} from '@wordpress/core-data'; | ||
|
||
const {__} = wp.i18n; | ||
|
||
const DEFAULT_TEXT = window.p4_vars.options.take_action_covers_button_text || __('Take action', 'planet4-blocks'); | ||
|
||
export default function Edit({context: {postId}}) { | ||
const [metaFields] = useEntityProp('postType', 'p4_action', 'meta', postId); | ||
|
||
return ( | ||
<button className="btn btn-primary btn-small"> | ||
{metaFields?.action_button_text || DEFAULT_TEXT} | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {registerBlockType} from '@wordpress/blocks'; | ||
|
||
import edit from './edit'; | ||
|
||
export const registerActionButtonTextBlock = () => registerBlockType( | ||
'planet4-blocks/action-button-text', | ||
{ | ||
title: 'Action Button Text', | ||
category: 'planet4-blocks', | ||
description: 'Displays the custom button text for an Action, or the default text if undefined', | ||
usesContext: ['postId'], | ||
edit, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* Action Button Text block class. | ||
* | ||
* @package P4\MasterTheme | ||
* @since 0.1 | ||
*/ | ||
|
||
namespace P4\MasterTheme\Blocks; | ||
|
||
use WP_Block; | ||
|
||
/** | ||
* Class ActionButtonText | ||
* | ||
* @package P4\MasterTheme\Blocks | ||
*/ | ||
class ActionButtonText extends BaseBlock | ||
{ | ||
/** | ||
* Block name. | ||
* | ||
* @const string BLOCK_NAME. | ||
*/ | ||
public const BLOCK_NAME = 'action-button-text'; | ||
|
||
/** | ||
* ActionButtonText constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->register_action_button_text_block(); | ||
} | ||
|
||
/** | ||
* Register ActionButtonText block. | ||
*/ | ||
public function register_action_button_text_block(): void | ||
{ | ||
register_block_type( | ||
self::get_full_block_name(), | ||
[ | ||
'render_callback' => [ $this, 'render_block' ], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Required by the `Base_Block` class. | ||
* | ||
* @param array $fields Unused, required by the abstract function. | ||
* @phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter | ||
*/ | ||
public function prepare_data(array $fields): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* @return string Returns the value for the field. | ||
* @phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter | ||
*/ | ||
public function render_block(array $attributes, string $content, WP_Block $block): string | ||
{ | ||
$post_id = $block->context['postId']; | ||
$link = get_permalink($post_id); | ||
$meta = get_post_meta($post_id); | ||
if (isset($meta['action_button_text']) && $meta['action_button_text'][0]) { | ||
$button_text = $meta['action_button_text'][0]; | ||
} else { | ||
$options = get_option('planet4_options'); | ||
$button_text = $options['take_action_covers_button_text'] ?? __('Take action', 'planet4-blocks'); | ||
} | ||
return '<a href="' . $link . '" class="btn btn-primary btn-small">' . $button_text . '</a>'; | ||
} | ||
// @phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters