Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 105 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SiteNavigationService | |
0.00% |
0 / 105 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_site_navigation_items | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
56 | |||
| setup_site_nav_menu | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
30 | |||
| add_page_to_navigation | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| get_nav_link_block_grammar | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | } |