Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SiteGenService | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| publish_homepage | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
56 | |||
| get_sitemap_page_title | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| add_page_to_navigation | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| get_prompt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| get_locale | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Onboarding\Services; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\SitePagesService; |
| 6 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService as LegacySiteGenService; |
| 7 | |
| 8 | class SiteGenService { |
| 9 | |
| 10 | /** |
| 11 | * The Redux input object. |
| 12 | * |
| 13 | * @var array|null |
| 14 | */ |
| 15 | private ?array $input_data = null; |
| 16 | |
| 17 | /** |
| 18 | * The Redux sitegen object. |
| 19 | * |
| 20 | * @var array|null |
| 21 | */ |
| 22 | private ?array $sitegen_data = null; |
| 23 | |
| 24 | public function __construct() { |
| 25 | $this->input_data = ReduxStateService::get( 'input' ); |
| 26 | $this->sitegen_data = ReduxStateService::get( 'sitegen' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Publish the selected sitegen homepage. |
| 31 | * |
| 32 | * @param string $selected_sitegen_homepage The selected sitegen homepage to publish. |
| 33 | * @return int|\WP_Error |
| 34 | */ |
| 35 | public function publish_homepage( string $selected_sitegen_homepage ): int | \WP_Error { |
| 36 | // Validate we have the selected homepage. |
| 37 | if ( |
| 38 | ! $this->sitegen_data || |
| 39 | ! is_array( $this->sitegen_data['homepages'] ) || |
| 40 | ! isset( $this->sitegen_data['homepages'][ $selected_sitegen_homepage ] ) |
| 41 | ) { |
| 42 | return new \WP_Error( |
| 43 | 'sitegen_homepage_publish_validation_error', |
| 44 | 'Error validating selected homepage.', |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | $selected_homepage = $this->sitegen_data['homepages'][ $selected_sitegen_homepage ]; |
| 49 | $content = $selected_homepage['content']; |
| 50 | $title = __( 'Home', 'wp-module-onboarding' ); |
| 51 | |
| 52 | $post_id = SitePagesService::publish_page( |
| 53 | $title, |
| 54 | $content, |
| 55 | true, |
| 56 | array( |
| 57 | 'nf_dc_page' => 'home', |
| 58 | ) |
| 59 | ); |
| 60 | if ( 0 === $post_id || is_wp_error( $post_id ) ) { |
| 61 | return new \WP_Error( |
| 62 | 'sitegen_homepage_publish_error', |
| 63 | 'Error publishing homepage.', |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | // Add the homepage to the site navigation. |
| 68 | $this->add_page_to_navigation( $post_id, $title, get_permalink( $post_id ) ); |
| 69 | |
| 70 | // Change WordPress reading options to show static page as homepage. |
| 71 | $wp_reading_homepage_option = get_option( 'show_on_front' ); |
| 72 | if ( 'page' !== $wp_reading_homepage_option ) { |
| 73 | update_option( 'show_on_front', 'page' ); |
| 74 | } |
| 75 | // Set the homepage as the front page. |
| 76 | update_option( 'page_on_front', $post_id ); |
| 77 | |
| 78 | return $post_id; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get AI generated page title for a given slug (if found in sitemap). |
| 83 | * |
| 84 | * @param string $slug The slug of the page to get the title for. |
| 85 | * @return string|false The page title, or false if not found. |
| 86 | */ |
| 87 | public function get_sitemap_page_title( string $slug ): string|false { |
| 88 | $prompt = $this->get_prompt(); |
| 89 | $locale = $this->get_locale(); |
| 90 | if ( ! $prompt || ! $locale ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | $sitemap = LegacySiteGenService::instantiate_site_meta( $prompt, 'sitemap', $locale ); |
| 95 | if ( ! is_wp_error( $sitemap ) ) { |
| 96 | foreach ( $sitemap as $page ) { |
| 97 | if ( $slug === $page['slug'] ) { |
| 98 | $title = $page['title']; |
| 99 | return $title; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Add a page to the site navigation. |
| 109 | * |
| 110 | * @param int $post_id The ID of the page to add to the navigation. |
| 111 | * @param string $page_title The title of the page. |
| 112 | * @param string $permalink The permalink of the page. |
| 113 | */ |
| 114 | public function add_page_to_navigation( int $post_id, string $page_title, string $permalink ): void { |
| 115 | $id = $post_id; |
| 116 | $label = $page_title; |
| 117 | $url = $permalink; |
| 118 | |
| 119 | $nav_link_grammar = "<!-- wp:navigation-link {\"label\":\"$label\",\"type\":\"page\",\"id\":$id,\"url\":\"$url\",\"kind\":\"post-type\"} /-->"; |
| 120 | |
| 121 | $navigation = new \WP_Query( |
| 122 | array( |
| 123 | 'name' => 'navigation', |
| 124 | 'post_type' => 'wp_navigation', |
| 125 | ) |
| 126 | ); |
| 127 | if ( ! empty( $navigation->posts ) ) { |
| 128 | wp_update_post( |
| 129 | array( |
| 130 | 'ID' => $navigation->posts[0]->ID, |
| 131 | 'post_content' => $nav_link_grammar . $navigation->posts[0]->post_content, |
| 132 | ) |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the prompt entered during Onboarding. |
| 139 | * |
| 140 | * @return string|false |
| 141 | */ |
| 142 | public function get_prompt(): string|false { |
| 143 | return ! empty( $this->input_data['prompt'] ) ? $this->input_data['prompt'] : false; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the locale entered during Onboarding. |
| 148 | * |
| 149 | * @return string |
| 150 | */ |
| 151 | public function get_locale(): string { |
| 152 | return ! empty( $this->input_data['locale'] ) ? $this->input_data['locale'] : 'en_US'; |
| 153 | } |
| 154 | } |