Skip to content

Commit

Permalink
Add display link to edit profile option into block.
Browse files Browse the repository at this point in the history
  • Loading branch information
ve3 committed Nov 30, 2023
1 parent c703b91 commit 5d302d4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
16 changes: 12 additions & 4 deletions blocks-src/loginlinks/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import './style.scss';
registerBlockType( metadata.name, {
edit: ({attributes, setAttributes}) => {
const {
displayLinkToAdmin
displayLinkToAdmin,
displayLinkToEditProfile
} = attributes;

console.log(__('Display link to admin dashboard', 'okv-oauth'));
return <p {...useBlockProps()}>
<ServerSideRender
block="rd-oauth/blocks-loginlinks"
attributes={ attributes.attributes }
label="hello label"
checked="{displayLinkToAdmin}"
attributes={ attributes }
/>
<InspectorControls key="setting">
<div id="rd-oauth-loginlinks-controls" class="components-panel__body is-opened">
Expand All @@ -37,6 +36,15 @@ registerBlockType( metadata.name, {
}) }
/>
</fieldset>
<fieldset>
<ToggleControl
label={__('Display link to edit profile', 'okv-oauth')}
checked={ displayLinkToEditProfile }
onChange={ () => setAttributes({
displayLinkToEditProfile: !displayLinkToEditProfile
}) }
/>
</fieldset>
</div>
</InspectorControls>
</p>;
Expand Down
4 changes: 4 additions & 0 deletions blocks-src/loginlinks/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"displayLinkToAdmin": {
"type": "boolean",
"default": false
},
"displayLinkToEditProfile": {
"type": "boolean",
"default": false
}
},
"category": "theme",
Expand Down
13 changes: 11 additions & 2 deletions blocks-src/loginlinks/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@
$contents = '';

if (!$isUserLoggedIn && get_option('users_can_register')) {
// if user is not logged in and has option can register.
$contents .= '<li><a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a></li>' . PHP_EOL;
} elseif ($isUserLoggedIn && isset($attributes) && is_array($attributes) && array_key_exists('displayLinkToAdmin', $attributes) && true === $attributes['displayLinkToAdmin']) {
$contents .= '<li><a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a></li>' . PHP_EOL;
} elseif ($isUserLoggedIn) {
// if user logged in.
if (isset($attributes) && is_array($attributes) && array_key_exists('displayLinkToAdmin', $attributes) && true === $attributes['displayLinkToAdmin']) {
// if block was set to display link to admin dashboard.
$contents .= '<li><a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a></li>' . PHP_EOL;
}
if (isset($attributes) && is_array($attributes) && array_key_exists('displayLinkToEditProfile', $attributes) && true === $attributes['displayLinkToEditProfile']) {
// if block was set to display link to edit profile.
$contents .= '<li><a href="' . get_edit_user_link() . '">' . __('Edit Profile') . '</a></li>' . PHP_EOL;
}
}
unset($isUserLoggedIn);

Expand Down
2 changes: 1 addition & 1 deletion blocks/loginlinks/block.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-server-side-render'), 'version' => 'ff70017631adf4144639');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-server-side-render'), 'version' => 'e3ac22a67f944046c3c4');
2 changes: 1 addition & 1 deletion blocks/loginlinks/block.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions blocks/loginlinks/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"displayLinkToAdmin": {
"type": "boolean",
"default": false
},
"displayLinkToEditProfile": {
"type": "boolean",
"default": false
}
},
"category": "theme",
Expand Down
15 changes: 12 additions & 3 deletions blocks/loginlinks/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@
$contents = '';

if (!$isUserLoggedIn && get_option('users_can_register')) {
$contents .= '<li><a href="' . esc_url(wp_registration_url()) . '">' . __('Register') . '</a></li>' . PHP_EOL;
} elseif ($isUserLoggedIn && isset($attributes) && is_array($attributes) && array_key_exists('displayLinkToAdmin', $attributes) && true === $attributes['displayLinkToAdmin']) {
$contents .= '<li><a href="' . admin_url() . '">' . __('Site Admin') . '</a></li>' . PHP_EOL;
// if user is not logged in and has option can register.
$contents .= '<li><a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a></li>' . PHP_EOL;
} elseif ($isUserLoggedIn) {
// if user logged in.
if (isset($attributes) && is_array($attributes) && array_key_exists('displayLinkToAdmin', $attributes) && true === $attributes['displayLinkToAdmin']) {
// if block was set to display link to admin dashboard.
$contents .= '<li><a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a></li>' . PHP_EOL;
}
if (isset($attributes) && is_array($attributes) && array_key_exists('displayLinkToEditProfile', $attributes) && true === $attributes['displayLinkToEditProfile']) {
// if block was set to display link to edit profile.
$contents .= '<li><a href="' . get_edit_user_link() . '">' . __('Edit Profile') . '</a></li>' . PHP_EOL;
}
}
unset($isUserLoggedIn);

Expand Down

0 comments on commit 5d302d4

Please sign in to comment.