diff --git a/app/Hooks/Sites.php b/app/Hooks/Sites.php index ca704b7..9c6b0a0 100644 --- a/app/Hooks/Sites.php +++ b/app/Hooks/Sites.php @@ -6,11 +6,26 @@ use App\Hooks\Sites\Block; use App\Hooks\Sites\Post; use App\Hooks\Sites\Routes; +use Roots\WPConfig\Config; class Sites { + public const LAST_UPDATE = 'Dool|Tinky|Sonya|Simple'; + public const CREATE_SITE_PAGE_CONTENT = << +
+
+ + + +

Auth Module for Mody Cloud

+
+ +EOF; + public static function init(): void { + add_action('init', self::wp_init(...)); add_action('init', Routes::permalink_structure(...)); add_action('init', Post::register_post_type(...)); add_action('wp_install', Routes::migrate(...)); @@ -18,5 +33,41 @@ public static function init(): void add_action('wp_ajax_check_space_name_exists', Ajax::check_space_name_exists(...)); add_action('wp_ajax_create_space', Ajax::create_space(...)); add_filter('app_before_render_block_create-site', Block::app_before_render_block(...)); + add_filter('render_block', Block::app_render_block(...), 10, 3); + } + + public static function wp_init(): void + { + if (Config::get('CHILD_SITE')) { + return; + } + $get_post_by_name = get_page_by_path('create-site'); + $create_site_page_id_option = get_option('create_site_page_id'); + if ($get_post_by_name?->ID && $create_site_page_id_option !== $get_post_by_name?->ID) { + $create_site_page_id = $get_post_by_name?->ID; + } else { + $create_site_page_id = $create_site_page_id_option; + } + $create_site_option_last_update = get_option('create_site_option_last_update'); + if (! $create_site_page_id || $create_site_option_last_update !== self::LAST_UPDATE) { + if (!$create_site_page_id) { + $create_site_page_id = wp_insert_post([ + 'post_type' => 'page', + 'post_title' => __('Create site', APP_THEME_LOCALE), + 'post_status' => 'publish', + 'post_author' => 1, + 'post_name' => 'create_site', + 'post_content' => self::CREATE_SITE_PAGE_CONTENT, + ]); + } + + wp_update_post([ + 'ID' => $create_site_page_id, + 'post_content' => self::CREATE_SITE_PAGE_CONTENT, + ]); + + update_option('create_site_option_last_update', self::LAST_UPDATE); + update_option('create_site_page_id', $create_site_page_id); + } } } diff --git a/app/Hooks/Sites/Block.php b/app/Hooks/Sites/Block.php index 1c0bef5..d2c729a 100644 --- a/app/Hooks/Sites/Block.php +++ b/app/Hooks/Sites/Block.php @@ -46,4 +46,38 @@ public static function app_before_render_block(array $context): array } return $context; } + + public static function app_render_block(string $block_content, array $block, \WP_Block $instance): string + { + if ($block['blockName'] != 'app/create-site-v2' || is_admin()) { + return $block_content; + } + + if (!is_user_logged_in()) { + wp_redirect(wp_login_url()); + exit; + } + + if (current_user_can('administrator')) { + return $block_content; + } + + $current_user_id = get_current_user_id(); + $site_id = app_user_has_a_site($current_user_id); + $site_uri = get_field('site_uri', $site_id); + $site_is_active = $site_id && app_site_is_active($site_id); + + if ($site_id && $site_is_active) { + wp_redirect($site_uri); + exit; + } elseif ($site_id && !$site_is_active) { + $space_install_setup = add_query_arg([ + 'key' => base64_encode(md5(rand(11111, 99999))), + 'installing' => true, + ], "{$site_uri}/content/space-install-setup.php"); + wp_redirect($space_install_setup); + exit; + } + return $block_content; + } } diff --git a/config/webpack.config.js b/config/webpack.config.js index 5d7a3e9..773ab86 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -6,7 +6,6 @@ module.exports = () => ( { entry: { app: path.resolve( process.cwd(), 'resources/scripts', 'app.js' ), editor: path.resolve( process.cwd(), 'resources/scripts', 'editor.js' ), - site: path.resolve( process.cwd(), 'resources/scripts', 'site.js' ), }, output: { filename: '[name].js', diff --git a/resources/blocks/activity/block.json b/resources/blocks/activity/block.json deleted file mode 100644 index dd2bcec..0000000 --- a/resources/blocks/activity/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/activity", - "title": "Activity", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["log", "activity", "history"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/blocks/apps/block.json b/resources/blocks/apps/block.json deleted file mode 100644 index ab9c03f..0000000 --- a/resources/blocks/apps/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/apps", - "title": "Apps", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["apps", "list", "plugins"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/blocks/create-site-v2/block.json b/resources/blocks/create-site-v2/block.json new file mode 100644 index 0000000..e92cb05 --- /dev/null +++ b/resources/blocks/create-site-v2/block.json @@ -0,0 +1,19 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "app/create-site-v2", + "version": "0.1.0", + "title": "Create site V2", + "category": "utilities", + "description": "Create site module.", + "example": {}, + "supports": { + "html": false + }, + "textdomain": "app", + "editorScript": "file:./index.js", + "editorStyle": "file:./index.css", + "style": "file:./style-index.css", + "viewScript": "file:./view.js", + "render": "file:./render.php" +} \ No newline at end of file diff --git a/resources/blocks/create-site-v2/edit.js b/resources/blocks/create-site-v2/edit.js new file mode 100644 index 0000000..7b0c43a --- /dev/null +++ b/resources/blocks/create-site-v2/edit.js @@ -0,0 +1,10 @@ +import { __ } from '@wordpress/i18n'; +import { useBlockProps } from '@wordpress/block-editor'; +import './editor.scss'; +export default function Edit() { + return ( +

+ { __( 'Auth Module for Mody Cloud', 'app' ) } +

+ ); +} diff --git a/resources/blocks/create-site-v2/editor.scss b/resources/blocks/create-site-v2/editor.scss new file mode 100644 index 0000000..6f8835a --- /dev/null +++ b/resources/blocks/create-site-v2/editor.scss @@ -0,0 +1,3 @@ +.wp-block-auth { + background-color: var(--color-white); +} diff --git a/resources/blocks/create-site-v2/index.js b/resources/blocks/create-site-v2/index.js new file mode 100644 index 0000000..73c3b77 --- /dev/null +++ b/resources/blocks/create-site-v2/index.js @@ -0,0 +1,21 @@ +import { registerBlockType } from '@wordpress/blocks'; +import './style.scss'; +import Edit from './edit'; +import save from './save'; +import metadata from './block.json'; + +registerBlockType( metadata.name, { + icon: ( + + + + ), + edit: Edit, + save, +} ); diff --git a/resources/blocks/create-site-v2/render.php b/resources/blocks/create-site-v2/render.php new file mode 100644 index 0000000..0f406fa --- /dev/null +++ b/resources/blocks/create-site-v2/render.php @@ -0,0 +1 @@ +
\ No newline at end of file diff --git a/resources/blocks/create-site-v2/save.js b/resources/blocks/create-site-v2/save.js new file mode 100644 index 0000000..80fbbb0 --- /dev/null +++ b/resources/blocks/create-site-v2/save.js @@ -0,0 +1,4 @@ +import { useBlockProps } from '@wordpress/block-editor'; +export default function save() { + return

{ 'Auth Module for Mody Cloud' }

; +} diff --git a/resources/scripts/site/components/CreateSiteForm.jsx b/resources/blocks/create-site-v2/src/components/CreateSiteForm.jsx similarity index 91% rename from resources/scripts/site/components/CreateSiteForm.jsx rename to resources/blocks/create-site-v2/src/components/CreateSiteForm.jsx index 8b7308a..be97bf9 100644 --- a/resources/scripts/site/components/CreateSiteForm.jsx +++ b/resources/blocks/create-site-v2/src/components/CreateSiteForm.jsx @@ -1,7 +1,8 @@ +// eslint-disable-next-line import/no-unresolved import { useState } from 'react'; -import { __, sprintf } from '@wordpress/i18n'; +import { __ } from '@wordpress/i18n'; import { toast } from 'react-toastify'; -import toKebabCase from '@modycloud/tools/kebabcase'; +import toKebabCase from '../../../../scripts/tools/kebabcase'; const CreateSiteForm = () => { const [ companyName, setCompanyName ] = useState( '' ); @@ -10,7 +11,6 @@ const CreateSiteForm = () => { const [ loadingMessage, setLoadingMessage ] = useState( 'We are creating your space, please wait...' ); - const [ counter, setCounter ] = useState( 0 ); const maskSpaceNameInput = ( event ) => { setSpaceName( toKebabCase( event.target.value ).substring( 0, 24 ) ); @@ -32,10 +32,10 @@ const CreateSiteForm = () => { __( "Oh! You're still here? Man what am I doing…", 'app' ), ]; - const checkInstallFinished = async ( queue_ui ) => { + const checkInstallFinished = async ( queueUi ) => { const data = new URLSearchParams( { action: 'check_setup_finished', - queue_id: queue_ui, + queue_id: queueUi, } ); try { @@ -82,18 +82,13 @@ const CreateSiteForm = () => { toast.error( __( 'Error creating site.' ), { autoClose: 3000, } ); - console.error( - sprintf( - __( 'Error creating site. Code: %s', 'app' ), - response.statusText - ) - ); setIsCreating( false ); } const { success, - data: { initial_page, message, queue_id }, + // eslint-disable-next-line camelcase + data: { message, queue_id }, } = await response.json(); if ( success ) { @@ -117,6 +112,7 @@ const CreateSiteForm = () => { isLoading: true, } ); const { + // eslint-disable-next-line camelcase data: { done, initial_page }, } = await checkInstallFinished( queue_id ); @@ -130,6 +126,7 @@ const CreateSiteForm = () => { isLoading: false, } ); setTimeout( () => { + // eslint-disable-next-line camelcase location.href = initial_page; }, 3000 ); } @@ -158,6 +155,7 @@ const CreateSiteForm = () => { ) } * + { /* eslint-disable jsx-a11y/tabindex-no-positive */ } { return ( diff --git a/resources/blocks/create-site/block.json b/resources/blocks/create-site/block.json deleted file mode 100644 index 16c4c3c..0000000 --- a/resources/blocks/create-site/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/create-site", - "title": "Create site", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["setup", "wizard", "first", "time", "create", "site"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/blocks/dashboard/block.json b/resources/blocks/dashboard/block.json deleted file mode 100644 index 0484e02..0000000 --- a/resources/blocks/dashboard/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/dashboard", - "title": "Dashboard", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["dashboard", "resume", "graphics", "charts"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/blocks/settings/block.json b/resources/blocks/settings/block.json deleted file mode 100644 index 6693714..0000000 --- a/resources/blocks/settings/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/settings", - "title": "Settings", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["settings", "configuration", "gear", "admin"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/blocks/support/block.json b/resources/blocks/support/block.json deleted file mode 100644 index 7a63305..0000000 --- a/resources/blocks/support/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/support", - "title": "Support", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["support", "help"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/blocks/users/block.json b/resources/blocks/users/block.json deleted file mode 100644 index c6eead6..0000000 --- a/resources/blocks/users/block.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "app/users", - "title": "Users", - "description": "", - "category": "utilities", - "icon": "", - "keywords": ["users", "list", "grid", "manage"], - "acf": { - "mode": "edit", - "post_types": ["page"], - "renderCallback": "app_render" - }, - "supports": { - "anchor": true - } -} diff --git a/resources/views/blocks/account.twig b/resources/views/blocks/account.twig deleted file mode 100644 index 85f073c..0000000 --- a/resources/views/blocks/account.twig +++ /dev/null @@ -1 +0,0 @@ -
\ No newline at end of file diff --git a/resources/views/blocks/auth-v2.twig b/resources/views/blocks/auth-v2.twig deleted file mode 100644 index 01e224f..0000000 --- a/resources/views/blocks/auth-v2.twig +++ /dev/null @@ -1,6 +0,0 @@ -if(typeof AuthData === 'undefined') { - var AuthData = {Settings: {}} -} -AuthData.Settings.Email = '{{ email }}'; -AuthData.Settings.Key = '{{ key ?? '' }}'; -AuthData.Settings.FirstTime = '{{ first_time ?? '' }}'; \ No newline at end of file diff --git a/resources/views/blocks/auth.twig b/resources/views/blocks/auth.twig deleted file mode 100644 index 6ffade7..0000000 --- a/resources/views/blocks/auth.twig +++ /dev/null @@ -1,2 +0,0 @@ -{% include '@app/common/logos/logo.twig' with { 'link': site_url } %} -
\ No newline at end of file diff --git a/resources/views/blocks/create-site.twig b/resources/views/blocks/create-site.twig deleted file mode 100644 index 8f175e1..0000000 --- a/resources/views/blocks/create-site.twig +++ /dev/null @@ -1,4 +0,0 @@ -
- {% include '@app/common/logos/logo.twig' with { link: site_url, max_width: '300px' } %} -
-
\ No newline at end of file