Skip to content

Commit

Permalink
Merge branch 'develop' into fix/headding-block-deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
kurudrive authored Jan 15, 2024
2 parents 0b8cdca + eb2c919 commit 3e88272
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 11 deletions.
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ e.g.

== Changelog ==

[ Other ][ Headding ] Marked as Not Recommended
[ Add Function ][ Dynamic Text (Pro) ] Add feature to display logged-in username.
[ Specification Change ][ Headding ] Marked as Not Recommended

= 1.67.0 =
[ Add Block ][ Category Badge (Pro) ] Creates badges displaying linked categories or terms for posts, with flexible design customization.
Expand Down
16 changes: 16 additions & 0 deletions src/blocks/_pro/dynamic-text/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
"type": "string",
"default": null
},
"userNamePrefixText": {
"type": "string",
"default": null
},
"userNameSuffixText": {
"type": "string",
"default": null
},
"userNameLoggedOutText": {
"type": "string",
"default": null
},
"isLoginLink": {
"type": "boolean",
"default": true
},
"fieldType": {
"type": "string",
"default": "text"
Expand Down
85 changes: 81 additions & 4 deletions src/blocks/_pro/dynamic-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
BlockControls,
InspectorControls,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import ServerSideRender from '@wordpress/server-side-render';

/**
Expand Down Expand Up @@ -85,6 +86,10 @@ export default function DynamicTextEdit(props) {
ancestorPageHiddenOption,
parentPageHiddenOption,
customFieldName,
userNamePrefixText,
userNameSuffixText,
userNameLoggedOutText,
isLoginLink,
fieldType,
isLinkSet,
isLinkTarget,
Expand All @@ -101,10 +106,17 @@ export default function DynamicTextEdit(props) {
}),
});

const postType = wp.data.select('core/editor').getCurrentPostType();
const parentPageId = wp.data
.select('core/editor')
.getEditedPostAttribute('parent');
const { postType, parentPageId, currentUser } = useSelect((select) => {
const { getCurrentPostType, getEditedPostAttribute } =
select('core/editor');
const { getCurrentUser } = select('core');

return {
postType: getCurrentPostType(),
parentPageId: getEditedPostAttribute('parent'),
currentUser: getCurrentUser(),
};
}, []);

let editContent;
const editAlertContent = (
Expand All @@ -127,12 +139,28 @@ export default function DynamicTextEdit(props) {
editContent = (
<TagName>{__('Parent Page Title', 'vk-blocks-pro')}</TagName>
);
} else if (displayElement === 'user-name' && !currentUser) {
editContent = (
<TagName>
{(userNamePrefixText ?? '') +
currentUser.name +
(userNameSuffixText ?? '')}
</TagName>
);
} else if (displayElement === 'custom-field' && !postType) {
editContent = (
<TagName>
{__('Custom field', 'vk-blocks-pro')} ({customFieldName})
</TagName>
);
} else if (displayElement === 'user-name') {
editContent = (
<TagName>
{(userNamePrefixText ?? '') +
currentUser.name +
(userNameSuffixText ?? '')}
</TagName>
);
} else if (displayElement === 'custom-field' && !customFieldName) {
editContent = (
<div className="alert alert-warning text-center">
Expand Down Expand Up @@ -203,6 +231,13 @@ export default function DynamicTextEdit(props) {
'vk-blocks-pro'
),
},
{
value: 'user-name',
label: __(
'Current login user name',
'vk-blocks-pro'
),
},
{
value: 'custom-field',
label: __('Custom Field', 'vk-blocks-pro'),
Expand Down Expand Up @@ -258,6 +293,48 @@ export default function DynamicTextEdit(props) {
)}
</BaseControl>
)}
{displayElement === 'user-name' && (
<BaseControl>
<TextControl
label={__('Prefix Label', 'vk-blocks-pro')}
value={userNamePrefixText}
onChange={(value) =>
setAttributes({ userNamePrefixText: value })
}
/>
<TextControl
label={__('Suffix Label', 'vk-blocks-pro')}
value={userNameSuffixText}
onChange={(value) =>
setAttributes({ userNameSuffixText: value })
}
/>
<TextControl
label={__(
'Text for Logged Out Users',
'vk-blocks-pro'
)}
value={userNameLoggedOutText}
onChange={(value) =>
setAttributes({
userNameLoggedOutText: value,
})
}
/>
<ToggleControl
label={__(
'Link to Login on Logout',
'vk-blocks-pro'
)}
checked={isLoginLink}
onChange={(checked) =>
setAttributes({
isLoginLink: checked,
})
}
/>
</BaseControl>
)}
{displayElement === 'custom-field' && (
<BaseControl>
<TextControl
Expand Down
48 changes: 48 additions & 0 deletions src/blocks/_pro/dynamic-text/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function vk_blocks_dynamic_text_get_attributes_default() {
'tagName' => 'div',
'ancestorPageHiddenOption' => null,
'parentPageHiddenOption' => null,
'userNameBeforeText' => null,
'userNameAfterText' => null,
'customFieldName' => null,
'fieldType' => 'text',
'isLinkSet' => false,
Expand Down Expand Up @@ -131,6 +133,31 @@ function vk_blocks_dynamic_text_render_callback( $attributes, $content, $block )
$parent_post_title = get_post( $post )->post_title;
}
$block_content .= $parent_post_title;
} elseif ( 'user-name' === $attributes['displayElement'] ) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( $current_user->display_name ) {
$prefix = isset( $attributes['userNamePrefixText'] ) ? esc_html( $attributes['userNamePrefixText'] ) : '';
$suffix = isset( $attributes['userNameSuffixText'] ) ? esc_html( $attributes['userNameSuffixText'] ) : '';

$block_content .= $prefix . $current_user->display_name . $suffix;
}
} else {
$loggedout_text = isset( $attributes['userNameLoggedOutText'] ) ? esc_html( $attributes['userNameLoggedOutText'] ) : '';
if ( isset( $attributes['isLoginLink'] ) && $attributes['isLoginLink'] ) {
$post = get_post();
if ( is_singular() ) {
$current_url = get_permalink( $post->id );
} else {
$http_host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $http_host . $request_uri;
}
$block_content .= '<a href="' . wp_login_url( $current_url ) . '">' . esc_html( $loggedout_text ) . '</a>';
} else {
$block_content .= esc_html( $loggedout_text );
}
}
} elseif ( 'custom-field' === $attributes['displayElement'] ) {
$block_content .= vk_blocks_dynamic_text_custom_field_render( $attributes, $content, $block );
}
Expand All @@ -141,6 +168,11 @@ function vk_blocks_dynamic_text_render_callback( $attributes, $content, $block )
return $block_content;
}

/**
* Register Dynamic Text block.
*
* @return void
*/
/**
* Register Dynamic Text block.
*
Expand Down Expand Up @@ -188,6 +220,22 @@ function vk_blocks_register_block_dynamic_text() {
'type' => 'boolean',
'default' => true,
),
'userNamePrefixText' => array(
'type' => 'string',
'default' => '',
),
'userNameSuffixText' => array(
'type' => 'string',
'default' => '',
),
'userNameLoggedOutText' => array(
'type' => 'string',
'default' => '',
),
'isLoginLink' => array(
'type' => 'boolean',
'default' => true,
),
'customFieldName' => array(
'type' => 'string',
'default' => '',
Expand Down
72 changes: 66 additions & 6 deletions test/phpunit/pro/test-dynamic-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,66 @@ public function test_vk_blocks_dynamic_text_render_callback() {
'target_url' => get_permalink( $data['parent_page_id'] ),
'correct' => '<h3 class="vk_dynamicText wp-block-vk-blocks-dynamic-text">ancestor_page</h3>',
),
// 親ページのタイトル(先祖ページ有り)
// ユーザー名(Prefix/Suffixあり)%%USER_DISPLAY_NAME%% は該当するユーザー名に置き換えられます
array(
'attributes' => array(
'displayElement' => 'parent-page',
'tagName' => 'span',
'parentPageHiddenOption' => true,
'displayElement' => 'user-name',
'tagName' => 'div',
'userNamePrefixText' => 'PrefixText',
'userNameSuffixText' => 'SuffixText',
),
'user' => 'administrator',
'target_url' => get_permalink( $data['child_page_id'] ),
'correct' => '<div class="vk_dynamicText wp-block-vk-blocks-dynamic-text">PrefixText%%USER_DISPLAY_NAME%%SuffixText</div>',
),

// ユーザー名(Prefixのみ)
array(
'attributes' => array(
'displayElement' => 'user-name',
'tagName' => 'h2',
'userNamePrefixText' => 'PrefixText'
),
'user' => 'administrator',
'target_url' => get_permalink( $data['child_page_id'] ),
'correct' => '<h2 class="vk_dynamicText wp-block-vk-blocks-dynamic-text">PrefixText%%USER_DISPLAY_NAME%%</h2>',
),

// ユーザー名(Suffixのみ)
array(
'attributes' => array(
'displayElement' => 'user-name',
'tagName' => 'h3',
'userNameSuffixText' => 'SuffixText'
),
'user' => 'administrator',
'target_url' => get_permalink( $data['child_page_id'] ),
'correct' => '<h3 class="vk_dynamicText wp-block-vk-blocks-dynamic-text">%%USER_DISPLAY_NAME%%SuffixText</h3>',
),

// ユーザー名(ログアウト)
array(
'attributes' => array(
'displayElement' => 'user-name',
'tagName' => 'h3',
'userNameSuffixText' => 'SuffixText',
'userNameLoggedOutText' => 'Please login!'
),
'target_url' => get_permalink( $data['child_page_id'] ),
'correct' => '<span class="vk_dynamicText wp-block-vk-blocks-dynamic-text">parent_page</span>',
'correct' => '<h3 class="vk_dynamicText wp-block-vk-blocks-dynamic-text">Please login!</h3>',
),

// ユーザー名(ログアウト・リンクあり)
array(
'attributes' => array(
'displayElement' => 'user-name',
'tagName' => 'h3',
'userNameSuffixText' => 'SuffixText',
'userNameLoggedOutText' => 'Please login!',
'isLoginLink' => true
),
'target_url' => get_permalink( $data['child_page_id'] ),
'correct' => '<h3 class="vk_dynamicText wp-block-vk-blocks-dynamic-text"><a href="' . wp_login_url(get_permalink( $data['child_page_id'] )) . '">Please login!</a></h3>',
),

// カスタムフィールド - テキスト
Expand Down Expand Up @@ -196,10 +247,19 @@ public function test_vk_blocks_dynamic_text_render_callback() {
// vk_blocks_dynamic_text_render_callback() の引数として渡津 $block オブジェクトを作成する。
$block = new BlockObject( $data['post_id'] );

var_dump( get_permalink( $data['child_page_id'] ));
foreach ( $test_data as $value ) {

// Move to test page.
$this->go_to( $value['target_url'] );

if ( isset( $value['user'] ) ) {
// ユーザーを作成し、カレントユーザーにセット
$user = $this->set_current_user( $value['user'] );
$value['correct'] = str_replace('%%USER_DISPLAY_NAME%%', $user->display_name, $value['correct']);
} else {
wp_logout();
}

WP_Block_Supports::$block_to_render = array(
'blockName' => 'vk-blocks/dynamic-text',
'attrs' => $value['attributes'],
Expand Down

0 comments on commit 3e88272

Please sign in to comment.