Skip to content

Commit 5126dba

Browse files
authored
Playground plugin: autologin fix, \n escaping, remove parent admin bar (#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
1 parent f3e719f commit 5126dba

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

packages/playground/assets/css/playground.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
.tools_page_playground #wpbody {
1515
position: initial;
1616
}
17+
.tools_page_playground #wpadminbar {
18+
display: none;
19+
}
1720

1821
#wp-playground-toolbar {
1922
background-color: #eaaa00;

packages/playground/assets/js/playground.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
path: '/wordpress/schema/_Schema.sql',
2727
},
2828
},
29-
{
30-
step: 'login',
31-
},
3229
],
3330
};
3431

@@ -53,9 +50,29 @@
5350

5451
await client.isReady();
5552

53+
// Login as the current user without a password
54+
await client.writeFile(
55+
'/wordpress/playground-login.php',
56+
`<?php
57+
require_once( dirname( __FILE__ ) . '/wp-load.php' );
58+
if ( is_user_logged_in() ) {
59+
return;
60+
}
61+
$user = get_user_by( 'id', ${playground.userId} );
62+
if( $user ) {
63+
wp_set_current_user( $user->ID, $user->user_login );
64+
wp_set_auth_cookie( $user->ID );
65+
do_action( 'wp_login', $user->user_login, $user );
66+
}`
67+
);
68+
await client.request({
69+
url: '/playground-login.php',
70+
});
71+
await client.unlink('/wordpress/playground-login.php');
72+
5673
if (playground.pluginSlug) {
57-
client.goTo('/wp-admin/plugins.php');
74+
await client.goTo('/wp-admin/plugins.php');
5875
} else {
59-
client.goTo('/wp-admin');
76+
await client.goTo('/wp-admin/');
6077
}
6178
})();

packages/playground/playground.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function enqueue_scripts($current_screen_id)
6262
),
6363
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
6464
'pluginSlug' => isset($_GET['pluginSlug']) ? $_GET['pluginSlug'] : false,
65+
'userId' => get_current_user_id(),
6566
]);
6667
wp_enqueue_script('playground');
6768
}

packages/playground/src/playground-db.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function escape_array($array)
1818
if (is_numeric($value)) {
1919
$escaped[] = $wpdb->prepare('%d', $value);
2020
} else {
21-
$escaped[] = $wpdb->prepare('%s', str_replace("\n", "\\n", $value));
21+
$escaped[] = $wpdb->prepare('%s', $value);
2222
}
2323
}
2424
return implode(',', $escaped);

0 commit comments

Comments
 (0)