Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/3.2.10/onboarding.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-core-data', 'wp-data', 'wp-deprecated', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-media-utils', 'wp-preferences', 'wp-url'), 'version' => '7225a2df2aab286997af');
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-core-data', 'wp-data', 'wp-deprecated', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-media-utils', 'wp-preferences', 'wp-url'), 'version' => 'f661930075f68d48ffeb');
2 changes: 1 addition & 1 deletion build/3.2.10/onboarding.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions includes/RestApi/SiteGenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function register_routes() {
);
\register_rest_route(
$this->namespace,
$this->rest_base . '/homepages',
$this->rest_base . '/homepages',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_homepages' ),
Expand Down Expand Up @@ -225,6 +225,7 @@ public function get_enabled_identifiers() {
* @return array|WP_Error
*/
public function generate_sitegen_meta( \WP_REST_Request $request ) {

if ( ! LegacySiteGenService::is_enabled() ) {
return new \WP_Error(
'nfd_onboarding_error',
Expand All @@ -234,7 +235,7 @@ public function generate_sitegen_meta( \WP_REST_Request $request ) {
}

$site_info = $request->get_param( 'site_info' );
$identifier = $request->get_param( 'identifier' );
$identifier = $request->get_param( 'identifier' );
$site_type = $request->get_param( 'site_type' );
$locale = $request->get_param( 'locale' );
$skip_cache = $request->get_param( 'skip_cache' );
Expand Down Expand Up @@ -274,6 +275,7 @@ public function get_homepages( \WP_REST_Request $request ) {
$locale,
true
);

} else {
$target_audience = LegacySiteGenService::instantiate_site_meta( $site_info, 'target_audience', $site_type, $locale );
if ( is_wp_error( $target_audience ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ContentGenerationPrompt {
private $site_description;

/**
* The type of site (e.g., 'business', 'blog', 'ecommerce').
* The type of site (e.g., 'business', 'blog', 'ecommerce', 'linkinbio').
*
* @var string
*/
Expand Down Expand Up @@ -58,7 +58,7 @@ class ContentGenerationPrompt {
* Constructor for ContentGenerationPrompt.
*
* @param string $site_description The user-provided site description.
* @param string $site_type The type of site (e.g., 'business', 'personal', 'ecommerce').
* @param string $site_type The type of site (e.g., 'business', 'personal', 'ecommerce', 'linkinbio').
* @param string $locale The locale/language code for the site.
*/
public function __construct( $site_description, $site_type, $locale ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use NewfoldLabs\WP\Module\Onboarding\Services\ParallelRequestsService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes\EcommerceSiteTypeService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes\CommonSiteTypeService;
use NewfoldLabs\WP\Module\Onboarding\Types\Page;
use NewfoldLabs\WP\Module\Onboarding\Types\Pages;
use NewfoldLabs\WP\Module\Onboarding\Types\ParallelRequest;
Expand All @@ -16,7 +17,7 @@
class SitekitsContentGeneration {

private static $site_types_supported = [
'ecommerce',
'ecommerce', 'personal', 'business', 'linkinbio'
];

/**
Expand Down Expand Up @@ -115,7 +116,6 @@ public function generate_sitekits( int $count = 3 ) {
$error_message,
array( 'status' => $response_code )
);

return $response;
}

Expand Down Expand Up @@ -150,9 +150,9 @@ private function process_sitekit_item( array $sitekit_item ): array {
$result = array();
$result['slug'] = $sitekit_item['slug'];
$result['title'] = $sitekit_item['title'];
$result['header'] = $sitekit_item['header']['patternContent'];
$result['footer'] = $sitekit_item['footer']['patternContent'];
$result['header'] = $this->check_custom_logo( $sitekit_item['header']['patternContent'] );
$result['footer'] = $this->check_custom_logo( $sitekit_item['footer']['patternContent'] );

$pages = array();
foreach ( $sitekit_item['pages'] as $page_slug => $page_patterns ) {
$page_title = ucfirst( str_replace( '-', ' ', $page_slug ) );
Expand Down Expand Up @@ -213,6 +213,19 @@ private function publish_content( array $posts = array() ): void {
);
}
}
$articles = $posts['articles']['posts'] ?? array();

if ( ! empty( $articles ) ) {
foreach ( $articles as $index => $article ) {
CommonSiteTypeService::publish_article(
$article['title'] ?? 'Article ' . $index + 1,
$article['excerpt'] ?? 'Excerpt for Article ' . $index + 1,
$article['content'] ?? 'Content for Article ' . $index + 1,
$article['image'] ?? '',
$article['categories'] ?? array()
);
}
}
}

/**
Expand All @@ -224,4 +237,22 @@ private function publish_content( array $posts = array() ): void {
public static function site_type_supported( string $site_type ): bool {
return in_array( $site_type, self::$site_types_supported );
}

/**
* Check if a custom logo exists; otherwise, replace the site logo block with the site title block.
*
* @var string $content Content to check.
* @return string
*/
private function check_custom_logo( string $content ): string {
if ( function_exists('has_custom_logo') && ! has_custom_logo() ) {
$content = preg_replace(
'/<!--\s*wp:site-logo\s*\/-->/',
'<!-- wp:site-logo /--><!-- wp:site-title /-->',
$content
);
}

return $content;
}
}
2 changes: 2 additions & 0 deletions includes/Services/Blueprints/BlueprintImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ private function search_replace( string $sql_content ) {

// Replace URLs in content
$sql_content = str_replace( $source_site_url, $target_site_url, $sql_content );
// Replace uploads URLs also for multisite.
$sql_content = preg_replace( "/uploads\/sites\/[0-9]+/", 'uploads/', $sql_content );
// Also handle URLs with trailing slashes
$sql_content = str_replace( $source_site_url . '/', $target_site_url . '/', $sql_content );

Expand Down
112 changes: 112 additions & 0 deletions includes/Services/SiteTypes/CommonSitetypeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Common site type service.
*
* @package NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes
*/

namespace NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes;

/**
* Common site type service.
*/
class CommonSiteTypeService {

/**
* Publishes a blog post.
*
* @param array $post The post.
* @return int|WP_Error The post ID.
*/
public static function publish_article( string $title, string $excerpt, string $content, string $image = '', array $categories = array()) {
// Remove hooks that can slow down the operation.
remove_all_actions('wp_insert_post');
remove_all_actions('save_post');

$post_data = array(
'post_title' => $title,
'post_content' => $content,
'post_excerpt' => $excerpt,
'post_status' => 'publish',
'post_type' => 'post',
'post_author' => get_current_user_id() ?: 1,
);
// Insert post.
$post_id = wp_insert_post( $post_data );
// Validate post was created successfully.
if ( is_wp_error( $post_id ) || !$post_id ) {
return new \WP_Error( 'error_publishing_blog_post', 'Failed to create post' );
}
// Post categories.
if ( ! empty( $categories ) ) {
$category_ids = array();
foreach ( $categories as $category ) {
$category_ids[] = self::create_blog_category( $category );
}
wp_set_post_terms( $post_id, $category_ids, 'category' );
}
// Featured image.
if ( ! empty( $image ) ) {
self::set_featured_image_from_url( $image, $post_id );
}

return $post_id;
}

/**
* Sets the featured image for a blog post.
*
* @param string $image_url The URL of the image.
* @param int $post_id The ID of the post.
* @return void
*/
private static function set_featured_image_from_url( string $image_url, int $post_id ): void {
$image_id = self::import_image_from_url( $image_url, $post_id );
if ( $image_id ) {
update_post_meta( $post_id, '_thumbnail_id', $image_id );
}
}

/**
* Imports an image from a URL.
*
* @param string $image_url The URL of the image.
* @param int $post_id The ID of the post.
* @return int The ID of the attachment.
*/
private static function import_image_from_url( string $image_url, int $post_id ): int {
if ( ! function_exists( 'media_handle_sideload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/media.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
}

// Add an arbitrary extension to the image URL to trick media_sideload_image to download the image.
$image_url = $image_url . '?ext=.jpeg';
$attachment_id = media_sideload_image( $image_url, $post_id, null, 'id' );
if ( is_wp_error( $attachment_id ) ) {
return 0;
}

return $attachment_id;
}

/**
* Creates or gets a blog category.
*
* @param string $name The name of the category.
* @return int The ID of the category.
*/
public static function create_blog_category( string $name ): int {
$category_slug = sanitize_title( $name );
$category = get_term_by( 'slug', $category_slug, 'category' );
if ( $category ) {
return $category->term_id;
}
$category = wp_insert_term( $name, 'category' );
if ( is_wp_error( $category ) ) {
return 0;
}
return $category['term_id'];
}
}
Loading
Loading