Skip to content

Commit 56cadb6

Browse files
committed
chore: 移除單例
1 parent af2f357 commit 56cadb6

File tree

4 files changed

+37
-61
lines changed

4 files changed

+37
-61
lines changed

inc/classes/Admin/CPT.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
<?php
2-
/**
3-
* Custom Post Type: My Refine App
4-
*/
52

63
declare(strict_types=1);
74

85
namespace J7\WpRefinePlugin\Admin;
96

107
use J7\WpRefinePlugin\Utils\Base;
11-
use J7\WpRefinePlugin\Plugin;
128

139
if (class_exists('J7\WpRefinePlugin\Admin\CPT')) {
1410
return;
1511
}
1612
/** Class CPT */
1713
final class CPT {
18-
use \J7\WpUtils\Traits\SingletonTrait;
1914

2015
const POST_TYPE = 'my-refine-app';
2116

22-
/** Constructor */
23-
public function __construct() {
24-
\add_action( 'init', [ $this, 'register_cpt' ] );
25-
\add_action( 'load-post.php', [ $this, 'add_metabox' ] );
26-
\add_action( 'load-post-new.php', [ $this, 'add_metabox' ] );
17+
/** Register hooks */
18+
public static function register_hooks() {
19+
\add_action( 'init', [ __CLASS__, 'register_cpt' ] );
20+
\add_action( 'load-post.php', [ __CLASS__, 'add_metabox' ] );
21+
\add_action( 'load-post-new.php', [ __CLASS__, 'add_metabox' ] );
2722
}
2823

2924
/**
@@ -102,13 +97,13 @@ public static function register_cpt(): void {
10297
*
10398
* @param string $post_type Post type.
10499
*/
105-
public function add_metabox( string $post_type ): void {
100+
public static function add_metabox( string $post_type ): void {
106101
$post_type = $post_type ?: $_GET['post_type']; // phpcs:ignore
107102
if ( in_array( $post_type, [ self::POST_TYPE ] ) ) {
108103
\add_meta_box(
109104
self::POST_TYPE . '-metabox',
110105
__( 'My Refine App', 'wp_refine_plugin' ),
111-
[ $this, 'render_meta_box' ],
106+
[ __CLASS__, 'render_meta_box' ],
112107
self::POST_TYPE,
113108
'advanced',
114109
'high'
@@ -119,7 +114,7 @@ public function add_metabox( string $post_type ): void {
119114
/**
120115
* Render meta box.
121116
*/
122-
public function render_meta_box(): void {
117+
public static function render_meta_box(): void {
123118
// phpcs:ignore
124119
echo '<div id="' . substr(Base::APP2_SELECTOR, 1) . '" class="relative"></div>';
125120
}

inc/classes/Bootstrap.php

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
/**
3-
* Bootstrap
4-
*/
52

63
declare (strict_types = 1);
74

@@ -15,12 +12,11 @@
1512
}
1613
/** Class Bootstrap */
1714
final class Bootstrap {
18-
use \J7\WpUtils\Traits\SingletonTrait;
1915

20-
/** Constructor */
21-
public function __construct() {
22-
FrontEnd\Entry::instance();
23-
Admin\CPT::instance();
16+
/** Register hooks */
17+
public static function register_hooks() {
18+
FrontEnd\Entry::register_hooks();
19+
Admin\CPT::register_hooks();
2420

2521
\add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue_script' ] );
2622
\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'frontend_enqueue_script' ]);
@@ -38,7 +34,6 @@ public static function admin_enqueue_script( $hook ): void {
3834
self::enqueue_script();
3935
}
4036

41-
4237
/**
4338
* Front-end Enqueue script
4439
* You can load the script on demand
@@ -49,6 +44,7 @@ public static function frontend_enqueue_script(): void {
4944
self::enqueue_script();
5045
}
5146

47+
5248
/**
5349
* Enqueue script
5450
* You can load the script on demand
@@ -69,34 +65,29 @@ public static function enqueue_script(): void {
6965
$post_id = \get_the_ID();
7066
$permalink = $post_id ? \get_permalink( $post_id ) : '';
7167

72-
\wp_localize_script(
73-
Plugin::$kebab,
74-
Plugin::$snake . '_data',
75-
[
76-
'env' => [
77-
'siteUrl' => \untrailingslashit( \site_url() ),
78-
'ajaxUrl' => \untrailingslashit( \admin_url( 'admin-ajax.php' ) ),
79-
'userId' => \wp_get_current_user()->data->ID ?? null,
80-
'postId' => $post_id,
81-
'permalink' => \untrailingslashit( $permalink ),
82-
'APP_NAME' => Plugin::$app_name,
83-
'KEBAB' => Plugin::$kebab,
84-
'SNAKE' => Plugin::$snake,
85-
'BASE_URL' => Base::BASE_URL,
86-
'APP1_SELECTOR' => Base::APP1_SELECTOR,
87-
'APP2_SELECTOR' => Base::APP2_SELECTOR,
88-
'API_TIMEOUT' => Base::API_TIMEOUT,
89-
'nonce' => \wp_create_nonce( Plugin::$kebab ),
90-
],
91-
]
92-
);
68+
/** @var array<string> $active_plugins */
69+
$active_plugins = \get_option( 'active_plugins', [] );
70+
71+
$env = [
72+
'SITE_URL' => \untrailingslashit( \site_url() ),
73+
'API_URL' => \untrailingslashit( \esc_url_raw( rest_url() ) ),
74+
'CURRENT_USER_ID' => \get_current_user_id(),
75+
'CURRENT_POST_ID' => $post_id,
76+
'PERMALINK' => \untrailingslashit( $permalink ),
77+
'APP_NAME' => Plugin::$app_name,
78+
'KEBAB' => Plugin::$kebab,
79+
'SNAKE' => Plugin::$snake,
80+
'NONCE' => \wp_create_nonce( 'wp_rest' ),
81+
'APP1_SELECTOR' => Base::APP1_SELECTOR,
82+
'APP2_SELECTOR' => Base::APP2_SELECTOR,
83+
'ELEMENTOR_ENABLED' => \in_array( 'elementor/elementor.php', $active_plugins, true ), // 檢查 elementor 是否啟用
84+
];
9385

9486
\wp_localize_script(
9587
Plugin::$kebab,
96-
'wpApiSettings',
88+
Plugin::$snake . '_data',
9789
[
98-
'root' => \untrailingslashit( \esc_url_raw( rest_url() ) ),
99-
'nonce' => \wp_create_nonce( 'wp_rest' ),
90+
'env' => $env,
10091
]
10192
);
10293
}

inc/classes/FrontEnd/Entry.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
/**
3-
* Front-end Entry
4-
*/
52

63
declare(strict_types=1);
74

@@ -12,22 +9,15 @@
129
if (class_exists('J7\WpRefinePlugin\FrontEnd\Entry')) {
1310
return;
1411
}
15-
/**
16-
* Class FrontEnd
17-
*/
12+
/** Class FrontEnd */
1813
final class Entry {
19-
use \J7\WpUtils\Traits\SingletonTrait;
2014

21-
/**
22-
* Constructor
23-
*/
24-
public function __construct() {
15+
/** Register hooks */
16+
public static function register_hooks(): void {
2517
\add_action( 'wp_footer', [ __CLASS__, 'render_app' ] );
2618
}
2719

28-
/**
29-
* Render application's markup
30-
*/
20+
/** Render application's markup */
3121
public static function render_app(): void {
3222
// phpcs:ignore
3323
echo '<div id="' . substr(Base::APP1_SELECTOR, 1) . '"></div>';

plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct() {
6262
[
6363
'app_name' => 'My Refine App',
6464
'github_repo' => 'https://github.com/j7-dev/wp-refine-plugin',
65-
'callback' => [ Bootstrap::class, 'instance' ],
65+
'callback' => [ Bootstrap::class, 'register_hooks' ],
6666
]
6767
);
6868
}

0 commit comments

Comments
 (0)