Skip to content

Add WordPress Playground plugin - #154

Merged
adamziel merged 57 commits into
trunkfrom
add/playground-plugin
Feb 7, 2024
Merged

adamziel merged 57 commits into
trunkfrom
add/playground-plugin

Conversation

@bgrgicak

@bgrgicak bgrgicak commented Feb 6, 2024 •

Copy link
Copy Markdown
Collaborator

What?

This adds the playground plugin for plugin-preview functionality and renames the plugin to WordPress Playground.

Requires:
WordPress/wordpress-playground#745 .
WordPress/blueprints#49

Why?

Users want to be able to preview plugins on their own sites before actually committing to installing them live.

Testing Instructions

  • Checkout the project and copy the plugin to your local WordPress install:
git checkout sm-collector-plugin
cp packages/collector/ [PATH_TO_YOUR_WORDPRESS]/wp-content/plugins/
  • Open your local WordPress install and activate the WordPress Playground plugin
  • Start Playground by clicking on Tools > Sandbox Site in wp-admin
  • After the site loads it should have the same content as your site (files and database)

Testing Previews

  • Install this plugin on a site
  • Navigate to plugins > add new
  • Search for "Akismet"
  • You should now see a Preview Now button next to the Install Now button.
  • Click Preview now, and you should see Playground boot your site with the plugin activated.

@bgrgicak bgrgicak self-assigned this Feb 6, 2024
@bgrgicak
bgrgicak requested a review from adamziel February 6, 2024 19:07
'INSERT INTO %1$s (%2$s) VALUES (%3$s);',
$wpdb->quote_identifier($table),
escape_array(
array_keys($record)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Column names are identifiers, but they're escaped as values here. Should $wpdb->quote_identifier be used here, too?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I used $wpdb->prepare here, but $wpdb->quote_identifier would be more appropriate here.

Done in 1eb00ee

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Now when I think of it, we don't need column names or am I missing something?

This example works.

		array_push(
				$sql_dump,
				sprintf(
					'INSERT INTO %1$s VALUES (%2$s);',
					$wpdb->quote_identifier($table),
					escape_array(array_values($record))
				)
			);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I removed column names in dd36deb.

return '';
}
// A query needs to be on a single line
return preg_replace("/\r?\n/", "", $schema['Create Table']);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice!

extractToPath: '/wordpress',
},
{
step: 'runSql',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lovely! I'd like Playground to do that automatically eventually, but today of course let's stick to an explicit step.

@bgrgicak

bgrgicak commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator Author

@adamziel I was able to remove the need for a proxy by fetching blueprints from the backend. 1faf0c5

Comment thread packages/playground/playground.php Outdated
return $action_links;
}

$blueprint = $plugin['blueprints'][0];

@adamziel adamziel Feb 7, 2024 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oooh this is quite tricky!

We can display the Preview button on every plugin, whether it has a Blueprint or not. When you install a plugin, you don't get any initial setup other than what the plugin does on activation. It makes sense to reproduce that experience here.

Then, what should we do when the plugin ships a Blueprint? My gut says ignore the Blueprint.

Why?

These Blueprints are written with a vanilla WordPress site in mind. They'll typically create dummy content, change site options, and so on to reach a minimal preview environment. In some cases, like bbpress, that makes a lot of sense even in the context of your site as it doesn't have any forums in place. In other cases, that would only add confusing bloat like lorem ipsum pages when your site already has pages.

Then, applying a Blueprint means the preview experience will be very different from the Installing experience. At best, you won't get the setup wizard. At worst, you'll think "what the... is this plugin going to create 50 dummy posts when I install it? I better go for another one".

So I suggest let's ignore these Blueprints and open a new issue to discuss that with @tellywort, @StevenDufresne, and also some plugin authors.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I removed the need for blueprints in 3bdd04a

@adamziel

adamziel commented Feb 7, 2024 •

Copy link
Copy Markdown
Collaborator

The plugin worked beautifully with a real WordPress using a local wp-env, yay! It failed to transfer the database when I used wp-now, there must be some limitation in the SQLite integration plugin. It's not a blocker, I'm just making a note for posterity. Other than the note I left inline in the diff, everything looks great. Thank you so much @bgrgicak!

@adamziel

adamziel commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator

I inspected a part of the SQL export that failed in wp-now, it seems like there's a bunch of newlines involved:

CleanShot 2024-02-07 at 13 02 52@2x

@adamziel

adamziel commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator

This fixed the whitespace but not the import:

function escape_array($array)
{
	global $wpdb;
	$escaped = array();
	foreach ($array as $value) {
		if (is_numeric($value)) {
			$escaped[] = $wpdb->prepare('%d', $value);
		} else {
-			$escaped[] = $wpdb->prepare('%s', $value);
+			$escaped[] = $wpdb->prepare('%s', str_replace("\n", "\\n", $value));
		}
	}
	return implode(',', $escaped);
}

@bgrgicak

bgrgicak commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator Author

@adamziel let's address the wp-now issue in a separate PR.

@adamziel

adamziel commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator

Yup, it's not a blocker at all here, I was just exploring. I isolated the problem and started a new issue here: WordPress/sqlite-database-integration#74

@adamziel
adamziel merged commit b40c8fb into trunk Feb 7, 2024
@adamziel
adamziel deleted the add/playground-plugin branch February 7, 2024 12:25
@adamziel

adamziel commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator

This is looking really good. Thank you @bgrgicak!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants