Skip to content

Commit

Permalink
Add block for display register, login links.
Browse files Browse the repository at this point in the history
  • Loading branch information
ve3 committed Nov 23, 2023
1 parent 9533521 commit 79197b2
Show file tree
Hide file tree
Showing 20 changed files with 18,894 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .dev-notes/dev-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
##Blocks

* Run `npm run start` to start editing blocks.
* Run `npm run build` to build the blocks.

10 changes: 7 additions & 3 deletions .dev-notes/how-to-create-pot.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Use this command:
`wp i18n make-pot . --exclude="assets,languages,*.css,*.js,*.svg,*.eot,*.woff,*.woff2,*.ttf,*.html,*.txt,*.pot"`
And check the result.
Use this command:
`wp i18n make-pot . --exclude="assets,block-src,languages,*.css,*.js,*.svg,*.eot,*.woff,*.woff2,*.ttf,*.html,*.txt,*.pot"`
And check the result.

Convert to JSON to make block i18n supported with this command:
`wp i18n make-json languages/okv-oauth-th.po --no-purge --pretty-print`
and rename **${domain}-${locale}-${md5}.json** to **${domain}-${locale}-${handle}.json** where `${handle}` is from `wp_set_script_translations()` function in PHP.
43 changes: 43 additions & 0 deletions App/Controllers/Blocks/LoginLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Login links block.
*
* @package rundiz-oauth
*/


namespace RundizOauth\App\Controllers\Blocks;


if (!class_exists('RundizOauth\App\Controllers\Blocks\\LoginLinks')) {
class LoginLinks implements \RundizOauth\App\Controllers\ControllerInterface
{


/**
* Register block.
*/
public function registerBlock()
{
register_block_type(
dirname(RUNDIZOAUTH_FILE) . DIRECTORY_SEPARATOR . 'blocks' . DIRECTORY_SEPARATOR . 'loginlinks'
);

if (function_exists('wp_set_script_translations')) {
wp_set_script_translations('rd-oauth-loginlinks-block-script', 'okv-oauth', str_replace([DIRECTORY_SEPARATOR], '/', plugin_dir_path(RUNDIZOAUTH_FILE)) . 'languages');
}
}// registerBlock


/**
* {@inheritDoc}
*/
public function registerHooks()
{
add_action('init', [$this, 'registerBlock']);
}// registerHooks


}
}

44 changes: 44 additions & 0 deletions blocks-src/loginlinks/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { registerBlockType } from '@wordpress/blocks';
import {
useBlockProps,
InspectorControls
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import ServerSideRender from '@wordpress/server-side-render';
import { ToggleControl } from '@wordpress/components';

import metadata from './block.json';

// import scss to make script build compile to css otherwise it won't work.
import './style.scss';

registerBlockType( metadata.name, {
edit: ({attributes, setAttributes}) => {
const {
displayLinkToAdmin
} = 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}"
/>
<InspectorControls key="setting">
<div id="rd-oauth-loginlinks-controls" class="components-panel__body is-opened">
<fieldset>
<ToggleControl
label={__('Display link to admin dashboard', 'okv-oauth')}
checked={ displayLinkToAdmin }
onChange={ () => setAttributes({
displayLinkToAdmin: !displayLinkToAdmin
}) }
/>
</fieldset>
</div>
</InspectorControls>
</p>;
},
} );
22 changes: 22 additions & 0 deletions blocks-src/loginlinks/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"attributes": {
"displayLinkToAdmin": {
"type": "boolean",
"default": false
}
},
"category": "theme",
"description": "Display login, register, logout links.",
"editorScript": "file:./block.js",
"icon": "admin-users",
"name": "rd-oauth/blocks-loginlinks",
"render": "file:./render.php",
"style": ["file:./style-block.css", "rd-oauth-loginlinks-block-style"],
"supports": {
"className": true
},
"textdomain": "okv-oauth",
"title": "Login links"
}
30 changes: 30 additions & 0 deletions blocks-src/loginlinks/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Render contents for loginlinks block.
*
* @package rundiz-oauth
*/


$currentUrl = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$isUserLoggedIn = is_user_logged_in();
$classes = ($isUserLoggedIn ? 'logged-in' : 'logged-out');
$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) && $attributes['displayLinkToAdmin'] === true) {
$contents .= '<li><a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a></li>' . PHP_EOL;
}
unset($isUserLoggedIn);

$contents .= '<li>' . wp_loginout($currentUrl, false) . '</li>' . PHP_EOL;
unset($currentUrl);

$wrapperAttributes = get_block_wrapper_attributes(['class' => $classes]);

unset($classes);

// must use echo, not return.
echo '<ul ' . $wrapperAttributes . '>' . $contents . '</ul>';
// that's all. end of file.
15 changes: 15 additions & 0 deletions blocks-src/loginlinks/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
@package rundiz-oauth
*/


.wp-block-rd-oauth-blocks-loginlinks {
list-style: disc;
margin: 0;
padding-left: 20px;

li {
margin: 0;
padding: 0;
}
}
1 change: 1 addition & 0 deletions blocks/loginlinks/block.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-server-side-render'), 'version' => 'ff70017631adf4144639');
1 change: 1 addition & 0 deletions blocks/loginlinks/block.js

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

25 changes: 25 additions & 0 deletions blocks/loginlinks/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"attributes": {
"displayLinkToAdmin": {
"type": "boolean",
"default": false
}
},
"category": "theme",
"description": "Display login, register, logout links.",
"editorScript": "file:./block.js",
"icon": "admin-users",
"name": "rd-oauth/blocks-loginlinks",
"render": "file:./render.php",
"style": [
"file:./style-block.css",
"rd-oauth-loginlinks-block-style"
],
"supports": {
"className": true
},
"textdomain": "okv-oauth",
"title": "Login links"
}
30 changes: 30 additions & 0 deletions blocks/loginlinks/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Render contents for loginlinks block.
*
* @package rundiz-oauth
*/


$currentUrl = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$isUserLoggedIn = is_user_logged_in();
$classes = ($isUserLoggedIn ? 'logged-in' : 'logged-out');
$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) && $attributes['displayLinkToAdmin'] === true) {
$contents .= '<li><a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a></li>' . PHP_EOL;
}
unset($isUserLoggedIn);

$contents .= '<li>' . wp_loginout($currentUrl, false) . '</li>' . PHP_EOL;
unset($currentUrl);

$wrapperAttributes = get_block_wrapper_attributes(['class' => $classes]);

unset($classes);

// must use echo, not return.
echo '<ul ' . $wrapperAttributes . '>' . $contents . '</ul>';
// that's all. end of file.
1 change: 1 addition & 0 deletions blocks/loginlinks/style-block.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.wp-block-rd-oauth-blocks-loginlinks{list-style:disc;margin:0;padding-left:20px}.wp-block-rd-oauth-blocks-loginlinks li{margin:0;padding:0}
18 changes: 18 additions & 0 deletions languages/okv-oauth-th-rd-oauth-loginlinks-block-script.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"translation-revision-date": "2023-11-24 03:13+0700",
"generator": "WP-CLI\/2.8.0-alpha-9dfa966",
"source": "blocks\/loginlinks\/block.js",
"domain": "okv-oauth",
"locale_data": {
"messages": {
"": {
"domain": "okv-oauth",
"lang": "th",
"plural-forms": "nplurals=1; plural=0;"
},
"Display link to admin dashboard": [
"\u0e41\u0e2a\u0e14\u0e07\u0e25\u0e34\u0e49\u0e07\u0e04\u0e4c\u0e44\u0e1b\u0e22\u0e31\u0e07\u0e2b\u0e19\u0e49\u0e32\u0e1c\u0e39\u0e49\u0e14\u0e39\u0e41\u0e25"
]
}
}
}
Binary file modified languages/okv-oauth-th.mo
Binary file not shown.
35 changes: 19 additions & 16 deletions languages/okv-oauth-th.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ msgid ""
msgstr ""
"Project-Id-Version: okv-oauth 1.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/okv-oauth\n"
"POT-Creation-Date: 2023-11-23T12:38:32+07:00\n"
"PO-Revision-Date: 2023-11-23 12:39+0700\n"
"POT-Creation-Date: 2023-11-24T03:13:17+07:00\n"
"PO-Revision-Date: 2023-11-24 03:13+0700\n"
"Last-Translator: Vee W.\n"
"Language-Team: <>\n"
"Language: th\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: Poedit 3.3.2\n"
"X-Generator: Poedit 3.4.1\n"
"X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e;__ngettext;_n;__ngettext_noop;_n_noop;_x;_nx;"
"_nx_noop;_ex;esc_attr__;esc_attr_e;esc_attr_x;esc_html__;esc_html_e;esc_html_x;_e;_nc\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-Basepath: ..\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-SearchPath-1: ..\n"

#. Plugin Name of the plugin
#: App/Controllers/Admin/Settings.php:33 App/Controllers/Front/RdOauth/Index.php:73
Expand Down Expand Up @@ -226,11 +225,11 @@ msgstr "เปิดใช้งาน"

#: App/config/settings_rdoauth.php:205
msgid "Client ID"
msgstr ""
msgstr "Client ID"

#: App/config/settings_rdoauth.php:212
msgid "Client secret"
msgstr ""
msgstr "Client secret"

#: App/config/settings_rdoauth.php:216
msgid "Auth parameters"
Expand Down Expand Up @@ -276,11 +275,11 @@ msgstr "เปิดใช้งานเข้าสู่ระบบด้ว

#: App/config/settings_rdoauth.php:275
msgid "App ID"
msgstr ""
msgstr "App ID"

#: App/config/settings_rdoauth.php:282
msgid "App secret"
msgstr ""
msgstr "App secret"

#: App/config/settings_rdoauth.php:293
msgid "Design pages"
Expand Down Expand Up @@ -431,12 +430,16 @@ msgstr "เข้าสู่ระบบ"
msgid "Register"
msgstr "ลงทะเบียน"

#~ msgid "Donate"
#~ msgstr "บริจาค"
#: blocks/loginlinks/block.js:1
msgid "Display link to admin dashboard"
msgstr "แสดงลิ้งค์ไปยังหน้าผู้ดูแล"

#~ msgid "File was not found!"
#~ msgstr "ไม่พบไฟล์!"
#: blocks/loginlinks/block.json
msgctxt "block description"
msgid "Display login, register, logout links."
msgstr "แสดงลิ้งค์เข้าสู่ระบบ, ลงทะเบียน, ออกจากระบบ."

#, php-format
#~ msgid "From the menu, go to APIs &amp; services &gt; Library and enable %1$sGoogle+ API%2$s."
#~ msgstr "จากเมนู, ไปที่ APIs &amp; services &gt; Library และเปิดใช้งาน %1$sGoogle+ API%2$s."
#: blocks/loginlinks/block.json
msgctxt "block title"
msgid "Login links"
msgstr "ลิ้งค์เข้าสู่ระบบ"
16 changes: 15 additions & 1 deletion languages/okv-oauth.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2023-11-23T12:38:32+07:00\n"
"POT-Creation-Date: 2023-11-24T03:13:17+07:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.8.0-alpha-9dfa966\n"
"X-Domain: okv-oauth\n"
Expand Down Expand Up @@ -416,3 +416,17 @@ msgstr ""
#: templates/okv-oauth/partials/registerForm_v.php:34
msgid "Register"
msgstr ""

#: blocks/loginlinks/block.js:1
msgid "Display link to admin dashboard"
msgstr ""

#: blocks/loginlinks/block.json
msgctxt "block description"
msgid "Display login, register, logout links."
msgstr ""

#: blocks/loginlinks/block.json
msgctxt "block title"
msgid "Login links"
msgstr ""
6 changes: 5 additions & 1 deletion node_tasks/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"patterns": "assets/**",
"destination": "assets"
},
{
"patterns": "blocks/**",
"destination": "blocks"
},
{
"patterns": "languages/**",
"destination": "languages"
Expand Down Expand Up @@ -108,8 +112,8 @@
".phpdoc",
".phpunit*",
"assets-src",
"blocks-src",
"composer.lock",
"gulpfile.js",
"node_modules",
"node_tasks",
"package*.json",
Expand Down
Loading

0 comments on commit 79197b2

Please sign in to comment.