Skip to content

Update urls in DB if site is being created from existing folder - #962

Merged
nightnei merged 3 commits into
trunkfrom
fix/updatingSiteUrlIfItWasCreatedFromExistingFolder
Feb 21, 2025
Merged

Update urls in DB if site is being created from existing folder#962
nightnei merged 3 commits into
trunkfrom
fix/updatingSiteUrlIfItWasCreatedFromExistingFolder

Conversation

@nightnei

Copy link
Copy Markdown
Contributor

Related issues

Proposed Changes

The initial issue was about broken URLs for images, but actually we have much bigger problems (Antonio was right here) - in 100% cases the website is completely broken. But this PR indeed fixes both - images and broken wp-admin.

Screenshot 2025-02-20 at 17 28 29

Testing Instructions

  1. We need to create a website with some big port, it will simplify testing
    1.1. Create a few websites (e.g. three)
    1.2 Open wp admin for the last one (let's assume that it has port 8883) and create a new post with adding some image there
    1.3 Remove the last one, but keep a folder on your laptop
    1.4 Remove other websites, together with folders, we don't need them anymore
  2. So now we have folder with WP site, which saved port 8883 in URLs inside your DB (you can confirm it by opening /wp-content/databases/.ht.sqlite and check for example wp-options -> home || siteurl)
  3. Create a new website, but provide this fodler as Local path in the creation dialog
  4. Click on "WP Admin" link
  5. Assert that wp admin looks good in browser, there are no error in console, connected to not available favicon, files etc
  6. Assert that the image is available from 1.2 step
  7. Additionally, you can check your DB (wp_options, wp_users and wp_posts)
@nightnei
nightnei requested a review from a team February 20, 2025 17:28
Comment thread src/ipc-types.d.ts
interface StartedSiteDetails extends StoppedSiteDetails {
running: true;

port: number;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed removing it in this my PR, so decided to remove in this PR

Comment thread src/ipc-handlers.ts
if ( ! ( await pathExists( nodePath.join( path, 'wp-config.php' ) ) ) ) {
await installSqliteIntegration( path );
} else {
await updateSiteUrlToLocal( details.id );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about different options and places to do it, but this one looks as the most correct one, since existence of wp-config.php tells us - was the site already initialized or not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good approach to fix the URL when the site is created.

// This method can be overridden by subclasses to prepare the SQL file before import.
}

protected async replaceSiteUrl( siteId: string ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it outside as a separate function since now it's used not only for import flow, so it's better to move it outside of import/export modules.


for ( const urlToReplace of oldUrlVariants ) {
const { stderr, exitCode } = await server.executeWpCliCommand(
`search-replace '${ urlToReplace }' '${ studioUrl }' --skip-columns=guid`,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole function is totally the same as it was before, so not necessary to review it, but two things:

  1. Renamed to updateSiteUrlToLocal.
  2. I added --skip-columns=guid to skip the guid column of posts, which is considered, AFAIU, as a best practice in WP world. I know that in this Testing instructions and similar cases it won't be a problem, but I believe that it's worth adding, since guid is just id, despite that fact that they look like urls.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for summarizing the changes.

@nightnei nightnei changed the title fix: update urls in DB if site is being created from existing folder Feb 20, 2025

@wojtekn wojtekn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as expected.

Comment thread src/ipc-handlers.ts
if ( ! ( await pathExists( nodePath.join( path, 'wp-config.php' ) ) ) ) {
await installSqliteIntegration( path );
} else {
await updateSiteUrlToLocal( details.id );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good approach to fix the URL when the site is created.


for ( const urlToReplace of oldUrlVariants ) {
const { stderr, exitCode } = await server.executeWpCliCommand(
`search-replace '${ urlToReplace }' '${ studioUrl }' --skip-columns=guid`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for summarizing the changes.

@nightnei
nightnei merged commit 69c5f61 into trunk Feb 21, 2025
@nightnei
nightnei deleted the fix/updatingSiteUrlIfItWasCreatedFromExistingFolder branch February 21, 2025 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants