@wp-playground/wordpress
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import { FileTree, PHP, PHPProcessManager, PHPRequestHandler, SpawnHandler } from '../../../php-wasm/universal/src/index.ts'; | ||
import { FileNotFoundAction, FileNotFoundGetActionCallback, FileTree, PHP, PHPProcessManager, PHPRequestHandler, SpawnHandler } from '../../../php-wasm/universal/src/index.ts'; | ||
export type PhpIniOptions = Record<string, string>; | ||
@@ -64,2 +64,7 @@ export type Hook = (php: PHP) => void | Promise<void>; | ||
createFiles?: FileTree; | ||
/** | ||
* A callback that decides how to handle a file-not-found condition for a | ||
* given request URI. | ||
*/ | ||
getFileNotFoundAction?: FileNotFoundGetActionCallback; | ||
} | ||
@@ -82,1 +87,2 @@ /** | ||
export declare function bootWordPress(options: BootOptions): Promise<PHPRequestHandler>; | ||
export declare function getFileNotFoundActionForWordPress(relativeUri: string): FileNotFoundAction; |
import { PHP, UniversalPHP } from '../../../php-wasm/universal/src/index.ts'; | ||
export { bootWordPress } from './boot'; | ||
export { bootWordPress, getFileNotFoundActionForWordPress } from './boot'; | ||
export { getLoadedWordPressVersion } from './version-detect'; | ||
export * from './version-detect'; | ||
@@ -4,0 +5,0 @@ export * from './rewrite-rules'; |
26
index.js
@@ -1,8 +0,8 @@ | ||
import{joinPaths as s,phpVar as o}from"@php-wasm/util";import{unzipFile as l}from"@wp-playground/common";import{PHPRequestHandler as c,withPHPIniValues as f,PHP as w,setPhpIniEntries as _,writeFiles as g,proxyFileSystem as m,rotatePHPRuntime as h}from"@php-wasm/universal";async function T(e){async function r(i,d){const n=new w(await e.createPhpRuntime());return e.sapiName&&n.setSapiName(e.sapiName),i&&(n.requestHandler=i),e.phpIniEntries&&_(n,e.phpIniEntries),d?(await y(n),await g(n,"/",e.createFiles||{}),await L(n,s(new URL(e.siteUrl).pathname,"phpinfo.php"))):m(await i.getPrimaryPhp(),n,["/tmp",i.documentRoot,"/internal/shared"]),e.spawnHandler&&await n.setSpawnHandler(e.spawnHandler(i.processManager)),h({php:n,cwd:i.documentRoot,recreateRuntime:e.createPhpRuntime,maxRequests:400}),n}const a=new c({phpFactory:async({isPrimary:i})=>r(a,i),documentRoot:e.documentRoot||"/wordpress",absoluteUrl:e.siteUrl,rewriteRules:b}),t=await a.getPrimaryPhp();if(e.hooks?.beforeWordPressFiles&&await e.hooks.beforeWordPressFiles(t),e.wordPressZip&&await R(t,await e.wordPressZip),e.constants)for(const i in e.constants)t.defineConstant(i,e.constants[i]);if(t.defineConstant("WP_HOME",e.siteUrl),t.defineConstant("WP_SITEURL",e.siteUrl),e.hooks?.beforeDatabaseSetup&&await e.hooks.beforeDatabaseSetup(t),e.sqliteIntegrationPluginZip&&await E(t,await e.sqliteIntegrationPluginZip),await u(t)||await $(t),!await u(t))throw new Error("WordPress installation has failed.");return a}async function u(e){return(await e.run({code:`<?php | ||
import{joinPaths as o,phpVar as s}from"@php-wasm/util";import{unzipFile as l}from"@wp-playground/common";import{PHPRequestHandler as c,withPHPIniValues as f,PHP as w,setPhpIniEntries as g,writeFiles as _,proxyFileSystem as m,rotatePHPRuntime as h}from"@php-wasm/universal";async function T(e){async function r(i,d){const n=new w(await e.createPhpRuntime());return e.sapiName&&n.setSapiName(e.sapiName),i&&(n.requestHandler=i),e.phpIniEntries&&g(n,e.phpIniEntries),d?(await L(n),await _(n,"/",e.createFiles||{}),await E(n,o(new URL(e.siteUrl).pathname,"phpinfo.php"))):m(await i.getPrimaryPhp(),n,["/tmp",i.documentRoot,"/internal/shared"]),e.spawnHandler&&await n.setSpawnHandler(e.spawnHandler(i.processManager)),h({php:n,cwd:i.documentRoot,recreateRuntime:e.createPhpRuntime,maxRequests:400}),n}const a=new c({phpFactory:async({isPrimary:i})=>r(a,i),documentRoot:e.documentRoot||"/wordpress",absoluteUrl:e.siteUrl,rewriteRules:y,getFileNotFoundAction:e.getFileNotFoundAction??P}),t=await a.getPrimaryPhp();if(e.hooks?.beforeWordPressFiles&&await e.hooks.beforeWordPressFiles(t),e.wordPressZip&&await R(t,await e.wordPressZip),e.constants)for(const i in e.constants)t.defineConstant(i,e.constants[i]);if(t.defineConstant("WP_HOME",e.siteUrl),t.defineConstant("WP_SITEURL",e.siteUrl),e.hooks?.beforeDatabaseSetup&&await e.hooks.beforeDatabaseSetup(t),e.sqliteIntegrationPluginZip&&await S(t,await e.sqliteIntegrationPluginZip),await u(t)||await $(t),!await u(t))throw new Error("WordPress installation has failed.");return a}async function u(e){return(await e.run({code:`<?php | ||
require '${e.documentRoot}/wp-load.php'; | ||
echo is_blog_installed() ? '1' : '0'; | ||
`})).text==="1"}async function $(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"}}))}async function x(e){const t=(await(await e.getPrimaryPhp()).run({code:`<?php | ||
`})).text==="1"}async function $(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 x(e){const t=(await(await e.getPrimaryPhp()).run({code:`<?php | ||
require '${e.documentRoot}/wp-includes/version.php'; | ||
echo $wp_version; | ||
`})).text;if(!t)throw new Error("Unable to read loaded WordPress version.");return P(t)}function P(e){if(/-(alpha|beta|RC)\d*-\d+$/.test(e))return"nightly";if(/-(beta|RC)\d*$/.test(e))return"beta";const t=e.match(/^(\d+\.\d+)(?:\.\d+)?$/);return t!==null?t[1]:e}const b=[{match:/^\/(.*?)(\/wp-(content|admin|includes)\/.*)/g,replacement:"$2"}];async function y(e){await e.mkdir("/internal/shared/mu-plugins"),await e.writeFile("/internal/shared/preload/env.php",`<?php | ||
`})).text;if(!t)throw new Error("Unable to read loaded WordPress version.");return b(t)}function b(e){if(/-(alpha|beta|RC)\d*-\d+$/.test(e))return"nightly";if(/-(beta|RC)\d*$/.test(e))return"beta";const t=e.match(/^(\d+\.\d+)(?:\.\d+)?$/);return t!==null?t[1]:e}const y=[{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 | ||
@@ -35,10 +35,2 @@ // Allow adding filters/actions prior to loading WordPress. | ||
`),await e.writeFile("/internal/shared/mu-plugins/0-playground.php",`<?php | ||
// Redirect /wp-admin to /wp-admin/ | ||
add_filter( 'redirect_canonical', function( $redirect_url ) { | ||
if ( '/wp-admin' === $redirect_url ) { | ||
return $redirect_url . '/'; | ||
} | ||
return $redirect_url; | ||
} ); | ||
// Needed because gethostbyname( 'wordpress.org' ) returns | ||
@@ -112,11 +104,11 @@ // a private network IP address for some reason. | ||
}); | ||
})();`)}async function L(e,r="/phpinfo.php"){await e.writeFile("/internal/shared/preload/phpinfo.php",`<?php | ||
})();`)}async function E(e,r="/phpinfo.php"){await e.writeFile("/internal/shared/preload/phpinfo.php",`<?php | ||
// Render PHPInfo if the requested page is /phpinfo.php | ||
if ( ${o(r)} === $_SERVER['REQUEST_URI'] ) { | ||
if ( ${s(r)} === $_SERVER['REQUEST_URI'] ) { | ||
phpinfo(); | ||
exit; | ||
} | ||
`)}async function E(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 a="/internal/shared/sqlite-database-integration";await e.mv("/tmp/sqlite-database-integration/sqlite-database-integration-main",a),await e.defineConstant("SQLITE_MAIN_FILE","1");const i=(await e.readFileAsText(s(a,"db.copy"))).replace("'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",o(a)).replace("'{SQLITE_PLUGIN}'",o(s(a,"load.php"))),d=s(await e.documentRoot,"wp-content/db.php"),n=`<?php | ||
`)}async function S(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 a="/internal/shared/sqlite-database-integration";await e.mv("/tmp/sqlite-database-integration/sqlite-database-integration-main",a),await e.defineConstant("SQLITE_MAIN_FILE","1");const i=(await e.readFileAsText(o(a,"db.copy"))).replace("'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",s(a)).replace("'{SQLITE_PLUGIN}'",s(o(a,"load.php"))),d=o(await e.documentRoot,"wp-content/db.php"),n=`<?php | ||
// Do not preload this if WordPress comes with a custom db.php file. | ||
if(file_exists(${o(d)})) { | ||
if(file_exists(${s(d)})) { | ||
return; | ||
@@ -174,3 +166,3 @@ } | ||
protected function load_sqlite_integration() { | ||
require_once ${o(p)}; | ||
require_once ${s(p)}; | ||
} | ||
@@ -197,2 +189,2 @@ } | ||
} | ||
`)}async function R(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");const a=e.fileExists("/tmp/unzipped-wordpress/wordpress")?"/tmp/unzipped-wordpress/wordpress":e.fileExists("/tmp/unzipped-wordpress/build")?"/tmp/unzipped-wordpress/build":"/tmp/unzipped-wordpress";e.mv(a,e.documentRoot),!e.fileExists(s(e.documentRoot,"wp-config.php"))&&e.fileExists(s(e.documentRoot,"wp-config-sample.php"))&&e.writeFile(s(e.documentRoot,"wp-config.php"),e.readFileAsText(s(e.documentRoot,"/wp-config-sample.php")))}export{T as bootWordPress,x as getLoadedWordPressVersion,L as preloadPhpInfoRoute,E as preloadSqliteIntegration,y as setupPlatformLevelMuPlugins,R as unzipWordPress,P as versionStringToLoadedWordPressVersion,b as wordPressRewriteRules}; | ||
`)}async function R(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");const a=e.fileExists("/tmp/unzipped-wordpress/wordpress")?"/tmp/unzipped-wordpress/wordpress":e.fileExists("/tmp/unzipped-wordpress/build")?"/tmp/unzipped-wordpress/build":"/tmp/unzipped-wordpress";e.mv(a,e.documentRoot),!e.fileExists(o(e.documentRoot,"wp-config.php"))&&e.fileExists(o(e.documentRoot,"wp-config-sample.php"))&&e.writeFile(o(e.documentRoot,"wp-config.php"),e.readFileAsText(o(e.documentRoot,"/wp-config-sample.php")))}export{T as bootWordPress,P as getFileNotFoundActionForWordPress,x as getLoadedWordPressVersion,E as preloadPhpInfoRoute,S as preloadSqliteIntegration,L as setupPlatformLevelMuPlugins,R as unzipWordPress,b as versionStringToLoadedWordPressVersion,y as wordPressRewriteRules}; |
{ | ||
"name": "@wp-playground/wordpress", | ||
"version": "0.9.22", | ||
"version": "0.9.23", | ||
"description": "WordPress-related plumbing for WordPress Playground", | ||
@@ -31,3 +31,3 @@ "repository": { | ||
}, | ||
"gitHead": "aac882e3528d0de9d928e8630a01c03b15b1a14b", | ||
"gitHead": "254500c0c65525d3c5bfb61d04eb8ccdb5e8e5bf", | ||
"dependencies": { | ||
@@ -39,8 +39,8 @@ "comlink": "^4.4.1", | ||
"yargs": "17.7.2", | ||
"@php-wasm/universal": "0.9.22", | ||
"@php-wasm/util": "0.9.22", | ||
"@wp-playground/common": "0.9.22", | ||
"@wp-playground/wordpress-builds": "0.9.22", | ||
"@php-wasm/node": "0.9.22" | ||
"@php-wasm/universal": "0.9.23", | ||
"@php-wasm/util": "0.9.23", | ||
"@wp-playground/common": "0.9.23", | ||
"@wp-playground/wordpress-builds": "0.9.23", | ||
"@php-wasm/node": "0.9.23" | ||
} | ||
} |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
17778
1.76%318
0.32%1
-50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated
Updated