Skip to content

Commit cd246f7

Browse files
authored
Merge pull request #7 from ModySolutions/create-site-to-regular-block
Create site to regular block
2 parents 3fffbef + 1181876 commit cd246f7

24 files changed

Lines changed: 158 additions & 140 deletions

File tree

app/Hooks/Sites.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,68 @@
66
use App\Hooks\Sites\Block;
77
use App\Hooks\Sites\Post;
88
use App\Hooks\Sites\Routes;
9+
use Roots\WPConfig\Config;
910

1011
class Sites
1112
{
13+
public const LAST_UPDATE = 'Dool|Tinky|Sonya|Simple';
14+
public const CREATE_SITE_PAGE_CONTENT = <<<EOF
15+
<!-- wp:group {"className":"wizard-form flex flex-column justify-center p-4 rounded radius-md bg-white","layout":{"type":"constrained"}} -->
16+
<div class="wp-block-group wizard-form flex flex-column justify-center p-4 rounded radius-md bg-white"><!-- wp:image {"width":"200px","sizeSlug":"medium","linkDestination":"none","align":"center"} -->
17+
<figure class="wp-block-image aligncenter size-medium is-resized"><img src="https://modycloud.test/content/uploads/2024/12/logo-mody-cloud-300x148.png" alt="" class="wp-image-51" style="width:200px"/></figure>
18+
<!-- /wp:image -->
19+
20+
<!-- wp:app/create-site-v2 -->
21+
<p class="wp-block-app-create-site-v2">Auth Module for Mody Cloud</p>
22+
<!-- /wp:app/create-site-v2 --></div>
23+
<!-- /wp:group -->
24+
EOF;
25+
1226
public static function init(): void
1327
{
28+
add_action('init', self::wp_init(...));
1429
add_action('init', Routes::permalink_structure(...));
1530
add_action('init', Post::register_post_type(...));
1631
add_action('wp_install', Routes::migrate(...));
1732
add_action('wp_ajax_check_setup_finished', Ajax::check_setup_finished(...));
1833
add_action('wp_ajax_check_space_name_exists', Ajax::check_space_name_exists(...));
1934
add_action('wp_ajax_create_space', Ajax::create_space(...));
2035
add_filter('app_before_render_block_create-site', Block::app_before_render_block(...));
36+
add_filter('render_block', Block::app_render_block(...), 10, 3);
37+
}
38+
39+
public static function wp_init(): void
40+
{
41+
if (Config::get('CHILD_SITE')) {
42+
return;
43+
}
44+
$get_post_by_name = get_page_by_path('create-site');
45+
$create_site_page_id_option = get_option('create_site_page_id');
46+
if ($get_post_by_name?->ID && $create_site_page_id_option !== $get_post_by_name?->ID) {
47+
$create_site_page_id = $get_post_by_name?->ID;
48+
} else {
49+
$create_site_page_id = $create_site_page_id_option;
50+
}
51+
$create_site_option_last_update = get_option('create_site_option_last_update');
52+
if (! $create_site_page_id || $create_site_option_last_update !== self::LAST_UPDATE) {
53+
if (!$create_site_page_id) {
54+
$create_site_page_id = wp_insert_post([
55+
'post_type' => 'page',
56+
'post_title' => __('Create site', APP_THEME_LOCALE),
57+
'post_status' => 'publish',
58+
'post_author' => 1,
59+
'post_name' => 'create_site',
60+
'post_content' => self::CREATE_SITE_PAGE_CONTENT,
61+
]);
62+
}
63+
64+
wp_update_post([
65+
'ID' => $create_site_page_id,
66+
'post_content' => self::CREATE_SITE_PAGE_CONTENT,
67+
]);
68+
69+
update_option('create_site_option_last_update', self::LAST_UPDATE);
70+
update_option('create_site_page_id', $create_site_page_id);
71+
}
2172
}
2273
}

app/Hooks/Sites/Block.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,38 @@ public static function app_before_render_block(array $context): array
4646
}
4747
return $context;
4848
}
49+
50+
public static function app_render_block(string $block_content, array $block, \WP_Block $instance): string
51+
{
52+
if ($block['blockName'] != 'app/create-site-v2' || is_admin()) {
53+
return $block_content;
54+
}
55+
56+
if (!is_user_logged_in()) {
57+
wp_redirect(wp_login_url());
58+
exit;
59+
}
60+
61+
if (current_user_can('administrator')) {
62+
return $block_content;
63+
}
64+
65+
$current_user_id = get_current_user_id();
66+
$site_id = app_user_has_a_site($current_user_id);
67+
$site_uri = get_field('site_uri', $site_id);
68+
$site_is_active = $site_id && app_site_is_active($site_id);
69+
70+
if ($site_id && $site_is_active) {
71+
wp_redirect($site_uri);
72+
exit;
73+
} elseif ($site_id && !$site_is_active) {
74+
$space_install_setup = add_query_arg([
75+
'key' => base64_encode(md5(rand(11111, 99999))),
76+
'installing' => true,
77+
], "{$site_uri}/content/space-install-setup.php");
78+
wp_redirect($space_install_setup);
79+
exit;
80+
}
81+
return $block_content;
82+
}
4983
}

config/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = () => ( {
66
entry: {
77
app: path.resolve( process.cwd(), 'resources/scripts', 'app.js' ),
88
editor: path.resolve( process.cwd(), 'resources/scripts', 'editor.js' ),
9-
site: path.resolve( process.cwd(), 'resources/scripts', 'site.js' ),
109
},
1110
output: {
1211
filename: '[name].js',

resources/blocks/activity/block.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

resources/blocks/apps/block.json

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 3,
4+
"name": "app/create-site-v2",
5+
"version": "0.1.0",
6+
"title": "Create site V2",
7+
"category": "utilities",
8+
"description": "Create site module.",
9+
"example": {},
10+
"supports": {
11+
"html": false
12+
},
13+
"textdomain": "app",
14+
"editorScript": "file:./index.js",
15+
"editorStyle": "file:./index.css",
16+
"style": "file:./style-index.css",
17+
"viewScript": "file:./view.js",
18+
"render": "file:./render.php"
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { __ } from '@wordpress/i18n';
2+
import { useBlockProps } from '@wordpress/block-editor';
3+
import './editor.scss';
4+
export default function Edit() {
5+
return (
6+
<p { ...useBlockProps() }>
7+
{ __( 'Auth Module for Mody Cloud', 'app' ) }
8+
</p>
9+
);
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wp-block-auth {
2+
background-color: var(--color-white);
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { registerBlockType } from '@wordpress/blocks';
2+
import './style.scss';
3+
import Edit from './edit';
4+
import save from './save';
5+
import metadata from './block.json';
6+
7+
registerBlockType( metadata.name, {
8+
icon: (
9+
<svg
10+
xmlns="http://www.w3.org/2000/svg"
11+
height="20px"
12+
viewBox="0 -960 960 960"
13+
width="20px"
14+
fill="#666666"
15+
>
16+
<path d="M744-202v-86h-48v106l79 79 34-34-65-65ZM481-792 245-657l235 135 235-135-234-135ZM144-320v-320q0-19.77 9.45-35.94Q162.9-692.12 180-702l262.62-152.27Q452-859 460.82-862q8.82-3 19.04-3 10.21 0 19.5 3 9.28 3 17.64 8l263 152q17.1 9.88 26.55 26.06Q816-659.77 816-640v184h-72v-135L479-439 216-591v270l240 139v83L180-258q-17-9-26.5-25.45T144-320ZM719.77 0Q640 0 584-56.23q-56-56.22-56-136Q528-272 584.23-328q56.22-56 136-56Q800-384 856-327.77q56 56.22 56 136Q912-112 855.77-56q-56.22 56-136 56ZM480-487Z" />
17+
</svg>
18+
),
19+
edit: Edit,
20+
save,
21+
} );
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="app-create-site-container" class='app-create-site-container'></div>

0 commit comments

Comments
 (0)