Skip to content

Commit 6d3c7b8

Browse files
committed
setup sitenav
1 parent ae2a826 commit 6d3c7b8

File tree

8 files changed

+328
-34
lines changed

8 files changed

+328
-34
lines changed

includes/RestApi/SiteGenController.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use NewfoldLabs\WP\Module\Onboarding\Data\SiteGen as SiteGenData;
88
use NewfoldLabs\WP\Module\Onboarding\Services\Ai\ContentGeneration\SitekitsContentGeneration;
99
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenService;
10+
use NewfoldLabs\WP\Module\Onboarding\Services\SiteNavigationService;
1011

1112
/**
1213
* Class SiteGenController
@@ -92,6 +93,17 @@ public function register_routes() {
9293
'args' => $this->get_publish_sitemap_pages_args(),
9394
)
9495
);
96+
97+
\register_rest_route(
98+
$this->namespace,
99+
$this->rest_base . '/setup-nav-menu',
100+
array(
101+
'methods' => \WP_REST_Server::CREATABLE,
102+
'callback' => array( $this, 'setup_nav_menu' ),
103+
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
104+
'args' => $this->get_setup_nav_menu_args(),
105+
)
106+
);
95107
}
96108

97109
/**
@@ -182,6 +194,20 @@ public function get_regenerate_homepage_args() {
182194
);
183195
}
184196

197+
/**
198+
* Gets the arguments for the 'setup-nav-menu' endpoint.
199+
*
200+
* @return array The array of arguments.
201+
*/
202+
public function get_setup_nav_menu_args() {
203+
return array(
204+
'site_type' => array(
205+
'required' => false,
206+
'type' => 'string',
207+
),
208+
);
209+
}
210+
185211
/**
186212
* Gets all the valid Identifiers
187213
*
@@ -359,4 +385,26 @@ public function publish_sitemap_pages( \WP_REST_Request $request ) {
359385
public function get_site_details_meta() {
360386
return SiteGenData::get_site_details_questionnaire();
361387
}
388+
389+
/**
390+
* Setup the nav menu.
391+
*
392+
* @return array|WP_Error
393+
*/
394+
public function setup_nav_menu( \WP_REST_Request $request ) {
395+
$site_type = $request->get_param( 'site_type' );
396+
397+
$siteGenService = new SiteNavigationService();
398+
$result = $siteGenService->setup_site_nav_menu( $site_type );
399+
400+
if ( ! $result ) {
401+
return new \WP_Error(
402+
'nfd_onboarding_error',
403+
__( 'Error at Setting up the nav menu.', 'wp-module-onboarding' ),
404+
array( 'status' => 500 )
405+
);
406+
}
407+
408+
return new \WP_REST_Response( array( 'success' => $result ), 200 );
409+
}
362410
}

includes/Services/SiteGenService.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ public function publish_homepage( string $selected_sitegen_homepage ) {
140140
// Process images immediately in background (non-blocking)
141141
SiteGenImageService::process_homepage_images_immediate_async( $post_id, $content );
142142

143-
// Add the homepage to the site navigation.
144-
$this->add_page_to_navigation( $post_id, $title, get_permalink( $post_id ) );
145-
146143
// Change WordPress reading options to show static page as homepage.
147144
$wp_reading_homepage_option = get_option( 'show_on_front' );
148145
if ( 'page' !== $wp_reading_homepage_option ) {
@@ -240,36 +237,6 @@ public function get_color_palette() {
240237
return new ColorPalette( $palette_slug, $palette_colors );
241238
}
242239

243-
/**
244-
* Add a page to the site navigation.
245-
*
246-
* @param int $post_id The ID of the page to add to the navigation.
247-
* @param string $page_title The title of the page.
248-
* @param string $permalink The permalink of the page.
249-
*/
250-
public function add_page_to_navigation( int $post_id, string $page_title, string $permalink ): void {
251-
$id = $post_id;
252-
$label = $page_title;
253-
$url = $permalink;
254-
255-
$nav_link_grammar = "<!-- wp:navigation-link {\"label\":\"$label\",\"type\":\"page\",\"id\":$id,\"url\":\"$url\",\"kind\":\"post-type\"} /-->";
256-
257-
$navigation = new \WP_Query(
258-
array(
259-
'name' => 'navigation',
260-
'post_type' => 'wp_navigation',
261-
)
262-
);
263-
if ( ! empty( $navigation->posts ) ) {
264-
wp_update_post(
265-
array(
266-
'ID' => $navigation->posts[0]->ID,
267-
'post_content' => $nav_link_grammar . $navigation->posts[0]->post_content,
268-
)
269-
);
270-
}
271-
}
272-
273240
/**
274241
* Get the prompt entered during Onboarding.
275242
*
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Site Navigation Service
4+
*
5+
* @package NewfoldLabs\WP\Module\Onboarding\Services
6+
*/
7+
8+
namespace NewfoldLabs\WP\Module\Onboarding\Services;
9+
10+
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService as LegacySiteGenService;
11+
use NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes\EcommerceSiteTypeService;
12+
13+
/**
14+
* Site Navigation Service Class
15+
*
16+
* Handles the setup and management of site navigation menus.
17+
*/
18+
class SiteNavigationService extends SiteGenService {
19+
20+
/**
21+
* Constructor.
22+
*/
23+
public function __construct() {
24+
parent::__construct();
25+
}
26+
27+
/**
28+
* Get the site navigation menu items.
29+
*
30+
* @param string|null $site_type The site type.
31+
* @return array Array of navigation items. Each item contains:
32+
* {
33+
* @type int $post_id The ID of the page
34+
* @type string $title The title of the page
35+
* @type string $permalink The permalink of the page
36+
* @type string $block_grammar The navigation link block grammar
37+
* }
38+
*/
39+
public function get_site_navigation_items( $site_type = null ): array {
40+
if ( ! $site_type ) {
41+
$site_type = $this->get_site_type();
42+
}
43+
44+
$sitemap = LegacySiteGenService::instantiate_site_meta(
45+
$this->get_prompt(),
46+
'sitemap',
47+
$site_type,
48+
$this->get_locale()
49+
);
50+
51+
$nav_items = array();
52+
53+
// Add home page.
54+
$home_page = array_filter( $sitemap, function( $page ) {
55+
return '/' === $page['path'];
56+
} );
57+
if ( ! empty( $home_page ) ) {
58+
$title = $home_page[0]['title'];
59+
$nav_items[] = array(
60+
'post_id' => 0,
61+
'title' => $title,
62+
'permalink' => get_site_url(),
63+
'block_grammar' => self::get_nav_link_block_grammar( 0, $title, get_site_url() ),
64+
);
65+
}
66+
67+
// Add WooCommerce shop page (ecommerce site type).
68+
if ( 'ecommerce' === $site_type ) {
69+
// Setup WooCommerce pages.
70+
EcommerceSiteTypeService::setup_woo_pages();
71+
72+
// Get the WooCommerce shop page info.
73+
$woo_shop_page = EcommerceSiteTypeService::get_woo_shop_page_info();
74+
if ( ! empty( $woo_shop_page ) ) {
75+
$nav_items[] = array(
76+
'post_id' => $woo_shop_page['id'],
77+
'title' => $woo_shop_page['title'],
78+
'permalink' => $woo_shop_page['permalink'],
79+
'block_grammar' => self::get_nav_link_block_grammar( $woo_shop_page['id'], $woo_shop_page['title'], $woo_shop_page['permalink'] ),
80+
);
81+
}
82+
}
83+
84+
// Add other published pages to the navigation.
85+
foreach ( $sitemap as $page ) {
86+
// Validate the page is published.
87+
$path = $page['path'];
88+
$page_id = get_page_by_path( $path );
89+
if ( ! $page_id ) {
90+
continue;
91+
}
92+
93+
$nav_items[] = array(
94+
'post_id' => $page_id->ID,
95+
'title' => $page['title'],
96+
'permalink' => get_permalink( $page_id ),
97+
'block_grammar' => self::get_nav_link_block_grammar( $page_id->ID, $page['title'], get_permalink( $page_id ) ),
98+
);
99+
}
100+
101+
return $nav_items;
102+
}
103+
104+
/**
105+
* Setup the site navigation menu.
106+
*
107+
* @param string|null $site_type The site type.
108+
* @return bool True if the navigation menu was setup, false otherwise.
109+
*/
110+
public function setup_site_nav_menu( $site_type = '' ): bool {
111+
if ( ! $site_type ) {
112+
$site_type = $this->get_site_type();
113+
}
114+
115+
$nav_items = $this->get_site_navigation_items( $site_type );
116+
if ( empty( $nav_items ) ) {
117+
return false;
118+
}
119+
120+
$navigation_links_grammar = '';
121+
foreach ( $nav_items as $nav_item ) {
122+
$navigation_links_grammar .= $nav_item['block_grammar'];
123+
}
124+
125+
$navigation = new \WP_Query(
126+
array(
127+
'name' => 'navigation',
128+
'post_type' => 'wp_navigation',
129+
)
130+
);
131+
if ( ! empty( $navigation->posts ) ) {
132+
// If we already have a navigation menu, update it.
133+
wp_update_post(
134+
array(
135+
'ID' => $navigation->posts[0]->ID,
136+
'post_content' => $navigation_links_grammar,
137+
)
138+
);
139+
} else {
140+
// Create a new navigation menu.
141+
wp_insert_post(
142+
array(
143+
'post_title' => 'Navigation',
144+
'post_content' => $navigation_links_grammar,
145+
'post_type' => 'wp_navigation',
146+
'post_status' => 'publish',
147+
)
148+
);
149+
}
150+
$navigation = new \WP_Query(
151+
array(
152+
'name' => 'navigation',
153+
'post_type' => 'wp_navigation',
154+
)
155+
);
156+
157+
return true;
158+
}
159+
160+
/**
161+
* Add a page to the site navigation.
162+
*
163+
* @param int $post_id The ID of the page to add to the navigation.
164+
* @param string $page_title The title of the page.
165+
* @param string $permalink The permalink of the page.
166+
*/
167+
public function add_page_to_navigation( int $post_id, string $page_title, string $permalink ): void {
168+
$id = (int) $post_id;
169+
$label = sanitize_text_field( $page_title );
170+
$url = esc_url_raw( $permalink );
171+
172+
$nav_link_grammar = self::get_nav_link_block_grammar( $id, $label, $url );
173+
174+
$navigation = new \WP_Query(
175+
array(
176+
'name' => 'navigation',
177+
'post_type' => 'wp_navigation',
178+
)
179+
);
180+
if ( ! empty( $navigation->posts ) ) {
181+
wp_update_post(
182+
array(
183+
'ID' => $navigation->posts[0]->ID,
184+
'post_content' => $nav_link_grammar . $navigation->posts[0]->post_content,
185+
)
186+
);
187+
}
188+
}
189+
190+
/**
191+
* Get the navigation link block grammar.
192+
*
193+
* @param int $post_id The ID of the page to add to the navigation.
194+
* @param string $page_title The title of the page.
195+
* @param string $permalink The permalink of the page.
196+
* @return string The navigation link block grammar.
197+
*/
198+
public static function get_nav_link_block_grammar( int $post_id, string $page_title, string $permalink ): string {
199+
$id = (int) $post_id;
200+
$label = sanitize_text_field( $page_title );
201+
$url = esc_url_raw( $permalink );
202+
203+
return sprintf(
204+
'<!-- wp:navigation-link {"label":"%s","type":"page","id":%d,"url":"%s","kind":"post-type"} /-->',
205+
$label,
206+
$id,
207+
$url
208+
);
209+
}
210+
}

includes/Services/SiteTypes/EcommerceSiteTypeService.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,42 @@ public static function create_or_get_woo_category( string $name ): int {
124124
return $category['term_id'];
125125
}
126126

127+
/**
128+
* Sets up the WooCommerce pages.
129+
*
130+
* @return bool
131+
*/
132+
public static function setup_woo_pages(): bool {
133+
if ( class_exists( '\WC_Install' ) ) {
134+
\WC_Install::create_pages();
135+
return true;
136+
}
137+
138+
return false;
139+
}
140+
141+
/**
142+
* Gets the WooCommerce shop page info.
143+
*
144+
* @return array The shop page info or an empty array if the shop page is not found.
145+
*/
146+
public static function get_woo_shop_page_info(): array {
147+
if ( ! function_exists( '\wc_get_page_id' ) || ! function_exists( '\wc_get_page_permalink' ) ) {
148+
return array();
149+
}
150+
151+
$shop_page_id = \wc_get_page_id( 'shop' );
152+
if ( $shop_page_id ) {
153+
return array(
154+
'id' => $shop_page_id,
155+
'title' => \get_the_title( $shop_page_id ),
156+
'permalink' => \get_permalink( $shop_page_id ),
157+
);
158+
}
159+
160+
return array();
161+
}
162+
127163
/**
128164
* Gets the ecommerce plugins.
129165
*

src/app/utils/api/onboarding.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ export async function getSitePages( prompt, siteType, locale ) {
198198
);
199199
}
200200

201+
export async function setupSiteNavigationMenu( siteType = '' ) {
202+
return await resolve(
203+
apiFetch( {
204+
url: onboardingRestURL( 'sitegen/setup-nav-menu' ),
205+
method: 'POST',
206+
data: {
207+
site_type: siteType,
208+
},
209+
} ).then()
210+
);
211+
}
212+
201213
/**
202214
* Get the snapshot data for a preview.
203215
*

0 commit comments

Comments
 (0)