Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect \n parsing. And missing adminbar/not loggedin #193

Closed
janw-me opened this issue Mar 17, 2024 · 13 comments · Fixed by #199
Closed

incorrect \n parsing. And missing adminbar/not loggedin #193

janw-me opened this issue Mar 17, 2024 · 13 comments · Fixed by #199
Assignees
Labels
Bug Something isn't working WordPress Plugin

Comments

@janw-me
Copy link

janw-me commented Mar 17, 2024

Screenshot from 2024-03-17 09-24-51

Viewed together with @adamziel yesterday at Cloudfest.

@adamziel adamziel added this to the Zero Crashes – Tools milestone Mar 19, 2024
@adamziel adamziel added Bug Something isn't working wp-now WordPress Plugin and removed wp-now labels Mar 19, 2024
@adamziel
Copy link
Collaborator

Ccing @bgrgicak

@bgrgicak bgrgicak self-assigned this Mar 20, 2024
@bgrgicak
Copy link
Collaborator

@janw-me would you be able to share more details about this issue?
I'm unable to recreate it.

@adamziel
Copy link
Collaborator

adamziel commented Mar 20, 2024

@bgrgicak we just clicked the "Sandbox site" on Janw's local blog after installing the Playground plugin from GitHub releases. I wonder if something is off with stringifying data to INSERT queries – perhaps newline code points sometimes come across as two characters \ and n ?

@bgrgicak
Copy link
Collaborator

Thanks! This already helps.

@bgrgicak
Copy link
Collaborator

I can recreate the incorrect \n parsing, but not the adminbar.

@janw-me are there any errors in the browser console when Playground is loaded? The missing adminbar makes me think that there is an error in the blueprint that prevents it from executing all steps (login is the last step).

@adamziel
Copy link
Collaborator

@bgrgicak could it be that a custom admin username or password would break the autologin process?

@bgrgicak
Copy link
Collaborator

@bgrgicak could it be that a custom admin username or password would break the autologin process?

Good catch! A custom password breaks login.

I can fix both issues.

@bgrgicak
Copy link
Collaborator

@adamziel I see two solutions for the login issue.

The first solution would be to add a user and password to Playground and use these credentials to login. It's simple to implement, but it modifies the DB data and could be hard to remove if we ever want to allow Playground to site syncing.

Alternatively, we could programmatically login the user. I explored this but wasn't able to get it to work. I assume there are some issues with the way I'm setting cookies.

Custom cookie example in packages/playground/assets/js/playground.js

	const response = await client.run({
		code: `<?php require_once '/wordpress/wp-load.php';
			echo json_encode(array(
				'value' => wp_generate_auth_cookie( ${playground.userId}, time() + ( 12 * HOUR_IN_SECONDS ) ),
				'name' => AUTH_COOKIE
			));`,
	});
	if (response.json) {
		document.cookie = `${response.json.name}=${response.json.value}`;
	}

Using WordPress functions example in packages/playground/assets/js/playground.js

	const response = await client.run({
		code: `<?php require_once '/wordpress/wp-load.php';
			$user = get_user_by( 'id', ${playground.userId} );
			if( $user ) {
				wp_clear_auth_cookie();
				wp_set_current_user( $user->ID );
				wp_set_auth_cookie( $user->ID, true, false );
				do_action( 'wp_login', $user->user_login );
			}`,
	});
@adamziel
Copy link
Collaborator

@bgrgicak client.run() doesn't go through the request handler where the cookies are stored. It could work as an mu-plugin, though

@bgrgicak
Copy link
Collaborator

I tried adding the mu-plugin using a blueprint, but it didn't work.

When I add the code to the 0-playground.php mu-plugin, login works.

	{
				step: 'writeFile',
				path: '/wordpress/wp-content/mu-plugins/0-login.php',
				data: `<?php
					add_action( 'init', function() {
						if ( is_user_logged_in() ) {
							return;
						}
						$user = get_user_by( 'id', ${playground.userId} );
						if( $user ) {
							wp_set_current_user( $user->ID, $user->user_login );
							wp_set_auth_cookie( $user->ID );
							do_action( 'wp_login', $user->user_login, $user );
						}
					} );
				`,
			},
@adamziel
Copy link
Collaborator

@bgrgicak maybe some error_log() statements would be helpful 😄

@janw-me
Copy link
Author

janw-me commented Mar 23, 2024

Looks like I'm late to the party. But just in case it helps, there are errors.
As said it's a local(by flywheel) site with user admin with pass admin.

I see 3 errors.

Error: Failed to log in as admin with password password
    login https://playground.wordpress.net/client/index.js:742
    o https://playground.wordpress.net/client/index.js:9386
    run https://playground.wordpress.net/client/index.js:9313
    runBlueprintSteps https://playground.wordpress.net/client/index.js:9421
    startPlaygroundWeb https://playground.wordpress.net/client/index.js:9903
    async* https://test.local/wp-content/plugins/playground/assets/js/playground.js?ver=6.4.3:48
    async* https://test.local/wp-content/plugins/playground/assets/js/playground.js?ver=6.4.3:61
[index.js:9316:27](https://playground.wordpress.net/client/index.js)
Uncaught (in promise) Error: Error when executing the blueprint step #2 ({"step":"login"}) : Failed to log in as admin with password password
    run https://playground.wordpress.net/client/index.js:9316
    runBlueprintSteps https://playground.wordpress.net/client/index.js:9421
    startPlaygroundWeb https://playground.wordpress.net/client/index.js:9903
    async* https://test.local/wp-content/plugins/playground/assets/js/playground.js?ver=6.4.3:48
    async* https://test.local/wp-content/plugins/playground/assets/js/playground.js?ver=6.4.3:61
Caused by: Error: Failed to log in as admin with password password
    login https://playground.wordpress.net/client/index.js:742
    o https://playground.wordpress.net/client/index.js:9386
    run https://playground.wordpress.net/client/index.js:9313
    runBlueprintSteps https://playground.wordpress.net/client/index.js:9421
    startPlaygroundWeb https://playground.wordpress.net/client/index.js:9903
    async* https://test.local/wp-content/plugins/playground/assets/js/playground.js?ver=6.4.3:48
    async* https://test.local/wp-content/plugins/playground/assets/js/playground.js?ver=6.4.3:61
[index.js:9316:37](https://playground.wordpress.net/client/index.js)
bgrgicak added a commit that referenced this issue Mar 26, 2024
#199)

Fixes #193

<!-- Thanks for contributing to WordPress Playground Tools! -->

## What?

This PR fixes a few issues reported in #193.
- Allows users to login if they don't use `admin/password` as login
credentials.
- Removes the parent admin bar from the sandbox page.
- Removes `\n` escaping from the DB exporter

## Why?

### Login

Users with custom usernames and passwords weren't automatically logged
into Playground because the credentials didn't match.

### Admin bar

We don't want the parent admin bar to be visible as it would be
confusing.

### \n

Initially, we decided to escape \n as a precaution, but it broke new
lines in post content and started displaying them as characters on the
page. Escaping isn't necessary and it breaks the export so we can remove
it.

## How?

### Login

By adding a mu-plugin using blueprints to programmatically login the
current user using their id.

### Admin bar

By hiding it with CSS on the Playground admin page.

### \n

By removing the escaping code.

## Testing Instructions

1. Check out the branch.
2. Install the plugin on a local WordPress site or use `wp-env`
3. On the local site update your password to be different from
_password_
4. Start a sandbox (wp-admin > Tools > Start Sandbox)
5. Confirm that the sandbox loaded and that you are logged in
6. If you are using the default WP theme open the Sample page by
clicking on the link in the header, if not open another page that has
multiple paragraphs with line breaks between them.
7. Confirm that there are no `\n` characters
@bgrgicak
Copy link
Collaborator

@janw-me we just released a new version of the Playground plugin that should fix your issues. Would you be able to update your plugin with the new version and let us know if everything works now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working WordPress Plugin
3 participants