@wp-playground/wordpress 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +74 -19
  2. package/package.json +7 -6
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import{joinPaths as r,phpVar as d}from"@php-wasm/util";import{unzipFile as l}from"@wp-playground/common";import{PHPRequestHandler as c,withPHPIniValues as f,PHP as g,setPhpIniEntries as w,writeFiles as m,proxyFileSystem as h,rotatePHPRuntime as $}from"@php-wasm/universal";async function v(e){async function a(n,s){const o=new g(await e.createPhpRuntime());return e.sapiName&&o.setSapiName(e.sapiName),n&&(o.requestHandler=n),e.phpIniEntries&&w(o,e.phpIniEntries),s?(await L(o),await m(o,"/",e.createFiles||{}),await R(o,r(new URL(e.siteUrl).pathname,"phpinfo.php"))):h(await n.getPrimaryPhp(),o,["/tmp",n.documentRoot,"/internal/shared"]),e.spawnHandler&&await o.setSpawnHandler(e.spawnHandler(n.processManager)),$({php:o,cwd:n.documentRoot,recreateRuntime:e.createPhpRuntime,maxRequests:400}),o}const t=new c({phpFactory:async({isPrimary:n})=>a(t,n),documentRoot:e.documentRoot||"/wordpress",absoluteUrl:e.siteUrl,rewriteRules:E,getFileNotFoundAction:e.getFileNotFoundAction??P}),i=await t.getPrimaryPhp();if(e.hooks?.beforeWordPressFiles&&await e.hooks.beforeWordPressFiles(i),e.wordPressZip&&await T(i,await e.wordPressZip),e.constants)for(const n in e.constants)i.defineConstant(n,e.constants[n]);if(i.defineConstant("WP_HOME",e.siteUrl),i.defineConstant("WP_SITEURL",e.siteUrl),e.hooks?.beforeDatabaseSetup&&await e.hooks.beforeDatabaseSetup(i),e.sqliteIntegrationPluginZip&&await S(i,await e.sqliteIntegrationPluginZip),await _(i)||await y(i),!await _(i))throw new Error("WordPress installation has failed.");return t}async function _(e){return(await e.run({code:`<?php
1
+ import{joinPaths as a,phpVar as d}from"@php-wasm/util";import{unzipFile as l}from"@wp-playground/common";import{PHPRequestHandler as _,withPHPIniValues as f,PHP as g,setPhpIniEntries as w,writeFiles as m,proxyFileSystem as h,rotatePHPRuntime as $}from"@php-wasm/universal";import{logger as y}from"@php-wasm/logger";async function x(e){async function n(r,s){const o=new g(await e.createPhpRuntime());return e.sapiName&&o.setSapiName(e.sapiName),r&&(o.requestHandler=r),e.phpIniEntries&&w(o,e.phpIniEntries),s?(await S(o),await m(o,"/",e.createFiles||{}),await T(o,a(new URL(e.siteUrl).pathname,"phpinfo.php"))):h(await r.getPrimaryPhp(),o,["/tmp",r.documentRoot,"/internal/shared"]),e.spawnHandler&&await o.setSpawnHandler(e.spawnHandler(r.processManager)),$({php:o,cwd:r.documentRoot,recreateRuntime:e.createPhpRuntime,maxRequests:400}),o}const t=new _({phpFactory:async({isPrimary:r})=>n(t,r),documentRoot:e.documentRoot||"/wordpress",absoluteUrl:e.siteUrl,rewriteRules:R,getFileNotFoundAction:e.getFileNotFoundAction??b}),i=await t.getPrimaryPhp();if(e.hooks?.beforeWordPressFiles&&await e.hooks.beforeWordPressFiles(i),e.wordPressZip&&await I(i,await e.wordPressZip),e.constants)for(const r in e.constants)i.defineConstant(r,e.constants[r]);if(i.defineConstant("WP_HOME",e.siteUrl),i.defineConstant("WP_SITEURL",e.siteUrl),e.hooks?.beforeDatabaseSetup&&await e.hooks.beforeDatabaseSetup(i),e.sqliteIntegrationPluginZip&&await L(i,await e.sqliteIntegrationPluginZip),await c(i)||await P(i),!await c(i))throw new Error("WordPress installation has failed.");return t}async function c(e){return(await e.run({code:`<?php
2
2
  $wp_load = getenv('DOCUMENT_ROOT') . '/wp-load.php';
3
3
  if (!file_exists($wp_load)) {
4
4
  echo '0';
@@ -6,10 +6,22 @@ if (!file_exists($wp_load)) {
6
6
  }
7
7
  require $wp_load;
8
8
  echo is_blog_installed() ? '1' : '0';
9
- `,env:{DOCUMENT_ROOT:e.documentRoot}})).text==="1"}async function y(e){await f(e,{disable_functions:"fsockopen",allow_url_fopen:"0"},async()=>await e.request({url:"/wp-admin/install.php?step=2",method:"POST",body:{language:"en",prefix:"wp_",weblog_title:"My WordPress Website",user_name:"admin",admin_password:"password",admin_password2:"password",Submit:"Install WordPress",pw_weak:"1",admin_email:"admin@localhost.com"}}))}function P(e){return{type:"internal-redirect",uri:"/index.php"}}async function W(e){const i=(await(await e.getPrimaryPhp()).run({code:`<?php
9
+ `,env:{DOCUMENT_ROOT:e.documentRoot}})).text==="1"}async function P(e){await f(e,{disable_functions:"fsockopen",allow_url_fopen:"0"},async()=>await e.request({url:"/wp-admin/install.php?step=2",method:"POST",body:{language:"en",prefix:"wp_",weblog_title:"My WordPress Website",user_name:"admin",admin_password:"password",admin_password2:"password",Submit:"Install WordPress",pw_weak:"1",admin_email:"admin@localhost.com"}})),(await e.run({code:`<?php
10
+ $wp_load = getenv('DOCUMENT_ROOT') . '/wp-load.php';
11
+ if (!file_exists($wp_load)) {
12
+ echo '0';
13
+ exit;
14
+ }
15
+ require $wp_load;
16
+ $option_result = update_option(
17
+ 'permalink_structure',
18
+ '/%year%/%monthnum%/%day%/%postname%/'
19
+ );
20
+ echo $option_result ? '1' : '0';
21
+ `,env:{DOCUMENT_ROOT:e.documentRoot}})).text!=="1"&&y.warn("Failed to default to pretty permalinks after WP install.")}function b(e){return{type:"internal-redirect",uri:"/index.php"}}async function W(e){const i=(await(await e.getPrimaryPhp()).run({code:`<?php
10
22
  require '${e.documentRoot}/wp-includes/version.php';
11
23
  echo $wp_version;
12
- `})).text;if(!i)throw new Error("Unable to read loaded WordPress version.");return b(i)}function b(e){if(/-(alpha|beta|RC)\d*-\d+$/.test(e))return"nightly";if(/-(beta|RC)\d*$/.test(e))return"beta";const i=e.match(/^(\d+\.\d+)(?:\.\d+)?$/);return i!==null?i[1]:e}const E=[{match:/^\/(.*?)(\/wp-(content|admin|includes)\/.*)/g,replacement:"$2"}];async function L(e){await e.mkdir("/internal/shared/mu-plugins"),await e.writeFile("/internal/shared/preload/env.php",`<?php
24
+ `})).text;if(!i)throw new Error("Unable to read loaded WordPress version.");return E(i)}function E(e){if(/-(alpha|beta|RC)\d*-\d+$/.test(e))return"nightly";if(/-(beta|RC)\d*$/.test(e))return"beta";const i=e.match(/^(\d+\.\d+)(?:\.\d+)?$/);return i!==null?i[1]:e}const R=[{match:/^\/(.*?)(\/wp-(content|admin|includes)\/.*)/g,replacement:"$2"}];async function S(e){await e.mkdir("/internal/shared/mu-plugins"),await e.writeFile("/internal/shared/preload/env.php",`<?php
13
25
 
14
26
  // Allow adding filters/actions prior to loading WordPress.
15
27
  // $function_to_add MUST be a string.
@@ -57,14 +69,30 @@ echo is_blog_installed() ? '1' : '0';
57
69
  * playground_force_auto_login_as_user GET parameter.
58
70
  */
59
71
  if ( defined('PLAYGROUND_FORCE_AUTO_LOGIN_ENABLED') && isset($_GET['playground_force_auto_login_as_user']) ) {
60
- return esc_attr($_GET['playground_force_auto_login_as_user']);
72
+ return $_GET['playground_force_auto_login_as_user'];
61
73
  }
62
74
  return false;
63
75
  }
76
+
64
77
  /**
65
78
  * Logs the user in on their first visit if the Playground runtime told us to.
66
79
  */
67
80
  function playground_auto_login() {
81
+ /**
82
+ * The redirect should only run if the current PHP request is
83
+ * a HTTP request. If it's a PHP CLI run, we can't login the user
84
+ * because logins require cookies which aren't available in the CLI.
85
+ *
86
+ * Currently all Playground requests use the "cli" SAPI name
87
+ * to ensure support for WP-CLI, so the best way to distinguish
88
+ * between a CLI run and an HTTP request is by checking if the
89
+ * $_SERVER['REQUEST_URI'] global is set.
90
+ *
91
+ * If $_SERVER['REQUEST_URI'] is not set, we assume it's a CLI run.
92
+ */
93
+ if (empty($_SERVER['REQUEST_URI'])) {
94
+ return;
95
+ }
68
96
  $user_name = playground_get_username_for_auto_login();
69
97
  if ( false === $user_name ) {
70
98
  return;
@@ -79,27 +107,42 @@ echo is_blog_installed() ? '1' : '0';
79
107
  if (!$user) {
80
108
  return;
81
109
  }
110
+ /**
111
+ * This approach is described in a comment on
112
+ * https://developer.wordpress.org/reference/functions/wp_set_current_user/
113
+ */
82
114
  wp_set_current_user( $user->ID, $user->user_login );
83
115
  wp_set_auth_cookie( $user->ID );
84
116
  do_action( 'wp_login', $user->user_login, $user );
85
117
  setcookie('playground_auto_login_already_happened', '1');
86
- }
87
118
 
119
+ /**
120
+ * Reload page to ensure the user is logged in correctly.
121
+ * WordPress uses cookies to determine if the user is logged in,
122
+ * so we need to reload the page to ensure the cookies are set.
123
+ *
124
+ * Both WordPress home url and REQUEST_URI may include the site scope
125
+ * subdirectory.
126
+ * To prevent the redirect from including two scopes, we need to
127
+ * remove the scope from the REQUEST_URI if it's present.
128
+ */
129
+ $redirect_url = $_SERVER['REQUEST_URI'];
130
+ if (strpos($redirect_url, '/scope:') === 0) {
131
+ $parts = explode('/', $redirect_url);
132
+ $redirect_url = '/' . implode('/', array_slice($parts, 2));
133
+ }
134
+ wp_redirect(
135
+ home_url($redirect_url),
136
+ 302
137
+ );
138
+ exit;
139
+ }
88
140
  /**
89
141
  * Autologin users from the wp-login.php page.
90
142
  *
91
143
  * The wp hook isn't triggered on
92
144
  **/
93
- add_action('init', function() {
94
- playground_auto_login();
95
- /**
96
- * Check if the request is for the login page.
97
- */
98
- if (is_login() && is_user_logged_in() && isset($_GET['redirect_to'])) {
99
- wp_redirect(esc_url($_GET['redirect_to']));
100
- exit;
101
- }
102
- }, 1);
145
+ add_action('init', 'playground_auto_login', 1);
103
146
 
104
147
  /**
105
148
  * Disable the Site Admin Email Verification Screen for any session started
@@ -152,6 +195,18 @@ echo is_blog_installed() ? '1' : '0';
152
195
  if (strpos($message, "fit in a PHP integer") !== false) {
153
196
  return;
154
197
  }
198
+ /**
199
+ * Networking support in Playground registers a http_api_transports filter.
200
+ *
201
+ * This filter is deprecated, and no longer actively used, but is needed for wp_http_supports().
202
+ * @see https://core.trac.wordpress.org/ticket/37708
203
+ */
204
+ if (
205
+ strpos($message, "http_api_transports") !== false &&
206
+ strpos($message, "since version 6.4.0 with no alternative available") !== false
207
+ ) {
208
+ return;
209
+ }
155
210
  /**
156
211
  * Playground defines some constants upfront, and some of them may be redefined
157
212
  * in wp-config.php. For example, SITE_URL or WP_DEBUG. This is expected and
@@ -180,13 +235,13 @@ echo is_blog_installed() ? '1' : '0';
180
235
  }
181
236
  return false;
182
237
  });
183
- })();`)}async function R(e,a="/phpinfo.php"){await e.writeFile("/internal/shared/preload/phpinfo.php",`<?php
238
+ })();`)}async function T(e,n="/phpinfo.php"){await e.writeFile("/internal/shared/preload/phpinfo.php",`<?php
184
239
  // Render PHPInfo if the requested page is /phpinfo.php
185
- if ( ${d(a)} === $_SERVER['REQUEST_URI'] ) {
240
+ if ( ${d(n)} === $_SERVER['REQUEST_URI'] ) {
186
241
  phpinfo();
187
242
  exit;
188
243
  }
189
- `)}async function S(e,a){await e.isDir("/tmp/sqlite-database-integration")&&await e.rmdir("/tmp/sqlite-database-integration",{recursive:!0}),await e.mkdir("/tmp/sqlite-database-integration"),await l(e,a,"/tmp/sqlite-database-integration");const t="/internal/shared/sqlite-database-integration",i=await e.isDir("/tmp/sqlite-database-integration/sqlite-database-integration-main")?"/tmp/sqlite-database-integration/sqlite-database-integration-main":"/tmp/sqlite-database-integration/sqlite-database-integration-develop";await e.mv(i,t),await e.defineConstant("SQLITE_MAIN_FILE","1");const s=(await e.readFileAsText(r(t,"db.copy"))).replace("'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",d(t)).replace("'{SQLITE_PLUGIN}'",d(r(t,"load.php"))),o=r(await e.documentRoot,"wp-content/db.php"),u=`<?php
244
+ `)}async function L(e,n){await e.isDir("/tmp/sqlite-database-integration")&&await e.rmdir("/tmp/sqlite-database-integration",{recursive:!0}),await e.mkdir("/tmp/sqlite-database-integration"),await l(e,n,"/tmp/sqlite-database-integration");const t="/internal/shared/sqlite-database-integration",i=await e.isDir("/tmp/sqlite-database-integration/sqlite-database-integration-main")?"/tmp/sqlite-database-integration/sqlite-database-integration-main":"/tmp/sqlite-database-integration/sqlite-database-integration-develop";await e.mv(i,t),await e.defineConstant("SQLITE_MAIN_FILE","1");const s=(await e.readFileAsText(a(t,"db.copy"))).replace("'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",d(t)).replace("'{SQLITE_PLUGIN}'",d(a(t,"load.php"))),o=a(await e.documentRoot,"wp-content/db.php"),u=`<?php
190
245
  // Do not preload this if WordPress comes with a custom db.php file.
191
246
  if(file_exists(${d(o)})) {
192
247
  return;
@@ -263,4 +318,4 @@ if(!function_exists('mysqli_connect')) {
263
318
  var_dump(isset($wpdb));
264
319
  die("SQLite integration not loaded " . get_class($wpdb));
265
320
  }
266
- `)}async function T(e,a){e.mkdir("/tmp/unzipped-wordpress"),await l(e,a,"/tmp/unzipped-wordpress"),e.fileExists("/tmp/unzipped-wordpress/wordpress.zip")&&await l(e,"/tmp/unzipped-wordpress/wordpress.zip","/tmp/unzipped-wordpress");let t=e.fileExists("/tmp/unzipped-wordpress/wordpress")?"/tmp/unzipped-wordpress/wordpress":e.fileExists("/tmp/unzipped-wordpress/build")?"/tmp/unzipped-wordpress/build":"/tmp/unzipped-wordpress";if(!e.fileExists(r(t,"wp-config-sample.php"))){const i=e.listFiles(t);if(i.length){const n=i[0];e.fileExists(r(t,n,"wp-config-sample.php"))&&(t=r(t,n))}}if(e.isDir(e.documentRoot)&&I(e.documentRoot,e)){for(const i of e.listFiles(t)){const n=r(t,i),s=r(e.documentRoot,i);e.mv(n,s)}e.rmdir(t,{recursive:!0})}else e.mv(t,e.documentRoot);!e.fileExists(r(e.documentRoot,"wp-config.php"))&&e.fileExists(r(e.documentRoot,"wp-config-sample.php"))&&e.writeFile(r(e.documentRoot,"wp-config.php"),e.readFileAsText(r(e.documentRoot,"/wp-config-sample.php")))}function I(e,a){const t=a.listFiles(e);return t.length===0||t.length===1&&t[0]==="playground-site-metadata.json"}export{v as bootWordPress,P as getFileNotFoundActionForWordPress,W as getLoadedWordPressVersion,R as preloadPhpInfoRoute,S as preloadSqliteIntegration,L as setupPlatformLevelMuPlugins,T as unzipWordPress,b as versionStringToLoadedWordPressVersion,E as wordPressRewriteRules};
321
+ `)}async function I(e,n){e.mkdir("/tmp/unzipped-wordpress"),await l(e,n,"/tmp/unzipped-wordpress"),e.fileExists("/tmp/unzipped-wordpress/wordpress.zip")&&await l(e,"/tmp/unzipped-wordpress/wordpress.zip","/tmp/unzipped-wordpress");let t=e.fileExists("/tmp/unzipped-wordpress/wordpress")?"/tmp/unzipped-wordpress/wordpress":e.fileExists("/tmp/unzipped-wordpress/build")?"/tmp/unzipped-wordpress/build":"/tmp/unzipped-wordpress";if(!e.fileExists(a(t,"wp-config-sample.php"))){const i=e.listFiles(t);if(i.length){const r=i[0];e.fileExists(a(t,r,"wp-config-sample.php"))&&(t=a(t,r))}}if(e.isDir(e.documentRoot)&&U(e.documentRoot,e)){for(const i of e.listFiles(t)){const r=a(t,i),s=a(e.documentRoot,i);e.mv(r,s)}e.rmdir(t,{recursive:!0})}else e.mv(t,e.documentRoot);!e.fileExists(a(e.documentRoot,"wp-config.php"))&&e.fileExists(a(e.documentRoot,"wp-config-sample.php"))&&e.writeFile(a(e.documentRoot,"wp-config.php"),e.readFileAsText(a(e.documentRoot,"/wp-config-sample.php")))}function U(e,n){const t=n.listFiles(e);return t.length===0||t.length===1&&t[0]==="playground-site-metadata.json"}export{x as bootWordPress,b as getFileNotFoundActionForWordPress,W as getLoadedWordPressVersion,T as preloadPhpInfoRoute,L as preloadSqliteIntegration,S as setupPlatformLevelMuPlugins,I as unzipWordPress,E as versionStringToLoadedWordPressVersion,R as wordPressRewriteRules};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-playground/wordpress",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "description": "WordPress-related plumbing for WordPress Playground",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  "access": "public",
29
29
  "directory": "../../../dist/packages/playground/wordpress"
30
30
  },
31
- "gitHead": "42c2ce8cbea58d728498a05ba332ff0c6f6dbda0",
31
+ "gitHead": "bdb4afc2df1bf187103909a7d74552775e6bb8a3",
32
32
  "dependencies": {
33
33
  "comlink": "^4.4.1",
34
34
  "events": "3.3.0",
@@ -37,9 +37,10 @@
37
37
  "wasm-feature-detect": "1.8.0",
38
38
  "ws": "8.18.0",
39
39
  "yargs": "17.7.2",
40
- "@php-wasm/universal": "1.0.3",
41
- "@php-wasm/util": "1.0.3",
42
- "@wp-playground/common": "1.0.3",
43
- "@php-wasm/node": "1.0.3"
40
+ "@php-wasm/universal": "1.0.6",
41
+ "@php-wasm/util": "1.0.6",
42
+ "@php-wasm/logger": "1.0.6",
43
+ "@wp-playground/common": "1.0.6",
44
+ "@php-wasm/node": "1.0.6"
44
45
  }
45
46
  }