Skip to content

Commit e6c2f71

Browse files
committed
feat: 新增單例模式,重新調整檔案結構
1 parent 98da45f commit e6c2f71

File tree

12 files changed

+133
-90
lines changed

12 files changed

+133
-90
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"usefulteam/jwt-auth": "^2.1",
2828
"lodash-php/lodash-php": "^0.0.7",
2929
"yahnis-elsts/plugin-update-checker": "^5.3",
30-
"j7-dev/tgm-plugin-activation-forked": "^1.0"
30+
"j7-dev/tgm-plugin-activation-forked": "^1.0",
31+
"micropackage/singleton": "^1.1"
3132
},
3233
"require-dev": {
3334
"squizlabs/php_codesniffer": "@stable",

composer.lock

Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
namespace J7\WpReactPlugin\Admin;
99

10-
use J7\WpReactPlugin\Utils;
10+
use Micropackage\Singleton\Singleton;
11+
use J7\WpReactPlugin\Utils\Base;
1112
use J7\WpReactPlugin\Plugin;
1213

1314
/**
1415
* Class CPT
1516
*/
16-
final class CPT {
17+
final class CPT extends Singleton {
1718

1819
/**
1920
* Post metas
@@ -184,7 +185,7 @@ public function add_metabox( string $post_type ): void {
184185
*/
185186
public function render_meta_box(): void {
186187
// phpcs:ignore
187-
echo '<div id="' . Utils::APP2_SELECTOR . '"></div>';
188+
echo '<div id="' . Base::APP2_SELECTOR . '"></div>';
188189
}
189190

190191

@@ -277,7 +278,7 @@ public function load_custom_template( $template ) {
277278
}
278279
}
279280

280-
new CPT(
281+
CPT::get(
281282
array(
282283
'post_metas' => array( 'meta', 'settings' ),
283284
'rewrite' => array(

inc/class/admin/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?php // phpcs:ignore
2-
require_once __DIR__ . '/cpt/index.php';
2+
require_once __DIR__ . '/class-cpt.php';

inc/class/class-bootstrap.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77

88
namespace J7\WpReactPlugin;
99

10+
use Micropackage\Singleton\Singleton;
11+
use J7\WpReactPlugin\Utils\Base;
1012
use Kucrut\Vite;
1113

1214
/**
1315
* Class Bootstrap
1416
*/
15-
final class Bootstrap {
17+
final class Bootstrap extends Singleton {
1618

1719

1820
/**
1921
* Constructor
2022
*/
2123
public function __construct() {
24+
require_once __DIR__ . '/utils/index.php';
2225
require_once __DIR__ . '/admin/index.php';
2326
require_once __DIR__ . '/front-end/index.php';
2427

@@ -82,10 +85,10 @@ public function enqueue_script(): void {
8285
'APP_NAME' => Plugin::APP_NAME,
8386
'KEBAB' => Plugin::KEBAB,
8487
'SNAKE' => Plugin::SNAKE,
85-
'BASE_URL' => Utils::BASE_URL,
86-
'APP1_SELECTOR' => '#' . Utils::APP1_SELECTOR,
87-
'APP2_SELECTOR' => '#' . Utils::APP2_SELECTOR,
88-
'API_TIMEOUT' => Utils::API_TIMEOUT,
88+
'BASE_URL' => Base::BASE_URL,
89+
'APP1_SELECTOR' => '#' . Base::APP1_SELECTOR,
90+
'APP2_SELECTOR' => '#' . Base::APP2_SELECTOR,
91+
'API_TIMEOUT' => Base::API_TIMEOUT,
8992
'nonce' => \wp_create_nonce( Plugin::KEBAB ),
9093
),
9194
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Front-end Entry
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace J7\WpReactPlugin\FrontEnd;
9+
10+
use Micropackage\Singleton\Singleton;
11+
use J7\WpReactPlugin\Utils\Base;
12+
/**
13+
* Class Entry
14+
*/
15+
final class Entry extends Singleton {
16+
17+
/**
18+
* Constructor
19+
*/
20+
public function __construct() {
21+
\add_action( 'wp_footer', array( $this, 'render_app' ) );
22+
}
23+
24+
/**
25+
* Render application's markup
26+
*/
27+
public function render_app(): void {
28+
// phpcs:ignore
29+
echo '<div id="' . Base::APP1_SELECTOR . '"></div>';
30+
}
31+
}
32+
33+
Entry::get();

inc/class/front-end/index.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,2 @@
1-
<?php
2-
/**
3-
* Front-end
4-
*/
5-
6-
declare(strict_types=1);
7-
8-
namespace J7\WpReactPlugin;
9-
10-
/**
11-
* Class FrontEnd
12-
*/
13-
final class FrontEnd {
14-
15-
/**
16-
* Constructor
17-
*/
18-
public function __construct() {
19-
\add_action( 'wp_footer', array( $this, 'render_app' ) );
20-
}
21-
22-
/**
23-
* Render application's markup
24-
*/
25-
public function render_app(): void {
26-
// phpcs:ignore
27-
echo '<div id="' . Utils::APP1_SELECTOR . '"></div>';
28-
}
29-
}
30-
31-
new FrontEnd();
1+
<?php // phpcs:ignore
2+
require_once __DIR__ . '/class-entry.php';

inc/class/utils/class-base.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Base
4+
*/
5+
6+
declare (strict_types = 1);
7+
8+
namespace J7\WpReactPlugin\Utils;
9+
10+
/**
11+
* Class Base
12+
*/
13+
abstract class Base {
14+
const BASE_URL = '/';
15+
const APP1_SELECTOR = 'my_app';
16+
const APP2_SELECTOR = 'my_app_metabox';
17+
const API_TIMEOUT = '30000';
18+
const DEFAULT_IMAGE = 'http://1.gravatar.com/avatar/1c39955b5fe5ae1bf51a77642f052848?s=96&d=mm&r=g';
19+
20+
/**
21+
* Is HPOS enabled
22+
*
23+
* @return bool
24+
*/
25+
public static function is_hpos_enabled(): bool {
26+
return class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
27+
}
28+
}

inc/class/utils/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php // phpcs:ignore
2+
require_once __DIR__ . '/class-base.php';

inc/utils/class-utils.php

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

0 commit comments

Comments
 (0)