66 exit ; // Exit if accessed directly.
77}
88
9+ use Elementor \Core \DocumentTypes \Page ;
910use HelloTheme \Includes \Utils ;
1011use WP_REST_Server ;
1112
@@ -24,9 +25,11 @@ public function register_routes() {
2425 }
2526
2627 public function get_admin_config () {
28+ $ elementor_page_id = Utils::is_elementor_active () ? $ this ->ensure_elementor_page_exists () : null ;
29+
2730 $ config = $ this ->get_welcome_box_config ( [] );
2831
29- $ config = $ this ->get_site_parts ( $ config );
32+ $ config = $ this ->get_site_parts ( $ config, $ elementor_page_id );
3033
3134 $ config = $ this ->get_resources ( $ config );
3235
@@ -40,6 +43,57 @@ public function get_admin_config() {
4043 return rest_ensure_response ( [ 'config ' => $ config ] );
4144 }
4245
46+ private function ensure_elementor_page_exists (): int {
47+ $ existing_page = \Elementor \Core \DocumentTypes \Page::get_elementor_page ();
48+
49+ if ( $ existing_page ) {
50+ return $ existing_page ->ID ;
51+ }
52+
53+ $ page_data = [
54+ 'post_title ' => 'Hello Theme page ' ,
55+ 'post_content ' => '' ,
56+ 'post_status ' => 'draft ' ,
57+ 'post_type ' => 'page ' ,
58+ 'meta_input ' => [
59+ '_elementor_edit_mode ' => 'builder ' ,
60+ '_elementor_template_type ' => 'wp-page ' ,
61+ ],
62+ ];
63+
64+ $ page_id = wp_insert_post ( $ page_data );
65+
66+ if ( is_wp_error ( $ page_id ) ) {
67+ throw new \RuntimeException ( 'Failed to create Elementor page: ' . esc_html ( $ page_id ->get_error_message () ) );
68+ }
69+
70+ if ( ! $ page_id ) {
71+ throw new \RuntimeException ( 'Page creation returned invalid ID ' );
72+ }
73+
74+ wp_update_post ([
75+ 'ID ' => $ page_id ,
76+ 'post_title ' => 'Hello Theme # ' . $ page_id ,
77+ ]);
78+ return $ page_id ;
79+ }
80+
81+ private function get_elementor_editor_url ( ?int $ page_id , string $ active_tab ): string {
82+ $ active_kit_id = Utils::elementor ()->kits_manager ->get_active_id ();
83+
84+ $ url = add_query_arg (
85+ [
86+ 'post ' => $ page_id ,
87+ 'action ' => 'elementor ' ,
88+ 'active-tab ' => $ active_tab ,
89+ 'active-document ' => $ active_kit_id ,
90+ ],
91+ admin_url ( 'post.php ' )
92+ );
93+
94+ return $ url . '#e:run:panel/global/open ' ;
95+ }
96+
4397 public function get_resources ( array $ config ) {
4498 $ config ['resourcesData ' ] = [
4599 'community ' => [
@@ -92,7 +146,7 @@ public function get_resources( array $config ) {
92146 return $ config ;
93147 }
94148
95- public function get_site_parts ( array $ config ): array {
149+ public function get_site_parts ( array $ config, ? int $ elementor_page_id = null ): array {
96150 $ last_five_pages_query = new \WP_Query (
97151 [
98152 'posts_per_page ' => 5 ,
@@ -136,7 +190,7 @@ public function get_site_parts( array $config ): array {
136190
137191 $ common_parts = [];
138192
139- $ customizer_header_footer_url = $ this ->get_open_homepage_with_tab ( '' , [ 'autofocus[section] ' => 'hello-options ' ] );
193+ $ customizer_header_footer_url = $ this ->get_open_homepage_with_tab ( $ elementor_page_id , '' , null , [ 'autofocus[section] ' => 'hello-options ' ] );
140194
141195 $ header_part = [
142196 'id ' => 'header ' ,
@@ -161,8 +215,8 @@ public function get_site_parts( array $config ): array {
161215 'icon ' => 'ThemeBuilderIcon ' ,
162216 ],
163217 ];
164- $ header_part ['link ' ] = $ this ->get_open_homepage_with_tab ( 'hello-settings-header ' );
165- $ footer_part ['link ' ] = $ this ->get_open_homepage_with_tab ( 'hello-settings-footer ' );
218+ $ header_part ['link ' ] = $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'hello-settings-header ' );
219+ $ footer_part ['link ' ] = $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'hello-settings-footer ' );
166220
167221 if ( Utils::has_pro () ) {
168222 $ header_part = $ this ->update_pro_part ( $ header_part , 'header ' );
@@ -184,7 +238,7 @@ public function get_site_parts( array $config ): array {
184238
185239 $ config ['siteParts ' ] = apply_filters ( 'hello-plus-theme/template-parts ' , $ site_parts );
186240
187- return $ this ->get_quicklinks ( $ config );
241+ return $ this ->get_quicklinks ( $ config, $ elementor_page_id );
188242 }
189243
190244 private function update_pro_part ( array $ part , string $ location ): array {
@@ -197,7 +251,7 @@ private function update_pro_part( array $part, string $location ): array {
197251 $ edit_link = get_edit_post_link ( $ first_document_id , 'admin ' ) . '&action=elementor ' ;
198252
199253 } else {
200- $ edit_link = $ this ->get_open_homepage_with_tab ( 'hello-settings- ' . $ location );
254+ $ edit_link = $ this ->get_open_homepage_with_tab ( null , 'hello-settings- ' . $ location );
201255 }
202256 $ part ['sublinks ' ] = [
203257 [
@@ -213,44 +267,50 @@ private function update_pro_part( array $part, string $location ): array {
213267 return $ part ;
214268 }
215269
216- public function get_open_homepage_with_tab ( $ action , $ customizer_fallback_args = [] ): string {
217- if ( Utils::is_elementor_active () && method_exists ( \Elementor \Core \DocumentTypes \Page::class, 'get_site_settings_url_config ' ) ) {
218- return \Elementor \Core \DocumentTypes \Page::get_site_settings_url_config ( $ action )['url ' ];
270+ public function get_open_homepage_with_tab ( ?int $ page_id , $ action , $ section = null , $ customizer_fallback_args = [] ): string {
271+ if ( Utils::is_elementor_active () ) {
272+ $ url = $ page_id ? $ this ->get_elementor_editor_url ( $ page_id , $ action ) : Page::get_site_settings_url_config ( $ action )['url ' ];
273+
274+ if ( $ section ) {
275+ $ url = add_query_arg ( 'active-section ' , $ section , $ url );
276+ }
277+
278+ return $ url ;
219279 }
220280
221281 return add_query_arg ( $ customizer_fallback_args , self_admin_url ( 'customize.php ' ) );
222282 }
223283
224- public function get_quicklinks ( $ config ): array {
284+ public function get_quicklinks ( $ config, ? int $ elementor_page_id = null ): array {
225285 $ config ['quickLinks ' ] = [
226286 'site_name ' => [
227287 'title ' => __ ( 'Site Name ' , 'hello-elementor ' ),
228- 'link ' => $ this ->get_open_homepage_with_tab ( 'settings-site-identity ' , [ 'autofocus[section] ' => 'title_tagline ' ] ),
288+ 'link ' => $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'settings-site-identity ' , null , [ 'autofocus[section] ' => 'title_tagline ' ] ),
229289 'icon ' => 'TextIcon ' ,
230290
231291 ],
232292 'site_logo ' => [
233293 'title ' => __ ( 'Site Logo ' , 'hello-elementor ' ),
234- 'link ' => $ this ->get_open_homepage_with_tab ( 'settings-site-identity ' , [ 'autofocus[section] ' => 'title_tagline ' ] ),
294+ 'link ' => $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'settings-site-identity ' , null , [ 'autofocus[section] ' => 'title_tagline ' ] ),
235295 'icon ' => 'PhotoIcon ' ,
236296 ],
237297 'site_favicon ' => [
238298 'title ' => __ ( 'Site Favicon ' , 'hello-elementor ' ),
239- 'link ' => $ this ->get_open_homepage_with_tab ( 'settings-site-identity ' , [ 'autofocus[section] ' => 'title_tagline ' ] ),
299+ 'link ' => $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'settings-site-identity ' , null , [ 'autofocus[section] ' => 'title_tagline ' ] ),
240300 'icon ' => 'AppsIcon ' ,
241301 ],
242302 ];
243303
244304 if ( Utils::is_elementor_active () ) {
245305 $ config ['quickLinks ' ]['site_colors ' ] = [
246306 'title ' => __ ( 'Site Colors ' , 'hello-elementor ' ),
247- 'link ' => $ this ->get_open_homepage_with_tab ( 'global-colors ' ),
307+ 'link ' => $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'global-colors ' ),
248308 'icon ' => 'BrushIcon ' ,
249309 ];
250310
251311 $ config ['quickLinks ' ]['site_fonts ' ] = [
252312 'title ' => __ ( 'Site Fonts ' , 'hello-elementor ' ),
253- 'link ' => $ this ->get_open_homepage_with_tab ( 'global-typography ' ),
313+ 'link ' => $ this ->get_open_homepage_with_tab ( $ elementor_page_id , 'global-typography ' ),
254314 'icon ' => 'UnderlineIcon ' ,
255315 ];
256316 }
0 commit comments