@wp-playground/wordpress 1.0.4 → 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 +48 -17
  2. package/package.json +7 -7
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import{joinPaths as a,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";import{logger as y}from"@php-wasm/logger";async function U(e){async function r(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 S(o,a(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})=>r(t,n),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 O(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 T(i,await e.sqliteIntegrationPluginZip),await _(i)||await P(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';
@@ -21,7 +21,7 @@ echo $option_result ? '1' : '0';
21
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
22
22
  require '${e.documentRoot}/wp-includes/version.php';
23
23
  echo $wp_version;
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 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
25
25
 
26
26
  // Allow adding filters/actions prior to loading WordPress.
27
27
  // $function_to_add MUST be a string.
@@ -73,10 +73,26 @@ echo $option_result ? '1' : '0';
73
73
  }
74
74
  return false;
75
75
  }
76
+
76
77
  /**
77
78
  * Logs the user in on their first visit if the Playground runtime told us to.
78
79
  */
79
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
+ }
80
96
  $user_name = playground_get_username_for_auto_login();
81
97
  if ( false === $user_name ) {
82
98
  return;
@@ -91,27 +107,42 @@ echo $option_result ? '1' : '0';
91
107
  if (!$user) {
92
108
  return;
93
109
  }
110
+ /**
111
+ * This approach is described in a comment on
112
+ * https://developer.wordpress.org/reference/functions/wp_set_current_user/
113
+ */
94
114
  wp_set_current_user( $user->ID, $user->user_login );
95
115
  wp_set_auth_cookie( $user->ID );
96
116
  do_action( 'wp_login', $user->user_login, $user );
97
117
  setcookie('playground_auto_login_already_happened', '1');
98
- }
99
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
+ }
100
140
  /**
101
141
  * Autologin users from the wp-login.php page.
102
142
  *
103
143
  * The wp hook isn't triggered on
104
144
  **/
105
- add_action('init', function() {
106
- playground_auto_login();
107
- /**
108
- * Check if the request is for the login page.
109
- */
110
- if (is_login() && is_user_logged_in() && !empty($_GET['redirect_to'])) {
111
- wp_redirect($_GET['redirect_to']);
112
- exit;
113
- }
114
- }, 1);
145
+ add_action('init', 'playground_auto_login', 1);
115
146
 
116
147
  /**
117
148
  * Disable the Site Admin Email Verification Screen for any session started
@@ -204,13 +235,13 @@ echo $option_result ? '1' : '0';
204
235
  }
205
236
  return false;
206
237
  });
207
- })();`)}async function S(e,r="/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
208
239
  // Render PHPInfo if the requested page is /phpinfo.php
209
- if ( ${d(r)} === $_SERVER['REQUEST_URI'] ) {
240
+ if ( ${d(n)} === $_SERVER['REQUEST_URI'] ) {
210
241
  phpinfo();
211
242
  exit;
212
243
  }
213
- `)}async function T(e,r){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,r,"/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
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
214
245
  // Do not preload this if WordPress comes with a custom db.php file.
215
246
  if(file_exists(${d(o)})) {
216
247
  return;
@@ -287,4 +318,4 @@ if(!function_exists('mysqli_connect')) {
287
318
  var_dump(isset($wpdb));
288
319
  die("SQLite integration not loaded " . get_class($wpdb));
289
320
  }
290
- `)}async function O(e,r){e.mkdir("/tmp/unzipped-wordpress"),await l(e,r,"/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 n=i[0];e.fileExists(a(t,n,"wp-config-sample.php"))&&(t=a(t,n))}}if(e.isDir(e.documentRoot)&&I(e.documentRoot,e)){for(const i of e.listFiles(t)){const n=a(t,i),s=a(e.documentRoot,i);e.mv(n,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 I(e,r){const t=r.listFiles(e);return t.length===0||t.length===1&&t[0]==="playground-site-metadata.json"}export{U as bootWordPress,b as getFileNotFoundActionForWordPress,W as getLoadedWordPressVersion,S as preloadPhpInfoRoute,T as preloadSqliteIntegration,L as setupPlatformLevelMuPlugins,O as unzipWordPress,E as versionStringToLoadedWordPressVersion,R 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.4",
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": "fd76ee2ebbe3d3ff71cf19ab7fe010dca8bcc17b",
31
+ "gitHead": "bdb4afc2df1bf187103909a7d74552775e6bb8a3",
32
32
  "dependencies": {
33
33
  "comlink": "^4.4.1",
34
34
  "events": "3.3.0",
@@ -37,10 +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.4",
41
- "@php-wasm/util": "1.0.4",
42
- "@php-wasm/logger": "1.0.4",
43
- "@wp-playground/common": "1.0.4",
44
- "@php-wasm/node": "1.0.4"
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"
45
45
  }
46
46
  }