11<?php
2- /**
3- * Bootstrap
4- */
52
63declare (strict_types = 1 );
74
1512}
1613/** Class Bootstrap */
1714final 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 }
0 commit comments